1 import org.xml.sax.*; 2 import org.xml.sax.ext.*; 3 import org.xml.sax.helpers.*; 4 5 6 13 public class SAXDump 14 { 15 private SAXDump () { } 16 17 21 public static void main (String argv []) 22 { 23 if (argv.length == 0) 24 argv = new String [] { 25 "com.jclark.xml.sax.SAX2Driver", 26 "gnu.xml.aelfred2.SAXDriver", 27 "gnu.xml.aelfred2.XmlReader", 28 "gnu.xml.util.DomParser", 29 "oracle.xml.parser.v2.SAXParser", 30 "org.apache.crimson.parser.XMLReaderImpl", 31 "org.apache.xerces.parsers.SAXParser", 32 }; 33 34 for (int i = 0; i < argv.length; i++) 35 checkParser (argv [i]); 36 } 37 38 private static final String FEATURE_URI 39 = "http://xml.org/sax/features/"; 40 41 private static final String PROPERTY_URI 42 = "http://xml.org/sax/properties/"; 43 44 46 private static void 47 checkFeature (String id, XMLReader producer) 48 { 49 int state = 0; 50 51 try { 52 boolean value; 53 54 final int align = 35; 56 57 for (int i = align - id.length (); i > 0; i--) 58 System.out.print (' '); 59 System.out.print (id); 60 System.out.print (": "); 61 62 id = FEATURE_URI + id; 63 64 value = producer.getFeature (id); 65 System.out.print (value); 66 System.out.print (", "); 67 state = 1; 68 69 producer.setFeature (id, value); 70 state = 2; 71 72 producer.setFeature (id, !value); 73 System.out.println ("read and write"); 74 75 77 } catch (SAXNotSupportedException e) { 78 switch (state) { 79 case 0: System.out.println ("(can't read now)"); break; 80 83 case 1: System.out.println ("bogus_1"); break; 84 case 2: System.out.println ("readonly"); break; 85 } 86 87 } catch (SAXNotRecognizedException e) { 88 if (state == 0) 89 System.out.println ("(unrecognized)"); 90 else 91 92 System.out.println ("bogus_2"); 93 } 94 } 95 96 private static void 97 showFeatures (XMLReader producer) 98 { 99 System.out.println ("FEATURES for " 100 + producer.getClass ().getName ()); 101 102 checkFeature ("namespace-prefixes", producer); 104 checkFeature ("namespaces", producer); 106 107 checkFeature ("external-general-entities", producer); 109 checkFeature ("external-parameter-entities", producer); 110 checkFeature ("is-standalone", producer); 111 checkFeature ("lexical-handler/parameter-entities", producer); 112 checkFeature ("resolve-dtd-uris", producer); 113 checkFeature ("string-interning", producer); 114 checkFeature ("use-attributes2", producer); 115 checkFeature ("use-locator2", producer); 116 checkFeature ("validation", producer); 117 } 118 119 121 private static void 122 checkProperty (String id, XMLReader producer, Object newValue) 123 { 124 int state = 0; 125 126 try { 127 Object value; 128 129 final int align = 20; 131 132 for (int i = align - id.length (); i > 0; i--) 133 System.out.print (' '); 134 System.out.print (id); 135 System.out.print (": "); 136 137 id = PROPERTY_URI + id; 138 139 value = producer.getProperty (id); 140 System.out.print (value); 141 System.out.print (", "); 142 state = 1; 143 144 producer.setProperty (id, value); 145 state = 2; 146 147 producer.setProperty (id, newValue); 148 System.out.println ("read and write"); 149 150 152 } catch (SAXNotSupportedException e) { 153 switch (state) { 154 case 0: System.out.println ("(can't read now)"); break; 155 158 case 1: System.out.println ("bogus_1"); break; 159 case 2: System.out.println ("readonly"); break; 160 } 161 162 } catch (SAXNotRecognizedException e) { 163 if (state == 0) 164 System.out.println ("(unrecognized)"); 165 else 166 167 System.out.println ("bogus_2"); 168 } 169 } 170 171 private static void 172 showProperties (XMLReader producer) 173 { 174 System.out.println ("PROPERTIES for " 175 + producer.getClass ().getName ()); 176 177 DefaultHandler2 handler = new DefaultHandler2 (); 178 179 checkProperty ("declaration-handler", producer, handler); 181 182 187 String [] domClassNames = { 188 "org.apache.crimson.tree.XmlDocument", 189 "org.apache.xerces.dom.DocumentImpl", 190 "gnu.xml.dom.DomDocument", 191 "oracle.xml.parser.v2.XMLDocument", 192 }; 193 org.w3c.dom.Node node = null; 194 for (int i = 0; i < domClassNames.length; i++) { 195 try { 196 Class domClass = Class.forName(domClassNames[i]); 197 node = (org.w3c.dom.Node ) domClass.newInstance(); 198 } 199 catch (ClassNotFoundException e) { continue; } 200 catch (InstantiationException e) { continue; } 201 catch (IllegalAccessException e) { continue; } 202 catch (ClassCastException e) { continue; } 203 } 204 205 if (node != null) { 208 checkProperty ("dom-node", producer, node); 209 } 210 211 checkProperty ("lexical-handler", producer, handler); 212 checkProperty ("xml-string", producer, "<root/>"); 213 } 214 215 217 private static void 218 checkParser (String classname) 219 { 220 XMLReader producer = null; 221 222 try { 223 producer = XMLReaderFactory.createXMLReader (classname); 224 225 showFeatures (producer); 226 System.out.println (""); 227 showProperties (producer); 228 229 } catch (Exception e) { 230 if (producer == null) 231 System.err.println ("(can't create " + classname + ")"); 232 else 233 e.printStackTrace (); 234 } finally { 235 System.out.println (""); 236 } 237 } 238 } 239 | Popular Tags |