1 57 58 package dom; 59 60 import org.enhydra.apache.xerces.dom.TextImpl; 61 import org.w3c.dom.Document ; 62 import org.w3c.dom.NamedNodeMap ; 63 import org.w3c.dom.Node ; 64 import org.w3c.dom.NodeList ; 65 66 import util.Arguments; 67 68 74 public class DOMCount { 75 76 80 81 private static final String 82 DEFAULT_PARSER_NAME = "dom.wrappers.DOMParser"; 83 84 private static boolean setValidation = false; private static boolean setNameSpaces = true; 86 private static boolean setSchemaSupport = true; 87 private static boolean setSchemaFullSupport = false; 88 private static boolean setDeferredDOM = true; 89 90 91 92 96 97 private long elements; 98 99 100 private long attributes; 101 102 103 private long characters; 104 105 106 private long ignorableWhitespace; 107 108 109 113 114 public static void count(String parserWrapperName, String uri) { 115 116 try { 117 DOMParserWrapper parser = 118 (DOMParserWrapper)Class.forName(parserWrapperName).newInstance(); 119 DOMCount counter = new DOMCount(); 120 long before = System.currentTimeMillis(); 121 parser.setFeature( "http://apache.org/xml/features/dom/defer-node-expansion", 122 123 setDeferredDOM ); 124 parser.setFeature( "http://xml.org/sax/features/validation", 125 setValidation ); 126 parser.setFeature( "http://xml.org/sax/features/namespaces", 127 setNameSpaces ); 128 parser.setFeature( "http://apache.org/xml/features/validation/schema", 129 setSchemaSupport ); 130 parser.setFeature( "http://apache.org/xml/features/validation/schema-full-checking", 131 setSchemaFullSupport ); 132 133 Document document = parser.parse(uri); 134 counter.traverse(document); 135 long after = System.currentTimeMillis(); 136 counter.printResults(uri, after - before); 137 } catch (org.xml.sax.SAXParseException spe) { 138 } catch (org.xml.sax.SAXNotRecognizedException ex ){ 139 } catch (org.xml.sax.SAXNotSupportedException ex ){ 140 } catch (org.xml.sax.SAXException se) { 141 if (se.getException() != null) 142 se.getException().printStackTrace(System.err); 143 else 144 se.printStackTrace(System.err); 145 } catch (Exception e) { 146 e.printStackTrace(System.err); 147 } 148 149 } 151 155 156 public void traverse(Node node) { 157 158 if (node == null) { 160 return; 161 } 162 163 int type = node.getNodeType(); 164 switch (type) { 165 case Node.DOCUMENT_NODE: { 167 elements = 0; 168 attributes = 0; 169 characters = 0; 170 ignorableWhitespace = 0; 171 traverse(((Document )node).getDocumentElement()); 172 break; 173 } 174 175 case Node.ELEMENT_NODE: { 177 elements++; 178 NamedNodeMap attrs = node.getAttributes(); 179 if (attrs != null) { 180 attributes += attrs.getLength(); 181 } 182 NodeList children = node.getChildNodes(); 183 if (children != null) { 184 int len = children.getLength(); 185 for (int i = 0; i < len; i++) { 186 traverse(children.item(i)); 187 } 188 } 189 break; 190 } 191 192 case Node.ENTITY_REFERENCE_NODE: { 194 NodeList children = node.getChildNodes(); 195 if (children != null) { 196 int len = children.getLength(); 197 for (int i = 0; i < len; i++) { 198 traverse(children.item(i)); 199 } 200 } 201 break; 202 } 203 204 case Node.CDATA_SECTION_NODE: { 206 characters += node.getNodeValue().length(); 207 break; 208 } 209 case Node.TEXT_NODE: { 210 if (node instanceof TextImpl) { 211 if (((TextImpl)node).isIgnorableWhitespace()) 212 ignorableWhitespace += node.getNodeValue().length(); 213 else 214 characters += node.getNodeValue().length(); 215 } else 216 characters += node.getNodeValue().length(); 217 break; 218 } 219 } 220 221 } 223 224 public void printResults(String uri, long time) { 225 226 System.out.print(uri); 228 System.out.print(": "); 229 System.out.print(time); 230 System.out.print(" ms ("); 231 System.out.print(elements); 232 System.out.print(" elems, "); 233 System.out.print(attributes); 234 System.out.print(" attrs, "); 235 System.out.print(ignorableWhitespace); 236 System.out.print(" spaces, "); 237 System.out.print(characters); 238 System.out.print(" chars)"); 239 System.out.println(); 240 241 } 243 247 248 public static void main(String argv[]) { 249 250 Arguments argopt = new Arguments(); 251 argopt.setUsage( new String [] { 252 "usage: java dom.DOMCount (options) uri ...", 253 "", 254 "options:", 255 " -p name Specify DOM parser wrapper by name.", 256 " -n | -N Turn on/off namespace [default=on]", 257 " -v | -V Turn on/off validation [default=off]", 258 " -s | -S Turn on/off Schema support [default=on]", 259 " -f | -F Turn on/off Schema full consraint checking [default=off]", 260 " -d | -D Turn on/off deferred DOM [default=on]", 261 " -h This help screen."} ); 262 263 264 if (argv.length == 0) { 266 argopt.printUsage(); 267 System.exit(1); 268 } 269 270 String parserName = DEFAULT_PARSER_NAME; 272 273 argopt.parseArgumentTokens(argv , new char[] { 'p'} ); 274 275 int c; 276 String arg = null; 277 while ( ( arg = argopt.getlistFiles() ) != null ) { 278 outer: 279 while ( (c = argopt.getArguments()) != -1 ){ 280 switch (c) { 281 case 'v': 282 setValidation = true; 283 break; 285 case 'V': 286 setValidation = false; 287 break; 289 case 'N': 290 setNameSpaces = false; 291 break; 292 case 'n': 293 setNameSpaces = true; 294 break; 295 case 'p': 296 parserName = argopt.getStringParameter(); 298 break; 300 case 'd': 301 setDeferredDOM = true; 302 break; 303 case 'D': 304 setDeferredDOM = false; 305 break; 306 case 's': 307 setSchemaSupport = true; 309 break; 310 case 'S': 311 setSchemaSupport = false; 313 break; 314 case 'f': 315 setSchemaFullSupport = true; 316 break; 317 case 'F': 318 setSchemaFullSupport = false; 319 break; 320 case '?': 321 case 'h': 322 case '-': 323 argopt.printUsage(); 324 System.exit(1); 325 break; 326 case -1: 327 break outer; 329 default: 330 331 break; 332 } 333 } 334 335 count(parserName, arg ); } 337 338 } 340 } | Popular Tags |