use of javax.xml.validation.Schema in project maven-plugins by apache.
the class DefaultChangesSchemaValidator method getSchema.
public Schema getSchema(String schemaPath) throws SAXException, IOException {
if (this.compiledSchemas.containsKey(schemaPath)) {
return (Schema) this.compiledSchemas.get(schemaPath);
}
Schema schema = this.compileJAXPSchema(schemaPath);
this.compiledSchemas.put(schemaPath, schema);
return schema;
}
use of javax.xml.validation.Schema in project maven-plugins by apache.
the class DefaultChangesSchemaValidator method validateXmlWithSchema.
public XmlValidationHandler validateXmlWithSchema(File file, String schemaVersion, boolean failOnValidationError) throws SchemaValidatorException {
Reader reader = null;
try {
String schemaPath = CHANGES_SCHEMA_PATH + "changes-" + schemaVersion + ".xsd";
Schema schema = getSchema(schemaPath);
Validator validator = schema.newValidator();
XmlValidationHandler baseHandler = new XmlValidationHandler(failOnValidationError);
validator.setErrorHandler(baseHandler);
reader = new XmlStreamReader(file);
validator.validate(new StreamSource(reader));
reader.close();
reader = null;
return baseHandler;
} catch (IOException e) {
throw new SchemaValidatorException("IOException : " + e.getMessage(), e);
} catch (SAXException e) {
throw new SchemaValidatorException("SAXException : " + e.getMessage(), e);
} catch (Exception e) {
throw new SchemaValidatorException("Exception : " + e.getMessage(), e);
} finally {
IOUtil.close(reader);
}
}
use of javax.xml.validation.Schema in project poi by apache.
the class XSSFExportToXml method isValid.
/**
* Validate the generated XML against the XML Schema associated with the XSSFMap
*
* @param xml the XML to validate
* @return true, if document is valid
* @throws SAXException If validating the document fails
*/
private boolean isValid(Document xml) throws SAXException {
try {
String language = "http://www.w3.org/2001/XMLSchema";
SchemaFactory factory = SchemaFactory.newInstance(language);
Source source = new DOMSource(map.getSchema());
Schema schema = factory.newSchema(source);
Validator validator = schema.newValidator();
validator.validate(new DOMSource(xml));
//if no exceptions where raised, the document is valid
return true;
} catch (IOException e) {
LOG.log(POILogger.ERROR, "document is not valid", e);
}
return false;
}
use of javax.xml.validation.Schema in project webservices-axiom by apache.
the class TestValidator method runTest.
protected void runTest() throws Throwable {
SchemaFactory factory = new XMLSchemaFactory();
DocumentBuilder builder = dbf.newDocumentBuilder();
Schema schema = factory.newSchema(new DOMSource(builder.parse(TestValidator.class.getResourceAsStream("ipo.xsd"))));
Validator validator = schema.newValidator();
validator.validate(new DOMSource(builder.parse(TestValidator.class.getResourceAsStream("ipo_1.xml"))));
}
use of javax.xml.validation.Schema in project webservices-axiom by apache.
the class ValidateSample method validateUsingDOM.
// START SNIPPET: dom
public void validateUsingDOM(InputStream in, URL schemaUrl) throws Exception {
OMMetaFactory mf = OMAbstractFactory.getMetaFactory(OMAbstractFactory.FEATURE_DOM);
SOAPModelBuilder builder = OMXMLBuilderFactory.createSOAPModelBuilder(mf, in, "UTF-8");
SOAPEnvelope envelope = builder.getSOAPEnvelope();
OMElement bodyContent = envelope.getBody().getFirstElement();
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(schemaUrl);
Validator validator = schema.newValidator();
validator.validate(new DOMSource((Element) bodyContent));
}
Aggregations