KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > spoon > reflect > factory > PackageFactory


1 package spoon.reflect.factory;
2
3 import java.io.Serializable JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.Collection JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.Map JavaDoc;
8 import java.util.StringTokenizer JavaDoc;
9 import java.util.TreeMap JavaDoc;
10
11 import spoon.reflect.Factory;
12 import spoon.reflect.declaration.CtPackage;
13 import spoon.reflect.declaration.CtType;
14 import spoon.reflect.reference.CtPackageReference;
15
16 /**
17  * The {@link CtPackage} sub-factory.
18  */

19 public class PackageFactory extends SubFactory implements Serializable JavaDoc {
20     private static final long serialVersionUID = 1L;
21
22     /**
23      * top-level package are indexed by name to improve search
24      */

25     private Map JavaDoc<String JavaDoc, CtPackage> packages = new TreeMap JavaDoc<String JavaDoc, CtPackage>();
26
27     /**
28      * Creates a new package sub-factory.
29      *
30      * @param factory
31      * the parent factory
32      */

33     public PackageFactory(Factory factory) {
34         super(factory);
35     }
36
37     /**
38      * Creates a reference to an existing package.
39      */

40     public CtPackageReference createReference(CtPackage pack) {
41         return createReference(pack.getQualifiedName());
42     }
43
44     /**
45      * Creates a reference to a package by using its Java runtime
46      * representation.
47      *
48      * @param pack
49      * a runtime package
50      * @return reference to the package
51      */

52     public CtPackageReference createReference(Package JavaDoc pack) {
53         return createReference(pack.getName());
54     }
55
56     /**
57      * Creates a reference to a package.
58      *
59      * @param name
60      * full name of the package to reference
61      */

62     public CtPackageReference createReference(String JavaDoc name) {
63         if (name.equals(CtPackage.TOP_LEVEL_PACKAGE_NAME))
64             return null;
65         CtPackageReference ref = factory.Core().createPackageReference();
66         ref.setSimpleName(name);
67         return ref;
68     }
69
70     /**
71      * Creates a new package (see also {@link #getOrCreate(String)}).
72      *
73      * @param parent
74      * the parent package (can be null)
75      * @param simpleName
76      * the package's simple name (no dots)
77      * @return the newly created package
78      */

79     public CtPackage create(CtPackage parent, String JavaDoc simpleName) {
80         return getOrCreate(parent.toString() + CtPackage.PACKAGE_SEPARATOR
81                 + simpleName);
82     }
83
84     /**
85      * Gets or creates a package.
86      *
87      * @param qualifiedName
88      * the full name of the package
89      */

90     public CtPackage getOrCreate(String JavaDoc qualifiedName) {
91         StringTokenizer JavaDoc token = new StringTokenizer JavaDoc(qualifiedName,
92                 CtPackage.PACKAGE_SEPARATOR);
93         CtPackage last = null;
94
95         if (token.hasMoreElements()) {
96             String JavaDoc name = token.nextToken();
97             if (name.length() < 0)
98                 name = CtPackage.TOP_LEVEL_PACKAGE_NAME;
99             if (packages.containsKey(name)) {
100                 last = packages.get(name);
101             } else {
102                 last = factory.Core().createPackage();
103                 last.setSimpleName(name);
104                 register(last);
105             }
106         }
107
108         while (token.hasMoreElements()) {
109             String JavaDoc name = token.nextToken();
110             CtPackage next = last.getPackage(name);
111             if (next == null) {
112                 next = factory.Core().createPackage();
113                 next.setSimpleName(name);
114                 last.getPackages().add(next);
115                 next.setParent(last);
116             }
117             last = next;
118         }
119
120         return last;
121     }
122
123     /**
124      * Gets a created package.
125      *
126      * @param qualifiedName
127      * the package to search
128      * @return a found package or null
129      */

130     public CtPackage get(String JavaDoc qualifiedName) {
131         if (qualifiedName.contains(CtType.INNERTTYPE_SEPARATOR)) {
132             throw new RuntimeException JavaDoc("Invalide package name " + qualifiedName);
133         }
134         StringTokenizer JavaDoc token = new StringTokenizer JavaDoc(qualifiedName,
135                 CtPackage.PACKAGE_SEPARATOR);
136         CtPackage current = null;
137         if (token.hasMoreElements()) {
138             current = packages.get(token.nextElement());
139             while (token.hasMoreElements() && current != null)
140                 current = current.getPackage(token.nextToken());
141         }
142         return current;
143     }
144
145     /**
146      * Gets the list of all created packages. It includes all the top-level
147      * packages and their sub-packages.
148      */

149     public Collection JavaDoc<CtPackage> getAll() {
150         Collection JavaDoc<CtPackage> packs = new ArrayList JavaDoc<CtPackage>();
151         for (CtPackage pack : packages.values()) {
152             packs.addAll(getSubPackageList(pack));
153         }
154         return packs;
155     }
156
157     /**
158      * Gets the list of all created root packages
159      */

160     public Collection JavaDoc<CtPackage> getAllRoots() {
161         return packages.values();
162     }
163
164     private List JavaDoc<CtPackage> getSubPackageList(CtPackage pack) {
165         List JavaDoc<CtPackage> packs = new ArrayList JavaDoc<CtPackage>();
166         packs.add(pack);
167         for (CtPackage p : pack.getPackages()) {
168             packs.addAll(getSubPackageList(p));
169         }
170         return packs;
171     }
172
173     /**
174      * Registers a top-level package.
175      */

176     public void register(CtPackage pck) {
177         if (packages.containsKey(pck.getQualifiedName())) {
178             throw new RuntimeException JavaDoc("package " + pck.getQualifiedName()
179                     + " already created");
180         }
181         packages.put(pck.getQualifiedName(), pck);
182     }
183
184 }
Popular Tags