Search in sources :

Example 11 with Schema

use of javax.xml.validation.Schema in project openblocks by mikaelhg.

the class WorkspaceController method validate.

/**
     * Validates the code blocks document against the schema
     * @param document The document to check
     * @throws RuntimeException If the validation failed
     */
private void validate(Document document) {
    try {
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        URL schemaUrl = ClassLoader.getSystemResource("edu/mit/blocks/codeblocks/codeblocks.xsd");
        Schema schema = schemaFactory.newSchema(schemaUrl);
        Validator validator = schema.newValidator();
        validator.validate(new DOMSource(document));
    } catch (MalformedURLException e) {
        throw new RuntimeException(e);
    } catch (SAXException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) DOMSource(javax.xml.transform.dom.DOMSource) MalformedURLException(java.net.MalformedURLException) Schema(javax.xml.validation.Schema) IOException(java.io.IOException) URL(java.net.URL) Validator(javax.xml.validation.Validator) SAXException(org.xml.sax.SAXException)

Example 12 with Schema

use of javax.xml.validation.Schema in project orientdb by orientechnologies.

the class StopSAXParser method parse.

/**
   * Parse the persistence.xml files referenced by the URLs in the collection
   * 
   * @param persistenceXml
   * @return A collection of parsed persistence units.
   * @throws IOException
   * @throws SAXException
   * @throws ParserConfigurationException
   */
public static Collection<? extends PersistenceUnitInfo> parse(URL persistenceXml) {
    InputStream is = null;
    try {
        // Buffer the InputStream so we can mark it, though we'll be in
        // trouble if we have to read more than 8192 characters before finding
        // the schema!
        is = new BufferedInputStream(persistenceXml.openStream());
        JPAVersion jpaVersion = getSchemaVersion(is);
        Schema schema = getSchema(jpaVersion);
        if (schema == null) {
            throw new PersistenceException("Schema is unknown");
        }
        // Get back to the beginning of the stream
        is = new BufferedInputStream(persistenceXml.openStream());
        parserFactory.setNamespaceAware(true);
        int endIndex = persistenceXml.getPath().length() - PERSISTENCE_XML_BASE_NAME.length();
        URL persistenceXmlRoot = new URL("file://" + persistenceXml.getFile().substring(0, endIndex));
        return getPersistenceUnits(is, persistenceXmlRoot, jpaVersion);
    } catch (Exception e) {
        throw new PersistenceException("Something goes wrong while parsing persistence.xml", e);
    } finally {
        if (is != null)
            try {
                is.close();
            } catch (IOException e) {
            // No logging necessary, just consume
            }
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) Schema(javax.xml.validation.Schema) PersistenceException(javax.persistence.PersistenceException) IOException(java.io.IOException) URL(java.net.URL) IOException(java.io.IOException) PersistenceException(javax.persistence.PersistenceException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 13 with Schema

use of javax.xml.validation.Schema in project camel by apache.

the class XAdESSignaturePropertiesTest method validateAgainstSchema.

private void validateAgainstSchema(Document doc) throws Exception {
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Source schema1 = new StreamSource(new File("target/test-classes/org/apache/camel/component/xmlsecurity/xades/XAdES.xsd"));
    Source schema2 = new StreamSource(new File("target/test-classes/org/apache/camel/component/xmlsecurity/xades/xmldsig-core-schema.xsd"));
    Schema schema = factory.newSchema(new Source[] { schema2, schema1 });
    Validator validator = schema.newValidator();
    validator.validate(new DOMSource(doc));
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Schema(javax.xml.validation.Schema) File(java.io.File) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) Validator(javax.xml.validation.Validator)

Example 14 with Schema

use of javax.xml.validation.Schema in project camel by apache.

the class XmlSignerProcessor method getSchemaForSigner.

protected Schema getSchemaForSigner(Message message, ValidatorErrorHandler errorHandler) throws XmlSignatureException, SAXException, IOException {
    Schema schema;
    String schemaResourceUri = getSchemaResourceUri(message);
    if (schemaResourceUri == null) {
        schema = null;
    } else {
        schema = getSchema(message);
    }
    return schema;
}
Also used : Schema(javax.xml.validation.Schema)

Example 15 with Schema

use of javax.xml.validation.Schema in project camel by apache.

the class XmlSignerProcessor method getMessageBodyNode.

protected Node getMessageBodyNode(Message message) throws Exception {
    //NOPMD
    InputStream is = message.getMandatoryBody(InputStream.class);
    Boolean isPlainText = isPlainText(message);
    Node node;
    if (isPlainText != null && isPlainText) {
        node = getTextNode(message, is);
    } else {
        ValidatorErrorHandler errorHandler = new DefaultValidationErrorHandler();
        Schema schema = getSchemaForSigner(message, errorHandler);
        Document doc = parseInput(is, getConfiguration().getDisallowDoctypeDecl(), schema, errorHandler);
        // throws ValidationException
        errorHandler.handleErrors(message.getExchange(), schema, null);
        node = doc.getDocumentElement();
        LOG.debug("Root element of document to be signed: {}", node);
    }
    return node;
}
Also used : InputStream(java.io.InputStream) Node(org.w3c.dom.Node) Schema(javax.xml.validation.Schema) ValidatorErrorHandler(org.apache.camel.processor.validation.ValidatorErrorHandler) DefaultValidationErrorHandler(org.apache.camel.processor.validation.DefaultValidationErrorHandler) Document(org.w3c.dom.Document)

Aggregations

Schema (javax.xml.validation.Schema)102 SchemaFactory (javax.xml.validation.SchemaFactory)72 Validator (javax.xml.validation.Validator)51 StreamSource (javax.xml.transform.stream.StreamSource)45 SAXException (org.xml.sax.SAXException)35 Source (javax.xml.transform.Source)29 DOMSource (javax.xml.transform.dom.DOMSource)25 IOException (java.io.IOException)22 JAXBContext (javax.xml.bind.JAXBContext)18 Document (org.w3c.dom.Document)18 InputStream (java.io.InputStream)17 File (java.io.File)15 URL (java.net.URL)15 DocumentBuilder (javax.xml.parsers.DocumentBuilder)14 JAXBException (javax.xml.bind.JAXBException)13 Unmarshaller (javax.xml.bind.Unmarshaller)13 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)13 InputSource (org.xml.sax.InputSource)13 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)12 SAXParseException (org.xml.sax.SAXParseException)9