SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(true); factory.setNamespaceAware(true); SAXParser parser = factory.newSAXParser(); XMLReader reader = parser.getXMLReader(); reader.setErrorHandler(new SimpleErrorHandler()); reader.parse(new InputSource(new FileReader ("document.xml")));
public XmlConfigurator(SugarConfiguration sugarConfiguration, ClassLoader classLoader) { this.sugarConfiguration = sugarConfiguration; this.classLoader = classLoader; saxParserFactory = SAXParserFactory.newInstance(); saxParserFactory.setNamespaceAware(true); qdox = new QDox(); }
protected final SAXParser createParser(SAXParserFactory parserFactory) throws ParserConfigurationException, SAXException { parserFactory.setNamespaceAware(true); final SAXParser parser = parserFactory.newSAXParser(); final XMLReader reader = parser.getXMLReader(); //reader.setProperty("http://xml.org/sax/properties/lexical-handler", this); //$NON-NLS-1$ // disable DTD validation (bug 63625) try { // be sure validation is "off" or the feature to ignore DTD's will not apply reader.setFeature("http://xml.org/sax/features/validation", false); //$NON-NLS-1$ reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); //$NON-NLS-1$ } catch (SAXNotRecognizedException e) { // not a big deal if the parser does not recognize the features } catch (SAXNotSupportedException e) { // not a big deal if the parser does not support the features } return parser; }
/** * Return the SAXParserFactory we will use, creating one if necessary. */ public SAXParserFactory getFactory() { if (factory == null) { factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(namespaceAware); factory.setXIncludeAware(xincludeAware); factory.setValidating(validating); factory.setSchema(schema); } return (factory); }
factory.setValidating(true); factory.setNamespaceAware(false); try { factory.newSAXParser(); } catch (Exception pce_val) { validating = false; factory.setValidating(false); factory.setNamespaceAware(true); try { factory.newSAXParser(); } catch (Exception pce_nsa) { namespaceaware = false; factory.setValidating(validating); factory.setNamespaceAware(namespaceaware);
@Test public void testNamespaceDeclarations() throws Exception { // Configure a namespace aware parser to ensure namespace // declarations are handled correctly. factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(true); saxParser = factory.newSAXParser(); final TestRunner runner = TestRunners.newTestRunner(new SplitXml()); runner.setProperty(SplitXml.SPLIT_DEPTH, "3"); runner.enqueue(Paths.get("src/test/resources/TestXml/namespace.xml")); runner.run(); runner.assertTransferCount(SplitXml.REL_ORIGINAL, 1); runner.getFlowFilesForRelationship(SplitXml.REL_ORIGINAL).get(0).assertAttributeEquals(FRAGMENT_COUNT.key(), "2"); runner.assertTransferCount(SplitXml.REL_SPLIT, 2); parseFlowFiles(runner.getFlowFilesForRelationship(SplitXml.REL_ORIGINAL)); parseFlowFiles(runner.getFlowFilesForRelationship(SplitXml.REL_SPLIT)); final MockFlowFile split1 = runner.getFlowFilesForRelationship(SplitXml.REL_SPLIT).get(0); split1.assertContentEquals(Paths.get("src/test/resources/TestXml/namespaceSplit1.xml")); final MockFlowFile split2 = runner.getFlowFilesForRelationship(SplitXml.REL_SPLIT).get(1); split2.assertContentEquals(Paths.get("src/test/resources/TestXml/namespaceSplit2.xml")); }
private static SAXParserFactory createParserFactory() throws ParserConfigurationException { if (saxParserFactory == null) { saxParserFactory = FactorySupport.createSaxParserFactory(); saxParserFactory.setNamespaceAware(true); saxParserFactory.setValidating(false); saxParserFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", false); } catch (Exception pce) { saxParserFactory.setFeature("http://xml.org/sax/features/external-general-entities", false); } catch (Exception pce) { saxParserFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false); } catch (Exception pce) {
/** * Returns properly configured (e.g. security features) parser factory * - namespaceAware == true * - securityProcessing == is set based on security processing property, default is true */ public static SAXParserFactory createParserFactory(boolean disableSecureProcessing) throws IllegalStateException { try { SAXParserFactory factory = SAXParserFactory.newInstance(); if (LOGGER.isLoggable(Level.FINE)) { LOGGER.log(Level.FINE, "SAXParserFactory instance: {0}", factory); } factory.setNamespaceAware(true); factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, !isXMLSecurityDisabled(disableSecureProcessing)); return factory; } catch (ParserConfigurationException ex) { LOGGER.log(Level.SEVERE, null, ex); throw new IllegalStateException( ex); } catch (SAXNotRecognizedException ex) { LOGGER.log(Level.SEVERE, null, ex); throw new IllegalStateException( ex); } catch (SAXNotSupportedException ex) { LOGGER.log(Level.SEVERE, null, ex); throw new IllegalStateException( ex); } catch (AbstractMethodError er) { LOGGER.log(Level.SEVERE, null, er); throw new IllegalStateException(Messages.INVALID_JAXP_IMPLEMENTATION.format(), er); } }
((SAXParserFactory) factory).setValidating(((Boolean) sref.getProperty(PARSER_VALIDATING)).booleanValue()); ((SAXParserFactory) factory).setNamespaceAware(((Boolean) sref.getProperty(PARSER_NAMESPACEAWARE)).booleanValue()); } else { if (factory instanceof DocumentBuilderFactory) {
private static SAXParser newSAXParser(boolean namespaceAware, boolean validating, Schema schema1) throws ParserConfigurationException, SAXException { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(validating); factory.setNamespaceAware(namespaceAware); factory.setSchema(schema1); return factory.newSAXParser(); }
SAXParserFactory fac = SAXParserFactory.newInstance(); boolean val = false; 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.
factory.setValidating(true); factory.setNamespaceAware(false); try { factory.newSAXParser(); factory.setValidating(false); factory.setNamespaceAware(true); try { factory.newSAXParser(); factory.setValidating(validating); factory.setNamespaceAware(namespaceaware);
public void setSchemaResource(String schemaResource) { SAXParserFactory saxParserFactory = parser.getSaxParserFactory(); saxParserFactory.setNamespaceAware(true); saxParserFactory.setValidating(true); try { saxParserFactory.setFeature("http://xml.org/sax/features/namespace-prefixes", true); } catch (Exception e) { LOG.unableToSetSchemaResource(e); } this.schemaResource = schemaResource; } }
/** * Returns properly configured (e.g. security features) parser factory * - namespaceAware == true * - securityProcessing == is set based on security processing property, default is true */ public static SAXParserFactory createParserFactory(boolean disableSecureProcessing) throws IllegalStateException { try { SAXParserFactory factory = SAXParserFactory.newInstance(); if (LOGGER.isLoggable(Level.FINE)) { LOGGER.log(Level.FINE, "SAXParserFactory instance: {0}", factory); } factory.setNamespaceAware(true); factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, !isXMLSecurityDisabled(disableSecureProcessing)); return factory; } catch (ParserConfigurationException ex) { LOGGER.log(Level.SEVERE, null, ex); throw new IllegalStateException( ex); } catch (SAXNotRecognizedException ex) { LOGGER.log(Level.SEVERE, null, ex); throw new IllegalStateException( ex); } catch (SAXNotSupportedException ex) { LOGGER.log(Level.SEVERE, null, ex); throw new IllegalStateException( ex); } catch (AbstractMethodError er) { LOGGER.log(Level.SEVERE, null, er); throw new IllegalStateException(Messages.INVALID_JAXP_IMPLEMENTATION.format(), er); } }
@Override public SAXParserFactory get() { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(true); if (!isXmlSecurityDisabled()) { factory = new SecureSaxParserFactory(factory); } return factory; } }
/** * XMLReader instances from this class will be configured to validate using * the supplied Schema instance. * * @param fac * The SAXParserFactory to use for creating XMLReader instances. * @param schema * The Schema to use for validation. */ public AbstractReaderSchemaFactory(final SAXParserFactory fac, final Schema schema) { if (schema == null) { throw new NullPointerException("Cannot create a " + "SchemaXMLReaderFactory with a null schema"); } saxfac = fac; if (saxfac != null) { saxfac.setNamespaceAware(true); saxfac.setValidating(false); saxfac.setSchema(schema); } }
/** * Create a reader if the use of the class did not specify another one. * The reason to not use {@link org.apache.tools.ant.util.JAXPUtils#getXMLReader()} was to * create our own factory with our own options. * @return a default XML parser */ @Override protected XMLReader createDefaultReader() { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(true); factory.setNamespaceAware(true); XMLReader reader = null; try { SAXParser saxParser = factory.newSAXParser(); reader = saxParser.getXMLReader(); } catch (ParserConfigurationException | SAXException e) { throw new BuildException(ERROR_PARSER_CREATION_FAILURE, e); } return reader; }
static SAXParserFactory saxFactory(XmlOptionsBean options) { SAXParserFactory saxFactory = SAXParserFactory.newInstance(); saxFactory.setValidating(false); saxFactory.setNamespaceAware(true); trySetSAXFeature(saxFactory, XMLConstants.FEATURE_SECURE_PROCESSING, true); trySetSAXFeature(saxFactory, XMLBeansConstants.FEATURE_LOAD_DTD_GRAMMAR, options.isLoadDTDGrammar()); trySetSAXFeature(saxFactory, XMLBeansConstants.FEATURE_LOAD_EXTERNAL_DTD, options.isLoadExternalDTD()); return saxFactory; }
public void setSchemaResource(String schemaResource) { SAXParserFactory saxParserFactory = parser.getSaxParserFactory(); saxParserFactory.setNamespaceAware(true); saxParserFactory.setValidating(true); try { saxParserFactory.setFeature("http://xml.org/sax/features/namespace-prefixes", true); } catch (Exception e) { LOG.unableToSetSchemaResource(e); } this.schemaResource = schemaResource; } }
/** * Creates a non-validating, non-namespace-aware {@link XMLReader} using the specified * {@link ContentHandler}. * * <p>If the given {@link ContentHandler} is <code>null</code>, the {@link XMLReader} is * not initialised. * * @param handler The content handler to use. * * @return The requested {@link XMLReader} * * @throws SAXException Should a SAX exception occur * @throws ParserConfigurationException Should a parser config exception occur */ public static XMLReader makeXMLReader(ContentHandler handler) throws SAXException, ParserConfigurationException { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(false); factory.setValidating(false); XMLReader reader = factory.newSAXParser().getXMLReader(); if (handler != null) { reader.setContentHandler(handler); } return reader; }