1 21 22 package JFlex.anttask; 23 24 import org.apache.tools.ant.Task; 25 import org.apache.tools.ant.BuildException; 26 27 import JFlex.Main; 28 import JFlex.Options; 29 30 import java.io.*; 31 32 38 public class JFlexTask extends Task { 39 private File inputFile; 40 41 private String className = null; 43 private String packageName = null; 44 45 46 private File destinationDir; 47 48 49 private File outputDir = null; 50 51 public JFlexTask() { 52 setVerbose(false); 54 Options.progress = false; 55 } 56 57 public void execute() throws BuildException { 58 try { 59 if (inputFile == null) 60 throw new BuildException("Input file needed. Use <jflex file=\"your_scanner.flex\"/>"); 61 62 if (!inputFile.canRead()) 63 throw new BuildException("Cannot read input file "+inputFile); 64 65 try { 66 findPackageAndClass(); 67 normalizeOutdir(); 68 File destFile = new File(outputDir, className + ".java"); 69 70 if (inputFile.lastModified() > destFile.lastModified()) { 71 Main.generate(inputFile); 72 if (!Options.verbose) 73 System.out.println("Generated: " + destFile.getName()); 74 } 75 } catch (IOException e1) { 76 throw new BuildException("IOException: " + e1.toString()); 77 } 78 } catch (JFlex.GeneratorException e) { 79 throw new BuildException("JFlex: generation failed!"); 80 } 81 } 82 83 88 public void findPackageAndClass() throws IOException { 89 packageName = null; 91 className = null; 92 93 LineNumberReader reader = new LineNumberReader(new FileReader(inputFile)); 94 95 while (className == null || packageName == null) { 96 String line = reader.readLine(); 97 if (line == null) break; 98 99 if (packageName == null) { 100 int index = line.indexOf("package"); 101 if (index >= 0) { 102 index += 7; 103 104 int end = line.indexOf(';', index); 105 if (end >= index) { 106 packageName = line.substring(index, end); 107 packageName = packageName.trim(); 108 } 109 } 110 } 111 112 if (className == null) { 113 int index = line.indexOf("%class"); 114 if (index >= 0) { 115 index += 6; 116 117 className = line.substring(index); 118 className = className.trim(); 119 } 120 } 121 } 122 123 if (className == null) className = "Yylex"; 125 } 126 127 135 public void normalizeOutdir() { 136 if (outputDir != null) return; 137 138 File destDir; 140 141 if (destinationDir != null) { 143 if (packageName == null) { 144 destDir = destinationDir; 145 } 146 else { 147 String path = packageName.replace('.', File.separatorChar); 148 destDir = new File(destinationDir,path); 149 } 150 } else { destDir = new File(inputFile.getParent()); 152 } 153 154 setOutdir(destDir); 155 } 156 157 162 public String getPackage() { 163 return packageName; 164 } 165 166 171 public String getClassName() { 172 return className; 173 } 174 175 public void setDestdir(File destinationDir) { 176 this.destinationDir = destinationDir; 177 } 178 179 public void setOutdir(File outDir) { 180 this.outputDir = outDir; 181 Options.setDir(outputDir); 182 } 183 184 public void setFile(File file) { 185 this.inputFile = file; 186 } 187 188 public void setGenerateDot(boolean genDot) { 189 setDot(genDot); 190 } 191 192 public void setTimeStatistics(boolean displayTime) { 193 Options.time = displayTime; 194 } 195 196 public void setTime(boolean displayTime) { 197 setTimeStatistics(displayTime); 198 } 199 200 public void setVerbose(boolean verbose) { 201 Options.verbose = verbose; 202 } 203 204 public void setSkeleton(File skeleton) { 205 Options.setSkeleton(skeleton); 206 } 207 208 public void setSkel(File skeleton) { 209 setSkeleton(skeleton); 210 } 211 212 public void setSkipMinimization(boolean skipMin) { 213 setNomin(skipMin); 214 } 215 216 public void setNomin(boolean b) { 217 Options.no_minimize = b; 218 } 219 220 public void setNobak(boolean b) { 221 Options.no_backup = b; 222 } 223 224 public void setSwitch(boolean b) { 225 if (b) { 226 Options.gen_method = Options.SWITCH; 227 } 228 else { 229 Options.gen_method = Options.PACK; 230 } 231 } 232 233 public void setTable(boolean b) { 234 if (b) { 235 Options.gen_method = Options.TABLE; 236 } 237 else { 238 Options.gen_method = Options.PACK; 239 } 240 } 241 242 public void setPack(boolean b) { 243 if (b) { 244 Options.gen_method = Options.PACK; 245 } 246 else { 247 Options.gen_method = Options.SWITCH; 248 } 249 } 250 251 public void setDot(boolean b) { 252 Options.dot = b; 253 } 254 255 public void setDump(boolean b) { 256 Options.dump = b; 257 } 258 259 public void setJLex(boolean b) { 260 Options.jlex = b; 261 } 262 } 263 | Popular Tags |