001: /*
002: * Project: BeautyJ - Customizable Java Source Code Transformer
003: * Class: de.gulden.util.javasource.Import
004: * Version: 1.0
005: *
006: * Date: 2002-10-27
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 de.gulden.util.xml.XMLToolbox;
022: import javax.xml.parsers.*;
023: import org.w3c.dom.*;
024: import java.io.*;
025: import java.util.*;
026:
027: /**
028: * Represent an import statement.
029: *
030: * @author Jens Gulden
031: * @version 1.0
032: */
033: public abstract class Import extends SourceObject implements
034: ParserTreeConstants {
035:
036: // ------------------------------------------------------------------------
037: // --- constructor ---
038: // ------------------------------------------------------------------------
039: /**
040: * Creates a new instance of Import.
041: */
042: public Import(Package p) {
043: myPackage = p;
044: }
045:
046: // ------------------------------------------------------------------------
047: // --- methods ---
048: // ------------------------------------------------------------------------
049: /**
050: * Fully qualifies a class identifier, if it matches the import statement.
051: *
052: * @return The qualified class identifier, or <code>null</code> if the identifer could not be qualified by this import.
053: */
054: public abstract String qualify(String name);
055:
056: /**
057: * Output this object as XML.
058: *
059: * @return The XML tag.
060: * @see #initFromXML
061: */
062: public Element buildXML(Document d) {
063: // overwrites SourceObject.buildXML
064: // will be overloaded by subclasses
065: String tagName = getXMLName();
066: Element e = d.createElement(tagName);
067: e.appendChild(d.createTextNode(getName()));
068: return e;
069: }
070:
071: /**
072: * Returns the name of the XML tag representing this SourceObject.
073: */
074: protected String getXMLName() {
075: return "import";
076: }
077:
078: // ------------------------------------------------------------------------
079: // --- static methods ---
080: // ------------------------------------------------------------------------
081: /**
082: * Create a new Import-object, specified by the XML element.
083: * The created object will either be of type ImportPackage or ImportClass.
084: *
085: * @throws IOException if an i/o error occurs
086: */
087: public static Import createFromXML(Package parent, Element element)
088: throws IOException {
089: String name = XMLToolbox.getText(element);//element.getAttribute("name");
090: String kind = element.getAttribute("kind");
091: boolean isClass = kind.equals("class");
092: if (!isClass) {
093: return new ImportPackage(parent, name);
094: } else {
095: return new ImportClass(parent, name);
096: }
097: }
098:
099: /**
100: * Create a new Import-object.
101: * The created object will either be of type ImportPackage or ImportClass.
102: */
103: static Import createFromAST(Package p, Node node) {
104: Import im;
105: if (node.getChild(JJT_PACKAGEIMPORT) != null) // sub-node 'ImportPackage' exists
106: {
107: im = new ImportPackage(p);
108: im.setName(node.getName() + ".*");
109: } else {
110: im = new ImportClass(p);
111: im.setName(node.getName());
112: }
113: return im;
114: }
115:
116: } // end Import
|