KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jibx > binding > ant > CompileTask


1 /*
2 Copyright (c) 2003, Andrew J. Glover
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7
8  * Redistributions of source code must retain the above copyright notice, this
9    list of conditions and the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice,
11    this list of conditions and the following disclaimer in the documentation
12    and/or other materials provided with the distribution.
13  * Neither the name of JiBX nor the names of its contributors may be used
14    to endorse or promote products derived from this software without specific
15    prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */

28 package org.jibx.binding.ant;
29
30 import java.io.File JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.List JavaDoc;
33
34 import org.apache.tools.ant.AntClassLoader;
35 import org.apache.tools.ant.BuildException;
36 import org.apache.tools.ant.DirectoryScanner;
37 import org.apache.tools.ant.Task;
38 import org.apache.tools.ant.types.FileSet;
39 import org.apache.tools.ant.types.Path;
40 import org.jibx.binding.Compile;
41 import org.jibx.runtime.JiBXException;
42
43 /**
44  * Class alters user defined class files for JiBX functionality.
45  * @author aglover
46  * @author pledbrook
47  */

48 public class CompileTask extends Task {
49     private boolean m_load;
50     private boolean m_verbose;
51     private Path m_classpath;
52     private List JavaDoc m_fileSet;
53     private List JavaDoc m_bindingFileSet;
54     private String JavaDoc m_bindingFile;
55     
56     /**
57      * Hook method called by ant framework to handle
58      * task initialization.
59      *
60      */

61     public void init() throws BuildException {
62         //
63
// Do aprent initialisation first.
64
//
65
super.init();
66         
67         //
68
// Now create the lists that store the classpathsets and
69
// the bindingsets.
70
//
71
m_fileSet = new ArrayList JavaDoc();
72         m_bindingFileSet = new ArrayList JavaDoc();
73     }
74     
75     /**
76      * Returns an array of paths of all the files specified by the
77      * <classpath> or <classpathset> tags. Note that the <classpath>
78      * and <classpathset> tags cannot both be specified.
79      */

80     private String JavaDoc[] getPaths() {
81         String JavaDoc[] pathArray = null;
82         if (m_classpath != null) {
83             //
84
// If a <classpath> tag has been set, m_classpath will
85
// not be null. In this case, just return the array of
86
// paths directly.
87
//
88
pathArray = m_classpath.list();
89         } else {
90             //
91
// Store the directory paths specified by each of the
92
// <classpathset> tags.
93
//
94
pathArray = new String JavaDoc[m_fileSet.size()];
95         
96             for(int i = 0; i < m_fileSet.size(); i++) {
97                 FileSet fileSet = (FileSet)m_fileSet.get(i);
98                 File JavaDoc directory = fileSet.getDir(project);
99                 pathArray[i] = directory.getAbsolutePath();
100             }
101         }
102         
103         // make Ant classpath usable by application
104
try {
105             System.setProperty("java.class.path",
106                 ((AntClassLoader)CompileTask.class.getClassLoader()).getClasspath());
107         } catch (Exception JavaDoc e) {
108             System.err.println("Failed setting classpath from Ant task");
109         }
110         
111         
112         //
113
// If verbose mode is on, print out the paths we are using.
114
//
115
if (m_verbose) {
116             log("Using the following paths:");
117             for (int i = 0; i < pathArray.length; i++) {
118                 log(" " + pathArray[i]);
119             }
120         }
121         
122         return pathArray;
123     }
124     
125     /**
126      * Returns an array of all the paths specified by the "binding"
127      * attribute or the <bindingfileset> tags.
128      */

129     private String JavaDoc[] getBindings() {
130         String JavaDoc[] bindings = null;
131         if (m_bindingFileSet.size() == 0) {
132             //
133
// No <bindingfileset> tags have been specified, so use
134
// the task's "binding" attribute instead.
135
//
136
bindings = new String JavaDoc[] { m_bindingFile };
137         } else {
138             //
139
// For each fileset, get all the file paths included by it.
140
//
141
ArrayList JavaDoc paths = new ArrayList JavaDoc();
142             for(int i = 0; i < m_bindingFileSet.size(); i++){
143                 FileSet bPath = (FileSet)m_bindingFileSet.get(i);
144                 
145                 DirectoryScanner dirScn = bPath.getDirectoryScanner(project);
146                 String JavaDoc[] bndingFiles = dirScn.getIncludedFiles();
147                 
148                 for(int x = 0; x < bndingFiles.length; x++){
149                     String JavaDoc fullPath = dirScn.getBasedir()
150                                       + System.getProperty("file.separator")
151                                       + bndingFiles[x];
152                     paths.add(fullPath);
153                 }
154             }
155             
156             //
157
// Convert the ArrayList of binding files into a native array.
158
//
159
bindings = (String JavaDoc[])paths.toArray(new String JavaDoc[paths.size()]);
160         }
161         
162         //
163
// If verbose mode is on, print out the binding files
164
// we are using.
165
//
166
if (m_verbose) {
167             log("Using the following binding paths:");
168             for (int i = 0; i < bindings.length; i++) {
169                 log(" " + bindings[i]);
170             }
171         }
172             
173         return bindings;
174     }
175     
176     /**
177      * This checks that the build file has specified the correct
178      * mix of &lt;classpath&gt;, &lt;classpathset&gt;, &lt;bindingfileset&gt;
179      * and "binding". The rules are as follows:
180      * <ul>
181      * <li>Either the "binding" attribute <b>or</b> &lt;bindingfileset&gt;
182      * tags must be set. Not both.</li>
183      * <li>Either a single &lt;classpath&gt; tag <b>or</b> multiple
184      * &lt;classpathset&gt; tags must be set. Not both.</li>
185      * </ul>
186      * @throws BuildException
187      */

188     private void validateRequiredFields() throws BuildException {
189         //
190
// Check at least one of the binding file properties is set.
191
//
192
if (m_bindingFileSet.isEmpty() && m_bindingFile == null) {
193             throw new BuildException("Either the binding attribute or at "
194                                      + "least one bindingset nested element "
195                                      + "must be defined.");
196         }
197         
198         //
199
// Make sure that the "binding" attribute has not been used with the
200
// <bindingset> nested element.
201
//
202
if (!m_bindingFileSet.isEmpty() && m_bindingFile != null) {
203             throw new BuildException("You cannot specify both a binding attribute and a "
204                                      + "bindingset nested element.");
205         }
206         
207         //
208
// Check either <classpath> or <classpathset> has been set.
209
//
210
if(m_classpath == null && m_fileSet.isEmpty()) {
211             throw new BuildException("You must specify either a classpath "
212                                      + "or at least one nested classpathset element.");
213         }
214         
215         //
216
// Make sure that only one of the above is specified.
217
//
218
if (m_classpath != null && !m_fileSet.isEmpty()) {
219             throw new BuildException("You cannot specify both a classpath and a "
220                                      + "classpathset nested element.");
221         }
222     }
223     
224     /**
225      *
226      * Hook method called by Ant. Method first determines
227      * class file path from user defined value, then
228      * determines all binding files for class file weaving.
229      *
230      */

231     public void execute() throws BuildException{
232         try{
233             validateRequiredFields();
234             
235             String JavaDoc[] pathArr = getPaths();
236             String JavaDoc[] bindings = getBindings();
237             
238             Compile compiler = new Compile();
239             
240             //right now there is an issue with dynamically loading
241
//class files with ant. it doesnt work unless the class
242
//files are part of ANT's classpath. so I am forcing this
243
//to always be false until we can come up with a better solution.
244
compiler.setLoad(m_load);
245             
246             compiler.setVerbose(m_verbose);
247             
248             compiler.compile(pathArr, bindings);
249                     
250         } catch(JiBXException jEx) {
251             jEx.printStackTrace();
252             throw new BuildException("JiBXException in JiBX binding compilation", jEx);
253         }
254     }
255     
256     /**
257      *
258      * @param fSet
259      */

260     public void addClassPathSet(FileSet fSet){
261         m_fileSet.add(fSet);
262     }
263     
264     /**
265      * Returns the current classpath.
266      */

267     public Path getClasspath() {
268         return m_classpath;
269     }
270     
271     /**
272      * Sets the classpath for this task. Multiple calls append the
273      * new classpath to the current one, rather than overwriting it.
274      * @param classpath The new classpath as a Path object.
275      */

276     public void setClasspath(Path classpath) {
277         if (m_classpath == null) {
278             m_classpath = classpath;
279         }
280         else {
281             m_classpath.append(classpath);
282         }
283     }
284     
285     /**
286      * Creates the classpath for this task and returns
287      * it. If the classpath has already been created,
288      * the method just returns that one.
289      */

290     public Path createClasspath() {
291         if (m_classpath == null) {
292             m_classpath = new Path(project);
293         }
294         
295         return m_classpath;
296     }
297
298     /**
299      * @param file
300      */

301     public void setBinding(String JavaDoc file) {
302         m_bindingFile = file;
303     }
304     
305     /**
306      *
307      * @param bfSet
308      */

309     public void addBindingFileSet(FileSet bfSet) {
310         m_bindingFileSet.add(bfSet);
311     }
312     
313     /**
314      * @param bool
315      */

316     public void setLoad(boolean bool) {
317         m_load = bool;
318     }
319     
320     /**
321      * @param bool
322      */

323     public void setVerbose(boolean bool) {
324         m_verbose = bool;
325     }
326 }
327
Popular Tags