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 public void addLibset(LibrarySet libset
) {
67 throw noChildrenAllowed();
70 throw new NullPointerException("libset");
73 allLibraries
.add(libset
);
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");
81 public void addConfiguredArgument(UserDefineArgument arg
) {
83 throw noChildrenAllowed();
85 addConfiguredProcessorArg(arg
);
89 * Creates an include path.
91 public IncludePath
createIncludePath() {
92 Project p
= getProject();
94 throw noChildrenAllowed();
96 IncludePath path
= new IncludePath(p
);
97 includePaths
.addElement(path
);
102 * Add a <includepath> if specify the file attribute
107 * File with multiple path
108 * @throws BuildException
109 * if the specify file not exist
111 protected void loadFile(Vector activePath
, File file
) throws BuildException
{
112 FileReader fileReader
;
115 if (!file
.exists()) {
116 throw new BuildException("The file " + file
+ " is not existed");
119 fileReader
= new FileReader(file
);
120 in
= new BufferedReader(fileReader
);
121 while ((str
= in
.readLine()) != null) {
122 if (str
.trim().endsWith("")) {
125 str
= getProject().replaceProperties(str
);
126 activePath
.addElement(str
.trim());
128 } catch (Exception e
) {
129 throw new BuildException(e
.getMessage());
134 * Returns the specific include path.
136 * @return All active include paths
138 public String
[] getActiveIncludePaths() {
140 return ((UserDefineDef
) getCheckedRef(UserDefineDef
.class,
141 "UserDefineDef")).getActiveIncludePaths();
143 return getActivePaths(includePaths
);
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
]);
159 loadFile(activePaths
, path
.getFile());
163 String
[] pathNames
= new String
[activePaths
.size()];
164 activePaths
.copyInto(pathNames
);
169 * Get include path delimiter.
171 * @return Include Path Delimiter
173 public String
getIncludePathDelimiter() {
175 return ((UserDefineDef
) getCheckedRef(UserDefineDef
.class,
176 "UserDefineDef")).getIncludePathDelimiter();
178 return includePathDelimiter
;
182 * Set include path delimiter.
184 * @param includePathDelimiter
185 * include path delimiter
187 public void setIncludePathDelimiter(String includePathDelimiter
) {
189 throw tooManyAttributes();
191 this.includePathDelimiter
= includePathDelimiter
;
199 public String
getType() {
201 return ((UserDefineDef
) getCheckedRef(UserDefineDef
.class,
202 "UserDefineDef")).getType();
213 public void setType(String type
) {
215 throw tooManyAttributes();
220 public String
getCmd() {
224 public void setCmd(String cmd
) {
226 throw tooManyAttributes();
231 public String
getFamily() {
235 public void setFamily(String family
) {
237 throw tooManyAttributes();
239 this.family
= family
;
242 public String
getOutputFile() {
246 public void setOutputFile(String outputFile
) {
248 throw tooManyAttributes();
250 this.outputFile
= outputFile
;
253 public File
getWorkdir() {
257 public void setWorkdir(File workdir
) {
259 throw tooManyAttributes();
261 this.workdir
= workdir
;
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());
277 libs
.add(basedir
.getPath() + File
.separatorChar
278 + libFile
.getPath());
281 libs
.add(libStrArray
[i
]);
285 return (String
[]) libs
.toArray(new String
[libs
.size()]);
288 public String
getOutputDelimiter() {
289 return outputDelimiter
;
292 public void setOutputDelimiter(String outputDelimiter
) {
293 this.outputDelimiter
= outputDelimiter
;