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:Main.java

private static Schema readSchemaFromFile(File schemaFile) throws SAXException {
    return (SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)).newSchema(schemaFile);
}

From source file:Main.java

/**
 * Generic method to Validate XML file while marshalling against their schema.
 * //from  w ww  . j  a  va 2s. co m
 * @param context
 * @param schemaFile
 * @param object
 * @return
 * @throws SAXException
 * @throws JAXBException
 */
public static String validateAndMarshallXML(JAXBContext context, String schemaFile, Object object)
        throws SAXException, JAXBException {
    String xmlFormOfBean = null;

    if (context != null && (schemaFile != null && schemaFile.trim().length() > 0)) {
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); // thread- safe 
        marshaller.setSchema(sf.newSchema(new File(schemaFile))); // validate jaxb context against schema 
        ByteArrayOutputStream sos = new ByteArrayOutputStream(); // for XML output into string

        marshaller.marshal(object, sos);
        xmlFormOfBean = sos.toString();

    }
    return xmlFormOfBean;
}

From source file:Main.java

/**
 * Perform a schema validation/*from  w  ww .  j a  v  a  2 s .co m*/
 */
public static void validate(Source schema, Source document) throws Exception {
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema sch = factory.newSchema(schema);
    Validator validator = sch.newValidator();
    validator.validate(document);
}

From source file:Main.java

/**
 * This method validate XML by input XML as String and XSD File.
 *
 * @param xml input XML as String//  w ww . jav  a  2  s  .co m
 * @param xsd input XSD File
 *
 * @return true or false, valid or not
 */
public static boolean validateXMLByXSD(String xml, File xsd) {
    try {
        SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(xsd).newValidator()
                .validate(new StreamSource(new StringReader(xml)));
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

From source file:Main.java

public static boolean validateXML(String schemaPath, String xmlPath) {
    try {//  w  w w  .j av a  2 s .  c  om
        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

/**
 * Validates the given document with its schema.
 * //from  w ww  .j a va2s. 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));
}

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

public static boolean validate(String xsd, String xml) {
    try {//ww  w  . j av a  2s.com
        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

/**
 * This method will validate a non-niem xml instance.
 * /*  w w  w  . j  a  va  2  s .c  o m*/
 * @param xsdPath - this is a relative path to an xsd, typically in OJB_Utilies or in the enclosing project
 * @param xml - This is the XML document as a string
 * @throws Exception - typically a validation exception or a file path exception
 */

public static void validateInstanceNonNIEMXsd(String xsdPath, String xml) throws Exception {

    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = factory.newSchema(new File(xsdPath));
    Validator validator = schema.newValidator();
    validator.validate(new StreamSource(new StringReader(xml)));
}

From source file:Main.java

public static Schema getSchema(String schemaString) {
    Schema schema = null;/*from   ww  w  .j  av a  2s .c  om*/
    try {
        if (schemaString != null) {
            SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            StreamSource ss = new StreamSource();
            ss.setReader(new StringReader(schemaString));
            schema = sf.newSchema(ss);
        }
    } catch (Exception e) {
        throw new RuntimeException("Failed to create schemma from string: " + schemaString, e);
    }
    return schema;
}

From source file:Main.java

/**
 * Check a xml file against a schema./*  w w  w .j a v  a2 s  .c  om*/
 * 
 * @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);
}