01: package net.sourceforge.jrefactory.query;
02:
03: import java.io.File;
04: import net.sourceforge.jrefactory.ast.ASTName;
05: import net.sourceforge.jrefactory.ast.SimpleNode;
06: import net.sourceforge.jrefactory.ast.ASTPackageDeclaration;
07: import net.sourceforge.jrefactory.factory.FileParserFactory;
08: import net.sourceforge.jrefactory.factory.ParserFactory;
09: import org.acm.seguin.awt.ExceptionPrinter;
10:
11: /**
12: * Gets the package name
13: *
14: *@author Chris Seguin
15: *@created November 23, 1999
16: */
17: public class PackageNameGetter {
18: /**
19: * Return the package name
20: *
21: *@param initialDir Description of Parameter
22: *@param filename Description of Parameter
23: *@return the package name
24: */
25: public static ASTName query(File initialDir, String filename) {
26: // Create a factory to get a root
27: File inputFile = new File(initialDir, filename);
28: ParserFactory factory = new FileParserFactory(inputFile);
29: SimpleNode root = factory.getAbstractSyntaxTree(false,
30: ExceptionPrinter.getInstance());
31:
32: return query(root);
33: }
34:
35: /**
36: * Gets the package name
37: *
38: *@param root the syntax tree
39: *@return the name of the package or null if there is none
40: */
41: public static ASTName query(SimpleNode root) {
42: if (root == null) {
43: System.out.println("Unable to find the file!");
44: return null;
45: }
46:
47: SimpleNode first = (SimpleNode) root.jjtGetFirstChild();
48: if (first instanceof ASTPackageDeclaration) {
49: return (ASTName) first.jjtGetFirstChild();
50: }
51:
52: return null;
53: }
54: }
|