Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e5910c597156c1cecd2fe0bb20704662325d7def (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*******************************************************************************
 * Copyright (c) 2007, 2008 Symbian Software Limited and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Bala Torati (Symbian) - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.core.templateengine.process;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.templateengine.TemplateCore;
import org.eclipse.cdt.core.templateengine.TemplateEngine;
import org.eclipse.cdt.core.templateengine.TemplateEngineMessages;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.w3c.dom.Element;


/**
 * This class contains methods to get first process block element, next process
 * block element and checks for next process block element.
 */
public class Process {
	public static final String ELEM_TYPE = "type"; //$NON-NLS-1$

	private ProcessRunner processRunner;
	private ProcessArgument[] args;
	private TemplateCore template;
	private String id;
	private String processType;

	/**
	 * Constructor to create a process.
	 * @param template
	 * @param element
	 * @param id
	 */
	public Process(TemplateCore template, Element element, String id) {
		this.template = template;
		this.id = id;
		processType = element.getAttribute(ELEM_TYPE);
		processRunner = ProcessRunnerFactory.getDefault().getProcessRunner(processType);
		if (processRunner != null) {
			buildArgs(template, element);
		}
	}

	/**
	 * This method build the necessary Arguments for the process 
	 * @param templateCore
	 * @param element
	 */
	private void buildArgs(TemplateCore templateCore, Element element) {
		List<Element> children = TemplateEngine.getChildrenOfElement(element);
		ProcessParameter[] params = processRunner.getProcessParameters();
		List<ProcessArgument> list = new ArrayList<ProcessArgument>(params.length);
		int childIndex = 0;
		for(int i=0; i<params.length; i++) {
			ProcessParameter param = params[i];
			boolean childrenRemain = childIndex < children.size();
			Element child = (childrenRemain ? children.get(childIndex) : null);
			if (param.isExternal() && (!childrenRemain || !param.getName().equals(child.getAttribute(ProcessArgument.ELEM_NAME)))) {
				list.add(new ProcessArgument(templateCore, param));
			} else if (childrenRemain) {
				list.add(new ProcessArgument(templateCore, child));
				childIndex++;
			}
		}
		while (childIndex < children.size()) {
			list.add(new ProcessArgument(templateCore, children.get(childIndex++)));
		}
		args = list.toArray(new ProcessArgument[list.size()]);
	}

	/**
	 * 
	 * @return boolean, true if the Process is Ready.
	 */
	public boolean isReadyToProcess() {
		if (processRunner == null || !processRunner.areArgumentsMatchingRequiredParameters(args) || !areAllMacrosExpandable()) {
			return false;
		}
		return true;
	}
	
	/**
	 * 
	 * @return boolean, true if Macros are Exapandable.
	 */
	private boolean areAllMacrosExpandable() {
		if (args != null) {
			for(int i=0; i<args.length; i++) {
				ProcessArgument arg = args[i];
				if (!arg.areAllMacrosExpandable()) {
					return false;
				}
			}
		}
		return true;
	}
	
	/**
	 * Returns First NonExpandable Macro Message
	 */
	private String getFirstNonExpandableMacroMessage(ProcessArgument[] args2) {
		if (args != null) {
			String macro;
			for(int i=0; i<args.length; i++) {
				ProcessArgument arg = args[i];
				if ((macro = arg.getFirstNonExpandableMacro()) != null) {
					return TemplateEngineMessages.getString("Process.argument") + arg.getName() + TemplateEngineMessages.getString("Process.expandableMacro") + macro; //$NON-NLS-1$ //$NON-NLS-2$
				}
			}
		}
		return null;
	}
	
	/**
	 * Returns the Process Message depending on the parameters.
	 * @param code
	 * @param msg
	 * @return
	 */
	private String getProcessMessage(int code, String msg) {
		switch (code) {
			case IStatus.ERROR:
				return id + TemplateEngineMessages.getString("Process.error") + msg; //$NON-NLS-1$
			case IStatus.OK:
				return id + TemplateEngineMessages.getString("Process.success") + msg; //$NON-NLS-1$
			default:
				return id + TemplateEngineMessages.getString("Process.info") + msg; //$NON-NLS-1$
		}
	}
	
	/**
     * Executes this process
	 * @param monitor 
	 * @return the result of executing this process
	 * @throws ProcessFailureException
	 */
	public IStatus process(IProgressMonitor monitor) throws ProcessFailureException {
		if (processRunner == null) {
			throw new ProcessFailureException(TemplateEngineMessages.getString("Process.unknownProcess") + processType); //$NON-NLS-1$
		}
		if (!processRunner.areArgumentsMatchingRequiredParameters(args)) {
			throw new ProcessFailureException(processRunner.getArgumentsMismatchMessage(args));
		}
		if (!areAllMacrosExpandable()) {
			throw new ProcessFailureException(getProcessMessage(IStatus.ERROR, getFirstNonExpandableMacroMessage(args)));
		}
		resolve();
		processRunner.process(template, args, id, monitor);
		return new Status(IStatus.INFO, CCorePlugin.PLUGIN_ID, IStatus.OK, getProcessMessage(IStatus.OK, TemplateEngineMessages.getString("Process.executedSuccessfully") + Arrays.asList(args)), null); //$NON-NLS-1$
	}

	private void resolve() {
		if (args != null) {
			for(int i=0; i<args.length; i++) {
				ProcessArgument arg = args[i];
				if (!arg.isResolved()) {
					arg.resolve();
				}
			}
		}
	}

	/**
	 * @return the macros defined in the context of this process
	 */
	public Set<String> getMacros() {
		Set<String> set = null;
		if (args != null) {
			for(int i=0; i<args.length; i++) {
				ProcessArgument arg = args[i];
				Set<String> subSet = arg.getMacros();
				if (subSet != null) {
					if (set == null) {
						set = new HashSet<String>();
					}
					set.addAll(subSet);
				}
			}
		}
		return set;
	}
	
	@Override
	public String toString() {
		return id;
	}
}

Back to the top