KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > java > plugin > tools > ant > DocTask


1 /*****************************************************************************
2  * Java Plug-in Framework (JPF)
3  * Copyright (C) 2004 Dmitry Olshansky
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *****************************************************************************/

19 package org.java.plugin.tools.ant;
20
21 import java.io.BufferedReader JavaDoc;
22 import java.io.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStreamReader JavaDoc;
26 import java.io.Reader JavaDoc;
27
28 import org.apache.tools.ant.BuildException;
29 import org.java.plugin.tools.docgen.DocGenerator;
30
31
32 /**
33  * The Ant task to generate documentation from plug-in manifest.
34  * @version $Id: DocTask.java,v 1.3 2005/06/09 18:38:21 ddimon Exp $
35  */

36 public final class DocTask extends BaseJpfTask {
37     private File JavaDoc destDir;
38     private File JavaDoc overviewFile;
39     private String JavaDoc encoding;
40     private String JavaDoc docEncoding;
41     private String JavaDoc templatesPath;
42     private File JavaDoc stylesheetFile;
43
44     /**
45      * @param aDestDir base directory for generated documentation files
46      */

47     public void setDestDir(final File JavaDoc aDestDir) {
48         this.destDir = aDestDir;
49     }
50
51     /**
52      * @param anOverviewFile documentation overview HTML file
53      */

54     public void setOverview(final File JavaDoc anOverviewFile) {
55         this.overviewFile = anOverviewFile;
56     }
57     
58     /**
59      * @param anEncoding source files encoding name (templates, overview etc.)
60      */

61     public void setEncoding(final String JavaDoc anEncoding) {
62         this.encoding = anEncoding;
63     }
64
65     /**
66      * @param anEncoding output files encoding name
67      */

68     public void setDocEncoding(final String JavaDoc anEncoding) {
69         this.docEncoding = anEncoding;
70     }
71
72     /**
73      * @param aStylesheetFile CSS style sheet to use
74      */

75     public void setStylesheetFile(final File JavaDoc aStylesheetFile) {
76         this.stylesheetFile = aStylesheetFile;
77     }
78
79     /**
80      * @param aTemplatesPath path to template files
81      * (should be available in classpath)
82      */

83     public void setTemplates(final String JavaDoc aTemplatesPath) {
84         this.templatesPath = aTemplatesPath;
85     }
86     
87     /**
88      * @see org.apache.tools.ant.Task#execute()
89      */

90     public void execute() {
91         if (destDir == null) {
92             throw new BuildException("destdir attribute must be set!", //$NON-NLS-1$
93
getLocation());
94         }
95         if (!destDir.exists() && !destDir.mkdirs()) {
96             throw new BuildException("can't make " + destDir //$NON-NLS-1$
97
+ " folder", getLocation()); //$NON-NLS-1$
98
}
99         if (destDir.list().length != 0) {
100             throw new BuildException("directory " + destDir //$NON-NLS-1$
101
+ " is not empty", getLocation()); //$NON-NLS-1$
102
}
103         initRegistry(true);
104         log("Generating plug-ins documentation..."); //$NON-NLS-1$
105
try {
106             DocGenerator docGen;
107             if (templatesPath != null) {
108                 docGen = new DocGenerator(getRegistry(), getPathResolver(),
109                         templatesPath, encoding);
110             } else {
111                 docGen = new DocGenerator(getRegistry(), getPathResolver());
112             }
113             if (overviewFile != null) {
114                 docGen.setDocumentationOverview(getFileContent(overviewFile));
115             }
116             if (stylesheetFile != null) {
117                 docGen.setStylesheet(getFileContent(stylesheetFile));
118             }
119             if (docEncoding != null) {
120                 docGen.setOutputEncoding(docEncoding);
121             }
122             docGen.generate(destDir);
123             log("... documentation generated to folder " + destDir); //$NON-NLS-1$
124
} catch (Exception JavaDoc e) {
125             throw new BuildException(e);
126         }
127     }
128     
129     private String JavaDoc getFileContent(final File JavaDoc file) throws IOException JavaDoc {
130         Reader JavaDoc reader;
131         if (encoding != null) {
132             reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(
133                     new FileInputStream JavaDoc(file), encoding));
134         } else {
135             reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(
136                     new FileInputStream JavaDoc(file)));
137         }
138         try {
139             StringBuffer JavaDoc result = new StringBuffer JavaDoc();
140             char[] buf = new char[256];
141             int len;
142             while ((len = reader.read(buf)) != -1) {
143                 result.append(buf, 0, len);
144             }
145             return result.toString();
146         } finally {
147             reader.close();
148         }
149     }
150 }
151
Popular Tags