View Javadoc

1   package org.codehaus.mojo.jflex;
2   
3   import java.io.File;
4   import java.io.FileNotFoundException;
5   import java.io.FileReader;
6   import java.io.IOException;
7   import java.io.LineNumberReader;
8   
9   public class LexSimpleAnalyzer {
10  	public static final String DEFAULT_NAME = "Yylex";
11  
12  	/**
13  	 * Guess the package and class name, based on this grammar definition. Does
14  	 * not overide the Mojo configuration if it exist.
15  	 * 
16  	 * @return The name of the java code to generate.
17  	 * 
18  	 * Credit goes to the authors of the maven-jlex-plugin
19  	 * 
20  	 * @throws IOException
21  	 * @author Rafal Mantiuk (Rafal.Mantiuk@bellstream.pl)
22  	 * @author Gerwin Klein (lsf@jflex.de)
23  	 * @throws FileNotFoundException
24  	 *             if the lex file does not exist
25  	 * 
26  	 */
27  	protected static ClassInfo guessPackageAndClass(File lexFile)
28  			throws FileNotFoundException, IOException {
29  
30  		LineNumberReader reader = new LineNumberReader(new FileReader(lexFile));
31  
32  		ClassInfo classInfo = new ClassInfo();
33  		// TODO Reuse code from parser if it exists.
34  		while (classInfo.className == null || classInfo.packageName == null) {
35  			String line = reader.readLine();
36  			if (line == null)
37  				break;
38  
39  			if (classInfo.packageName == null) {
40  				int index = line.indexOf("package");
41  				if (index >= 0) {
42  					index += 7;
43  
44  					int end = line.indexOf(';', index);
45  					if (end >= index) {
46  						classInfo.packageName = line.substring(index, end);
47  						classInfo.packageName = classInfo.packageName.trim();
48  					}
49  				}
50  			}
51  
52  			if (classInfo.className == null) {
53  				int index = line.indexOf("%class");
54  				if (index >= 0) {
55  					index += 6;
56  
57  					classInfo.className = line.substring(index);
58  					classInfo.className = classInfo.className.trim();
59  				}
60  			}
61  		}
62  
63  		// package name may be null, but class name not
64  		if (classInfo.className == null) {
65  			// log.warn("Could not guess class name from " +
66  			// lexFile.getName()
67  			// + ". Class name set to " + DEFAULT_NAME);
68  			classInfo.className = DEFAULT_NAME;
69  		}
70  		return classInfo;
71  	}
72  }