KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > java2html > anttasks > Java2HtmlTask


1 package de.java2html.anttasks;
2
3 import java.io.File JavaDoc;
4 import java.io.FileWriter JavaDoc;
5 import java.io.IOException JavaDoc;
6 import java.io.Writer JavaDoc;
7
8 import de.java2html.converter.IJavaSourceConverter;
9 import de.java2html.converter.JavaSourceConverterProvider;
10 import de.java2html.javasource.JavaSource;
11 import de.java2html.javasource.JavaSourceParser;
12 import de.java2html.options.ConversionOptionsUtilities;
13 import de.java2html.options.HorizontalAlignment;
14 import de.java2html.options.JavaSourceConversionOptions;
15 import de.java2html.options.JavaSourceStyleTable;
16 import de.java2html.util.IoUtilities;
17
18 import org.apache.tools.ant.BuildException;
19 import org.apache.tools.ant.DirectoryScanner;
20 import org.apache.tools.ant.Project;
21 import org.apache.tools.ant.taskdefs.MatchingTask;
22 import org.apache.tools.ant.util.FileNameMapper;
23 import org.apache.tools.ant.util.GlobPatternMapper;
24 import org.apache.tools.ant.util.SourceFileScanner;
25
26 /**
27  * Runs the java2html converter as a task inside the well known build tool
28  * "ant" (see ant.apache.org).
29  *
30  * Thanks to <a HREF="mailto:markus@jave.de">Markus Gebhard</a>, the author
31  * of java2html itself. I contribute this code to the project under the same
32  * license as java2html.
33  *
34  * For an example for a <code>build.xml</code> containing this task have a
35  * look at the docs/anttask/ folder.
36  *
37  * @author <a HREF="mailto:mbohlen@mbohlen.de">Matthias Bohlen</a>
38  * @author <a HREF="mailto:markus@jave.de">Markus Gebhard</a>
39  */

40 public class Java2HtmlTask extends MatchingTask {
41   private String JavaDoc style = JavaSourceConversionOptions.getDefault().getStyleTable().getName();
42   private File JavaDoc srcDir;
43   private File JavaDoc destDir;
44   private boolean overwrite = false;
45   private String JavaDoc outputFormat = JavaSourceConverterProvider.getDefaultConverterName();
46   private int tabs = JavaSourceConversionOptions.getDefault().getTabSize();
47   private boolean showLineNumbers = JavaSourceConversionOptions.getDefault().isShowLineNumbers();
48   private boolean showDefaultTitle = false;
49   private boolean addLineAnchors = false;
50   private String JavaDoc lineAnchorPrefix = "";
51   private boolean showTableBorder = false;
52   private boolean showFileName = false;
53   private boolean includeDocumentHeader = true;
54   private boolean includeDocumentFooter = true;
55   private boolean useShortFileName = false;
56   private String JavaDoc horizontalAlignment = JavaSourceConversionOptions
57       .getDefault().getHorizontalAlignment().getName();
58
59
60   /**
61    * Sets the directory where the Java sources are stored.
62    *
63    * @param srcDir
64    * directory name
65    */

66   public void setSrcDir(File JavaDoc srcDir) {
67     this.srcDir = srcDir;
68   }
69
70   /**
71    * Sets the directory where the output is written.
72    *
73    * @param destDir
74    * directory name
75    */

76   public void setDestDir(File JavaDoc destDir) {
77     this.destDir = destDir;
78   }
79
80   /**
81    * Sets the output format.
82    *
83    * @param outputFormat
84    * the output format identifier ("html", "xhtml", "latex")
85    */

86   public void setOutputFormat(String JavaDoc outputFormat) {
87     this.outputFormat = outputFormat;
88   }
89
90   /**
91    * @see org.apache.tools.ant.Task#execute()
92    */

93   public void execute() throws BuildException {
94     if (srcDir == null) {
95       // We directly change the user variable, because it
96
// shouldn't lead to problems
97
srcDir = project.resolveFile(".");
98     }
99
100     // find the files/directories
101
DirectoryScanner dirScanner = getDirectoryScanner(srcDir);
102
103     // get a list of files to work on
104
String JavaDoc[] allSourceFiles = dirScanner.getIncludedFiles();
105
106     IJavaSourceConverter converter = getConverter();
107     JavaSourceConversionOptions options = getConversionOptions();
108     SourceFileScanner sourceScanner = new SourceFileScanner(this);
109
110     String JavaDoc[] sourceFilesToProcess;
111     if (isOverwrite()) {
112       sourceFilesToProcess = allSourceFiles;
113     }
114     else {
115       FileNameMapper sourceToOutMapper = new GlobPatternMapper();
116       sourceToOutMapper.setFrom("*");
117       sourceToOutMapper.setTo("*." + converter.getMetaData().getDefaultFileExtension());
118       sourceFilesToProcess = sourceScanner.restrict(allSourceFiles, srcDir, destDir, sourceToOutMapper);
119     }
120
121     if (sourceFilesToProcess.length > 0) {
122       String JavaDoc files = (sourceFilesToProcess.length == 1 ? " file" : " files");
123       log("Converting " + sourceFilesToProcess.length + files, Project.MSG_INFO);
124     }
125
126     for (int i = 0; i < sourceFilesToProcess.length; ++i) {
127       process(sourceFilesToProcess[i], options, converter);
128     }
129   }
130
131   /**
132    * Returns a new conversions options object filled in from the Ant task.
133    *
134    * @return a new conversions options object
135    */

136   private JavaSourceConversionOptions getConversionOptions() {
137     JavaSourceConversionOptions options = JavaSourceConversionOptions.getDefault();
138     options.setTabSize(tabs);
139     options.setShowFileName(isShowFileName());
140     options.setShowTableBorder(isShowTableBorder());
141     options.setShowLineNumbers(isShowLineNumbers());
142     options.setAddLineAnchors(isAddLineAnchors());
143     options.setLineAnchorPrefix(lineAnchorPrefix);
144
145     JavaSourceStyleTable table = JavaSourceStyleTable.getPredefinedTable(style);
146     if (table == null) {
147       throw new BuildException("Specified style table '"
148           + style
149           + "' does not exist "
150           + " - valid values are: "
151           + ConversionOptionsUtilities.getPredefinedStyleTableNameString());
152     }
153     options.setStyleTable(table);
154
155     HorizontalAlignment alignment = HorizontalAlignment.getByName(horizontalAlignment);
156     if (alignment == null) {
157       throw new BuildException("Specified alignment '" //$NON-NLS-1$
158
+ horizontalAlignment
159           + "'does not exist - valid values are: " //$NON-NLS-1$
160
+ ConversionOptionsUtilities.getAvailableHorizontalAlignmentNames());
161     }
162     options.setHorizontalAlignment(alignment);
163
164     return options;
165   }
166
167   private IJavaSourceConverter getConverter() throws BuildException {
168     IJavaSourceConverter converter = JavaSourceConverterProvider.getJavaSourceConverterByName(outputFormat);
169     if (converter == null) {
170       throw new BuildException("unknown output file format: " + outputFormat); //$NON-NLS-1$
171
}
172     return converter;
173   }
174
175   /**
176    * Convert a Java source to HTML, XHTML or LaTex.
177    *
178    * @param sourcefileName
179    * the name of the file to convert
180    * @param options
181    * conversion options
182    * @param converter
183    * the converter to use
184    */

185   private void process(String JavaDoc sourcefileName, JavaSourceConversionOptions options, IJavaSourceConverter converter)
186       throws BuildException {
187     log("Converting '" + sourcefileName + "'", Project.MSG_VERBOSE); //$NON-NLS-1$ //$NON-NLS-2$
188
JavaSourceParser parser = new JavaSourceParser(options);
189     JavaSource source;
190     File JavaDoc inFile = new File JavaDoc(srcDir, sourcefileName);
191     try {
192       source = parser.parse(inFile);
193     }
194     catch (IOException JavaDoc e1) {
195       throw new BuildException("Unable to parse file " + inFile.getName(), e1); //$NON-NLS-1$
196
}
197
198     File JavaDoc outFile = createOutputFile(sourcefileName, converter);
199     ensureDirectoryFor(outFile);
200     Writer JavaDoc writer = null;
201     try {
202       writer = new FileWriter JavaDoc(outFile);
203     }
204     catch (Exception JavaDoc e) {
205       throw new BuildException("Error opening output file " + outFile.getName(), e); //$NON-NLS-1$
206
}
207
208     String JavaDoc title = ""; //$NON-NLS-1$
209
if (isShowDefaultTitle()) {
210       title = sourcefileName.replace('\\', '/');
211     }
212     try {
213       if (isIncludeDocumentHeader()) {
214         converter.writeDocumentHeader(writer, options, title);
215       }
216       converter.convert(source, options, writer);
217       if (isIncludeDocumentFooter()) {
218         converter.writeDocumentFooter(writer, options);
219       }
220     }
221     catch (Exception JavaDoc e) {
222       throw new BuildException("Error writing output to " + outFile.getName(), e); //$NON-NLS-1$
223
}
224     finally {
225       IoUtilities.close(writer);
226     }
227
228     log("Output: " + outFile, Project.MSG_VERBOSE); //$NON-NLS-1$
229
}
230
231   private File JavaDoc createOutputFile(String JavaDoc sourcefileName, IJavaSourceConverter converter) {
232     String JavaDoc fileNamePrefix = sourcefileName;
233     if (isUseShortFileName()) {
234       int index = sourcefileName.lastIndexOf('.');
235       if (index != -1) {
236         fileNamePrefix = sourcefileName.substring(0, index);
237       }
238     }
239     return new File JavaDoc(destDir, fileNamePrefix + "." + converter.getMetaData().getDefaultFileExtension());
240   }
241
242   /**
243    * Sets the number of spaces per tab.
244    *
245    * @param tabs
246    */

247   public void setTabs(int tabs) {
248     this.tabs = tabs;
249   }
250
251   /**
252    * Sets the table name for the output style, e.g. "kawa" or "eclipse".
253    *
254    * @see JavaSourceStyleTable
255    */

256   public void setStyle(String JavaDoc style) {
257     this.style = style;
258   }
259
260   /**
261    * Creates directories as needed.
262    *
263    * @param targetFile
264    * a <code>File</code> whose parent directories need to exist
265    * @exception BuildException
266    * if the parent directories couldn't be created
267    */

268   private void ensureDirectoryFor(File JavaDoc targetFile) throws BuildException {
269     File JavaDoc directory = new File JavaDoc(targetFile.getParent());
270     if (!directory.exists()) {
271       if (!directory.mkdirs()) {
272         throw new BuildException("Unable to create directory: " + directory.getAbsolutePath());
273       }
274     }
275   }
276
277   private boolean isShowFileName() {
278     return showFileName;
279   }
280
281   private boolean isShowLineNumbers() {
282     return showLineNumbers;
283   }
284
285   private boolean isShowDefaultTitle() {
286     return showDefaultTitle;
287   }
288
289   private boolean isShowTableBorder() {
290     return showTableBorder;
291   }
292
293   public void setShowFileName(boolean showFileName) {
294     this.showFileName = showFileName;
295   }
296
297   public void setShowLineNumbers(boolean showLineNumbers) {
298     this.showLineNumbers = showLineNumbers;
299   }
300
301   public void setShowDefaultTitle(boolean showDefaultTitle) {
302     this.showDefaultTitle = showDefaultTitle;
303   }
304
305   public void setShowTableBorder(boolean showTableBorder) {
306     this.showTableBorder = showTableBorder;
307   }
308
309   private boolean isIncludeDocumentFooter() {
310     return includeDocumentFooter;
311   }
312
313   private boolean isIncludeDocumentHeader() {
314     return includeDocumentHeader;
315   }
316
317   public void setIncludeDocumentFooter(boolean includeDocumentFooter) {
318     this.includeDocumentFooter = includeDocumentFooter;
319   }
320
321   public void setIncludeDocumentHeader(boolean includeDocumentHeader) {
322     this.includeDocumentHeader = includeDocumentHeader;
323   }
324
325   private boolean isAddLineAnchors() {
326     return addLineAnchors;
327   }
328
329   public void setAddLineAnchors(boolean addLineAnchors) {
330     this.addLineAnchors = addLineAnchors;
331   }
332
333   public void setLineAnchorPrefix(String JavaDoc string) {
334     lineAnchorPrefix = string;
335   }
336
337   public void setHorizontalAlignment(String JavaDoc horizontalAlignment) {
338     this.horizontalAlignment = horizontalAlignment;
339   }
340
341   private boolean isUseShortFileName() {
342     return useShortFileName;
343   }
344
345   public void setUseShortFileName(boolean useShortFileName) {
346     this.useShortFileName = useShortFileName;
347   }
348
349   private boolean isOverwrite() {
350     return overwrite;
351   }
352
353   public void setOverwrite(boolean overwrite) {
354     this.overwrite = overwrite;
355   }
356 }
Popular Tags