kenai_tools 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,351 @@
1
+ // SAXTest.java - test application for SAX2
2
+
3
+ import java.io.IOException;
4
+
5
+ import java.net.MalformedURLException;
6
+ import java.net.URL;
7
+
8
+ import org.xml.sax.Attributes;
9
+ import org.xml.sax.ContentHandler;
10
+ import org.xml.sax.ErrorHandler;
11
+ import org.xml.sax.Locator;
12
+ import org.xml.sax.SAXException;
13
+ import org.xml.sax.SAXNotRecognizedException;
14
+ import org.xml.sax.SAXNotSupportedException;
15
+ import org.xml.sax.SAXParseException;
16
+ import org.xml.sax.XMLReader;
17
+
18
+ import org.xml.sax.helpers.XMLReaderFactory;
19
+
20
+
21
+ /**
22
+ * Test class for SAX2.
23
+ */
24
+ public class SAXTest implements ContentHandler, ErrorHandler
25
+ {
26
+
27
+
28
+ ////////////////////////////////////////////////////////////////////
29
+ // Main app.
30
+ ////////////////////////////////////////////////////////////////////
31
+
32
+
33
+ /**
34
+ * Main application entry point.
35
+ */
36
+ public static void main (String args[])
37
+ {
38
+
39
+ System.out.println("************************************" +
40
+ "************************************");
41
+ System.out.println("* Testing SAX2");
42
+ System.out.println("************************************" +
43
+ "************************************");
44
+ System.out.print("\n");
45
+
46
+ //
47
+ // Figure out the XML reader
48
+ //
49
+
50
+ String driverName =
51
+ System.getProperty("org.xml.sax.driver",
52
+ "org.apache.xerces.parsers.SAXParser");
53
+ System.out.println("SAX driver class: " +
54
+ driverName +
55
+ "\n (you can specify a different one using the " +
56
+ "org.xml.sax.driver property)");
57
+ System.out.print("\n");
58
+
59
+
60
+ //
61
+ // Create the XML reader
62
+ //
63
+
64
+ System.out.println("Now, we'll try to create an instance of the " +
65
+ "driver, using XMLReaderFactory");
66
+ XMLReader reader = null;
67
+ try {
68
+ reader = XMLReaderFactory.createXMLReader(driverName);
69
+ } catch (SAXException e) {
70
+ System.out.println("Failed to create XMLReader: " +
71
+ e.getMessage() +
72
+ "\nMake sure that the class actually " +
73
+ "exists and is present on your CLASSPATH" +
74
+ "\nor specify a different class using the " +
75
+ "org.xml.sax.driver property");
76
+ System.exit(1);
77
+ }
78
+ System.out.println("XMLReader created successfully\n");
79
+
80
+
81
+ //
82
+ // Check features.
83
+ //
84
+ System.out.println("Checking defaults for some well-known features:");
85
+ checkFeature(reader, "http://xml.org/sax/features/namespaces");
86
+ checkFeature(reader, "http://xml.org/sax/features/namespace-prefixes");
87
+ checkFeature(reader, "http://xml.org/sax/features/string-interning");
88
+ checkFeature(reader, "http://xml.org/sax/features/validation");
89
+ checkFeature(reader,
90
+ "http://xml.org/sax/features/external-general-entities");
91
+ checkFeature(reader,
92
+ "http://xml.org/sax/features/external-parameter-entities");
93
+ System.out.print("\n");
94
+
95
+
96
+ //
97
+ // Assign handlers.
98
+ //
99
+ System.out.println("Creating and assigning handlers\n");
100
+ SAXTest handler = new SAXTest();
101
+ reader.setContentHandler(handler);
102
+ reader.setErrorHandler(handler);
103
+
104
+ //
105
+ // Parse documents.
106
+ //
107
+ if (args.length > 0) {
108
+ for (int i = 0; i < args.length; i++) {
109
+ String systemId = makeAbsoluteURL(args[i]);
110
+ System.out.println("Trying file " + systemId);
111
+ try {
112
+ reader.parse(systemId);
113
+ } catch (SAXException e1) {
114
+ System.out.println(systemId +
115
+ " failed with XML error: " +
116
+ e1.getMessage());
117
+ } catch (IOException e2) {
118
+ System.out.println(systemId +
119
+ " failed with I/O error: " +
120
+ e2.getMessage());
121
+ }
122
+ System.out.print("\n");
123
+ }
124
+ } else {
125
+ System.out.println("No documents supplied on command line; " +
126
+ "parsing skipped.");
127
+ }
128
+
129
+
130
+ //
131
+ // Done.
132
+ //
133
+ System.out.println("SAX2 test finished.");
134
+ }
135
+
136
+
137
+ /**
138
+ * Check and display the value of a feature.
139
+ */
140
+ private static void checkFeature (XMLReader reader, String name)
141
+ {
142
+ try {
143
+ System.out.println(" " +
144
+ name +
145
+ " = " +
146
+ reader.getFeature(name));
147
+ } catch (SAXNotRecognizedException e) {
148
+ System.out.println("XMLReader does not recognize feature " +
149
+ name);
150
+ } catch (SAXNotSupportedException e) {
151
+ System.out.println("XMLReader recognizes feature " +
152
+ name +
153
+ " but does not support checking its value");
154
+ }
155
+ }
156
+
157
+
158
+ /**
159
+ * Construct an absolute URL if necessary.
160
+ *
161
+ * This method is useful for relative file paths on a command
162
+ * line; it converts them to absolute file: URLs, using the
163
+ * correct path separator. This method is based on an
164
+ * original suggestion by James Clark.
165
+ *
166
+ * @param url The (possibly relative) URL.
167
+ * @return An absolute URL of some sort.
168
+ */
169
+ private static String makeAbsoluteURL (String url)
170
+ {
171
+ URL baseURL;
172
+
173
+ String currentDirectory = System.getProperty("user.dir");
174
+ String fileSep = System.getProperty("file.separator");
175
+ String file = currentDirectory.replace(fileSep.charAt(0), '/') + '/';
176
+
177
+ if (file.charAt(0) != '/') {
178
+ file = "/" + file;
179
+ }
180
+
181
+ try {
182
+ baseURL = new URL("file", null, file);
183
+ return new URL(baseURL, url).toString();
184
+ } catch (MalformedURLException e) {
185
+ System.err.println(url + ": " + e.getMessage());
186
+ return url;
187
+ }
188
+ }
189
+
190
+ private static String makeNSName (String uri, String localName,
191
+ String qName)
192
+ {
193
+ if (uri.equals(""))
194
+ uri = "[none]";
195
+ if (localName.equals(""))
196
+ localName = "[none]";
197
+ if (qName.equals(""))
198
+ qName = "[none]";
199
+ return uri + '/' + localName + '/' + qName;
200
+ }
201
+
202
+ private static String escapeData (char ch[], int start, int length)
203
+ {
204
+ StringBuffer buf = new StringBuffer();
205
+ for (int i = start; i < start + length; i++) {
206
+ switch(ch[i]) {
207
+ case '\n':
208
+ buf.append("\\n");
209
+ break;
210
+ case '\t':
211
+ buf.append("\\t");
212
+ break;
213
+ case '\r':
214
+ buf.append("\\r");
215
+ break;
216
+ default:
217
+ buf.append(ch[i]);
218
+ break;
219
+ }
220
+ }
221
+ return buf.toString();
222
+ }
223
+
224
+
225
+
226
+ ////////////////////////////////////////////////////////////////////
227
+ // Implementation of org.xml.sax.ContentHandler.
228
+ ////////////////////////////////////////////////////////////////////
229
+
230
+ public void setDocumentLocator (Locator locator)
231
+ {
232
+ System.out.println(" EVENT: setDocumentLocator");
233
+ }
234
+
235
+ public void startDocument ()
236
+ throws SAXException
237
+ {
238
+ System.out.println(" EVENT: startDocument");
239
+ }
240
+
241
+ public void endDocument ()
242
+ throws SAXException
243
+ {
244
+ System.out.println(" EVENT: endDocument");
245
+ }
246
+
247
+ public void startPrefixMapping (String prefix, String uri)
248
+ throws SAXException
249
+ {
250
+ System.out.println(" EVENT: startPrefixMapping " +
251
+ prefix + " = " + uri);
252
+ }
253
+
254
+ public void endPrefixMapping (String prefix)
255
+ throws SAXException
256
+ {
257
+ System.out.println(" EVENT: endPrefixMapping " + prefix);
258
+ }
259
+
260
+ public void startElement (String namespaceURI, String localName,
261
+ String qName, Attributes atts)
262
+ throws SAXException
263
+ {
264
+ System.out.println(" EVENT: startElement " +
265
+ makeNSName(namespaceURI, localName, qName));
266
+ int attLen = atts.getLength();
267
+ for (int i = 0; i < attLen; i++) {
268
+ char ch[] = atts.getValue(i).toCharArray();
269
+ System.out.println(" Attribute " +
270
+ makeNSName(atts.getURI(i),
271
+ atts.getLocalName(i),
272
+ atts.getQName(i)) +
273
+ '=' +
274
+ escapeData(ch, 0, ch.length));
275
+ }
276
+ }
277
+
278
+ public void endElement (String namespaceURI, String localName,
279
+ String qName)
280
+ throws SAXException
281
+ {
282
+ System.out.println(" EVENT: endElement " +
283
+ makeNSName(namespaceURI, localName, qName));
284
+ }
285
+
286
+ public void characters (char ch[], int start, int length)
287
+ throws SAXException
288
+ {
289
+ System.out.println(" EVENT: characters " +
290
+ escapeData(ch, start, length));
291
+ }
292
+
293
+ public void ignorableWhitespace (char ch[], int start, int length)
294
+ throws SAXException
295
+ {
296
+ System.out.println(" EVENT: ignorableWhitespace " +
297
+ escapeData(ch, start, length));
298
+ }
299
+
300
+ public void processingInstruction (String target, String data)
301
+ throws SAXException
302
+ {
303
+ System.out.println(" EVENT: processingInstruction " +
304
+ target + ' ' + data);
305
+ }
306
+
307
+ public void skippedEntity (String name)
308
+ throws SAXException
309
+ {
310
+ System.out.println(" EVENT: skippedEntity " + name);
311
+ }
312
+
313
+
314
+
315
+ ////////////////////////////////////////////////////////////////////
316
+ // Implementation of org.xml.sax.ErrorHandler.
317
+ ////////////////////////////////////////////////////////////////////
318
+
319
+ public void warning (SAXParseException e)
320
+ throws SAXException
321
+ {
322
+ System.out.println(" EVENT: warning " +
323
+ e.getMessage() + ' ' +
324
+ e.getSystemId() + ' ' +
325
+ e.getLineNumber() + ' ' +
326
+ e.getColumnNumber());
327
+ }
328
+
329
+ public void error (SAXParseException e)
330
+ throws SAXException
331
+ {
332
+ System.out.println(" EVENT: error " +
333
+ e.getMessage() + ' ' +
334
+ e.getSystemId() + ' ' +
335
+ e.getLineNumber() + ' ' +
336
+ e.getColumnNumber());
337
+ }
338
+
339
+ public void fatalError (SAXParseException e)
340
+ throws SAXException
341
+ {
342
+ System.out.println(" EVENT: fatal error " +
343
+ e.getMessage() + ' ' +
344
+ e.getSystemId() + ' ' +
345
+ e.getLineNumber() + ' ' +
346
+ e.getColumnNumber());
347
+ }
348
+
349
+ }
350
+
351
+ // end of SAXTest.java
@@ -0,0 +1,257 @@
1
+ // Attributes.java - attribute list with Namespace support
2
+ // http://www.saxproject.org
3
+ // Written by David Megginson
4
+ // NO WARRANTY! This class is in the public domain.
5
+ // $Id: Attributes.java,v 1.13 2004/03/18 12:28:05 dmegginson Exp $
6
+
7
+ package org.xml.sax;
8
+
9
+
10
+ /**
11
+ * Interface for a list of XML attributes.
12
+ *
13
+ * <blockquote>
14
+ * <em>This module, both source code and documentation, is in the
15
+ * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
16
+ * See <a href='http://www.saxproject.org'>http://www.saxproject.org</a>
17
+ * for further information.
18
+ * </blockquote>
19
+ *
20
+ * <p>This interface allows access to a list of attributes in
21
+ * three different ways:</p>
22
+ *
23
+ * <ol>
24
+ * <li>by attribute index;</li>
25
+ * <li>by Namespace-qualified name; or</li>
26
+ * <li>by qualified (prefixed) name.</li>
27
+ * </ol>
28
+ *
29
+ * <p>The list will not contain attributes that were declared
30
+ * #IMPLIED but not specified in the start tag. It will also not
31
+ * contain attributes used as Namespace declarations (xmlns*) unless
32
+ * the <code>http://xml.org/sax/features/namespace-prefixes</code>
33
+ * feature is set to <var>true</var> (it is <var>false</var> by
34
+ * default).
35
+ * Because SAX2 conforms to the original "Namespaces in XML"
36
+ * recommendation, it normally does not
37
+ * give namespace declaration attributes a namespace URI.
38
+ * </p>
39
+ *
40
+ * <p>Some SAX2 parsers may support using an optional feature flag
41
+ * (<code>http://xml.org/sax/features/xmlns-uris</code>) to request
42
+ * that those attributes be given URIs, conforming to a later
43
+ * backwards-incompatible revision of that recommendation. (The
44
+ * attribute's "local name" will be the prefix, or "xmlns" when
45
+ * defining a default element namespace.) For portability, handler
46
+ * code should always resolve that conflict, rather than requiring
47
+ * parsers that can change the setting of that feature flag. </p>
48
+ *
49
+ * <p>If the namespace-prefixes feature (see above) is
50
+ * <var>false</var>, access by qualified name may not be available; if
51
+ * the <code>http://xml.org/sax/features/namespaces</code> feature is
52
+ * <var>false</var>, access by Namespace-qualified names may not be
53
+ * available.</p>
54
+ *
55
+ * <p>This interface replaces the now-deprecated SAX1 {@link
56
+ * org.xml.sax.AttributeList AttributeList} interface, which does not
57
+ * contain Namespace support. In addition to Namespace support, it
58
+ * adds the <var>getIndex</var> methods (below).</p>
59
+ *
60
+ * <p>The order of attributes in the list is unspecified, and will
61
+ * vary from implementation to implementation.</p>
62
+ *
63
+ * @since SAX 2.0
64
+ * @author David Megginson
65
+ * @version 2.0.1 (sax2r2)
66
+ * @see org.xml.sax.helpers.AttributesImpl
67
+ * @see org.xml.sax.ext.DeclHandler#attributeDecl
68
+ */
69
+ public interface Attributes
70
+ {
71
+
72
+
73
+ ////////////////////////////////////////////////////////////////////
74
+ // Indexed access.
75
+ ////////////////////////////////////////////////////////////////////
76
+
77
+
78
+ /**
79
+ * Return the number of attributes in the list.
80
+ *
81
+ * <p>Once you know the number of attributes, you can iterate
82
+ * through the list.</p>
83
+ *
84
+ * @return The number of attributes in the list.
85
+ * @see #getURI(int)
86
+ * @see #getLocalName(int)
87
+ * @see #getQName(int)
88
+ * @see #getType(int)
89
+ * @see #getValue(int)
90
+ */
91
+ public abstract int getLength ();
92
+
93
+
94
+ /**
95
+ * Look up an attribute's Namespace URI by index.
96
+ *
97
+ * @param index The attribute index (zero-based).
98
+ * @return The Namespace URI, or the empty string if none
99
+ * is available, or null if the index is out of
100
+ * range.
101
+ * @see #getLength
102
+ */
103
+ public abstract String getURI (int index);
104
+
105
+
106
+ /**
107
+ * Look up an attribute's local name by index.
108
+ *
109
+ * @param index The attribute index (zero-based).
110
+ * @return The local name, or the empty string if Namespace
111
+ * processing is not being performed, or null
112
+ * if the index is out of range.
113
+ * @see #getLength
114
+ */
115
+ public abstract String getLocalName (int index);
116
+
117
+
118
+ /**
119
+ * Look up an attribute's XML qualified (prefixed) name by index.
120
+ *
121
+ * @param index The attribute index (zero-based).
122
+ * @return The XML qualified name, or the empty string
123
+ * if none is available, or null if the index
124
+ * is out of range.
125
+ * @see #getLength
126
+ */
127
+ public abstract String getQName (int index);
128
+
129
+
130
+ /**
131
+ * Look up an attribute's type by index.
132
+ *
133
+ * <p>The attribute type is one of the strings "CDATA", "ID",
134
+ * "IDREF", "IDREFS", "NMTOKEN", "NMTOKENS", "ENTITY", "ENTITIES",
135
+ * or "NOTATION" (always in upper case).</p>
136
+ *
137
+ * <p>If the parser has not read a declaration for the attribute,
138
+ * or if the parser does not report attribute types, then it must
139
+ * return the value "CDATA" as stated in the XML 1.0 Recommendation
140
+ * (clause 3.3.3, "Attribute-Value Normalization").</p>
141
+ *
142
+ * <p>For an enumerated attribute that is not a notation, the
143
+ * parser will report the type as "NMTOKEN".</p>
144
+ *
145
+ * @param index The attribute index (zero-based).
146
+ * @return The attribute's type as a string, or null if the
147
+ * index is out of range.
148
+ * @see #getLength
149
+ */
150
+ public abstract String getType (int index);
151
+
152
+
153
+ /**
154
+ * Look up an attribute's value by index.
155
+ *
156
+ * <p>If the attribute value is a list of tokens (IDREFS,
157
+ * ENTITIES, or NMTOKENS), the tokens will be concatenated
158
+ * into a single string with each token separated by a
159
+ * single space.</p>
160
+ *
161
+ * @param index The attribute index (zero-based).
162
+ * @return The attribute's value as a string, or null if the
163
+ * index is out of range.
164
+ * @see #getLength
165
+ */
166
+ public abstract String getValue (int index);
167
+
168
+
169
+
170
+ ////////////////////////////////////////////////////////////////////
171
+ // Name-based query.
172
+ ////////////////////////////////////////////////////////////////////
173
+
174
+
175
+ /**
176
+ * Look up the index of an attribute by Namespace name.
177
+ *
178
+ * @param uri The Namespace URI, or the empty string if
179
+ * the name has no Namespace URI.
180
+ * @param localName The attribute's local name.
181
+ * @return The index of the attribute, or -1 if it does not
182
+ * appear in the list.
183
+ */
184
+ public int getIndex (String uri, String localName);
185
+
186
+
187
+ /**
188
+ * Look up the index of an attribute by XML qualified (prefixed) name.
189
+ *
190
+ * @param qName The qualified (prefixed) name.
191
+ * @return The index of the attribute, or -1 if it does not
192
+ * appear in the list.
193
+ */
194
+ public int getIndex (String qName);
195
+
196
+
197
+ /**
198
+ * Look up an attribute's type by Namespace name.
199
+ *
200
+ * <p>See {@link #getType(int) getType(int)} for a description
201
+ * of the possible types.</p>
202
+ *
203
+ * @param uri The Namespace URI, or the empty String if the
204
+ * name has no Namespace URI.
205
+ * @param localName The local name of the attribute.
206
+ * @return The attribute type as a string, or null if the
207
+ * attribute is not in the list or if Namespace
208
+ * processing is not being performed.
209
+ */
210
+ public abstract String getType (String uri, String localName);
211
+
212
+
213
+ /**
214
+ * Look up an attribute's type by XML qualified (prefixed) name.
215
+ *
216
+ * <p>See {@link #getType(int) getType(int)} for a description
217
+ * of the possible types.</p>
218
+ *
219
+ * @param qName The XML qualified name.
220
+ * @return The attribute type as a string, or null if the
221
+ * attribute is not in the list or if qualified names
222
+ * are not available.
223
+ */
224
+ public abstract String getType (String qName);
225
+
226
+
227
+ /**
228
+ * Look up an attribute's value by Namespace name.
229
+ *
230
+ * <p>See {@link #getValue(int) getValue(int)} for a description
231
+ * of the possible values.</p>
232
+ *
233
+ * @param uri The Namespace URI, or the empty String if the
234
+ * name has no Namespace URI.
235
+ * @param localName The local name of the attribute.
236
+ * @return The attribute value as a string, or null if the
237
+ * attribute is not in the list.
238
+ */
239
+ public abstract String getValue (String uri, String localName);
240
+
241
+
242
+ /**
243
+ * Look up an attribute's value by XML qualified (prefixed) name.
244
+ *
245
+ * <p>See {@link #getValue(int) getValue(int)} for a description
246
+ * of the possible values.</p>
247
+ *
248
+ * @param qName The XML qualified name.
249
+ * @return The attribute value as a string, or null if the
250
+ * attribute is not in the list or if qualified names
251
+ * are not available.
252
+ */
253
+ public abstract String getValue (String qName);
254
+
255
+ }
256
+
257
+ // end of Attributes.java