Example usage for javax.xml XMLConstants W3C_XML_SCHEMA_NS_URI

List of usage examples for javax.xml XMLConstants W3C_XML_SCHEMA_NS_URI

Introduction

In this page you can find the example usage for javax.xml XMLConstants W3C_XML_SCHEMA_NS_URI.

Prototype

String W3C_XML_SCHEMA_NS_URI

To view the source code for javax.xml XMLConstants W3C_XML_SCHEMA_NS_URI.

Click Source Link

Document

W3C XML Schema Namespace URI.

Usage

From source file:org.kuali.rice.core.impl.impex.xml.XmlIngesterServiceImpl.java

private static void validate(final XmlDoc xmlDoc, EntityResolver resolver)
        throws ParserConfigurationException, IOException, SAXException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(true);// ww w. ja  v a 2 s.  co  m
    dbf.setNamespaceAware(true);
    dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
            XMLConstants.W3C_XML_SCHEMA_NS_URI);
    DocumentBuilder db = dbf.newDocumentBuilder();
    db.setEntityResolver(resolver);
    db.setErrorHandler(new ErrorHandler() {
        public void warning(SAXParseException se) {
            LOG.warn("Warning parsing xml doc " + xmlDoc, se);
            addProcessingException(xmlDoc, "Warning parsing xml doc " + xmlDoc, se);
        }

        public void error(SAXParseException se) throws SAXException {
            LOG.error("Error parsing xml doc " + xmlDoc, se);
            addProcessingException(xmlDoc, "Error parsing xml doc " + xmlDoc, se);
            throw se;
        }

        public void fatalError(SAXParseException se) throws SAXException {
            LOG.error("Fatal error parsing xml doc " + xmlDoc, se);
            addProcessingException(xmlDoc, "Fatal error parsing xml doc " + xmlDoc, se);
            throw se;
        }
    });
    db.parse(xmlDoc.getStream());
}

From source file:org.kuali.rice.kew.xml.DocumentTypeXmlParserTest.java

private boolean validate(String docName) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(true);//from   w w w.  ja  va  2 s  .c o m
    dbf.setNamespaceAware(true);
    dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
            XMLConstants.W3C_XML_SCHEMA_NS_URI);
    DocumentBuilder db = dbf.newDocumentBuilder();
    db.setEntityResolver(new org.kuali.rice.core.impl.impex.xml.ClassLoaderEntityResolver());
    db.setErrorHandler(new DefaultHandler() {
        @Override
        public void error(SAXParseException e) throws SAXException {
            this.fatalError(e);
        }

        @Override
        public void fatalError(SAXParseException e) throws SAXException {
            super.fatalError(e);
        }
    });
    try {
        db.parse(getClass().getResourceAsStream(docName + ".xml"));
        return true;
    } catch (SAXException se) {
        log.error("Error validating " + docName + ".xml", se);
        return false;
    }
}

From source file:org.lib4j.cli.Options.java

public static Options parse(final URL cliURL, final Class<?> mainClass, final String[] args) {
    try {//ww w. jav a  2 s .c om
        final Unmarshaller unmarshaller = JAXBContext.newInstance(Cli.class).createUnmarshaller();
        unmarshaller
                .setSchema(
                        Options.schema == null
                                ? Options.schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
                                        .newSchema(Thread.currentThread().getContextClassLoader()
                                                .getResource("cli.xsd"))
                                : Options.schema);

        try (final InputStream in = cliURL.openStream()) {
            final JAXBElement<Cli> element = unmarshaller
                    .unmarshal(XMLInputFactory.newInstance().createXMLStreamReader(in), Cli.class);
            return parse(element.getValue(), mainClass, args);
        }
    } catch (final FactoryConfigurationError e) {
        throw new UnsupportedOperationException(e);
    } catch (final IOException e) {
        throw new RuntimeException(e);
    } catch (final JAXBException | SAXException | XMLStreamException e) {
        throw new IllegalArgumentException(e);
    }
}

From source file:org.lib4j.dbcp.DataSources.java

/**
 * Create a <code>BasicDataSource</code> given a dbcp JAXB binding.
 *
 * @param dbcpXml URL of dbcp xml resource.
 * @param driverClassLoader Class loader to be used to load the JDBC driver.
 * @return the <code>BasicDataSource</code> instance.
 * @throws SAXException If a XML validation error occurs.
 * @throws SQLException If a database access error occurs.
 * @throws IOException If an IO exception occurs.
 *//* w w  w .  ja  v  a  2s  .c  o  m*/
public static BasicDataSource createDataSource(final URL dbcpXml, final ClassLoader driverClassLoader)
        throws IOException, SAXException, SQLException {
    try {
        final Unmarshaller unmarshaller = JAXBContext.newInstance(Dbcp.class).createUnmarshaller();
        unmarshaller
                .setSchema(
                        DataSources.schema == null
                                ? DataSources.schema = SchemaFactory
                                        .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
                                        .newSchema(Thread.currentThread().getContextClassLoader()
                                                .getResource("dbcp.xsd"))
                                : DataSources.schema);

        try (final InputStream in = dbcpXml.openStream()) {
            final JAXBElement<Dbcp> element = unmarshaller
                    .unmarshal(XMLInputFactory.newInstance().createXMLStreamReader(in), Dbcp.class);
            return createDataSource(element.getValue(), driverClassLoader);
        }
    } catch (final FactoryConfigurationError e) {
        throw new UnsupportedOperationException(e);
    } catch (final JAXBException | XMLStreamException e) {
        throw new SAXException(e);
    }
}

From source file:org.lilyproject.indexer.model.indexerconf.LilyIndexerConfBuilder.java

private void validate(Document document) throws IndexerConfException {
    try {//from  w  w  w  .jav a2  s .  c om
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        URL url = getClass().getClassLoader()
                .getResource("org/lilyproject/indexer/model/indexerconf/indexerconf.xsd");
        Schema schema = factory.newSchema(url);
        Validator validator = schema.newValidator();
        validator.validate(new DOMSource(document));
    } catch (Exception e) {
        throw new IndexerConfException("Error validating indexer configuration against XML Schema.", e);
    }
}

From source file:org.lilyproject.indexer.model.indexerconf.LilyIndexerConfBuilder.java

public static void validate(InputStream is) throws IndexerConfException {
    MyErrorHandler errorHandler = new MyErrorHandler();

    try {//  w w w  .  ja  v  a 2  s. c  o  m
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        URL url = LilyIndexerConfBuilder.class.getClassLoader()
                .getResource("org/lilyproject/indexer/model/indexerconf/indexerconf.xsd");
        Schema schema = factory.newSchema(url);
        Validator validator = schema.newValidator();
        validator.setErrorHandler(errorHandler);
        validator.validate(new StreamSource(is));
    } catch (Exception e) {
        if (!errorHandler.hasErrors()) {
            throw new IndexerConfException("Error validating indexer configuration.", e);
        } // else it will be reported below
    }

    if (errorHandler.hasErrors()) {
        throw new IndexerConfException("The following errors occurred validating the indexer configuration:\n"
                + errorHandler.getMessage());
    }
}

From source file:org.lsc.configuration.JaxbXmlConfigurationHelper.java

/**
 * Load an XML file to the object/*from   w  w w .  ja  v a 2s .c  o m*/
 * 
 * @param filename
 *            filename to read from
 * @return the completed configuration object
 * @throws FileNotFoundException
 *             thrown if the file can not be accessed (either because of a
 *             misconfiguration or due to a rights issue)
 * @throws LscConfigurationException 
 */
public Lsc getConfiguration(String filename) throws LscConfigurationException {
    LOGGER.debug("Loading XML configuration from: " + filename);
    try {
        Unmarshaller unmarshaller = jaxbc.createUnmarshaller();
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema lscSchema = null;
        try {
            int i = 0;
            Set<URL> urls = new HashSet<URL>();
            urls.addAll(ClasspathHelper.forPackage("org.lsc"));
            if (System.getProperty("LSC.PLUGINS.PACKAGEPATH") != null) {
                String[] pathElements = System.getProperty("LSC.PLUGINS.PACKAGEPATH")
                        .split(System.getProperty("path.separator"));
                for (String pathElement : pathElements) {
                    urls.addAll(ClasspathHelper.forPackage(pathElement));
                }
            }
            Reflections reflections = new Reflections(new ConfigurationBuilder().addUrls(urls)
                    .setScanners(new ResourcesScanner(), new SubTypesScanner()));

            Set<String> xsdFiles = reflections.getResources(Pattern.compile(".*\\.xsd"));
            Source[] schemasSource = new Source[xsdFiles.size()];
            List<String> xsdFilesList = new ArrayList<String>(xsdFiles);
            Collections.reverse(xsdFilesList);
            for (String schemaFile : xsdFilesList) {
                LOGGER.debug("Importing XML schema file: " + schemaFile);
                InputStream schemaStream = this.getClass().getClassLoader().getResourceAsStream(schemaFile);
                schemasSource[i++] = new StreamSource(schemaStream);
            }
            lscSchema = schemaFactory.newSchema(schemasSource);
            unmarshaller.setSchema(lscSchema);

        } catch (VerifyError e) {
            throw new LscConfigurationException(e.toString(), e);
        } catch (SAXException e) {
            throw new LscConfigurationException(e);
        }

        return (Lsc) unmarshaller.unmarshal(new File(filename));
    } catch (JAXBException e) {
        throw new LscConfigurationException(e);
    }
}

From source file:org.mifos.framework.components.mifosmenu.MenuParser.java

/**
 * Method to parse xml and return crude menu
 *
 * @return array of crude Menu objects/*from w ww  .  j av  a  2 s. c om*/
 */
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);
    }
}

From source file:org.mule.config.spring.AbstractSchemaValidationTestCase.java

protected void doTest(String config) throws SAXException, IOException {
    try {/*w ww .ja v a 2 s .c  o  m*/
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        schemaFactory.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);
        Schema schema = schemaFactory.newSchema(getSchemasAsSources());
        schema.newValidator().validate(load(config));
    } catch (SAXParseException ex) {
        System.err.println(MessageFormat.format("SAX parsing exception occurs at line {0}, column {1}",
                ex.getLineNumber(), ex.getColumnNumber()));
        throw ex;
    }
}

From source file:org.ojbc.util.xml.XmlUtils.java

public static Document validateInstance(String rootXsdPath, Document docXmlToValidate,
        IEPDFullPathResourceResolver fullPathResolver, List<String> additionalSchemaRelativePaths)
        throws Exception {

    List<String> schemaPaths = new ArrayList<String>();
    schemaPaths.add(rootXsdPath);/* ww  w .  j a v  a2 s  .  com*/
    schemaPaths.addAll(additionalSchemaRelativePaths);

    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    schemaFactory.setResourceResolver(fullPathResolver);

    Source[] sources = new Source[schemaPaths.size()];
    int i = 0;
    for (String path : schemaPaths) {
        sources[i++] = new StreamSource(XmlUtils.class.getClassLoader().getResourceAsStream(path));
    }

    Schema schema = schemaFactory.newSchema(sources);

    Validator v = schema.newValidator();

    try {
        v.validate(new DOMSource(docXmlToValidate));

    } catch (Exception e) {
        try {
            e.printStackTrace();
            System.err.println("FAILED Input Doc: \n");
            XmlUtils.printNode(docXmlToValidate);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        throw e;
    }
    return docXmlToValidate;
}