1 2 import org.w3c.dom.TypeInfo ; 3 import org.w3c.dom.ls.LSInput ; 4 import org.w3c.dom.ls.LSResourceResolver ; 5 import org.xml.sax.*; 6 import org.xml.sax.helpers.DefaultHandler ; 7 8 import javax.xml.parsers.SAXParser ; 9 import javax.xml.parsers.SAXParserFactory ; 10 import javax.xml.validation.Schema ; 11 import javax.xml.validation.SchemaFactory ; 12 import javax.xml.validation.TypeInfoProvider ; 13 import javax.xml.validation.ValidatorHandler ; 14 import java.io.File ; 15 16 26 27 public class SchemaValidatorHandlerExample { 28 29 32 private SchemaValidatorHandlerExample() {} 33 34 37 38 protected static class LocalErrorHandler implements ErrorHandler { 39 40 44 public void error(SAXParseException ex) { 45 System.err.println("At line " + ex.getLineNumber() + " of " + ex.getSystemId() + ':'); 46 System.err.println(ex.getMessage()); 47 } 48 49 53 54 public void fatalError(SAXParseException ex) { 55 System.err.println("At line " + ex.getLineNumber() + " of " + ex.getSystemId() + ':'); 56 System.err.println(ex.getMessage()); 57 } 58 59 63 public void warning(SAXParseException ex) { 64 System.err.println("At line " + ex.getLineNumber() + " of " + ex.getSystemId() + ':'); 65 System.err.println(ex.getMessage()); 66 } 67 68 } 69 70 75 76 protected static class Resolver implements LSResourceResolver { 77 78 89 90 public LSInput resolveResource(String type, String namespace, String publicId, String systemId, String baseURI) { 91 return null; 92 } 93 94 } 95 96 99 100 public static class LocalContentHandler extends DefaultHandler { 101 102 int indent = 0; 103 private TypeInfoProvider provider; 104 105 public LocalContentHandler(TypeInfoProvider provider) { 106 this.provider = provider; 107 } 108 109 112 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { 113 TypeInfo etype = provider.getElementTypeInfo(); 114 StringBuffer sb = new StringBuffer (100); 115 for (int i=0; i<indent; i++) { 116 sb.append(" "); 117 } 118 sb.append("Element " + qName); 119 sb.append(" of type {" + etype.getTypeNamespace() + '}' + etype.getTypeName()); 120 System.out.println(sb.toString()); 121 for (int a=0; a<attributes.getLength(); a++) { 122 TypeInfo atype = provider.getAttributeTypeInfo(a); 123 boolean spec = provider.isSpecified(a); 124 sb.setLength(0); 125 for (int i=0; i<indent+2; i++) { 126 sb.append(" "); 127 } 128 sb.append("Attribute " + attributes.getQName(a) + (spec ? " (specified)" : (" (defaulted)"))); 129 sb.append(" of type {" + atype.getTypeNamespace() + '}' + atype.getTypeName()); 130 System.out.println(sb.toString()); 131 } 132 indent++; 133 } 134 135 138 public void endElement(String uri, String localName, String qName) throws SAXException { 139 indent--; 140 } 141 } 142 143 147 public static void main(String [] args) { 148 try { 149 if(args.length != 2){ 150 printUsage(); 151 return; 152 } 153 154 SchemaFactory schemaFactory; 155 156 160 163 schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); 164 System.err.println("Loaded schema validation provider " + schemaFactory.getClass().getName()); 165 166 schemaFactory.setErrorHandler(new LocalErrorHandler()); 167 Schema schemaGrammar = schemaFactory.newSchema(new File (args[0])); 169 170 System.err.println("Created Grammar object for schema : "+args[0]); 171 172 Resolver resolver = new Resolver (); 173 174 ValidatorHandler schemaValidator = schemaGrammar.newValidatorHandler(); 176 schemaValidator.setResourceResolver(resolver); 177 schemaValidator.setErrorHandler(new LocalErrorHandler()); 178 schemaValidator.setContentHandler(new LocalContentHandler(schemaValidator.getTypeInfoProvider())); 179 180 System.err.println("Validating "+args[1] +" against grammar "+args[0]); 181 SAXParserFactory parserFactory = SAXParserFactory.newInstance(); 182 parserFactory.setNamespaceAware(true); 183 SAXParser parser = parserFactory.newSAXParser(); 184 XMLReader reader = parser.getXMLReader(); 185 reader.setContentHandler(schemaValidator); 186 reader.parse(new InputSource(new File (args[1]).toURI().toString())); 187 188 System.err.println("Validation successful"); 189 } catch (SAXException saxe) { 190 exit(1, "Error: " + saxe.getMessage()); 191 } catch (Exception e) { 192 e.printStackTrace(); 193 exit(2, "Fatal Error: " + e); 194 } 195 } 196 197 202 public static void exit(int errCode, String msg) { 203 System.err.println(msg); 204 System.exit(errCode); 205 } 206 207 public static void printUsage(){ 208 System.err.println("Usage : java SchemaValidatorHandlerExample <schemaFile> <xmlFile>"); 209 } 210 } 211
| Popular Tags
|