Various StAX improvements.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
* Copyright 2002-2010 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.
|
||||
@@ -20,7 +20,6 @@ import javax.xml.namespace.QName;
|
||||
import javax.xml.stream.Location;
|
||||
import javax.xml.stream.XMLStreamException;
|
||||
|
||||
import org.xml.sax.ContentHandler;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.Locator;
|
||||
import org.xml.sax.SAXException;
|
||||
@@ -108,17 +107,6 @@ abstract class AbstractStaxXMLReader extends AbstractXMLReader {
|
||||
return this.namespacePrefixesFeature;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sett the SAX <code>Locator</code> based on the given StAX <code>Location</code>.
|
||||
* @param location the location
|
||||
* @see ContentHandler#setDocumentLocator(org.xml.sax.Locator)
|
||||
*/
|
||||
protected void setLocator(Location location) {
|
||||
if (getContentHandler() != null) {
|
||||
getContentHandler().setDocumentLocator(new StaxLocator(location));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a <code>QName</code> to a qualified name, as used by DOM and SAX.
|
||||
* The returned string has a format of <code>prefix:localName</code> if the
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
* Copyright 2002-2010 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.
|
||||
@@ -18,6 +18,7 @@ package org.springframework.util.xml;
|
||||
|
||||
import java.util.Iterator;
|
||||
import javax.xml.namespace.QName;
|
||||
import javax.xml.stream.Location;
|
||||
import javax.xml.stream.XMLEventReader;
|
||||
import javax.xml.stream.XMLStreamConstants;
|
||||
import javax.xml.stream.XMLStreamException;
|
||||
@@ -31,11 +32,13 @@ import javax.xml.stream.events.EntityReference;
|
||||
import javax.xml.stream.events.Namespace;
|
||||
import javax.xml.stream.events.NotationDeclaration;
|
||||
import javax.xml.stream.events.ProcessingInstruction;
|
||||
import javax.xml.stream.events.StartDocument;
|
||||
import javax.xml.stream.events.StartElement;
|
||||
import javax.xml.stream.events.XMLEvent;
|
||||
|
||||
import org.xml.sax.Attributes;
|
||||
import org.xml.sax.SAXException;
|
||||
import org.xml.sax.ext.Locator2;
|
||||
import org.xml.sax.helpers.AttributesImpl;
|
||||
|
||||
import org.springframework.util.StringUtils;
|
||||
@@ -86,10 +89,14 @@ class StaxEventXMLReader extends AbstractStaxXMLReader {
|
||||
while (reader.hasNext() && elementDepth >= 0) {
|
||||
XMLEvent event = reader.nextEvent();
|
||||
if (!event.isStartDocument() && !event.isEndDocument() && !documentStarted) {
|
||||
handleStartDocument();
|
||||
handleStartDocument(event);
|
||||
documentStarted = true;
|
||||
}
|
||||
switch (event.getEventType()) {
|
||||
case XMLStreamConstants.START_DOCUMENT:
|
||||
handleStartDocument(event);
|
||||
documentStarted = true;
|
||||
break;
|
||||
case XMLStreamConstants.START_ELEMENT:
|
||||
elementDepth++;
|
||||
handleStartElement(event.asStartElement());
|
||||
@@ -108,11 +115,6 @@ class StaxEventXMLReader extends AbstractStaxXMLReader {
|
||||
case XMLStreamConstants.CDATA:
|
||||
handleCharacters(event.asCharacters());
|
||||
break;
|
||||
case XMLStreamConstants.START_DOCUMENT:
|
||||
setLocator(event.getLocation());
|
||||
handleStartDocument();
|
||||
documentStarted = true;
|
||||
break;
|
||||
case XMLStreamConstants.END_DOCUMENT:
|
||||
handleEndDocument();
|
||||
documentEnded = true;
|
||||
@@ -140,6 +142,51 @@ class StaxEventXMLReader extends AbstractStaxXMLReader {
|
||||
|
||||
}
|
||||
|
||||
private void handleStartDocument(final XMLEvent event) throws SAXException {
|
||||
if (getContentHandler() != null) {
|
||||
final Location location = event.getLocation();
|
||||
getContentHandler().setDocumentLocator(new Locator2() {
|
||||
|
||||
public int getColumnNumber() {
|
||||
return location.getColumnNumber();
|
||||
}
|
||||
|
||||
public int getLineNumber() {
|
||||
return location.getLineNumber();
|
||||
}
|
||||
|
||||
public String getPublicId() {
|
||||
return location.getPublicId();
|
||||
}
|
||||
|
||||
public String getSystemId() {
|
||||
return location.getSystemId();
|
||||
}
|
||||
|
||||
public String getXMLVersion() {
|
||||
if (event.isStartDocument()) {
|
||||
StartDocument startDocument = (StartDocument) event;
|
||||
String version = startDocument.getVersion();
|
||||
return StringUtils.hasLength(version) ? version : "1.0";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getEncoding() {
|
||||
if (event.isStartDocument()) {
|
||||
StartDocument startDocument = (StartDocument) event;
|
||||
if (startDocument.encodingSet()) {
|
||||
return startDocument.getCharacterEncodingScheme();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
getContentHandler().startDocument();
|
||||
}
|
||||
}
|
||||
|
||||
private void handleStartElement(StartElement startElement) throws SAXException {
|
||||
if (getContentHandler() != null) {
|
||||
QName qName = startElement.getName();
|
||||
@@ -174,12 +221,6 @@ class StaxEventXMLReader extends AbstractStaxXMLReader {
|
||||
}
|
||||
}
|
||||
|
||||
private void handleEndDocument() throws SAXException {
|
||||
if (getContentHandler() != null) {
|
||||
getContentHandler().endDocument();
|
||||
}
|
||||
}
|
||||
|
||||
private void handleEndElement(EndElement endElement) throws SAXException {
|
||||
if (getContentHandler() != null) {
|
||||
QName qName = endElement.getName();
|
||||
@@ -197,6 +238,12 @@ class StaxEventXMLReader extends AbstractStaxXMLReader {
|
||||
}
|
||||
}
|
||||
|
||||
private void handleEndDocument() throws SAXException {
|
||||
if (getContentHandler() != null) {
|
||||
getContentHandler().endDocument();
|
||||
}
|
||||
}
|
||||
|
||||
private void handleNotationDeclaration(NotationDeclaration declaration) throws SAXException {
|
||||
if (getDTDHandler() != null) {
|
||||
getDTDHandler().notationDecl(declaration.getName(), declaration.getPublicId(), declaration.getSystemId());
|
||||
@@ -216,12 +263,6 @@ class StaxEventXMLReader extends AbstractStaxXMLReader {
|
||||
}
|
||||
}
|
||||
|
||||
private void handleStartDocument() throws SAXException {
|
||||
if (getContentHandler() != null) {
|
||||
getContentHandler().startDocument();
|
||||
}
|
||||
}
|
||||
|
||||
private void handleComment(Comment comment) throws SAXException {
|
||||
if (getLexicalHandler() != null) {
|
||||
char[] ch = comment.getText().toCharArray();
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
* Copyright 2002-2010 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.
|
||||
@@ -17,12 +17,14 @@
|
||||
package org.springframework.util.xml;
|
||||
|
||||
import javax.xml.namespace.QName;
|
||||
import javax.xml.stream.Location;
|
||||
import javax.xml.stream.XMLStreamConstants;
|
||||
import javax.xml.stream.XMLStreamException;
|
||||
import javax.xml.stream.XMLStreamReader;
|
||||
|
||||
import org.xml.sax.Attributes;
|
||||
import org.xml.sax.SAXException;
|
||||
import org.xml.sax.ext.Locator2;
|
||||
import org.xml.sax.helpers.AttributesImpl;
|
||||
|
||||
import org.springframework.util.StringUtils;
|
||||
@@ -91,7 +93,6 @@ class StaxStreamXMLReader extends AbstractStaxXMLReader {
|
||||
handleCharacters();
|
||||
break;
|
||||
case XMLStreamConstants.START_DOCUMENT:
|
||||
setLocator(reader.getLocation());
|
||||
handleStartDocument();
|
||||
documentStarted = true;
|
||||
break;
|
||||
@@ -123,6 +124,35 @@ class StaxStreamXMLReader extends AbstractStaxXMLReader {
|
||||
|
||||
private void handleStartDocument() throws SAXException {
|
||||
if (getContentHandler() != null) {
|
||||
final Location location = reader.getLocation();
|
||||
|
||||
getContentHandler().setDocumentLocator(new Locator2() {
|
||||
|
||||
public int getColumnNumber() {
|
||||
return location.getColumnNumber();
|
||||
}
|
||||
|
||||
public int getLineNumber() {
|
||||
return location.getLineNumber();
|
||||
}
|
||||
|
||||
public String getPublicId() {
|
||||
return location.getPublicId();
|
||||
}
|
||||
|
||||
public String getSystemId() {
|
||||
return location.getSystemId();
|
||||
}
|
||||
|
||||
public String getXMLVersion() {
|
||||
String version = reader.getVersion();
|
||||
return StringUtils.hasLength(version) ? version : "1.0";
|
||||
}
|
||||
|
||||
public String getEncoding() {
|
||||
return reader.getEncoding();
|
||||
}
|
||||
});
|
||||
getContentHandler().startDocument();
|
||||
if (reader.standaloneSet()) {
|
||||
setStandalone(reader.isStandalone());
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
* Copyright 2002-2010 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.
|
||||
@@ -61,7 +61,7 @@ class XMLEventStreamReader extends AbstractXMLStreamReader {
|
||||
return ((StartDocument) event).getVersion();
|
||||
}
|
||||
else {
|
||||
throw new IllegalStateException();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user