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