Search in sources :

Example 96 with Schema

use of javax.xml.validation.Schema in project maven-plugins by apache.

the class DefaultChangesSchemaValidator method getSchema.

public Schema getSchema(String schemaPath) throws SAXException, IOException {
    if (this.compiledSchemas.containsKey(schemaPath)) {
        return (Schema) this.compiledSchemas.get(schemaPath);
    }
    Schema schema = this.compileJAXPSchema(schemaPath);
    this.compiledSchemas.put(schemaPath, schema);
    return schema;
}
Also used : Schema(javax.xml.validation.Schema)

Example 97 with Schema

use of javax.xml.validation.Schema in project maven-plugins by apache.

the class DefaultChangesSchemaValidator method validateXmlWithSchema.

public XmlValidationHandler validateXmlWithSchema(File file, String schemaVersion, boolean failOnValidationError) throws SchemaValidatorException {
    Reader reader = null;
    try {
        String schemaPath = CHANGES_SCHEMA_PATH + "changes-" + schemaVersion + ".xsd";
        Schema schema = getSchema(schemaPath);
        Validator validator = schema.newValidator();
        XmlValidationHandler baseHandler = new XmlValidationHandler(failOnValidationError);
        validator.setErrorHandler(baseHandler);
        reader = new XmlStreamReader(file);
        validator.validate(new StreamSource(reader));
        reader.close();
        reader = null;
        return baseHandler;
    } catch (IOException e) {
        throw new SchemaValidatorException("IOException : " + e.getMessage(), e);
    } catch (SAXException e) {
        throw new SchemaValidatorException("SAXException : " + e.getMessage(), e);
    } catch (Exception e) {
        throw new SchemaValidatorException("Exception : " + e.getMessage(), e);
    } finally {
        IOUtil.close(reader);
    }
}
Also used : Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) Reader(java.io.Reader) XmlStreamReader(org.apache.commons.io.input.XmlStreamReader) XmlStreamReader(org.apache.commons.io.input.XmlStreamReader) IOException(java.io.IOException) Validator(javax.xml.validation.Validator) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException)

Example 98 with Schema

use of javax.xml.validation.Schema 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 99 with Schema

use of javax.xml.validation.Schema 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 100 with Schema

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

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