private static DocumentBuilderFactory createAndGetFactory(Collection<Source> schemaSources) throws SAXException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); factory.setIgnoringComments(true); factory.setIgnoringElementContentWhitespace(true); factory.setSchema(newSchema(schemaSources.toArray(new Source[schemaSources.size()]))); return factory; }
public static DocumentBuilder documentBuilder(Schema schema) throws ParserConfigurationException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); factory.setIgnoringComments(true); factory.setIgnoringElementContentWhitespace(true); factory.setSchema(schema); DocumentBuilder documentBuilder = factory.newDocumentBuilder(); documentBuilder.setErrorHandler(new FatalErrorHandler()); return documentBuilder; }
public RuleTst() { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema; try { schema = schemaFactory.newSchema(RuleTst.class.getResource("/rule-tests_1_0_0.xsd")); dbf.setSchema(schema); dbf.setNamespaceAware(true); DocumentBuilder builder = dbf.newDocumentBuilder(); builder.setErrorHandler(new ErrorHandler() { @Override public void warning(SAXParseException exception) throws SAXException { throw exception; } @Override public void fatalError(SAXParseException exception) throws SAXException { throw exception; } @Override public void error(SAXParseException exception) throws SAXException { throw exception; } }); documentBuilder = builder; } catch (SAXException | ParserConfigurationException e) { throw new RuntimeException(e); } }
/** * Validate the document against a given schema. Error will be written to {@link java.lang.System#err} * * @param xsdFilename schema to validate against. * @param in document to be validated. * @param options to be applied during parsing. * @throws Exception if an error occurs when parsing the document or schema. */ public static void validate(final String xsdFilename, final InputStream in, final ParserOptions options) throws Exception { final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); factory.setSchema(schemaFactory.newSchema(new File(xsdFilename))); factory.setNamespaceAware(true); if (options.xIncludeAware()) { factory.setXIncludeAware(true); factory.setFeature("http://apache.org/xml/features/xinclude/fixup-base-uris", false); } factory.newDocumentBuilder().parse(in); }
private static Document parseFlowBytes(final byte[] flow) throws FlowSerializationException { // create document by parsing proposed flow bytes try { // create validating document builder final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); final Schema schema = schemaFactory.newSchema(FLOW_XSD_RESOURCE); final DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); docFactory.setNamespaceAware(true); docFactory.setSchema(schema); final DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); docBuilder.setErrorHandler(new LoggingXmlParserErrorHandler("Flow Configuration", logger)); // parse flow return (flow == null || flow.length == 0) ? null : docBuilder.parse(new ByteArrayInputStream(flow)); } catch (final SAXException | ParserConfigurationException | IOException ex) { throw new FlowSerializationException(ex); } }
public FingerprintFactory(final StringEncryptor encryptor, final ExtensionManager extensionManager) { this.encryptor = encryptor; this.extensionManager = extensionManager; final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(true); final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); final Schema schema; try { schema = schemaFactory.newSchema(FingerprintFactory.class.getResource(FLOW_CONFIG_XSD)); } catch (final Exception e) { throw new RuntimeException("Failed to parse schema for file flow configuration.", e); } try { documentBuilderFactory.setSchema(schema); flowConfigDocBuilder = documentBuilderFactory.newDocumentBuilder(); flowConfigDocBuilder.setErrorHandler(new LoggingXmlParserErrorHandler("Flow Configuration", logger)); } catch (final Exception e) { throw new RuntimeException("Failed to create document builder for flow configuration.", e); } }
/** * Returns a validating XML parser given an XSD (not DTD!). * * @param schemaFile File wit hXML schema * @return An XML parser in the form of a DocumentBuilder */ public static DocumentBuilder getValidatingXmlParser(File schemaFile) { DocumentBuilder db = null; try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = factory.newSchema(schemaFile); dbf.setSchema(schema); db = dbf.newDocumentBuilder(); db.setErrorHandler(new SAXErrorHandler()); } catch (ParserConfigurationException e) { log.warnf("%s: Unable to create XML parser\n", XMLUtils.class.getName()); log.warn(e); } catch (SAXException e) { log.warnf("%s: XML parsing exception while loading schema %s\n", XMLUtils.class.getName(),schemaFile.getPath()); log.warn(e); } catch(UnsupportedOperationException e) { log.warnf("%s: API error while setting up XML parser. Check your JAXP version\n", XMLUtils.class.getName()); log.warn(e); } return db; }
docFactory.setSchema(flowSchema);
public Element checkBySchema(String fileToCheck, boolean wantThrow) throws FileNotFoundException, SAXException, IOException, ParserConfigurationException, FHIRException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); factory.setValidating(false); factory.setSchema(schema); DocumentBuilder builder = factory.newDocumentBuilder(); MyErrorHandler err = new MyErrorHandler(errors, fileToCheck); builder.setErrorHandler(err); CSFileInputStream f = new CSFileInputStream(new CSFile(fileToCheck)); Document doc = builder.parse(f); if (wantThrow && err.getErrors().size() > 0) throw new FHIRException("File " + fileToCheck + " failed schema validation"); return doc.getDocumentElement(); }
/** * Sets XML Schema for the parser. * * @param schema schema for the parser * @throws XMLPlatformException exception occurred while setting XMLSchema */ @Override public void setXMLSchema(Schema schema) throws XMLPlatformException { documentBuilderFactory.setSchema(schema); }
private DocumentBuilder newDocumentBuilder() { DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); if (schema != null) { builderFactory.setSchema(schema); } try { return builderFactory.newDocumentBuilder(); } catch (ParserConfigurationException e) { throw new RuntimeException(e); } }
private static Schema activateXmlSchemaValidation(DocumentBuilderFactory factory, String schemaUrl) { try { SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = schemaFactory.newSchema(new URL(schemaUrl)); factory.setSchema(schema); return schema; } catch (Exception e) { // some XML parsers may not support attributes in general or especially XML Schema LOGGER.error("Error activating schema validation, possibly you are offline or behind a proxy?", e.getMessage()); return null; } }
InputStream in = new ByteArrayInputStream(assertion.getBytes()); SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = schemaFactory.newSchema(new URL("http://docs.oasis-open.org/security/saml/v2.0/saml-schema-protocol-2.0.xsd")); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); factory.setSchema(schema); Document document = factory.newDocumentBuilder().parse(in);
private void initializeSchemaValidation(DocumentBuilderFactory factory) throws SAXException { if (Boolean.getBoolean(Sejda.PERFORM_SCHEMA_VALIDATION_PROPERTY_NAME)) { SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); factory.setSchema(schemaFactory.newSchema(new Source[] { new StreamSource( Thread.currentThread().getContextClassLoader().getResourceAsStream(DEFAULT_SEJDA_CONFIG)) })); factory.setNamespaceAware(true); } }
private static void setJaxp13Validating(DocumentBuilderFactory factory, Node schema) throws IOException, SAXException { SchemaFactory sf; Source src; sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); src = new StreamSource(schema.newInputStream()); factory.setSchema(sf.newSchema(src)); }
public DescriptorParser() throws Exception { factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); factory.setSchema(DeploymentManager.getDescriptorSchema()); }
public void setValidating(boolean validate) { if (validate) { this.schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); try { this.schema = schemaFactory.newSchema(new StreamSource(getSchemaLocation())); } catch (org.xml.sax.SAXException e) { LOG.log(Level.SEVERE, "SCHEMA_FACTORY_EXCEPTION_MSG"); } this.parserFactory.setSchema(this.schema); } }
private static void setJaxp13Validating(DocumentBuilderFactory factory, Node schema) throws IOException, SAXException { SchemaFactory sf; Source src; sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); src = new StreamSource(schema.createInputStream()); factory.setSchema(sf.newSchema(src)); }
private DocumentBuilder getBuilderForSchema(Schema schema) throws Exception { this.factory = DbfFactory.getFactory(); try { factory.setSchema(schema); } catch (UnsupportedOperationException upe) { return getNonValidatingBuilder(); } DocumentBuilder builder = factory.newDocumentBuilder(); builder.setEntityResolver(FACES_ENTITY_RESOLVER); builder.setErrorHandler(FACES_ERROR_HANDLER); return builder; }
private DocumentBuilder getBuilderForSchema(Schema schema) throws Exception { this.factory = DbfFactory.getFactory(); try { factory.setSchema(schema); } catch (UnsupportedOperationException upe) { return getNonValidatingBuilder(); } DocumentBuilder builder = factory.newDocumentBuilder(); builder.setEntityResolver(FACES_ENTITY_RESOLVER); builder.setErrorHandler(FACES_ERROR_HANDLER); return builder; }