Restructuring for better separation of Tool packages.
[mirror_edk2.git] / Tools / Java / 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 private String dpath = null;
66
67 public void addLibset(LibrarySet libset) {
68 if (isReference()) {
69 throw noChildrenAllowed();
70 }
71 if (libset == null) {
72 throw new NullPointerException("libset");
73 }
74
75 allLibraries.add(libset);
76 }
77
78 public void execute() throws org.apache.tools.ant.BuildException {
79 throw new org.apache.tools.ant.BuildException(
80 "Not an actual task, but looks like one for documentation purposes");
81 }
82
83 public void addConfiguredArgument(UserDefineArgument arg) {
84 if (isReference()) {
85 throw noChildrenAllowed();
86 }
87 addConfiguredProcessorArg(arg);
88 }
89
90 /**
91 * Creates an include path.
92 */
93 public IncludePath createIncludePath() {
94 Project p = getProject();
95 if (isReference()) {
96 throw noChildrenAllowed();
97 }
98 IncludePath path = new IncludePath(p);
99 includePaths.addElement(path);
100 return path;
101 }
102
103 /**
104 * Add a <includepath> if specify the file attribute
105 *
106 * @param activePath
107 * Active Path Vector
108 * @param file
109 * File with multiple path
110 * @throws BuildException
111 * if the specify file not exist
112 */
113 protected void loadFile(Vector activePath, File file) throws BuildException {
114 FileReader fileReader;
115 BufferedReader in;
116 String str;
117 if (!file.exists()) {
118 throw new BuildException("The file " + file + " is not existed");
119 }
120 try {
121 fileReader = new FileReader(file);
122 in = new BufferedReader(fileReader);
123 while ((str = in.readLine()) != null) {
124 if (str.trim().endsWith("")) {
125 continue;
126 }
127 str = getProject().replaceProperties(str);
128 activePath.addElement(str.trim());
129 }
130 } catch (Exception e) {
131 throw new BuildException(e.getMessage());
132 }
133 }
134
135 /**
136 * Returns the specific include path.
137 *
138 * @return All active include paths
139 */
140 public String[] getActiveIncludePaths() {
141 if (isReference()) {
142 return ((UserDefineDef) getCheckedRef(UserDefineDef.class,
143 "UserDefineDef")).getActiveIncludePaths();
144 }
145 return getActivePaths(includePaths);
146 }
147
148 private String[] getActivePaths(Vector paths) {
149 Project p = getProject();
150 Vector activePaths = new Vector(paths.size());
151 int length = paths.size();
152 for (int i = 0; i < length; i++) {
153 ConditionalPath path = (ConditionalPath) paths.elementAt(i);
154 if (path.isActive(p)) {
155 if (path.getFile() == null) {
156 String[] pathEntries = path.list();
157 for (int j = 0; j < pathEntries.length; j++) {
158 activePaths.addElement(pathEntries[j]);
159 }
160 } else {
161 loadFile(activePaths, path.getFile());
162 }
163 }
164 }
165 String[] pathNames = new String[activePaths.size()];
166 activePaths.copyInto(pathNames);
167 return pathNames;
168 }
169
170 /**
171 * Get include path delimiter.
172 *
173 * @return Include Path Delimiter
174 */
175 public String getIncludePathDelimiter() {
176 if (isReference()) {
177 return ((UserDefineDef) getCheckedRef(UserDefineDef.class,
178 "UserDefineDef")).getIncludePathDelimiter();
179 }
180 return includePathDelimiter;
181 }
182
183 /**
184 * Set include path delimiter.
185 *
186 * @param includePathDelimiter
187 * include path delimiter
188 */
189 public void setIncludePathDelimiter(String includePathDelimiter) {
190 if (isReference()) {
191 throw tooManyAttributes();
192 }
193 this.includePathDelimiter = includePathDelimiter;
194 }
195
196 /**
197 * Get type.
198 *
199 * @return type
200 */
201 public String getType() {
202 if (isReference()) {
203 return ((UserDefineDef) getCheckedRef(UserDefineDef.class,
204 "UserDefineDef")).getType();
205 }
206 return type;
207 }
208
209 /**
210 * Set type.
211 *
212 * @param type
213 * Type
214 */
215 public void setType(String type) {
216 if (isReference()) {
217 throw tooManyAttributes();
218 }
219 this.type = type;
220 }
221
222 public String getCmd() {
223 return cmd;
224 }
225
226 public void setCmd(String cmd) {
227 if (isReference()) {
228 throw tooManyAttributes();
229 }
230 this.cmd = cmd;
231 }
232
233 public String getFamily() {
234 return family;
235 }
236
237 public void setFamily(String family) {
238 if (isReference()) {
239 throw tooManyAttributes();
240 }
241 this.family = family;
242 }
243
244 public String getOutputFile() {
245 return outputFile;
246 }
247
248 public void setOutputFile(String outputFile) {
249 if (isReference()) {
250 throw tooManyAttributes();
251 }
252 this.outputFile = outputFile;
253 }
254
255 public File getWorkdir() {
256 return workdir;
257 }
258
259 public void setWorkdir(File workdir) {
260 if (isReference()) {
261 throw tooManyAttributes();
262 }
263 this.workdir = workdir;
264 }
265
266 public String[] getLibset() {
267 Set libs = new LinkedHashSet();
268 Iterator iter = allLibraries.iterator();
269 while (iter.hasNext()) {
270 LibrarySet librarySet = (LibrarySet) iter.next();
271 File basedir = librarySet.getDir(getProject());
272 String[] libStrArray = librarySet.getLibs();
273 for (int i = 0; i < libStrArray.length; i++) {
274 if (basedir != null) {
275 File libFile = new File(libStrArray[i]);
276 if (libFile.isAbsolute()) {
277 libs.add(libFile.getPath());
278 } else {
279 libs.add(basedir.getPath() + File.separatorChar
280 + libFile.getPath());
281 }
282 } else {
283 libs.add(libStrArray[i]);
284 }
285 }
286 }
287 return (String[]) libs.toArray(new String[libs.size()]);
288 }
289
290 public String getOutputDelimiter() {
291 return outputDelimiter;
292 }
293
294 public void setOutputDelimiter(String outputDelimiter) {
295 this.outputDelimiter = outputDelimiter;
296 }
297
298 public String getDpath() {
299 return dpath;
300 }
301
302 public void setDpath(String dpath) {
303 this.dpath = dpath;
304 }
305
306 }