KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JFlex > anttask > JFlexTask


1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * JFlex Anttask *
3  * Copyright (C) 2001 Rafal Mantiuk <Rafal.Mantiuk@bellstream.pl> *
4  * Copyright (C) 2003 changes by Gerwin Klein <lsf@jflex.de> *
5  * All rights reserved. *
6  * *
7  * This program is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License. See the file *
9  * COPYRIGHT for more information. *
10  * *
11  * This program is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU General Public License along *
17  * with this program; if not, write to the Free Software Foundation, Inc., *
18  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
19  * *
20  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

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 /**
33  * JFlex task class
34  *
35  * @author Rafal Mantiuk
36  * @version JFlex 1.4.1, $Revision: 2.14 $, $Date: 2004/11/06 23:03:33 $
37  */

38 public class JFlexTask extends Task {
39   private File inputFile;
40
41     // found out by looking into .flex file
42
private String JavaDoc className = null;
43     private String JavaDoc packageName = null;
44
45   /** for javac-like dest dir behaviour */
46   private File destinationDir;
47     
48     /** the actual output directory (outputDir = destinationDir + package)) */
49     private File outputDir = null;
50
51   public JFlexTask() {
52     // ant default is different from the rest of JFlex
53
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     /**
84      * Peek into .flex file to get package and class name
85      *
86      * @throws IOException if there is a problem reading the .flex file
87      */

88     public void findPackageAndClass() throws IOException {
89         // find name of the package and class in jflex source file
90
packageName = null;
91         className = null;
92
93         LineNumberReader reader = new LineNumberReader(new FileReader(inputFile));
94
95         while (className == null || packageName == null) {
96             String JavaDoc 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         // package name may be null, but class name not
124
if (className == null) className = "Yylex";
125     }
126
127     /**
128      * Sets the actual output directory if not already set.
129      *
130      * Uses javac logic to determine output dir = dest dir + package name
131      * If not destdir has been set, output dir = parent of input file
132      *
133      * Assumes that package name is already set.
134      */

135   public void normalizeOutdir() {
136     if (outputDir != null) return;
137     
138     // find out what the destination directory is. Append packageName to dest dir.
139
File destDir;
140     
141     // this is not the default the jflex logic, but javac-like
142
if (destinationDir != null) {
143       if (packageName == null) {
144             destDir = destinationDir;
145       }
146       else {
147         String JavaDoc path = packageName.replace('.', File.separatorChar);
148         destDir = new File(destinationDir,path);
149       }
150     } else { //save parser to the same dir as .flex
151
destDir = new File(inputFile.getParent());
152     }
153     
154     setOutdir(destDir);
155   }
156
157     /**
158      * @return package name of input file
159      *
160      * @see JFlexTask.findPackageAndClass
161      */

162     public String JavaDoc getPackage() {
163         return packageName;
164     }
165
166     /**
167      * @return class name of input file
168      *
169      * @see JFlexTask.findPackageAndClass
170      */

171     public String JavaDoc 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