treebuilder/Attribute.java 0100600 0000764 0000764 00000003433 10123343744 014625 0 ustar iank iank /** \file * * Sep 4, 2004 * * Copyright Ian Kaplan 2004, Bear Products International * * You may use this code for any purpose, without restriction, * including in proprietary code for which you charge a fee. * In using this code you acknowledge that you understand its * function completely and accept all risk in its use. * * @author Ian Kaplan, www.bearcave.com, iank@bearcave.com */ package treebuilder; /** * Attribute * Sep 4, 2004 *
An XML attribute object. This object is a subclass of NamedEntity. The fields in the NamedEntity superclass store the attribute name information.
According to the XmlPullParser documentation, if the parser is non-validating the type is always CDATA. At the time of this writing there were no validating XmlPullParsers, so the type field will always be CDATA, making this field rather useless.
* @author Ian Kaplan, www.bearcave.com, iank@bearcave.com */ public class Attribute extends NamedEntity { /** attribute value */ private String mValue = null; /** attribute type */ private String mType = null; public Attribute(String name ) { super( TreeNodeType.ATTRIBUTE, name ); } public Attribute( String name, String prefix, String namespace ) { super(TreeNodeType.ATTRIBUTE, name, prefix, namespace ); } public String getValue() { return mValue; } public void setValue( String value ) { mValue = value; } public String getAttrType() { return mType; } public void setAttrType( String ty ) { mType = ty; } public TreeNode getChild() { return null; } public void setChild(TreeNode child) {} public Attribute getNext() { return (Attribute)this.getSibling(); } public void setNext( Attribute attr ) { this.setSibling( attr ); } } treebuilder/AttributeList.java 0100600 0000764 0000764 00000005476 10123651025 015464 0 ustar iank iank /** \file * * Sep 4, 2004 * * Copyright Ian Kaplan 2004, Bear Products International * * You may use this code for any purpose, without restriction, * including in proprietary code for which you charge a fee. * In using this code you acknowledge that you understand its * function completely and accept all risk in its use. * * @author Ian Kaplan, www.bearcave.com, iank@bearcave.com */ package treebuilder; import java.util.Iterator; import java.util.NoSuchElementException; /** * AttributeList * Sep 4, 2004 *A list of Attribute objects. The AttributeList is used to as a container for a set of XML tag attributes.
* @author Ian Kaplan, www.bearcave.com, iank@bearcave.com */ public class AttributeList { private static class AttrListIter implements Iterator { private Attribute mCurrent = null; public AttrListIter( Attribute attrList ) { mCurrent = attrList; } public boolean hasNext() { boolean rslt = mCurrent != null; return rslt; } public Object next() throws NoSuchElementException { if (mCurrent != null) { Attribute attr = mCurrent; mCurrent = mCurrent.getNext(); return attr; } else { throw new NoSuchElementException(); } } public void remove() throws UnsupportedOperationException { throw new UnsupportedOperationException(); } } // static class AttrListIter private Attribute mHead = null; private Attribute mTail = null; public boolean hasAttributes() { return mHead != null; } /** Add to the end of the attribute list */ public void append( Attribute attr ) { if (attr != null) { if (mHead == null) { mHead = attr; mTail = attr; } else { mTail.setNext( attr ); mTail = attr; } } } // append /** Insert at the head (front) of the attribute list */ public void insert( Attribute attr ) { if (attr != null) { if (mHead == null) { mHead = attr; mTail = attr; } else { attr.setNext( mHead ); mHead = attr; } } } // insert /** Get an Iterator for the attribute listNote that the Iterator returned does not support the remove() method and will throw and UnsupportedOperationException if remove() is called.
*/ public Iterator getIterator() { AttrListIter iter = new AttrListIter( mHead ); return iter; } // getIterator } treebuilder/EndTag.java 0100600 0000764 0000764 00000001330 10123344273 014014 0 ustar iank iank /** \file * * Sep 13, 2004 * * Copyright Ian Kaplan 2004, Bear Products International * * You may use this code for any purpose, without restriction, * including in proprietary code for which you charge a fee. * In using this code you acknowledge that you understand its * function completely and accept all risk in its use. * * @author Ian Kaplan, www.bearcave.com, iank@bearcave.com */ package treebuilder; /** * EndTag * Sep 13, 2004 *An XML end tag.
* @author Ian Kaplan, www.bearcave.com, iank@bearcave.com */ public class EndTag extends NamedEntity { /** * @param ty * @param name */ public EndTag(String name) { super(TreeNodeType.END_TAG, name); } } treebuilder/Main.java 0100600 0000764 0000764 00000005532 10124122404 013535 0 ustar iank iank /** \file * * Sep 14, 2004 * * Copyright Ian Kaplan 2004, Bear Products International * * You may use this code for any purpose, without restriction, * including in proprietary code for which you charge a fee. * In using this code you acknowledge that you understand its * function completely and accept all risk in its use. * * @author Ian Kaplan, www.bearcave.com, iank@bearcave.com */ package treebuilder; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import org.xmlpull.v1.XmlPullParserException; /** * Main * Sep 14, 2004 *Read an XML file, build an in-memory tree that mirrors the XML structure and then serialize the in-memory tree back to XML.
* @author Ian Kaplan, www.bearcave.com, iank@bearcave.com */ public class Main { private void usage() { String name = getClass().getName(); System.out.println("usage: " + name + "An object that has an XML name. That is, an optional name space, a optional prefix and an name.
* @author Ian Kaplan, www.bearcave.com, iank@bearcave.com */ public class NamedEntity extends TreeNode { /** The namespace, if one is specified (e.g., like http://www.bearcave.com/expression) */ private String mNamespace = null; /** The prefix associated with this tag, if there is one. This prefix should also be associated with a namespace */ private String mPrefix = null; /** The name of the tag or attribute. For text, this will be TEXT. This is always the local name (in org.w3c.dom terms) */ private String mName = null; /** * @param ty */ public NamedEntity(TreeNodeType ty) { super(ty); } public NamedEntity(TreeNodeType ty, String name ) { super(ty); mName = name; } public NamedEntity(TreeNodeType ty, String name, String prefix, String namespace ) { super(ty); mName = name; mPrefix = prefix; mNamespace = namespace; } public String getNamespace() { return mNamespace; } public void setNamesapce(String namespace ) { mNamespace = namespace; } public String getPrefix() { return mPrefix; } public void setPrefix( String prefix ) { mPrefix = prefix; } public String getName() { return mName; } public void setName( String name ) { mName = name; } public String toString() { String name = mName; if (mPrefix != null) { name = mPrefix + ':' + name; } return name; } // toString } treebuilder/TagNode.java 0100600 0000764 0000764 00000002445 10123645406 014205 0 ustar iank iank /** \file * * Sep 4, 2004 * * Copyright Ian Kaplan 2004, Bear Products International * * You may use this code for any purpose, without restriction, * including in proprietary code for which you charge a fee. * In using this code you acknowledge that you understand its * function completely and accept all risk in its use. * * @author Ian Kaplan, www.bearcave.com, iank@bearcave.com */ package treebuilder; /** * TagNode *An XML tag node. This class is derived from a NamedEntity, which is an XML object with a name (this include a tag and an attribute). It extends this object with an attribute list (something a tag node can have but and attribute node cannot).
*/ public class TagNode extends NamedEntity { /** attribute list (or null, if there is no attribute list) */ private AttributeList mAttrList = null; public TagNode() { super( TreeNodeType.TAG ); } public TagNode( String tagName ) { super( TreeNodeType.TAG, tagName ); } public TagNode( String tagName, String prefix, String namespace ) { super( TreeNodeType.TAG, tagName, prefix, namespace ); } public AttributeList getAttrList() { return mAttrList; } public void setAttrList( AttributeList attrList ) { mAttrList = attrList; } } treebuilder/TextNode.java 0100600 0000764 0000764 00000002236 10123344776 014422 0 ustar iank iank /** \file * * Sep 19, 2004 * * Copyright Ian Kaplan 2004, Bear Products International * * You may use this code for any purpose, without restriction, * including in proprietary code for which you charge a fee. * In using this code you acknowledge that you understand its * function completely and accept all risk in its use. * * @author Ian Kaplan, www.bearcave.com, iank@bearcave.com */ package treebuilder; /** * TextNode * Sep 19, 2004 *An XML text node. "Text" in XML consists of the characters between two tags. This may be white space.
* @author Ian Kaplan, www.bearcave.com, iank@bearcave.com */ public class TextNode extends TreeNode { private String mText = null; /** * @param ty */ public TextNode(String text) { super(TreeNodeType.TEXT); mText = text; } /** Used for building COMMENT nodes. Like text nodes, comment nodes basicly consist of a String. */ public TextNode(TreeNodeType ty, String text) { super(ty); mText = text; } public String getText() { return mText; } public String toString() { return mText; } } treebuilder/TreeBuilder.java 0100600 0000764 0000764 00000021756 10124116101 015062 0 ustar iank iank /** \file * * Sep 12, 2004 * * Copyright Ian Kaplan 2004, Bear Products International * * You may use this code for any purpose, without restriction, * including in proprietary code for which you charge a fee. * In using this code you acknowledge that you understand its * function completely and accept all risk in its use. * * @author Ian Kaplan, www.bearcave.com, iank@bearcave.com */ package treebuilder; import java.io.FileReader; import java.io.IOException; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserFactory; /** * TreeBuilder * Sep 12, 2004 *This class supports the construction of a light weight in memory tree from an XML file. Like the W3C DOM Document object, the structure of this tree reflects the structure of the XML.
The tree that is constructed is designed for rapid traversal and in memory modification. It also has the advantage of using less memory than the java.sun.com DOM Document implementation.
This code demonstrates how little source code is required to parse XML using the XmlPullParser.
A few notes about attributes and namespaces. In general I think that the XmlPullParser rocks. The API is will designed and the calls mostly make sense. But... the XmlPullParser does not treat all attributes the same way. In particular it does not treat name space definitions like other attributes. This can be seen in XML designed to be processed via a schema. For example:
<ex:EXPRESSION xmlns:ex="http://www.bearcave.com/expression" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.bearcave.com/expression xmlexpr/expression.xsd">
Here there are two name space definitions. One defining the namespace associated with the "ex" prefix and one associated with XML schemas (for the schema location attribute).
The getAttributeCount() method will return 1 when processing the EXPRESSION tag. This is because the name space definitions (the attributes with the xmlns prefix) are not treated as normal attributes. And sometimes this is good, because these attributes are not necessarily of interest. In this case, however, the intent is to exactly mirror the XML in an in-memory tree. So that if the tree is serialized the original XML will be recovered (with the exception of white space TEXT, since this is not included).
The attributes are only available when the END_TAG element for the document tag is processed. The getDepth() method tells the current XML nesting depth, so it can be determined that an END_TAG is the document end tag. The attributes are then fetched and prepended to the attribute list. Sort of awkward. This is the one place where I would differ in the design of the XmlPullParser. I'd treat all attributes the same way, including those with the "xmlns" prefix. Then the user can simply ignore operands with the namespace prefix.
* @author Ian Kaplan, www.bearcave.com, iank@bearcave.com */ public class TreeBuilder { private XmlPullParser mParser = null; private TreeNode mDocumentTag = null; private Attribute buildAttr( int index ) { String name = mParser.getAttributeName( index ); String prefix = mParser.getAttributePrefix( index ); String namespace = mParser.getAttributeNamespace( index ); Attribute attr = new Attribute( name, prefix, namespace ); String attrType = mParser.getAttributeType( index ); String attrVal = mParser.getAttributeValue( index ); attr.setAttrType( attrType ); attr.setValue( attrVal ); return attr; } // buildAttr private Attribute buildNS( int index ) throws XmlPullParserException { String nsName = mParser.getNamespacePrefix( index ); String uri = mParser.getNamespaceUri( index ); Attribute attr = new Attribute( nsName, "xmlns", uri ); attr.setValue( uri ); return attr; } /** This method is called for the end tag of the root document tag. If there are name spaces, they would have been defined in this tag. Insert them in the front of the attribute list. * @throws XmlPullParserException */ private void addNamespaces( int depth ) throws XmlPullParserException { TagNode tag = (TagNode)mDocumentTag; AttributeList attrList = tag.getAttrList(); int nsStart = mParser.getNamespaceCount( depth-1 ); int nsEnd = mParser.getNamespaceCount( depth ); for (int i = nsEnd-1; i >= nsStart; i--) { Attribute attr = buildNS( i ); attrList.insert( attr ); } } // addNamespaces /** Build a tag node. Note that a tag node always has an AttributeList object, even if there is no attribute list. This wastes some memory, but in theory should make the tree processing more regular, since it can always be assumed that this object exits. */ private TagNode buildTagNode() { String name = mParser.getName(); String prefix = mParser.getPrefix(); String namespace = mParser.getNamespace(); TagNode tag = new TagNode( name, prefix, namespace ); AttributeList attrList = new AttributeList(); int numAttr = mParser.getAttributeCount(); for (int i = 0; i < numAttr; i++) { Attribute attr = buildAttr( i ); attrList.append( attr ); } // for tag.setAttrList( attrList ); return tag; } // buildTagNode /** Recursively parse an XML file into an in-memory tree data structure.Currently this code only handles the COMMENT, TEXT and START_TAG elements returned by the XmlPullParser. Other XML elements are ignored.
* @throws XmlPullParserException * @throws IOException */ private TreeNode buildTree() throws XmlPullParserException, IOException { TreeNode root = null; TreeNode child = null; TreeNode curSib = null; boolean done = false; do { int event = mParser.nextToken(); if (event == XmlPullParser.START_TAG) { root = buildTagNode(); if (mParser.getDepth() == 1) { mDocumentTag = root; } for (TreeNode t = buildTree(); !(t instanceof EndTag); t = buildTree()) { if (child == null) { child = t; curSib = child; } else { curSib.setSibling( t ); curSib = t; } } // for root.setChild( child ); done = true;; } else if (event == XmlPullParser.COMMENT) { String comment = mParser.getText(); root = new TextNode( TreeNodeType.COMMENT, comment ); done = true; } else if (event == XmlPullParser.TEXT) { String text = mParser.getText(); root = new TextNode( text ); done = true; } else if (event == XmlPullParser.END_TAG) { String name = mParser.getName(); root = new EndTag( name ); int depth = mParser.getDepth(); if (depth == 1) { // add the namespace attribtues to the document tag, if they exist addNamespaces( depth ); } done = true; } else if (event == XmlPullParser.END_DOCUMENT) { root = new EndTag( "END DOCUMENT" ); done = true; } } while (! done); return root; } // buildTree /** Allocate and initial an XmlPullParserAt the time this code was written the XmlPullParser did not support validation, so the call to setValidating() is passed "false".
*/ private XmlPullParser getParser() throws XmlPullParserException { XmlPullParserFactory factory; factory = XmlPullParserFactory.newInstance(); factory.setNamespaceAware( true ); factory.setValidating( false ); XmlPullParser parser = factory.newPullParser(); return parser; } // getParser /** This is the public entry point for the TreeBuilder. It is passed a FileReader, which has been opened for an XML file. */ public TreeNode parseXML( FileReader reader ) throws XmlPullParserException, IOException { TreeNode root = null; mParser = getParser(); if (mParser != null) { mParser.setInput( reader ); root = buildTree(); } return root; } // parseXML } treebuilder/TreeNode.java 0100600 0000764 0000764 00000005105 10121663035 014361 0 ustar iank iank /** \file * * Sep 4, 2004 * * Copyright Ian Kaplan 2004, Bear Products International * * You may use this code for any purpose, without restriction, * including in proprietary code for which you charge a fee. * In using this code you acknowledge that you understand its * function completely and accept all risk in its use. * * @author Ian Kaplan, www.bearcave.com, iank@bearcave.com */ package treebuilder; /** * treeNode * A light weight XML tree node */ public abstract class TreeNode { /** Node type: tag, attribute, text */ private TreeNodeType mType = null; /** if (this) is a child, mParent points to the parent node */ private TreeNode mParent = null; /** reference to a child node */ private TreeNode mChild = null; /** reference to the next sibling node */ private TreeNode mSibling = null; /** reference to the predecessor to (this) sibling */ private TreeNode mSibPred = null; public TreeNode( TreeNodeType ty ) { mType = ty; } public TreeNodeType getType() { return mType; } public boolean isLeaf() { return (getChild() == null); } public void setParent(TreeNode parent) { mParent = parent; } public TreeNode getParent() { return mParent; } public void setChild(TreeNode child) { mChild = child; if (child != null) { child.setParent( this ); } } public TreeNode getChild() { return mChild; } public TreeNode getSecondChild() { TreeNode child = null; if (getChild() != null) { child = getChild().getSibling(); } return child; } public TreeNode getThirdChild() { TreeNode child = null; if (getChild() != null) { TreeNode tmp = getChild().getSibling(); if (tmp != null) { child = tmp.getSibling(); } } return child; } /** Get child N, where N = {1, 2, ... n} */ public TreeNode getChildN( int n ) { TreeNode nthChild = getChild(); for (int i = 1; i < n && nthChild != null; i++) { nthChild = nthChild.getSibling(); } return nthChild; } public void setSibling(TreeNode sibling) { mSibling = sibling; if (sibling != null) { sibling.setSibPred( this ); } } public TreeNode getSibling() { return mSibling; } public void setSibPred(TreeNode pred ) { mSibPred = pred; } public TreeNode getSibPred() { return mSibPred; } public abstract String toString(); } treebuilder/TreeNodeType.java 0100700 0000764 0000764 00000002044 10123327720 015223 0 ustar iank iank /* * Created on Jul 12, 2004 * * Copyright Ian Kaplan 2004, Bear Products International * * You may use this code for any purpose, without restriction, * including in proprietary code for which you charge a fee. * In using this code you acknowledge that you understand its * function completely and accept all risk in its use. * * @author Ian Kaplan, www.bearcave.com, iank@bearcave.com */ package treebuilder; /** * TreeNodeType * * @author Ian Kaplan, iank@bearcave.com * */ public class TreeNodeType extends TypeSafeEnum { public TreeNodeType( String typeName ) { super( typeName, TreeNodeType.class ); } String getString() { return this.toString(); } public static TreeNodeType ATTRIBUTE = new TreeNodeType("ATTRIBUTE"); public static TreeNodeType COMMENT = new TreeNodeType("COMMENT"); public static TreeNodeType END_TAG = new TreeNodeType("END_TAG"); public static TreeNodeType TAG = new TreeNodeType("TAG"); public static TreeNodeType TEXT = new TreeNodeType("TEXT"); } treebuilder/TreeToString.java 0100600 0000764 0000764 00000006776 10124122534 015261 0 ustar iank iank package treebuilder; /** \file * * Sep 13, 2004 * * Copyright Ian Kaplan 2004, Bear Products International * * You may use this code for any purpose, without restriction, * including in proprietary code for which you charge a fee. * In using this code you acknowledge that you understand its * function completely and accept all risk in its use. * * @author Ian Kaplan, www.bearcave.com, iank@bearcave.com */ /** * TreeToString * A class to support representation of the TreeNode tree as a String. Note that there is a similar class, TreeToXML which will serialize the in-memory tree back to XML.For those of you who speak the "gang of four" Design Patterns languauge, this class follows a "visitor" pattern.
*/ public class TreeToString { private final char startChar = '<'; private final char endChar = '>'; private TreeNode mRoot; private StringBuffer mBuf = null; private final int INDENT = 2; private int mIndentLevel; private void indent() { for (int i = 0; i < mIndentLevel; i++) { mBuf.append(' '); } } // indent private void openAngle() { if (mIndentLevel > 0) { mBuf.append('\n'); } indent(); mBuf.append( startChar ); mIndentLevel += INDENT; } // openAngle private void closeAngle( boolean first ) { assert mIndentLevel > 0; mIndentLevel -= INDENT; if (! first ) { mBuf.append('\n'); indent(); } mBuf.append( endChar ); mBuf.append(' '); } // closeAngle private void nodeToString( TreeNode node ) { TreeNodeType ty = node.getType(); String nodeStr = node.toString(); if (ty == TreeNodeType.TEXT) { nodeStr = nodeStr.trim(); } else if (ty == TreeNodeType.COMMENT) { nodeStr = "\n<--" + nodeStr + "-->"; } if (nodeStr.length() > 0) { // String tyName = ty.toString(); // mBuf.append( tyName ); // mBuf.append(':'); mBuf.append( nodeStr ); mBuf.append(' '); } } // nodeToString private void leavesToString( TreeNode root ) { if (root != null) { for (TreeNode n = root.getChild(); n != null; n = n.getSibling()) { if (! n.isLeaf()) { rootToString( n ); } else { nodeToString( n ); } } } } // leavesToString private void rootToString( TreeNode root ) { boolean first = true; if (root != null) { openAngle(); nodeToString(root ); if (! root.isLeaf()) { // foreach child of root ... for (TreeNode n = root.getChild(); n != null; n = n.getSibling()) { if (! n.isLeaf()) { first = false; } } // for leavesToString( root ); } // if root is not a leaf closeAngle( first ); } } // rootToString public TreeToString( TreeNode root ) { mRoot = root; } public String toString() { mBuf = new StringBuffer(); for (TreeNode t = mRoot; t != null; t = t.getSibling()) { mIndentLevel = 0; rootToString( t ); mBuf.append('\n'); } return mBuf.toString(); } // toString } treebuilder/TreeToXML.java 0100600 0000764 0000764 00000011227 10124116445 014442 0 ustar iank iank /** \file * * Sep 19, 2004 * * Copyright Ian Kaplan 2004, Bear Products International * * You may use this code for any purpose, without restriction, * including in proprietary code for which you charge a fee. * In using this code you acknowledge that you understand its * function completely and accept all risk in its use. * * @author Ian Kaplan, www.bearcave.com, iank@bearcave.com */ package treebuilder; import java.util.Iterator; /** * TreeToXML * Sep 19, 2004 *Traverse the in-memory tree and build an XML representation for the tree. Leaving asside white space, this XML should be the same as the original XML that was read to build the tree. Or at least the same relative to the supported XML elements. For example, the TreeBuilder code does not support the "documentation" elements (DOCDECL).
* @author Ian Kaplan, www.bearcave.com, iank@bearcave.com */ public class TreeToXML { private TreeNode mRoot; private StringBuffer mBuf = null; private final int INDENT = 4; private int mIndentLevel; private void indent() { for (int i = 0; i < mIndentLevel; i++) { mBuf.append(' '); } } // indent private void openTag() { if (mIndentLevel > 0) { mBuf.append('\n'); } indent(); mIndentLevel += INDENT; } // openTag private void closeTag( boolean first ) { assert mIndentLevel > 0; mIndentLevel -= INDENT; if (! first ) { mBuf.append('\n'); indent(); } } // closeTag private void serializeAttribute( Attribute attr ) { String attrName = attr.toString(); mBuf.append( attrName ); mBuf.append("=\""); String attrVal = attr.getValue(); mBuf.append( attrVal ); mBuf.append('"'); } // serializeAttribute private void serializeTag( TreeNode node ) { String tagName = node.toString(); mBuf.append("<"); mBuf.append( tagName ); AttributeList attrList = ((TagNode)node).getAttrList(); if (attrList != null) { Iterator iter = attrList.getIterator(); while (iter.hasNext()) { Attribute attr = (Attribute)iter.next(); mBuf.append(' '); serializeAttribute( attr ); } // while } if (node.isLeaf()) { mBuf.append("/>"); } else { mBuf.append('>'); } } // serializeTag private void serializeNode( TreeNode node ) { if (node != null) { TreeNodeType ty = node.getType(); if (ty == TreeNodeType.TAG) { serializeTag( node ); } else { String nodeStr = node.toString(); if (ty == TreeNodeType.COMMENT) { nodeStr = "\n<--" + nodeStr + "-->"; } mBuf.append( nodeStr ); } } } // serializeNode private void endTag( TreeNode root ) { if (root != null && root.getType() == TreeNodeType.TAG) { String tagName = root.toString(); mBuf.append(""); mBuf.append( tagName ); mBuf.append('>'); } } // endTag private void leavesToString( TreeNode root ) { if (root != null) { for (TreeNode n = root.getChild(); n != null; n = n.getSibling()) { if (! n.isLeaf()) { rootToString( n ); } else { serializeNode( n ); } } } } // leavesToString private void rootToString( TreeNode root ) { boolean first = true; if (root != null) { openTag(); serializeNode( root ); if (! root.isLeaf()) { // foreach child of root ... for (TreeNode n = root.getChild(); n != null; n = n.getSibling()) { if (! n.isLeaf()) { first = false; } } // for leavesToString( root ); endTag( root ); } // if root is not a leaf closeTag( first ); } } // rootToString public TreeToXML( TreeNode root ) { mRoot = root; } public String toString() { mBuf = new StringBuffer(); for (TreeNode t = mRoot; t != null; t = t.getSibling()) { mIndentLevel = 0; rootToString( t ); mBuf.append('\n'); } return mBuf.toString(); } // toString } treebuilder/TypeSafeEnum.java 0100700 0000764 0000764 00000005670 10116427032 015230 0 ustar iank iank /* * Created on Jul 12, 2004 * * Copyright Ian Kaplan 2004, Bear Products International * * You may use this code for any purpose, without restriction, * including in proprietary code for which you charge a fee. * In using this code you acknowledge that you understand its * function completely and accept all risk in its use. * * @author Ian Kaplan, www.bearcave.com, iank@bearcave.com */ package treebuilder; /** * TypeSafeEnum * * @author Ian Kaplan, iank@bearcave.com, Jul 12, 2004 * */ import java.util.ArrayList; import java.util.Iterator; /** Yet another type safe enumeration base class. This class supports the creation of multiple enumeration subclasses. In each subclass the enumeration values start at 0.Documentation for this class can be found here. Apparently TypeSafeEnums are supported in the new version of Java. So at some point the classes that use this class should be replaced with something more generic.
*/ public abstract class TypeSafeEnum { private static class enumInfo { public int hashCode; public int count; public ArrayList values; enumInfo( int hash ) { hashCode = hash; count = 0; values = new ArrayList(); } } // class enumInfo private static ArrayList infoVec = new ArrayList(); private String mName; private int mValue; public TypeSafeEnum( String name, Class cls ) { mName = name; enumInfo elem = findInfo( cls, true ); mValue = elem.count; elem.count++; elem.values.add( this ); } // TypeSafeEnum constructor public static Iterator enumValues( Class cls ) { Iterator iter = null; enumInfo elem = findInfo( cls, false ); if (elem != null) { iter = elem.values.iterator(); } return iter; } // enumValues public String getName() { return mName; } public int getValue() { return mValue; } public String toString() { return getName(); } /** Find the entry for the enumeration, if it exists. If not, add it to the end of the enumInfo. Note that this function has linear time, but the assumption is that there will not a large number of enumeration classes. */ private static enumInfo findInfo(Class cls, boolean add) { enumInfo foundElem = null; int hashCode = cls.hashCode(); for (Iterator iter = infoVec.iterator(); iter.hasNext(); ) { enumInfo elem = (enumInfo)iter.next(); if (elem.hashCode == hashCode) { foundElem = elem; break; } } if (foundElem == null && add) { foundElem = new enumInfo(hashCode); infoVec.add( foundElem ); } return foundElem; } // findInfo } treebuilder/doxygenTreeBuilder 0100600 0000764 0000764 00000127433 10124123061 015542 0 ustar iank iank # Doxyfile 1.3.3 # This file describes the settings to be used by the documentation system # doxygen (www.doxygen.org) for a project # # All text after a hash (#) is considered a comment and will be ignored # The format is: # TAG = value [value, ...] # For lists items can also be appended using: # TAG += value [value, ...] # Values that contain spaces should be placed between quotes (" ") #--------------------------------------------------------------------------- # General configuration options #--------------------------------------------------------------------------- # The PROJECT_NAME tag is a single word (or a sequence of words surrounded # by quotes) that should identify the project. PROJECT_NAME = "Building an in-memory tree using the XmlPullParser" # The PROJECT_NUMBER tag can be used to enter a project or revision number. # This could be handy for archiving the generated documentation or # if some version control system is used. PROJECT_NUMBER = # The OUTPUT_DIRECTORY tag is used to specify the (relative or absolute) # base path where the generated documentation will be put. # If a relative path is entered, it will be relative to the location # where doxygen was started. If left blank the current directory will be used. OUTPUT_DIRECTORY = treebuilder_doc # The OUTPUT_LANGUAGE tag is used to specify the language in which all # documentation generated by doxygen is written. Doxygen will use this # information to generate all constant output in the proper language. # The default language is English, other supported languages are: # Brazilian, Catalan, Chinese, Chinese-Traditional, Croatian, Czech, Danish, Dutch, # Finnish, French, German, Greek, Hungarian, Italian, Japanese, Japanese-en # (Japanese with English messages), Korean, Norwegian, Polish, Portuguese, # Romanian, Russian, Serbian, Slovak, Slovene, Spanish, Swedish, and Ukrainian. OUTPUT_LANGUAGE = English # This tag can be used to specify the encoding used in the generated output. # The encoding is not always determined by the language that is chosen, # but also whether or not the output is meant for Windows or non-Windows users. # In case there is a difference, setting the USE_WINDOWS_ENCODING tag to YES # forces the Windows encoding (this is the default for the Windows binary), # whereas setting the tag to NO uses a Unix-style encoding (the default for # all platforms other than Windows). USE_WINDOWS_ENCODING = YES # If the EXTRACT_ALL tag is set to YES doxygen will assume all entities in # documentation are documented, even if no documentation was available. # Private class members and static file members will be hidden unless # the EXTRACT_PRIVATE and EXTRACT_STATIC tags are set to YES EXTRACT_ALL = YES # If the EXTRACT_PRIVATE tag is set to YES all private members of a class # will be included in the documentation. EXTRACT_PRIVATE = YES # If the EXTRACT_STATIC tag is set to YES all static members of a file # will be included in the documentation. EXTRACT_STATIC = YES # If the EXTRACT_LOCAL_CLASSES tag is set to YES classes (and structs) # defined locally in source files will be included in the documentation. # If set to NO only classes defined in header files are included. EXTRACT_LOCAL_CLASSES = YES # If the HIDE_UNDOC_MEMBERS tag is set to YES, Doxygen will hide all # undocumented members of documented classes, files or namespaces. # If set to NO (the default) these members will be included in the # various overviews, but no documentation section is generated. # This option has no effect if EXTRACT_ALL is enabled. HIDE_UNDOC_MEMBERS = NO # If the HIDE_UNDOC_CLASSES tag is set to YES, Doxygen will hide all # undocumented classes that are normally visible in the class hierarchy. # If set to NO (the default) these classes will be included in the various # overviews. This option has no effect if EXTRACT_ALL is enabled. HIDE_UNDOC_CLASSES = NO # If the HIDE_FRIEND_COMPOUNDS tag is set to YES, Doxygen will hide all # friend (class|struct|union) declarations. # If set to NO (the default) these declarations will be included in the # documentation. HIDE_FRIEND_COMPOUNDS = NO # If the HIDE_IN_BODY_DOCS tag is set to YES, Doxygen will hide any # documentation blocks found inside the body of a function. # If set to NO (the default) these blocks will be appended to the # function's detailed documentation block. HIDE_IN_BODY_DOCS = NO # If the BRIEF_MEMBER_DESC tag is set to YES (the default) Doxygen will # include brief member descriptions after the members that are listed in # the file and class documentation (similar to JavaDoc). # Set to NO to disable this. BRIEF_MEMBER_DESC = YES # If the REPEAT_BRIEF tag is set to YES (the default) Doxygen will prepend # the brief description of a member or function before the detailed description. # Note: if both HIDE_UNDOC_MEMBERS and BRIEF_MEMBER_DESC are set to NO, the # brief descriptions will be completely suppressed. REPEAT_BRIEF = YES # If the ALWAYS_DETAILED_SEC and REPEAT_BRIEF tags are both set to YES then # Doxygen will generate a detailed section even if there is only a brief # description. ALWAYS_DETAILED_SEC = NO # If the INLINE_INHERITED_MEMB tag is set to YES, doxygen will show all inherited # members of a class in the documentation of that class as if those members were # ordinary class members. Constructors, destructors and assignment operators of # the base classes will not be shown. INLINE_INHERITED_MEMB = NO # If the FULL_PATH_NAMES tag is set to YES then Doxygen will prepend the full # path before files name in the file list and in the header files. If set # to NO the shortest path that makes the file name unique will be used. FULL_PATH_NAMES = NO # If the FULL_PATH_NAMES tag is set to YES then the STRIP_FROM_PATH tag # can be used to strip a user-defined part of the path. Stripping is # only done if one of the specified strings matches the left-hand part of # the path. It is allowed to use relative paths in the argument list. STRIP_FROM_PATH = # The INTERNAL_DOCS tag determines if documentation # that is typed after a \internal command is included. If the tag is set # to NO (the default) then the documentation will be excluded. # Set it to YES to include the internal documentation. INTERNAL_DOCS = NO # If the CASE_SENSE_NAMES tag is set to NO then Doxygen will only generate # file names in lower-case letters. If set to YES upper-case letters are also # allowed. This is useful if you have classes or files whose names only differ # in case and if your file system supports case sensitive file names. Windows # users are advised to set this option to NO. CASE_SENSE_NAMES = YES # If the SHORT_NAMES tag is set to YES, doxygen will generate much shorter # (but less readable) file names. This can be useful is your file systems # doesn't support long names like on DOS, Mac, or CD-ROM. SHORT_NAMES = NO # If the HIDE_SCOPE_NAMES tag is set to NO (the default) then Doxygen # will show members with their full class and namespace scopes in the # documentation. If set to YES the scope will be hidden. HIDE_SCOPE_NAMES = NO # If the SHOW_INCLUDE_FILES tag is set to YES (the default) then Doxygen # will put a list of the files that are included by a file in the documentation # of that file. SHOW_INCLUDE_FILES = YES # If the JAVADOC_AUTOBRIEF tag is set to YES then Doxygen # will interpret the first line (until the first dot) of a JavaDoc-style # comment as the brief description. If set to NO, the JavaDoc # comments will behave just like the Qt-style comments (thus requiring an # explict @brief command for a brief description. JAVADOC_AUTOBRIEF = YES # The MULTILINE_CPP_IS_BRIEF tag can be set to YES to make Doxygen # treat a multi-line C++ special comment block (i.e. a block of //! or /// # comments) as a brief description. This used to be the default behaviour. # The new default is to treat a multi-line C++ comment block as a detailed # description. Set this tag to YES if you prefer the old behaviour instead. MULTILINE_CPP_IS_BRIEF = NO # If the DETAILS_AT_TOP tag is set to YES then Doxygen # will output the detailed description near the top, like JavaDoc. # If set to NO, the detailed description appears after the member # documentation. DETAILS_AT_TOP = YES # If the INHERIT_DOCS tag is set to YES (the default) then an undocumented # member inherits the documentation from any documented member that it # reimplements. INHERIT_DOCS = YES # If the INLINE_INFO tag is set to YES (the default) then a tag [inline] # is inserted in the documentation for inline members. INLINE_INFO = YES # If the SORT_MEMBER_DOCS tag is set to YES (the default) then doxygen # will sort the (detailed) documentation of file and class members # alphabetically by member name. If set to NO the members will appear in # declaration order. SORT_MEMBER_DOCS = YES # If member grouping is used in the documentation and the DISTRIBUTE_GROUP_DOC # tag is set to YES, then doxygen will reuse the documentation of the first # member in the group (if any) for the other members of the group. By default # all members of a group must be documented explicitly. DISTRIBUTE_GROUP_DOC = NO # The TAB_SIZE tag can be used to set the number of spaces in a tab. # Doxygen uses this value to replace tabs by spaces in code fragments. TAB_SIZE = 8 # The GENERATE_TODOLIST tag can be used to enable (YES) or # disable (NO) the todo list. This list is created by putting \todo # commands in the documentation. GENERATE_TODOLIST = YES # The GENERATE_TESTLIST tag can be used to enable (YES) or # disable (NO) the test list. This list is created by putting \test # commands in the documentation. GENERATE_TESTLIST = YES # The GENERATE_BUGLIST tag can be used to enable (YES) or # disable (NO) the bug list. This list is created by putting \bug # commands in the documentation. GENERATE_BUGLIST = YES # The GENERATE_DEPRECATEDLIST tag can be used to enable (YES) or # disable (NO) the deprecated list. This list is created by putting # \deprecated commands in the documentation. GENERATE_DEPRECATEDLIST= YES # This tag can be used to specify a number of aliases that acts # as commands in the documentation. An alias has the form "name=value". # For example adding "sideeffect=\par Side Effects:\n" will allow you to # put the command \sideeffect (or @sideeffect) in the documentation, which # will result in a user-defined paragraph with heading "Side Effects:". # You can put \n's in the value part of an alias to insert newlines. ALIASES = # The ENABLED_SECTIONS tag can be used to enable conditional # documentation sections, marked by \if sectionname ... \endif. ENABLED_SECTIONS = # The MAX_INITIALIZER_LINES tag determines the maximum number of lines # the initial value of a variable or define consists of for it to appear in # the documentation. If the initializer consists of more lines than specified # here it will be hidden. Use a value of 0 to hide initializers completely. # The appearance of the initializer of individual variables and defines in the # documentation can be controlled using \showinitializer or \hideinitializer # command in the documentation regardless of this setting. MAX_INITIALIZER_LINES = 30 # Set the OPTIMIZE_OUTPUT_FOR_C tag to YES if your project consists of C sources # only. Doxygen will then generate output that is more tailored for C. # For instance, some of the names that are used will be different. The list # of all members will be omitted, etc. OPTIMIZE_OUTPUT_FOR_C = NO # Set the OPTIMIZE_OUTPUT_JAVA tag to YES if your project consists of Java sources # only. Doxygen will then generate output that is more tailored for Java. # For instance, namespaces will be presented as packages, qualified scopes # will look different, etc. OPTIMIZE_OUTPUT_JAVA = YES # Set the SHOW_USED_FILES tag to NO to disable the list of files generated # at the bottom of the documentation of classes and structs. If set to YES the # list will mention the files that were used to generate the documentation. SHOW_USED_FILES = YES # Set the SUBGROUPING tag to YES (the default) to allow class member groups of # the same type (for instance a group of public functions) to be put as a # subgroup of that type (e.g. under the Public Functions section). Set it to # NO to prevent subgrouping. Alternatively, this can be done per class using # the \nosubgrouping command. SUBGROUPING = YES #--------------------------------------------------------------------------- # configuration options related to warning and progress messages #--------------------------------------------------------------------------- # The QUIET tag can be used to turn on/off the messages that are generated # by doxygen. Possible values are YES and NO. If left blank NO is used. QUIET = NO # The WARNINGS tag can be used to turn on/off the warning messages that are # generated by doxygen. Possible values are YES and NO. If left blank # NO is used. WARNINGS = YES # If WARN_IF_UNDOCUMENTED is set to YES, then doxygen will generate warnings # for undocumented members. If EXTRACT_ALL is set to YES then this flag will # automatically be disabled. WARN_IF_UNDOCUMENTED = YES # If WARN_IF_DOC_ERROR is set to YES, doxygen will generate warnings for # potential errors in the documentation, such as not documenting some # parameters in a documented function, or documenting parameters that # don't exist or using markup commands wrongly. WARN_IF_DOC_ERROR = YES # The WARN_FORMAT tag determines the format of the warning messages that # doxygen can produce. The string should contain the $file, $line, and $text # tags, which will be replaced by the file and line number from which the # warning originated and the warning text. WARN_FORMAT = "$file:$line: $text" # The WARN_LOGFILE tag can be used to specify a file to which warning # and error messages should be written. If left blank the output is written # to stderr. WARN_LOGFILE = #--------------------------------------------------------------------------- # configuration options related to the input files #--------------------------------------------------------------------------- # The INPUT tag can be used to specify the files and/or directories that contain # documented source files. You may enter file names like "myfile.cpp" or # directories like "/usr/src/myproject". Separate the files or directories # with spaces. INPUT = # If the value of the INPUT tag contains directories, you can use the # FILE_PATTERNS tag to specify one or more wildcard pattern (like *.cpp # and *.h) to filter out the source-files in the directories. If left # blank the following patterns are tested: # *.c *.cc *.cxx *.cpp *.c++ *.java *.ii *.ixx *.ipp *.i++ *.inl *.h *.hh *.hxx *.hpp # *.h++ *.idl *.odl *.cs FILE_PATTERNS = Attribute.java AttributeList.java EndTag.java Main.java NamedEntity.java TagNode.java TextNode.java TreeBuilder.java TreeNode.java TreeNodeType.java TreeToString.java TreeToXML.java TypeSafeEnum.java # The RECURSIVE tag can be used to turn specify whether or not subdirectories # should be searched for input files as well. Possible values are YES and NO. # If left blank NO is used. RECURSIVE = NO # The EXCLUDE tag can be used to specify files and/or directories that should # excluded from the INPUT source files. This way you can easily exclude a # subdirectory from a directory tree whose root is specified with the INPUT tag. EXCLUDE = # The EXCLUDE_SYMLINKS tag can be used select whether or not files or directories # that are symbolic links (a Unix filesystem feature) are excluded from the input. EXCLUDE_SYMLINKS = NO # If the value of the INPUT tag contains directories, you can use the # EXCLUDE_PATTERNS tag to specify one or more wildcard patterns to exclude # certain files from those directories. EXCLUDE_PATTERNS = # The EXAMPLE_PATH tag can be used to specify one or more files or # directories that contain example code fragments that are included (see # the \include command). EXAMPLE_PATH = # If the value of the EXAMPLE_PATH tag contains directories, you can use the # EXAMPLE_PATTERNS tag to specify one or more wildcard pattern (like *.cpp # and *.h) to filter out the source-files in the directories. If left # blank all files are included. EXAMPLE_PATTERNS = # If the EXAMPLE_RECURSIVE tag is set to YES then subdirectories will be # searched for input files to be used with the \include or \dontinclude # commands irrespective of the value of the RECURSIVE tag. # Possible values are YES and NO. If left blank NO is used. EXAMPLE_RECURSIVE = NO # The IMAGE_PATH tag can be used to specify one or more files or # directories that contain image that are included in the documentation (see # the \image command). IMAGE_PATH = # The INPUT_FILTER tag can be used to specify a program that doxygen should # invoke to filter for each input file. Doxygen will invoke the filter program # by executing (via popen()) the command