Skip to content

Commit 7894ecf

Browse files
committed
Polishing
1 parent 0e3f23e commit 7894ecf

File tree

9 files changed

+57
-74
lines changed

9 files changed

+57
-74
lines changed

spring-core/src/main/java/org/springframework/util/xml/AbstractStaxXMLReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public final void parse(InputSource ignored) throws SAXException {
147147
* Parse the StAX XML reader passed at construction-time.
148148
* <p><b>NOTE:</b>: The given system identifier is not read, but ignored.
149149
* @param ignored is ignored
150-
* @throws SAXException A SAX exception, possibly wrapping a {@code XMLStreamException}
150+
* @throws SAXException a SAX exception, possibly wrapping a {@code XMLStreamException}
151151
*/
152152
@Override
153153
public final void parse(String ignored) throws SAXException {

spring-core/src/main/java/org/springframework/util/xml/AbstractXMLStreamReader.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,7 +22,6 @@
2222
import javax.xml.stream.XMLStreamReader;
2323

2424
import org.springframework.lang.Nullable;
25-
import org.springframework.util.Assert;
2625

2726
/**
2827
* Abstract base class for {@code XMLStreamReader}s.
@@ -35,7 +34,7 @@ abstract class AbstractXMLStreamReader implements XMLStreamReader {
3534
@Override
3635
public String getElementText() throws XMLStreamException {
3736
if (getEventType() != XMLStreamConstants.START_ELEMENT) {
38-
throw new XMLStreamException("parser must be on START_ELEMENT to read next text", getLocation());
37+
throw new XMLStreamException("Parser must be on START_ELEMENT to read next text", getLocation());
3938
}
4039
int eventType = next();
4140
StringBuilder builder = new StringBuilder();
@@ -49,11 +48,11 @@ else if (eventType == XMLStreamConstants.PROCESSING_INSTRUCTION ||
4948
// skipping
5049
}
5150
else if (eventType == XMLStreamConstants.END_DOCUMENT) {
52-
throw new XMLStreamException("unexpected end of document when reading element text content",
51+
throw new XMLStreamException("Unexpected end of document when reading element text content",
5352
getLocation());
5453
}
5554
else if (eventType == XMLStreamConstants.START_ELEMENT) {
56-
throw new XMLStreamException("element text content may not contain START_ELEMENT", getLocation());
55+
throw new XMLStreamException("Element text content may not contain START_ELEMENT", getLocation());
5756
}
5857
else {
5958
throw new XMLStreamException("Unexpected event type " + eventType, getLocation());
@@ -85,22 +84,21 @@ public String getNamespaceURI() {
8584
return getName().getNamespaceURI();
8685
}
8786
else {
88-
throw new IllegalStateException("parser must be on START_ELEMENT or END_ELEMENT state");
87+
throw new IllegalStateException("Parser must be on START_ELEMENT or END_ELEMENT state");
8988
}
9089
}
9190

9291
@Override
9392
public String getNamespaceURI(String prefix) {
94-
Assert.notNull(prefix, "No prefix given");
9593
return getNamespaceContext().getNamespaceURI(prefix);
9694
}
9795

9896
@Override
9997
public boolean hasText() {
10098
int eventType = getEventType();
101-
return eventType == XMLStreamConstants.SPACE || eventType == XMLStreamConstants.CHARACTERS ||
99+
return (eventType == XMLStreamConstants.SPACE || eventType == XMLStreamConstants.CHARACTERS ||
102100
eventType == XMLStreamConstants.COMMENT || eventType == XMLStreamConstants.CDATA ||
103-
eventType == XMLStreamConstants.ENTITY_REFERENCE;
101+
eventType == XMLStreamConstants.ENTITY_REFERENCE);
104102
}
105103

106104
@Override
@@ -110,14 +108,14 @@ public String getPrefix() {
110108
return getName().getPrefix();
111109
}
112110
else {
113-
throw new IllegalStateException("parser must be on START_ELEMENT or END_ELEMENT state");
111+
throw new IllegalStateException("Parser must be on START_ELEMENT or END_ELEMENT state");
114112
}
115113
}
116114

117115
@Override
118116
public boolean hasName() {
119117
int eventType = getEventType();
120-
return eventType == XMLStreamConstants.START_ELEMENT || eventType == XMLStreamConstants.END_ELEMENT;
118+
return (eventType == XMLStreamConstants.START_ELEMENT || eventType == XMLStreamConstants.END_ELEMENT);
121119
}
122120

123121
@Override
@@ -176,7 +174,7 @@ public String getAttributeValue(@Nullable String namespaceURI, String localName)
176174
}
177175

178176
@Override
179-
public boolean hasNext() throws XMLStreamException {
177+
public boolean hasNext() {
180178
return getEventType() != END_DOCUMENT;
181179
}
182180

@@ -191,8 +189,7 @@ public char[] getTextCharacters() {
191189
}
192190

193191
@Override
194-
public int getTextCharacters(int sourceStart, char[] target, int targetStart, int length)
195-
throws XMLStreamException {
192+
public int getTextCharacters(int sourceStart, char[] target, int targetStart, int length) {
196193
char[] source = getTextCharacters();
197194
length = Math.min(length, source.length);
198195
System.arraycopy(source, sourceStart, target, targetStart, length);
@@ -203,4 +200,5 @@ public int getTextCharacters(int sourceStart, char[] target, int targetStart, in
203200
public int getTextLength() {
204201
return getText().length();
205202
}
203+
206204
}
Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,16 +27,13 @@
2727
import org.xml.sax.Attributes;
2828
import org.xml.sax.ContentHandler;
2929
import org.xml.sax.Locator;
30-
import org.xml.sax.SAXException;
31-
32-
import org.springframework.util.Assert;
3330

3431
/**
3532
* SAX {@code ContentHandler} that transforms callback calls to DOM {@code Node}s.
3633
*
3734
* @author Arjen Poutsma
38-
* @see org.w3c.dom.Node
3935
* @since 3.0
36+
* @see org.w3c.dom.Node
4037
*/
4138
class DomContentHandler implements ContentHandler {
4239

@@ -46,36 +43,35 @@ class DomContentHandler implements ContentHandler {
4643

4744
private final Node node;
4845

46+
4947
/**
50-
* Creates a new instance of the {@code DomContentHandler} with the given node.
51-
*
48+
* Create a new instance of the {@code DomContentHandler} with the given node.
5249
* @param node the node to publish events to
5350
*/
5451
DomContentHandler(Node node) {
55-
Assert.notNull(node, "node must not be null");
5652
this.node = node;
5753
if (node instanceof Document) {
58-
document = (Document) node;
54+
this.document = (Document) node;
5955
}
6056
else {
61-
document = node.getOwnerDocument();
57+
this.document = node.getOwnerDocument();
6258
}
63-
Assert.notNull(document, "document must not be null");
6459
}
6560

61+
6662
private Node getParent() {
67-
if (!elements.isEmpty()) {
68-
return elements.get(elements.size() - 1);
63+
if (!this.elements.isEmpty()) {
64+
return this.elements.get(this.elements.size() - 1);
6965
}
7066
else {
71-
return node;
67+
return this.node;
7268
}
7369
}
7470

7571
@Override
76-
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
72+
public void startElement(String uri, String localName, String qName, Attributes attributes) {
7773
Node parent = getParent();
78-
Element element = document.createElementNS(uri, qName);
74+
Element element = this.document.createElementNS(uri, qName);
7975
for (int i = 0; i < attributes.getLength(); i++) {
8076
String attrUri = attributes.getURI(i);
8177
String attrQname = attributes.getQName(i);
@@ -85,64 +81,64 @@ public void startElement(String uri, String localName, String qName, Attributes
8581
}
8682
}
8783
element = (Element) parent.appendChild(element);
88-
elements.add(element);
84+
this.elements.add(element);
8985
}
9086

9187
@Override
92-
public void endElement(String uri, String localName, String qName) throws SAXException {
93-
elements.remove(elements.size() - 1);
88+
public void endElement(String uri, String localName, String qName) {
89+
this.elements.remove(this.elements.size() - 1);
9490
}
9591

9692
@Override
97-
public void characters(char[] ch, int start, int length) throws SAXException {
93+
public void characters(char[] ch, int start, int length) {
9894
String data = new String(ch, start, length);
9995
Node parent = getParent();
10096
Node lastChild = parent.getLastChild();
10197
if (lastChild != null && lastChild.getNodeType() == Node.TEXT_NODE) {
10298
((Text) lastChild).appendData(data);
10399
}
104100
else {
105-
Text text = document.createTextNode(data);
101+
Text text = this.document.createTextNode(data);
106102
parent.appendChild(text);
107103
}
108104
}
109105

110106
@Override
111-
public void processingInstruction(String target, String data) throws SAXException {
107+
public void processingInstruction(String target, String data) {
112108
Node parent = getParent();
113-
ProcessingInstruction pi = document.createProcessingInstruction(target, data);
109+
ProcessingInstruction pi = this.document.createProcessingInstruction(target, data);
114110
parent.appendChild(pi);
115111
}
116112

117-
/*
118-
* Unsupported
119-
*/
113+
114+
// Unsupported
120115

121116
@Override
122117
public void setDocumentLocator(Locator locator) {
123118
}
124119

125120
@Override
126-
public void startDocument() throws SAXException {
121+
public void startDocument() {
127122
}
128123

129124
@Override
130-
public void endDocument() throws SAXException {
125+
public void endDocument() {
131126
}
132127

133128
@Override
134-
public void startPrefixMapping(String prefix, String uri) throws SAXException {
129+
public void startPrefixMapping(String prefix, String uri) {
135130
}
136131

137132
@Override
138-
public void endPrefixMapping(String prefix) throws SAXException {
133+
public void endPrefixMapping(String prefix) {
139134
}
140135

141136
@Override
142-
public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
137+
public void ignorableWhitespace(char[] ch, int start, int length) {
143138
}
144139

145140
@Override
146-
public void skippedEntity(String name) throws SAXException {
141+
public void skippedEntity(String name) {
147142
}
143+
148144
}

spring-core/src/main/java/org/springframework/util/xml/StaxEventHandler.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -97,16 +97,17 @@ protected void startElementInternal(QName name, Attributes atts,
9797

9898
}
9999

100-
private List<Namespace> getNamespaces(Map<String, String> namespaceMapping) {
101-
List<Namespace> result = new ArrayList<>();
102-
namespaceMapping.forEach((prefix, namespaceUri) ->
100+
private List<Namespace> getNamespaces(Map<String, String> namespaceMappings) {
101+
List<Namespace> result = new ArrayList<>(namespaceMappings.size());
102+
namespaceMappings.forEach((prefix, namespaceUri) ->
103103
result.add(this.eventFactory.createNamespace(prefix, namespaceUri)));
104104
return result;
105105
}
106106

107107
private List<Attribute> getAttributes(Attributes attributes) {
108-
List<Attribute> result = new ArrayList<>();
109-
for (int i = 0; i < attributes.getLength(); i++) {
108+
int attrLength = attributes.getLength();
109+
List<Attribute> result = new ArrayList<>(attrLength);
110+
for (int i = 0; i < attrLength; i++) {
110111
QName attrName = toQName(attributes.getURI(i), attributes.getQName(i));
111112
if (!isNamespaceDeclaration(attrName)) {
112113
result.add(this.eventFactory.createAttribute(attrName, attributes.getValue(i)));
@@ -152,9 +153,8 @@ protected void commentInternal(String comment) throws XMLStreamException {
152153
}
153154

154155
// Ignored
155-
156156
@Override
157-
protected void skippedEntityInternal(String name) throws XMLStreamException {
157+
protected void skippedEntityInternal(String name) {
158158
}
159159

160160

spring-core/src/main/java/org/springframework/util/xml/StaxEventXMLReader.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -42,7 +42,6 @@
4242
import org.xml.sax.helpers.AttributesImpl;
4343

4444
import org.springframework.lang.Nullable;
45-
import org.springframework.util.Assert;
4645
import org.springframework.util.StringUtils;
4746

4847
/**
@@ -71,14 +70,13 @@ class StaxEventXMLReader extends AbstractStaxXMLReader {
7170

7271

7372
/**
74-
* Constructs a new instance of the {@code StaxEventXmlReader} that reads from the given
75-
* {@code XMLEventReader}. The supplied event reader must be in {@code XMLStreamConstants.START_DOCUMENT} or
76-
* {@code XMLStreamConstants.START_ELEMENT} state.
73+
* Constructs a new instance of the {@code StaxEventXmlReader} that reads from
74+
* the given {@code XMLEventReader}. The supplied event reader must be in
75+
* {@code XMLStreamConstants.START_DOCUMENT} or {@code XMLStreamConstants.START_ELEMENT} state.
7776
* @param reader the {@code XMLEventReader} to read from
7877
* @throws IllegalStateException if the reader is not at the start of a document or element
7978
*/
8079
StaxEventXMLReader(XMLEventReader reader) {
81-
Assert.notNull(reader, "XMLEventReader must not be null");
8280
try {
8381
XMLEvent event = reader.peek();
8482
if (event != null && !(event.isStartDocument() || event.isStartElement())) {

spring-core/src/main/java/org/springframework/util/xml/StaxStreamHandler.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
import org.xml.sax.SAXException;
2828
import org.xml.sax.ext.LexicalHandler;
2929

30-
import org.springframework.util.Assert;
31-
3230
/**
3331
* SAX {@link org.xml.sax.ContentHandler} and {@link LexicalHandler}
3432
* that writes to an {@link XMLStreamWriter}.
@@ -42,7 +40,6 @@ class StaxStreamHandler extends AbstractStaxHandler {
4240

4341

4442
public StaxStreamHandler(XMLStreamWriter streamWriter) {
45-
Assert.notNull(streamWriter, "XMLStreamWriter must not be null");
4643
this.streamWriter = streamWriter;
4744
}
4845

spring-core/src/main/java/org/springframework/util/xml/StaxStreamXMLReader.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.xml.sax.helpers.AttributesImpl;
2929

3030
import org.springframework.lang.Nullable;
31-
import org.springframework.util.Assert;
3231
import org.springframework.util.StringUtils;
3332

3433
/**
@@ -63,7 +62,6 @@ class StaxStreamXMLReader extends AbstractStaxXMLReader {
6362
* @throws IllegalStateException if the reader is not at the start of a document or element
6463
*/
6564
StaxStreamXMLReader(XMLStreamReader reader) {
66-
Assert.notNull(reader, "XMLStreamReader must not be null");
6765
int event = reader.getEventType();
6866
if (!(event == XMLStreamConstants.START_DOCUMENT || event == XMLStreamConstants.START_ELEMENT)) {
6967
throw new IllegalStateException("XMLEventReader not at start of document or element");

spring-core/src/main/java/org/springframework/util/xml/XMLEventStreamReader.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -95,7 +95,7 @@ public Object getProperty(String name) throws IllegalArgumentException {
9595
@Override
9696
public boolean isStandalone() {
9797
if (this.event.isStartDocument()) {
98-
return ((StartDocument) event).isStandalone();
98+
return ((StartDocument) this.event).isStandalone();
9999
}
100100
else {
101101
throw new IllegalStateException();
@@ -152,7 +152,7 @@ public int getTextStart() {
152152
@Override
153153
public String getText() {
154154
if (this.event.isCharacters()) {
155-
return event.asCharacters().getData();
155+
return this.event.asCharacters().getData();
156156
}
157157
else if (this.event.getEventType() == XMLEvent.COMMENT) {
158158
return ((Comment) this.event).getText();

0 commit comments

Comments
 (0)