use of javax.xml.validation.Validator in project camel by apache.
the class ValidatingProcessor method doProcess.
protected void doProcess(Exchange exchange) throws Exception {
Schema schema;
if (isUseSharedSchema()) {
schema = getSchema();
} else {
schema = createSchema();
}
Validator validator = schema.newValidator();
// the underlying input stream, which we need to close to avoid locking files or other resources
Source source = null;
InputStream is = null;
try {
Result result = null;
// only convert to input stream if really needed
if (isInputStreamNeeded(exchange)) {
is = getContentToValidate(exchange, InputStream.class);
if (is != null) {
source = getSource(exchange, is);
}
} else {
Object content = getContentToValidate(exchange);
if (content != null) {
source = getSource(exchange, content);
}
}
if (shouldUseHeader()) {
if (source == null && isFailOnNullHeader()) {
throw new NoXmlHeaderValidationException(exchange, headerName);
}
} else {
if (source == null && isFailOnNullBody()) {
throw new NoXmlBodyValidationException(exchange);
}
}
//CAMEL-7036 We don't need to set the result if the source is an instance of StreamSource
if (source instanceof DOMSource) {
result = new DOMResult();
} else if (source instanceof SAXSource) {
result = new SAXResult();
} else if (source instanceof StAXSource || source instanceof StreamSource) {
result = null;
}
if (source != null) {
// create a new errorHandler and set it on the validator
// must be a local instance to avoid problems with concurrency (to be
// thread safe)
ValidatorErrorHandler handler = errorHandler.getClass().newInstance();
validator.setErrorHandler(handler);
try {
LOG.trace("Validating {}", source);
validator.validate(source, result);
handler.handleErrors(exchange, schema, result);
} catch (SAXParseException e) {
// can be thrown for non well formed XML
throw new SchemaValidationException(exchange, schema, Collections.singletonList(e), Collections.<SAXParseException>emptyList(), Collections.<SAXParseException>emptyList());
}
}
} finally {
IOHelper.close(is);
}
}
use of javax.xml.validation.Validator in project head by mifos.
the class ChartOfAccountsConfig method load.
/**
* Factory method which loads the Chart of Accounts configuration from the
* given filename. Given XML filename will be validated against
* {@link FilePaths#CHART_OF_ACCOUNTS_SCHEMA}.
*
* @param chartOfAccountsXml
* a relative path to the Chart of Accounts configuration file.
*/
public static ChartOfAccountsConfig load(String chartOfAccountsXml) throws ConfigurationException {
ChartOfAccountsConfig instance = null;
Document document = null;
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder parser = dbf.newDocumentBuilder();
if (FilePaths.CHART_OF_ACCOUNTS_DEFAULT.equals(chartOfAccountsXml)) {
// default chart of accounts
document = parser.parse(MifosResourceUtil.getClassPathResourceAsStream(chartOfAccountsXml));
} else {
// custom chart of accounts
document = parser.parse(MifosResourceUtil.getFile(chartOfAccountsXml));
}
// create a SchemaFactory capable of understanding XML schemas
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
// load an XML schema
ClassPathResource schemaFileResource = new ClassPathResource(FilePaths.CHART_OF_ACCOUNTS_SCHEMA);
Source schemaFile = null;
if (schemaFileResource.exists()) {
InputStream in = ChartOfAccountsConfig.class.getClassLoader().getResourceAsStream(FilePaths.CHART_OF_ACCOUNTS_SCHEMA);
schemaFile = new StreamSource(in);
} else {
schemaFile = new StreamSource(MifosResourceUtil.getFile(FilePaths.CHART_OF_ACCOUNTS_SCHEMA));
}
Schema schema = factory.newSchema(schemaFile);
// create a Validator instance and validate document
Validator validator = schema.newValidator();
validator.validate(new DOMSource(document));
} catch (IOException e) {
throw new ConfigurationException(e);
} catch (SAXException e) {
throw new ConfigurationException(e);
} catch (ParserConfigurationException e) {
throw new ConfigurationException(e);
}
instance = new ChartOfAccountsConfig();
instance.coaDocument = document;
return instance;
}
use of javax.xml.validation.Validator in project openblocks by mikaelhg.
the class WorkspaceController method validate.
/**
* Validates the code blocks document against the schema
* @param document The document to check
* @throws RuntimeException If the validation failed
*/
private void validate(Document document) {
try {
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
URL schemaUrl = ClassLoader.getSystemResource("edu/mit/blocks/codeblocks/codeblocks.xsd");
Schema schema = schemaFactory.newSchema(schemaUrl);
Validator validator = schema.newValidator();
validator.validate(new DOMSource(document));
} catch (MalformedURLException e) {
throw new RuntimeException(e);
} catch (SAXException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of javax.xml.validation.Validator in project camel by apache.
the class XAdESSignaturePropertiesTest method validateAgainstSchema.
private void validateAgainstSchema(Document doc) throws Exception {
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Source schema1 = new StreamSource(new File("target/test-classes/org/apache/camel/component/xmlsecurity/xades/XAdES.xsd"));
Source schema2 = new StreamSource(new File("target/test-classes/org/apache/camel/component/xmlsecurity/xades/xmldsig-core-schema.xsd"));
Schema schema = factory.newSchema(new Source[] { schema2, schema1 });
Validator validator = schema.newValidator();
validator.validate(new DOMSource(doc));
}
use of javax.xml.validation.Validator in project hazelcast by hazelcast.
the class ClientDiscoverySpiTest method testSchema.
@Test
public void testSchema() throws Exception {
String xmlFileName = "hazelcast-client-discovery-spi-test.xml";
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
URL schemaResource = ClientDiscoverySpiTest.class.getClassLoader().getResource("hazelcast-client-config-3.9.xsd");
Schema schema = factory.newSchema(schemaResource);
InputStream xmlResource = ClientDiscoverySpiTest.class.getClassLoader().getResourceAsStream(xmlFileName);
Source source = new StreamSource(xmlResource);
Validator validator = schema.newValidator();
validator.validate(source);
}
Aggregations