Polishing

This commit is contained in:
Juergen Hoeller
2018-08-13 14:23:41 +02:00
parent 5bd5df3ec4
commit 484a2f3f2d
12 changed files with 79 additions and 99 deletions

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.
@@ -145,7 +145,7 @@ abstract class AbstractStaxXMLReader extends AbstractXMLReader {
* Parse the StAX XML reader passed at construction-time.
* <p><b>NOTE:</b>: The given system identifier is not read, but ignored.
* @param ignored is ignored
* @throws SAXException A SAX exception, possibly wrapping a {@code XMLStreamException}
* @throws SAXException a SAX exception, possibly wrapping a {@code XMLStreamException}
*/
@Override
public final void parse(String ignored) throws SAXException {
@@ -182,13 +182,10 @@ abstract class AbstractStaxXMLReader extends AbstractXMLReader {
* @see org.xml.sax.ContentHandler#startPrefixMapping(String, String)
*/
protected void startPrefixMapping(String prefix, String namespace) throws SAXException {
if (getContentHandler() != null) {
if (getContentHandler() != null && StringUtils.hasLength(namespace)) {
if (prefix == null) {
prefix = "";
}
if (!StringUtils.hasLength(namespace)) {
return;
}
if (!namespace.equals(this.namespaces.get(prefix))) {
getContentHandler().startPrefixMapping(prefix, namespace);
this.namespaces.put(prefix, namespace);
@@ -201,11 +198,9 @@ abstract class AbstractStaxXMLReader extends AbstractXMLReader {
* @see org.xml.sax.ContentHandler#endPrefixMapping(String)
*/
protected void endPrefixMapping(String prefix) throws SAXException {
if (getContentHandler() != null) {
if (this.namespaces.containsKey(prefix)) {
getContentHandler().endPrefixMapping(prefix);
this.namespaces.remove(prefix);
}
if (getContentHandler() != null && this.namespaces.containsKey(prefix)) {
getContentHandler().endPrefixMapping(prefix);
this.namespaces.remove(prefix);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 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.
@@ -21,8 +21,6 @@ import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import org.springframework.util.Assert;
/**
* Abstract base class for {@code XMLStreamReader}s.
*
@@ -34,7 +32,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();
@@ -48,11 +46,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());
@@ -84,22 +82,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
@@ -109,14 +106,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
@@ -174,7 +171,7 @@ abstract class AbstractXMLStreamReader implements XMLStreamReader {
}
@Override
public boolean hasNext() throws XMLStreamException {
public boolean hasNext() {
return getEventType() != END_DOCUMENT;
}
@@ -189,8 +186,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);
@@ -201,4 +197,5 @@ abstract class AbstractXMLStreamReader implements XMLStreamReader {
public int getTextLength() {
return getText().length();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 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.
@@ -27,16 +27,13 @@ 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.
*
* @author Arjen Poutsma
* @see org.w3c.dom.Node
* @since 3.0
* @see org.w3c.dom.Node
*/
class DomContentHandler implements ContentHandler {
@@ -46,36 +43,35 @@ 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) {
document = (Document) node;
this.document = (Document) node;
}
else {
document = node.getOwnerDocument();
this.document = node.getOwnerDocument();
}
Assert.notNull(document, "document must not be null");
}
private Node getParent() {
if (!elements.isEmpty()) {
return elements.get(elements.size() - 1);
if (!this.elements.isEmpty()) {
return this.elements.get(this.elements.size() - 1);
}
else {
return node;
return this.node;
}
}
@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 = document.createElementNS(uri, qName);
Element element = this.document.createElementNS(uri, qName);
for (int i = 0; i < attributes.getLength(); i++) {
String attrUri = attributes.getURI(i);
String attrQname = attributes.getQName(i);
@@ -85,16 +81,16 @@ class DomContentHandler implements ContentHandler {
}
}
element = (Element) parent.appendChild(element);
elements.add(element);
this.elements.add(element);
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
elements.remove(elements.size() - 1);
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();
@@ -102,47 +98,47 @@ class DomContentHandler implements ContentHandler {
((Text) lastChild).appendData(data);
}
else {
Text text = document.createTextNode(data);
Text text = this.document.createTextNode(data);
parent.appendChild(text);
}
}
@Override
public void processingInstruction(String target, String data) throws SAXException {
public void processingInstruction(String target, String data) {
Node parent = getParent();
ProcessingInstruction pi = document.createProcessingInstruction(target, data);
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-2014 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.
@@ -95,9 +95,9 @@ class StaxEventHandler extends AbstractStaxHandler {
}
private List<Namespace> getNamespaces(Map<String, String> namespaceMapping) {
List<Namespace> result = new ArrayList<Namespace>();
for (Map.Entry<String, String> entry : namespaceMapping.entrySet()) {
private List<Namespace> getNamespaces(Map<String, String> namespaceMappings) {
List<Namespace> result = new ArrayList<Namespace>(namespaceMappings.size());
for (Map.Entry<String, String> entry : namespaceMappings.entrySet()) {
String prefix = entry.getKey();
String namespaceUri = entry.getValue();
result.add(this.eventFactory.createNamespace(prefix, namespaceUri));
@@ -106,8 +106,9 @@ class StaxEventHandler extends AbstractStaxHandler {
}
private List<Attribute> getAttributes(Attributes attributes) {
List<Attribute> result = new ArrayList<Attribute>();
for (int i = 0; i < attributes.getLength(); i++) {
int attrLength = attributes.getLength();
List<Attribute> result = new ArrayList<Attribute>(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)));
@@ -153,9 +154,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-2013 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.
@@ -41,7 +41,6 @@ import org.xml.sax.SAXException;
import org.xml.sax.ext.Locator2;
import org.xml.sax.helpers.AttributesImpl;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
@@ -69,14 +68,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, "'reader' 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

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2017 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.
@@ -27,7 +27,6 @@ import org.xml.sax.SAXException;
import org.xml.sax.ext.Locator2;
import org.xml.sax.helpers.AttributesImpl;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
@@ -61,7 +60,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, "'reader' 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");
@@ -238,7 +236,7 @@ class StaxStreamXMLReader extends AbstractStaxXMLReader {
private void handleDtd() throws SAXException {
if (getLexicalHandler() != null) {
javax.xml.stream.Location location = this.reader.getLocation();
Location location = this.reader.getLocation();
getLexicalHandler().startDTD(null, location.getPublicId(), location.getSystemId());
}
if (getLexicalHandler() != null) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 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.
@@ -92,7 +92,7 @@ class XMLEventStreamReader extends AbstractXMLStreamReader {
@Override
public boolean isStandalone() {
if (this.event.isStartDocument()) {
return ((StartDocument) event).isStandalone();
return ((StartDocument) this.event).isStandalone();
}
else {
throw new IllegalStateException();
@@ -147,7 +147,7 @@ class XMLEventStreamReader extends AbstractXMLStreamReader {
@Override
public String getText() {
if (this.event.isCharacters()) {
return event.asCharacters().getData();
return this.event.asCharacters().getData();
}
else if (this.event.getEventType() == XMLEvent.COMMENT) {
return ((Comment) this.event).getText();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 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;
}