Search in sources :

Example 66 with Validator

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

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

use of javax.xml.validation.Validator in project jdk8u_jdk by JetBrains.

the class ValidationWarningsTest method doOneTestIteration.

//One iteration of xml validation test case. It will be called from each
//TestWorker task defined in WarningsTestBase class.
void doOneTestIteration() throws Exception {
    Source src = new StreamSource(new StringReader(xml));
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    SAXSource xsdSource = new SAXSource(new InputSource(new ByteArrayInputStream(xsd.getBytes())));
    Schema schema = schemaFactory.newSchema(xsdSource);
    Validator v = schema.newValidator();
    v.validate(src);
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) ByteArrayInputStream(java.io.ByteArrayInputStream) StreamSource(javax.xml.transform.stream.StreamSource) Schema(javax.xml.validation.Schema) StringReader(java.io.StringReader) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) Validator(javax.xml.validation.Validator)

Example 69 with Validator

use of javax.xml.validation.Validator in project midpoint by Evolveum.

the class TestPrismObjectConstruction method serializeAndValidate.

private void serializeAndValidate(PrismObject<UserType> user, PrismContext prismContext) throws SchemaException, SAXException, IOException {
    String xmlString = prismContext.serializeObjectToString(user, PrismContext.LANG_XML);
    System.out.println("Serialized XML");
    System.out.println(xmlString);
    Document xmlDocument = DOMUtil.parseDocument(xmlString);
    Schema javaxSchema = prismContext.getSchemaRegistry().getJavaxSchema();
    Validator validator = javaxSchema.newValidator();
    validator.setResourceResolver(prismContext.getEntityResolver());
    validator.validate(new DOMSource(xmlDocument));
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Schema(javax.xml.validation.Schema) Document(org.w3c.dom.Document) Validator(javax.xml.validation.Validator)

Example 70 with Validator

use of javax.xml.validation.Validator in project midpoint by Evolveum.

the class TestExtraSchema method testExtraSchema.

/**
	 * Test is extra schema can be loaded to the schema registry and whether the file compliant to that
	 * schema can be validated.
	 */
@Test
public void testExtraSchema() throws SAXException, IOException, SchemaException {
    System.out.println("===[ testExtraSchema ]===");
    Document dataDoc = DOMUtil.parseFile(new File(COMMON_DIR_PATH, "root-foo.xml"));
    PrismContext context = constructPrismContext();
    SchemaRegistryImpl reg = (SchemaRegistryImpl) context.getSchemaRegistry();
    Document extraSchemaDoc = DOMUtil.parseFile(new File(EXTRA_SCHEMA_DIR, "root.xsd"));
    reg.registerSchema(extraSchemaDoc, "file root.xsd");
    reg.initialize();
    Schema javaxSchema = reg.getJavaxSchema();
    assertNotNull(javaxSchema);
    Validator validator = javaxSchema.newValidator();
    DOMResult validationResult = new DOMResult();
    validator.validate(new DOMSource(dataDoc), validationResult);
//		System.out.println("Validation result:");
//		System.out.println(DOMUtil.serializeDOMToString(validationResult.getNode()));
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DOMResult(javax.xml.transform.dom.DOMResult) Schema(javax.xml.validation.Schema) PrismSchema(com.evolveum.midpoint.prism.schema.PrismSchema) Document(org.w3c.dom.Document) SchemaRegistryImpl(com.evolveum.midpoint.prism.schema.SchemaRegistryImpl) File(java.io.File) Validator(javax.xml.validation.Validator) Test(org.testng.annotations.Test)

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