KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > gulden > util > javasource > Package


1 /*
2  * Project: BeautyJ - Customizable Java Source Code Transformer
3  * Class: de.gulden.util.javasource.Package
4  * Version: 1.1
5  *
6  * Date: 2004-09-29
7  *
8  * Note: Contains auto-generated Javadoc comments created by BeautyJ.
9  *
10  * This is licensed under the GNU General Public License (GPL)
11  * and comes with NO WARRANTY. See file license.txt for details.
12  *
13  * Author: Jens Gulden
14  * Email: beautyj@jensgulden.de
15  */

16
17 package de.gulden.util.javasource;
18
19 import de.gulden.util.xml.XMLToolbox;
20 import de.gulden.util.javasource.jjt.Node;
21 import javax.xml.parsers.*;
22 import org.w3c.dom.*;
23 import java.io.*;
24 import java.util.*;
25
26 /**
27  * Represents a Java package.
28  *
29  * @author Jens Gulden
30  * @version 1.1
31  */

32 public class Package extends SourceObject implements PackageMember {
33
34     // ------------------------------------------------------------------------
35
// --- fields ---
36
// ------------------------------------------------------------------------
37

38     /**
39      * The my class.
40      */

41     public Vector myClass;
42
43     /**
44      * The children.
45      */

46     public Vector children;
47
48     /**
49      * The father.
50      */

51     public Package JavaDoc father;
52
53
54     // ------------------------------------------------------------------------
55
// --- constructor ---
56
// ------------------------------------------------------------------------
57

58     /**
59      * Creates a new instance of Package.
60      */

61     public Package() {
62         myClass=new Vector();
63         children=new Vector();
64         name=""; // initially treat as default (base-)package
65
}
66
67
68     // ------------------------------------------------------------------------
69
// --- methods ---
70
// ------------------------------------------------------------------------
71

72     /**
73      * Sets the name.
74      */

75     public void setName(String JavaDoc qualifiedName) {
76         super.setName(qualifiedName);
77         initEmptyFathers(); // !!
78
}
79
80     public void add(PackageMember p) {
81         p.addToPackage(this);
82     }
83
84     /**
85      * Returns a Class object that carries the qualifiedName, if the class
86      * is contained in this package, or if it is contained in any sub-package
87      * of this package, or is an inner class of any class inside this package.
88      *
89      * @return The Class representation, or <code>null</code> if not found.
90      */

91     public Class JavaDoc findClass(String JavaDoc qualifiedName) {
92                 String JavaDoc selfName = this.getName();
93                 boolean base = (selfName.equals(""));
94                 String JavaDoc q;
95                 if (!base) {
96                     if (qualifiedName.startsWith(selfName+".")) {
97                         q = qualifiedName.substring(selfName.length()+1);
98                     } else {
99                         return null;
100                     }
101                 } else {
102                     q = qualifiedName;
103                 }
104                 // q now is stripped by self name (like 'de.gulden.application.beautyj.BeautyJ' 'application.beautyj.BeautyJ' if this is 'de.gulden')
105
String JavaDoc firstPart;
106                 int dot = q.indexOf('.');
107                 if (dot!=-1) {
108                     firstPart = q.substring(0, dot);
109                     String JavaDoc find;
110                     if (!base) {
111                         find = selfName + "." + firstPart;
112                     } else {
113                         find = firstPart;
114                     }
115                     // find could be a subpackage of this, or an outer class if qualifiedName is an inner class
116
// inner package?
117
NamedIterator it=getInnerPackages();
118                     Package JavaDoc p=(Package JavaDoc)it.find(find);
119                     if (p!=null) {
120                         return p.findClass(qualifiedName); // recursion
121
}
122                     // must be looking for an inner class of a class in this package now
123
it=getClasses();
124                     Class JavaDoc c=(Class JavaDoc)it.find(find);
125                     if (c != null) {
126                         ClassInner ci = c.findInnerClass(qualifiedName);
127                         return ci;
128                     } else {
129                         return null;
130                     }
131                 } else {
132                     // can only be a class in this package
133
NamedIterator it=getClasses();
134                     Class JavaDoc c=(Class JavaDoc)it.find(qualifiedName);
135                     return c;
136                 }
137     }
138
139     /**
140      * Returns a Package object that carries the qualifiedName, if the package
141      * is this package, or if it is contained in this or any sub-package
142      * of this package.
143      *
144      * @return The Package representation, or <code>null</code> if not found.
145      */

146     public Package JavaDoc findPackage(String JavaDoc qualifiedName) {
147                 String JavaDoc selfName = this.getName();
148                 boolean base = (selfName.equals(""));
149                 if (selfName.equals(qualifiedName)) {
150                     return this;
151                 }
152                 String JavaDoc q;
153                 if (!base) {
154                     if (qualifiedName.startsWith(selfName+".")) {
155                         q = qualifiedName.substring(selfName.length()+1);
156                     } else {
157                         return null;
158                     }
159                 } else {
160                     q = qualifiedName;
161                 }
162                 // q now is stripped by self name (like 'de.gulden.application.beautyj' 'application.beautyj' if this is 'de.gulden')
163
String JavaDoc find;
164                 String JavaDoc firstPart;
165                 int dot = q.indexOf('.');
166                 if (dot!=-1) {
167                     firstPart = q.substring(0, dot);
168                     if (!base) {
169                         find = selfName + "." + firstPart;
170                     } else {
171                         find = firstPart;
172                     }
173                     // find can only be a subpackage of this, otherwise not found
174
} else {
175                     // can only be a sub-package of this package
176
find = qualifiedName;
177                 }
178                 NamedIterator it=getInnerPackages();
179                 Package JavaDoc p=(Package JavaDoc)it.find(find);
180                 if ((p!=null)&&(find != qualifiedName)) { // wasn't the final package to find
181
p = p.findPackage(qualifiedName); // recursion
182
}
183                 return p;
184     }
185
186     /**
187      * Returns an iterator that carries all classes and interfaces in this package.
188      */

189     public NamedIterator getClasses() {
190         NamedIterator it=new NamedIterator(myClass);
191         return it;
192     }
193
194     /**
195      * Returns an iterator that carries all classes and interfaces in this package
196      * and recursively in all its sub-packages.
197      */

198     public NamedIterator getAllClasses() {
199         Vector v=new Vector();
200         for (NamedIterator it=getClasses();it.hasMore();) {
201             v.addElement(it.next());
202         }
203         for (NamedIterator it=getInnerPackages();it.hasMore();) {
204             Package JavaDoc p=(Package JavaDoc)it.next();
205             for (NamedIterator it2=p.getAllClasses();it2.hasMore();) {
206                 v.addElement(it2.next());
207             }
208         }
209         return new NamedIterator(v);
210     }
211
212     /**
213      * Returns an iterator that carries all direct sub-packages of this package.
214      */

215     public NamedIterator getInnerPackages() {
216         return new NamedIterator(children);
217     }
218
219     /**
220      * Returns the parent package of this package.
221      */

222     public Package JavaDoc getParentPackage() {
223         return getPackage();
224     }
225
226     /**
227      * Add this package to its parent package <code>p</code>.
228      */

229     public void addToPackage(Package JavaDoc p) {
230         if (this.getName().startsWith(p.getName())) // legal to insert?
231
{
232             if (p.getName().equals(getName())) // target is same package
233
{
234                 // add all classes in this package
235
NamedIterator classes=getClasses();
236                 while (classes.hasMore()) {
237                     p.add((Class JavaDoc)classes.next());
238                 }
239                 // add child packages
240
NamedIterator packages=getInnerPackages();
241                 while (packages.hasMore()) {
242                     Package JavaDoc inner=(Package JavaDoc)packages.next();
243                     p.add(inner);
244                 }
245             }
246             else if (p.getName().equals(getParentPackage().getName())) // target is same as own _parent_ package
247
{
248                 NamedIterator it=p.getInnerPackages();
249                 Package JavaDoc inner=(Package JavaDoc)it.find(getName()); // already a sub-package with same name as this?
250
if (inner!=null) {
251                     inner.add(this); // yes
252
}
253                 else {
254                     p.registerPackage(this); // no
255
}
256             }
257             else {
258                 // add own parent package instead
259
p.add(getParentPackage());
260             }
261         }
262         else {
263             throw new IllegalArgumentException JavaDoc("cannot add package '"+getName()+"' into package '"+p.getName()+"'");
264         }
265     }
266
267     /**
268      * Output this object as XML.
269      *
270      * @return The XML tag.
271      * @see #initFromXML
272      */

273     public Element buildXML(Document d) {
274         Element e;
275         if (!isBasePackage()) {
276             e=super.buildXML(d);
277         } else {
278             e=d.getDocumentElement();
279         }
280         NamedIterator it=getClasses();
281         while (it.hasMore()) {
282             Class JavaDoc c=(Class JavaDoc)it.next();
283             e.appendChild(c.buildXML(d));
284         }
285         it=getInnerPackages();
286         while (it.hasMore()) {
287             Package JavaDoc p=(Package JavaDoc)it.next();
288             e.appendChild(p.buildXML(d));
289         }
290         return e;
291     }
292
293     /**
294      * Initialize this object from XML.
295      *
296      * @param element The XML tag.
297      * @throws IOException if an i/o error occurs
298      */

299     public void initFromXML(Element element) throws IOException {
300         // to be extended (not overwritten) by subclasses
301
String JavaDoc tagname=element.getTagName();
302         if (tagname.equals("package")) {
303             super.initFromXML(element);
304         } else if (tagname.equals("xjava")) { // special: treat <xjava> as base package tag
305
name="";
306         } else {
307             throw new IOException("illegal tag name "+tagname+" expected 'xjava' or 'package'");
308         }
309
310         initEmptyFathers();
311
312         // classes / interfaces
313
myClass.removeAllElements();
314
315         // classes
316
NodeList nl=XMLToolbox.getChildren(element,"class");
317         for (int i=0;i<nl.getLength();i++) {
318             Class JavaDoc cl=new Class JavaDoc();
319             cl.setPackage(this);
320             cl.initFromXML((Element)nl.item(i));
321         }
322
323         // interfaces
324
nl=XMLToolbox.getChildren(element,"interface");
325         for (int i=0;i<nl.getLength();i++) {
326             Class JavaDoc cl=new Class JavaDoc();
327             cl.setPackage(this);
328             cl.setInterface(true);
329             cl.initFromXML((Element)nl.item(i));
330         }
331
332         // inner packages
333
children.removeAllElements();
334         nl=XMLToolbox.getChildren(element,"package");
335         for (int i=0;i<nl.getLength();i++) {
336             Package JavaDoc pa=new Package JavaDoc();
337             pa.initFromXML((Element)nl.item(i));
338             registerPackage(pa);
339         }
340     }
341
342     /**
343      * Tests whether this represents the root package of a Java classpath.
344      * (A class C is member of the root package if you can invoke it by simply calling "java C".)
345      */

346     public boolean isBasePackage() {
347         return getName().equals("");
348     }
349
350     /**
351      * Returns the root package.
352      */

353     public Package JavaDoc getBasePackage() {
354         if (isBasePackage()) {
355             return this;
356         }
357         else {
358             return getPackage().getBasePackage();
359         }
360     }
361
362     /**
363      * Make sure that all parent packages exist.
364      */

365     protected void initEmptyFathers() {
366         if (!isBasePackage()) {
367             String JavaDoc fatherName=getParentPackageName(getName());
368             Package JavaDoc f=new Package JavaDoc();
369             f.setName(fatherName);
370             f.registerPackage(this);
371             f.initEmptyFathers();
372             myPackage=f;
373         }
374         else {
375             myPackage=null;
376         }
377     }
378
379     /**
380      * Add a class to this package. (Without caring about the class's own reference to <code>this</code>.)
381      */

382     void registerClass(Class JavaDoc c) {
383         if (!vectorContainsReference(myClass,c)) {
384             myClass.addElement(c);
385         }
386     }
387
388     /**
389      * Add a package to this package. (Without caring about the package's own reference to <code>this</code>.)
390      */

391     void registerPackage(Package JavaDoc p) {
392         children.addElement(p);
393     }
394
395     /**
396      * Initialize this object from parsed Java code.
397      */

398     void initFromAST(Node node) {
399         // to be extended (not overwritten) by subclasses
400
super.initFromAST(node);
401         initEmptyFathers();
402     }
403
404
405     // ------------------------------------------------------------------------
406
// --- static methods ---
407
// ------------------------------------------------------------------------
408

409     public static boolean isSourcePackage(Package JavaDoc base, String JavaDoc name) {
410         return (base.findPackage(name) != null);
411     }
412
413     /**
414      * Removes the last part of the package name from the end of the
415      * qualified name string.
416      */

417     static String JavaDoc getParentPackageName(String JavaDoc n) {
418         int pos=n.lastIndexOf('.');
419         if (pos!=-1) {
420             return n.substring(0,pos);
421         }
422         else {
423             return "";
424         }
425     }
426
427     /**
428      * Tests whether an objects <b>reference</b> is contained in the Vector.
429      * (<code>Vector.contains()</code> tests if elements match calling
430      * <code>equals()</code>.)
431      */

432     static boolean vectorContainsReference(Vector v, Object JavaDoc o) {
433         for (Enumeration e=v.elements();e.hasMoreElements();) {
434             if (e.nextElement()==o) {
435                 return true;
436             }
437         }
438         return false;
439     }
440
441 } // end Package
442
Popular Tags