KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ashkelon > ant > AshkelonTask


1 /**********************************************************************
2 Copyright (c) 1998 - 2003 Bob Hays and others.
3 All rights reserved. This program and the accompanying materials
4 are made available under the terms of the Common Public License v1.0
5 which accompanies this distribution, and is available at
6 http://www.eclipse.org/legal/cpl-v10.html
7
8 Contributors:
9     Bob Hays, Computer Geek
10 **********************************************************************/

11 package org.ashkelon.ant;
12
13 import java.io.File JavaDoc;
14 import java.io.FileReader JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Arrays JavaDoc;
17 import java.util.Collection JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20
21 import org.apache.tools.ant.BuildException;
22 import org.apache.tools.ant.taskdefs.Javadoc;
23 import org.apache.tools.ant.types.PatternSet;
24 import org.ashkelon.API;
25 import org.ashkelon.Ashkelon;
26 import org.ashkelon.util.Logger;
27
28 /**
29  * This is an ant task to run ashkelon, a JavaDoc-in-a-database toolkit.
30  *
31  * @author <a HREF="mailto:electricbob@alephnaught.com">Bob Hays, Computer Geek</a>
32  * @version 0.1
33  */

34 public class AshkelonTask extends Javadoc
35 {
36     /**
37      * Default constructor
38      */

39     public AshkelonTask()
40     {
41         super();
42     }
43
44     /**
45      * Overrides the execute() method in org.apache.tools.ant.Task to run ashkelon.
46      *
47      * @see org.apache.tools.ant.Task#execute()
48      */

49     public void execute() throws BuildException
50     {
51         Logger log = Logger.getInstance();
52         log.setPrefix(_PROGRAM_NAME);
53         if (_verbose)
54         {
55             log.setTraceLevel(Logger.VERBOSE);
56         }
57         if (_debug)
58         {
59             log.setTraceLevel(Logger.DEBUG);
60         }
61         try
62         {
63             _parameterCheck();
64             if (_operation.compareTo(_RESET_OPERATION) == 0)
65             {
66                 Ashkelon.resetCmd();
67             }
68             else if (_operation.compareTo(_LIST_OPERATION) == 0)
69             {
70                 // TODO: This isn't working right now
71
Ashkelon.listCmd();
72             }
73             else if (_operation.compareTo(_REMOVE_OPERATION) == 0)
74             {
75                 String JavaDoc[] includes =
76                     _includePatterns.getIncludePatterns(getProject());
77                 includes =
78                     _appendStringPrefixToArray(new String JavaDoc("remove"), includes);
79                 Ashkelon.removeCmd(includes);
80             }
81             else if (_operation.compareTo(_UPDATEREFS_OPERATION) == 0)
82             {
83                 Ashkelon.updateRefsCmd();
84             }
85             else if (_operation.compareTo(_ADD_OPERATION) == 0)
86             {
87                 // The next two lines won't work - javadoc must run in a separate thread, and
88
// the javadoc task in ant knows how to do that already.
89
// String[] args = {"-api", "@"+_descriptionFile.getCanonicalPath()};
90
// Ashkelon.addapiCmd(args);
91
//
92
// Load the api with our description file
93
FileReader JavaDoc reader = new FileReader JavaDoc(_descriptionFile);
94                 API api = new API();
95                 api = api.load(reader);
96                 reader.close();
97                 // Now get the package names we are to process so we can pass them along to the doclet
98
Collection JavaDoc packagenames = api.getPackagenames();
99                 Iterator JavaDoc iterator = packagenames.iterator();
100                 while (iterator.hasNext())
101                 {
102                     Object JavaDoc item = iterator.next();
103                     PackageName pn = new PackageName();
104                     pn.setName(item.toString());
105                     addPackage(pn);
106                 }
107                 // And finally execute our super class, the javadoc task for ant
108
super.execute();
109             }
110         }
111         catch (BuildException bex)
112         {
113             throw bex;
114         }
115         catch (Exception JavaDoc ex)
116         {
117             ex.printStackTrace(log.getWriter());
118             throw new BuildException(
119                 _PROGRAM_NAME + " task threw an exception",
120                 ex);
121         }
122     }
123
124     public void setApi(File JavaDoc f) throws BuildException
125     {
126         _descriptionFile = f;
127     }
128
129     public void setOperation(String JavaDoc op)
130     {
131         _operation = op;
132     }
133
134     public void setVerbose(boolean v)
135     {
136         _verbose = v;
137     }
138
139     public void setDebug(boolean d)
140     {
141         _debug = d;
142     }
143
144     /**
145      * Add a name entry on the include list
146      */

147     public PatternSet.NameEntry createInclude()
148     {
149         return _includePatterns.createInclude();
150     }
151
152     public PatternSet getIncludes()
153     {
154         return _includePatterns;
155     }
156     /**
157      * Tests that the operation attribute has a value.
158      *
159      * @return true indicates that the attribute has a value.
160      */

161     private boolean _isOperationSet()
162     {
163         return !(_operation == null);
164     }
165
166     /**
167      * Insert the specified string at the start of the array.
168      *
169      * @param aStr is the string to insert ahead in the array
170      * @param aArray is the target array
171      * @return the new String[] with the prepended item.
172      */

173     private String JavaDoc[] _appendStringPrefixToArray(String JavaDoc aStr, String JavaDoc[] aArray)
174     {
175         List JavaDoc al = new ArrayList JavaDoc(Arrays.asList(aArray));
176         al.add(0, new String JavaDoc(aStr));
177         String JavaDoc[] newArray = (String JavaDoc[]) al.toArray(new String JavaDoc[0]);
178         return newArray;
179     }
180
181     /**
182      * Tests that the descriptionfile attribute has a value.
183      *
184      * @return true indicates that the attribute has a value.
185      */

186     private boolean _isDescriptionFileSet()
187     {
188         return !(_descriptionFile == null);
189     }
190
191     /**
192      * Checks that all parameter inputs are valid before doing any real work.
193      *
194      * @throws BuildException
195      */

196     private void _parameterCheck() throws BuildException
197     {
198         // Check that operation attribute has an appropriate value
199
if (_isOperationSet())
200         {
201             boolean match = false;
202             for (int i = 0; i < _VALID_OPERATIONS.length; i++)
203             {
204                 if (_operation.compareTo(_VALID_OPERATIONS[i]) == 0)
205                 {
206                     match = true;
207                     break;
208                 }
209             }
210             if (!match)
211             {
212                 throw new BuildException(
213                     _PROGRAM_NAME
214                         + " task valid operations are "
215                         + _convertArrayToString(_VALID_OPERATIONS));
216             }
217         }
218         else
219         {
220             throw new BuildException(
221                 _PROGRAM_NAME
222                     + " task requires the operation attribute; valid operations are "
223                     + _convertArrayToString(_VALID_OPERATIONS));
224         }
225         if (_operation.compareTo(_ADD_OPERATION) == 0)
226         {
227             if (_isDescriptionFileSet())
228             {
229                 if (!_descriptionFile.exists())
230                 {
231                     throw new BuildException(
232                         _PROGRAM_NAME
233                             + " task cannot find api file "
234                             + _descriptionFile);
235                 }
236             }
237             else
238             {
239                 throw new BuildException(
240                     _PROGRAM_NAME
241                         + " task requires the api attribute, which is the XML file to describe the project");
242             }
243         }
244         else if (_operation.compareTo(_REMOVE_OPERATION) == 0)
245         {
246             String JavaDoc[] list = _includePatterns.getIncludePatterns(getProject());
247             if (list.length <= 0)
248             {
249                 throw new BuildException(
250                     _PROGRAM_NAME
251                         + " task operation remove requires an include list");
252             }
253         }
254     }
255
256     private String JavaDoc _convertArrayToString(String JavaDoc[] a)
257     {
258         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
259         for (int i = 0; i < a.length; i++)
260         {
261             if (i != 0)
262             {
263                 buffer.append(", ");
264             }
265             buffer.append(a[i]);
266         }
267         return buffer.toString();
268     }
269
270     private static final String JavaDoc _PROGRAM_NAME = "Ashkelon";
271     private static final String JavaDoc _ADD_OPERATION = "add";
272     private static final String JavaDoc _RESET_OPERATION = "reset";
273     private static final String JavaDoc _LIST_OPERATION = "list";
274     private static final String JavaDoc _REMOVE_OPERATION = "remove";
275     private static final String JavaDoc _UPDATEREFS_OPERATION = "updaterefs";
276
277     private static final String JavaDoc[] _VALID_OPERATIONS =
278         {
279             _RESET_OPERATION,
280             _LIST_OPERATION,
281             _REMOVE_OPERATION,
282             _ADD_OPERATION,
283             _UPDATEREFS_OPERATION };
284
285     private boolean _verbose = false;
286     private boolean _debug = false;
287     private String JavaDoc _operation = null;
288     private String JavaDoc _extdirs = null;
289     private File JavaDoc _descriptionFile = null;
290     private PatternSet _includePatterns = new PatternSet();
291 }
292
Popular Tags