KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SAXDump


1 import org.xml.sax.*;
2 import org.xml.sax.ext.*;
3 import org.xml.sax.helpers.*;
4
5
6 /**
7  * Poke at parsers and find out what features and properties
8  * they support, using the SAX2 (+extensions) standard list.
9  *
10  * @author David Brownell
11  * @version $Id: SAXDump.java,v 1.3 2002/07/29 23:07:40 dbrownell Exp $
12  */

13 public class SAXDump
14 {
15     private SAXDump () { }
16     
17     /**
18      * Pass a list of parser classes; each will be scanned.
19      * If not, parsers from a built-in list are scanned.
20      */

21     public static void main (String JavaDoc argv [])
22     {
23     if (argv.length == 0)
24         argv = new String JavaDoc [] {
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 JavaDoc FEATURE_URI
39     = "http://xml.org/sax/features/";
40
41     private static final String JavaDoc PROPERTY_URI
42     = "http://xml.org/sax/properties/";
43     
44     //////////////////////////////////////////////////////////////////
45

46     private static void
47     checkFeature (String JavaDoc id, XMLReader producer)
48     {
49     int state = 0;
50
51     try {
52         boolean value;
53
54         // align to longest id...
55
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         // changes value and leaves it
76

77     } catch (SAXNotSupportedException e) {
78         switch (state) {
79           case 0: System.out.println ("(can't read now)"); break;
80         /* bogus_1 means can't reassign the default;
81          * probably intended to be read/write
82          */

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         /* bogus_2 means the wrong exception was thrown */
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     // must-have, default=false
103
checkFeature ("namespace-prefixes", producer);
104     // must-have, default=true
105
checkFeature ("namespaces", producer);
106
107     // lots of optional features
108
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     //////////////////////////////////////////////////////////////////
120

121     private static void
122     checkProperty (String JavaDoc id, XMLReader producer, Object JavaDoc newValue)
123     {
124     int state = 0;
125
126     try {
127         Object JavaDoc value;
128
129         // align to longest id...
130
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         // changes value and leaves it
151

152     } catch (SAXNotSupportedException e) {
153         switch (state) {
154           case 0: System.out.println ("(can't read now)"); break;
155         /* bogus_1 means can't reassign the default;
156          * probably intended to be read/write
157          */

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         /* bogus_2 means the wrong exception was thrown */
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     // all properties are optional
180
checkProperty ("declaration-handler", producer, handler);
181
182     // To check the dom-node property, we need an instance
183
// of an org.w3c.dom.Node. Any one will do; but since
184
// we don't know what is installed, we try four likely
185
// suspects.
186

187     String JavaDoc[] 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 JavaDoc node = null;
194     for (int i = 0; i < domClassNames.length; i++) {
195         try {
196             Class JavaDoc domClass = Class.forName(domClassNames[i]);
197             node = (org.w3c.dom.Node JavaDoc) domClass.newInstance();
198         }
199         catch (ClassNotFoundException JavaDoc e) { continue; }
200         catch (InstantiationException JavaDoc e) { continue; }
201         catch (IllegalAccessException JavaDoc e) { continue; }
202         catch (ClassCastException JavaDoc e) { continue; }
203     }
204
205     // If none of the classes yielded a workable instance,
206
// just skip the test.
207
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     //////////////////////////////////////////////////////////////////
216

217     private static void
218     checkParser (String JavaDoc 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 JavaDoc 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