From e2d0f5d9c38561d67f23754c00addb4a3547efb2 Mon Sep 17 00:00:00 2001 From: Stiver Date: Tue, 4 Mar 2014 15:13:11 +0100 Subject: initial commit --- src/test/TestValidateXml.java | 69 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/test/TestValidateXml.java (limited to 'src/test/TestValidateXml.java') diff --git a/src/test/TestValidateXml.java b/src/test/TestValidateXml.java new file mode 100644 index 0000000..a84e428 --- /dev/null +++ b/src/test/TestValidateXml.java @@ -0,0 +1,69 @@ +package test; + +import java.io.File; +import java.io.IOException; + +import javax.xml.transform.Source; +import javax.xml.transform.stream.StreamSource; +import javax.xml.validation.Schema; +import javax.xml.validation.SchemaFactory; +import javax.xml.validation.Validator; + +import org.xml.sax.ErrorHandler; +import org.xml.sax.SAXException; +import org.xml.sax.SAXParseException; + +public class TestValidateXml { + + public static void main(String[] args) { + + try { + // 1. Lookup a factory for the W3C XML Schema language + SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); + + // 2. Compile the schema. + // Here the schema is loaded from a java.io.File, but you could use + // a java.net.URL or a javax.xml.transform.Source instead. + Schema schema = factory.newSchema(new File("/opt/xml/docbook/xsd/docbook.xsd")); + + // 3. Get a validator from the schema. + Validator validator = schema.newValidator(); + validator.setErrorHandler(new ForgivingErrorHandler()); + + // 4. Parse the document you want to check. + Source source = new StreamSource(args[0]); + + // 5. Check the document + try { + validator.validate(source); + System.out.println(args[0] + " is valid."); + } catch (SAXException ex) { + System.out.println(args[0] + " is not valid because "); + System.out.println(ex.getMessage()); + } + } catch(IOException ex) { + System.out.println(args[0] + " is not valid because "); + System.out.println(ex.getMessage()); + } catch(SAXException ex) { + System.out.println(args[0] + " is not valid because "); + System.out.println(ex.getMessage()); + } + } + +} + +class ForgivingErrorHandler implements ErrorHandler { + + public void warning(SAXParseException ex) { + System.err.println(ex.getMessage()); + } + + public void error(SAXParseException ex) { + System.err.println(ex.getMessage()); + } + + public void fatalError(SAXParseException ex) throws SAXException { + throw ex; + } + +} -- cgit v1.2.3