|
|
|
|
@ -1,5 +1,5 @@
@@ -1,5 +1,5 @@
|
|
|
|
|
/* |
|
|
|
|
* Copyright 2002-2012 the original author or authors. |
|
|
|
|
* Copyright 2002-2013 the original author or authors. |
|
|
|
|
* |
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
|
@ -31,16 +31,16 @@ import org.springframework.util.Assert;
@@ -31,16 +31,16 @@ import org.springframework.util.Assert;
|
|
|
|
|
import org.springframework.util.StringUtils; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* SAX {@code XMLReader} that reads from a StAX {@code XMLStreamReader}. Reads from an |
|
|
|
|
* SAX {@code XMLReader} that reads from a StAX {@code XMLStreamReader}. Reads from an |
|
|
|
|
* {@code XMLStreamReader}, and calls the corresponding methods on the SAX callback interfaces. |
|
|
|
|
* |
|
|
|
|
* @author Arjen Poutsma |
|
|
|
|
* @since 3.0 |
|
|
|
|
* @see XMLStreamReader |
|
|
|
|
* @see #setContentHandler(org.xml.sax.ContentHandler) |
|
|
|
|
* @see #setDTDHandler(org.xml.sax.DTDHandler) |
|
|
|
|
* @see #setEntityResolver(org.xml.sax.EntityResolver) |
|
|
|
|
* @see #setErrorHandler(org.xml.sax.ErrorHandler) |
|
|
|
|
* @since 3.0 |
|
|
|
|
*/ |
|
|
|
|
class StaxStreamXMLReader extends AbstractStaxXMLReader { |
|
|
|
|
|
|
|
|
|
@ -52,11 +52,11 @@ class StaxStreamXMLReader extends AbstractStaxXMLReader {
@@ -52,11 +52,11 @@ class StaxStreamXMLReader extends AbstractStaxXMLReader {
|
|
|
|
|
|
|
|
|
|
private String encoding; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Constructs a new instance of the {@code StaxStreamXmlReader} that reads from the given |
|
|
|
|
* {@code XMLStreamReader}. The supplied stream reader must be in {@code XMLStreamConstants.START_DOCUMENT} |
|
|
|
|
* Construct a new instance of the {@code StaxStreamXmlReader} that reads from the given |
|
|
|
|
* {@code XMLStreamReader}. The supplied stream reader must be in {@code XMLStreamConstants.START_DOCUMENT} |
|
|
|
|
* or {@code XMLStreamConstants.START_ELEMENT} state. |
|
|
|
|
* |
|
|
|
|
* @param reader the {@code XMLEventReader} to read from |
|
|
|
|
* @throws IllegalStateException if the reader is not at the start of a document or element |
|
|
|
|
*/ |
|
|
|
|
@ -69,12 +69,13 @@ class StaxStreamXMLReader extends AbstractStaxXMLReader {
@@ -69,12 +69,13 @@ class StaxStreamXMLReader extends AbstractStaxXMLReader {
|
|
|
|
|
this.reader = reader; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
protected void parseInternal() throws SAXException, XMLStreamException { |
|
|
|
|
boolean documentStarted = false; |
|
|
|
|
boolean documentEnded = false; |
|
|
|
|
int elementDepth = 0; |
|
|
|
|
int eventType = reader.getEventType(); |
|
|
|
|
int eventType = this.reader.getEventType(); |
|
|
|
|
while (true) { |
|
|
|
|
if (eventType != XMLStreamConstants.START_DOCUMENT && eventType != XMLStreamConstants.END_DOCUMENT && |
|
|
|
|
!documentStarted) { |
|
|
|
|
@ -118,8 +119,8 @@ class StaxStreamXMLReader extends AbstractStaxXMLReader {
@@ -118,8 +119,8 @@ class StaxStreamXMLReader extends AbstractStaxXMLReader {
|
|
|
|
|
handleEntityReference(); |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
if (reader.hasNext() && elementDepth >= 0) { |
|
|
|
|
eventType = reader.next(); |
|
|
|
|
if (this.reader.hasNext() && elementDepth >= 0) { |
|
|
|
|
eventType = this.reader.next(); |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
break; |
|
|
|
|
@ -131,72 +132,64 @@ class StaxStreamXMLReader extends AbstractStaxXMLReader {
@@ -131,72 +132,64 @@ class StaxStreamXMLReader extends AbstractStaxXMLReader {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void handleStartDocument() throws SAXException { |
|
|
|
|
if (XMLStreamConstants.START_DOCUMENT == reader.getEventType()) { |
|
|
|
|
String xmlVersion = reader.getVersion(); |
|
|
|
|
if (XMLStreamConstants.START_DOCUMENT == this.reader.getEventType()) { |
|
|
|
|
String xmlVersion = this.reader.getVersion(); |
|
|
|
|
if (StringUtils.hasLength(xmlVersion)) { |
|
|
|
|
this.xmlVersion = xmlVersion; |
|
|
|
|
} |
|
|
|
|
this.encoding = reader.getCharacterEncodingScheme(); |
|
|
|
|
this.encoding = this.reader.getCharacterEncodingScheme(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (getContentHandler() != null) { |
|
|
|
|
final Location location = reader.getLocation(); |
|
|
|
|
|
|
|
|
|
final Location location = this.reader.getLocation(); |
|
|
|
|
getContentHandler().setDocumentLocator(new Locator2() { |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public int getColumnNumber() { |
|
|
|
|
return location != null ? location.getColumnNumber() : -1; |
|
|
|
|
return (location != null ? location.getColumnNumber() : -1); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public int getLineNumber() { |
|
|
|
|
return location != null ? location.getLineNumber() : -1; |
|
|
|
|
return (location != null ? location.getLineNumber() : -1); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public String getPublicId() { |
|
|
|
|
return location != null ? location.getPublicId() : null; |
|
|
|
|
return (location != null ? location.getPublicId() : null); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public String getSystemId() { |
|
|
|
|
return location != null ? location.getSystemId() : null; |
|
|
|
|
return (location != null ? location.getSystemId() : null); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public String getXMLVersion() { |
|
|
|
|
return xmlVersion; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public String getEncoding() { |
|
|
|
|
return encoding; |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
getContentHandler().startDocument(); |
|
|
|
|
if (reader.standaloneSet()) { |
|
|
|
|
setStandalone(reader.isStandalone()); |
|
|
|
|
if (this.reader.standaloneSet()) { |
|
|
|
|
setStandalone(this.reader.isStandalone()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void handleStartElement() throws SAXException { |
|
|
|
|
if (getContentHandler() != null) { |
|
|
|
|
QName qName = reader.getName(); |
|
|
|
|
QName qName = this.reader.getName(); |
|
|
|
|
if (hasNamespacesFeature()) { |
|
|
|
|
for (int i = 0; i < reader.getNamespaceCount(); i++) { |
|
|
|
|
startPrefixMapping(reader.getNamespacePrefix(i), reader.getNamespaceURI(i)); |
|
|
|
|
for (int i = 0; i < this.reader.getNamespaceCount(); i++) { |
|
|
|
|
startPrefixMapping(this.reader.getNamespacePrefix(i), this.reader.getNamespaceURI(i)); |
|
|
|
|
} |
|
|
|
|
for (int i = 0; i < reader.getAttributeCount(); i++) { |
|
|
|
|
String prefix = reader.getAttributePrefix(i); |
|
|
|
|
String namespace = reader.getAttributeNamespace(i); |
|
|
|
|
for (int i = 0; i < this.reader.getAttributeCount(); i++) { |
|
|
|
|
String prefix = this.reader.getAttributePrefix(i); |
|
|
|
|
String namespace = this.reader.getAttributeNamespace(i); |
|
|
|
|
if (StringUtils.hasLength(namespace)) { |
|
|
|
|
startPrefixMapping(prefix, namespace); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
getContentHandler().startElement(qName.getNamespaceURI(), qName.getLocalPart(), toQualifiedName(qName), |
|
|
|
|
getAttributes()); |
|
|
|
|
getContentHandler().startElement(qName.getNamespaceURI(), qName.getLocalPart(), |
|
|
|
|
toQualifiedName(qName), getAttributes()); |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
getContentHandler().startElement("", "", toQualifiedName(qName), getAttributes()); |
|
|
|
|
@ -206,11 +199,11 @@ class StaxStreamXMLReader extends AbstractStaxXMLReader {
@@ -206,11 +199,11 @@ class StaxStreamXMLReader extends AbstractStaxXMLReader {
|
|
|
|
|
|
|
|
|
|
private void handleEndElement() throws SAXException { |
|
|
|
|
if (getContentHandler() != null) { |
|
|
|
|
QName qName = reader.getName(); |
|
|
|
|
QName qName = this.reader.getName(); |
|
|
|
|
if (hasNamespacesFeature()) { |
|
|
|
|
getContentHandler().endElement(qName.getNamespaceURI(), qName.getLocalPart(), toQualifiedName(qName)); |
|
|
|
|
for (int i = 0; i < reader.getNamespaceCount(); i++) { |
|
|
|
|
String prefix = reader.getNamespacePrefix(i); |
|
|
|
|
for (int i = 0; i < this.reader.getNamespaceCount(); i++) { |
|
|
|
|
String prefix = this.reader.getNamespacePrefix(i); |
|
|
|
|
if (prefix == null) { |
|
|
|
|
prefix = ""; |
|
|
|
|
} |
|
|
|
|
@ -224,31 +217,33 @@ class StaxStreamXMLReader extends AbstractStaxXMLReader {
@@ -224,31 +217,33 @@ class StaxStreamXMLReader extends AbstractStaxXMLReader {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void handleCharacters() throws SAXException { |
|
|
|
|
if (getContentHandler() != null && reader.isWhiteSpace()) { |
|
|
|
|
getContentHandler() |
|
|
|
|
.ignorableWhitespace(reader.getTextCharacters(), reader.getTextStart(), reader.getTextLength()); |
|
|
|
|
if (getContentHandler() != null && this.reader.isWhiteSpace()) { |
|
|
|
|
getContentHandler().ignorableWhitespace(this.reader.getTextCharacters(), |
|
|
|
|
this.reader.getTextStart(), this.reader.getTextLength()); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
if (XMLStreamConstants.CDATA == reader.getEventType() && getLexicalHandler() != null) { |
|
|
|
|
if (XMLStreamConstants.CDATA == this.reader.getEventType() && getLexicalHandler() != null) { |
|
|
|
|
getLexicalHandler().startCDATA(); |
|
|
|
|
} |
|
|
|
|
if (getContentHandler() != null) { |
|
|
|
|
getContentHandler().characters(reader.getTextCharacters(), reader.getTextStart(), reader.getTextLength()); |
|
|
|
|
getContentHandler().characters(this.reader.getTextCharacters(), |
|
|
|
|
this.reader.getTextStart(), this.reader.getTextLength()); |
|
|
|
|
} |
|
|
|
|
if (XMLStreamConstants.CDATA == reader.getEventType() && getLexicalHandler() != null) { |
|
|
|
|
if (XMLStreamConstants.CDATA == this.reader.getEventType() && getLexicalHandler() != null) { |
|
|
|
|
getLexicalHandler().endCDATA(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void handleComment() throws SAXException { |
|
|
|
|
if (getLexicalHandler() != null) { |
|
|
|
|
getLexicalHandler().comment(reader.getTextCharacters(), reader.getTextStart(), reader.getTextLength()); |
|
|
|
|
getLexicalHandler().comment(this.reader.getTextCharacters(), |
|
|
|
|
this.reader.getTextStart(), this.reader.getTextLength()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void handleDtd() throws SAXException { |
|
|
|
|
if (getLexicalHandler() != null) { |
|
|
|
|
javax.xml.stream.Location location = reader.getLocation(); |
|
|
|
|
javax.xml.stream.Location location = this.reader.getLocation(); |
|
|
|
|
getLexicalHandler().startDTD(null, location.getPublicId(), location.getSystemId()); |
|
|
|
|
} |
|
|
|
|
if (getLexicalHandler() != null) { |
|
|
|
|
@ -258,10 +253,10 @@ class StaxStreamXMLReader extends AbstractStaxXMLReader {
@@ -258,10 +253,10 @@ class StaxStreamXMLReader extends AbstractStaxXMLReader {
|
|
|
|
|
|
|
|
|
|
private void handleEntityReference() throws SAXException { |
|
|
|
|
if (getLexicalHandler() != null) { |
|
|
|
|
getLexicalHandler().startEntity(reader.getLocalName()); |
|
|
|
|
getLexicalHandler().startEntity(this.reader.getLocalName()); |
|
|
|
|
} |
|
|
|
|
if (getLexicalHandler() != null) { |
|
|
|
|
getLexicalHandler().endEntity(reader.getLocalName()); |
|
|
|
|
getLexicalHandler().endEntity(this.reader.getLocalName()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -273,29 +268,28 @@ class StaxStreamXMLReader extends AbstractStaxXMLReader {
@@ -273,29 +268,28 @@ class StaxStreamXMLReader extends AbstractStaxXMLReader {
|
|
|
|
|
|
|
|
|
|
private void handleProcessingInstruction() throws SAXException { |
|
|
|
|
if (getContentHandler() != null) { |
|
|
|
|
getContentHandler().processingInstruction(reader.getPITarget(), reader.getPIData()); |
|
|
|
|
getContentHandler().processingInstruction(this.reader.getPITarget(), this.reader.getPIData()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private Attributes getAttributes() { |
|
|
|
|
AttributesImpl attributes = new AttributesImpl(); |
|
|
|
|
|
|
|
|
|
for (int i = 0; i < reader.getAttributeCount(); i++) { |
|
|
|
|
String namespace = reader.getAttributeNamespace(i); |
|
|
|
|
for (int i = 0; i < this.reader.getAttributeCount(); i++) { |
|
|
|
|
String namespace = this.reader.getAttributeNamespace(i); |
|
|
|
|
if (namespace == null || !hasNamespacesFeature()) { |
|
|
|
|
namespace = ""; |
|
|
|
|
} |
|
|
|
|
String type = reader.getAttributeType(i); |
|
|
|
|
String type = this.reader.getAttributeType(i); |
|
|
|
|
if (type == null) { |
|
|
|
|
type = "CDATA"; |
|
|
|
|
} |
|
|
|
|
attributes.addAttribute(namespace, reader.getAttributeLocalName(i), |
|
|
|
|
toQualifiedName(reader.getAttributeName(i)), type, reader.getAttributeValue(i)); |
|
|
|
|
attributes.addAttribute(namespace, this.reader.getAttributeLocalName(i), |
|
|
|
|
toQualifiedName(this.reader.getAttributeName(i)), type, this.reader.getAttributeValue(i)); |
|
|
|
|
} |
|
|
|
|
if (hasNamespacePrefixesFeature()) { |
|
|
|
|
for (int i = 0; i < reader.getNamespaceCount(); i++) { |
|
|
|
|
String prefix = reader.getNamespacePrefix(i); |
|
|
|
|
String namespaceUri = reader.getNamespaceURI(i); |
|
|
|
|
for (int i = 0; i < this.reader.getNamespaceCount(); i++) { |
|
|
|
|
String prefix = this.reader.getNamespacePrefix(i); |
|
|
|
|
String namespaceUri = this.reader.getNamespaceURI(i); |
|
|
|
|
String qName; |
|
|
|
|
if (StringUtils.hasLength(prefix)) { |
|
|
|
|
qName = "xmlns:" + prefix; |
|
|
|
|
|