Search in sources :

Example 1 with SchemaFactory

use of javax.xml.validation.SchemaFactory 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 2 with SchemaFactory

use of javax.xml.validation.SchemaFactory 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 3 with SchemaFactory

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

the class ReadXmlTest method createValidator.

@BeforeClass
public static void createValidator() throws Exception {
    InputStream xsd = ReadXmlTest.class.getResourceAsStream("/jaxb/mtwilson-tag-selection/mtwilson-tag-selection.xsd");
    try {
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = factory.newSchema(new StreamSource(xsd));
        validator = schema.newValidator();
    } catch (Exception e) {
        throw e;
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) InputStream(java.io.InputStream) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) BeforeClass(org.junit.BeforeClass)

Example 4 with SchemaFactory

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

the class GenerationTool method load.

/**
     * Load a jOOQ codegen configuration file from an input stream
     */
public static Configuration load(InputStream in) throws IOException {
    // [#1149] If there is no namespace defined, add the default one
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    copyLarge(in, out);
    String xml = out.toString();
    // TODO [#1201] Add better error handling here
    xml = xml.replaceAll("<(\\w+:)?configuration xmlns(:\\w+)?=\"http://www.jooq.org/xsd/jooq-codegen-\\d+\\.\\d+\\.\\d+.xsd\">", "<$1configuration xmlns$2=\"" + Constants.NS_CODEGEN + "\">");
    xml = xml.replace("<configuration>", "<configuration xmlns=\"" + Constants.NS_CODEGEN + "\">");
    try {
        SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        javax.xml.validation.Schema schema = sf.newSchema(GenerationTool.class.getResource("/xsd/" + Constants.XSD_CODEGEN));
        JAXBContext ctx = JAXBContext.newInstance(Configuration.class);
        Unmarshaller unmarshaller = ctx.createUnmarshaller();
        unmarshaller.setSchema(schema);
        unmarshaller.setEventHandler(new ValidationEventHandler() {

            @Override
            public boolean handleEvent(ValidationEvent event) {
                log.warn("Unmarshal warning", event.getMessage());
                return true;
            }
        });
        return (Configuration) unmarshaller.unmarshal(new StringReader(xml));
    } catch (Exception e) {
        throw new GeneratorException("Error while reading XML configuration", e);
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) ValidationEventHandler(javax.xml.bind.ValidationEventHandler) Configuration(org.jooq.util.jaxb.Configuration) JAXBContext(javax.xml.bind.JAXBContext) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StringUtils.defaultString(org.jooq.tools.StringUtils.defaultString) SQLException(java.sql.SQLException) IOException(java.io.IOException) ValidationEvent(javax.xml.bind.ValidationEvent) StringReader(java.io.StringReader) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 5 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)

Aggregations

SchemaFactory (javax.xml.validation.SchemaFactory)256 Schema (javax.xml.validation.Schema)188 StreamSource (javax.xml.transform.stream.StreamSource)123 SAXException (org.xml.sax.SAXException)98 Validator (javax.xml.validation.Validator)88 Source (javax.xml.transform.Source)71 IOException (java.io.IOException)69 URL (java.net.URL)67 File (java.io.File)51 JAXBContext (javax.xml.bind.JAXBContext)49 InputStream (java.io.InputStream)39 Unmarshaller (javax.xml.bind.Unmarshaller)36 JAXBException (javax.xml.bind.JAXBException)34 DOMSource (javax.xml.transform.dom.DOMSource)32 ByteArrayInputStream (java.io.ByteArrayInputStream)28 Test (org.junit.Test)26 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)24 InputSource (org.xml.sax.InputSource)24 StringReader (java.io.StringReader)20 DocumentBuilder (javax.xml.parsers.DocumentBuilder)19