use of javax.xml.validation.Validator 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.Validator 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.Validator 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));
}
use of javax.xml.validation.Validator in project simba-os by cegeka.
the class Utils method validateXML.
/**
* This function attempts to validate an XML string against the specified
* schema. It will parse the string into a DOM document and validate this
* document against the schema.
*
* @param xmlString The XML document which should be validated
* @param schemaName The schema filename which should be used
* @throws Exception
*/
public static Document validateXML(String xmlString, String schemaName, Boolean... debugMode) throws Exception {
try {
String schemaFullPath = "schemas/" + schemaName;
log.debug("schemaFullPath: " + schemaFullPath);
ClassLoader classLoader = Utils.class.getClassLoader();
URL schemaFile = classLoader.getResource(schemaFullPath);
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(schemaFile);
Validator validator = schema.newValidator();
XMLErrorHandler errorHandler = new XMLErrorHandler();
validator.setErrorHandler(errorHandler);
validator.validate(new StreamSource(new StringReader(xmlString)));
if (errorHandler.getErrorXML().size() > 0) {
throw new Error("Invalid XML. See the log");
}
} catch (Exception ex) {
log.error("Error executing validateXML: " + ex.getMessage(), ex);
throw ex;
}
return convertStringToDocument(xmlString);
}
use of javax.xml.validation.Validator in project ddf by codice.
the class TestSingleSignOn method validateSaml.
private void validateSaml(String xml, SamlSchema schema) throws IOException {
// Prepare the schema and xml
String schemaFileName = "saml-schema-" + schema.toString().toLowerCase() + "-2.0.xsd";
URL schemaURL = AbstractIntegrationTest.class.getClassLoader().getResource(schemaFileName);
StreamSource streamSource = new StreamSource(new StringReader(xml));
// If we fail to create a validator we don't want to stop the show, so we just log a warning
Validator validator = null;
try {
validator = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(schemaURL).newValidator();
} catch (SAXException e) {
LOGGER.debug("Exception creating validator. ", e);
}
// If the xml is invalid, then we want to fail completely
if (validator != null) {
try {
validator.validate(streamSource);
} catch (SAXException e) {
LoggingUtils.failWithThrowableStacktrace(e, "Failed to validate SAML: ");
}
}
}
Aggregations