Example usage for javax.xml.validation SchemaFactory newInstance

List of usage examples for javax.xml.validation SchemaFactory newInstance

Introduction

In this page you can find the example usage for javax.xml.validation SchemaFactory newInstance.

Prototype

public static SchemaFactory newInstance(String schemaLanguage) 

Source Link

Document

Lookup an implementation of the SchemaFactory that supports the specified schema language and return it.

Usage

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 .  co m
}

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

public void validate(String sld) {
    try {/*www.j  a  v a  2  s .c  o  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
 * /*ww w  . j a va2 s.c  o m*/
 * @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  ww w .ja  v a  2  s .co 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();//from  w  w  w . j a va2s  . c o  m
    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:com.predic8.membrane.core.interceptor.schemavalidation.XMLSchemaValidator.java

@Override
protected List<Validator> createValidators() throws Exception {
    SchemaFactory sf = SchemaFactory.newInstance(Constants.XSD_NS);
    sf.setResourceResolver(resourceResolver.toLSResourceResolver());
    List<Validator> validators = new ArrayList<Validator>();
    log.debug("Creating validator for schema: " + location);
    StreamSource ss = new StreamSource(resourceResolver.resolve(location));
    ss.setSystemId(location);// w  ww.  j ava 2  s  .co  m
    Validator validator = sf.newSchema(ss).newValidator();
    validator.setResourceResolver(resourceResolver.toLSResourceResolver());
    validator.setErrorHandler(new SchemaValidatorErrorHandler());
    validators.add(validator);
    return validators;
}

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);//w  w  w . j a  va  2s.  c om
    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:at.tfr.securefs.module.validation.SchemaValidationModule.java

protected void loadSchema(ModuleConfiguration moduleConfig) {
    try {/*from  w w  w. jav a2  s. c om*/
        String moduleSchemaName = moduleConfig.getProperties().getProperty("schemaName");
        if (StringUtils.isNoneBlank(moduleSchemaName) && !schemaName.equals(moduleSchemaName)) {
            schemaName = moduleSchemaName;
            schema = null;
        }
        if (schema == null) {
            Path schemaPath = configuration.getSchemaPath().resolve(schemaName);
            schema = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema")
                    .newSchema(schemaPath.toFile());
            log.info("loaded schema: " + schemaPath);
        }
    } catch (Exception e) {
        String message = "cannot load schema " + schemaName + " from path: " + configuration.getSchemaPath();
        log.warn(message, e);
    }
}

From source file:Main.java

public static void validateXml(Node root, InputStream xsd) throws SAXException, IOException {
    try {/*from   ww  w.ja  v a  2s  . c  om*/
        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);
    }
}

From source file:arxiv.xml.XMLParser.java

/**
 * Constructs a new XML parser by initializing the JAXB unmarshaller and setting up the XML validation.
 *
 * @throws HarvesterError if there are any problems
 *//*w  w w  .ja v a2  s . co m*/
public XMLParser() {
    try {
        unmarshaller = JAXBContext.newInstance("org.openarchives.oai._2:org.arxiv.oai.arxivraw")
                .createUnmarshaller();
    } catch (JAXBException e) {
        throw new HarvesterError("Error creating JAXB unmarshaller", e);
    }

    ClassLoader classLoader = this.getClass().getClassLoader();
    List<Source> schemaSources = Lists.newArrayList();

    schemaSources.add(new StreamSource(classLoader.getResourceAsStream("OAI-PMH.xsd")));
    schemaSources.add(new StreamSource(classLoader.getResourceAsStream("arXivRaw.xsd")));

    try {
        Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
                .newSchema(schemaSources.toArray(new Source[schemaSources.size()]));
        unmarshaller.setSchema(schema);
    } catch (SAXException e) {
        throw new HarvesterError("Error creating validation schema", e);
    }
}