001: package uk.org.ponder.saxalizer;
002:
003: import java.util.ArrayList;
004: import java.util.Enumeration;
005: import java.util.HashMap;
006: import java.util.List;
007: import java.util.Map;
008: import java.util.TreeMap;
009: import java.util.Vector;
010:
011: import uk.org.ponder.util.EnumerationConverter;
012:
013: //import uk.org.ponder.stringutil.CharWrap;
014:
015: // This class is similar to the DOM ElementNode!
016: // It maintains a tree of children all of type GenericSax, and also keeps
017: // hold of data for arbitary attributes.
018:
019: /** The class <code>GenericSAXImpl</code> is useful as a base class for SAXalizable
020: * classes that wish to store the information from arbitrary XML subtags.
021: * <code>GenericSAXImpl</code> stores all attributes and all subtags that are
022: * seen for the tag that corresponds to it, and in addition will create
023: * <code>GenericSAXImpl</code> children so that subtags are also stored
024: * recursively.
025: * <p>Ultimately we will arrive at a <code>GenericSAXImpl</code> leaf node, which
026: * has no children (although it may have attributes), and contains a <code>String</code>
027: * representing the text of the node.
028: */
029:
030: public class GenericSAXImpl implements GenericSAX {
031: // There are no specific SAX methods that a GenericSAXImpl provides ---
032: // the SAXalizer directly recognises it and sends it other GenericSAXImpl objects.
033: public SAXAccessMethodSpec[] getSAXSetMethods() {
034: return null;
035: }
036:
037: public SAXAccessMethodSpec[] getSAXSetAttrMethods() {
038: return null;
039: }
040:
041: public SAXAccessMethodSpec[] getSAXGetMethods() {
042: return null;
043: }
044:
045: public SAXAccessMethodSpec[] getSAXGetAttrMethods() {
046: return null;
047: }
048:
049: String tagname;
050: String data; // wtf is this data - only meaningful for leaf nodes.
051: TreeMap attrs;
052: String comment;
053: // A heterogeneous vector.
054: List children; // not a hash, since tags may be identical - also, order should be preserved.
055:
056: // The child will be another GenericSax. Leaf nodes will just have data.
057: /** Add the specified child object to the collection.
058: * @param child The child object to add.
059: */
060: public void addChild(GenericSAX child) {
061: if (children == null)
062: children = new ArrayList();
063: children.add(child);
064: }
065:
066: /** Return the number of child objects.
067: * @return The number of child objects.
068: */
069: public int size() {
070: return (children == null ? 0 : children.size());
071: }
072:
073: /** Return an enumeration of all child objects.
074: @return The required child object.
075: */
076: public Enumeration getChildEnum() {
077: return children == null ? null : EnumerationConverter
078: .getEnumeration(children);
079: }
080:
081: /** Return the child object corresponding to the tag with the given name.
082: @param tagname The tag name that the child object is required for.
083: @return The required child object, or <code>null</code> if not found.
084: */
085: public GenericSAXImpl findChild(String tagname) {
086: // System.out.println("Tagname: "+ tagname +" required");
087: for (int i = 0; i < children.size(); ++i) {
088: GenericSAXImpl childi = (GenericSAXImpl) children.get(i);
089: // System.out.println("Passing child with tag: "+childi.tagname);
090: if (childi.tagname.equals(tagname))
091: return childi;
092: }
093: return null;
094: }
095:
096: /** Set the data for this node to the specified string - only used if this
097: * is a leaf node.
098: * @param s The new node data.
099: */
100: public void setData(String s) {
101: data = s;
102: }
103:
104: /** Return the stored data for this node.
105: * @return The stored data.
106: */
107: public String getData() {
108: return data;
109: }
110:
111: /** Set the tag name for this node to the specified string.
112: * @param s The new tag name.
113: */
114: public void setTag(String s) {
115: tagname = s;
116: }
117:
118: /** Gets the tag name for this node.
119: * @return The required tag name
120: */
121: public String getTag() {
122: return tagname;
123: }
124:
125: public Map getAttributes() {
126: if (attrs == null) {
127: attrs = new TreeMap();
128: }
129: return attrs;
130: }
131:
132: // The data attribute will only be used if this is a leaf node.
133:
134: /* GenericSAXImpl(String tagname, String data, AttributeList attrs) {
135: this.tagname = tagname;
136: this.data = data;
137: this.attrs = new SAXAttributeHash(attrs);
138: }*/
139: }
|