Search in sources :

Example 6 with Schema

use of javax.xml.validation.Schema in project head by mifos.

the class ChartOfAccountsConfig method load.

/**
     * Factory method which loads the Chart of Accounts configuration from the
     * given filename. Given XML filename will be validated against
     * {@link FilePaths#CHART_OF_ACCOUNTS_SCHEMA}.
     *
     * @param chartOfAccountsXml
     *            a relative path to the Chart of Accounts configuration file.
     */
public static ChartOfAccountsConfig load(String chartOfAccountsXml) throws ConfigurationException {
    ChartOfAccountsConfig instance = null;
    Document document = null;
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder parser = dbf.newDocumentBuilder();
        if (FilePaths.CHART_OF_ACCOUNTS_DEFAULT.equals(chartOfAccountsXml)) {
            // default chart of accounts
            document = parser.parse(MifosResourceUtil.getClassPathResourceAsStream(chartOfAccountsXml));
        } else {
            // custom chart of accounts
            document = parser.parse(MifosResourceUtil.getFile(chartOfAccountsXml));
        }
        // create a SchemaFactory capable of understanding XML schemas
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        // load an XML schema
        ClassPathResource schemaFileResource = new ClassPathResource(FilePaths.CHART_OF_ACCOUNTS_SCHEMA);
        Source schemaFile = null;
        if (schemaFileResource.exists()) {
            InputStream in = ChartOfAccountsConfig.class.getClassLoader().getResourceAsStream(FilePaths.CHART_OF_ACCOUNTS_SCHEMA);
            schemaFile = new StreamSource(in);
        } else {
            schemaFile = new StreamSource(MifosResourceUtil.getFile(FilePaths.CHART_OF_ACCOUNTS_SCHEMA));
        }
        Schema schema = factory.newSchema(schemaFile);
        // create a Validator instance and validate document
        Validator validator = schema.newValidator();
        validator.validate(new DOMSource(document));
    } catch (IOException e) {
        throw new ConfigurationException(e);
    } catch (SAXException e) {
        throw new ConfigurationException(e);
    } catch (ParserConfigurationException e) {
        throw new ConfigurationException(e);
    }
    instance = new ChartOfAccountsConfig();
    instance.coaDocument = document;
    return instance;
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) Schema(javax.xml.validation.Schema) IOException(java.io.IOException) Document(org.w3c.dom.Document) ClassPathResource(org.springframework.core.io.ClassPathResource) 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) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) ConfigurationException(org.mifos.config.exceptions.ConfigurationException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Validator(javax.xml.validation.Validator)

Example 7 with Schema

use of javax.xml.validation.Schema in project hibernate-orm by hibernate.

the class XmlParserHelper method getSchema.

public Schema getSchema(String schemaResource) throws XmlParsingException {
    Schema schema = SCHEMA_CACHE.get(schemaResource);
    if (schema != null) {
        return schema;
    }
    schema = loadSchema(schemaResource);
    Schema previous = SCHEMA_CACHE.putIfAbsent(schemaResource, schema);
    return previous != null ? previous : schema;
}
Also used : Schema(javax.xml.validation.Schema)

Example 8 with Schema

use of javax.xml.validation.Schema in project hibernate-orm by hibernate.

the class XmlParserHelper method loadSchema.

private Schema loadSchema(String schemaName) throws XmlParsingException {
    Schema schema = null;
    URL schemaUrl = this.getClass().getClassLoader().getResource(schemaName);
    if (schemaUrl == null) {
        return schema;
    }
    SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
    try {
        schema = sf.newSchema(schemaUrl);
    } catch (SAXException e) {
        throw new XmlParsingException("Unable to create schema for " + schemaName + ": " + e.getMessage(), e);
    }
    return schema;
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) Schema(javax.xml.validation.Schema) URL(java.net.URL) SAXException(org.xml.sax.SAXException)

Example 9 with Schema

use of javax.xml.validation.Schema in project hibernate-orm by hibernate.

the class JpaDescriptorParser method loadEntityMappings.

private void loadEntityMappings(Collection<String> mappingFileNames) {
    for (String mappingFile : mappingFileNames) {
        InputStream stream = xmlParserHelper.getInputStreamForResource(mappingFile);
        if (stream == null) {
            continue;
        }
        EntityMappings mapping = null;
        try {
            Schema schema = xmlParserHelper.getSchema(ORM_SCHEMA);
            mapping = xmlParserHelper.getJaxbRoot(stream, EntityMappings.class, schema);
        } catch (XmlParsingException e) {
            context.logMessage(Diagnostic.Kind.WARNING, "Unable to parse " + mappingFile + ": " + e.getMessage());
        }
        if (mapping != null) {
            entityMappings.add(mapping);
        }
        try {
            stream.close();
        } catch (IOException e) {
        // eat it
        }
    }
}
Also used : ObjectInputStream(java.io.ObjectInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) XmlParsingException(org.hibernate.jpamodelgen.util.xml.XmlParsingException) Schema(javax.xml.validation.Schema) EntityMappings(org.hibernate.jpamodelgen.xml.jaxb.EntityMappings) IOException(java.io.IOException)

Example 10 with Schema

use of javax.xml.validation.Schema in project hibernate-orm by hibernate.

the class JpaDescriptorParser method getPersistence.

private Persistence getPersistence() {
    Persistence persistence = null;
    String persistenceXmlLocation = context.getPersistenceXmlLocation();
    InputStream stream = xmlParserHelper.getInputStreamForResource(persistenceXmlLocation);
    if (stream == null) {
        return null;
    }
    try {
        Schema schema = xmlParserHelper.getSchema(PERSISTENCE_SCHEMA);
        persistence = xmlParserHelper.getJaxbRoot(stream, Persistence.class, schema);
    } catch (XmlParsingException e) {
        context.logMessage(Diagnostic.Kind.WARNING, "Unable to parse persistence.xml: " + e.getMessage());
    }
    try {
        stream.close();
    } catch (IOException e) {
    // eat it
    }
    return persistence;
}
Also used : Persistence(org.hibernate.jpamodelgen.xml.jaxb.Persistence) ObjectInputStream(java.io.ObjectInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) XmlParsingException(org.hibernate.jpamodelgen.util.xml.XmlParsingException) Schema(javax.xml.validation.Schema) IOException(java.io.IOException)

Aggregations

Schema (javax.xml.validation.Schema)102 SchemaFactory (javax.xml.validation.SchemaFactory)72 Validator (javax.xml.validation.Validator)51 StreamSource (javax.xml.transform.stream.StreamSource)45 SAXException (org.xml.sax.SAXException)35 Source (javax.xml.transform.Source)29 DOMSource (javax.xml.transform.dom.DOMSource)25 IOException (java.io.IOException)22 JAXBContext (javax.xml.bind.JAXBContext)18 Document (org.w3c.dom.Document)18 InputStream (java.io.InputStream)17 File (java.io.File)15 URL (java.net.URL)15 DocumentBuilder (javax.xml.parsers.DocumentBuilder)14 JAXBException (javax.xml.bind.JAXBException)13 Unmarshaller (javax.xml.bind.Unmarshaller)13 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)13 InputSource (org.xml.sax.InputSource)13 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)12 SAXParseException (org.xml.sax.SAXParseException)9