KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Project: BeautyJ - Customizable Java Source Code Transformer
3  * Class: de.gulden.util.javasource.SourceObject
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.javasource.jjt.Node;
20 import de.gulden.util.javasource.jjt.*;
21 import javax.xml.parsers.*;
22 import org.w3c.dom.*;
23 import java.io.*;
24 import java.util.*;
25
26 /**
27  * This is the common super-class of all classes in this package which represent Java source code element declarations.
28  *
29  * @author Jens Gulden
30  * @version 1.0
31  */

32 public abstract class SourceObject implements Named, Serializable, ParserTreeConstants {
33
34     // ------------------------------------------------------------------------
35
// --- static field ---
36
// ------------------------------------------------------------------------
37

38     /**
39      * The nl.
40      */

41     public static String JavaDoc nl = System.getProperty("line.separator");
42
43
44     // ------------------------------------------------------------------------
45
// --- fields ---
46
// ------------------------------------------------------------------------
47

48     /**
49      * The my package.
50      */

51     public Package JavaDoc myPackage;
52
53     /**
54      * The name.
55      */

56     protected String JavaDoc name;
57
58
59     // ------------------------------------------------------------------------
60
// --- methods ---
61
// ------------------------------------------------------------------------
62

63     /**
64      * Returns the name.
65      */

66     public String JavaDoc getName() {
67         return name;
68     }
69
70     /**
71      * Sets the name.
72      */

73     public void setName(String JavaDoc n) {
74         name=n;
75     }
76
77     /**
78      * Gets the unqualified name of this, that means the name without any leading package information.
79      */

80     public String JavaDoc getUnqualifiedName() {
81         return unqualify(getName());
82     }
83
84     /**
85      * Returns the package of which this is a member.
86      */

87     public Package JavaDoc getPackage() {
88         return myPackage;
89     }
90
91     /**
92      * Initialize this object from XML.
93      *
94      * @param element The corresponding XML tag.
95      * @throws IOException if an i/o error occurs
96      */

97     public void initFromXML(Element element) throws IOException {
98         name=element.getAttribute("name");
99         // to be extended (not overwritten) by subclasses
100
}
101
102     /**
103      * Output this object as XML.
104      *
105      * @return The XML tag.
106      * @see #initFromXML
107      */

108     public Element buildXML(Document d) {
109         // must be extended by subclasses (i.e. overwritten starting with ...=super.buildXML(..))
110
String JavaDoc tagName=getXMLName();
111         Element e=d.createElement(tagName);
112         e.setAttribute("name",getName());
113         return e;
114     }
115
116     /**
117      * Returns the full signature of this SourceObject. This is the fully
118      * qualified name and, depending on the type of the SourceObject,
119      * the (return-)type, and the parameters' signatures
120      */

121     public String JavaDoc getSignature() {
122         // will be extended by subclasses
123
return getUnqualifiedName();
124     }
125
126     /**
127      * Tests if two source objects represent the same source code element.
128      * The test is performed based on the fully referenced signature strings.
129      * Executing this is quite expensive, so consider testing for reference identity (a==b) where possible.
130      *
131      * @return <code>true</code> if equal.
132      */

133     public boolean equals(Object JavaDoc o) {
134         return o.getClass().equals(this.getClass())
135         && ((SourceObject)o).getSignature().equals(this.getSignature());
136     }
137
138     public String JavaDoc toString() {
139         return getXMLName()+" "+getSignature();
140     }
141
142     /**
143      * Returns the name of the XML tag representing this SourceObject.
144      */

145     protected String JavaDoc getXMLName() {
146         return SourceObjectDeclaredVisible.unqualify(this.getClass().getName()).toLowerCase(); // dynamically get tag name from class name (!)
147
}
148
149     /**
150      * Initialize this object with values from parsed Java code.
151      *
152      * @param rootnode The corresponding node in the abstract syntax tree (AST).
153      */

154     void initFromAST(Node rootnode) {
155         // get name
156
name=rootnode.getName();
157         // to be extended (not overwritten) by subclasses
158
}
159
160
161     // ------------------------------------------------------------------------
162
// --- static methods ---
163
// ------------------------------------------------------------------------
164

165     /**
166      * Returns the unqualified name part of the string.
167      *
168      * @param n The qualified name.
169      * @return The name's part after the last occurrence of '.', or the unchanged name if no '.' is contained.
170      */

171     static String JavaDoc unqualify(String JavaDoc n) {
172         int pos=n.lastIndexOf('.');
173         if (pos!=-1) {
174             return n.substring(pos+1);
175         }
176         else {
177             return n;
178         }
179     }
180
181     /**
182      * Returns a less qualified name part of the string, including withParents parents, too.
183      *
184      * @param n The qualified name.
185      * @return The name's part after the withParent's-last occurrence of '.', or the unchanged name if no '.' is contained.
186      */

187     static String JavaDoc unqualify(String JavaDoc n, int withParents) {
188         int pos=n.lastIndexOf('.');
189         while ((pos > 0) && (withParents > 0)) {
190             pos = n.lastIndexOf('.', pos-1);
191             withParents--;
192         }
193         if (pos != -1) {
194             return n.substring(pos+1);
195         } else {
196             return n;
197         }
198     }
199
200     /**
201      * Tool method for outputting a string to an OutputStream.
202      *
203      * @throws IOException if an i/o error occurs
204      */

205     static void write(OutputStream out, String JavaDoc s) throws IOException {
206         out.write(s.getBytes());
207     }
208
209     /**
210      * Tool method for converting an array to a Vector.
211      */

212     static void stringsIntoVector(Object JavaDoc[] s, Vector v) {
213         v.removeAllElements();
214         for (int i=0;i<s.length;i++) {
215             v.addElement(s[i]);
216         }
217     }
218
219 } // end SourceObject
220
Popular Tags