A helper class for parsing XML into a tree of Node instances for a
simple way of processing XML. This parser does not preserve the XML
InfoSet - if that's what you need try using W3C DOM, dom4j, JDOM, XOM etc.
This parser ignores comments and processing instructions and converts
the XML into a Node for each element in the XML with attributes
and child Nodes and Strings. This simple model is sufficient for
most simple use cases of processing XML.
| Method from groovy.util.XmlParser Detail: |
protected void addTextToNode() {
String text = bodyText.toString();
if (trimWhitespace) {
text = text.trim();
}
if (text.length() > 0) {
parent.children().add(text);
}
bodyText = new StringBuffer();
}
|
public void characters(char[] buffer,
int start,
int length) throws SAXException {
bodyText.append(buffer, start, length);
}
|
protected Node createNode(Node parent,
Object name,
Map attributes) {
return new Node(parent, name, attributes);
}
Creates a new node with the given parent, name, and attributes. The
default implementation returns an instance of
groovy.util.Node. |
public void endDocument() throws SAXException {
stack.clear();
}
|
public void endElement(String namespaceURI,
String localName,
String qName) throws SAXException {
addTextToNode();
if (!stack.isEmpty()) {
stack.remove(stack.size() - 1);
if (!stack.isEmpty()) {
parent = stack.get(stack.size() - 1);
}
}
}
|
public void endPrefixMapping(String prefix) throws SAXException {
}
|
public DTDHandler getDTDHandler() {
return this.reader.getDTDHandler();
}
|
public Locator getDocumentLocator() {
return locator;
}
|
protected Object getElementName(String namespaceURI,
String localName,
String qName) {
String name = localName;
String prefix = "";
if ((name == null) || (name.length() < 1)) {
name = qName;
}
if (namespaceURI == null || namespaceURI.length() < = 0) {
return name;
}
if (qName != null && qName.length() > 0 && namespaceAware) {
int index = qName.lastIndexOf(":");
if (index > 0) {
prefix = qName.substring(0, index);
}
}
return new QName(namespaceURI, name, prefix);
}
Return a name given the namespaceURI, localName and qName. |
public EntityResolver getEntityResolver() {
return this.reader.getEntityResolver();
}
|
public ErrorHandler getErrorHandler() {
return this.reader.getErrorHandler();
}
|
public boolean getFeature(String uri) throws SAXNotSupportedException, SAXNotRecognizedException {
return this.reader.getFeature(uri);
}
|
public Object getProperty(String uri) throws SAXNotSupportedException, SAXNotRecognizedException {
return this.reader.getProperty(uri);
}
|
protected XMLReader getXMLReader() {
reader.setContentHandler(this);
return reader;
}
|
public void ignorableWhitespace(char[] buffer,
int start,
int len) throws SAXException {
}
|
public boolean isNamespaceAware() {
return namespaceAware;
}
Determine if namspace handling is enabled. |
public boolean isTrimWhitespace() {
return trimWhitespace;
}
Returns the current trim whitespace setting. |
public Node parse(File file) throws IOException, SAXException {
InputSource input = new InputSource(new FileInputStream(file));
input.setSystemId("file://" + file.getAbsolutePath());
getXMLReader().parse(input);
return parent;
}
Parses the content of the given file as XML turning it into a tree
of Nodes. |
public Node parse(InputSource input) throws IOException, SAXException {
getXMLReader().parse(input);
return parent;
}
Parse the content of the specified input source into a tree of Nodes. |
public Node parse(InputStream input) throws IOException, SAXException {
InputSource is = new InputSource(input);
getXMLReader().parse(is);
return parent;
}
Parse the content of the specified input stream into a tree of Nodes.
Note that using this method will not provide the parser with any URI
for which to find DTDs etc |
public Node parse(Reader in) throws IOException, SAXException {
InputSource is = new InputSource(in);
getXMLReader().parse(is);
return parent;
}
Parse the content of the specified reader into a tree of Nodes.
Note that using this method will not provide the parser with any URI
for which to find DTDs etc |
public Node parse(String uri) throws IOException, SAXException {
InputSource is = new InputSource(uri);
getXMLReader().parse(is);
return parent;
}
Parse the content of the specified URI into a tree of Nodes. |
public Node parseText(String text) throws IOException, SAXException {
return parse(new StringReader(text));
}
A helper method to parse the given text as XML. |
public void processingInstruction(String target,
String data) throws SAXException {
}
|
public void setDTDHandler(DTDHandler dtdHandler) {
this.reader.setDTDHandler(dtdHandler);
}
|
public void setDocumentLocator(Locator locator) {
this.locator = locator;
}
|
public void setEntityResolver(EntityResolver entityResolver) {
this.reader.setEntityResolver(entityResolver);
}
|
public void setErrorHandler(ErrorHandler errorHandler) {
this.reader.setErrorHandler(errorHandler);
}
|
public void setFeature(String uri,
boolean value) throws SAXNotSupportedException, SAXNotRecognizedException {
this.reader.setFeature(uri, value);
}
|
public void setNamespaceAware(boolean namespaceAware) {
this.namespaceAware = namespaceAware;
}
Enable and/or disable namspace handling. |
public void setProperty(String uri,
Object value) throws SAXNotSupportedException, SAXNotRecognizedException {
reader.setProperty(uri, value);
}
|
public void setTrimWhitespace(boolean trimWhitespace) {
this.trimWhitespace = trimWhitespace;
}
Sets the trim whitespace setting value. |
public void skippedEntity(String name) throws SAXException {
}
|
public void startDocument() throws SAXException {
parent = null;
}
|
public void startElement(String namespaceURI,
String localName,
String qName,
Attributes list) throws SAXException {
addTextToNode();
Object nodeName = getElementName(namespaceURI, localName, qName);
int size = list.getLength();
Map< Object, String > attributes = new LinkedHashMap< Object, String >(size);
for (int i = 0; i < size; i++) {
Object attributeName = getElementName(list.getURI(i), list.getLocalName(i), list.getQName(i));
String value = list.getValue(i);
attributes.put(attributeName, value);
}
parent = createNode(parent, nodeName, attributes);
stack.add(parent);
}
|
public void startPrefixMapping(String prefix,
String namespaceURI) throws SAXException {
}
|