use of javax.xml.validation.SchemaFactory in project logging-log4j2 by apache.
the class XmlCompactFileAppenderValidationTest method validateXmlSchema.
private void validateXmlSchema(final File file) throws SAXException, IOException {
final URL schemaFile = this.getClass().getClassLoader().getResource("Log4j-events.xsd");
final Source xmlFile = new StreamSource(file);
final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
final Schema schema = schemaFactory.newSchema(schemaFile);
final Validator validator = schema.newValidator();
validator.validate(xmlFile);
}
use of javax.xml.validation.SchemaFactory in project karaf by apache.
the class JaxbUtil method getSchema.
private static Schema getSchema(String namespace) throws SAXException {
Schema schema = SCHEMAS.get(namespace);
if (schema == null) {
String schemaLocation;
switch(namespace) {
case FeaturesNamespaces.URI_1_0_0:
schemaLocation = "/org/apache/karaf/features/karaf-features-1.0.0.xsd";
break;
case FeaturesNamespaces.URI_1_1_0:
schemaLocation = "/org/apache/karaf/features/karaf-features-1.1.0.xsd";
break;
case FeaturesNamespaces.URI_1_2_0:
schemaLocation = "/org/apache/karaf/features/karaf-features-1.2.0.xsd";
break;
case FeaturesNamespaces.URI_1_2_1:
schemaLocation = "/org/apache/karaf/features/karaf-features-1.2.1.xsd";
break;
case FeaturesNamespaces.URI_1_3_0:
schemaLocation = "/org/apache/karaf/features/karaf-features-1.3.0.xsd";
break;
case FeaturesNamespaces.URI_1_4_0:
schemaLocation = "/org/apache/karaf/features/karaf-features-1.4.0.xsd";
break;
case FeaturesNamespaces.URI_1_5_0:
schemaLocation = "/org/apache/karaf/features/karaf-features-1.5.0.xsd";
break;
default:
throw new IllegalArgumentException("Unsupported namespace: " + namespace);
}
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
// root element has namespace - we can use schema validation
URL url = JaxbUtil.class.getResource(schemaLocation);
if (url == null) {
throw new IllegalStateException("Could not find resource: " + schemaLocation);
}
schema = factory.newSchema(new StreamSource(url.toExternalForm()));
SCHEMAS.put(namespace, schema);
}
return schema;
}
use of javax.xml.validation.SchemaFactory 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.SchemaFactory 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.SchemaFactory 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