KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > importscrubber > PackageStmt


1 package net.sourceforge.importscrubber;
2
3 /**
4  * Encapsulates operations on a class' package statement.
5  */

6 public class PackageStmt
7 {
8     public static final String JavaDoc MARKER = "package ";
9
10     private final String JavaDoc _stmt;
11     private final boolean _isInDefaultPackage;
12     private final String JavaDoc _myPkg;
13
14     /**
15      * Creates a new PackageStatement that's not in the default package.
16      *
17      * @param stmt
18      */

19     public PackageStmt(String JavaDoc stmt)
20     {
21         this(stmt, false);
22     }
23
24     /**
25      * Creates a new PackageStatement that's in the default package.
26      */

27     public PackageStmt()
28     {
29         this(null, true);
30     }
31
32     private PackageStmt(String JavaDoc stmt, boolean inDefaultPackage)
33     {
34         _stmt = stmt;
35         _isInDefaultPackage = inDefaultPackage;
36         if (!_isInDefaultPackage) {
37             int firstSpace = _stmt.indexOf(" ") + 1;
38             int semiColon = _stmt.indexOf(";");
39             _myPkg = _stmt.substring(firstSpace, semiColon);
40         } else {
41             _myPkg = null;
42         }
43     }
44
45     public String JavaDoc getPkg()
46     {
47         return _myPkg;
48     }
49
50     public boolean isInSamePackageAs(ImportStatement importStmt)
51     {
52         if (_isInDefaultPackage) {
53             return false; // default-package imports have already been removed
54
}
55         return importStmt.getPackage().compareTo(_myPkg) == 0;
56     }
57
58     public StringBuffer JavaDoc getOutput()
59     {
60         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(64);
61         if (_isInDefaultPackage) {
62             return buffer;
63         }
64         buffer.append(_stmt);
65         buffer.append(ImportScrubber.LINE_SEPARATOR);
66         buffer.append(ImportScrubber.LINE_SEPARATOR);
67         return buffer;
68     }
69 }
70
71
72
Popular Tags