Example usage for javax.xml.validation Schema newValidator

List of usage examples for javax.xml.validation Schema newValidator

Introduction

In this page you can find the example usage for javax.xml.validation Schema newValidator.

Prototype

public abstract Validator newValidator();

Source Link

Document

Creates a new Validator for this Schema .

Usage

From source file:com.edmunds.etm.common.xml.XmlValidator.java

/**
 * Creates schema factory, opens schema file and initializes schema rulesConfigValidator.
 *
 * @param schemaFileName XSD schema file name.
 * @return validator object./* www  . j  a va2 s . c o m*/
 * @throws org.xml.sax.SAXException when parser error occurs.
 * @throws java.io.IOException      when IO error occurs.
 */
private Validator initValidator(String schemaFileName) throws SAXException, IOException {
    InputStream is = null;
    try {
        SchemaFactory factory = SchemaFactory.newInstance(W3_ORG_XMLSCHEMA);
        is = getClass().getResourceAsStream(schemaFileName);
        Source source = new StreamSource(is);
        Schema schema = factory.newSchema(source);
        return schema.newValidator();
    } finally {
        if (is != null) {
            is.close();
        }
    }
}

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.  j  a va 2s  .  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:ar.com.tadp.xml.rinzo.core.resources.validation.XMLStringValidator.java

private Validator createValidator(StreamSource[] sources) throws Exception {
    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    sf.setFeature("http://apache.org/xml/features/xinclude", true);
    Schema schema = sf.newSchema(sources);
    return schema.newValidator();
}

From source file:com.maxl.java.aips2xml.Aips2Xml.java

static List<MedicalInformations.MedicalInformation> readAipsFile() {
    List<MedicalInformations.MedicalInformation> med_list = null;
    try {/*  w  w w.  j a va2s  .com*/
        JAXBContext context = JAXBContext.newInstance(MedicalInformations.class);

        // Validation
        SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = sf.newSchema(new File(FILE_MEDICAL_INFOS_XSD));
        Validator validator = schema.newValidator();
        validator.setErrorHandler(new MyErrorHandler());

        // Marshaller
        /*
        Marshaller ma = context.createMarshaller();
        ma.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        MedicalInformations medi_infos = new MedicalInformations();
        ma.marshal(medi_infos, System.out);
        */
        // Unmarshaller   
        long startTime = System.currentTimeMillis();
        if (SHOW_LOGS)
            System.out.print("- Unmarshalling Swissmedic xml ... ");

        FileInputStream fis = new FileInputStream(new File(FILE_MEDICAL_INFOS_XML));
        Unmarshaller um = context.createUnmarshaller();
        MedicalInformations med_infos = (MedicalInformations) um.unmarshal(fis);
        med_list = med_infos.getMedicalInformation();

        long stopTime = System.currentTimeMillis();
        if (SHOW_LOGS)
            System.out.println(med_list.size() + " medis in " + (stopTime - startTime) / 1000.0f + " sec");
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JAXBException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    }

    return med_list;
}

From source file:cz.strmik.cmmitool.cmmi.DefaultRatingScalesProvider.java

private void validateScalesDocument(Document document) throws SAXException, IOException {
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Source schemaFile = new StreamSource(getClass().getClassLoader().getResourceAsStream(SCALES_XSD));
    Schema schema = schemaFactory.newSchema(schemaFile);
    Validator validator = schema.newValidator();
    validator.validate(new DOMSource(document));
}

From source file:com.smartitengineering.cms.spi.impl.type.validator.XMLSchemaBasedTypeValidator.java

protected boolean isValid(Document document) throws Exception {
    // create a SchemaFactory capable of understanding WXS schemas
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    final InputStream xsdStream = getClass().getClassLoader().getResourceAsStream(XSD_LOCATION);
    // load a WXS schema, represented by a Schema instance
    Source schemaFile = new StreamSource(xsdStream);
    schemaFile.setSystemId(CONTENT_TYPE_SCHEMA_URI);
    Schema schema = factory.newSchema(schemaFile);
    // create a Validator instance, which can be used to validate an instance document
    Validator validator = schema.newValidator();
    // validate the DOM tree
    try {/*from   w ww.  j  a  va 2 s . c o  m*/
        validator.validate(new DOMSource(document));
        return true;
    } catch (SAXException e) {
        e.printStackTrace();
        return false;
    }
}

From source file:mx.bigdata.cfdi.CFDv3.java

public void validate(ErrorHandler handler) throws Exception {
    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = sf.newSchema(getClass().getResource(XSD));
    Validator validator = schema.newValidator();
    if (handler != null) {
        validator.setErrorHandler(handler);
    }/*w w  w .  ja  v a2  s  .  co  m*/
    validator.validate(new JAXBSource(CONTEXT, document));
}

From source file:mx.bigdata.sat.cfdi.TFDv11c33.java

public void validar(ErrorHandler handler) throws Exception {
    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Source[] schemas = new Source[XSD.length];
    for (int i = 0; i < XSD.length; i++) {
        schemas[i] = new StreamSource(getClass().getResourceAsStream(XSD[i]));
    }//from  w w  w  . j av  a  2s  . c o m
    Schema schema = sf.newSchema(schemas);
    Validator validator = schema.newValidator();
    if (handler != null) {
        validator.setErrorHandler(handler);
    }
    validator.validate(new JAXBSource(CONTEXT, tfd));
}

From source file:mx.bigdata.sat.cfdi.TFDv1.java

public void validar(ErrorHandler handler) throws Exception {
    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = sf.newSchema(getClass().getResource(XSD));
    Validator validator = schema.newValidator();
    if (handler != null) {
        validator.setErrorHandler(handler);
    }/*from   w  w w.  jav a2s.c  om*/
    validator.validate(new JAXBSource(CONTEXT, tfd));
}