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:hadoopInstaller.io.XMLDocumentReader.java

public static Document parse(FileObject xmlDocument, FileObject xsdDocument)
        throws InstallerConfigurationParseError {

    try {/*ww w  . ja v a 2 s.  c o  m*/
        // Validate against XML Schema
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
                .newSchema(new StreamSource(xsdDocument.getContent().getInputStream()));
        dbf.setValidating(false);
        dbf.setSchema(schema);
        DocumentBuilder db = dbf.newDocumentBuilder();
        db.setErrorHandler(new ParseErrorHandler());
        return db.parse(xmlDocument.getContent().getInputStream());
    } catch (ParserConfigurationException | SAXException | IOException e) {
        throw new InstallerConfigurationParseError(e, "XMLDocumentReader.ParseError", xmlDocument.getName()); //$NON-NLS-1$
    }
}

From source file:com.ebay.jetstream.event.processor.esper.raw.EsperTestConfigurationValidator.java

public boolean validate(String instance, String schema) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    try {//w w  w .  j  av a 2 s. c o  m
        warnings = 0;
        errors = 0;
        fatalErrors = 0;
        try {
            //Set the validation feature
            factory.setFeature("http://xml.org/sax/features/validation", true);

            //Set the schema validation feature
            factory.setFeature("http://apache.org/xml/features/validation/schema", true);

            //Set schema full grammar checking
            factory.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);

            // If there is an external schema set it
            if (schema != null) {
                SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
                Schema s = sf.newSchema(new StreamSource(schema));
                factory.setSchema(s);
            }
            DocumentBuilder parser = factory.newDocumentBuilder();
            parser.setErrorHandler(this);
            // Parse and validate
            parser.parse(instance);
            // Return true if we made it this far with no errors
            return ((errors == 0) && (fatalErrors == 0));
        } catch (SAXException e) {
            log.error("Could not activate validation features - " + e.getMessage());
        }
    } catch (Exception e) {
        log.error(e.getMessage());
    }
    return false;
}

From source file:com.aionemu.gameserver.dataholders.ReloadableData.java

protected Schema getSchema(String xml_schema) {
    Schema schema = null;/*  w  w  w .  ja  v a2 s . c om*/
    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    try {
        schema = sf.newSchema(new File(xml_schema));
    } catch (SAXException saxe) {
        throw new Error("Error while getting schema", saxe);
    }
    return schema;
}

From source file:com.spoledge.util.xml.XMLValidator.java

public XMLValidator(URL schema) throws SAXException {
    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    init(sf.newSchema(schema));/*from  w  w  w .  ja  v  a2 s .  c  om*/
}

From source file:net.geoprism.gis.sld.SLDValidator.java

public void validate(String sld) {
    try {//from w  w w . j  a  va2 s  .co  m
        SchemaFactory f = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Source xsdS = new StreamSource(xsd);
        Schema schema = f.newSchema(xsdS);
        Validator v = schema.newValidator();

        InputStream stream;
        stream = new ByteArrayInputStream(sld.getBytes("UTF-8"));
        Source s = new StreamSource(stream);
        v.validate(s);
    } catch (Throwable e) {
        log.error(sld, e);
        throw new ProgrammingErrorException("Invalid SLD", e);
    }
}

From source file:eu.scape_project.tool.toolwrapper.data.components_spec.utils.Utils.java

/**
 * Method that creates a {@link Components} instance from the provided
 * component spec file, validating it against component spec XML Schema
 * /*  w  ww.java  2s  .  com*/
 * @param componentSpecFilePath
 *            path to the component spec file
 */
public static Components createComponents(String componentSpecFilePath) {
    Components component = null;
    File schemaFile = null;
    try {
        JAXBContext context = JAXBContext.newInstance(Components.class);
        Unmarshaller unmarshaller = context.createUnmarshaller();

        // copy XML Schema from resources to a temporary location
        schemaFile = File.createTempFile("schema", null);
        FileUtils.copyInputStreamToFile(Utils.class.getResourceAsStream(COMPONENTS_SPEC_FILENAME_IN_RESOURCES),
                schemaFile);
        Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(schemaFile);

        // validate provided toolspec against XML Schema
        unmarshaller.setSchema(schema);

        // unmarshal it
        final FileInputStream stream = new FileInputStream(new File(componentSpecFilePath));
        try {
            component = unmarshaller.unmarshal(new StreamSource(stream), Components.class).getValue();
        } finally {
            stream.close();
        }
    } catch (JAXBException e) {
        log.error("The component spec provided doesn't validate against its schema!", e);
    } catch (SAXException e) {
        log.error("The XML Schema is not valid!", e);
    } catch (IOException e) {
        log.error("An error occured while copying the XML Schema from the resources to a temporary location!",
                e);
    } catch (Exception e) {
        log.error("An error occured!", e);
    } finally {
        if (schemaFile != null) {
            schemaFile.deleteOnExit();
        }
    }
    return component;
}

From source file:com.bluecloud.ioc.parse.KernelXMLParser.java

public KernelXMLParser() throws KernelXMLParserException {
    InputStream is = KernelXMLParser.class.getClassLoader().getResourceAsStream(SHCAME);
    if (null == is) {
        throw new KernelXMLParserException(SHCAME + "?");
    }//from  w w w .  j ava 2 s .  c o m
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    try {
        Schema schema = factory.newSchema(new StreamSource(is));
        validator = schema.newValidator();
        is.close();
    } catch (Exception e) {
        throw new KernelXMLParserException(e);
    }
}

From source file:au.id.hazelwood.xmltvguidebuilder.config.ConfigFactory.java

public ConfigFactory() throws Exception {
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();//  w ww.ja  v a 2s.  com
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    schema = schemaFactory.newSchema(new StreamSource(toInputStream("/config.xsd")));
    stopWatch.stop();
    LOGGER.debug("ConfigFactory created in {}", formatDurationWords(stopWatch.getTime()));
}

From source file:net.nicholaswilliams.java.teamcity.plugin.buildNumber.TestConfigurationDigesterModule.java

@Before
public void setUp() throws SAXException, ParserConfigurationException {
    ConfigurationDigesterModule module = new ConfigurationDigesterModule();
    DigesterLoader loader = DigesterLoader.newLoader(module);

    Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
            .newSchema(this.getResource("shared-build-number-config-1.0.xsd"));

    loader.setNamespaceAware(true);//from  w w w .  j  a  v a 2  s  .c o m
    loader.setSchema(schema);
    loader.setErrorHandler(new ConfigurationErrorHandler());
    loader.setUseContextClassLoader(false);
    loader.setClassLoader(Digester.class.getClassLoader());

    ConvertUtils.register(new JodaXML8601DateTimeConverter(), DateTime.class);

    this.digester = loader.newDigester();
    this.digester.setFeature("http://xml.org/sax/features/validation", true);
    this.digester.setFeature("http://apache.org/xml/features/validation/schema", true);
    this.digester.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);
}

From source file:Main.java

public static void validateXml(Node root, InputStream xsd) throws SAXException, IOException {
    try {/*from   w ww .  ja v a 2  s. c  o  m*/
        Source source = new StreamSource(xsd);
        Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(source);

        Validator validator = schema.newValidator();
        validator.validate(new DOMSource(root));
    } finally {
        closeStream(xsd);
    }
}