1 /* 2 * The Apache Software License, Version 1.1 3 * 4 * 5 * Copyright (c) 1999, 2000 The Apache Software Foundation. All rights 6 * reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in 17 * the documentation and/or other materials provided with the 18 * distribution. 19 * 20 * 3. The end-user documentation included with the redistribution, 21 * if any, must include the following acknowledgment: 22 * "This product includes software developed by the 23 * Apache Software Foundation (http://www.apache.org/)." 24 * Alternately, this acknowledgment may appear in the software itself, 25 * if and wherever such third-party acknowledgments normally appear. 26 * 27 * 4. The names "Xerces" and "Apache Software Foundation" must 28 * not be used to endorse or promote products derived from this 29 * software without prior written permission. For written 30 * permission, please contact apache@apache.org. 31 * 32 * 5. Products derived from this software may not be called "Apache", 33 * nor may "Apache" appear in their name, without prior written 34 * permission of the Apache Software Foundation. 35 * 36 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 37 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 38 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 39 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 42 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 43 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 44 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 45 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 46 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 47 * SUCH DAMAGE. 48 * ==================================================================== 49 * 50 * This software consists of voluntary contributions made by many 51 * individuals on behalf of the Apache Software Foundation and was 52 * originally based on software copyright (c) 1999, International 53 * Business Machines, Inc., http://www.apache.org. For more 54 * information on the Apache Software Foundation, please see 55 * <http://www.apache.org/>. 56 */ 57 58 package dom; 59 60 import util.Arguments; 61 62 import java.io.OutputStreamWriter; 63 import java.io.PrintWriter; 64 import java.io.UnsupportedEncodingException; 65 66 import org.w3c.dom.Attr; 67 import org.w3c.dom.Document; 68 import org.w3c.dom.Element; 69 import org.w3c.dom.NamedNodeMap; 70 import org.w3c.dom.Node; 71 import org.w3c.dom.NodeList; 72 73 74 /** 75 * A sample DOM filter. This sample program illustrates how to 76 * use the Document#getElementsByTagName() method to quickly 77 * and easily locate elements by name. 78 * 79 * @version $Id: DOMFilter.java,v 1.3 2000/10/07 18:06:52 markd Exp $ 80 */ 81 public class DOMFilter { 82 83 // 84 // Constants 85 // 86 87 /** Default parser name. */ 88 private static final String 89 DEFAULT_PARSER_NAME = "dom.wrappers.DOMParser"; 90 91 private static boolean setValidation = false; //defaults 92 private static boolean setNameSpaces = true; 93 private static boolean setSchemaSupport = true; 94 private static boolean setDeferredDOM = true; 95 96 97 98 // 99 // Public static methods 100 // 101 102 /** Prints the specified elements in the given document. */ 103 public static void print(String parserWrapperName, String uri, 104 String elementName, String attributeName) { 105 106 try { 107 // parse document 108 DOMParserWrapper parser = 109 (DOMParserWrapper)Class.forName(parserWrapperName).newInstance(); 110 111 parser.setFeature( "http://apache.org/xml/features/dom/defer-node-expansion", 112 setDeferredDOM ); 113 parser.setFeature( "http://xml.org/sax/features/validation", 114 setValidation ); 115 parser.setFeature( "http://xml.org/sax/features/namespaces", 116 setNameSpaces ); 117 parser.setFeature( "http://apache.org/xml/features/validation/schema", 118 setSchemaSupport ); 119 120 121 Document document = parser.parse(uri); 122 123 // get elements that match 124 NodeList elements = document.getElementsByTagName(elementName); 125 126 // print nodes 127 print(elements, attributeName); 128 } catch (Exception e) { 129 e.printStackTrace(System.err); 130 } 131 132 } // print(String,String,String,String) 133 134 // 135 // Private static methods 136 // 137 138 /** 139 * Prints the contents of the given element node list. If the given 140 * attribute name is non-null, then all of the elements are printed 141 * out 142 */ 143 private static void print(NodeList elements, String attributeName) { 144 145 // is there anything to do? 146 if (elements == null) { 147 return; 148 } 149 150 // print all elements 151 if (attributeName == null) { 152 int elementCount = elements.getLength(); 153 for (int i = 0; i < elementCount; i++) { 154 Element element = (Element)elements.item(i); 155 print(element, element.getAttributes()); 156 } 157 } 158 159 // print elements with given attribute name 160 else { 161 int elementCount = elements.getLength(); 162 for (int i = 0; i < elementCount; i++) { 163 Element element = (Element)elements.item(i); 164 NamedNodeMap attributes = element.getAttributes(); 165 if (attributes.getNamedItem(attributeName) != null) { 166 print(element, attributes); 167 } 168 } 169 } 170 171 } // print(NodeList,String) 172 173 /** Prints the specified element. */ 174 private static void print(Element element, NamedNodeMap attributes) { 175 176 System.out.print('<'); 177 System.out.print(element.getNodeName()); 178 if (attributes != null) { 179 int attributeCount = attributes.getLength(); 180 for (int i = 0; i < attributeCount; i++) { 181 Attr attribute = (Attr)attributes.item(i); 182 System.out.print(' '); 183 System.out.print(attribute.getNodeName()); 184 System.out.print("=\""); 185 System.out.print(normalize(attribute.getNodeValue())); 186 System.out.print('"'); 187 } 188 } 189 System.out.println('>'); 190 191 } // print(Element,NamedNodeMap) 192 193 /** Normalizes the given string. */ 194 private static String normalize(String s) { 195 StringBuffer str = new StringBuffer(); 196 197 int len = (s != null) ? s.length() : 0; 198 for (int i = 0; i < len; i++) { 199 char ch = s.charAt(i); 200 switch (ch) { 201 case '<': { 202 str.append("<"); 203 break; 204 } 205 case '>': { 206 str.append(">"); 207 break; 208 } 209 case '&': { 210 str.append("&"); 211 break; 212 } 213 case '"': { 214 str.append("""); 215 break; 216 } 217 case '\r': 218 case '\n': { 219 str.append("&#"); 220 str.append(Integer.toString(ch)); 221 str.append(';'); 222 break; 223 } 224 default: { 225 str.append(ch); 226 } 227 } 228 } 229 230 return str.toString(); 231 232 } // normalize(String):String 233 234 // 235 // Main 236 // 237 238 /** Main program entry point. */ 239 public static void main(String argv[]) { 240 241 Arguments argopt = new Arguments(); 242 argopt.setUsage( new String[] 243 { "usage: java dom.DOMFilter (options) uri ...","", 244 "options:", 245 " -p name Specify DOM parser wrapper by name.", 246 " -e name Specify element name to search for. Default is \"*\".", 247 " -a name Specify attribute name of specified elements.", 248 " -p name Specify DOM parser wrapper by name.", 249 " -n | -N Turn on/off namespace [default=on]", 250 " -v | -V Turn on/off validation [default=on]", 251 " -s | -S Turn on/off Schema support [default=on]", 252 " -d | -D Turn on/off deferred DOM [default=on]", 253 " -h This help screen."} ); 254 255 // is there anything to do? 256 if (argv.length == 0) { 257 argopt.printUsage(); 258 System.exit(1); 259 } 260 261 // vars 262 String parserName = DEFAULT_PARSER_NAME; 263 String elementName = "*"; // all elements 264 String attributeName = null; 265 266 267 ///// 268 269 argopt.parseArgumentTokens(argv , new char[] { 'p', 'e', 'a'} ); 270 int c; 271 String arg = null; 272 while ( ( arg = argopt.getlistFiles() ) != null ) { 273 outer: 274 while ( (c = argopt.getArguments()) != -1 ){ 275 switch (c) { 276 case 'v': 277 setValidation = true; 278 break; 279 case 'V': 280 setValidation = false; 281 break; 282 case 'N': 283 setNameSpaces = false; 284 break; 285 case 'n': 286 setNameSpaces = true; 287 break; 288 case 'p': 289 parserName = argopt.getStringParameter(); 290 break; 291 case 'd': 292 setDeferredDOM = true; 293 break; 294 case 'D': 295 setDeferredDOM = false; 296 break; 297 case 's': 298 setSchemaSupport = true; 299 break; 300 case 'S': 301 setSchemaSupport = false; 302 break; 303 case 'e': 304 elementName = argopt.getStringParameter(); 305 break; 306 case 'a': 307 attributeName = argopt.getStringParameter(); 308 break; 309 case '?': 310 case 'h': 311 case '-': 312 argopt.printUsage(); 313 System.exit(1); 314 break; 315 case -1: 316 break outer; 317 default: 318 break; 319 } 320 } 321 // print uri 322 System.err.println(arg+':'); 323 print(parserName, arg, elementName, attributeName); 324 } 325 } // main(String[]) 326 327 } // class DOMFilter 328