3 * Copyright 2002-2006 The Ant-Contrib project
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package net
.sf
.antcontrib
.cpptasks
.userdefine
;
19 import java
.io
.BufferedReader
;
21 import java
.io
.FileReader
;
22 import java
.util
.Iterator
;
23 import java
.util
.LinkedHashSet
;
25 import java
.util
.Vector
;
27 import org
.apache
.tools
.ant
.BuildException
;
28 import org
.apache
.tools
.ant
.Project
;
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
;
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
42 public class UserDefineDef
extends ProcessorDef
{
44 public UserDefineDef () {
47 private String type
= "CC";
49 private String family
= "MSFT";
53 private String includePathDelimiter
;
55 private String outputDelimiter
;
59 private Vector includePaths
= new Vector();
61 private String outputFile
;
63 private Vector allLibraries
= new Vector();
65 private String dpath
= null;
67 private String libpath
= null;
69 private String include
= null;
71 public void addLibset(LibrarySet libset
) {
73 throw noChildrenAllowed();
76 throw new NullPointerException("libset");
79 allLibraries
.add(libset
);
82 public void execute() throws org
.apache
.tools
.ant
.BuildException
{
83 throw new org
.apache
.tools
.ant
.BuildException(
84 "Not an actual task, but looks like one for documentation purposes");
87 public void addConfiguredArgument(UserDefineArgument arg
) {
89 throw noChildrenAllowed();
91 addConfiguredProcessorArg(arg
);
95 * Creates an include path.
97 public IncludePath
createIncludePath() {
98 Project p
= getProject();
100 throw noChildrenAllowed();
102 IncludePath path
= new IncludePath(p
);
103 includePaths
.addElement(path
);
108 * Add a <includepath> if specify the file attribute
113 * File with multiple path
114 * @throws BuildException
115 * if the specify file not exist
117 protected void loadFile(Vector activePath
, File file
) throws BuildException
{
118 FileReader fileReader
;
121 if (!file
.exists()) {
122 throw new BuildException("The file " + file
+ " is not existed");
125 fileReader
= new FileReader(file
);
126 in
= new BufferedReader(fileReader
);
127 while ((str
= in
.readLine()) != null) {
128 if (str
.trim().endsWith("")) {
131 str
= getProject().replaceProperties(str
);
132 activePath
.addElement(str
.trim());
134 } catch (Exception e
) {
135 throw new BuildException(e
.getMessage());
140 * Returns the specific include path.
142 * @return All active include paths
144 public String
[] getActiveIncludePaths() {
146 return ((UserDefineDef
) getCheckedRef(UserDefineDef
.class,
147 "UserDefineDef")).getActiveIncludePaths();
149 return getActivePaths(includePaths
);
152 private String
[] getActivePaths(Vector paths
) {
153 Project p
= getProject();
154 Vector activePaths
= new Vector(paths
.size());
155 int length
= paths
.size();
156 for (int i
= 0; i
< length
; i
++) {
157 ConditionalPath path
= (ConditionalPath
) paths
.elementAt(i
);
158 if (path
.isActive(p
)) {
159 if (path
.getFile() == null) {
160 String
[] pathEntries
= path
.list();
161 for (int j
= 0; j
< pathEntries
.length
; j
++) {
162 activePaths
.addElement(pathEntries
[j
]);
165 loadFile(activePaths
, path
.getFile());
169 String
[] pathNames
= new String
[activePaths
.size()];
170 activePaths
.copyInto(pathNames
);
175 * Get include path delimiter.
177 * @return Include Path Delimiter
179 public String
getIncludePathDelimiter() {
181 return ((UserDefineDef
) getCheckedRef(UserDefineDef
.class,
182 "UserDefineDef")).getIncludePathDelimiter();
184 return includePathDelimiter
;
188 * Set include path delimiter.
190 * @param includePathDelimiter
191 * include path delimiter
193 public void setIncludePathDelimiter(String includePathDelimiter
) {
195 throw tooManyAttributes();
197 this.includePathDelimiter
= includePathDelimiter
;
205 public String
getType() {
207 return ((UserDefineDef
) getCheckedRef(UserDefineDef
.class,
208 "UserDefineDef")).getType();
219 public void setType(String type
) {
221 throw tooManyAttributes();
226 public String
getCmd() {
230 public void setCmd(String cmd
) {
232 throw tooManyAttributes();
234 if (cmd
== null || cmd
.trim().length() == 0) {
235 throw new BuildException("cmd attribute is empty!");
237 File cmdProgram
= new File(cmd
);
238 if (cmdProgram
.isDirectory()) {
239 throw new BuildException(cmd
+ " is not valid or executable!");
244 public String
getFamily() {
248 public void setFamily(String family
) {
250 throw tooManyAttributes();
252 this.family
= family
;
255 public String
getOutputFile() {
259 public void setOutputFile(String outputFile
) {
261 throw tooManyAttributes();
263 this.outputFile
= outputFile
;
266 public File
getWorkdir() {
270 public void setWorkdir(File workdir
) {
272 throw tooManyAttributes();
274 this.workdir
= workdir
;
277 public String
[] getLibset() {
278 Set libs
= new LinkedHashSet();
279 Iterator iter
= allLibraries
.iterator();
280 while (iter
.hasNext()) {
281 LibrarySet librarySet
= (LibrarySet
) iter
.next();
282 File basedir
= librarySet
.getDir(getProject());
283 String
[] libStrArray
= librarySet
.getLibs();
284 for (int i
= 0; i
< libStrArray
.length
; i
++) {
285 if (basedir
!= null) {
286 File libFile
= new File(libStrArray
[i
]);
287 if (libFile
.isAbsolute()) {
288 libs
.add(libFile
.getPath());
290 libs
.add(basedir
.getPath() + File
.separatorChar
291 + libFile
.getPath());
294 libs
.add(libStrArray
[i
]);
298 return (String
[]) libs
.toArray(new String
[libs
.size()]);
301 public String
getOutputDelimiter() {
302 return outputDelimiter
;
305 public void setOutputDelimiter(String outputDelimiter
) {
306 this.outputDelimiter
= outputDelimiter
;
309 public String
getDpath() {
313 public void setDpath(String dpath
) {
317 public String
getLibpath() {
321 public void setLibpath(String libpath
) {
322 this.libpath
= libpath
;
325 public String
getInclude() {
329 public void setInclude(String include
) {
330 this.include
= include
;