Search in sources :

Example 81 with Schema

use of javax.xml.validation.Schema in project wildfly by wildfly.

the class StandardConfigsXMLValidationUnitTestCase method parseXml.

private void parseXml(String xmlName) throws ParserConfigurationException, SAXException, IOException {
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    schemaFactory.setErrorHandler(new ErrorHandlerImpl());
    schemaFactory.setResourceResolver(DEFAULT_RESOURCE_RESOLVER);
    Schema schema = schemaFactory.newSchema(SCHEMAS);
    Validator validator = schema.newValidator();
    validator.setErrorHandler(new ErrorHandlerImpl());
    validator.setFeature("http://apache.org/xml/features/validation/schema", true);
    validator.setResourceResolver(DEFAULT_RESOURCE_RESOLVER);
    validator.validate(new StreamSource(getXmlFile(xmlName)));
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) Validator(javax.xml.validation.Validator)

Example 82 with Schema

use of javax.xml.validation.Schema in project wildfly by wildfly.

the class XSDValidationUnitTestCase method validateXsd.

private void validateXsd(final File xsdFile) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder parser = factory.newDocumentBuilder();
    Document document = parser.parse(xsdFile);
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    schemaFactory.setErrorHandler(new ErrorHandlerImpl());
    schemaFactory.setResourceResolver(new XMLResourceResolver());
    Schema schema = schemaFactory.newSchema(resource("schema/XMLSchema.xsd"));
    Validator validator = schema.newValidator();
    validator.validate(new DOMSource(document));
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) XMLResourceResolver(org.jboss.metadata.parser.util.XMLResourceResolver) Schema(javax.xml.validation.Schema) Document(org.w3c.dom.Document) Validator(javax.xml.validation.Validator)

Example 83 with Schema

use of javax.xml.validation.Schema in project Activiti by Activiti.

the class BpmnXMLConverter method validateModel.

public void validateModel(XMLStreamReader xmlStreamReader) throws Exception {
    Schema schema = createSchema();
    Validator validator = schema.newValidator();
    validator.validate(new StAXSource(xmlStreamReader));
}
Also used : Schema(javax.xml.validation.Schema) StAXSource(javax.xml.transform.stax.StAXSource) Validator(javax.xml.validation.Validator)

Example 84 with Schema

use of javax.xml.validation.Schema in project ACS by ACS-Community.

the class XMLValidator method run.

public void run(String[] args) {
    try {
        if (args.length != 2) {
            System.out.println("Incorrect arguments. You need to provide the XML and XSD files.");
            System.exit(3);
        }
        XMLValidator.error = false;
        // define the type of schema - we use W3C:
        String schemaLang = "http://www.w3.org/2001/XMLSchema";
        // get validation driver:
        SchemaFactory factory = SchemaFactory.newInstance(schemaLang);
        // create schema by reading it from an XSD file:
        Schema schema = factory.newSchema(new StreamSource(args[1]));
        Validator validator = schema.newValidator();
        ErrorHandler eh = new XMLErrorHandler(args[0]);
        validator.setErrorHandler(eh);
        // at last perform validation:
        XMLInputFactory xFact = XMLInputFactory.newInstance();
        XMLStreamReader xRead = xFact.createXMLStreamReader(new FileReader(args[0]));
        if (xRead.getVersion() == null) {
            System.err.println(args[0] + ": There is no XML Definition.");
            XMLValidator.error = true;
        } else if (xRead.getCharacterEncodingScheme() == null) {
            System.err.println(args[0] + ": The encoding attribute is not defined in the XML Definition.");
            XMLValidator.error = true;
        } else if (xRead.getCharacterEncodingScheme().compareTo("ISO-8859-1") != 0) {
            System.err.println(args[0] + ": Incorrect encoding type in the XML Definition.");
            XMLValidator.error = true;
        }
        validator.validate(new StreamSource(args[0]));
    } catch (SAXException ex) {
        System.err.println("Fatal Error");
        System.err.println(args[0] + ": " + ex.getMessage());
        XMLValidator.error = true;
    } catch (Exception ex) {
        ex.printStackTrace();
        XMLValidator.error = true;
    }
    if (XMLValidator.error) {
        //System.exit(1); //Error
        //Warning
        System.exit(0);
    } else {
        System.exit(0);
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) ErrorHandler(org.xml.sax.ErrorHandler) XMLStreamReader(javax.xml.stream.XMLStreamReader) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) FileReader(java.io.FileReader) Validator(javax.xml.validation.Validator) XMLInputFactory(javax.xml.stream.XMLInputFactory) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException)

Example 85 with Schema

use of javax.xml.validation.Schema in project oxTrust by GluuFederation.

the class Shibboleth3ConfService method validateMetadata.

/**
	 * @param stream
	 * @throws IOException
	 * @throws SAXException
	 * @throws ParserConfigurationException
         * @return GluuErrorHandler
	 */
public GluuErrorHandler validateMetadata(InputStream stream) throws ParserConfigurationException, SAXException, IOException {
    String idpTemplatesLocation = configurationFactory.getIDPTemplatesLocation();
    // String schemaDir = configurationFactory.DIR + "shibboleth3" + File.separator + "idp" + File.separator + "schema" + File.separator;
    String schemaDir = idpTemplatesLocation + "shibboleth3" + File.separator + "idp" + File.separator + "schema" + File.separator;
    Schema schema = SchemaBuilder.buildSchema(SchemaLanguage.XML, schemaDir);
    return XMLValidator.validateMetadata(stream, schema);
}
Also used : Schema(javax.xml.validation.Schema)

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