1 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 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 public static void main(String [] args) { 87 try { 88 Wiki2Markup engine = new Wiki2Markup(); 89 engine.compileFiles(args); 90 } 91 catch (Exception e) { 92 System.out.println("Caught: " + e); 93 e.printStackTrace(); 94 } 95 } 96 97 public Wiki2Markup() { 98 context = new BaseRenderContext(); 99 engine = createRenderEngine(); 100 m.setFrom("*.wiki"); 101 m.setTo(getExtension()); 102 } 103 104 109 public Path createSrc() { 110 if (src == null) { 111 src = new Path(getProject()); 112 } 113 return src.createPath(); 114 } 115 116 121 protected Path recreateSrc() { 122 src = null; 123 return createSrc(); 124 } 125 126 130 public void setSrcdir(Path srcDir) { 131 if (src == null) { 132 src = srcDir; 133 } 134 else { 135 src.append(srcDir); 136 } 137 } 138 139 143 public Path getSrcdir() { 144 return src; 145 } 146 147 152 public void setDestdir(File destDir) { 153 this.destDir = destDir; 154 } 155 156 161 public File getDestdir() { 162 return destDir; 163 } 164 165 169 public void setListfiles(boolean list) { 170 listFiles = list; 171 } 172 173 177 public boolean getListfiles() { 178 return listFiles; 179 } 180 181 186 public void setFailonerror(boolean fail) { 187 failOnError = fail; 188 } 189 190 194 public void setProceed(boolean proceed) { 195 failOnError = !proceed; 196 } 197 198 202 public boolean getFailonerror() { 203 return failOnError; 204 } 205 206 210 public void execute() throws BuildException { 211 checkParameters(); 212 resetFileLists(); 213 214 String [] list = src.list(); 217 for (int i = 0; i < list.length; i++) { 218 File srcDir = getProject().resolveFile(list[i]); 219 if (!srcDir.exists()) { 220 throw new BuildException("srcdir \"" + srcDir.getPath() + "\" does not exist!", getLocation()); 221 } 222 223 DirectoryScanner ds = this.getDirectoryScanner(srcDir); 224 String [] files = ds.getIncludedFiles(); 225 226 scanDir(srcDir, destDir != null ? destDir : srcDir, files); 227 } 228 229 compile(); 230 } 231 232 235 protected void resetFileLists() { 236 compileList = new File [0]; 237 } 238 239 247 protected void scanDir(File srcDir, File destDir, String [] files) { 248 SourceFileScanner sfs = new SourceFileScanner(this); 249 File [] newFiles = sfs.restrictAsFiles(files, srcDir, destDir, m); 250 251 if (newFiles.length > 0) { 252 File [] newCompileList = new File [compileList.length + newFiles.length]; 253 System.arraycopy(compileList, 0, newCompileList, 0, compileList.length); 254 System.arraycopy(newFiles, 0, newCompileList, compileList.length, newFiles.length); 255 compileList = newCompileList; 256 } 257 } 258 259 263 public File [] getFileList() { 264 return compileList; 265 } 266 267 protected void checkParameters() throws BuildException { 268 if (src == null) { 269 throw new BuildException("srcdir attribute must be set!", getLocation()); 270 } 271 if (src.size() == 0) { 272 throw new BuildException("srcdir attribute must be set!", getLocation()); 273 } 274 275 if (destDir != null && !destDir.isDirectory()) { 276 throw new BuildException( 277 "destination directory \"" + destDir + "\" does not exist " + "or is not a directory", 278 getLocation()); 279 } 280 } 281 282 public void compileFiles(String [] args) throws IOException { 283 for (int i = 0; i < args.length; i++) { 284 File file = new File (args[i]); 285 compile(file, args[i]); 286 } 287 } 288 289 protected void compile() { 290 if (compileList.length > 0) { 291 log( 292 "Compiling " 293 + compileList.length 294 + " source file" 295 + (compileList.length == 1 ? "" : "s") 296 + (destDir != null ? " to " + destDir : "")); 297 298 try { 299 for (int i = 0; i < compileList.length; i++) { 300 String filename = compileList[i].getAbsolutePath(); 301 if (listFiles) { 302 log(filename); 303 } 304 compile(compileList[i], compileList[i].getName()); 305 } 306 } 307 catch (Exception e) { 308 String message = "Compile failed: " + e; 309 if (failOnError) { 310 throw new BuildException(message, e, getLocation()); 311 } 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 } 363 | Popular Tags |