1 18 19 package org.apache.tools.ant.taskdefs.optional.javacc; 20 21 import java.io.File ; 22 import java.io.IOException ; 23 import java.util.Enumeration ; 24 import java.util.Hashtable ; 25 26 import org.apache.tools.ant.BuildException; 27 import org.apache.tools.ant.Project; 28 import org.apache.tools.ant.Task; 29 import org.apache.tools.ant.taskdefs.Execute; 30 import org.apache.tools.ant.taskdefs.LogStreamHandler; 31 import org.apache.tools.ant.types.Commandline; 32 import org.apache.tools.ant.types.CommandlineJava; 33 import org.apache.tools.ant.types.Path; 34 import org.apache.tools.ant.util.JavaEnvUtils; 35 36 40 public class JJDoc extends Task { 41 42 private static final String OUTPUT_FILE = "OUTPUT_FILE"; 44 private static final String TEXT = "TEXT"; 45 private static final String ONE_TABLE = "ONE_TABLE"; 46 47 private final Hashtable optionalAttrs = new Hashtable (); 48 49 private String outputFile = null; 50 private boolean plainText = false; 51 52 private static final String DEFAULT_SUFFIX_HTML = ".html"; 53 private static final String DEFAULT_SUFFIX_TEXT = ".txt"; 54 55 private File targetFile = null; 57 private File javaccHome = null; 58 59 private CommandlineJava cmdl = new CommandlineJava(); 60 61 62 66 public void setText(boolean plainText) { 67 optionalAttrs.put(TEXT, plainText ? Boolean.TRUE : Boolean.FALSE); 68 this.plainText = plainText; 69 } 70 71 75 public void setOnetable(boolean oneTable) { 76 optionalAttrs.put(ONE_TABLE, oneTable ? Boolean.TRUE : Boolean.FALSE); 77 } 78 79 85 public void setOutputfile(String outputFile) { 86 this.outputFile = outputFile; 87 } 88 89 93 public void setTarget(File target) { 94 this.targetFile = target; 95 } 96 97 101 public void setJavacchome(File javaccHome) { 102 this.javaccHome = javaccHome; 103 } 104 105 108 public JJDoc() { 109 cmdl.setVm(JavaEnvUtils.getJreExecutable("java")); 110 } 111 112 116 public void execute() throws BuildException { 117 118 Enumeration iter = optionalAttrs.keys(); 120 while (iter.hasMoreElements()) { 121 String name = (String ) iter.nextElement(); 122 Object value = optionalAttrs.get(name); 123 cmdl.createArgument() 124 .setValue("-" + name + ":" + value.toString()); 125 } 126 127 if (targetFile == null || !targetFile.isFile()) { 128 throw new BuildException("Invalid target: " + targetFile); 129 } 130 131 if (outputFile != null) { 132 cmdl.createArgument() .setValue("-" + OUTPUT_FILE + ":" 133 + outputFile.replace('\\', '/')); 134 } 135 136 File javaFile = new File (createOutputFileName(targetFile, outputFile, 138 plainText)); 139 140 if (javaFile.exists() 141 && targetFile.lastModified() < javaFile.lastModified()) { 142 log("Target is already built - skipping (" + targetFile + ")", 143 Project.MSG_VERBOSE); 144 return; 145 } 146 147 cmdl.createArgument().setValue(targetFile.getAbsolutePath()); 148 149 final Path classpath = cmdl.createClasspath(getProject()); 150 final File javaccJar = JavaCC.getArchiveFile(javaccHome); 151 classpath.createPathElement().setPath(javaccJar.getAbsolutePath()); 152 classpath.addJavaRuntime(); 153 154 cmdl.setClassname(JavaCC.getMainClass(classpath, 155 JavaCC.TASKDEF_TYPE_JJDOC)); 156 157 final Commandline.Argument arg = cmdl.createVmArgument(); 158 arg.setValue("-mx140M"); 159 arg.setValue("-Dinstall.root=" + javaccHome.getAbsolutePath()); 160 161 final Execute process = 162 new Execute(new LogStreamHandler(this, 163 Project.MSG_INFO, 164 Project.MSG_INFO), 165 null); 166 log(cmdl.describeCommand(), Project.MSG_VERBOSE); 167 process.setCommandline(cmdl.getCommandline()); 168 169 try { 170 if (process.execute() != 0) { 171 throw new BuildException("JJDoc failed."); 172 } 173 } catch (IOException e) { 174 throw new BuildException("Failed to launch JJDoc", e); 175 } 176 } 177 178 private String createOutputFileName(File destFile, String optionalOutputFile, 179 boolean plain) { 180 String suffix = DEFAULT_SUFFIX_HTML; 181 String javaccFile = destFile.getAbsolutePath().replace('\\', '/'); 182 183 if (plain) { 184 suffix = DEFAULT_SUFFIX_TEXT; 185 } 186 187 if ((optionalOutputFile == null) || optionalOutputFile.equals("")) { 188 int filePos = javaccFile.lastIndexOf("/"); 189 190 if (filePos >= 0) { 191 javaccFile = javaccFile.substring(filePos + 1); 192 } 193 194 int suffixPos = javaccFile.lastIndexOf('.'); 195 196 if (suffixPos == -1) { 197 optionalOutputFile = javaccFile + suffix; 198 } else { 199 String currentSuffix = javaccFile.substring(suffixPos); 200 201 if (currentSuffix.equals(suffix)) { 202 optionalOutputFile = javaccFile + suffix; 203 } else { 204 optionalOutputFile = javaccFile.substring(0, suffixPos) 205 + suffix; 206 } 207 } 208 } else { 209 optionalOutputFile = optionalOutputFile.replace('\\', '/'); 210 } 211 212 return (getProject().getBaseDir() + "/" + optionalOutputFile) 213 .replace('\\', '/'); 214 } 215 } 216 | Popular Tags |