1 // Copyright (C) 2004, 2015 Christian Jaekl
3 // Abstract class representing the result of parsing an XML Element.
4 // A class derived from this one will know how to parse a subtree inside an XML file, and
5 // will contain the result of that parse within itself when the parse has completed.
7 // Note that this code will need to be augmented and fixed if XML namespace support is desired.
9 package net.jaekl.qd.xml;
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.HashSet;
14 import java.util.Iterator;
15 import java.util.Stack;
17 import org.xml.sax.Attributes;
18 import org.xml.sax.helpers.AttributesImpl;
20 public abstract class ParseResult
22 public static final String TAG_FIELD_NAME = "TAG";
24 Stack<CurrentInfo> m_current; // Name of the element that we're currently inside
25 StringBuilder m_chars; // character content of m_current.peek()
26 ArrayList<ParseResult> m_childParsers; // Set of all child parsers
27 boolean m_haveSeenMyTag; // Have I encountered my own (root) tag yet?
29 String m_tagName; // Name of the (root) element tag that I'm parsing
30 HashSet<String> m_internal; // Tags that we will store as members of this class instance
31 HashMap<String,Class<? extends ParseResult>> m_external; // Tags that we will store as child ParseResult-derived objects
33 // Information about the "current" tag, which we'll keep on the m_current stack.
34 static class CurrentInfo {
38 public CurrentInfo(String tagName, Attributes attr) {
40 m_attr = new AttributesImpl(attr);
43 public String getTagName() { return m_tagName; }
44 public final Attributes getAttributes() { return m_attr; }
47 @SuppressWarnings("unchecked")
48 public ParseResult(String tagName, String[] internalMemberTags, Object[][] externalParserTags)
50 m_current = new Stack<CurrentInfo>();
51 m_chars = new StringBuilder();
52 m_childParsers = new ArrayList<ParseResult>();
53 m_haveSeenMyTag = false;
56 m_internal = new HashSet<String>();
57 m_external = new HashMap<String, Class<? extends ParseResult>>();
59 for (String internalTag : internalMemberTags) {
60 m_internal.add(internalTag);
63 for (int idx = 0; idx < externalParserTags.length; ++idx) {
64 String externalTag = (String)externalParserTags[idx][0];
65 Class<? extends ParseResult> parserClass = (Class<? extends ParseResult>)externalParserTags[idx][1];
66 m_external.put(externalTag, parserClass);
70 public abstract void endContents(String uri, String localName, String qName, String chars, Attributes attr) throws XmlParseException;
71 public abstract void endExternal(String uri, String localName, String qName) throws XmlParseException;
73 public String getTagName() { return m_tagName; }
74 public boolean haveSeenMyTag() { return m_haveSeenMyTag; }
76 public void characters(char[] ch, int start, int length) throws XmlParseException
78 m_chars.append(ch, start, length);
81 protected ParseResult[] collectParsedChildren(Class<? extends ParseResult> cls) {
82 ArrayList<ParseResult> collection = new ArrayList<ParseResult>();
83 Iterator<ParseResult> iter = m_childParsers.iterator();
84 while (iter.hasNext()) {
85 ParseResult pr = iter.next();
86 if (pr.getClass().isAssignableFrom(cls)) {
92 ParseResult[] result = new ParseResult[collection.size()];
93 return collection.toArray(result);
96 // returns true if this ParseResult's context has ended with this endElement() call
97 public boolean endElement(String uri, String localName, String qName) throws XmlParseException
99 assert (null != localName);
101 boolean isInternal = m_internal.contains(localName);
103 if (! m_haveSeenMyTag) {
104 // We're in some unrecognised prologue. Ignore it and move on.
108 if (m_tagName.equals(localName)) {
114 // Unrecognized tag. Ignore it.
118 CurrentInfo info = m_current.pop();
119 String tag = info.getTagName();
120 if ( ! tag.equals(localName) ) {
121 throw new MismatchedTagsException(tag, localName);
124 String chars = m_chars.toString();
125 endContents(uri, localName, qName, chars, info.getAttributes());
130 // returns either itself, or a new ParseResult-derived object, whichever should handle parsing the inside of this element
131 public ParseResult startElement(String uri, String localName, String qName, Attributes attributes)
132 throws XmlParseException
134 assert (null != localName);
136 m_chars.setLength(0);
138 if (! m_haveSeenMyTag) {
139 // Have we opened our own (root) tag yet?
141 if (m_tagName.equals(localName)) {
142 m_haveSeenMyTag = true;
146 // One of two things has happened here.
147 // Either (a) we've got some sort of wrapper here, and have not yet reach our own tag,
148 // or (b) we're parsing XML that doesn't match expectations.
149 // In either case, we're going to ignore this tag, and scan forward looking for our own root.
154 if (m_internal.contains(localName)) {
155 CurrentInfo info = new CurrentInfo(localName, attributes);
156 m_current.push(info);
160 Class<? extends ParseResult> parserClass = m_external.get(localName);
161 if (null != parserClass) {
163 ParseResult childParser = (ParseResult) parserClass.newInstance();
164 m_childParsers.add(childParser);
165 return childParser.startElement(uri, localName, qName, attributes);
167 catch (IllegalAccessException iae) {
168 throw new XmlParseException(iae);
170 catch (InstantiationException ie) {
171 throw new XmlParseException(ie);
175 // Not a recognized tag. Ignore it, rather than complaining.
179 public void validate() throws XmlParseException
181 // Default implementation is a no-op.
182 // Override if you want to validate on endElement()
185 protected String getRequiredAttr(String tagName, Attributes attr, String attrName)
186 throws MissingAttributeException
188 String result = attr.getValue(attrName);
189 if (null == result) {
190 throw new MissingAttributeException(tagName, attrName);
195 protected String getOptionalAttr(Attributes attr, String attrName, String defaultValue)
197 String value = attr.getValue(attrName);