Search in sources :

Example 51 with SchemaFactory

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

the class JaxbCfgProcessor method resolveLocalSchema.

private Schema resolveLocalSchema(String schemaName, String schemaLanguage) {
    URL url = classLoaderService.locateResource(schemaName);
    if (url == null) {
        throw new XsdException("Unable to locate schema [" + schemaName + "] via classpath", schemaName);
    }
    try {
        InputStream schemaStream = url.openStream();
        try {
            StreamSource source = new StreamSource(url.openStream());
            SchemaFactory schemaFactory = SchemaFactory.newInstance(schemaLanguage);
            return schemaFactory.newSchema(source);
        } catch (SAXException e) {
            throw new XsdException("Unable to load schema [" + schemaName + "]", e, schemaName);
        } catch (IOException e) {
            throw new XsdException("Unable to load schema [" + schemaName + "]", e, schemaName);
        } finally {
            try {
                schemaStream.close();
            } catch (IOException e) {
                log.debugf("Problem closing schema stream [%s]", e.toString());
            }
        }
    } catch (IOException e) {
        throw new XsdException("Stream error handling schema url [" + url.toExternalForm() + "]", schemaName);
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) XsdException(org.hibernate.internal.util.xml.XsdException) IOException(java.io.IOException) URL(java.net.URL) SAXException(org.xml.sax.SAXException)

Example 52 with SchemaFactory

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

the class XmlClientConfigBuilderTest method testXSDConfigXML.

private void testXSDConfigXML(String xmlFileName) throws SAXException, IOException {
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    URL schemaResource = XMLConfigBuilderTest.class.getClassLoader().getResource("hazelcast-client-config-3.9.xsd");
    InputStream xmlResource = XMLConfigBuilderTest.class.getClassLoader().getResourceAsStream(xmlFileName);
    Schema schema = factory.newSchema(schemaResource);
    Source source = new StreamSource(xmlResource);
    Validator validator = schema.newValidator();
    try {
        validator.validate(source);
    } catch (SAXException ex) {
        fail(xmlFileName + " is not valid because: " + ex.toString());
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) XMLConfigBuilderTest(com.hazelcast.config.XMLConfigBuilderTest) URL(java.net.URL) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) Validator(javax.xml.validation.Validator) SAXException(org.xml.sax.SAXException)

Example 53 with SchemaFactory

use of javax.xml.validation.SchemaFactory in project languagetool by languagetool-org.

the class XMLValidator method getValidator.

private Validator getValidator(URL xmlSchema) throws SAXException {
    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = sf.newSchema(xmlSchema);
    Validator validator = schema.newValidator();
    validator.setErrorHandler(new ErrorHandler());
    return validator;
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) Schema(javax.xml.validation.Schema) Validator(javax.xml.validation.Validator)

Example 54 with SchemaFactory

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

the class MenuParser method parse.

/**
     * Method to parse xml and return crude menu
     *
     * @return array of crude Menu objects
     */
public static Menu[] parse() throws SystemException {
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        SchemaFactory schfactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        schfactory.setErrorHandler(null);
        Schema schema = schfactory.newSchema(new StreamSource(MifosResourceUtil.getClassPathResourceAsStream(FilePaths.MENUSCHEMA)));
        factory.setNamespaceAware(false);
        factory.setValidating(false);
        factory.setSchema(schema);
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(MifosResourceUtil.getClassPathResourceAsStream(FilePaths.MENUPATH));
        NodeList tabNodeList = document.getElementsByTagName(MenuConstants.TOPMENUTAB);
        Menu[] leftMenus = new Menu[tabNodeList.getLength()];
        for (int i = 0; i < tabNodeList.getLength(); i++) {
            leftMenus[i] = new Menu();
            leftMenus[i].setTopMenuTabName(((Element) tabNodeList.item(i)).getAttribute(MenuConstants.NAME));
            leftMenus[i].setMenuGroups(createMenuGroup(tabNodeList.item(i)));
            String menuHeading = ((Element) tabNodeList.item(i)).getElementsByTagName(MenuConstants.LEFTMENULABEL).item(0).getFirstChild().getTextContent().trim();
            leftMenus[i].setMenuHeading(menuHeading);
        }
        return leftMenus;
    } catch (SAXParseException spe) {
        throw new MenuParseException(spe);
    } catch (SAXException sxe) {
        throw new MenuParseException(sxe);
    } catch (ParserConfigurationException pce) {
        throw new MenuParseException(pce);
    } catch (IOException ioe) {
        throw new MenuParseException(ioe);
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXParseException(org.xml.sax.SAXParseException) MenuParseException(org.mifos.framework.exceptions.MenuParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 55 with SchemaFactory

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

the class TableTagParser method parser.

public Table parser(String file) throws TableTagParseException {
    Table table = null;
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        SchemaFactory schfactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        schfactory.setErrorHandler(null);
        Schema schema = schfactory.newSchema(new StreamSource(MifosResourceUtil.getClassPathResourceAsURIString(FilePaths.CUSTOMTABLETAGXSD)));
        factory.setNamespaceAware(false);
        factory.setValidating(false);
        factory.setSchema(schema);
        DocumentBuilder builder = factory.newDocumentBuilder();
        builder.setErrorHandler(null);
        Document document = builder.parse(MifosResourceUtil.getClassPathResourceAsURIString(file));
        table = createTable(document);
    } catch (Exception e) {
        throw new TableTagParseException(e);
    }
    return table;
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) TableTagParseException(org.mifos.framework.exceptions.TableTagParseException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) Document(org.w3c.dom.Document) TableTagParseException(org.mifos.framework.exceptions.TableTagParseException)

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