Search in sources :

Example 61 with SchemaFactory

use of javax.xml.validation.SchemaFactory in project jangaroo-tools by CoreMedia.

the class ValidateSchemas method validateExmlSchema.

@Test
public void validateExmlSchema() throws IOException, SAXException, URISyntaxException {
    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:
    factory.newSchema(new StreamSource(getFile("/net/jangaroo/exml/schemas/exml.xsd")));
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) StreamSource(javax.xml.transform.stream.StreamSource) Test(org.junit.Test)

Example 62 with SchemaFactory

use of javax.xml.validation.SchemaFactory in project nokogiri by sparklemotion.

the class XmlSchema method getSchema.

private Schema getSchema(Source source, String currentDir, String scriptFileName) throws SAXException {
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    SchemaResourceResolver resourceResolver = new SchemaResourceResolver(currentDir, scriptFileName, null);
    schemaFactory.setResourceResolver(resourceResolver);
    schemaFactory.setErrorHandler(new IgnoreSchemaErrorsErrorHandler());
    return schemaFactory.newSchema(source);
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) IgnoreSchemaErrorsErrorHandler(nokogiri.internals.IgnoreSchemaErrorsErrorHandler)

Example 63 with SchemaFactory

use of javax.xml.validation.SchemaFactory in project CoreNLP by stanfordnlp.

the class XMLUtils method getValidatingXmlParser.

/**
   * Returns a validating XML parser given an XSD (not DTD!).
   *
   * @param schemaFile File wit hXML schema
   * @return An XML parser in the form of a DocumentBuilder
   */
public static DocumentBuilder getValidatingXmlParser(File schemaFile) {
    DocumentBuilder db = null;
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = factory.newSchema(schemaFile);
        dbf.setSchema(schema);
        db = dbf.newDocumentBuilder();
        db.setErrorHandler(new SAXErrorHandler());
    } catch (ParserConfigurationException e) {
        System.err.printf("%s: Unable to create XML parser\n", XMLUtils.class.getName());
        e.printStackTrace();
    } catch (SAXException e) {
        System.err.printf("%s: XML parsing exception while loading schema %s\n", XMLUtils.class.getName(), schemaFile.getPath());
        e.printStackTrace();
    } catch (UnsupportedOperationException e) {
        System.err.printf("%s: API error while setting up XML parser. Check your JAXP version\n", XMLUtils.class.getName());
        e.printStackTrace();
    }
    return db;
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Schema(javax.xml.validation.Schema) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 64 with SchemaFactory

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

the class ValidateV2RC2SamplesTest method unmarshall.

private Object unmarshall(Reader reader, Class<?> type, String schemaPath) throws SAXException, URISyntaxException {
    try {
        JAXBContext context = JAXBContext.newInstance(type);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        if (schemaPath != null) {
            SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            Schema schema = sf.newSchema(new File(getClass().getResource(schemaPath).toURI()));
            unmarshaller.setSchema(schema);
        }
        return unmarshaller.unmarshal(reader);
    } catch (JAXBException e) {
        throw new RuntimeException("Unable to unmarshall orcid message" + e);
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) Schema(javax.xml.validation.Schema) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) File(java.io.File)

Example 65 with SchemaFactory

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

the class ValidateV2RC2SamplesTest method marshall.

private void marshall(Object object, String path) throws JAXBException, SAXException, URISyntaxException {
    JAXBContext context = JAXBContext.newInstance(object.getClass());
    Marshaller marshaller = context.createMarshaller();
    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = sf.newSchema(new File(getClass().getResource(path).toURI()));
    marshaller.setSchema(schema);
    marshaller.marshal(object, System.out);
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) Marshaller(javax.xml.bind.Marshaller) Schema(javax.xml.validation.Schema) JAXBContext(javax.xml.bind.JAXBContext) File(java.io.File)

Aggregations

SchemaFactory (javax.xml.validation.SchemaFactory)92 Schema (javax.xml.validation.Schema)72 StreamSource (javax.xml.transform.stream.StreamSource)47 Validator (javax.xml.validation.Validator)39 SAXException (org.xml.sax.SAXException)29 Source (javax.xml.transform.Source)28 IOException (java.io.IOException)20 URL (java.net.URL)18 DOMSource (javax.xml.transform.dom.DOMSource)18 JAXBContext (javax.xml.bind.JAXBContext)17 File (java.io.File)16 InputStream (java.io.InputStream)16 InputSource (org.xml.sax.InputSource)14 DocumentBuilder (javax.xml.parsers.DocumentBuilder)13 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)12 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)12 Unmarshaller (javax.xml.bind.Unmarshaller)11 Test (org.junit.Test)10 Document (org.w3c.dom.Document)10 ByteArrayInputStream (java.io.ByteArrayInputStream)8