|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
Wiki2Markup.java | 0% | 0% | 0% | 0% |
|
1 |
/*
|
|
2 |
$Id: Wiki2Markup.java,v 1.3 2003/12/31 12:39:48 jstrachan Exp $
|
|
3 |
|
|
4 |
Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
|
|
5 |
|
|
6 |
Redistribution and use of this software and associated documentation
|
|
7 |
("Software"), with or without modification, are permitted provided
|
|
8 |
that the following conditions are met:
|
|
9 |
|
|
10 |
1. Redistributions of source code must retain copyright
|
|
11 |
statements and notices. Redistributions must also contain a
|
|
12 |
copy of this document.
|
|
13 |
|
|
14 |
2. Redistributions in binary form must reproduce the
|
|
15 |
above copyright notice, this list of conditions and the
|
|
16 |
following disclaimer in the documentation and/or other
|
|
17 |
materials provided with the distribution.
|
|
18 |
|
|
19 |
3. The name "groovy" must not be used to endorse or promote
|
|
20 |
products derived from this Software without prior written
|
|
21 |
permission of The Codehaus. For written permission,
|
|
22 |
please contact info@codehaus.org.
|
|
23 |
|
|
24 |
4. Products derived from this Software may not be called "groovy"
|
|
25 |
nor may "groovy" appear in their names without prior written
|
|
26 |
permission of The Codehaus. "groovy" is a registered
|
|
27 |
trademark of The Codehaus.
|
|
28 |
|
|
29 |
5. Due credit should be given to The Codehaus -
|
|
30 |
http://groovy.codehaus.org/
|
|
31 |
|
|
32 |
THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
|
|
33 |
``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
|
|
34 |
NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
35 |
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
|
36 |
THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
|
37 |
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
38 |
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
39 |
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
40 |
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
41 |
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
42 |
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
43 |
OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
44 |
|
|
45 |
*/
|
|
46 |
package org.codehaus.groovy.wiki;
|
|
47 |
|
|
48 |
import java.io.BufferedReader;
|
|
49 |
import java.io.File;
|
|
50 |
import java.io.FileReader;
|
|
51 |
import java.io.FileWriter;
|
|
52 |
import java.io.IOException;
|
|
53 |
|
|
54 |
import org.apache.tools.ant.BuildException;
|
|
55 |
import org.apache.tools.ant.DirectoryScanner;
|
|
56 |
import org.apache.tools.ant.Project;
|
|
57 |
import org.apache.tools.ant.taskdefs.MatchingTask;
|
|
58 |
import org.apache.tools.ant.types.Path;
|
|
59 |
import org.apache.tools.ant.util.GlobPatternMapper;
|
|
60 |
import org.apache.tools.ant.util.SourceFileScanner;
|
|
61 |
import org.radeox.api.engine.RenderEngine;
|
|
62 |
import org.radeox.api.engine.context.RenderContext;
|
|
63 |
import org.radeox.engine.BaseRenderEngine;
|
|
64 |
import org.radeox.engine.context.BaseRenderContext;
|
|
65 |
|
|
66 |
/**
|
|
67 |
* Converts the Wiki markup into XML/HTML so that it can be styled
|
|
68 |
* by the Maven build
|
|
69 |
*
|
|
70 |
* @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
|
|
71 |
* @version $Revision: 1.3 $
|
|
72 |
*/
|
|
73 |
public class Wiki2Markup extends MatchingTask { |
|
74 |
|
|
75 |
private Path src;
|
|
76 |
private File destDir;
|
|
77 |
|
|
78 |
protected boolean failOnError = true; |
|
79 |
protected boolean listFiles = false; |
|
80 |
protected File[] compileList = new File[0]; |
|
81 |
private GlobPatternMapper m = new GlobPatternMapper(); |
|
82 |
|
|
83 |
private RenderContext context;
|
|
84 |
private RenderEngine engine;
|
|
85 |
|
|
86 | 0 |
public static void main(String[] args) { |
87 | 0 |
try {
|
88 | 0 |
Wiki2Markup engine = new Wiki2Markup();
|
89 | 0 |
engine.compileFiles(args); |
90 |
} |
|
91 |
catch (Exception e) {
|
|
92 | 0 |
System.out.println("Caught: " + e);
|
93 | 0 |
e.printStackTrace(); |
94 |
} |
|
95 |
} |
|
96 |
|
|
97 | 0 |
public Wiki2Markup() {
|
98 | 0 |
context = new BaseRenderContext();
|
99 | 0 |
engine = createRenderEngine(); |
100 | 0 |
m.setFrom("*.wiki");
|
101 | 0 |
m.setTo(getExtension()); |
102 |
} |
|
103 |
|
|
104 |
/**
|
|
105 |
* Adds a path for source compilation.
|
|
106 |
*
|
|
107 |
* @return a nested src element.
|
|
108 |
*/
|
|
109 | 0 |
public Path createSrc() {
|
110 | 0 |
if (src == null) { |
111 | 0 |
src = new Path(getProject());
|
112 |
} |
|
113 | 0 |
return src.createPath();
|
114 |
} |
|
115 |
|
|
116 |
/**
|
|
117 |
* Recreate src.
|
|
118 |
*
|
|
119 |
* @return a nested src element.
|
|
120 |
*/
|
|
121 | 0 |
protected Path recreateSrc() {
|
122 | 0 |
src = null;
|
123 | 0 |
return createSrc();
|
124 |
} |
|
125 |
|
|
126 |
/**
|
|
127 |
* Set the source directories to find the source Java files.
|
|
128 |
* @param srcDir the source directories as a path
|
|
129 |
*/
|
|
130 | 0 |
public void setSrcdir(Path srcDir) { |
131 | 0 |
if (src == null) { |
132 | 0 |
src = srcDir; |
133 |
} |
|
134 |
else {
|
|
135 | 0 |
src.append(srcDir); |
136 |
} |
|
137 |
} |
|
138 |
|
|
139 |
/**
|
|
140 |
* Gets the source dirs to find the source java files.
|
|
141 |
* @return the source directorys as a path
|
|
142 |
*/
|
|
143 | 0 |
public Path getSrcdir() {
|
144 | 0 |
return src;
|
145 |
} |
|
146 |
|
|
147 |
/**
|
|
148 |
* Set the destination directory into which the Java source
|
|
149 |
* files should be compiled.
|
|
150 |
* @param destDir the destination director
|
|
151 |
*/
|
|
152 | 0 |
public void setDestdir(File destDir) { |
153 | 0 |
this.destDir = destDir;
|
154 |
} |
|
155 |
|
|
156 |
/**
|
|
157 |
* Gets the destination directory into which the java source files
|
|
158 |
* should be compiled.
|
|
159 |
* @return the destination directory
|
|
160 |
*/
|
|
161 | 0 |
public File getDestdir() {
|
162 | 0 |
return destDir;
|
163 |
} |
|
164 |
|
|
165 |
/**
|
|
166 |
* If true, list the source files being handed off to the compiler.
|
|
167 |
* @param list if true list the source files
|
|
168 |
*/
|
|
169 | 0 |
public void setListfiles(boolean list) { |
170 | 0 |
listFiles = list; |
171 |
} |
|
172 |
|
|
173 |
/**
|
|
174 |
* Get the listfiles flag.
|
|
175 |
* @return the listfiles flag
|
|
176 |
*/
|
|
177 | 0 |
public boolean getListfiles() { |
178 | 0 |
return listFiles;
|
179 |
} |
|
180 |
|
|
181 |
/**
|
|
182 |
* Indicates whether the build will continue
|
|
183 |
* even if there are compilation errors; defaults to true.
|
|
184 |
* @param fail if true halt the build on failure
|
|
185 |
*/
|
|
186 | 0 |
public void setFailonerror(boolean fail) { |
187 | 0 |
failOnError = fail; |
188 |
} |
|
189 |
|
|
190 |
/**
|
|
191 |
* @ant.attribute ignore="true"
|
|
192 |
* @param proceed inverse of failoferror
|
|
193 |
*/
|
|
194 | 0 |
public void setProceed(boolean proceed) { |
195 | 0 |
failOnError = !proceed; |
196 |
} |
|
197 |
|
|
198 |
/**
|
|
199 |
* Gets the failonerror flag.
|
|
200 |
* @return the failonerror flag
|
|
201 |
*/
|
|
202 | 0 |
public boolean getFailonerror() { |
203 | 0 |
return failOnError;
|
204 |
} |
|
205 |
|
|
206 |
/**
|
|
207 |
* Executes the task.
|
|
208 |
* @exception BuildException if an error occurs
|
|
209 |
*/
|
|
210 | 0 |
public void execute() throws BuildException { |
211 | 0 |
checkParameters(); |
212 | 0 |
resetFileLists(); |
213 |
|
|
214 |
// scan source directories and dest directory to build up
|
|
215 |
// compile lists
|
|
216 | 0 |
String[] list = src.list(); |
217 | 0 |
for (int i = 0; i < list.length; i++) { |
218 | 0 |
File srcDir = getProject().resolveFile(list[i]); |
219 | 0 |
if (!srcDir.exists()) {
|
220 | 0 |
throw new BuildException("srcdir \"" + srcDir.getPath() + "\" does not exist!", getLocation()); |
221 |
} |
|
222 |
|
|
223 | 0 |
DirectoryScanner ds = this.getDirectoryScanner(srcDir);
|
224 | 0 |
String[] files = ds.getIncludedFiles(); |
225 |
|
|
226 | 0 |
scanDir(srcDir, destDir != null ? destDir : srcDir, files);
|
227 |
} |
|
228 |
|
|
229 | 0 |
compile(); |
230 |
} |
|
231 |
|
|
232 |
/**
|
|
233 |
* Clear the list of files to be compiled and copied..
|
|
234 |
*/
|
|
235 | 0 |
protected void resetFileLists() { |
236 | 0 |
compileList = new File[0];
|
237 |
} |
|
238 |
|
|
239 |
/**
|
|
240 |
* Scans the directory looking for source files to be compiled.
|
|
241 |
* The results are returned in the class variable compileList
|
|
242 |
*
|
|
243 |
* @param srcDir The source directory
|
|
244 |
* @param destDir The destination directory
|
|
245 |
* @param files An array of filenames
|
|
246 |
*/
|
|
247 | 0 |
protected void scanDir(File srcDir, File destDir, String[] files) { |
248 | 0 |
SourceFileScanner sfs = new SourceFileScanner(this); |
249 | 0 |
File[] newFiles = sfs.restrictAsFiles(files, srcDir, destDir, m); |
250 |
|
|
251 | 0 |
if (newFiles.length > 0) {
|
252 | 0 |
File[] newCompileList = new File[compileList.length + newFiles.length];
|
253 | 0 |
System.arraycopy(compileList, 0, newCompileList, 0, compileList.length); |
254 | 0 |
System.arraycopy(newFiles, 0, newCompileList, compileList.length, newFiles.length); |
255 | 0 |
compileList = newCompileList; |
256 |
} |
|
257 |
} |
|
258 |
|
|
259 |
/**
|
|
260 |
* Gets the list of files to be compiled.
|
|
261 |
* @return the list of files as an array
|
|
262 |
*/
|
|
263 | 0 |
public File[] getFileList() {
|
264 | 0 |
return compileList;
|
265 |
} |
|
266 |
|
|
267 | 0 |
protected void checkParameters() throws BuildException { |
268 | 0 |
if (src == null) { |
269 | 0 |
throw new BuildException("srcdir attribute must be set!", getLocation()); |
270 |
} |
|
271 | 0 |
if (src.size() == 0) {
|
272 | 0 |
throw new BuildException("srcdir attribute must be set!", getLocation()); |
273 |
} |
|
274 |
|
|
275 | 0 |
if (destDir != null && !destDir.isDirectory()) { |
276 | 0 |
throw new BuildException( |
277 |
"destination directory \"" + destDir + "\" does not exist " + "or is not a directory", |
|
278 |
getLocation()); |
|
279 |
} |
|
280 |
} |
|
281 |
|
|
282 | 0 |
public void compileFiles(String[] args) throws IOException { |
283 | 0 |
for (int i = 0; i < args.length; i++) { |
284 | 0 |
File file = new File(args[i]);
|
285 | 0 |
compile(file, args[i]); |
286 |
} |
|
287 |
} |
|
288 |
|
|
289 | 0 |
protected void compile() { |
290 | 0 |
if (compileList.length > 0) {
|
291 | 0 |
log( |
292 |
"Compiling "
|
|
293 |
+ compileList.length |
|
294 |
+ " source file"
|
|
295 | 0 |
+ (compileList.length == 1 ? "" : "s") |
296 | 0 |
+ (destDir != null ? " to " + destDir : "")); |
297 |
|
|
298 | 0 |
try {
|
299 | 0 |
for (int i = 0; i < compileList.length; i++) { |
300 | 0 |
String filename = compileList[i].getAbsolutePath(); |
301 | 0 |
if (listFiles) {
|
302 | 0 |
log(filename); |
303 |
} |
|
304 | 0 |
compile(compileList[i], compileList[i].getName()); |
305 |
} |
|
306 |
} |
|
307 |
catch (Exception e) {
|
|
308 | 0 |
String message = "Compile failed: " + e;
|
309 | 0 |
if (failOnError) {
|
310 | 0 |
throw new BuildException(message, e, getLocation()); |
311 |
} |
|
312 |
else {
|
|
313 | 0 |
log(message, Project.MSG_ERR); |
314 |
} |
|
315 |
} |
|
316 |
} |
|
317 |
} |
|
318 |
|
|
319 | 0 |
protected void compile(File file, String name) throws IOException { |
320 | 0 |
String[] names = m.mapFileName(name); |
321 | 0 |
String outputName = names[0]; |
322 |
|
|
323 | 0 |
context.set("name", name);
|
324 |
|
|
325 | 0 |
String text = readFile(file); |
326 | 0 |
String result = engine.render(text, context); |
327 |
|
|
328 | 0 |
File outputFile = new File(getDestdir(), outputName);
|
329 | 0 |
System.out.println("Creating file: " + outputFile);
|
330 |
|
|
331 | 0 |
FileWriter writer = new FileWriter(outputFile);
|
332 | 0 |
result = filter(result); |
333 | 0 |
writer.write(result); |
334 | 0 |
writer.close(); |
335 |
} |
|
336 |
|
|
337 | 0 |
protected String filter(String result) {
|
338 | 0 |
return "<html><body>\n" + result + "\n<body><html>\n"; |
339 |
} |
|
340 |
|
|
341 | 0 |
protected String readFile(File file) throws IOException { |
342 | 0 |
StringBuffer buffer = new StringBuffer();
|
343 | 0 |
BufferedReader reader = new BufferedReader(new FileReader(file)); |
344 | 0 |
while (true) { |
345 | 0 |
String line = reader.readLine(); |
346 | 0 |
if (line == null) { |
347 | 0 |
break;
|
348 |
} |
|
349 | 0 |
buffer.append(line); |
350 | 0 |
buffer.append("\n");
|
351 |
} |
|
352 | 0 |
return buffer.toString();
|
353 |
} |
|
354 |
|
|
355 | 0 |
protected RenderEngine createRenderEngine() {
|
356 | 0 |
return new BaseRenderEngine(); |
357 |
} |
|
358 |
|
|
359 | 0 |
protected String getExtension() {
|
360 | 0 |
return "*.html"; |
361 |
} |
|
362 |
} |
|
363 |
|
|