use of javax.xml.validation.Schema in project fc-java-sdk by aliyun.
the class XmlUtils method validateXml.
public static void validateXml(Node root, InputStream xsd) throws SAXException, IOException {
try {
Source source = new StreamSource(xsd);
Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(source);
Validator validator = schema.newValidator();
validator.validate(new DOMSource(root));
} finally {
closeStream(xsd);
}
}
use of javax.xml.validation.Schema in project aries by apache.
the class BlueprintFileWriterTest method generatedXmlIsValid.
@Test
public void generatedXmlIsValid() throws Exception {
Document document = readToDocument(xmlAsBytes, true);
Source[] schemas = new StreamSource[] { new StreamSource(BlueprintFileWriterTest.class.getResourceAsStream("/schema/example.xsd")), new StreamSource(BlueprintFileWriterTest.class.getResourceAsStream("/schema/org/apache/aries/blueprint/blueprint.xsd")), new StreamSource(BlueprintFileWriterTest.class.getResourceAsStream("/schema/org/apache/aries/blueprint/ext/impl/blueprint-ext.xsd")), new StreamSource(BlueprintFileWriterTest.class.getResourceAsStream("/schema/org/apache/aries/blueprint/ext/impl/blueprint-ext-1.1.xsd")), new StreamSource(BlueprintFileWriterTest.class.getResourceAsStream("/schema/org/apache/aries/blueprint/ext/impl/blueprint-ext-1.2.xsd")), new StreamSource(BlueprintFileWriterTest.class.getResourceAsStream("/schema/org/apache/aries/blueprint/ext/impl/blueprint-ext-1.3.xsd")), new StreamSource(BlueprintFileWriterTest.class.getResourceAsStream("/schema/org/apache/aries/blueprint/ext/impl/blueprint-ext-1.4.xsd")), new StreamSource(BlueprintFileWriterTest.class.getResourceAsStream("/schema/org/apache/aries/blueprint/ext/impl/blueprint-ext-1.5.xsd")), new StreamSource(BlueprintFileWriterTest.class.getResourceAsStream("/schema/org/apache/aries/transaction/parsing/transactionv12.xsd")), new StreamSource(BlueprintFileWriterTest.class.getResourceAsStream("/schema/org/apache/aries/jpa/blueprint/namespace/jpa_110.xsd")), new StreamSource(BlueprintFileWriterTest.class.getResourceAsStream("/schema/org/apache/aries/blueprint/compendium/cm/blueprint-cm-1.0.0.xsd")), new StreamSource(BlueprintFileWriterTest.class.getResourceAsStream("/schema/org/apache/aries/blueprint/compendium/cm/blueprint-cm-1.1.0.xsd")), new StreamSource(BlueprintFileWriterTest.class.getResourceAsStream("/schema/org/apache/aries/blueprint/compendium/cm/blueprint-cm-1.3.0.xsd")), new StreamSource(BlueprintFileWriterTest.class.getResourceAsStream("/schema/org/apache/aries/blueprint/compendium/cm/blueprint-cm-1.2.0.xsd")) };
Source xmlFile = new DOMSource(document);
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(schemas);
Validator validator = schema.newValidator();
validator.validate(xmlFile);
}
use of javax.xml.validation.Schema 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.Schema in project karaf by apache.
the class JaxbUtil method unmarshalValidate.
private static Features unmarshalValidate(String uri, InputStream stream) {
try {
Document doc;
if (stream != null) {
doc = XmlUtils.parse(stream);
doc.setDocumentURI(uri);
} else {
doc = XmlUtils.parse(uri);
}
String nsuri = doc.getDocumentElement().getNamespaceURI();
if (nsuri == null) {
LOGGER.warn("Old style feature file without namespace found (URI: {}). This format is deprecated and support for it will soon be removed", uri);
} else {
Schema schema = getSchema(nsuri);
try {
schema.newValidator().validate(new DOMSource(doc));
} catch (SAXException e) {
throw new IllegalArgumentException("Unable to validate " + uri, e);
}
}
fixDom(doc, doc.getDocumentElement());
Unmarshaller unmarshaller = FEATURES_CONTEXT.createUnmarshaller();
Features features = (Features) unmarshaller.unmarshal(new DOMSource(doc));
features.setNamespace(nsuri);
return features;
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException("Unable to load " + uri, e);
}
}
use of javax.xml.validation.Schema 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;
}
Aggregations