/** * Create a SAX parser from the JAXP factory. The result can be used to parse XML files. * * <p>See class Javadoc for hints on setting an entity resolver. This parser has its entity * resolver set to the system entity resolver chain. * * @param validate if true, a validating parser is returned * @param namespaceAware if true, a namespace aware parser is returned * @throws FactoryConfigurationError Application developers should never need to directly catch * errors of this type. * @throws SAXException if a parser fulfilling given parameters can not be created * @return XMLReader configured according to passed parameters */ public static XMLReader createXMLReader(boolean validate, boolean namespaceAware) throws SAXException { SAXParserFactory factory; if (!validate && useFastSAXParserFactory) { try { factory = createFastSAXParserFactory(); } catch (ParserConfigurationException ex) { factory = SAXParserFactory.newInstance(); } catch (SAXException ex) { factory = SAXParserFactory.newInstance(); } } else { useFastSAXParserFactory = false; factory = SAXParserFactory.newInstance(); } factory.setValidating(validate); factory.setNamespaceAware(namespaceAware); try { return factory.newSAXParser().getXMLReader(); } catch (ParserConfigurationException ex) { throw new SAXException( "Cannot create parser satisfying configuration parameters", ex); // NOI18N } }
private static SAXParserFactory makeParserFactory(boolean validating, boolean ns) { SAXParserFactory factory = null; // create factory according to javax.xml.parsers.SAXParserFactory property // or platform default (i.e. com.sun...) try { factory = SAXParserFactory.newInstance(); factory.setValidating(validating); factory.setNamespaceAware(ns); } catch (FactoryConfigurationError err) { notifyFactoryErr(err, "javax.xml.parsers.SAXParserFactory"); // NOI18N throw err; } return factory; }