KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > util > ant > Cup


1 package org.objectweb.jac.util.ant;
2
3 import java.io.File JavaDoc;
4 import java.io.FileInputStream JavaDoc;
5 import java.util.List JavaDoc;
6 import java.util.Vector JavaDoc;
7 import org.apache.tools.ant.BuildException;
8 import org.apache.tools.ant.Task;
9
10 public class Cup extends Task {
11    private File JavaDoc cupFile;
12    private String JavaDoc options;
13    private String JavaDoc symbols;
14    private String JavaDoc parser;
15    
16    private List JavaDoc split(String JavaDoc str) {
17       int begin = 0;
18       int end = 0;
19       Vector JavaDoc list = new Vector JavaDoc();
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 JavaDoc();
39          }
40          List JavaDoc opt = split(options);
41          opt.add("-parser");
42          opt.add(parser);
43          File JavaDoc directory = cupFile.getParentFile();
44          File JavaDoc target = new File JavaDoc(directory,parser+".java");
45          File JavaDoc symTarget = new File JavaDoc(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 JavaDoc theFileStream = new FileInputStream JavaDoc(cupFile);
65          System.setIn(theFileStream);
66          java_cup.Main.main((String JavaDoc[])opt.toArray(new String JavaDoc[] {}));
67
68          // Move file generated in current directory
69
target.delete();
70          new File JavaDoc(parser+".java").renameTo(target);
71          if (symbols!=null) {
72             target = new File JavaDoc(directory,symbols+".java");
73             new File JavaDoc(symbols+".java").renameTo(target);
74          }
75       }
76       catch (Exception JavaDoc 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 JavaDoc file) {
85       this.cupFile = file;
86    }
87    
88    public void setOptions(String JavaDoc options) {
89       this.options = options;
90    }
91
92    public void setParser(String JavaDoc parser) {
93       this.parser = parser;
94    }
95
96    public void setSymbols(String JavaDoc symbols) {
97       this.symbols = symbols;
98    }
99 }
100
Popular Tags