Search in sources :

Example 61 with Validator

use of javax.xml.validation.Validator in project ORCID-Source by ORCID.

the class ValidateV2RC4Identifiers method validateSampleXML.

public void validateSampleXML(String name) throws SAXException, IOException {
    Source source = getInputStream("/record_2.0_rc4/samples/" + name + "-2.0_rc4.xml");
    Validator validator = getValidator(name);
    validator.validate(source);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) JAXBSource(javax.xml.bind.util.JAXBSource) Validator(javax.xml.validation.Validator)

Example 62 with Validator

use of javax.xml.validation.Validator in project opennms by OpenNMS.

the class XmlTest method validateXmlString.

protected void validateXmlString(final String xml) throws Exception {
    if (getSchemaFile() == null) {
        LOG.warn("skipping validation, schema file not set");
        return;
    }
    final SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
    final File schemaFile = new File(getSchemaFile());
    LOG.debug("Validating using schema file: {}", schemaFile);
    final Schema schema = schemaFactory.newSchema(schemaFile);
    final SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
    saxParserFactory.setValidating(true);
    saxParserFactory.setNamespaceAware(true);
    saxParserFactory.setSchema(schema);
    assertTrue("make sure our SAX implementation can validate", saxParserFactory.isValidating());
    final Validator validator = schema.newValidator();
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(xml.getBytes());
    final Source source = new StreamSource(inputStream);
    validator.validate(source);
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) File(java.io.File) Validator(javax.xml.validation.Validator) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 63 with Validator

use of javax.xml.validation.Validator in project opennms by OpenNMS.

the class JaxbUtilsTest method testMarshalEvent.

@Test
public void testMarshalEvent() throws Exception {
    final Event e = getEvent();
    final String xml = JaxbUtils.marshal(e);
    assertTrue(xml.contains("JaxbUtilsTest"));
    LOG.debug("event = {}", e);
    LOG.debug("xml = {}", xml);
    final StringWriter sw = new StringWriter();
    JaxbUtils.marshal(e, sw);
    assertEquals(sw.toString(), xml);
    final SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
    final InputStream is = this.getClass().getResourceAsStream("/xsds/event.xsd");
    // if this is null, it's because Eclipse can be confused by "classifier" test dependencies like opennms-model-*-xsds
    // it only works if opennms-model is *not* pulled into eclipse (go figure)
    Assume.assumeNotNull(is);
    LOG.debug("Hooray!  We have an XSD!");
    final Schema schema = factory.newSchema(new StreamSource(is));
    final Validator v = schema.newValidator();
    v.validate(new StreamSource(new StringReader(xml)));
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) StringWriter(java.io.StringWriter) InputStream(java.io.InputStream) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader) Event(org.opennms.netmgt.xml.event.Event) Validator(javax.xml.validation.Validator) Test(org.junit.Test)

Example 64 with Validator

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

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

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