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 Stack<CurrentInfo> m_current; // Name of the element that we're currently inside
23 StringBuilder m_chars; // character content of m_current.peek()
24 ArrayList<ParseResult> m_childParsers; // Set of all child parsers
25 boolean m_haveSeenMyTag; // Have I encountered my own (root) tag yet?
27 String m_tagName; // Name of the (root) element tag that I'm parsing
28 HashSet<String> m_internal; // Tags that we will store as members of this class instance
29 HashMap<String,Class<? extends ParseResult>> m_external; // Tags that we will store as child ParseResult-derived objects
31 // Information about the "current" tag, which we'll keep on the m_current stack.
32 static class CurrentInfo {
36 public CurrentInfo(String tagName, Attributes attr) {
38 m_attr = new AttributesImpl(attr);
41 public String getTagName() { return m_tagName; }
42 public final Attributes getAttributes() { return m_attr; }
45 @SuppressWarnings("unchecked")
46 public ParseResult(String tagName, String[] internalMemberTags, Object[][] externalParserTags)
48 m_current = new Stack<CurrentInfo>();
49 m_chars = new StringBuilder();
50 m_childParsers = new ArrayList<ParseResult>();
51 m_haveSeenMyTag = false;
54 m_internal = new HashSet<String>();
55 m_external = new HashMap<String, Class<? extends ParseResult>>();
57 for (String internalTag : internalMemberTags) {
58 m_internal.add(internalTag);
61 for (int idx = 0; idx < externalParserTags.length; ++idx) {
62 String externalTag = (String)externalParserTags[idx][0];
63 Class<? extends ParseResult> parserClass = (Class<? extends ParseResult>)externalParserTags[idx][1];
64 m_external.put(externalTag, parserClass);
68 public abstract void endContents(String uri, String localName, String qName, String chars, Attributes attr) throws XmlParseException;
69 public abstract void endExternal(String uri, String localName, String qName) throws XmlParseException;
71 public String getTagName() { return m_tagName; }
72 public boolean haveSeenMyTag() { return m_haveSeenMyTag; }
74 public void characters(char[] ch, int start, int length) throws XmlParseException
76 m_chars.append(ch, start, length);
79 protected ParseResult[] collectParsedChildren(Class<? extends ParseResult> cls) {
80 ArrayList<ParseResult> collection = new ArrayList<ParseResult>();
81 Iterator<ParseResult> iter = m_childParsers.iterator();
82 while (iter.hasNext()) {
83 ParseResult pr = iter.next();
84 if (pr.getClass().isAssignableFrom(cls)) {
90 ParseResult[] result = new ParseResult[collection.size()];
91 return collection.toArray(result);
94 // returns true if this ParseResult's context has ended with this endElement() call
95 public boolean endElement(String uri, String localName, String qName) throws XmlParseException
97 assert (null != localName);
99 boolean isInternal = m_internal.contains(localName);
101 if (! m_haveSeenMyTag) {
102 // We're in some unrecognised prologue. Ignore it and move on.
106 if (m_tagName.equals(localName)) {
112 // Unrecognized tag. Ignore it.
116 CurrentInfo info = m_current.pop();
117 String tag = info.getTagName();
118 if ( ! tag.equals(localName) ) {
119 throw new MismatchedTagsException(tag, localName);
122 String chars = m_chars.toString();
123 endContents(uri, localName, qName, chars, info.getAttributes());
128 // returns either itself, or a new ParseResult-derived object, whichever should handle parsing the inside of this element
129 public ParseResult startElement(String uri, String localName, String qName, Attributes attributes)
130 throws XmlParseException
132 assert (null != localName);
134 m_chars.setLength(0);
136 if (! m_haveSeenMyTag) {
137 // Have we opened our own (root) tag yet?
139 if (m_tagName.equals(localName)) {
140 m_haveSeenMyTag = true;
144 // One of two things has happened here.
145 // Either (a) we've got some sort of wrapper here, and have not yet reach our own tag,
146 // or (b) we're parsing XML that doesn't match expectations.
147 // In either case, we're going to ignore this tag, and scan forward looking for our own root.
152 if (m_internal.contains(localName)) {
153 CurrentInfo info = new CurrentInfo(localName, attributes);
154 m_current.push(info);
158 Class<? extends ParseResult> parserClass = m_external.get(localName);
159 if (null != parserClass) {
161 ParseResult childParser = (ParseResult) parserClass.newInstance();
162 m_childParsers.add(childParser);
163 return childParser.startElement(uri, localName, qName, attributes);
165 catch (IllegalAccessException iae) {
166 throw new XmlParseException(iae);
168 catch (InstantiationException ie) {
169 throw new XmlParseException(ie);
173 // Not a recognized tag. Ignore it, rather than complaining.
177 public void validate() throws XmlParseException
179 // Default implementation is a no-op.
180 // Override if you want to validate on endElement()