~ubuntu-branches/debian/sid/eclipse-cdt/sid

« back to all changes in this revision

Viewing changes to results/plugins/org.eclipse.cdt.core/templateengine/org/eclipse/cdt/core/templateengine/process/Process.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2011-10-06 21:15:04 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20111006211504-8dutmljjih0zikfv
Tags: 8.0.1-1
* New upstream release.
* Split the JNI packages into a separate architecture dependent
  package and made eclipse-cdt architecture independent.
* Install JNI libraries into multiarch aware location
* Bumped Standards-Version to 3.9.2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
7
 
 *
8
 
 * Contributors:
9
 
 * Bala Torati (Symbian) - Initial API and implementation
10
 
 *******************************************************************************/
11
 
package org.eclipse.cdt.core.templateengine.process;
12
 
 
13
 
import java.util.ArrayList;
14
 
import java.util.Arrays;
15
 
import java.util.HashSet;
16
 
import java.util.List;
17
 
import java.util.Set;
18
 
 
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;
27
 
 
28
 
 
29
 
/**
30
 
 * This class contains methods to get first process block element, next process
31
 
 * block element and checks for next process block element.
32
 
 */
33
 
public class Process {
34
 
        public static final String ELEM_TYPE = "type"; //$NON-NLS-1$
35
 
 
36
 
        private ProcessRunner processRunner;
37
 
        private ProcessArgument[] args;
38
 
        private TemplateCore template;
39
 
        private String id;
40
 
        private String processType;
41
 
 
42
 
        /**
43
 
         * Constructor to create a process.
44
 
         * @param template
45
 
         * @param element
46
 
         * @param id
47
 
         */
48
 
        public Process(TemplateCore template, Element element, String id) {
49
 
                this.template = template;
50
 
                this.id = id;
51
 
                processType = element.getAttribute(ELEM_TYPE);
52
 
                processRunner = ProcessRunnerFactory.getDefault().getProcessRunner(processType);
53
 
                if (processRunner != null) {
54
 
                        buildArgs(template, element);
55
 
                }
56
 
        }
57
 
 
58
 
        /**
59
 
         * This method build the necessary Arguments for the process 
60
 
         * @param templateCore
61
 
         * @param element
62
 
         */
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);
67
 
                int childIndex = 0;
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));
76
 
                                childIndex++;
77
 
                        }
78
 
                }
79
 
                while (childIndex < children.size()) {
80
 
                        list.add(new ProcessArgument(templateCore, children.get(childIndex++)));
81
 
                }
82
 
                args = list.toArray(new ProcessArgument[list.size()]);
83
 
        }
84
 
 
85
 
        /**
86
 
         * 
87
 
         * @return boolean, true if the Process is Ready.
88
 
         */
89
 
        public boolean isReadyToProcess() {
90
 
                if (processRunner == null || !processRunner.areArgumentsMatchingRequiredParameters(args) || !areAllMacrosExpandable()) {
91
 
                        return false;
92
 
                }
93
 
                return true;
94
 
        }
95
 
        
96
 
        /**
97
 
         * 
98
 
         * @return boolean, true if Macros are Exapandable.
99
 
         */
100
 
        private boolean areAllMacrosExpandable() {
101
 
                if (args != null) {
102
 
                        for(int i=0; i<args.length; i++) {
103
 
                                ProcessArgument arg = args[i];
104
 
                                if (!arg.areAllMacrosExpandable()) {
105
 
                                        return false;
106
 
                                }
107
 
                        }
108
 
                }
109
 
                return true;
110
 
        }
111
 
        
112
 
        /**
113
 
         * Returns First NonExpandable Macro Message
114
 
         */
115
 
        private String getFirstNonExpandableMacroMessage(ProcessArgument[] args2) {
116
 
                if (args != null) {
117
 
                        String macro;
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$
122
 
                                }
123
 
                        }
124
 
                }
125
 
                return null;
126
 
        }
127
 
        
128
 
        /**
129
 
         * Returns the Process Message depending on the parameters.
130
 
         * @param code
131
 
         * @param msg
132
 
         * @return
133
 
         */
134
 
        private String getProcessMessage(int code, String msg) {
135
 
                switch (code) {
136
 
                        case IStatus.ERROR:
137
 
                                return id + TemplateEngineMessages.getString("Process.error") + msg; //$NON-NLS-1$
138
 
                        case IStatus.OK:
139
 
                                return id + TemplateEngineMessages.getString("Process.success") + msg; //$NON-NLS-1$
140
 
                        default:
141
 
                                return id + TemplateEngineMessages.getString("Process.info") + msg; //$NON-NLS-1$
142
 
                }
143
 
        }
144
 
        
145
 
        /**
146
 
     * Executes this process
147
 
         * @param monitor 
148
 
         * @return the result of executing this process
149
 
         * @throws ProcessFailureException
150
 
         */
151
 
        public IStatus process(IProgressMonitor monitor) throws ProcessFailureException {
152
 
                if (processRunner == null) {
153
 
                        throw new ProcessFailureException(TemplateEngineMessages.getString("Process.unknownProcess") + processType); //$NON-NLS-1$
154
 
                }
155
 
                if (!processRunner.areArgumentsMatchingRequiredParameters(args)) {
156
 
                        throw new ProcessFailureException(processRunner.getArgumentsMismatchMessage(args));
157
 
                }
158
 
                if (!areAllMacrosExpandable()) {
159
 
                        throw new ProcessFailureException(getProcessMessage(IStatus.ERROR, getFirstNonExpandableMacroMessage(args)));
160
 
                }
161
 
                resolve();
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$
164
 
        }
165
 
 
166
 
        private void resolve() {
167
 
                if (args != null) {
168
 
                        for(int i=0; i<args.length; i++) {
169
 
                                ProcessArgument arg = args[i];
170
 
                                if (!arg.isResolved()) {
171
 
                                        arg.resolve();
172
 
                                }
173
 
                        }
174
 
                }
175
 
        }
176
 
 
177
 
        /**
178
 
         * @return the macros defined in the context of this process
179
 
         */
180
 
        public Set<String> getMacros() {
181
 
                Set<String> set = null;
182
 
                if (args != 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) {
187
 
                                        if (set == null) {
188
 
                                                set = new HashSet<String>();
189
 
                                        }
190
 
                                        set.addAll(subSet);
191
 
                                }
192
 
                        }
193
 
                }
194
 
                return set;
195
 
        }
196
 
        
197
 
        @Override
198
 
        public String toString() {
199
 
                return id;
200
 
        }
201
 
}