Search in sources :

Example 31 with SchemaFactory

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

the class XmlSchemaValidationHelper method validateAgainstSchema.

public void validateAgainstSchema(String xml, InputStream 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(new InputSource(new StringReader(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) {
        // ioe.printStackTrace();
        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) InputSource(org.xml.sax.InputSource) 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) StringReader(java.io.StringReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Validator(javax.xml.validation.Validator)

Example 32 with SchemaFactory

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

the class XmlSchemaValidationHelper method validateAgainstSchema.

public void validateAgainstSchema(File xmlFile, 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(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 33 with SchemaFactory

use of javax.xml.validation.SchemaFactory in project voltdb by VoltDB.

the class CatalogPasswordScrambler method getDeployment.

public static DeploymentType getDeployment(File sourceFH) {
    try {
        JAXBContext jc = JAXBContext.newInstance("org.voltdb.compiler.deploymentfile");
        // This schema shot the sheriff.
        SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = sf.newSchema(DependencyPair.class.getResource("compiler/DeploymentFileSchema.xsd"));
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        unmarshaller.setSchema(schema);
        @SuppressWarnings("unchecked") JAXBElement<DeploymentType> result = (JAXBElement<DeploymentType>) unmarshaller.unmarshal(sourceFH);
        DeploymentType deployment = result.getValue();
        return deployment;
    } catch (JAXBException e) {
        // Convert some linked exceptions to more friendly errors.
        if (e.getLinkedException() instanceof java.io.FileNotFoundException) {
            System.err.println(e.getLinkedException().getMessage());
            return null;
        } else if (e.getLinkedException() instanceof org.xml.sax.SAXParseException) {
            System.err.println("Error schema validating deployment.xml file. " + e.getLinkedException().getMessage());
            return null;
        } else {
            throw new RuntimeException(e);
        }
    } catch (SAXException e) {
        System.err.println("Error schema validating deployment.xml file. " + e.getMessage());
        return null;
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) Schema(javax.xml.validation.Schema) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) JAXBElement(javax.xml.bind.JAXBElement) DeploymentType(org.voltdb.compiler.deploymentfile.DeploymentType) SAXException(org.xml.sax.SAXException) Unmarshaller(javax.xml.bind.Unmarshaller) DependencyPair(org.voltdb.DependencyPair)

Example 34 with SchemaFactory

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

the class ExmlValidator method setupSAXParser.

private SAXParser setupSAXParser() throws IOException {
    try {
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        ExmlSchemaResolver exmlSchemaResolver = new ExmlSchemaResolver();
        schemaFactory.setResourceResolver(exmlSchemaResolver);
        List<Source> schemas = new ArrayList<Source>();
        schemas.add(new StreamSource(getClass().getResourceAsStream(Exmlc.EXML_SCHEMA_LOCATION), "exml"));
        schemas.add(new StreamSource(getClass().getResourceAsStream(Exmlc.EXML_UNTYPED_SCHEMA_LOCATION), "untyped"));
        Collection<ExmlSchemaSource> exmlSchemaSources = exmlSchemaSourceByNamespace.values();
        for (ExmlSchemaSource exmlSchemaSource : exmlSchemaSources) {
            schemas.add(exmlSchemaSource.newStreamSource());
        }
        Schema exmlSchema = schemaFactory.newSchema(schemas.toArray(new Source[schemas.size()]));
        final SAXParserFactory saxFactory = SAXParserFactory.newInstance();
        saxFactory.setNamespaceAware(true);
        saxFactory.setSchema(exmlSchema);
        SAXParser saxParser = saxFactory.newSAXParser();
        saxParser.getXMLReader().setEntityResolver(new EntityResolver() {

            @Override
            public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
                //To change body of implemented methods use File | Settings | File Templates.
                return null;
            }
        });
        return saxParser;
    } catch (ParserConfigurationException e) {
        throw new IllegalStateException("A default dom builder should be provided.", e);
    } catch (SAXParseException e) {
        // SAX parser error while parsing EXML schemas: log only, will cause error or warning, depending on configuration:
        logSAXParseException(null, e, true);
        return null;
    } catch (SAXException e) {
        throw new IllegalStateException("SAX parser does not support validation.", e);
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) InputSource(org.xml.sax.InputSource) StreamSource(javax.xml.transform.stream.StreamSource) Schema(javax.xml.validation.Schema) ArrayList(java.util.ArrayList) EntityResolver(org.xml.sax.EntityResolver) IOException(java.io.IOException) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) SAXException(org.xml.sax.SAXException) SAXParseException(org.xml.sax.SAXParseException) SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 35 with SchemaFactory

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

the class ValidateSchemas method validateUntypedSchema.

@Test
public void validateUntypedSchema() 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/untyped.xsd")));
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) StreamSource(javax.xml.transform.stream.StreamSource) Test(org.junit.Test)

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