Created
June 26, 2014 15:01
-
-
Save ilm-informatique/57ba6c76bf332c3409e1 to your computer and use it in GitHub Desktop.
XMLReaders patch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Index: core/src/java/org/jdom2/input/SAXBuilder.java | |
=================================================================== | |
--- core/src/java/org/jdom2/input/SAXBuilder.java (revision 1727) | |
+++ core/src/java/org/jdom2/input/SAXBuilder.java (working copy) | |
@@ -224,7 +224,7 @@ | |
/** | |
* Creates a new JAXP-based SAXBuilder. The underlying parser will validate | |
* (using DTD) or not according to the given parameter. If you want Schema | |
- * validation then use SAXBuilder(XMLReaders.XSDVALIDATOR) | |
+ * validation then use SAXBuilder(XMLReaders.getXSDValidating()) | |
* | |
* @see SAXBuilder#SAXBuilder(XMLReaderJDOMFactory, SAXHandlerFactory, | |
* JDOMFactory) | |
Index: core/src/java/org/jdom2/input/sax/XMLReaders.java | |
=================================================================== | |
--- core/src/java/org/jdom2/input/sax/XMLReaders.java (revision 1727) | |
+++ core/src/java/org/jdom2/input/sax/XMLReaders.java (working copy) | |
@@ -56,125 +56,163 @@ | |
import javax.xml.XMLConstants; | |
import javax.xml.parsers.ParserConfigurationException; | |
+import javax.xml.parsers.SAXParser; | |
import javax.xml.parsers.SAXParserFactory; | |
import javax.xml.validation.Schema; | |
import javax.xml.validation.SchemaFactory; | |
+import org.jdom2.JDOMException; | |
import org.xml.sax.SAXException; | |
import org.xml.sax.XMLReader; | |
-import org.jdom2.JDOMException; | |
- | |
/** | |
- * An enumeration of {@link XMLReaderJDOMFactory} that allows for a single | |
- * central location to create XMLReaders. The Singletons (members) of this | |
- * enumeration can produce the most common XMLReaders: non-validating, XSD | |
+ * A set of {@link XMLReaderJDOMFactory} that allows for a single central location to create | |
+ * XMLReaders. The members of this class can produce the most common XMLReaders: non-validating, XSD | |
* validating, and DocType validating. | |
* <p> | |
- * See the {@link org.jdom2.input.sax package documentation} for details of how | |
- * to create the SAXParser you desire. | |
+ * See the {@link org.jdom2.input.sax package documentation} for details of how to create the | |
+ * SAXParser you desire. | |
* | |
* @see org.jdom2.input.sax | |
* @author Rolf Lear | |
*/ | |
-public enum XMLReaders implements XMLReaderJDOMFactory { | |
+public class XMLReaders implements XMLReaderJDOMFactory { | |
- /** | |
- * The non-validating singleton | |
- */ | |
- NONVALIDATING(0), | |
+ public static enum Validation { | |
+ NONE(false), DTD(true), SCHEMA(true); | |
- /** | |
- * The DTD-validating Singleton | |
- */ | |
- DTDVALIDATING(1), | |
+ private final boolean validate; | |
- /** | |
- * The XSD-validating Singleton | |
- */ | |
- XSDVALIDATING(2); | |
+ private Validation(boolean validate) { | |
+ this.validate = validate; | |
+ } | |
- /** the actual SAXParserFactory in the respective singletons. */ | |
- private final SAXParserFactory jaxpfactory; | |
- private final Exception failcause; | |
- /** Is this a validating parser */ | |
- private final boolean validates; | |
+ public final boolean isValidating() { | |
+ return this.validate; | |
+ } | |
+ } | |
- /** Private constructor */ | |
- private XMLReaders(int validate) { | |
- SAXParserFactory fac = SAXParserFactory.newInstance(); | |
- boolean val = false; | |
- Exception problem = null; | |
- // All JDOM parsers are namespace aware. | |
- fac.setNamespaceAware(true); | |
- switch (validate) { | |
- case 0: | |
- fac.setValidating(false); | |
- break; | |
- case 1: | |
- fac.setValidating(true); | |
- val = true; | |
- break; | |
- case 2: | |
- fac.setValidating(false); | |
- try { | |
- SchemaFactory sfac = SchemaFactory. | |
- newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); | |
- Schema schema = sfac.newSchema(); | |
- fac.setSchema(schema); | |
- val = true; | |
- } catch (SAXException se) { | |
- // we could not get a validating system, set the fac to null | |
- fac = null; | |
- problem = se; | |
- } catch (IllegalArgumentException iae) { | |
- // this system does not support XSD Validation.... which is true for android! | |
- // we could not get a validating system, set the fac to null | |
- fac = null; | |
- problem = iae; | |
- } catch (UnsupportedOperationException uoe) { | |
- // SAXParserFactory throws this exception when setSchema is called. | |
- // Therefore every factory throws this exception unless it overrides | |
- // setSchema. A popular example is Apache Xerces SAXParserFactoryImpl | |
- // before version 2.7.0. | |
- fac = null; | |
- problem = uoe; | |
- } | |
- break; | |
- } | |
- jaxpfactory = fac; | |
- validates = val; | |
- failcause = problem; | |
- } | |
+ /** | |
+ * The non-validating instance | |
+ */ | |
+ public static final XMLReaders NONVALIDATING = new XMLReaders(Validation.NONE); | |
- /** | |
- * Get a new XMLReader from this JAXP-based {@link XMLReaderJDOMFactory}. | |
- * <p> | |
- * | |
- * @return a new XMLReader instance. | |
- * @throws JDOMException | |
- * if there is a problem creating the XMLReader | |
- */ | |
- @Override | |
- public XMLReader createXMLReader() throws JDOMException { | |
- if (jaxpfactory == null) { | |
- throw new JDOMException("It was not possible to configure a " + | |
- "suitable XMLReader to support " + this, failcause); | |
- } | |
- try { | |
- return jaxpfactory.newSAXParser().getXMLReader(); | |
- } catch (SAXException e) { | |
- throw new JDOMException( | |
- "Unable to create a new XMLReader instance", e); | |
- } catch (ParserConfigurationException e) { | |
- throw new JDOMException( | |
- "Unable to create a new XMLReader instance", e); | |
- } | |
- } | |
+ /** | |
+ * The DTD-validating instance | |
+ */ | |
+ public static final XMLReaders DTDVALIDATING = new XMLReaders(Validation.DTD); | |
- @Override | |
- public boolean isValidating() { | |
- return validates; | |
- } | |
+ private static XMLReaders XSDVALIDATING; | |
+ /** | |
+ * Create a new instance that validates using XSD. I.e. this calls | |
+ * {@link SchemaFactory#newInstance(String)} with {@link XMLConstants#W3C_XML_SCHEMA_NS_URI}. | |
+ * | |
+ * @return a new instance. | |
+ * @throws JDOMException if a new instance couldn't be configured. | |
+ */ | |
+ public static final XMLReaders createXSDValidating() throws JDOMException { | |
+ try { | |
+ final SchemaFactory sfac = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); | |
+ final Schema schema = sfac.newSchema(); | |
+ final XMLReaders res = new XMLReaders(Validation.SCHEMA); | |
+ res.setSchema(schema); | |
+ return res; | |
+ } catch (Exception e) { | |
+ throw new JDOMException("Could not configure", e); | |
+ } | |
+ } | |
+ | |
+ /** | |
+ * Return the instance that validates using XSD, {@link #createXSDValidating() initialising} it | |
+ * if necessary. | |
+ * | |
+ * @return the instance that validates using XSD | |
+ * @throws JDOMException if this was never called and a new instance couldn't be configured, in | |
+ * that case the next call will try again (allowing to change the lookup result, see | |
+ * {@link #createXSDValidating()}). | |
+ * @see #setXSDValidating(XMLReaders) | |
+ */ | |
+ public static final XMLReaders getXSDValidating() throws JDOMException { | |
+ synchronized (XMLReaders.class) { | |
+ if (XSDVALIDATING == null) { | |
+ XSDVALIDATING = createXSDValidating(); | |
+ } | |
+ return XSDVALIDATING; | |
+ } | |
+ } | |
+ | |
+ /** | |
+ * Set the XSD instance. | |
+ * | |
+ * @param instance the instance to be returned by {@link #getXSDValidating()}. | |
+ * @throws JDOMException if the instance wasn't returned by {@link #createXSDValidating()}. | |
+ */ | |
+ public static final void setXSDValidating(final XMLReaders instance) throws JDOMException { | |
+ if (instance.getValidation() != Validation.SCHEMA) | |
+ throw new JDOMException("Not a schema validation : " + instance); | |
+ synchronized (XMLReaders.class) { | |
+ XSDVALIDATING = instance; | |
+ } | |
+ } | |
+ | |
+ /** the actual SAXParserFactory in the respective singletons. */ | |
+ private final SAXParserFactory jaxpfactory; | |
+ private final Validation val; | |
+ | |
+ /** Private constructor */ | |
+ private XMLReaders(final Validation val) { | |
+ final SAXParserFactory fac = SAXParserFactory.newInstance(); | |
+ // All JDOM parsers are namespace aware. | |
+ fac.setNamespaceAware(true); | |
+ fac.setValidating(val == Validation.DTD); | |
+ this.val = val; | |
+ synchronized (this) { | |
+ this.jaxpfactory = fac; | |
+ } | |
+ } | |
+ | |
+ protected final void setSchema(final Schema schema) throws JDOMException { | |
+ if (this.getValidation() != Validation.SCHEMA) | |
+ throw new JDOMException("Validation isn't schema : " + this.getValidation()); | |
+ synchronized (this) { | |
+ this.jaxpfactory.setSchema(schema); | |
+ } | |
+ } | |
+ | |
+ /** | |
+ * Get a new XMLReader from this JAXP-based {@link XMLReaderJDOMFactory}. | |
+ * <p> | |
+ * | |
+ * @return a new XMLReader instance. | |
+ * @throws JDOMException if there is a problem creating the XMLReader | |
+ */ | |
+ @Override | |
+ public final XMLReader createXMLReader() throws JDOMException { | |
+ try { | |
+ final SAXParser newSAXParser; | |
+ synchronized (this) { | |
+ newSAXParser = this.jaxpfactory.newSAXParser(); | |
+ } | |
+ return newSAXParser.getXMLReader(); | |
+ } catch (SAXException e) { | |
+ throw new JDOMException("Unable to create a new XMLReader instance", e); | |
+ } catch (ParserConfigurationException e) { | |
+ throw new JDOMException("Unable to create a new XMLReader instance", e); | |
+ } | |
+ } | |
+ | |
+ public final Validation getValidation() { | |
+ return this.val; | |
+ } | |
+ | |
+ @Override | |
+ public final boolean isValidating() { | |
+ return this.val.isValidating(); | |
+ } | |
+ | |
+ @Override | |
+ public String toString() { | |
+ return this.getClass().getSimpleName() + (!this.isValidating() ? " not validating" : " validating using " + this.getValidation()); | |
+ } | |
} | |
Index: test/src/java/org/jdom2/test/cases/input/TestDOMBuilder.java | |
=================================================================== | |
--- test/src/java/org/jdom2/test/cases/input/TestDOMBuilder.java (revision 1727) | |
+++ test/src/java/org/jdom2/test/cases/input/TestDOMBuilder.java (working copy) | |
@@ -79,7 +79,7 @@ | |
Element domroot = db.build(HelpTestDOMBuilder.getRoot(domdoc)); | |
SAXBuilder sb = new SAXBuilder(xsdvalidate | |
- ? XMLReaders.XSDVALIDATING | |
+ ? XMLReaders.getXSDValidating() | |
: XMLReaders.NONVALIDATING ); | |
sb.setExpandEntities(false); | |
Index: test/src/java/org/jdom2/test/cases/input/TestSAXBuilder.java | |
=================================================================== | |
--- test/src/java/org/jdom2/test/cases/input/TestSAXBuilder.java (revision 1727) | |
+++ test/src/java/org/jdom2/test/cases/input/TestSAXBuilder.java (working copy) | |
@@ -440,12 +440,12 @@ | |
} | |
@Test | |
- public void testGetSetXMLReaderFactory() { | |
+ public void testGetSetXMLReaderFactory() throws JDOMException { | |
SAXBuilder sb = new SAXBuilder(); | |
XMLReaderJDOMFactory xrjf = sb.getXMLReaderFactory(); | |
assertTrue(xrjf == XMLReaders.NONVALIDATING); | |
- sb.setXMLReaderFactory(XMLReaders.XSDVALIDATING); | |
- assertTrue(sb.getXMLReaderFactory() == XMLReaders.XSDVALIDATING); | |
+ sb.setXMLReaderFactory(XMLReaders.getXSDValidating()); | |
+ assertTrue(sb.getXMLReaderFactory() == XMLReaders.getXSDValidating()); | |
sb.setXMLReaderFactory(null); | |
assertTrue(xrjf == XMLReaders.NONVALIDATING); | |
} | |
Index: test/src/java/org/jdom2/test/cases/input/TestSAXComplexSchema.java | |
=================================================================== | |
--- test/src/java/org/jdom2/test/cases/input/TestSAXComplexSchema.java (revision 1727) | |
+++ test/src/java/org/jdom2/test/cases/input/TestSAXComplexSchema.java (working copy) | |
@@ -83,8 +83,8 @@ | |
* Test method for {@link org.jdom.input.SAXBuilder#build(java.io.File)}. | |
*/ | |
@Test | |
- public void testBuildFileNewSAX() throws IOException { | |
- SAXBuilder builder = new SAXBuilder(XMLReaders.XSDVALIDATING); | |
+ public void testBuildFileNewSAX() throws IOException, JDOMException { | |
+ SAXBuilder builder = new SAXBuilder(XMLReaders.getXSDValidating()); | |
URL rurl = FidoFetch.getFido().getURL("/xsdcomplex/input.xml"); | |
Index: test/src/java/org/jdom2/test/cases/input/sax/TestXMLReaderSingletons.java | |
=================================================================== | |
--- test/src/java/org/jdom2/test/cases/input/sax/TestXMLReaderSingletons.java (revision 1727) | |
+++ test/src/java/org/jdom2/test/cases/input/sax/TestXMLReaderSingletons.java (working copy) | |
@@ -50,7 +50,7 @@ | |
@Test | |
public void testXSDValidatingReader() throws JDOMException, IOException { | |
- SAXBuilder builder = new SAXBuilder(XMLReaders.XSDVALIDATING); | |
+ SAXBuilder builder = new SAXBuilder(XMLReaders.getXSDValidating()); | |
assertTrue(builder.isValidating()); | |
Document doc = builder.build(FidoFetch.getFido().getURL("/xsdcomplex/input.xml")); | |
assertEquals("test", doc.getRootElement().getName()); | |
@@ -65,8 +65,8 @@ | |
} | |
@Test | |
- public void testXSDValidatingReaderFails() { | |
- SAXBuilder builder = new SAXBuilder(XMLReaders.XSDVALIDATING); | |
+ public void testXSDValidatingReaderFails() throws JDOMException { | |
+ SAXBuilder builder = new SAXBuilder(XMLReaders.getXSDValidating()); | |
assertTrue(builder.isValidating()); | |
try { | |
builder.build(FidoFetch.getFido().getURL("/DOMBuilder/attributes.xml")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment