Skip to content

Commit d7fe92d

Browse files
committed
Abstract(Stax)XMLReader recognizes standard features as not supported
Issue: SPR-14056 (cherry picked from commit 35eb52e)
1 parent 3b7ca7e commit d7fe92d

File tree

2 files changed

+62
-43
lines changed

2 files changed

+62
-43
lines changed

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

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -35,6 +35,7 @@
3535
* Abstract base class for SAX {@code XMLReader} implementations that use StAX as a basis.
3636
*
3737
* @author Arjen Poutsma
38+
* @author Juergen Hoeller
3839
* @since 3.0
3940
* @see #setContentHandler(org.xml.sax.ContentHandler)
4041
* @see #setDTDHandler(org.xml.sax.DTDHandler)
@@ -58,6 +59,7 @@ abstract class AbstractStaxXMLReader extends AbstractXMLReader {
5859

5960
private final Map<String, String> namespaces = new LinkedHashMap<String, String>();
6061

62+
6163
@Override
6264
public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
6365
if (NAMESPACES_FEATURE_NAME.equals(name)) {
@@ -170,12 +172,13 @@ private void parse() throws SAXException {
170172
}
171173

172174
/**
173-
* Template-method that parses the StAX reader passed at construction-time.
175+
* Template method that parses the StAX reader passed at construction-time.
174176
*/
175177
protected abstract void parseInternal() throws SAXException, XMLStreamException;
176178

179+
177180
/**
178-
* Starts the prefix mapping for the given prefix.
181+
* Start the prefix mapping for the given prefix.
179182
* @see org.xml.sax.ContentHandler#startPrefixMapping(String, String)
180183
*/
181184
protected void startPrefixMapping(String prefix, String namespace) throws SAXException {
@@ -186,57 +189,58 @@ protected void startPrefixMapping(String prefix, String namespace) throws SAXExc
186189
if (!StringUtils.hasLength(namespace)) {
187190
return;
188191
}
189-
if (!namespace.equals(namespaces.get(prefix))) {
192+
if (!namespace.equals(this.namespaces.get(prefix))) {
190193
getContentHandler().startPrefixMapping(prefix, namespace);
191-
namespaces.put(prefix, namespace);
194+
this.namespaces.put(prefix, namespace);
192195
}
193196
}
194197
}
195198

196199
/**
197-
* Ends the prefix mapping for the given prefix.
200+
* End the prefix mapping for the given prefix.
198201
* @see org.xml.sax.ContentHandler#endPrefixMapping(String)
199202
*/
200203
protected void endPrefixMapping(String prefix) throws SAXException {
201204
if (getContentHandler() != null) {
202-
if (namespaces.containsKey(prefix)) {
205+
if (this.namespaces.containsKey(prefix)) {
203206
getContentHandler().endPrefixMapping(prefix);
204-
namespaces.remove(prefix);
207+
this.namespaces.remove(prefix);
205208
}
206209
}
207210
}
208211

212+
209213
/**
210-
* Implementation of the {@code Locator} interface that is based on a StAX {@code Location}.
214+
* Implementation of the {@code Locator} interface based on a given StAX {@code Location}.
211215
* @see Locator
212216
* @see Location
213217
*/
214218
private static class StaxLocator implements Locator {
215219

216-
private Location location;
220+
private final Location location;
217221

218-
protected StaxLocator(Location location) {
222+
public StaxLocator(Location location) {
219223
this.location = location;
220224
}
221225

222226
@Override
223227
public String getPublicId() {
224-
return location.getPublicId();
228+
return this.location.getPublicId();
225229
}
226230

227231
@Override
228232
public String getSystemId() {
229-
return location.getSystemId();
233+
return this.location.getSystemId();
230234
}
231235

232236
@Override
233237
public int getLineNumber() {
234-
return location.getLineNumber();
238+
return this.location.getLineNumber();
235239
}
236240

237241
@Override
238242
public int getColumnNumber() {
239-
return location.getColumnNumber();
243+
return this.location.getColumnNumber();
240244
}
241245
}
242246

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

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -26,15 +26,16 @@
2626
import org.xml.sax.ext.LexicalHandler;
2727

2828
/**
29-
* Abstract base class for SAX {@code XMLReader} implementations. Contains properties as defined in {@link
30-
* XMLReader}, and does not recognize any features.
29+
* Abstract base class for SAX {@code XMLReader} implementations.
30+
* Contains properties as defined in {@link XMLReader}, and does not recognize any features.
3131
*
3232
* @author Arjen Poutsma
33+
* @author Juergen Hoeller
34+
* @since 3.0
3335
* @see #setContentHandler(org.xml.sax.ContentHandler)
3436
* @see #setDTDHandler(org.xml.sax.DTDHandler)
3537
* @see #setEntityResolver(org.xml.sax.EntityResolver)
3638
* @see #setErrorHandler(org.xml.sax.ErrorHandler)
37-
* @since 3.0
3839
*/
3940
abstract class AbstractXMLReader implements XMLReader {
4041

@@ -48,29 +49,25 @@ abstract class AbstractXMLReader implements XMLReader {
4849

4950
private LexicalHandler lexicalHandler;
5051

51-
@Override
52-
public ContentHandler getContentHandler() {
53-
return contentHandler;
54-
}
5552

5653
@Override
5754
public void setContentHandler(ContentHandler contentHandler) {
5855
this.contentHandler = contentHandler;
5956
}
6057

6158
@Override
62-
public void setDTDHandler(DTDHandler dtdHandler) {
63-
this.dtdHandler = dtdHandler;
59+
public ContentHandler getContentHandler() {
60+
return this.contentHandler;
6461
}
6562

6663
@Override
67-
public DTDHandler getDTDHandler() {
68-
return dtdHandler;
64+
public void setDTDHandler(DTDHandler dtdHandler) {
65+
this.dtdHandler = dtdHandler;
6966
}
7067

7168
@Override
72-
public EntityResolver getEntityResolver() {
73-
return entityResolver;
69+
public DTDHandler getDTDHandler() {
70+
return this.dtdHandler;
7471
}
7572

7673
@Override
@@ -79,38 +76,55 @@ public void setEntityResolver(EntityResolver entityResolver) {
7976
}
8077

8178
@Override
82-
public ErrorHandler getErrorHandler() {
83-
return errorHandler;
79+
public EntityResolver getEntityResolver() {
80+
return this.entityResolver;
8481
}
8582

8683
@Override
8784
public void setErrorHandler(ErrorHandler errorHandler) {
8885
this.errorHandler = errorHandler;
8986
}
9087

88+
@Override
89+
public ErrorHandler getErrorHandler() {
90+
return this.errorHandler;
91+
}
92+
9193
protected LexicalHandler getLexicalHandler() {
92-
return lexicalHandler;
94+
return this.lexicalHandler;
9395
}
9496

97+
9598
/**
96-
* Throws a {@code SAXNotRecognizedException} exception.
97-
*
98-
* @throws org.xml.sax.SAXNotRecognizedException
99-
* always
99+
* This implementation throws a {@code SAXNotRecognizedException} exception
100+
* for any feature outside of the "http://xml.org/sax/features/" namespace
101+
* and returns {@code false} for any feature within.
100102
*/
101103
@Override
102104
public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
103-
throw new SAXNotRecognizedException(name);
105+
if (name.startsWith("http://xml.org/sax/features/")) {
106+
return false;
107+
}
108+
else {
109+
throw new SAXNotRecognizedException(name);
110+
}
104111
}
105112

106113
/**
107-
* Throws a {@code SAXNotRecognizedException} exception.
108-
*
109-
* @throws SAXNotRecognizedException always
114+
* This implementation throws a {@code SAXNotRecognizedException} exception
115+
* for any feature outside of the "http://xml.org/sax/features/" namespace
116+
* and accepts a {@code false} value for any feature within.
110117
*/
111118
@Override
112119
public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException {
113-
throw new SAXNotRecognizedException(name);
120+
if (name.startsWith("http://xml.org/sax/features/")) {
121+
if (value) {
122+
throw new SAXNotSupportedException(name);
123+
}
124+
}
125+
else {
126+
throw new SAXNotRecognizedException(name);
127+
}
114128
}
115129

116130
/**
@@ -120,7 +134,7 @@ public void setFeature(String name, boolean value) throws SAXNotRecognizedExcept
120134
@Override
121135
public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
122136
if ("http://xml.org/sax/properties/lexical-handler".equals(name)) {
123-
return lexicalHandler;
137+
return this.lexicalHandler;
124138
}
125139
else {
126140
throw new SAXNotRecognizedException(name);
@@ -134,10 +148,11 @@ public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotS
134148
@Override
135149
public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException {
136150
if ("http://xml.org/sax/properties/lexical-handler".equals(name)) {
137-
lexicalHandler = (LexicalHandler) value;
151+
this.lexicalHandler = (LexicalHandler) value;
138152
}
139153
else {
140154
throw new SAXNotRecognizedException(name);
141155
}
142156
}
157+
143158
}

0 commit comments

Comments
 (0)