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 public void addLibset(LibrarySet libset
) {
69 throw noChildrenAllowed();
72 throw new NullPointerException("libset");
75 allLibraries
.add(libset
);
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");
83 public void addConfiguredArgument(UserDefineArgument arg
) {
85 throw noChildrenAllowed();
87 addConfiguredProcessorArg(arg
);
91 * Creates an include path.
93 public IncludePath
createIncludePath() {
94 Project p
= getProject();
96 throw noChildrenAllowed();
98 IncludePath path
= new IncludePath(p
);
99 includePaths
.addElement(path
);
104 * Add a <includepath> if specify the file attribute
109 * File with multiple path
110 * @throws BuildException
111 * if the specify file not exist
113 protected void loadFile(Vector activePath
, File file
) throws BuildException
{
114 FileReader fileReader
;
117 if (!file
.exists()) {
118 throw new BuildException("The file " + file
+ " is not existed");
121 fileReader
= new FileReader(file
);
122 in
= new BufferedReader(fileReader
);
123 while ((str
= in
.readLine()) != null) {
124 if (str
.trim().endsWith("")) {
127 str
= getProject().replaceProperties(str
);
128 activePath
.addElement(str
.trim());
130 } catch (Exception e
) {
131 throw new BuildException(e
.getMessage());
136 * Returns the specific include path.
138 * @return All active include paths
140 public String
[] getActiveIncludePaths() {
142 return ((UserDefineDef
) getCheckedRef(UserDefineDef
.class,
143 "UserDefineDef")).getActiveIncludePaths();
145 return getActivePaths(includePaths
);
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
]);
161 loadFile(activePaths
, path
.getFile());
165 String
[] pathNames
= new String
[activePaths
.size()];
166 activePaths
.copyInto(pathNames
);
171 * Get include path delimiter.
173 * @return Include Path Delimiter
175 public String
getIncludePathDelimiter() {
177 return ((UserDefineDef
) getCheckedRef(UserDefineDef
.class,
178 "UserDefineDef")).getIncludePathDelimiter();
180 return includePathDelimiter
;
184 * Set include path delimiter.
186 * @param includePathDelimiter
187 * include path delimiter
189 public void setIncludePathDelimiter(String includePathDelimiter
) {
191 throw tooManyAttributes();
193 this.includePathDelimiter
= includePathDelimiter
;
201 public String
getType() {
203 return ((UserDefineDef
) getCheckedRef(UserDefineDef
.class,
204 "UserDefineDef")).getType();
215 public void setType(String type
) {
217 throw tooManyAttributes();
222 public String
getCmd() {
226 public void setCmd(String cmd
) {
228 throw tooManyAttributes();
233 public String
getFamily() {
237 public void setFamily(String family
) {
239 throw tooManyAttributes();
241 this.family
= family
;
244 public String
getOutputFile() {
248 public void setOutputFile(String outputFile
) {
250 throw tooManyAttributes();
252 this.outputFile
= outputFile
;
255 public File
getWorkdir() {
259 public void setWorkdir(File workdir
) {
261 throw tooManyAttributes();
263 this.workdir
= workdir
;
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());
279 libs
.add(basedir
.getPath() + File
.separatorChar
280 + libFile
.getPath());
283 libs
.add(libStrArray
[i
]);
287 return (String
[]) libs
.toArray(new String
[libs
.size()]);
290 public String
getOutputDelimiter() {
291 return outputDelimiter
;
294 public void setOutputDelimiter(String outputDelimiter
) {
295 this.outputDelimiter
= outputDelimiter
;
298 public String
getDpath() {
302 public void setDpath(String dpath
) {