Search in sources :

Example 76 with Validator

use of javax.xml.validation.Validator in project poi by apache.

the class XSSFExportToXml method isValid.

/**
     * Validate the generated XML against the XML Schema associated with the XSSFMap
     *
     * @param xml the XML to validate
     * @return true, if document is valid
     * @throws SAXException If validating the document fails
     */
private boolean isValid(Document xml) throws SAXException {
    try {
        String language = "http://www.w3.org/2001/XMLSchema";
        SchemaFactory factory = SchemaFactory.newInstance(language);
        Source source = new DOMSource(map.getSchema());
        Schema schema = factory.newSchema(source);
        Validator validator = schema.newValidator();
        validator.validate(new DOMSource(xml));
        //if no exceptions where raised, the document is valid
        return true;
    } catch (IOException e) {
        LOG.log(POILogger.ERROR, "document is not valid", e);
    }
    return false;
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) DOMSource(javax.xml.transform.dom.DOMSource) Schema(javax.xml.validation.Schema) IOException(java.io.IOException) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) Validator(javax.xml.validation.Validator)

Example 77 with Validator

use of javax.xml.validation.Validator in project webservices-axiom by apache.

the class TestValidator method runTest.

protected void runTest() throws Throwable {
    SchemaFactory factory = new XMLSchemaFactory();
    DocumentBuilder builder = dbf.newDocumentBuilder();
    Schema schema = factory.newSchema(new DOMSource(builder.parse(TestValidator.class.getResourceAsStream("ipo.xsd"))));
    Validator validator = schema.newValidator();
    validator.validate(new DOMSource(builder.parse(TestValidator.class.getResourceAsStream("ipo_1.xml"))));
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) XMLSchemaFactory(org.apache.xerces.jaxp.validation.XMLSchemaFactory) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilder(javax.xml.parsers.DocumentBuilder) XMLSchemaFactory(org.apache.xerces.jaxp.validation.XMLSchemaFactory) Schema(javax.xml.validation.Schema) Validator(javax.xml.validation.Validator)

Example 78 with Validator

use of javax.xml.validation.Validator in project webservices-axiom by apache.

the class ValidateSample method validateUsingDOM.

// START SNIPPET: dom
public void validateUsingDOM(InputStream in, URL schemaUrl) throws Exception {
    OMMetaFactory mf = OMAbstractFactory.getMetaFactory(OMAbstractFactory.FEATURE_DOM);
    SOAPModelBuilder builder = OMXMLBuilderFactory.createSOAPModelBuilder(mf, in, "UTF-8");
    SOAPEnvelope envelope = builder.getSOAPEnvelope();
    OMElement bodyContent = envelope.getBody().getFirstElement();
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = schemaFactory.newSchema(schemaUrl);
    Validator validator = schema.newValidator();
    validator.validate(new DOMSource((Element) bodyContent));
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) DOMSource(javax.xml.transform.dom.DOMSource) Schema(javax.xml.validation.Schema) OMElement(org.apache.axiom.om.OMElement) Element(org.w3c.dom.Element) OMElement(org.apache.axiom.om.OMElement) SOAPModelBuilder(org.apache.axiom.soap.SOAPModelBuilder) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) OMMetaFactory(org.apache.axiom.om.OMMetaFactory) Validator(javax.xml.validation.Validator)

Example 79 with Validator

use of javax.xml.validation.Validator 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)

Example 80 with Validator

use of javax.xml.validation.Validator in project ddf by codice.

the class TestSingleSignOn method validateSaml.

private void validateSaml(String xml, SamlSchema schema) throws IOException {
    // Prepare the schema and xml
    String schemaFileName = "saml-schema-" + schema.toString().toLowerCase() + "-2.0.xsd";
    URL schemaURL = AbstractIntegrationTest.class.getClassLoader().getResource(schemaFileName);
    StreamSource streamSource = new StreamSource(new StringReader(xml));
    // If we fail to create a validator we don't want to stop the show, so we just log a warning
    Validator validator = null;
    try {
        validator = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(schemaURL).newValidator();
    } catch (SAXException e) {
        LOGGER.debug("Exception creating validator. ", e);
    }
    // If the xml is invalid, then we want to fail completely
    if (validator != null) {
        try {
            validator.validate(streamSource);
        } catch (SAXException e) {
            LoggingUtils.failWithThrowableStacktrace(e, "Failed to validate SAML: ");
        }
    }
}
Also used : AbstractIntegrationTest(org.codice.ddf.itests.common.AbstractIntegrationTest) StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) URL(java.net.URL) Validator(javax.xml.validation.Validator) SAXException(org.xml.sax.SAXException)

Aggregations

Validator (javax.xml.validation.Validator)80 Schema (javax.xml.validation.Schema)51 SchemaFactory (javax.xml.validation.SchemaFactory)39 StreamSource (javax.xml.transform.stream.StreamSource)38 DOMSource (javax.xml.transform.dom.DOMSource)30 Source (javax.xml.transform.Source)29 Test (org.junit.Test)21 SAXException (org.xml.sax.SAXException)21 IOException (java.io.IOException)17 MarshallingTest (org.orcid.jaxb.model.notification.custom.MarshallingTest)17 Document (org.w3c.dom.Document)13 InputStream (java.io.InputStream)9 URL (java.net.URL)9 DocumentBuilder (javax.xml.parsers.DocumentBuilder)9 Url (org.orcid.jaxb.model.common_v2.Url)8 InputSource (org.xml.sax.InputSource)8 StringReader (java.io.StringReader)7 JAXBSource (javax.xml.bind.util.JAXBSource)7 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)7 SAXParseException (org.xml.sax.SAXParseException)7