Search in sources :

Example 1 with VerifierFactory

use of org.iso_relax.verifier.VerifierFactory in project nokogiri by sparklemotion.

the class XmlRelaxng method getSchema.

private Schema getSchema(Source source, ThreadContext context) {
    InputStream is;
    VerifierFactory factory = new com.thaiopensource.relaxng.jarv.VerifierFactoryImpl();
    if (source instanceof StreamSource) {
        StreamSource ss = (StreamSource) source;
        is = ss.getInputStream();
    } else {
        //if (this.source instanceof DOMSource)
        DOMSource ds = (DOMSource) source;
        StringWriter xmlAsWriter = new StringWriter();
        StreamResult result = new StreamResult(xmlAsWriter);
        try {
            TransformerFactory.newInstance().newTransformer().transform(ds, result);
        } catch (TransformerConfigurationException ex) {
            throw context.getRuntime().newRuntimeError("Could not parse document: " + ex.getMessage());
        } catch (TransformerException ex) {
            throw context.getRuntime().newRuntimeError("Could not parse document: " + ex.getMessage());
        }
        try {
            is = new ByteArrayInputStream(xmlAsWriter.toString().getBytes("UTF-8"));
        } catch (UnsupportedEncodingException ex) {
            throw context.getRuntime().newRuntimeError("Could not parse document: " + ex.getMessage());
        }
    }
    try {
        return factory.compileSchema(is);
    } catch (VerifierConfigurationException ex) {
        throw context.getRuntime().newRuntimeError("Could not parse document: " + ex.getMessage());
    } catch (SAXException ex) {
        throw context.getRuntime().newRuntimeError("Could not parse document: " + ex.getMessage());
    } catch (IOException ex) {
        throw context.getRuntime().newIOError(ex.getMessage());
    }
}
Also used : VerifierFactory(org.iso_relax.verifier.VerifierFactory) DOMSource(javax.xml.transform.dom.DOMSource) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) VerifierConfigurationException(org.iso_relax.verifier.VerifierConfigurationException) SAXException(org.xml.sax.SAXException) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) TransformerException(javax.xml.transform.TransformerException)

Example 2 with VerifierFactory

use of org.iso_relax.verifier.VerifierFactory in project scheduling by ow2-proactive.

the class ValidationUtil method validate.

/**
 * Validates the job descriptor file against the specified schema.
 *
 * @param jobFile
 *            the job descriptor file
 * @param schemaIs
 *            the job schema
 *
 * @throws JobCreationException
 *             if the job descriptor is invalid
 */
public static void validate(File jobFile, InputStream schemaIs) throws SAXException, IOException, JobCreationException {
    try {
        XMLReader reader = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
        VerifierFactory vfactory = new com.sun.msv.verifier.jarv.TheFactoryImpl();
        Schema schema = vfactory.compileSchema(schemaIs);
        Verifier verifier = schema.newVerifier();
        VerifierHandler handler = verifier.getVerifierHandler();
        ContentHandlerDecorator contentHandlerDecorator = new ContentHandlerDecorator(handler);
        reader.setContentHandler(contentHandlerDecorator);
        ValidationErrorHandler errHandler = new ValidationErrorHandler(contentHandlerDecorator);
        verifier.setErrorHandler(errHandler);
        reader.parse(jobFile.getAbsolutePath());
    } catch (SAXException se) {
        Throwable cause = se.getCause();
        if (cause != null && cause instanceof JobCreationException) {
            // unwrap
            throw (JobCreationException) cause;
        } else {
            throw se;
        }
    } catch (VerifierConfigurationException e) {
        throw new IllegalStateException(e);
    }
}
Also used : VerifierFactory(org.iso_relax.verifier.VerifierFactory) VerifierHandler(org.iso_relax.verifier.VerifierHandler) Schema(org.iso_relax.verifier.Schema) JobCreationException(org.ow2.proactive.scheduler.common.exception.JobCreationException) Verifier(org.iso_relax.verifier.Verifier) VerifierConfigurationException(org.iso_relax.verifier.VerifierConfigurationException) SAXException(org.xml.sax.SAXException) XMLReader(org.xml.sax.XMLReader)

Example 3 with VerifierFactory

use of org.iso_relax.verifier.VerifierFactory in project gocd by gocd.

the class XmlRelaxng method getSchema.

private Schema getSchema(Source source, ThreadContext context) {
    InputStream is = null;
    VerifierFactory factory = new com.thaiopensource.relaxng.jarv.VerifierFactoryImpl();
    if (source instanceof StreamSource) {
        StreamSource ss = (StreamSource) source;
        is = ss.getInputStream();
    } else {
        // if (this.source instanceof DOMSource)
        DOMSource ds = (DOMSource) source;
        StringWriter xmlAsWriter = new StringWriter();
        StreamResult result = new StreamResult(xmlAsWriter);
        try {
            TransformerFactory.newInstance().newTransformer().transform(ds, result);
        } catch (TransformerConfigurationException ex) {
            throw context.getRuntime().newRuntimeError("Could not parse document: " + ex.getMessage());
        } catch (TransformerException ex) {
            throw context.getRuntime().newRuntimeError("Could not parse document: " + ex.getMessage());
        }
        try {
            is = new ByteArrayInputStream(xmlAsWriter.toString().getBytes("UTF-8"));
        } catch (UnsupportedEncodingException ex) {
            throw context.getRuntime().newRuntimeError("Could not parse document: " + ex.getMessage());
        }
    }
    try {
        return factory.compileSchema(is);
    } catch (VerifierConfigurationException ex) {
        throw context.getRuntime().newRuntimeError("Could not parse document: " + ex.getMessage());
    } catch (SAXException ex) {
        throw context.getRuntime().newRuntimeError("Could not parse document: " + ex.getMessage());
    } catch (IOException ex) {
        throw context.getRuntime().newIOError(ex.getMessage());
    }
}
Also used : VerifierFactory(org.iso_relax.verifier.VerifierFactory) DOMSource(javax.xml.transform.dom.DOMSource) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) VerifierConfigurationException(org.iso_relax.verifier.VerifierConfigurationException) SAXException(org.xml.sax.SAXException) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) TransformerException(javax.xml.transform.TransformerException)

Aggregations

VerifierConfigurationException (org.iso_relax.verifier.VerifierConfigurationException)3 VerifierFactory (org.iso_relax.verifier.VerifierFactory)3 SAXException (org.xml.sax.SAXException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 StringWriter (java.io.StringWriter)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)2 TransformerException (javax.xml.transform.TransformerException)2 DOMSource (javax.xml.transform.dom.DOMSource)2 StreamResult (javax.xml.transform.stream.StreamResult)2 StreamSource (javax.xml.transform.stream.StreamSource)2 Schema (org.iso_relax.verifier.Schema)1 Verifier (org.iso_relax.verifier.Verifier)1 VerifierHandler (org.iso_relax.verifier.VerifierHandler)1 JobCreationException (org.ow2.proactive.scheduler.common.exception.JobCreationException)1 XMLReader (org.xml.sax.XMLReader)1