Search in sources :

Example 91 with SchemaFactory

use of javax.xml.validation.SchemaFactory in project jqa-core-framework by buschmais.

the class XmlHelper method getSchema.

/**
     * Return a {@link Schema} instance for the given resource.
     * 
     * @param resource
     *            The resource.
     * @return The {@link Schema} instance.
     */
public static Schema getSchema(String resource) {
    Schema schema;
    try {
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        schema = schemaFactory.newSchema(XmlHelper.class.getResource(resource));
    } catch (SAXException e) {
        throw new IllegalStateException("Cannot read rules schema.", e);
    }
    return schema;
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) Schema(javax.xml.validation.Schema) SAXException(org.xml.sax.SAXException)

Example 92 with SchemaFactory

use of javax.xml.validation.SchemaFactory in project simba-os by cegeka.

the class Utils method validateXML.

/**
     * This function attempts to validate an XML string against the specified
     * schema. It will parse the string into a DOM document and validate this
     * document against the schema.
     *
     * @param xmlString  The XML document which should be validated
     * @param schemaName The schema filename which should be used
     * @throws Exception
     */
public static Document validateXML(String xmlString, String schemaName, Boolean... debugMode) throws Exception {
    try {
        String schemaFullPath = "schemas/" + schemaName;
        log.debug("schemaFullPath: " + schemaFullPath);
        ClassLoader classLoader = Utils.class.getClassLoader();
        URL schemaFile = classLoader.getResource(schemaFullPath);
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = schemaFactory.newSchema(schemaFile);
        Validator validator = schema.newValidator();
        XMLErrorHandler errorHandler = new XMLErrorHandler();
        validator.setErrorHandler(errorHandler);
        validator.validate(new StreamSource(new StringReader(xmlString)));
        if (errorHandler.getErrorXML().size() > 0) {
            throw new Error("Invalid XML. See the log");
        }
    } catch (Exception ex) {
        log.error("Error executing validateXML: " + ex.getMessage(), ex);
        throw ex;
    }
    return convertStringToDocument(xmlString);
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader) URL(java.net.URL) Validator(javax.xml.validation.Validator) MarshalException(javax.xml.crypto.MarshalException) TransformerException(javax.xml.transform.TransformerException)

Aggregations

SchemaFactory (javax.xml.validation.SchemaFactory)92 Schema (javax.xml.validation.Schema)72 StreamSource (javax.xml.transform.stream.StreamSource)47 Validator (javax.xml.validation.Validator)39 SAXException (org.xml.sax.SAXException)29 Source (javax.xml.transform.Source)28 IOException (java.io.IOException)20 URL (java.net.URL)18 DOMSource (javax.xml.transform.dom.DOMSource)18 JAXBContext (javax.xml.bind.JAXBContext)17 File (java.io.File)16 InputStream (java.io.InputStream)16 InputSource (org.xml.sax.InputSource)14 DocumentBuilder (javax.xml.parsers.DocumentBuilder)13 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)12 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)12 Unmarshaller (javax.xml.bind.Unmarshaller)11 Test (org.junit.Test)10 Document (org.w3c.dom.Document)10 ByteArrayInputStream (java.io.ByteArrayInputStream)8