Polishing
This commit is contained in:
parent
04a7f0884b
commit
ac544924c8
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2017 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.
|
||||||
|
@ -22,7 +22,6 @@ import javax.xml.stream.XMLStreamException;
|
||||||
import javax.xml.stream.XMLStreamReader;
|
import javax.xml.stream.XMLStreamReader;
|
||||||
|
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
import org.springframework.util.Assert;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Abstract base class for {@code XMLStreamReader}s.
|
* Abstract base class for {@code XMLStreamReader}s.
|
||||||
|
@ -35,7 +34,7 @@ abstract class AbstractXMLStreamReader implements XMLStreamReader {
|
||||||
@Override
|
@Override
|
||||||
public String getElementText() throws XMLStreamException {
|
public String getElementText() throws XMLStreamException {
|
||||||
if (getEventType() != XMLStreamConstants.START_ELEMENT) {
|
if (getEventType() != XMLStreamConstants.START_ELEMENT) {
|
||||||
throw new XMLStreamException("parser must be on START_ELEMENT to read next text", getLocation());
|
throw new XMLStreamException("Parser must be on START_ELEMENT to read next text", getLocation());
|
||||||
}
|
}
|
||||||
int eventType = next();
|
int eventType = next();
|
||||||
StringBuilder builder = new StringBuilder();
|
StringBuilder builder = new StringBuilder();
|
||||||
|
@ -49,11 +48,11 @@ abstract class AbstractXMLStreamReader implements XMLStreamReader {
|
||||||
// skipping
|
// skipping
|
||||||
}
|
}
|
||||||
else if (eventType == XMLStreamConstants.END_DOCUMENT) {
|
else if (eventType == XMLStreamConstants.END_DOCUMENT) {
|
||||||
throw new XMLStreamException("unexpected end of document when reading element text content",
|
throw new XMLStreamException("Unexpected end of document when reading element text content",
|
||||||
getLocation());
|
getLocation());
|
||||||
}
|
}
|
||||||
else if (eventType == XMLStreamConstants.START_ELEMENT) {
|
else if (eventType == XMLStreamConstants.START_ELEMENT) {
|
||||||
throw new XMLStreamException("element text content may not contain START_ELEMENT", getLocation());
|
throw new XMLStreamException("Element text content may not contain START_ELEMENT", getLocation());
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throw new XMLStreamException("Unexpected event type " + eventType, getLocation());
|
throw new XMLStreamException("Unexpected event type " + eventType, getLocation());
|
||||||
|
@ -85,22 +84,21 @@ abstract class AbstractXMLStreamReader implements XMLStreamReader {
|
||||||
return getName().getNamespaceURI();
|
return getName().getNamespaceURI();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throw new IllegalStateException("parser must be on START_ELEMENT or END_ELEMENT state");
|
throw new IllegalStateException("Parser must be on START_ELEMENT or END_ELEMENT state");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getNamespaceURI(String prefix) {
|
public String getNamespaceURI(String prefix) {
|
||||||
Assert.notNull(prefix, "No prefix given");
|
|
||||||
return getNamespaceContext().getNamespaceURI(prefix);
|
return getNamespaceContext().getNamespaceURI(prefix);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hasText() {
|
public boolean hasText() {
|
||||||
int eventType = getEventType();
|
int eventType = getEventType();
|
||||||
return eventType == XMLStreamConstants.SPACE || eventType == XMLStreamConstants.CHARACTERS ||
|
return (eventType == XMLStreamConstants.SPACE || eventType == XMLStreamConstants.CHARACTERS ||
|
||||||
eventType == XMLStreamConstants.COMMENT || eventType == XMLStreamConstants.CDATA ||
|
eventType == XMLStreamConstants.COMMENT || eventType == XMLStreamConstants.CDATA ||
|
||||||
eventType == XMLStreamConstants.ENTITY_REFERENCE;
|
eventType == XMLStreamConstants.ENTITY_REFERENCE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -110,14 +108,14 @@ abstract class AbstractXMLStreamReader implements XMLStreamReader {
|
||||||
return getName().getPrefix();
|
return getName().getPrefix();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throw new IllegalStateException("parser must be on START_ELEMENT or END_ELEMENT state");
|
throw new IllegalStateException("Parser must be on START_ELEMENT or END_ELEMENT state");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hasName() {
|
public boolean hasName() {
|
||||||
int eventType = getEventType();
|
int eventType = getEventType();
|
||||||
return eventType == XMLStreamConstants.START_ELEMENT || eventType == XMLStreamConstants.END_ELEMENT;
|
return (eventType == XMLStreamConstants.START_ELEMENT || eventType == XMLStreamConstants.END_ELEMENT);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -176,7 +174,7 @@ abstract class AbstractXMLStreamReader implements XMLStreamReader {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hasNext() throws XMLStreamException {
|
public boolean hasNext() {
|
||||||
return getEventType() != END_DOCUMENT;
|
return getEventType() != END_DOCUMENT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -191,8 +189,7 @@ abstract class AbstractXMLStreamReader implements XMLStreamReader {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getTextCharacters(int sourceStart, char[] target, int targetStart, int length)
|
public int getTextCharacters(int sourceStart, char[] target, int targetStart, int length) {
|
||||||
throws XMLStreamException {
|
|
||||||
char[] source = getTextCharacters();
|
char[] source = getTextCharacters();
|
||||||
length = Math.min(length, source.length);
|
length = Math.min(length, source.length);
|
||||||
System.arraycopy(source, sourceStart, target, targetStart, length);
|
System.arraycopy(source, sourceStart, target, targetStart, length);
|
||||||
|
@ -203,4 +200,5 @@ abstract class AbstractXMLStreamReader implements XMLStreamReader {
|
||||||
public int getTextLength() {
|
public int getTextLength() {
|
||||||
return getText().length();
|
return getText().length();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,9 +27,6 @@ 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.
|
||||||
|
@ -46,13 +43,12 @@ 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) {
|
||||||
this.document = (Document) node;
|
this.document = (Document) node;
|
||||||
|
@ -60,9 +56,9 @@ class DomContentHandler implements ContentHandler {
|
||||||
else {
|
else {
|
||||||
this.document = node.getOwnerDocument();
|
this.document = node.getOwnerDocument();
|
||||||
}
|
}
|
||||||
Assert.notNull(this.document, "document must not be null");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private Node getParent() {
|
private Node getParent() {
|
||||||
if (!this.elements.isEmpty()) {
|
if (!this.elements.isEmpty()) {
|
||||||
return this.elements.get(this.elements.size() - 1);
|
return this.elements.get(this.elements.size() - 1);
|
||||||
|
@ -73,7 +69,7 @@ class DomContentHandler implements ContentHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
@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 = this.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++) {
|
||||||
|
@ -89,12 +85,12 @@ class DomContentHandler implements ContentHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void endElement(String uri, String localName, String qName) throws SAXException {
|
public void endElement(String uri, String localName, String qName) {
|
||||||
this.elements.remove(this.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();
|
||||||
|
@ -108,41 +104,41 @@ class DomContentHandler implements ContentHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
@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 = this.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) {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2017 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.
|
||||||
|
@ -97,16 +97,17 @@ class StaxEventHandler extends AbstractStaxHandler {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<Namespace> getNamespaces(Map<String, String> namespaceMapping) {
|
private List<Namespace> getNamespaces(Map<String, String> namespaceMappings) {
|
||||||
List<Namespace> result = new ArrayList<>();
|
List<Namespace> result = new ArrayList<>(namespaceMappings.size());
|
||||||
namespaceMapping.forEach((prefix, namespaceUri) ->
|
namespaceMappings.forEach((prefix, namespaceUri) ->
|
||||||
result.add(this.eventFactory.createNamespace(prefix, namespaceUri)));
|
result.add(this.eventFactory.createNamespace(prefix, namespaceUri)));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<Attribute> getAttributes(Attributes attributes) {
|
private List<Attribute> getAttributes(Attributes attributes) {
|
||||||
List<Attribute> result = new ArrayList<>();
|
int attrLength = attributes.getLength();
|
||||||
for (int i = 0; i < attributes.getLength(); i++) {
|
List<Attribute> result = new ArrayList<>(attrLength);
|
||||||
|
for (int i = 0; i < attrLength; i++) {
|
||||||
QName attrName = toQName(attributes.getURI(i), attributes.getQName(i));
|
QName attrName = toQName(attributes.getURI(i), attributes.getQName(i));
|
||||||
if (!isNamespaceDeclaration(attrName)) {
|
if (!isNamespaceDeclaration(attrName)) {
|
||||||
result.add(this.eventFactory.createAttribute(attrName, attributes.getValue(i)));
|
result.add(this.eventFactory.createAttribute(attrName, attributes.getValue(i)));
|
||||||
|
@ -152,9 +153,8 @@ class StaxEventHandler extends AbstractStaxHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ignored
|
// Ignored
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void skippedEntityInternal(String name) throws XMLStreamException {
|
protected void skippedEntityInternal(String name) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2017 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.
|
||||||
|
@ -42,7 +42,6 @@ import org.xml.sax.ext.Locator2;
|
||||||
import org.xml.sax.helpers.AttributesImpl;
|
import org.xml.sax.helpers.AttributesImpl;
|
||||||
|
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
import org.springframework.util.Assert;
|
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -71,14 +70,13 @@ class StaxEventXMLReader extends AbstractStaxXMLReader {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a new instance of the {@code StaxEventXmlReader} that reads from the given
|
* Constructs a new instance of the {@code StaxEventXmlReader} that reads from
|
||||||
* {@code XMLEventReader}. The supplied event reader must be in {@code XMLStreamConstants.START_DOCUMENT} or
|
* the given {@code XMLEventReader}. The supplied event reader must be in
|
||||||
* {@code XMLStreamConstants.START_ELEMENT} state.
|
* {@code XMLStreamConstants.START_DOCUMENT} or {@code XMLStreamConstants.START_ELEMENT} state.
|
||||||
* @param reader the {@code XMLEventReader} to read from
|
* @param reader the {@code XMLEventReader} to read from
|
||||||
* @throws IllegalStateException if the reader is not at the start of a document or element
|
* @throws IllegalStateException if the reader is not at the start of a document or element
|
||||||
*/
|
*/
|
||||||
StaxEventXMLReader(XMLEventReader reader) {
|
StaxEventXMLReader(XMLEventReader reader) {
|
||||||
Assert.notNull(reader, "XMLEventReader must not be null");
|
|
||||||
try {
|
try {
|
||||||
XMLEvent event = reader.peek();
|
XMLEvent event = reader.peek();
|
||||||
if (event != null && !(event.isStartDocument() || event.isStartElement())) {
|
if (event != null && !(event.isStartDocument() || event.isStartElement())) {
|
||||||
|
|
|
@ -27,8 +27,6 @@ import org.xml.sax.Locator;
|
||||||
import org.xml.sax.SAXException;
|
import org.xml.sax.SAXException;
|
||||||
import org.xml.sax.ext.LexicalHandler;
|
import org.xml.sax.ext.LexicalHandler;
|
||||||
|
|
||||||
import org.springframework.util.Assert;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SAX {@link org.xml.sax.ContentHandler} and {@link LexicalHandler}
|
* SAX {@link org.xml.sax.ContentHandler} and {@link LexicalHandler}
|
||||||
* that writes to an {@link XMLStreamWriter}.
|
* that writes to an {@link XMLStreamWriter}.
|
||||||
|
@ -42,7 +40,6 @@ class StaxStreamHandler extends AbstractStaxHandler {
|
||||||
|
|
||||||
|
|
||||||
public StaxStreamHandler(XMLStreamWriter streamWriter) {
|
public StaxStreamHandler(XMLStreamWriter streamWriter) {
|
||||||
Assert.notNull(streamWriter, "XMLStreamWriter must not be null");
|
|
||||||
this.streamWriter = streamWriter;
|
this.streamWriter = streamWriter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -28,7 +28,6 @@ import org.xml.sax.ext.Locator2;
|
||||||
import org.xml.sax.helpers.AttributesImpl;
|
import org.xml.sax.helpers.AttributesImpl;
|
||||||
|
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
import org.springframework.util.Assert;
|
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -63,7 +62,6 @@ class StaxStreamXMLReader extends AbstractStaxXMLReader {
|
||||||
* @throws IllegalStateException if the reader is not at the start of a document or element
|
* @throws IllegalStateException if the reader is not at the start of a document or element
|
||||||
*/
|
*/
|
||||||
StaxStreamXMLReader(XMLStreamReader reader) {
|
StaxStreamXMLReader(XMLStreamReader reader) {
|
||||||
Assert.notNull(reader, "XMLStreamReader must not be null");
|
|
||||||
int event = reader.getEventType();
|
int event = reader.getEventType();
|
||||||
if (!(event == XMLStreamConstants.START_DOCUMENT || event == XMLStreamConstants.START_ELEMENT)) {
|
if (!(event == XMLStreamConstants.START_DOCUMENT || event == XMLStreamConstants.START_ELEMENT)) {
|
||||||
throw new IllegalStateException("XMLEventReader not at start of document or element");
|
throw new IllegalStateException("XMLEventReader not at start of document or element");
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2016 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.
|
||||||
|
@ -29,8 +29,6 @@ import javax.xml.stream.events.EndElement;
|
||||||
import javax.xml.stream.events.Namespace;
|
import javax.xml.stream.events.Namespace;
|
||||||
import javax.xml.stream.events.StartElement;
|
import javax.xml.stream.events.StartElement;
|
||||||
|
|
||||||
import org.springframework.util.Assert;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implementation of the {@link javax.xml.stream.XMLStreamWriter} interface
|
* Implementation of the {@link javax.xml.stream.XMLStreamWriter} interface
|
||||||
* that wraps an {@link XMLEventWriter}.
|
* that wraps an {@link XMLEventWriter}.
|
||||||
|
@ -53,8 +51,6 @@ class XMLEventStreamWriter implements XMLStreamWriter {
|
||||||
|
|
||||||
|
|
||||||
public XMLEventStreamWriter(XMLEventWriter eventWriter, XMLEventFactory eventFactory) {
|
public XMLEventStreamWriter(XMLEventWriter eventWriter, XMLEventFactory eventFactory) {
|
||||||
Assert.notNull(eventWriter, "'eventWriter' must not be null");
|
|
||||||
Assert.notNull(eventFactory, "'eventFactory' must not be null");
|
|
||||||
this.eventWriter = eventWriter;
|
this.eventWriter = eventWriter;
|
||||||
this.eventFactory = eventFactory;
|
this.eventFactory = eventFactory;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue