From d7fe92d022c390321002f0bbf84a35624aefae3f Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 16 Mar 2016 18:04:02 +0100 Subject: [PATCH] Abstract(Stax)XMLReader recognizes standard features as not supported Issue: SPR-14056 (cherry picked from commit 35eb52e) --- .../util/xml/AbstractStaxXMLReader.java | 34 +++++---- .../util/xml/AbstractXMLReader.java | 71 +++++++++++-------- 2 files changed, 62 insertions(+), 43 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/util/xml/AbstractStaxXMLReader.java b/spring-core/src/main/java/org/springframework/util/xml/AbstractStaxXMLReader.java index 25dc97b299d..77c74d26f68 100644 --- a/spring-core/src/main/java/org/springframework/util/xml/AbstractStaxXMLReader.java +++ b/spring-core/src/main/java/org/springframework/util/xml/AbstractStaxXMLReader.java @@ -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"); * 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. * * @author Arjen Poutsma + * @author Juergen Hoeller * @since 3.0 * @see #setContentHandler(org.xml.sax.ContentHandler) * @see #setDTDHandler(org.xml.sax.DTDHandler) @@ -58,6 +59,7 @@ abstract class AbstractStaxXMLReader extends AbstractXMLReader { private final Map namespaces = new LinkedHashMap(); + @Override public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException { 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; + /** - * Starts the prefix mapping for the given prefix. + * Start the prefix mapping for the given prefix. * @see org.xml.sax.ContentHandler#startPrefixMapping(String, String) */ protected void startPrefixMapping(String prefix, String namespace) throws SAXException { @@ -186,57 +189,58 @@ abstract class AbstractStaxXMLReader extends AbstractXMLReader { if (!StringUtils.hasLength(namespace)) { return; } - if (!namespace.equals(namespaces.get(prefix))) { + if (!namespace.equals(this.namespaces.get(prefix))) { 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) */ protected void endPrefixMapping(String prefix) throws SAXException { if (getContentHandler() != null) { - if (namespaces.containsKey(prefix)) { + if (this.namespaces.containsKey(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 Location */ private static class StaxLocator implements Locator { - private Location location; + private final Location location; - protected StaxLocator(Location location) { + public StaxLocator(Location location) { this.location = location; } @Override public String getPublicId() { - return location.getPublicId(); + return this.location.getPublicId(); } @Override public String getSystemId() { - return location.getSystemId(); + return this.location.getSystemId(); } @Override public int getLineNumber() { - return location.getLineNumber(); + return this.location.getLineNumber(); } @Override public int getColumnNumber() { - return location.getColumnNumber(); + return this.location.getColumnNumber(); } } diff --git a/spring-core/src/main/java/org/springframework/util/xml/AbstractXMLReader.java b/spring-core/src/main/java/org/springframework/util/xml/AbstractXMLReader.java index 7b24a3c1c23..f759ccba37d 100644 --- a/spring-core/src/main/java/org/springframework/util/xml/AbstractXMLReader.java +++ b/spring-core/src/main/java/org/springframework/util/xml/AbstractXMLReader.java @@ -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"); * 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; /** - * Abstract base class for SAX {@code XMLReader} implementations. Contains properties as defined in {@link - * XMLReader}, and does not recognize any features. + * Abstract base class for SAX {@code XMLReader} implementations. + * Contains properties as defined in {@link XMLReader}, and does not recognize any features. * * @author Arjen Poutsma + * @author Juergen Hoeller + * @since 3.0 * @see #setContentHandler(org.xml.sax.ContentHandler) * @see #setDTDHandler(org.xml.sax.DTDHandler) * @see #setEntityResolver(org.xml.sax.EntityResolver) * @see #setErrorHandler(org.xml.sax.ErrorHandler) - * @since 3.0 */ abstract class AbstractXMLReader implements XMLReader { @@ -48,10 +49,6 @@ abstract class AbstractXMLReader implements XMLReader { private LexicalHandler lexicalHandler; - @Override - public ContentHandler getContentHandler() { - return contentHandler; - } @Override public void setContentHandler(ContentHandler contentHandler) { @@ -59,18 +56,18 @@ abstract class AbstractXMLReader implements XMLReader { } @Override - public void setDTDHandler(DTDHandler dtdHandler) { - this.dtdHandler = dtdHandler; + public ContentHandler getContentHandler() { + return this.contentHandler; } @Override - public DTDHandler getDTDHandler() { - return dtdHandler; + public void setDTDHandler(DTDHandler dtdHandler) { + this.dtdHandler = dtdHandler; } @Override - public EntityResolver getEntityResolver() { - return entityResolver; + public DTDHandler getDTDHandler() { + return this.dtdHandler; } @Override @@ -79,8 +76,8 @@ abstract class AbstractXMLReader implements XMLReader { } @Override - public ErrorHandler getErrorHandler() { - return errorHandler; + public EntityResolver getEntityResolver() { + return this.entityResolver; } @Override @@ -88,29 +85,46 @@ abstract class AbstractXMLReader implements XMLReader { this.errorHandler = errorHandler; } + @Override + public ErrorHandler getErrorHandler() { + return this.errorHandler; + } + protected LexicalHandler getLexicalHandler() { - return lexicalHandler; + return this.lexicalHandler; } + /** - * Throws a {@code SAXNotRecognizedException} exception. - * - * @throws org.xml.sax.SAXNotRecognizedException - * always + * This implementation throws a {@code SAXNotRecognizedException} exception + * for any feature outside of the "http://xml.org/sax/features/" namespace + * and returns {@code false} for any feature within. */ @Override 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. - * - * @throws SAXNotRecognizedException always + * This implementation throws a {@code SAXNotRecognizedException} exception + * for any feature outside of the "http://xml.org/sax/features/" namespace + * and accepts a {@code false} value for any feature within. */ @Override 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 public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException { if ("http://xml.org/sax/properties/lexical-handler".equals(name)) { - return lexicalHandler; + return this.lexicalHandler; } else { throw new SAXNotRecognizedException(name); @@ -134,10 +148,11 @@ abstract class AbstractXMLReader implements XMLReader { @Override public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException { if ("http://xml.org/sax/properties/lexical-handler".equals(name)) { - lexicalHandler = (LexicalHandler) value; + this.lexicalHandler = (LexicalHandler) value; } else { throw new SAXNotRecognizedException(name); } } + }