Permalink
Browse files

Separate single constructor into two constructors.

One constructor creates the XSD validating factory and the other
creates the two non XSD validating factories. Hopefully this makes
the code easier to read, because there is no magic number which
controls the constructor.
  • Loading branch information...
1 parent 690e795 commit 89894486ca3b5e2258585a6f9eb377784448ccaa @stefanbirkner committed Oct 1, 2012
Showing with 48 additions and 44 deletions.
  1. +48 −44 core/src/java/org/jdom2/input/sax/XMLReaders.java
@@ -82,65 +82,69 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
/**
* The non-validating singleton
*/
- NONVALIDATING(0),
+ NONVALIDATING(false),
/**
* The DTD-validating Singleton
*/
- DTDVALIDATING(1),
+ DTDVALIDATING(true),
/**
* The XSD-validating Singleton
*/
- XSDVALIDATING(2);
+ XSDVALIDATING();
+
+ private static SAXParserFactory factoryWithValidating(boolean validating) {
+ SAXParserFactory fac = SAXParserFactory.newInstance();
+ fac.setNamespaceAware(true); // All JDOM parsers are namespace aware.
+ fac.setValidating(validating);
+ return fac;
+ }
/** the actual SAXParserFactory in the respective singletons. */
private final SAXParserFactory jaxpfactory;
private final Exception failcause;
/** Is this a validating parser */
private final boolean validates;
- /** 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;
+ /** Creates a non schema validating factory */
+ private XMLReaders(boolean validating) {
+ jaxpfactory = factoryWithValidating(validating);
+ failcause = null;
+ validates = validating;
+ }
+
+ /** Creates a schema validating factory */
+ private XMLReaders() {
+ SAXParserFactory fac = factoryWithValidating(false);
+ boolean val;
+ Exception problem;
+ try {
+ SchemaFactory sfac = SchemaFactory.
+ newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
+ Schema schema = sfac.newSchema();
+ fac.setSchema(schema);
+ problem = null;
+ val = true;
+ } catch (SAXException se) {
+ // we could not get a validating system, set the fac to null
+ fac = null;
+ problem = se;
+ val = false;
+ } 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;
+ val = false;
+ } 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;
+ val = false;
}
jaxpfactory = fac;
validates = val;

0 comments on commit 8989448

Please sign in to comment.