Search in sources :

Example 56 with Schema

use of javax.xml.validation.Schema 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 57 with Schema

use of javax.xml.validation.Schema 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 58 with Schema

use of javax.xml.validation.Schema 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)

Example 59 with Schema

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

the class XML method parseDocumentElement.

/**
     * Example schema locations:
     * http://docs.oasis-open.org/security/saml/v2.0/saml-schema-protocol-2.0.xsd
     * http://docs.oasis-open.org/security/saml/v2.0/saml-schema-assertion-2.0.xsd
     * http://www.w3.org/TR/2002/REC-xmlenc-core-20021210/xenc-schema.xsd
     * http://www.w3.org/TR/2002/REC-xmldsig-core-20020212/xmldsig-core-schema.xsd
     *
     * @param xml
     * @return
     * @throws ParserConfigurationException
     * @throws SAXException
     * @throws IOException
     */
public Element parseDocumentElement(String xml) throws ParserConfigurationException, SAXException, IOException {
    ClasspathResourceResolver resolver = new ClasspathResourceResolver();
    resolver.setResourcePackage(schemaPackageName);
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    schemaFactory.setResourceResolver(resolver);
    Source[] schemaSources = new Source[schemaLocations.size()];
    int i = 0;
    InputStream inStream = null;
    Schema schema;
    try {
        for (String schemaLocation : schemaLocations) {
            inStream = resolver.findResource(schemaLocation);
            //            if(in == null) log.debug("parseDocumentElement - InputStream is NULL");
            //            else           log.debug("parseDocumentElement - InputStream OK");
            schemaSources[i] = new StreamSource(inStream);
            log.debug("parseDocumentElement - schemaSources[" + i + "] :" + schemaSources[i].getSystemId());
            i++;
        }
        //        if(schemaSources!=null) log.debug("parseDocumentElement - schemaSources.length: " + schemaSources.length); 
        //        else                    log.debug("parseDocumentElement - schemaSources is null");
        schema = schemaFactory.newSchema(schemaSources);
    } catch (Exception e) {
        schema = null;
        log.error(e.getMessage());
    } finally {
        if (inStream != null)
            inStream.close();
    }
    //        Validator validator = schema.newValidator();
    //        validator.validate(new StreamSource(new ByteArrayInputStream(xml.getBytes())));
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    // bug #1038 prevent XXE
    factory.setExpandEntityReferences(false);
    // bug #1038 prevent XXE
    factory.setXIncludeAware(false);
    // fix for javax.xml.crypto.dsig.XMLSignatureException: javax.xml.crypto.URIReferenceException: com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverException: Cannot resolve element with ID HostTrustAssertion
    factory.setSchema(schema);
    // ParserConfigurationException
    DocumentBuilder builder = factory.newDocumentBuilder();
    try (ByteArrayInputStream in = new ByteArrayInputStream(xml.getBytes())) {
        // SAXException, IOException
        Element document = builder.parse(in).getDocumentElement();
        return document;
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) Element(org.w3c.dom.Element) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 60 with Schema

use of javax.xml.validation.Schema in project honeycomb by altamiracorp.

the class ConfigurationParser method checkValidConfig.

/**
     * Performs validation on the configuration content supplied by the
     * configuration supplier against the schema document provided by the
     * validation supplier.  Throws Runtime exception if validation fails.
     *
     * @param configSupplier The supplier that provides the configuration to inspect, not null
     * @param schemaSupplier The supplier that provides the schema used to inspect the configuration, not null
     */
private static void checkValidConfig(final InputSupplier<? extends InputStream> configSupplier, final InputSupplier<? extends InputStream> schemaSupplier) {
    try {
        final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        final Schema schema = schemaFactory.newSchema(new StreamSource(schemaSupplier.getInput()));
        final Validator validator = schema.newValidator();
        validator.validate(new StreamSource(configSupplier.getInput()));
    } catch (Exception e) {
        logger.error("Unable to validate honeycomb configuration.", e);
        throw new RuntimeException("Exception while validating honeycomb configuration.", e);
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) Validator(javax.xml.validation.Validator) XPathExpressionException(javax.xml.xpath.XPathExpressionException)

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