Index: C:/a/Eclipse/3.3/apache/groovy/src/main/groovy/util/XmlParser.java =================================================================== --- C:/a/Eclipse/3.3/apache/groovy/src/main/groovy/util/XmlParser.java (revision 9903) +++ C:/a/Eclipse/3.3/apache/groovy/src/main/groovy/util/XmlParser.java (working copy) @@ -44,6 +44,21 @@ * and child Nodes and Strings. This simple model is sufficient for * most simple use cases of processing XML. * + * This basic model can be augmented in two ways + * This class can be subclassed: + * The following private fields are made accessible to subclasses: + * locater (get/set) + * reader (get, set via constructor) + * parent (get, created via callbacks) + * The subclass can do things like collect file & location information, or + * add handlers for comments + * + * The class of the generated node can be changed. + * The generated nodes must be Node or a subclass of Node + * A new field, if non-null, is taken to be a factory class + * implementing newInstance(Node parent, Object name, Map attributes, Object value) + * Alternative generated nodes can hold more information, such as location information + * * @author James Strachan * @author Paul King * @version $Revision$ @@ -58,7 +73,7 @@ private boolean trimWhitespace = true; private boolean namespaceAware; - + public XmlParser() throws ParserConfigurationException, SAXException { this(false, true); } @@ -319,9 +334,23 @@ String value = list.getValue(i); attributes.put(attributeName, value); } - parent = new Node(parent, name, attributes, new NodeList()); + parent = newNodeInstance(parent, name, attributes, new NodeList()); stack.add(parent); } + + /** + * Override this in subclasses to return alternatives to Node, which must be + * subclasses of Node. + * + * @param parent the parent of this node or null + * @param name the element name - a String or a QName or other kind of name + * @param attributes a Map + * @param value typically a String or a List of subnodes + * @return a new node object, linked in to its parent + */ + protected Node newNodeInstance(Node parent, Object name, Map attributes, Object value) { + return new Node(parent, name, attributes, value); + } public void endElement(String namespaceURI, String localName, String qName) throws SAXException { addTextToNode(); @@ -396,4 +425,12 @@ } return new QName(namespaceURI, name, prefix); } + + /** + * Gets the current parent node + * @return the parent node + */ + public Node getParent() { + return parent; + } }