--- SAXBuilder.java.orig Mon Jan 22 13:02:19 2001 +++ SAXBuilder.java Sat Jan 27 16:42:43 2001 @@ -104,7 +104,10 @@ */ public class SAXBuilder { - /** Default parser class to use */ + /** + * Default parser class to use. This is used when no other parser + * is given and JAXP isn't available. + */ private static final String DEFAULT_SAX_DRIVER = "org.apache.xerces.parsers.SAXParser"; @@ -158,24 +161,28 @@ /** *

- * This sets validation for the Builder. + * Creates a new SAXBuilder which will attempt to first locate + * a parser via JAXP, then will try to use a set of default + * SAX Drivers. The underlying parser will validate or not + * according to the given parameter. *

* * @param validate boolean indicating if * validation should occur. */ public SAXBuilder(boolean validate) { - this(DEFAULT_SAX_DRIVER, validate); + this.validate = validate; } /** *

- * This creates a SAXBuilder with - * the default SAX driver and no validation. + * Creates a new SAXBuilder which will attempt to first locate + * a parser via JAXP, then will try to use a set of default + * SAX Drivers. The underlying parser will not validate. *

*/ public SAXBuilder() { - this(DEFAULT_SAX_DRIVER, false); + this(false); } /** @@ -250,8 +257,42 @@ Document doc = new Document(null); try { - XMLReader parser = - XMLReaderFactory.createXMLReader(saxDriverClass); + XMLReader parser = null; + if (saxDriverClass != null) { + // the user knows that they want to use a particular class + parser = XMLReaderFactory.createXMLReader(saxDriverClass); + } else { + // try using JAXP... + try { + Class factoryClass = + Class.forName("javax.xml.parsers.SAXParserFactory"); + Method newParserInstance = + factoryClass.getMethod("newInstance", null); + Object factory = newParserInstance.invoke(null, null); + Method setValidating = + factoryClass.getMethod("setValidating", + new Class[]{boolean.class}); + setValidating.invoke(factory, + new Object[]{new Boolean(validate)}); + Method newSAXParser = + factoryClass.getMethod("newSAXParser", null); + Object jaxpParser = newSAXParser.invoke(factory, null); + Class parserClass = jaxpParser.getClass(); + Method getXMLReader = + parserClass.getMethod("getXMLReader", null); + parser = (XMLReader)getXMLReader.invoke(jaxpParser, null); + } catch (ClassNotFoundException cnfe) { + System.out.println(cnfe); + } catch (InvocationTargetException ite) { + System.out.println(ite); + } + } + + // Check to see if we got a parser yet, if not, try to use a + // hard coded default + if (parser == null) { + parser = XMLReaderFactory.createXMLReader(DEFAULT_SAX_DRIVER); + } // Install optional filter if (saxXMLFilter != null) {