|
|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2012 the original author or authors. |
|
|
|
* Copyright 2002-2018 the original author or authors. |
|
|
|
* |
|
|
|
* |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -27,16 +27,13 @@ import org.w3c.dom.Text; |
|
|
|
import org.xml.sax.Attributes; |
|
|
|
import org.xml.sax.Attributes; |
|
|
|
import org.xml.sax.ContentHandler; |
|
|
|
import org.xml.sax.ContentHandler; |
|
|
|
import org.xml.sax.Locator; |
|
|
|
import org.xml.sax.Locator; |
|
|
|
import org.xml.sax.SAXException; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.util.Assert; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* SAX {@code ContentHandler} that transforms callback calls to DOM {@code Node}s. |
|
|
|
* SAX {@code ContentHandler} that transforms callback calls to DOM {@code Node}s. |
|
|
|
* |
|
|
|
* |
|
|
|
* @author Arjen Poutsma |
|
|
|
* @author Arjen Poutsma |
|
|
|
* @see org.w3c.dom.Node |
|
|
|
|
|
|
|
* @since 3.0 |
|
|
|
* @since 3.0 |
|
|
|
|
|
|
|
* @see org.w3c.dom.Node |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
class DomContentHandler implements ContentHandler { |
|
|
|
class DomContentHandler implements ContentHandler { |
|
|
|
|
|
|
|
|
|
|
|
@ -46,36 +43,35 @@ class DomContentHandler implements ContentHandler { |
|
|
|
|
|
|
|
|
|
|
|
private final Node node; |
|
|
|
private final Node node; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Creates a new instance of the {@code DomContentHandler} with the given node. |
|
|
|
* Create a new instance of the {@code DomContentHandler} with the given node. |
|
|
|
* |
|
|
|
|
|
|
|
* @param node the node to publish events to |
|
|
|
* @param node the node to publish events to |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
DomContentHandler(Node node) { |
|
|
|
DomContentHandler(Node node) { |
|
|
|
Assert.notNull(node, "node must not be null"); |
|
|
|
|
|
|
|
this.node = node; |
|
|
|
this.node = node; |
|
|
|
if (node instanceof Document) { |
|
|
|
if (node instanceof Document) { |
|
|
|
document = (Document) node; |
|
|
|
this.document = (Document) node; |
|
|
|
} |
|
|
|
} |
|
|
|
else { |
|
|
|
else { |
|
|
|
document = node.getOwnerDocument(); |
|
|
|
this.document = node.getOwnerDocument(); |
|
|
|
} |
|
|
|
} |
|
|
|
Assert.notNull(document, "document must not be null"); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Node getParent() { |
|
|
|
private Node getParent() { |
|
|
|
if (!elements.isEmpty()) { |
|
|
|
if (!this.elements.isEmpty()) { |
|
|
|
return elements.get(elements.size() - 1); |
|
|
|
return this.elements.get(this.elements.size() - 1); |
|
|
|
} |
|
|
|
} |
|
|
|
else { |
|
|
|
else { |
|
|
|
return node; |
|
|
|
return this.node; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { |
|
|
|
public void startElement(String uri, String localName, String qName, Attributes attributes) { |
|
|
|
Node parent = getParent(); |
|
|
|
Node parent = getParent(); |
|
|
|
Element element = document.createElementNS(uri, qName); |
|
|
|
Element element = this.document.createElementNS(uri, qName); |
|
|
|
for (int i = 0; i < attributes.getLength(); i++) { |
|
|
|
for (int i = 0; i < attributes.getLength(); i++) { |
|
|
|
String attrUri = attributes.getURI(i); |
|
|
|
String attrUri = attributes.getURI(i); |
|
|
|
String attrQname = attributes.getQName(i); |
|
|
|
String attrQname = attributes.getQName(i); |
|
|
|
@ -85,16 +81,16 @@ class DomContentHandler implements ContentHandler { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
element = (Element) parent.appendChild(element); |
|
|
|
element = (Element) parent.appendChild(element); |
|
|
|
elements.add(element); |
|
|
|
this.elements.add(element); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void endElement(String uri, String localName, String qName) throws SAXException { |
|
|
|
public void endElement(String uri, String localName, String qName) { |
|
|
|
elements.remove(elements.size() - 1); |
|
|
|
this.elements.remove(this.elements.size() - 1); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void characters(char[] ch, int start, int length) throws SAXException { |
|
|
|
public void characters(char[] ch, int start, int length) { |
|
|
|
String data = new String(ch, start, length); |
|
|
|
String data = new String(ch, start, length); |
|
|
|
Node parent = getParent(); |
|
|
|
Node parent = getParent(); |
|
|
|
Node lastChild = parent.getLastChild(); |
|
|
|
Node lastChild = parent.getLastChild(); |
|
|
|
@ -102,47 +98,47 @@ class DomContentHandler implements ContentHandler { |
|
|
|
((Text) lastChild).appendData(data); |
|
|
|
((Text) lastChild).appendData(data); |
|
|
|
} |
|
|
|
} |
|
|
|
else { |
|
|
|
else { |
|
|
|
Text text = document.createTextNode(data); |
|
|
|
Text text = this.document.createTextNode(data); |
|
|
|
parent.appendChild(text); |
|
|
|
parent.appendChild(text); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void processingInstruction(String target, String data) throws SAXException { |
|
|
|
public void processingInstruction(String target, String data) { |
|
|
|
Node parent = getParent(); |
|
|
|
Node parent = getParent(); |
|
|
|
ProcessingInstruction pi = document.createProcessingInstruction(target, data); |
|
|
|
ProcessingInstruction pi = this.document.createProcessingInstruction(target, data); |
|
|
|
parent.appendChild(pi); |
|
|
|
parent.appendChild(pi); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
|
|
|
|
|
* Unsupported |
|
|
|
// Unsupported
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void setDocumentLocator(Locator locator) { |
|
|
|
public void setDocumentLocator(Locator locator) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void startDocument() throws SAXException { |
|
|
|
public void startDocument() { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void endDocument() throws SAXException { |
|
|
|
public void endDocument() { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void startPrefixMapping(String prefix, String uri) throws SAXException { |
|
|
|
public void startPrefixMapping(String prefix, String uri) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void endPrefixMapping(String prefix) throws SAXException { |
|
|
|
public void endPrefixMapping(String prefix) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { |
|
|
|
public void ignorableWhitespace(char[] ch, int start, int length) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void skippedEntity(String name) throws SAXException { |
|
|
|
public void skippedEntity(String name) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|