use of javax.xml.validation.Validator in project JMRI by JMRI.
the class XMLUtil method validate.
/**
* Check whether a DOM tree is valid according to a schema. Example of
* usage:
* <pre>
* Element fragment = ...;
* SchemaFactory f = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
* Schema s = f.newSchema(This.class.getResource("something.xsd"));
* try {
* XMLUtil.validate(fragment, s);
* // valid
* } catch (SAXException x) {
* // invalid
* }
* </pre>
*
* @param data a DOM tree
* @param schema a parsed schema
* @throws SAXException if validation failed
* @since org.openide.util 7.17
*/
public static void validate(Element data, Schema schema) throws SAXException {
Validator v = schema.newValidator();
final SAXException[] error = { null };
v.setErrorHandler(new ErrorHandler() {
@Override
public void warning(SAXParseException x) throws SAXException {
}
@Override
public void error(SAXParseException x) throws SAXException {
// Just rethrowing it is bad because it will also print it to stderr.
error[0] = x;
}
@Override
public void fatalError(SAXParseException x) throws SAXException {
error[0] = x;
}
});
try {
v.validate(new DOMSource(fixupAttrs(data)));
} catch (IOException x) {
assert false : x;
}
if (error[0] != null) {
throw error[0];
}
}
use of javax.xml.validation.Validator 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.Validator 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.Validator 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.Validator 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);
}
}
Aggregations