1
/*******************************************************************************
2
* Copyright (c) 2007, 2008 Symbian Software Limited and others.
3
* All rights reserved. This program and the accompanying materials
4
* are made available under the terms of the Eclipse Public License v1.0
5
* which accompanies this distribution, and is available at
6
* http://www.eclipse.org/legal/epl-v10.html
9
* Bala Torati (Symbian) - Initial API and implementation
10
*******************************************************************************/
11
package org.eclipse.cdt.core.templateengine.process;
13
import java.util.ArrayList;
14
import java.util.Arrays;
15
import java.util.HashSet;
16
import java.util.List;
19
import org.eclipse.cdt.core.CCorePlugin;
20
import org.eclipse.cdt.core.templateengine.TemplateCore;
21
import org.eclipse.cdt.core.templateengine.TemplateEngine;
22
import org.eclipse.cdt.core.templateengine.TemplateEngineMessages;
23
import org.eclipse.core.runtime.IProgressMonitor;
24
import org.eclipse.core.runtime.IStatus;
25
import org.eclipse.core.runtime.Status;
26
import org.w3c.dom.Element;
30
* This class contains methods to get first process block element, next process
31
* block element and checks for next process block element.
33
public class Process {
34
public static final String ELEM_TYPE = "type"; //$NON-NLS-1$
36
private ProcessRunner processRunner;
37
private ProcessArgument[] args;
38
private TemplateCore template;
40
private String processType;
43
* Constructor to create a process.
48
public Process(TemplateCore template, Element element, String id) {
49
this.template = template;
51
processType = element.getAttribute(ELEM_TYPE);
52
processRunner = ProcessRunnerFactory.getDefault().getProcessRunner(processType);
53
if (processRunner != null) {
54
buildArgs(template, element);
59
* This method build the necessary Arguments for the process
63
private void buildArgs(TemplateCore templateCore, Element element) {
64
List<Element> children = TemplateEngine.getChildrenOfElement(element);
65
ProcessParameter[] params = processRunner.getProcessParameters();
66
List<ProcessArgument> list = new ArrayList<ProcessArgument>(params.length);
68
for(int i=0; i<params.length; i++) {
69
ProcessParameter param = params[i];
70
boolean childrenRemain = childIndex < children.size();
71
Element child = (childrenRemain ? children.get(childIndex) : null);
72
if (param.isExternal() && (!childrenRemain || !param.getName().equals(child.getAttribute(ProcessArgument.ELEM_NAME)))) {
73
list.add(new ProcessArgument(templateCore, param));
74
} else if (childrenRemain) {
75
list.add(new ProcessArgument(templateCore, child));
79
while (childIndex < children.size()) {
80
list.add(new ProcessArgument(templateCore, children.get(childIndex++)));
82
args = list.toArray(new ProcessArgument[list.size()]);
87
* @return boolean, true if the Process is Ready.
89
public boolean isReadyToProcess() {
90
if (processRunner == null || !processRunner.areArgumentsMatchingRequiredParameters(args) || !areAllMacrosExpandable()) {
98
* @return boolean, true if Macros are Exapandable.
100
private boolean areAllMacrosExpandable() {
102
for(int i=0; i<args.length; i++) {
103
ProcessArgument arg = args[i];
104
if (!arg.areAllMacrosExpandable()) {
113
* Returns First NonExpandable Macro Message
115
private String getFirstNonExpandableMacroMessage(ProcessArgument[] args2) {
118
for(int i=0; i<args.length; i++) {
119
ProcessArgument arg = args[i];
120
if ((macro = arg.getFirstNonExpandableMacro()) != null) {
121
return TemplateEngineMessages.getString("Process.argument") + arg.getName() + TemplateEngineMessages.getString("Process.expandableMacro") + macro; //$NON-NLS-1$ //$NON-NLS-2$
129
* Returns the Process Message depending on the parameters.
134
private String getProcessMessage(int code, String msg) {
137
return id + TemplateEngineMessages.getString("Process.error") + msg; //$NON-NLS-1$
139
return id + TemplateEngineMessages.getString("Process.success") + msg; //$NON-NLS-1$
141
return id + TemplateEngineMessages.getString("Process.info") + msg; //$NON-NLS-1$
146
* Executes this process
148
* @return the result of executing this process
149
* @throws ProcessFailureException
151
public IStatus process(IProgressMonitor monitor) throws ProcessFailureException {
152
if (processRunner == null) {
153
throw new ProcessFailureException(TemplateEngineMessages.getString("Process.unknownProcess") + processType); //$NON-NLS-1$
155
if (!processRunner.areArgumentsMatchingRequiredParameters(args)) {
156
throw new ProcessFailureException(processRunner.getArgumentsMismatchMessage(args));
158
if (!areAllMacrosExpandable()) {
159
throw new ProcessFailureException(getProcessMessage(IStatus.ERROR, getFirstNonExpandableMacroMessage(args)));
162
processRunner.process(template, args, id, monitor);
163
return new Status(IStatus.INFO, CCorePlugin.PLUGIN_ID, IStatus.OK, getProcessMessage(IStatus.OK, TemplateEngineMessages.getString("Process.executedSuccessfully") + Arrays.asList(args)), null); //$NON-NLS-1$
166
private void resolve() {
168
for(int i=0; i<args.length; i++) {
169
ProcessArgument arg = args[i];
170
if (!arg.isResolved()) {
178
* @return the macros defined in the context of this process
180
public Set<String> getMacros() {
181
Set<String> set = null;
183
for(int i=0; i<args.length; i++) {
184
ProcessArgument arg = args[i];
185
Set<String> subSet = arg.getMacros();
186
if (subSet != null) {
188
set = new HashSet<String>();
198
public String toString() {