Abstract(Stax)XMLReader recognizes standard features as not supported

Issue: SPR-14056
This commit is contained in:
Juergen Hoeller 2016-03-16 18:04:02 +01:00
parent b1121fba70
commit 35eb52e558
2 changed files with 63 additions and 44 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2016 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.
@ -35,6 +35,7 @@ import org.springframework.util.StringUtils;
* Abstract base class for SAX {@code XMLReader} implementations that use StAX as a basis. * Abstract base class for SAX {@code XMLReader} implementations that use StAX as a basis.
* *
* @author Arjen Poutsma * @author Arjen Poutsma
* @author Juergen Hoeller
* @since 3.0 * @since 3.0
* @see #setContentHandler(org.xml.sax.ContentHandler) * @see #setContentHandler(org.xml.sax.ContentHandler)
* @see #setDTDHandler(org.xml.sax.DTDHandler) * @see #setDTDHandler(org.xml.sax.DTDHandler)
@ -58,6 +59,7 @@ abstract class AbstractStaxXMLReader extends AbstractXMLReader {
private final Map<String, String> namespaces = new LinkedHashMap<String, String>(); private final Map<String, String> namespaces = new LinkedHashMap<String, String>();
@Override @Override
public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException { public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
if (NAMESPACES_FEATURE_NAME.equals(name)) { if (NAMESPACES_FEATURE_NAME.equals(name)) {
@ -170,12 +172,13 @@ abstract class AbstractStaxXMLReader extends AbstractXMLReader {
} }
/** /**
* Template-method that parses the StAX reader passed at construction-time. * Template method that parses the StAX reader passed at construction-time.
*/ */
protected abstract void parseInternal() throws SAXException, XMLStreamException; protected abstract void parseInternal() throws SAXException, XMLStreamException;
/** /**
* Starts the prefix mapping for the given prefix. * Start the prefix mapping for the given prefix.
* @see org.xml.sax.ContentHandler#startPrefixMapping(String, String) * @see org.xml.sax.ContentHandler#startPrefixMapping(String, String)
*/ */
protected void startPrefixMapping(String prefix, String namespace) throws SAXException { protected void startPrefixMapping(String prefix, String namespace) throws SAXException {
@ -186,57 +189,58 @@ abstract class AbstractStaxXMLReader extends AbstractXMLReader {
if (!StringUtils.hasLength(namespace)) { if (!StringUtils.hasLength(namespace)) {
return; return;
} }
if (!namespace.equals(namespaces.get(prefix))) { if (!namespace.equals(this.namespaces.get(prefix))) {
getContentHandler().startPrefixMapping(prefix, namespace); getContentHandler().startPrefixMapping(prefix, namespace);
namespaces.put(prefix, namespace); this.namespaces.put(prefix, namespace);
} }
} }
} }
/** /**
* Ends the prefix mapping for the given prefix. * End the prefix mapping for the given prefix.
* @see org.xml.sax.ContentHandler#endPrefixMapping(String) * @see org.xml.sax.ContentHandler#endPrefixMapping(String)
*/ */
protected void endPrefixMapping(String prefix) throws SAXException { protected void endPrefixMapping(String prefix) throws SAXException {
if (getContentHandler() != null) { if (getContentHandler() != null) {
if (namespaces.containsKey(prefix)) { if (this.namespaces.containsKey(prefix)) {
getContentHandler().endPrefixMapping(prefix); getContentHandler().endPrefixMapping(prefix);
namespaces.remove(prefix); this.namespaces.remove(prefix);
} }
} }
} }
/** /**
* Implementation of the {@code Locator} interface that is based on a StAX {@code Location}. * Implementation of the {@code Locator} interface based on a given StAX {@code Location}.
* @see Locator * @see Locator
* @see Location * @see Location
*/ */
private static class StaxLocator implements Locator { private static class StaxLocator implements Locator {
private Location location; private final Location location;
protected StaxLocator(Location location) { public StaxLocator(Location location) {
this.location = location; this.location = location;
} }
@Override @Override
public String getPublicId() { public String getPublicId() {
return location.getPublicId(); return this.location.getPublicId();
} }
@Override @Override
public String getSystemId() { public String getSystemId() {
return location.getSystemId(); return this.location.getSystemId();
} }
@Override @Override
public int getLineNumber() { public int getLineNumber() {
return location.getLineNumber(); return this.location.getLineNumber();
} }
@Override @Override
public int getColumnNumber() { public int getColumnNumber() {
return location.getColumnNumber(); return this.location.getColumnNumber();
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2016 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.
@ -26,15 +26,16 @@ import org.xml.sax.XMLReader;
import org.xml.sax.ext.LexicalHandler; import org.xml.sax.ext.LexicalHandler;
/** /**
* Abstract base class for SAX {@code XMLReader} implementations. Contains properties as defined in {@link * Abstract base class for SAX {@code XMLReader} implementations.
* XMLReader}, and does not recognize any features. * Contains properties as defined in {@link XMLReader}, and does not recognize any features.
* *
* @author Arjen Poutsma * @author Arjen Poutsma
* @author Juergen Hoeller
* @since 3.0
* @see #setContentHandler(org.xml.sax.ContentHandler) * @see #setContentHandler(org.xml.sax.ContentHandler)
* @see #setDTDHandler(org.xml.sax.DTDHandler) * @see #setDTDHandler(org.xml.sax.DTDHandler)
* @see #setEntityResolver(org.xml.sax.EntityResolver) * @see #setEntityResolver(org.xml.sax.EntityResolver)
* @see #setErrorHandler(org.xml.sax.ErrorHandler) * @see #setErrorHandler(org.xml.sax.ErrorHandler)
* @since 3.0
*/ */
abstract class AbstractXMLReader implements XMLReader { abstract class AbstractXMLReader implements XMLReader {
@ -48,16 +49,17 @@ abstract class AbstractXMLReader implements XMLReader {
private LexicalHandler lexicalHandler; private LexicalHandler lexicalHandler;
@Override
public ContentHandler getContentHandler() {
return contentHandler;
}
@Override @Override
public void setContentHandler(ContentHandler contentHandler) { public void setContentHandler(ContentHandler contentHandler) {
this.contentHandler = contentHandler; this.contentHandler = contentHandler;
} }
@Override
public ContentHandler getContentHandler() {
return this.contentHandler;
}
@Override @Override
public void setDTDHandler(DTDHandler dtdHandler) { public void setDTDHandler(DTDHandler dtdHandler) {
this.dtdHandler = dtdHandler; this.dtdHandler = dtdHandler;
@ -65,12 +67,7 @@ abstract class AbstractXMLReader implements XMLReader {
@Override @Override
public DTDHandler getDTDHandler() { public DTDHandler getDTDHandler() {
return dtdHandler; return this.dtdHandler;
}
@Override
public EntityResolver getEntityResolver() {
return entityResolver;
} }
@Override @Override
@ -79,8 +76,8 @@ abstract class AbstractXMLReader implements XMLReader {
} }
@Override @Override
public ErrorHandler getErrorHandler() { public EntityResolver getEntityResolver() {
return errorHandler; return this.entityResolver;
} }
@Override @Override
@ -88,29 +85,46 @@ abstract class AbstractXMLReader implements XMLReader {
this.errorHandler = errorHandler; this.errorHandler = errorHandler;
} }
protected LexicalHandler getLexicalHandler() { @Override
return lexicalHandler; public ErrorHandler getErrorHandler() {
return this.errorHandler;
} }
protected LexicalHandler getLexicalHandler() {
return this.lexicalHandler;
}
/** /**
* Throws a {@code SAXNotRecognizedException} exception. * This implementation throws a {@code SAXNotRecognizedException} exception
* * for any feature outside of the "http://xml.org/sax/features/" namespace
* @throws org.xml.sax.SAXNotRecognizedException * and returns {@code false} for any feature within.
* always
*/ */
@Override @Override
public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException { public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
throw new SAXNotRecognizedException(name); if (name.startsWith("http://xml.org/sax/features/")) {
return false;
}
else {
throw new SAXNotRecognizedException(name);
}
} }
/** /**
* Throws a {@code SAXNotRecognizedException} exception. * This implementation throws a {@code SAXNotRecognizedException} exception
* * for any feature outside of the "http://xml.org/sax/features/" namespace
* @throws SAXNotRecognizedException always * and accepts a {@code false} value for any feature within.
*/ */
@Override @Override
public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException { public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException {
throw new SAXNotRecognizedException(name); if (name.startsWith("http://xml.org/sax/features/")) {
if (value) {
throw new SAXNotSupportedException(name);
}
}
else {
throw new SAXNotRecognizedException(name);
}
} }
/** /**
@ -120,7 +134,7 @@ abstract class AbstractXMLReader implements XMLReader {
@Override @Override
public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException { public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
if ("http://xml.org/sax/properties/lexical-handler".equals(name)) { if ("http://xml.org/sax/properties/lexical-handler".equals(name)) {
return lexicalHandler; return this.lexicalHandler;
} }
else { else {
throw new SAXNotRecognizedException(name); throw new SAXNotRecognizedException(name);
@ -134,10 +148,11 @@ abstract class AbstractXMLReader implements XMLReader {
@Override @Override
public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException { public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException {
if ("http://xml.org/sax/properties/lexical-handler".equals(name)) { if ("http://xml.org/sax/properties/lexical-handler".equals(name)) {
lexicalHandler = (LexicalHandler) value; this.lexicalHandler = (LexicalHandler) value;
} }
else { else {
throw new SAXNotRecognizedException(name); throw new SAXNotRecognizedException(name);
} }
} }
} }