1 16 17 package xs; 18 19 import org.apache.xerces.xs.XSConstants; 20 import org.apache.xerces.xs.XSImplementation; 21 import org.apache.xerces.xs.XSLoader; 22 import org.apache.xerces.xs.XSModel; 23 import org.apache.xerces.xs.XSNamedMap; 24 import org.apache.xerces.xs.XSObject; 25 import org.w3c.dom.DOMConfiguration ; 26 import org.w3c.dom.DOMError ; 27 import org.w3c.dom.DOMErrorHandler ; 28 import org.w3c.dom.bootstrap.DOMImplementationRegistry ; 29 import org.w3c.dom.ls.LSParser ; 30 31 38 public class QueryXS implements DOMErrorHandler { 39 40 41 protected static final boolean DEFAULT_NAMESPACES = true; 42 43 44 protected static final boolean DEFAULT_VALIDATION = false; 45 46 47 protected static final boolean DEFAULT_SCHEMA_VALIDATION = false; 48 49 static LSParser builder; 50 public static void main(String [] argv) { 51 52 if (argv.length == 0) { 53 printUsage(); 54 System.exit(1); 55 } 56 57 try { 58 System.setProperty( 60 DOMImplementationRegistry.PROPERTY, 61 "org.apache.xerces.dom.DOMXSImplementationSourceImpl"); 62 DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); 63 64 XSImplementation impl = (XSImplementation) registry.getDOMImplementation("XS-Loader"); 65 66 XSLoader schemaLoader = impl.createXSLoader(null); 67 68 DOMConfiguration config = schemaLoader.getConfig(); 69 70 DOMErrorHandler errorHandler = new QueryXS(); 72 73 config.setParameter("error-handler", errorHandler); 75 76 config.setParameter("validate", Boolean.TRUE); 78 79 System.out.println("Parsing " + argv[0] + "..."); 81 XSModel model = schemaLoader.loadURI(argv[0]); 82 if (model != null) { 83 XSNamedMap map = model.getComponents(XSConstants.ELEMENT_DECLARATION); 85 if (map.getLength() != 0) { 86 System.out.println("*************************************************"); 87 System.out.println("* Global element declarations: {namespace} name "); 88 System.out.println("*************************************************"); 89 for (int i = 0; i < map.getLength(); i++) { 90 XSObject item = map.item(i); 91 System.out.println("{" + item.getNamespace() + "}" + item.getName()); 92 } 93 } 94 map = model.getComponents(XSConstants.ATTRIBUTE_DECLARATION); 96 if (map.getLength() != 0) { 97 System.out.println("*************************************************"); 98 System.out.println("* Global attribute declarations: {namespace} name"); 99 System.out.println("*************************************************"); 100 for (int i = 0; i < map.getLength(); i++) { 101 XSObject item = map.item(i); 102 System.out.println("{" + item.getNamespace() + "}" + item.getName()); 103 } 104 } 105 map = model.getComponents(XSConstants.TYPE_DEFINITION); 107 if (map.getLength() != 0) { 108 System.out.println("*************************************************"); 109 System.out.println("* Global type declarations: {namespace} name"); 110 System.out.println("*************************************************"); 111 for (int i = 0; i < map.getLength(); i++) { 112 XSObject item = map.item(i); 113 System.out.println("{" + item.getNamespace() + "}" + item.getName()); 114 } 115 } 116 117 map = model.getComponents(XSConstants.NOTATION_DECLARATION); 119 if (map.getLength() != 0) { 120 System.out.println("*************************************************"); 121 System.out.println("* Global notation declarations: {namespace} name"); 122 System.out.println("*************************************************"); 123 for (int i = 0; i < map.getLength(); i++) { 124 XSObject item = map.item(i); 125 System.out.println("{" + item.getNamespace() + "}" + item.getName()); 126 } 127 } 128 129 } 130 131 } catch (Exception ex) { 132 ex.printStackTrace(); 133 } 134 } 135 136 private static void printUsage() { 137 138 System.err.println("usage: java dom.QueryXS uri ..."); 139 System.err.println(); 140 141 } 143 144 public boolean handleError(DOMError error){ 145 short severity = error.getSeverity(); 146 if (severity == DOMError.SEVERITY_ERROR) { 147 System.out.println("[xs-error]: "+error.getMessage()); 148 } 149 150 if (severity == DOMError.SEVERITY_WARNING) { 151 System.out.println("[xs-warning]: "+error.getMessage()); 152 } 153 return true; 154 155 } 156 157 } 158 159 160 161 | Popular Tags |