Polishing

This commit is contained in:
Juergen Hoeller 2018-08-13 14:29:36 +02:00
parent 04a7f0884b
commit ac544924c8
7 changed files with 41 additions and 58 deletions

View File

@ -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");
* 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 org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
* Abstract base class for {@code XMLStreamReader}s.
@ -35,7 +34,7 @@ abstract class AbstractXMLStreamReader implements XMLStreamReader {
@Override
public String getElementText() throws XMLStreamException {
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();
StringBuilder builder = new StringBuilder();
@ -49,11 +48,11 @@ abstract class AbstractXMLStreamReader implements XMLStreamReader {
// skipping
}
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());
}
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 {
throw new XMLStreamException("Unexpected event type " + eventType, getLocation());
@ -85,22 +84,21 @@ abstract class AbstractXMLStreamReader implements XMLStreamReader {
return getName().getNamespaceURI();
}
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
public String getNamespaceURI(String prefix) {
Assert.notNull(prefix, "No prefix given");
return getNamespaceContext().getNamespaceURI(prefix);
}
@Override
public boolean hasText() {
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.ENTITY_REFERENCE;
eventType == XMLStreamConstants.ENTITY_REFERENCE);
}
@Override
@ -110,14 +108,14 @@ abstract class AbstractXMLStreamReader implements XMLStreamReader {
return getName().getPrefix();
}
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
public boolean hasName() {
int eventType = getEventType();
return eventType == XMLStreamConstants.START_ELEMENT || eventType == XMLStreamConstants.END_ELEMENT;
return (eventType == XMLStreamConstants.START_ELEMENT || eventType == XMLStreamConstants.END_ELEMENT);
}
@Override
@ -176,7 +174,7 @@ abstract class AbstractXMLStreamReader implements XMLStreamReader {
}
@Override
public boolean hasNext() throws XMLStreamException {
public boolean hasNext() {
return getEventType() != END_DOCUMENT;
}
@ -191,8 +189,7 @@ abstract class AbstractXMLStreamReader implements XMLStreamReader {
}
@Override
public int getTextCharacters(int sourceStart, char[] target, int targetStart, int length)
throws XMLStreamException {
public int getTextCharacters(int sourceStart, char[] target, int targetStart, int length) {
char[] source = getTextCharacters();
length = Math.min(length, source.length);
System.arraycopy(source, sourceStart, target, targetStart, length);
@ -203,4 +200,5 @@ abstract class AbstractXMLStreamReader implements XMLStreamReader {
public int getTextLength() {
return getText().length();
}
}

View File

@ -27,9 +27,6 @@ import org.w3c.dom.Text;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
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.
@ -46,13 +43,12 @@ class DomContentHandler implements ContentHandler {
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
*/
DomContentHandler(Node node) {
Assert.notNull(node, "node must not be null");
this.node = node;
if (node instanceof Document) {
this.document = (Document) node;
@ -60,9 +56,9 @@ class DomContentHandler implements ContentHandler {
else {
this.document = node.getOwnerDocument();
}
Assert.notNull(this.document, "document must not be null");
}
private Node getParent() {
if (!this.elements.isEmpty()) {
return this.elements.get(this.elements.size() - 1);
@ -73,7 +69,7 @@ class DomContentHandler implements ContentHandler {
}
@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();
Element element = this.document.createElementNS(uri, qName);
for (int i = 0; i < attributes.getLength(); i++) {
@ -89,12 +85,12 @@ class DomContentHandler implements ContentHandler {
}
@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);
}
@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);
Node parent = getParent();
Node lastChild = parent.getLastChild();
@ -108,41 +104,41 @@ class DomContentHandler implements ContentHandler {
}
@Override
public void processingInstruction(String target, String data) throws SAXException {
public void processingInstruction(String target, String data) {
Node parent = getParent();
ProcessingInstruction pi = this.document.createProcessingInstruction(target, data);
parent.appendChild(pi);
}
/*
* Unsupported
*/
// Unsupported
@Override
public void setDocumentLocator(Locator locator) {
}
@Override
public void startDocument() throws SAXException {
public void startDocument() {
}
@Override
public void endDocument() throws SAXException {
public void endDocument() {
}
@Override
public void startPrefixMapping(String prefix, String uri) throws SAXException {
public void startPrefixMapping(String prefix, String uri) {
}
@Override
public void endPrefixMapping(String prefix) throws SAXException {
public void endPrefixMapping(String prefix) {
}
@Override
public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
public void ignorableWhitespace(char[] ch, int start, int length) {
}
@Override
public void skippedEntity(String name) throws SAXException {
public void skippedEntity(String name) {
}
}

View File

@ -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");
* 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) {
List<Namespace> result = new ArrayList<>();
namespaceMapping.forEach((prefix, namespaceUri) ->
private List<Namespace> getNamespaces(Map<String, String> namespaceMappings) {
List<Namespace> result = new ArrayList<>(namespaceMappings.size());
namespaceMappings.forEach((prefix, namespaceUri) ->
result.add(this.eventFactory.createNamespace(prefix, namespaceUri)));
return result;
}
private List<Attribute> getAttributes(Attributes attributes) {
List<Attribute> result = new ArrayList<>();
for (int i = 0; i < attributes.getLength(); i++) {
int attrLength = attributes.getLength();
List<Attribute> result = new ArrayList<>(attrLength);
for (int i = 0; i < attrLength; i++) {
QName attrName = toQName(attributes.getURI(i), attributes.getQName(i));
if (!isNamespaceDeclaration(attrName)) {
result.add(this.eventFactory.createAttribute(attrName, attributes.getValue(i)));
@ -152,9 +153,8 @@ class StaxEventHandler extends AbstractStaxHandler {
}
// Ignored
@Override
protected void skippedEntityInternal(String name) throws XMLStreamException {
protected void skippedEntityInternal(String name) {
}

View File

@ -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");
* 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.springframework.lang.Nullable;
import org.springframework.util.Assert;
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
* {@code XMLEventReader}. The supplied event reader must be in {@code XMLStreamConstants.START_DOCUMENT} or
* {@code XMLStreamConstants.START_ELEMENT} state.
* Constructs a new instance of the {@code StaxEventXmlReader} that reads from
* the given {@code XMLEventReader}. The supplied event 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
*/
StaxEventXMLReader(XMLEventReader reader) {
Assert.notNull(reader, "XMLEventReader must not be null");
try {
XMLEvent event = reader.peek();
if (event != null && !(event.isStartDocument() || event.isStartElement())) {

View File

@ -27,8 +27,6 @@ import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.ext.LexicalHandler;
import org.springframework.util.Assert;
/**
* SAX {@link org.xml.sax.ContentHandler} and {@link LexicalHandler}
* that writes to an {@link XMLStreamWriter}.
@ -42,7 +40,6 @@ class StaxStreamHandler extends AbstractStaxHandler {
public StaxStreamHandler(XMLStreamWriter streamWriter) {
Assert.notNull(streamWriter, "XMLStreamWriter must not be null");
this.streamWriter = streamWriter;
}

View File

@ -28,7 +28,6 @@ import org.xml.sax.ext.Locator2;
import org.xml.sax.helpers.AttributesImpl;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
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
*/
StaxStreamXMLReader(XMLStreamReader reader) {
Assert.notNull(reader, "XMLStreamReader must not be null");
int event = reader.getEventType();
if (!(event == XMLStreamConstants.START_DOCUMENT || event == XMLStreamConstants.START_ELEMENT)) {
throw new IllegalStateException("XMLEventReader not at start of document or element");

View File

@ -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");
* 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.StartElement;
import org.springframework.util.Assert;
/**
* Implementation of the {@link javax.xml.stream.XMLStreamWriter} interface
* that wraps an {@link XMLEventWriter}.
@ -53,8 +51,6 @@ class XMLEventStreamWriter implements XMLStreamWriter {
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.eventFactory = eventFactory;
}