1 package org.objectweb.jac.util.ant; 2 3 import java.io.File ; 4 import java.io.FileInputStream ; 5 import java.util.List ; 6 import java.util.Vector ; 7 import org.apache.tools.ant.BuildException; 8 import org.apache.tools.ant.Task; 9 10 public class Cup extends Task { 11 private File cupFile; 12 private String options; 13 private String symbols; 14 private String parser; 15 16 private List split(String str) { 17 int begin = 0; 18 int end = 0; 19 Vector list = new Vector (); 20 21 while (end < str.length()) 22 { 23 while ((end < str.length()) && (str.charAt(end) != ' ')) 24 end++; 25 list.add(str.substring(begin, end)); 26 end = end + 1; 27 begin = end; 28 } 29 30 return list; 31 } 32 33 public void execute() throws BuildException { 34 try 35 { 36 if (parser==null) { 37 System.err.println("No parser class specified"); 38 throw new Exception (); 39 } 40 List opt = split(options); 41 opt.add("-parser"); 42 opt.add(parser); 43 File directory = cupFile.getParentFile(); 44 File target = new File (directory,parser+".java"); 45 File symTarget = new File (directory,symbols+".java"); 46 boolean skip = false; 47 if (target.exists() 48 && cupFile.lastModified()<=target.lastModified()) { 49 skip = true; 50 } 51 52 if (symbols!=null) { 53 opt.add("-symbols"); 54 opt.add(symbols); 55 if (skip && symTarget.exists() 56 && cupFile.lastModified()<=symTarget.lastModified()) { 57 return; 58 } 59 } 60 61 System.out.println("target="+target); 62 System.out.println("Opening "+cupFile); 63 System.out.println("Options "+options); 64 FileInputStream theFileStream = new FileInputStream (cupFile); 65 System.setIn(theFileStream); 66 java_cup.Main.main((String [])opt.toArray(new String [] {})); 67 68 target.delete(); 70 new File (parser+".java").renameTo(target); 71 if (symbols!=null) { 72 target = new File (directory,symbols+".java"); 73 new File (symbols+".java").renameTo(target); 74 } 75 } 76 catch (Exception e) 77 { 78 e.printStackTrace(); 79 System.out.println("it does not work"); 80 throw new BuildException("Parser generation failed"); 81 } 82 } 83 84 public void setFilename(File file) { 85 this.cupFile = file; 86 } 87 88 public void setOptions(String options) { 89 this.options = options; 90 } 91 92 public void setParser(String parser) { 93 this.parser = parser; 94 } 95 96 public void setSymbols(String symbols) { 97 this.symbols = symbols; 98 } 99 } 100 | Popular Tags |