diff options
author | Stiver <stiver.mail@gmail.com> | 2014-03-04 15:13:11 +0100 |
---|---|---|
committer | Stiver <stiver.mail@gmail.com> | 2014-03-04 15:13:11 +0100 |
commit | e2d0f5d9c38561d67f23754c00addb4a3547efb2 (patch) | |
tree | 1832f16037c086b48266b8566aecc61f45f4e5f1 /src/test/TestValidateXml.java | |
download | fernflower-e2d0f5d9c38561d67f23754c00addb4a3547efb2.tar fernflower-e2d0f5d9c38561d67f23754c00addb4a3547efb2.tar.gz fernflower-e2d0f5d9c38561d67f23754c00addb4a3547efb2.tar.lz fernflower-e2d0f5d9c38561d67f23754c00addb4a3547efb2.tar.xz fernflower-e2d0f5d9c38561d67f23754c00addb4a3547efb2.zip |
initial commit
Diffstat (limited to 'src/test/TestValidateXml.java')
-rw-r--r-- | src/test/TestValidateXml.java | 69 |
1 files changed, 69 insertions, 0 deletions
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; + } + +} |