001: /*****************************************************************************
002: * Java Plug-in Framework (JPF)
003: * Copyright (C) 2004 Dmitry Olshansky
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *****************************************************************************/package org.java.plugin.tools.ant;
019:
020: import java.io.BufferedReader;
021: import java.io.File;
022: import java.io.FileInputStream;
023: import java.io.IOException;
024: import java.io.InputStreamReader;
025: import java.io.Reader;
026:
027: import org.apache.tools.ant.BuildException;
028: import org.java.plugin.tools.docgen.DocGenerator;
029:
030: /**
031: * The Ant task to generate documentation from plug-in manifest.
032: * @version $Id$
033: */
034: public final class DocTask extends BaseJpfTask {
035: private File destDir;
036: private File overviewFile;
037: private String encoding;
038: private String docEncoding;
039: private String templatesPath;
040: private File stylesheetFile;
041:
042: /**
043: * @param aDestDir base directory for generated documentation files
044: */
045: public void setDestDir(final File aDestDir) {
046: this .destDir = aDestDir;
047: }
048:
049: /**
050: * @param anOverviewFile documentation overview HTML file
051: */
052: public void setOverview(final File anOverviewFile) {
053: this .overviewFile = anOverviewFile;
054: }
055:
056: /**
057: * @param anEncoding source files encoding name (templates, overview etc.)
058: */
059: public void setEncoding(final String anEncoding) {
060: this .encoding = anEncoding;
061: }
062:
063: /**
064: * @param anEncoding output files encoding name
065: */
066: public void setDocEncoding(final String anEncoding) {
067: this .docEncoding = anEncoding;
068: }
069:
070: /**
071: * @param aStylesheetFile CSS style sheet to use
072: */
073: public void setStylesheetFile(final File aStylesheetFile) {
074: this .stylesheetFile = aStylesheetFile;
075: }
076:
077: /**
078: * @param aTemplatesPath path to template files
079: * (should be available in classpath)
080: */
081: public void setTemplates(final String aTemplatesPath) {
082: this .templatesPath = aTemplatesPath;
083: }
084:
085: /**
086: * @see org.apache.tools.ant.Task#execute()
087: */
088: @Override
089: public void execute() {
090: if (destDir == null) {
091: throw new BuildException("destdir attribute must be set!", //$NON-NLS-1$
092: getLocation());
093: }
094: if (!destDir.exists() && !destDir.mkdirs()) {
095: throw new BuildException("can't make " + destDir //$NON-NLS-1$
096: + " folder", getLocation()); //$NON-NLS-1$
097: }
098: if (destDir.list().length != 0) {
099: throw new BuildException("directory " + destDir //$NON-NLS-1$
100: + " is not empty", getLocation()); //$NON-NLS-1$
101: }
102: initRegistry(true);
103: try {
104: DocGenerator docGen;
105: if (templatesPath != null) {
106: docGen = new DocGenerator(getRegistry(),
107: getPathResolver(), templatesPath, encoding);
108: } else {
109: docGen = new DocGenerator(getRegistry(),
110: getPathResolver());
111: }
112: if (overviewFile != null) {
113: docGen
114: .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 e) {
125: throw new BuildException(e);
126: }
127: }
128:
129: private String getFileContent(final File file) throws IOException {
130: Reader reader;
131: if (encoding != null) {
132: reader = new BufferedReader(new InputStreamReader(
133: new FileInputStream(file), encoding));
134: } else {
135: reader = new BufferedReader(new InputStreamReader(
136: new FileInputStream(file)));
137: }
138: try {
139: StringBuilder result = new StringBuilder();
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: }
|