Polishing

Issue: SPR-11254
(cherry picked from commit 9a39f39)
This commit is contained in:
Juergen Hoeller 2013-12-23 23:05:10 +01:00
parent a2de329db9
commit a05c957d0e
1 changed files with 145 additions and 136 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2011 the original author or authors. * Copyright 2002-2013 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.
@ -30,9 +30,9 @@ import javax.xml.stream.events.StartDocument;
import javax.xml.stream.events.XMLEvent; import javax.xml.stream.events.XMLEvent;
/** /**
* Implementation of the {@link javax.xml.stream.XMLStreamReader} interface that wraps a {@link XMLEventReader}. Useful, * Implementation of the {@link javax.xml.stream.XMLStreamReader} interface that wraps a
* because the StAX {@link javax.xml.stream.XMLInputFactory} allows one to create a event reader from a stream reader, * {@link XMLEventReader}. Useful because the StAX {@link javax.xml.stream.XMLInputFactory}
* but not vice-versa. * allows one to create a event reader from a stream reader, but not vice-versa.
* *
* @author Arjen Poutsma * @author Arjen Poutsma
* @since 3.0 * @since 3.0
@ -44,92 +44,19 @@ class XMLEventStreamReader extends AbstractXMLStreamReader {
private final XMLEventReader eventReader; private final XMLEventReader eventReader;
XMLEventStreamReader(XMLEventReader eventReader) throws XMLStreamException {
public XMLEventStreamReader(XMLEventReader eventReader) throws XMLStreamException {
this.eventReader = eventReader; this.eventReader = eventReader;
event = eventReader.nextEvent(); this.event = eventReader.nextEvent();
} }
public boolean isStandalone() {
if (event.isStartDocument()) {
return ((StartDocument) event).isStandalone();
}
else {
throw new IllegalStateException();
}
}
public String getVersion() {
if (event.isStartDocument()) {
return ((StartDocument) event).getVersion();
}
else {
return null;
}
}
public int getTextStart() {
return 0;
}
public String getText() {
if (event.isCharacters()) {
return event.asCharacters().getData();
}
else if (event.getEventType() == XMLEvent.COMMENT) {
return ((Comment) event).getText();
}
else {
throw new IllegalStateException();
}
}
public String getPITarget() {
if (event.isProcessingInstruction()) {
return ((ProcessingInstruction) event).getTarget();
}
else {
throw new IllegalStateException();
}
}
public String getPIData() {
if (event.isProcessingInstruction()) {
return ((ProcessingInstruction) event).getData();
}
else {
throw new IllegalStateException();
}
}
public int getNamespaceCount() {
Iterator namespaces;
if (event.isStartElement()) {
namespaces = event.asStartElement().getNamespaces();
}
else if (event.isEndElement()) {
namespaces = event.asEndElement().getNamespaces();
}
else {
throw new IllegalStateException();
}
return countIterator(namespaces);
}
public NamespaceContext getNamespaceContext() {
if (event.isStartElement()) {
return event.asStartElement().getNamespaceContext();
}
else {
throw new IllegalStateException();
}
}
public QName getName() { public QName getName() {
if (event.isStartElement()) { if (this.event.isStartElement()) {
return event.asStartElement().getName(); return this.event.asStartElement().getName();
} }
else if (event.isEndElement()) { else if (this.event.isEndElement()) {
return event.asEndElement().getName(); return this.event.asEndElement().getName();
} }
else { else {
throw new IllegalStateException(); throw new IllegalStateException();
@ -137,11 +64,42 @@ class XMLEventStreamReader extends AbstractXMLStreamReader {
} }
public Location getLocation() { public Location getLocation() {
return event.getLocation(); return this.event.getLocation();
} }
public int getEventType() { public int getEventType() {
return event.getEventType(); return this.event.getEventType();
}
public String getVersion() {
if (this.event.isStartDocument()) {
return ((StartDocument) this.event).getVersion();
}
else {
return null;
}
}
public Object getProperty(String name) throws IllegalArgumentException {
return this.eventReader.getProperty(name);
}
public boolean isStandalone() {
if (this.event.isStartDocument()) {
return ((StartDocument) event).isStandalone();
}
else {
throw new IllegalStateException();
}
}
public boolean standaloneSet() {
if (this.event.isStartDocument()) {
return ((StartDocument) this.event).standaloneSet();
}
else {
throw new IllegalStateException();
}
} }
public String getEncoding() { public String getEncoding() {
@ -152,16 +110,51 @@ class XMLEventStreamReader extends AbstractXMLStreamReader {
return null; return null;
} }
public int getAttributeCount() { public String getPITarget() {
if (!event.isStartElement()) { if (this.event.isProcessingInstruction()) {
return ((ProcessingInstruction) this.event).getTarget();
}
else {
throw new IllegalStateException(); throw new IllegalStateException();
} }
Iterator attributes = event.asStartElement().getAttributes(); }
public String getPIData() {
if (this.event.isProcessingInstruction()) {
return ((ProcessingInstruction) this.event).getData();
}
else {
throw new IllegalStateException();
}
}
public int getTextStart() {
return 0;
}
public String getText() {
if (this.event.isCharacters()) {
return event.asCharacters().getData();
}
else if (this.event.getEventType() == XMLEvent.COMMENT) {
return ((Comment) this.event).getText();
}
else {
throw new IllegalStateException();
}
}
@SuppressWarnings("rawtypes")
public int getAttributeCount() {
if (!this.event.isStartElement()) {
throw new IllegalStateException();
}
Iterator attributes = this.event.asStartElement().getAttributes();
return countIterator(attributes); return countIterator(attributes);
} }
public void close() throws XMLStreamException { public boolean isAttributeSpecified(int index) {
eventReader.close(); return getAttribute(index).isSpecified();
} }
public QName getAttributeName(int index) { public QName getAttributeName(int index) {
@ -176,51 +169,13 @@ class XMLEventStreamReader extends AbstractXMLStreamReader {
return getAttribute(index).getValue(); return getAttribute(index).getValue();
} }
public String getNamespacePrefix(int index) { @SuppressWarnings("rawtypes")
return getNamespace(index).getPrefix();
}
public String getNamespaceURI(int index) {
return getNamespace(index).getNamespaceURI();
}
public Object getProperty(String name) throws IllegalArgumentException {
return eventReader.getProperty(name);
}
public boolean isAttributeSpecified(int index) {
return getAttribute(index).isSpecified();
}
public int next() throws XMLStreamException {
event = eventReader.nextEvent();
return event.getEventType();
}
public boolean standaloneSet() {
if (event.isStartDocument()) {
return ((StartDocument) event).standaloneSet();
}
else {
throw new IllegalStateException();
}
}
private int countIterator(Iterator iterator) {
int count = 0;
while (iterator.hasNext()) {
iterator.next();
count++;
}
return count;
}
private Attribute getAttribute(int index) { private Attribute getAttribute(int index) {
if (!event.isStartElement()) { if (!this.event.isStartElement()) {
throw new IllegalStateException(); throw new IllegalStateException();
} }
int count = 0; int count = 0;
Iterator attributes = event.asStartElement().getAttributes(); Iterator attributes = this.event.asStartElement().getAttributes();
while (attributes.hasNext()) { while (attributes.hasNext()) {
Attribute attribute = (Attribute) attributes.next(); Attribute attribute = (Attribute) attributes.next();
if (count == index) { if (count == index) {
@ -233,13 +188,46 @@ class XMLEventStreamReader extends AbstractXMLStreamReader {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
public NamespaceContext getNamespaceContext() {
if (this.event.isStartElement()) {
return this.event.asStartElement().getNamespaceContext();
}
else {
throw new IllegalStateException();
}
}
@SuppressWarnings("rawtypes")
public int getNamespaceCount() {
Iterator namespaces;
if (this.event.isStartElement()) {
namespaces = this.event.asStartElement().getNamespaces();
}
else if (this.event.isEndElement()) {
namespaces = this.event.asEndElement().getNamespaces();
}
else {
throw new IllegalStateException();
}
return countIterator(namespaces);
}
public String getNamespacePrefix(int index) {
return getNamespace(index).getPrefix();
}
public String getNamespaceURI(int index) {
return getNamespace(index).getNamespaceURI();
}
@SuppressWarnings("rawtypes")
private Namespace getNamespace(int index) { private Namespace getNamespace(int index) {
Iterator namespaces; Iterator namespaces;
if (event.isStartElement()) { if (this.event.isStartElement()) {
namespaces = event.asStartElement().getNamespaces(); namespaces = this.event.asStartElement().getNamespaces();
} }
else if (event.isEndElement()) { else if (this.event.isEndElement()) {
namespaces = event.asEndElement().getNamespaces(); namespaces = this.event.asEndElement().getNamespaces();
} }
else { else {
throw new IllegalStateException(); throw new IllegalStateException();
@ -256,4 +244,25 @@ class XMLEventStreamReader extends AbstractXMLStreamReader {
} }
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
public int next() throws XMLStreamException {
this.event = this.eventReader.nextEvent();
return this.event.getEventType();
}
public void close() throws XMLStreamException {
this.eventReader.close();
}
@SuppressWarnings("rawtypes")
private static int countIterator(Iterator iterator) {
int count = 0;
while (iterator.hasNext()) {
iterator.next();
count++;
}
return count;
}
} }