bf045d1e58f2a91ab73beb846efad1b30105a5a3
[mirror_edk2.git] / Tools / Source / Cpptasks / net / sf / antcontrib / cpptasks / userdefine / UserDefineDef.java
1 /*
2 *
3 * Copyright 2002-2006 The Ant-Contrib project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package net.sf.antcontrib.cpptasks.userdefine;
18
19 import java.io.BufferedReader;
20 import java.io.File;
21 import java.io.FileReader;
22 import java.util.Iterator;
23 import java.util.LinkedHashSet;
24 import java.util.Set;
25 import java.util.Vector;
26
27 import org.apache.tools.ant.BuildException;
28 import org.apache.tools.ant.Project;
29
30 import net.sf.antcontrib.cpptasks.ProcessorDef;
31 import net.sf.antcontrib.cpptasks.types.ConditionalPath;
32 import net.sf.antcontrib.cpptasks.types.IncludePath;
33 import net.sf.antcontrib.cpptasks.types.LibrarySet;
34
35 /**
36 * A userdefinedef definition. userdefine elements may be placed either as
37 * children of a cc element or the project element. A userdefine element with an
38 * id attribute may be referenced by userdefine elements with refid or extends
39 * attributes.
40 *
41 */
42 public class UserDefineDef extends ProcessorDef {
43
44 public UserDefineDef () {
45 }
46
47 private String type = "CC";
48
49 private String family = "MSFT";
50
51 private String cmd;
52
53 private String includePathDelimiter;
54
55 private String outputDelimiter;
56
57 private File workdir;
58
59 private Vector includePaths = new Vector();
60
61 private String outputFile;
62
63 private Vector allLibraries = new Vector();
64
65 public void addLibset(LibrarySet libset) {
66 if (isReference()) {
67 throw noChildrenAllowed();
68 }
69 if (libset == null) {
70 throw new NullPointerException("libset");
71 }
72
73 allLibraries.add(libset);
74 }
75
76 public void execute() throws org.apache.tools.ant.BuildException {
77 throw new org.apache.tools.ant.BuildException(
78 "Not an actual task, but looks like one for documentation purposes");
79 }
80
81 public void addConfiguredArgument(UserDefineArgument arg) {
82 if (isReference()) {
83 throw noChildrenAllowed();
84 }
85 addConfiguredProcessorArg(arg);
86 }
87
88 /**
89 * Creates an include path.
90 */
91 public IncludePath createIncludePath() {
92 Project p = getProject();
93 if (isReference()) {
94 throw noChildrenAllowed();
95 }
96 IncludePath path = new IncludePath(p);
97 includePaths.addElement(path);
98 return path;
99 }
100
101 /**
102 * Add a <includepath> if specify the file attribute
103 *
104 * @param activePath
105 * Active Path Vector
106 * @param file
107 * File with multiple path
108 * @throws BuildException
109 * if the specify file not exist
110 */
111 protected void loadFile(Vector activePath, File file) throws BuildException {
112 FileReader fileReader;
113 BufferedReader in;
114 String str;
115 if (!file.exists()) {
116 throw new BuildException("The file " + file + " is not existed");
117 }
118 try {
119 fileReader = new FileReader(file);
120 in = new BufferedReader(fileReader);
121 while ((str = in.readLine()) != null) {
122 if (str.trim().endsWith("")) {
123 continue;
124 }
125 str = getProject().replaceProperties(str);
126 activePath.addElement(str.trim());
127 }
128 } catch (Exception e) {
129 throw new BuildException(e.getMessage());
130 }
131 }
132
133 /**
134 * Returns the specific include path.
135 *
136 * @return All active include paths
137 */
138 public String[] getActiveIncludePaths() {
139 if (isReference()) {
140 return ((UserDefineDef) getCheckedRef(UserDefineDef.class,
141 "UserDefineDef")).getActiveIncludePaths();
142 }
143 return getActivePaths(includePaths);
144 }
145
146 private String[] getActivePaths(Vector paths) {
147 Project p = getProject();
148 Vector activePaths = new Vector(paths.size());
149 int length = paths.size();
150 for (int i = 0; i < length; i++) {
151 ConditionalPath path = (ConditionalPath) paths.elementAt(i);
152 if (path.isActive(p)) {
153 if (path.getFile() == null) {
154 String[] pathEntries = path.list();
155 for (int j = 0; j < pathEntries.length; j++) {
156 activePaths.addElement(pathEntries[j]);
157 }
158 } else {
159 loadFile(activePaths, path.getFile());
160 }
161 }
162 }
163 String[] pathNames = new String[activePaths.size()];
164 activePaths.copyInto(pathNames);
165 return pathNames;
166 }
167
168 /**
169 * Get include path delimiter.
170 *
171 * @return Include Path Delimiter
172 */
173 public String getIncludePathDelimiter() {
174 if (isReference()) {
175 return ((UserDefineDef) getCheckedRef(UserDefineDef.class,
176 "UserDefineDef")).getIncludePathDelimiter();
177 }
178 return includePathDelimiter;
179 }
180
181 /**
182 * Set include path delimiter.
183 *
184 * @param includePathDelimiter
185 * include path delimiter
186 */
187 public void setIncludePathDelimiter(String includePathDelimiter) {
188 if (isReference()) {
189 throw tooManyAttributes();
190 }
191 this.includePathDelimiter = includePathDelimiter;
192 }
193
194 /**
195 * Get type.
196 *
197 * @return type
198 */
199 public String getType() {
200 if (isReference()) {
201 return ((UserDefineDef) getCheckedRef(UserDefineDef.class,
202 "UserDefineDef")).getType();
203 }
204 return type;
205 }
206
207 /**
208 * Set type.
209 *
210 * @param type
211 * Type
212 */
213 public void setType(String type) {
214 if (isReference()) {
215 throw tooManyAttributes();
216 }
217 this.type = type;
218 }
219
220 public String getCmd() {
221 return cmd;
222 }
223
224 public void setCmd(String cmd) {
225 if (isReference()) {
226 throw tooManyAttributes();
227 }
228 this.cmd = cmd;
229 }
230
231 public String getFamily() {
232 return family;
233 }
234
235 public void setFamily(String family) {
236 if (isReference()) {
237 throw tooManyAttributes();
238 }
239 this.family = family;
240 }
241
242 public String getOutputFile() {
243 return outputFile;
244 }
245
246 public void setOutputFile(String outputFile) {
247 if (isReference()) {
248 throw tooManyAttributes();
249 }
250 this.outputFile = outputFile;
251 }
252
253 public File getWorkdir() {
254 return workdir;
255 }
256
257 public void setWorkdir(File workdir) {
258 if (isReference()) {
259 throw tooManyAttributes();
260 }
261 this.workdir = workdir;
262 }
263
264 public String[] getLibset() {
265 Set libs = new LinkedHashSet();
266 Iterator iter = allLibraries.iterator();
267 while (iter.hasNext()) {
268 LibrarySet librarySet = (LibrarySet) iter.next();
269 File basedir = librarySet.getDir(getProject());
270 String[] libStrArray = librarySet.getLibs();
271 for (int i = 0; i < libStrArray.length; i++) {
272 if (basedir != null) {
273 File libFile = new File(libStrArray[i]);
274 if (libFile.isAbsolute()) {
275 libs.add(libFile.getPath());
276 } else {
277 libs.add(basedir.getPath() + File.separatorChar
278 + libFile.getPath());
279 }
280 } else {
281 libs.add(libStrArray[i]);
282 }
283 }
284 }
285 return (String[]) libs.toArray(new String[libs.size()]);
286 }
287
288 public String getOutputDelimiter() {
289 return outputDelimiter;
290 }
291
292 public void setOutputDelimiter(String outputDelimiter) {
293 this.outputDelimiter = outputDelimiter;
294 }
295
296 }