1
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
3
* Copyright (C) 2001 Rafal Mantiuk <Rafal.Mantiuk@bellstream.pl> *
4
* All rights reserved. *
6
* This program is free software; you can redistribute it and/or modify *
7
* it under the terms of the GNU General Public License. See the file *
8
* COPYRIGHT for more information. *
10
* This program is distributed in the hope that it will be useful, *
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13
* GNU General Public License for more details. *
15
* You should have received a copy of the GNU General Public License along *
16
* with this program; if not, write to the Free Software Foundation, Inc., *
17
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
19
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
21
package JFlex.anttask;
28
* @author Rafal Mantiuk
29
* @version JFlex 1.3.5, $Revision: 1.6 $, $Date: 2001/10/08 10:08:11 $
31
public class JFlexTask {
33
private File destinationDir;
34
private File inputFile;
35
private JFlexWrapper wrapper = new JFlexWrapper();
37
private boolean verbose = false;
38
private boolean generateDot = false; //write graphviz .dot files for the generated automata (alpha)
39
private boolean skipMin = false; //skip minimization step (alpha status, use with care
40
private boolean displayTime = false; //display generation time statistics
41
private File skeletonFile = null;
43
public void execute() throws BuildException {
47
if( inputFile == null ) {
48
throw new BuildException("You must specify the input file for JFlex!");
51
processFile( inputFile );
54
catch( JFlex.GeneratorException e ) {
55
throw new BuildException( "JFlex: generation failed!" );
59
public void setDestdir( File destinationDir ) {
60
this.destinationDir = destinationDir;
63
public void setFile( File file ) {
64
this.inputFile = file;
67
public void setGenerateDot( boolean genDot )
69
this.generateDot = genDot;
72
public void setTimeStatistics( boolean displayTime )
74
this.displayTime = displayTime;
77
public void setVerbose( boolean verbose )
79
this.verbose = verbose;
82
public void setSkeleton( File skeleton )
84
this.skeletonFile = skeleton;
87
public void setSkipMinimization( boolean skipMin )
89
this.skipMin = skipMin;
92
protected void processFile( File file ) throws BuildException
96
// find name of the package and class in jflex source file
97
String packageName = null;
98
String className = null;
100
LineNumberReader reader = new LineNumberReader( new InputStreamReader( new FileInputStream( file ) ) );
105
String line = reader.readLine();
106
if( line == null ) break;
108
if( packageName == null ) {
109
int index = line.indexOf( "package" );
113
int end = line.indexOf( ';', index );
115
packageName = line.substring( index, end );
116
packageName = packageName.trim();
122
if( className == null ) {
123
int index = line.indexOf( "%class" );
127
className = line.substring( index );
128
className = className.trim();
133
// We have all the necessary information. No further parsing is necessary
134
if( className != null && packageName != null ) break;
138
if( className == null )
143
// find out what the destination directory is. Append packageName to dest dir.
144
StringBuffer destDir = new StringBuffer();
147
if( destinationDir != null ) {
148
destDir.append( destinationDir.getAbsolutePath() );
150
if( packageName != null ) {
151
destDir.append( File.separatorChar );
152
String path = packageName.replace( '.', File.separatorChar );
153
destDir.append( path );
156
} else { //save parser to the same dir as .flex
157
destDir.append( file.getParent() );
161
File destFile = new File( destDir.toString() + File.separator + className + ".java" );
163
if( file.lastModified() > destFile.lastModified() ) {
166
wrapper.setTimeStatistics( displayTime );
167
wrapper.setVerbose( verbose );
168
wrapper.setGenerateDot( generateDot );
169
wrapper.setSkipMinimization( skipMin );
170
wrapper.setSkeleton( skeletonFile );
171
wrapper.setDestinationDir( destDir.toString() );
173
wrapper.generate( file );
175
if( !verbose ) System.out.println( "Generated: " + destFile.getName() );
178
catch( IOException e )
180
throw new BuildException( "IOException: " + e.toString() );