1 9 10 package org.dom4j.samples.validate; 11 12 import org.dom4j.Document; 13 import org.dom4j.DocumentException; 14 import org.dom4j.io.SAXReader; 15 import org.iso_relax.verifier.Schema; 16 import org.iso_relax.verifier.Verifier; 17 import org.iso_relax.verifier.VerifierFactory; 18 import org.iso_relax.verifier.VerifierFilter; 19 import org.xml.sax.ErrorHandler ; 20 import org.xml.sax.SAXParseException ; 21 22 29 public class JARVDemo2 { 30 31 public static void main(String [] args) { 32 new JARVDemo2().run(args); 33 } 34 35 public void run(String [] args) { 36 try { 37 if (args.length < 2) { 38 System.out.println("usage: <xmlDoc> <schemaDoc>"); 39 System.out 40 .println("Which validates the given XML document against the given schema document"); 41 System.out 42 .println("The schema can be XML Schema, RelaxNG, Relax or TREX"); 43 return; 44 } 45 String xmlFile = args[0]; 46 String schema = args[1]; 47 48 SAXReader reader = createSAXReader(schema); 49 Document document = reader.read(xmlFile); 50 51 System.out.println("Parsed document: " + xmlFile + " correctly."); 52 } catch (DocumentException e) { 53 System.out.println("Exception occurred: " + e); 54 Throwable nestedException = e.getNestedException(); 55 if (nestedException != null) { 56 System.out.println("NestedException: " + nestedException); 57 nestedException.printStackTrace(); 58 } else { 59 e.printStackTrace(); 60 } 61 } catch (Throwable t) { 62 System.out.println("Exception occurred: " + t); 63 t.printStackTrace(); 64 } 65 } 66 67 68 protected SAXReader createSAXReader(String schemaURI) throws Exception { 69 70 System.out.println("Loaded schema document: " + schemaURI); 71 72 VerifierFactory factory = new com.sun.msv.verifier.jarv.TheFactoryImpl(); 74 Schema schema = factory.compileSchema(schemaURI); 75 76 Verifier verifier = schema.newVerifier(); 77 verifier.setErrorHandler(new ErrorHandler () { 78 public void error(SAXParseException e) { 79 System.out.println("ERROR: " + e); 80 } 81 82 public void fatalError(SAXParseException e) { 83 System.out.println("FATAL: " + e); 84 } 85 86 public void warning(SAXParseException e) { 87 System.out.println("WARNING: " + e); 88 } 89 }); 90 91 VerifierFilter filter = verifier.getVerifierFilter(); 93 SAXReader reader = new SAXReader(); 94 reader.setXMLFilter(filter); 95 return reader; 96 } 97 } 98 99 137 | Popular Tags |