1 19 package org.java.plugin.tools.ant; 20 21 import java.io.BufferedReader ; 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.IOException ; 25 import java.io.InputStreamReader ; 26 import java.io.Reader ; 27 28 import org.apache.tools.ant.BuildException; 29 import org.java.plugin.tools.docgen.DocGenerator; 30 31 32 36 public final class DocTask extends BaseJpfTask { 37 private File destDir; 38 private File overviewFile; 39 private String encoding; 40 private String docEncoding; 41 private String templatesPath; 42 private File stylesheetFile; 43 44 47 public void setDestDir(final File aDestDir) { 48 this.destDir = aDestDir; 49 } 50 51 54 public void setOverview(final File anOverviewFile) { 55 this.overviewFile = anOverviewFile; 56 } 57 58 61 public void setEncoding(final String anEncoding) { 62 this.encoding = anEncoding; 63 } 64 65 68 public void setDocEncoding(final String anEncoding) { 69 this.docEncoding = anEncoding; 70 } 71 72 75 public void setStylesheetFile(final File aStylesheetFile) { 76 this.stylesheetFile = aStylesheetFile; 77 } 78 79 83 public void setTemplates(final String aTemplatesPath) { 84 this.templatesPath = aTemplatesPath; 85 } 86 87 90 public void execute() { 91 if (destDir == null) { 92 throw new BuildException("destdir attribute must be set!", getLocation()); 94 } 95 if (!destDir.exists() && !destDir.mkdirs()) { 96 throw new BuildException("can't make " + destDir + " folder", getLocation()); } 99 if (destDir.list().length != 0) { 100 throw new BuildException("directory " + destDir + " is not empty", getLocation()); } 103 initRegistry(true); 104 log("Generating plug-ins documentation..."); 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); } 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 StringBuffer result = new StringBuffer (); 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 |