001: /*
002: $Id: Wiki2Markup.java 481 2003-12-31 12:39:48Z jstrachan $
003:
004: Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
005:
006: Redistribution and use of this software and associated documentation
007: ("Software"), with or without modification, are permitted provided
008: that the following conditions are met:
009:
010: 1. Redistributions of source code must retain copyright
011: statements and notices. Redistributions must also contain a
012: copy of this document.
013:
014: 2. Redistributions in binary form must reproduce the
015: above copyright notice, this list of conditions and the
016: following disclaimer in the documentation and/or other
017: materials provided with the distribution.
018:
019: 3. The name "groovy" must not be used to endorse or promote
020: products derived from this Software without prior written
021: permission of The Codehaus. For written permission,
022: please contact info@codehaus.org.
023:
024: 4. Products derived from this Software may not be called "groovy"
025: nor may "groovy" appear in their names without prior written
026: permission of The Codehaus. "groovy" is a registered
027: trademark of The Codehaus.
028:
029: 5. Due credit should be given to The Codehaus -
030: http://groovy.codehaus.org/
031:
032: THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
033: ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
034: NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
035: FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
036: THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
037: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
038: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
039: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
040: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
041: STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
042: ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
043: OF THE POSSIBILITY OF SUCH DAMAGE.
044:
045: */
046: package org.codehaus.groovy.wiki;
047:
048: import java.io.BufferedReader;
049: import java.io.File;
050: import java.io.FileReader;
051: import java.io.FileWriter;
052: import java.io.IOException;
053:
054: import org.apache.tools.ant.BuildException;
055: import org.apache.tools.ant.DirectoryScanner;
056: import org.apache.tools.ant.Project;
057: import org.apache.tools.ant.taskdefs.MatchingTask;
058: import org.apache.tools.ant.types.Path;
059: import org.apache.tools.ant.util.GlobPatternMapper;
060: import org.apache.tools.ant.util.SourceFileScanner;
061: import org.radeox.api.engine.RenderEngine;
062: import org.radeox.api.engine.context.RenderContext;
063: import org.radeox.engine.BaseRenderEngine;
064: import org.radeox.engine.context.BaseRenderContext;
065:
066: /**
067: * Converts the Wiki markup into XML/HTML so that it can be styled
068: * by the Maven build
069: *
070: * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
071: * @version $Revision: 481 $
072: */
073: public class Wiki2Markup extends MatchingTask {
074:
075: private Path src;
076: private File destDir;
077:
078: protected boolean failOnError = true;
079: protected boolean listFiles = false;
080: protected File[] compileList = new File[0];
081: private GlobPatternMapper m = new GlobPatternMapper();
082:
083: private RenderContext context;
084: private RenderEngine engine;
085:
086: public static void main(String[] args) {
087: try {
088: Wiki2Markup engine = new Wiki2Markup();
089: engine.compileFiles(args);
090: } catch (Exception e) {
091: System.out.println("Caught: " + e);
092: e.printStackTrace();
093: }
094: }
095:
096: public Wiki2Markup() {
097: context = new BaseRenderContext();
098: engine = createRenderEngine();
099: m.setFrom("*.wiki");
100: m.setTo(getExtension());
101: }
102:
103: /**
104: * Adds a path for source compilation.
105: *
106: * @return a nested src element.
107: */
108: public Path createSrc() {
109: if (src == null) {
110: src = new Path(getProject());
111: }
112: return src.createPath();
113: }
114:
115: /**
116: * Recreate src.
117: *
118: * @return a nested src element.
119: */
120: protected Path recreateSrc() {
121: src = null;
122: return createSrc();
123: }
124:
125: /**
126: * Set the source directories to find the source Java files.
127: * @param srcDir the source directories as a path
128: */
129: public void setSrcdir(Path srcDir) {
130: if (src == null) {
131: src = srcDir;
132: } else {
133: src.append(srcDir);
134: }
135: }
136:
137: /**
138: * Gets the source dirs to find the source java files.
139: * @return the source directorys as a path
140: */
141: public Path getSrcdir() {
142: return src;
143: }
144:
145: /**
146: * Set the destination directory into which the Java source
147: * files should be compiled.
148: * @param destDir the destination director
149: */
150: public void setDestdir(File destDir) {
151: this .destDir = destDir;
152: }
153:
154: /**
155: * Gets the destination directory into which the java source files
156: * should be compiled.
157: * @return the destination directory
158: */
159: public File getDestdir() {
160: return destDir;
161: }
162:
163: /**
164: * If true, list the source files being handed off to the compiler.
165: * @param list if true list the source files
166: */
167: public void setListfiles(boolean list) {
168: listFiles = list;
169: }
170:
171: /**
172: * Get the listfiles flag.
173: * @return the listfiles flag
174: */
175: public boolean getListfiles() {
176: return listFiles;
177: }
178:
179: /**
180: * Indicates whether the build will continue
181: * even if there are compilation errors; defaults to true.
182: * @param fail if true halt the build on failure
183: */
184: public void setFailonerror(boolean fail) {
185: failOnError = fail;
186: }
187:
188: /**
189: * @ant.attribute ignore="true"
190: * @param proceed inverse of failoferror
191: */
192: public void setProceed(boolean proceed) {
193: failOnError = !proceed;
194: }
195:
196: /**
197: * Gets the failonerror flag.
198: * @return the failonerror flag
199: */
200: public boolean getFailonerror() {
201: return failOnError;
202: }
203:
204: /**
205: * Executes the task.
206: * @exception BuildException if an error occurs
207: */
208: public void execute() throws BuildException {
209: checkParameters();
210: resetFileLists();
211:
212: // scan source directories and dest directory to build up
213: // compile lists
214: String[] list = src.list();
215: for (int i = 0; i < list.length; i++) {
216: File srcDir = getProject().resolveFile(list[i]);
217: if (!srcDir.exists()) {
218: throw new BuildException("srcdir \"" + srcDir.getPath()
219: + "\" does not exist!", getLocation());
220: }
221:
222: DirectoryScanner ds = this .getDirectoryScanner(srcDir);
223: String[] files = ds.getIncludedFiles();
224:
225: scanDir(srcDir, destDir != null ? destDir : srcDir, files);
226: }
227:
228: compile();
229: }
230:
231: /**
232: * Clear the list of files to be compiled and copied..
233: */
234: protected void resetFileLists() {
235: compileList = new File[0];
236: }
237:
238: /**
239: * Scans the directory looking for source files to be compiled.
240: * The results are returned in the class variable compileList
241: *
242: * @param srcDir The source directory
243: * @param destDir The destination directory
244: * @param files An array of filenames
245: */
246: protected void scanDir(File srcDir, File destDir, String[] files) {
247: SourceFileScanner sfs = new SourceFileScanner(this );
248: File[] newFiles = sfs
249: .restrictAsFiles(files, srcDir, destDir, m);
250:
251: if (newFiles.length > 0) {
252: File[] newCompileList = new File[compileList.length
253: + newFiles.length];
254: System.arraycopy(compileList, 0, newCompileList, 0,
255: compileList.length);
256: System.arraycopy(newFiles, 0, newCompileList,
257: compileList.length, newFiles.length);
258: compileList = newCompileList;
259: }
260: }
261:
262: /**
263: * Gets the list of files to be compiled.
264: * @return the list of files as an array
265: */
266: public File[] getFileList() {
267: return compileList;
268: }
269:
270: protected void checkParameters() throws BuildException {
271: if (src == null) {
272: throw new BuildException("srcdir attribute must be set!",
273: getLocation());
274: }
275: if (src.size() == 0) {
276: throw new BuildException("srcdir attribute must be set!",
277: getLocation());
278: }
279:
280: if (destDir != null && !destDir.isDirectory()) {
281: throw new BuildException("destination directory \""
282: + destDir + "\" does not exist "
283: + "or is not a directory", getLocation());
284: }
285: }
286:
287: public void compileFiles(String[] args) throws IOException {
288: for (int i = 0; i < args.length; i++) {
289: File file = new File(args[i]);
290: compile(file, args[i]);
291: }
292: }
293:
294: protected void compile() {
295: if (compileList.length > 0) {
296: log("Compiling " + compileList.length + " source file"
297: + (compileList.length == 1 ? "" : "s")
298: + (destDir != null ? " to " + destDir : ""));
299:
300: try {
301: for (int i = 0; i < compileList.length; i++) {
302: String filename = compileList[i].getAbsolutePath();
303: if (listFiles) {
304: log(filename);
305: }
306: compile(compileList[i], compileList[i].getName());
307: }
308: } catch (Exception e) {
309: String message = "Compile failed: " + e;
310: if (failOnError) {
311: throw new BuildException(message, e, getLocation());
312: } else {
313: log(message, Project.MSG_ERR);
314: }
315: }
316: }
317: }
318:
319: protected void compile(File file, String name) throws IOException {
320: String[] names = m.mapFileName(name);
321: String outputName = names[0];
322:
323: context.set("name", name);
324:
325: String text = readFile(file);
326: String result = engine.render(text, context);
327:
328: File outputFile = new File(getDestdir(), outputName);
329: System.out.println("Creating file: " + outputFile);
330:
331: FileWriter writer = new FileWriter(outputFile);
332: result = filter(result);
333: writer.write(result);
334: writer.close();
335: }
336:
337: protected String filter(String result) {
338: return "<html><body>\n" + result + "\n<body><html>\n";
339: }
340:
341: protected String readFile(File file) throws IOException {
342: StringBuffer buffer = new StringBuffer();
343: BufferedReader reader = new BufferedReader(new FileReader(file));
344: while (true) {
345: String line = reader.readLine();
346: if (line == null) {
347: break;
348: }
349: buffer.append(line);
350: buffer.append("\n");
351: }
352: return buffer.toString();
353: }
354:
355: protected RenderEngine createRenderEngine() {
356: return new BaseRenderEngine();
357: }
358:
359: protected String getExtension() {
360: return "*.html";
361: }
362: }
|