Search in sources :

Example 26 with SchemaFactory

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

the class ValidateV2RC4Identifiers method getValidator.

public Validator getValidator(String name) throws SAXException {
    SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
    Schema schema = factory.newSchema(getClass().getResource("/record_2.0_rc4/" + name + "-2.0_rc4.xsd"));
    Validator validator = schema.newValidator();
    return validator;
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) Schema(javax.xml.validation.Schema) Validator(javax.xml.validation.Validator)

Example 27 with SchemaFactory

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

the class ValidateV2RC3SamplesTest 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)

Example 28 with SchemaFactory

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

the class ValidateV2RC4SamplesTest 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)

Example 29 with SchemaFactory

use of javax.xml.validation.SchemaFactory in project OpenClinica by OpenClinica.

the class XmlSchemaValidationHelper method validateAgainstSchema.

public void validateAgainstSchema(File xmlFile, InputStream xsdFile) {
    try {
        // parse an XML document into a DOM tree
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        builderFactory.setNamespaceAware(true);
        DocumentBuilder parser = builderFactory.newDocumentBuilder();
        // parser.isNamespaceAware();
        Document document = parser.parse(xmlFile);
        // create a SchemaFactory capable of understanding WXS schemas
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        // load a WXS schema, represented by a Schema instance
        Source schemaFile = new StreamSource(xsdFile);
        Schema schema = factory.newSchema(schemaFile);
        // create a Validator instance, which can be used to validate an
        // instance document
        Validator validator = schema.newValidator();
        // validate the DOM tree
        validator.validate(new DOMSource(document));
    } catch (FileNotFoundException ex) {
        throw new OpenClinicaSystemException("File was not found", ex.getCause());
    } catch (IOException ioe) {
        throw new OpenClinicaSystemException("IO Exception", ioe.getCause());
    } catch (SAXParseException spe) {
        //spe.printStackTrace();
        throw new OpenClinicaSystemException("Line : " + spe.getLineNumber() + " - " + spe.getMessage(), spe.getCause());
    } catch (SAXException e) {
        throw new OpenClinicaSystemException(e.getMessage(), e.getCause());
    } catch (ParserConfigurationException pce) {
        throw new OpenClinicaSystemException(pce.getMessage(), pce.getCause());
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) StreamSource(javax.xml.transform.stream.StreamSource) Schema(javax.xml.validation.Schema) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Document(org.w3c.dom.Document) OpenClinicaSystemException(org.akaza.openclinica.exception.OpenClinicaSystemException) InputSource(org.xml.sax.InputSource) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXParseException(org.xml.sax.SAXParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Validator(javax.xml.validation.Validator)

Example 30 with SchemaFactory

use of javax.xml.validation.SchemaFactory in project OpenClinica by OpenClinica.

the class XmlSchemaValidationHelper method validateAgainstSchema.

public void validateAgainstSchema(String xml, File xsdFile) {
    try {
        // parse an XML document into a DOM tree
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        builderFactory.setNamespaceAware(true);
        DocumentBuilder parser = builderFactory.newDocumentBuilder();
        Document document = parser.parse(xml);
        // create a SchemaFactory capable of understanding WXS schemas
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        // load a WXS schema, represented by a Schema instance
        Source schemaFile = new StreamSource(xsdFile);
        Schema schema = factory.newSchema(schemaFile);
        // create a Validator instance, which can be used to validate an
        // instance document
        Validator validator = schema.newValidator();
        // validate the DOM tree
        validator.validate(new DOMSource(document));
    } catch (FileNotFoundException ex) {
        throw new OpenClinicaSystemException("File was not found", ex.getCause());
    } catch (IOException ioe) {
        throw new OpenClinicaSystemException("IO Exception", ioe.getCause());
    } catch (SAXParseException spe) {
        //spe.printStackTrace();
        throw new OpenClinicaSystemException("Line : " + spe.getLineNumber() + " - " + spe.getMessage(), spe.getCause());
    } catch (SAXException e) {
        throw new OpenClinicaSystemException(e.getMessage(), e.getCause());
    } catch (ParserConfigurationException pce) {
        throw new OpenClinicaSystemException(pce.getMessage(), pce.getCause());
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) StreamSource(javax.xml.transform.stream.StreamSource) Schema(javax.xml.validation.Schema) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Document(org.w3c.dom.Document) OpenClinicaSystemException(org.akaza.openclinica.exception.OpenClinicaSystemException) InputSource(org.xml.sax.InputSource) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXParseException(org.xml.sax.SAXParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Validator(javax.xml.validation.Validator)

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