Example usage for javax.xml.validation SchemaFactory newSchema

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

Introduction

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

Prototype

public abstract Schema newSchema(Source[] schemas) throws SAXException;

Source Link

Document

Parses the specified source(s) as a schema and returns it as a schema.

Usage

From source file:Main.java

public static boolean xmlStringValidate(String xmlStr, String xsdPath) {
    boolean flag = false;
    try {/*from   w w  w.  ja v a 2 s.c o m*/
        SchemaFactory factory = SchemaFactory.newInstance(SCHEMALANG);
        File schemaLocation = new File(xsdPath);
        Schema schema = factory.newSchema(schemaLocation);
        Validator validator = schema.newValidator();
        InputStream is = new ByteArrayInputStream(xmlStr.getBytes());
        Source source = new StreamSource(is);
        try {
            validator.validate(source);
            flag = true;
        } catch (SAXException ex) {
            System.out.println(ex.getMessage());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return flag;
}

From source file:Main.java

public static boolean validateXML(String schemaPath, String xmlPath) {
    try {/*from  w  w  w.ja  v a 2  s  .co m*/
        String schemaLang = "http://www.w3.org/2001/XMLSchema";
        SchemaFactory factory = SchemaFactory.newInstance(schemaLang);
        // create schema by reading it from an XSD file:
        Schema schema = factory.newSchema(new StreamSource(schemaPath));
        Validator validator = schema.newValidator();
        // at last perform validation:
        validator.validate(new StreamSource(xmlPath));
    } catch (Exception ex) {
        return false;
    }
    return true;
}

From source file:Main.java

public static String validate(String xsdPath, String xmlPath) {
    String response = "OK";

    try {//from   w  w  w.  j  a  v  a2 s .com
        SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
        File schemaLocation = new File(xsdPath);
        Schema schema = factory.newSchema(schemaLocation);
        Validator validator = schema.newValidator();
        Source source = new StreamSource(xmlPath);
        Result result = null;
        validator.validate(source, result);
    } catch (Exception e) {
        response = e.getMessage();
    }

    return response;
}

From source file:com.github.woozoo73.ht.XmlUtils.java

public static boolean validate(String xsd, String xml) {
    try {/*from   w w w. j  a v  a 2 s  .co  m*/
        SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
        Schema schema = factory.newSchema(new StreamSource(new ByteArrayInputStream(xsd.getBytes("UTF-8"))));
        Validator validator = schema.newValidator();

        boolean valid = false;
        try {
            validator.validate(new StreamSource(new ByteArrayInputStream(xml.getBytes("UTF-8"))));
            valid = true;
        } catch (Exception e) {
            logger.warn(e.getMessage(), e);
        }

        return valid;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * Check a xml file against a schema./*from ww w .ja  va 2s .  co  m*/
 * 
 * @param fileToCheck
 * @param schemURL
 * @throws Exception
 */
static public void schemaCheck(File fileToCheck, URL schemURL) throws Exception {

    final String W3C_SCHEMA_SPEC = "http://www.w3.org/2001/XMLSchema";
    HashMap<String, Schema> MetsSchema = null;

    if (MetsSchema == null) {
        MetsSchema = new HashMap();
    }

    if (MetsSchema.get(schemURL.toString()) == null) {
        // 1. Lookup a factory for the W3C XML Schema language
        SchemaFactory factory = SchemaFactory.newInstance(W3C_SCHEMA_SPEC);

        // 2. Compile the schema. 
        MetsSchema.put(schemURL.toString(), factory.newSchema(schemURL));
    }

    // 3. Get a validator from the schema.
    Validator validator = MetsSchema.get(schemURL.toString()).newValidator();

    // 4. Parse the document you want to check.
    Source source = new StreamSource(fileToCheck);

    // 5. Check the document        
    validator.validate(source);
}

From source file:Main.java

public static boolean doesXMLMatchXSD(String xmlUrl) throws SAXException, FileNotFoundException, IOException {

    boolean doesMatchXSD = false;

    SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
    SAXSource sourceXSD = new SAXSource(new InputSource(new FileInputStream(new File(xsdUrl))));
    Schema schema = factory.newSchema(sourceXSD);
    Validator validator = (Validator) schema.newValidator();
    validator.validate(new StreamSource(new File(xmlUrl)));

    doesMatchXSD = true;//from   www.  j a v a  2s  .  c  o m
    return doesMatchXSD;
}

From source file:eu.europa.esig.dss.validation.ValidationResourceManager.java

/**
 * This is the utility method that loads the data from the inputstream determined by the inputstream parameter into
 * a/*from   w w  w  .  j  ava 2s.c o m*/
 * {@link ConstraintsParameters}.
 *
 * @param inputStream
 * @return
 */
public static ConstraintsParameters load(final InputStream inputStream) throws DSSException {
    try {
        SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = sf.newSchema(new StreamSource(
                ValidationResourceManager.class.getResourceAsStream(defaultPolicyXsdLocation)));

        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        unmarshaller.setSchema(schema);

        return (ConstraintsParameters) unmarshaller.unmarshal(inputStream);
    } catch (Exception e) {
        throw new DSSException("Unable to load policy : " + e.getMessage(), e);
    }
}

From source file:io.konik.utils.InvoiceLoaderUtils.java

public static Validator getSchemaValidator() throws SAXException {
    SchemaFactory sf = SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI);
    URL schemaInvoice = notNull(InvoiceLoaderUtils.class.getResource(ZF_1_SCHEMA_XSD));
    Schema invoiceSchema = sf.newSchema(schemaInvoice);
    return invoiceSchema.newValidator();
}

From source file:energy.usef.core.util.XMLUtil.java

/**
 * Converts xml to an object after optional validation against the xsd.
 *
 * @param xml xml string/* w ww . j ava  2s. c  o m*/
 * @param validate true when the xml needs to be validated
 * @return object corresponding to this xml
 */
public static Object xmlToMessage(String xml, boolean validate) {
    try (InputStream is = IOUtils.toInputStream(xml, UTF_8)) {
        Unmarshaller unmarshaller = CONTEXT.createUnmarshaller();

        if (validate) {
            // setup xsd validation
            SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            Schema schema = sf.newSchema(XMLUtil.class.getClassLoader().getResource(MESSAGING_XSD_FILE));

            unmarshaller.setSchema(schema);
        }

        return unmarshaller.unmarshal(is);
    } catch (JAXBException | IOException e) {
        LOGGER.error(e.getMessage(), e);
        throw new TechnicalException("Invalid XML content: " + e.getMessage(), e);
    } catch (SAXException e) {
        LOGGER.error(e.getMessage(), e);
        throw new TechnicalException("Unable to read XSD schema", e);
    }
}

From source file:Main.java

/**
 * Validates the given document with its schema.
 * /*from w ww.  ja va  2  s . c  o m*/
 * @param document - The XML file contents
 * @param schemaFileStream - the inputStream of the schema content
 * @throws Exception
 */
public static void validateDocument(Document document, InputStream schemaFileStream) throws Exception {
    // create a SchemaFactory capable of understanding WXS schemas
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    // load a WXS schema, represented by a Schema instance
    Source schemaFile = new StreamSource(schemaFileStream);
    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
    validator.validate(new DOMSource(document));
}