001: /*
002: * Project: BeautyJ - Customizable Java Source Code Transformer
003: * Class: de.gulden.util.javasource.SourceObject
004: * Version: 1.1
005: *
006: * Date: 2004-09-29
007: *
008: * Note: Contains auto-generated Javadoc comments created by BeautyJ.
009: *
010: * This is licensed under the GNU General Public License (GPL)
011: * and comes with NO WARRANTY. See file license.txt for details.
012: *
013: * Author: Jens Gulden
014: * Email: beautyj@jensgulden.de
015: */
016:
017: package de.gulden.util.javasource;
018:
019: import de.gulden.util.javasource.jjt.Node;
020: import de.gulden.util.javasource.jjt.*;
021: import javax.xml.parsers.*;
022: import org.w3c.dom.*;
023: import java.io.*;
024: import java.util.*;
025:
026: /**
027: * This is the common super-class of all classes in this package which represent Java source code element declarations.
028: *
029: * @author Jens Gulden
030: * @version 1.0
031: */
032: public abstract class SourceObject implements Named, Serializable,
033: ParserTreeConstants {
034:
035: // ------------------------------------------------------------------------
036: // --- static field ---
037: // ------------------------------------------------------------------------
038:
039: /**
040: * The nl.
041: */
042: public static String nl = System.getProperty("line.separator");
043:
044: // ------------------------------------------------------------------------
045: // --- fields ---
046: // ------------------------------------------------------------------------
047:
048: /**
049: * The my package.
050: */
051: public Package myPackage;
052:
053: /**
054: * The name.
055: */
056: protected String name;
057:
058: // ------------------------------------------------------------------------
059: // --- methods ---
060: // ------------------------------------------------------------------------
061:
062: /**
063: * Returns the name.
064: */
065: public String getName() {
066: return name;
067: }
068:
069: /**
070: * Sets the name.
071: */
072: public void setName(String n) {
073: name = n;
074: }
075:
076: /**
077: * Gets the unqualified name of this, that means the name without any leading package information.
078: */
079: public String getUnqualifiedName() {
080: return unqualify(getName());
081: }
082:
083: /**
084: * Returns the package of which this is a member.
085: */
086: public Package getPackage() {
087: return myPackage;
088: }
089:
090: /**
091: * Initialize this object from XML.
092: *
093: * @param element The corresponding XML tag.
094: * @throws IOException if an i/o error occurs
095: */
096: public void initFromXML(Element element) throws IOException {
097: name = element.getAttribute("name");
098: // to be extended (not overwritten) by subclasses
099: }
100:
101: /**
102: * Output this object as XML.
103: *
104: * @return The XML tag.
105: * @see #initFromXML
106: */
107: public Element buildXML(Document d) {
108: // must be extended by subclasses (i.e. overwritten starting with ...=super.buildXML(..))
109: String tagName = getXMLName();
110: Element e = d.createElement(tagName);
111: e.setAttribute("name", getName());
112: return e;
113: }
114:
115: /**
116: * Returns the full signature of this SourceObject. This is the fully
117: * qualified name and, depending on the type of the SourceObject,
118: * the (return-)type, and the parameters' signatures
119: */
120: public String getSignature() {
121: // will be extended by subclasses
122: return getUnqualifiedName();
123: }
124:
125: /**
126: * Tests if two source objects represent the same source code element.
127: * The test is performed based on the fully referenced signature strings.
128: * Executing this is quite expensive, so consider testing for reference identity (a==b) where possible.
129: *
130: * @return <code>true</code> if equal.
131: */
132: public boolean equals(Object o) {
133: return o.getClass().equals(this .getClass())
134: && ((SourceObject) o).getSignature().equals(
135: this .getSignature());
136: }
137:
138: public String toString() {
139: return getXMLName() + " " + getSignature();
140: }
141:
142: /**
143: * Returns the name of the XML tag representing this SourceObject.
144: */
145: protected String getXMLName() {
146: return SourceObjectDeclaredVisible.unqualify(
147: this .getClass().getName()).toLowerCase(); // dynamically get tag name from class name (!)
148: }
149:
150: /**
151: * Initialize this object with values from parsed Java code.
152: *
153: * @param rootnode The corresponding node in the abstract syntax tree (AST).
154: */
155: void initFromAST(Node rootnode) {
156: // get name
157: name = rootnode.getName();
158: // to be extended (not overwritten) by subclasses
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 unqualify(String n) {
172: int pos = n.lastIndexOf('.');
173: if (pos != -1) {
174: return n.substring(pos + 1);
175: } else {
176: return n;
177: }
178: }
179:
180: /**
181: * Returns a less qualified name part of the string, including withParents parents, too.
182: *
183: * @param n The qualified name.
184: * @return The name's part after the withParent's-last occurrence of '.', or the unchanged name if no '.' is contained.
185: */
186: static String unqualify(String n, int withParents) {
187: int pos = n.lastIndexOf('.');
188: while ((pos > 0) && (withParents > 0)) {
189: pos = n.lastIndexOf('.', pos - 1);
190: withParents--;
191: }
192: if (pos != -1) {
193: return n.substring(pos + 1);
194: } else {
195: return n;
196: }
197: }
198:
199: /**
200: * Tool method for outputting a string to an OutputStream.
201: *
202: * @throws IOException if an i/o error occurs
203: */
204: static void write(OutputStream out, String s) throws IOException {
205: out.write(s.getBytes());
206: }
207:
208: /**
209: * Tool method for converting an array to a Vector.
210: */
211: static void stringsIntoVector(Object[] s, Vector v) {
212: v.removeAllElements();
213: for (int i = 0; i < s.length; i++) {
214: v.addElement(s[i]);
215: }
216: }
217:
218: } // end SourceObject
|