1 16 17 package dom; 18 19 import java.io.PrintWriter ; 20 21 import org.w3c.dom.Attr ; 22 import org.w3c.dom.Document ; 23 import org.w3c.dom.Element ; 24 import org.w3c.dom.NamedNodeMap ; 25 import org.w3c.dom.NodeList ; 26 import org.xml.sax.SAXException ; 27 import org.xml.sax.SAXParseException ; 28 29 39 public class GetElementsByTagName { 40 41 45 47 48 protected static final String NAMESPACES_FEATURE_ID = "http://xml.org/sax/features/namespaces"; 49 50 51 protected static final String VALIDATION_FEATURE_ID = "http://xml.org/sax/features/validation"; 52 53 54 protected static final String SCHEMA_VALIDATION_FEATURE_ID = "http://apache.org/xml/features/validation/schema"; 55 56 57 protected static final String SCHEMA_FULL_CHECKING_FEATURE_ID = "http://apache.org/xml/features/validation/schema-full-checking"; 58 59 60 protected static final String VALIDATE_ANNOTATIONS_ID = "http://apache.org/xml/features/validate-annotations"; 61 62 63 protected static final String DYNAMIC_VALIDATION_FEATURE_ID = "http://apache.org/xml/features/validation/dynamic"; 64 65 66 protected static final String XINCLUDE_FEATURE_ID = "http://apache.org/xml/features/xinclude"; 67 68 69 protected static final String XINCLUDE_FIXUP_BASE_URIS_FEATURE_ID = "http://apache.org/xml/features/xinclude/fixup-base-uris"; 70 71 72 protected static final String XINCLUDE_FIXUP_LANGUAGE_FEATURE_ID = "http://apache.org/xml/features/xinclude/fixup-language"; 73 74 76 77 protected static final String DEFAULT_PARSER_NAME = "dom.wrappers.Xerces"; 78 79 80 protected static final String DEFAULT_ELEMENT_NAME = "*"; 81 82 83 protected static final boolean DEFAULT_NAMESPACES = true; 84 85 86 protected static final boolean DEFAULT_VALIDATION = false; 87 88 89 protected static final boolean DEFAULT_SCHEMA_VALIDATION = false; 90 91 92 protected static final boolean DEFAULT_SCHEMA_FULL_CHECKING = false; 93 94 95 protected static final boolean DEFAULT_VALIDATE_ANNOTATIONS = false; 96 97 98 protected static final boolean DEFAULT_DYNAMIC_VALIDATION = false; 99 100 101 protected static final boolean DEFAULT_XINCLUDE = false; 102 103 104 protected static final boolean DEFAULT_XINCLUDE_FIXUP_BASE_URIS = true; 105 106 107 protected static final boolean DEFAULT_XINCLUDE_FIXUP_LANGUAGE = true; 108 109 113 114 public static void print(PrintWriter out, Document document, 115 String elementName, String attributeName) { 116 117 NodeList elements = document.getElementsByTagName(elementName); 119 120 if (elements == null) { 122 return; 123 } 124 125 if (attributeName == null) { 127 int elementCount = elements.getLength(); 128 for (int i = 0; i < elementCount; i++) { 129 Element element = (Element )elements.item(i); 130 print(out, element, element.getAttributes()); 131 } 132 } 133 134 else { 136 int elementCount = elements.getLength(); 137 for (int i = 0; i < elementCount; i++) { 138 Element element = (Element )elements.item(i); 139 NamedNodeMap attributes = element.getAttributes(); 140 if (attributes.getNamedItem(attributeName) != null) { 141 print(out, element, attributes); 142 } 143 } 144 } 145 146 } 148 152 153 protected static void print(PrintWriter out, 154 Element element, NamedNodeMap attributes) { 155 156 out.print('<'); 157 out.print(element.getNodeName()); 158 if (attributes != null) { 159 int attributeCount = attributes.getLength(); 160 for (int i = 0; i < attributeCount; i++) { 161 Attr attribute = (Attr )attributes.item(i); 162 out.print(' '); 163 out.print(attribute.getNodeName()); 164 out.print("=\""); 165 out.print(normalize(attribute.getNodeValue())); 166 out.print('"'); 167 } 168 } 169 out.println('>'); 170 out.flush(); 171 172 } 174 175 protected static String normalize(String s) { 176 StringBuffer str = new StringBuffer (); 177 178 int len = (s != null) ? s.length() : 0; 179 for (int i = 0; i < len; i++) { 180 char ch = s.charAt(i); 181 switch (ch) { 182 case '<': { 183 str.append("<"); 184 break; 185 } 186 case '>': { 187 str.append(">"); 188 break; 189 } 190 case '&': { 191 str.append("&"); 192 break; 193 } 194 case '"': { 195 str.append("""); 196 break; 197 } 198 case '\r': 199 case '\n': { 200 str.append("&#"); 201 str.append(Integer.toString(ch)); 202 str.append(';'); 203 break; 204 } 205 default: { 206 str.append(ch); 207 } 208 } 209 } 210 211 return str.toString(); 212 213 } 215 219 220 public static void main(String argv[]) { 221 222 if (argv.length == 0) { 224 printUsage(); 225 System.exit(1); 226 } 227 228 Counter counter = new Counter(); 230 PrintWriter out = new PrintWriter (System.out); 231 ParserWrapper parser = null; 232 String elementName = DEFAULT_ELEMENT_NAME; 233 String attributeName = null; 234 boolean namespaces = DEFAULT_NAMESPACES; 235 boolean validation = DEFAULT_VALIDATION; 236 boolean schemaValidation = DEFAULT_SCHEMA_VALIDATION; 237 boolean schemaFullChecking = DEFAULT_SCHEMA_FULL_CHECKING; 238 boolean validateAnnotations = DEFAULT_VALIDATE_ANNOTATIONS; 239 boolean dynamicValidation = DEFAULT_DYNAMIC_VALIDATION; 240 boolean xincludeProcessing = DEFAULT_XINCLUDE; 241 boolean xincludeFixupBaseURIs = DEFAULT_XINCLUDE_FIXUP_BASE_URIS; 242 boolean xincludeFixupLanguage = DEFAULT_XINCLUDE_FIXUP_LANGUAGE; 243 244 for (int i = 0; i < argv.length; i++) { 246 String arg = argv[i]; 247 if (arg.startsWith("-")) { 248 String option = arg.substring(1); 249 if (option.equals("p")) { 250 if (++i == argv.length) { 252 System.err.println("error: Missing argument to -p option."); 253 } 254 String parserName = argv[i]; 255 256 try { 258 parser = (ParserWrapper)Class.forName(parserName).newInstance(); 259 } 260 catch (Exception e) { 261 parser = null; 262 System.err.println("error: Unable to instantiate parser ("+parserName+")"); 263 } 264 continue; 265 } 266 if (option.equals("e")) { 267 if (++i == argv.length) { 268 System.err.println("error: Missing argument to -e option."); 269 } 270 elementName = argv[i]; 271 continue; 272 } 273 if (option.equals("a")) { 274 if (++i == argv.length) { 275 System.err.println("error: Missing argument to -a option."); 276 } 277 attributeName = argv[i]; 278 continue; 279 } 280 if (option.equalsIgnoreCase("n")) { 281 namespaces = option.equals("n"); 282 continue; 283 } 284 if (option.equalsIgnoreCase("v")) { 285 validation = option.equals("v"); 286 continue; 287 } 288 if (option.equalsIgnoreCase("s")) { 289 schemaValidation = option.equals("s"); 290 continue; 291 } 292 if (option.equalsIgnoreCase("f")) { 293 schemaFullChecking = option.equals("f"); 294 continue; 295 } 296 if (option.equalsIgnoreCase("va")) { 297 validateAnnotations = option.equals("va"); 298 continue; 299 } 300 if (option.equalsIgnoreCase("dv")) { 301 dynamicValidation = option.equals("dv"); 302 continue; 303 } 304 if (option.equalsIgnoreCase("xi")) { 305 xincludeProcessing = option.equals("xi"); 306 continue; 307 } 308 if (option.equalsIgnoreCase("xb")) { 309 xincludeFixupBaseURIs = option.equals("xb"); 310 continue; 311 } 312 if (option.equalsIgnoreCase("xl")) { 313 xincludeFixupLanguage = option.equals("xl"); 314 continue; 315 } 316 if (option.equals("h")) { 317 printUsage(); 318 continue; 319 } 320 } 321 322 if (parser == null) { 324 325 try { 327 parser = (ParserWrapper)Class.forName(DEFAULT_PARSER_NAME).newInstance(); 328 } 329 catch (Exception e) { 330 System.err.println("error: Unable to instantiate parser ("+DEFAULT_PARSER_NAME+")"); 331 continue; 332 } 333 } 334 335 try { 337 parser.setFeature(NAMESPACES_FEATURE_ID, namespaces); 338 } 339 catch (SAXException e) { 340 System.err.println("warning: Parser does not support feature ("+NAMESPACES_FEATURE_ID+")"); 341 } 342 try { 343 parser.setFeature(VALIDATION_FEATURE_ID, validation); 344 } 345 catch (SAXException e) { 346 System.err.println("warning: Parser does not support feature ("+VALIDATION_FEATURE_ID+")"); 347 } 348 try { 349 parser.setFeature(SCHEMA_VALIDATION_FEATURE_ID, schemaValidation); 350 } 351 catch (SAXException e) { 352 System.err.println("warning: Parser does not support feature ("+SCHEMA_VALIDATION_FEATURE_ID+")"); 353 } 354 try { 355 parser.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, schemaFullChecking); 356 } 357 catch (SAXException e) { 358 System.err.println("warning: Parser does not support feature ("+SCHEMA_FULL_CHECKING_FEATURE_ID+")"); 359 } 360 try { 361 parser.setFeature(VALIDATE_ANNOTATIONS_ID, validateAnnotations); 362 } 363 catch (SAXException e) { 364 System.err.println("warning: Parser does not support feature ("+VALIDATE_ANNOTATIONS_ID+")"); 365 } 366 try { 367 parser.setFeature(DYNAMIC_VALIDATION_FEATURE_ID, dynamicValidation); 368 } 369 catch (SAXException e) { 370 System.err.println("warning: Parser does not support feature ("+DYNAMIC_VALIDATION_FEATURE_ID+")"); 371 } 372 try { 373 parser.setFeature(XINCLUDE_FEATURE_ID, xincludeProcessing); 374 } 375 catch (SAXException e) { 376 System.err.println("warning: Parser does not support feature ("+XINCLUDE_FEATURE_ID+")"); 377 } 378 try { 379 parser.setFeature(XINCLUDE_FIXUP_BASE_URIS_FEATURE_ID, xincludeFixupBaseURIs); 380 } 381 catch (SAXException e) { 382 System.err.println("warning: Parser does not support feature ("+XINCLUDE_FIXUP_BASE_URIS_FEATURE_ID+")"); 383 } 384 try { 385 parser.setFeature(XINCLUDE_FIXUP_LANGUAGE_FEATURE_ID, xincludeFixupLanguage); 386 } 387 catch (SAXException e) { 388 System.err.println("warning: Parser does not support feature ("+XINCLUDE_FIXUP_LANGUAGE_FEATURE_ID+")"); 389 } 390 391 try { 393 Document document = parser.parse(arg); 394 GetElementsByTagName.print(out, document, elementName, attributeName); 395 } 396 catch (SAXParseException e) { 397 } 399 catch (Exception e) { 400 System.err.println("error: Parse error occurred - "+e.getMessage()); 401 if (e instanceof SAXException ) { 402 Exception nested = ((SAXException )e).getException(); 403 if (nested != null) { 404 e = nested; 405 } 406 } 407 e.printStackTrace(System.err); 408 } 409 } 410 411 } 413 417 418 private static void printUsage() { 419 420 System.err.println("usage: java dom.GetElementsByTagName (options) uri ..."); 421 System.err.println(); 422 423 System.err.println("options:"); 424 System.err.println(" -p name Select parser by name."); 425 System.err.println(" -e name Specify element name for search."); 426 System.err.println(" -a name Specify attribute name for specified elements."); 427 System.err.println(" -n | -N Turn on/off namespace processing."); 428 System.err.println(" -v | -V Turn on/off validation."); 429 System.err.println(" -s | -S Turn on/off Schema validation support."); 430 System.err.println(" NOTE: Not supported by all parsers."); 431 System.err.println(" -f | -F Turn on/off Schema full checking."); 432 System.err.println(" NOTE: Requires use of -s and not supported by all parsers."); 433 System.err.println(" -va | -VA Turn on/off validation of schema annotations."); 434 System.err.println(" NOTE: Requires use of -s and not supported by all parsers."); 435 System.err.println(" -dv | -DV Turn on/off dynamic validation."); 436 System.err.println(" NOTE: Not supported by all parsers."); 437 System.err.println(" -xi | -XI Turn on/off XInclude processing."); 438 System.err.println(" NOTE: Not supported by all parsers."); 439 System.err.println(" -xb | -XB Turn on/off base URI fixup during XInclude processing."); 440 System.err.println(" NOTE: Requires use of -xi and not supported by all parsers."); 441 System.err.println(" -xl | -XL Turn on/off language fixup during XInclude processing."); 442 System.err.println(" NOTE: Requires use of -xi and not supported by all parsers."); 443 System.err.println(" -h This help screen."); 444 System.err.println(); 445 446 System.err.println("defaults:"); 447 System.err.println(" Parser: "+DEFAULT_PARSER_NAME); 448 System.err.println(" Element: "+DEFAULT_ELEMENT_NAME); 449 System.err.print(" Namespaces: "); 450 System.err.println(DEFAULT_NAMESPACES ? "on" : "off"); 451 System.err.print(" Validation: "); 452 System.err.println(DEFAULT_VALIDATION ? "on" : "off"); 453 System.err.print(" Schema: "); 454 System.err.println(DEFAULT_SCHEMA_VALIDATION ? "on" : "off"); 455 System.err.print(" Schema full checking: "); 456 System.err.println(DEFAULT_SCHEMA_FULL_CHECKING ? "on" : "off"); 457 System.err.print(" Dynamic: "); 458 System.err.println(DEFAULT_DYNAMIC_VALIDATION ? "on" : "off"); 459 System.err.print(" XInclude: "); 460 System.err.println(DEFAULT_XINCLUDE ? "on" : "off"); 461 System.err.print(" XInclude base URI fixup: "); 462 System.err.println(DEFAULT_XINCLUDE_FIXUP_BASE_URIS ? "on" : "off"); 463 System.err.print(" XInclude language fixup: "); 464 System.err.println(DEFAULT_XINCLUDE_FIXUP_LANGUAGE ? "on" : "off"); 465 466 } 468 } | Popular Tags |