author | joehw |
Wed, 04 Oct 2017 10:54:18 -0700 | |
changeset 47312 | d4f959806fe9 |
parent 47216 | 71c04702a3d5 |
child 47359 | e1a6c0168741 |
permissions | -rw-r--r-- |
12005 | 1 |
/* |
45853
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
25868
diff
changeset
|
2 |
* Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved. |
12005 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
// ParserAdapter.java - adapt a SAX1 Parser to a SAX2 XMLReader. |
|
27 |
// http://www.saxproject.org |
|
28 |
// Written by David Megginson |
|
29 |
// NO WARRANTY! This class is in the public domain. |
|
30 |
// $Id: ParserAdapter.java,v 1.3 2004/11/03 22:53:09 jsuttor Exp $ |
|
31 |
||
32 |
package org.xml.sax.helpers; |
|
33 |
||
34 |
import java.io.IOException; |
|
35 |
import java.util.Enumeration; |
|
36 |
import java.util.Vector; |
|
47312 | 37 |
import jdk.xml.internal.SecuritySupport; |
12005 | 38 |
import org.xml.sax.AttributeList; // deprecated |
47312 | 39 |
import org.xml.sax.Attributes; |
40 |
import org.xml.sax.ContentHandler; |
|
12005 | 41 |
import org.xml.sax.DTDHandler; |
42 |
import org.xml.sax.DocumentHandler; // deprecated |
|
47312 | 43 |
import org.xml.sax.EntityResolver; |
12005 | 44 |
import org.xml.sax.ErrorHandler; |
47312 | 45 |
import org.xml.sax.InputSource; |
46 |
import org.xml.sax.Locator; |
|
47 |
import org.xml.sax.Parser; // deprecated |
|
12005 | 48 |
import org.xml.sax.SAXException; |
49 |
import org.xml.sax.SAXNotRecognizedException; |
|
50 |
import org.xml.sax.SAXNotSupportedException; |
|
47312 | 51 |
import org.xml.sax.SAXParseException; |
52 |
import org.xml.sax.XMLReader; |
|
12005 | 53 |
|
54 |
||
55 |
/** |
|
56 |
* Adapt a SAX1 Parser as a SAX2 XMLReader. |
|
57 |
* |
|
58 |
* <blockquote> |
|
59 |
* <em>This module, both source code and documentation, is in the |
|
60 |
* Public Domain, and comes with <strong>NO WARRANTY</strong>.</em> |
|
61 |
* See <a href='http://www.saxproject.org'>http://www.saxproject.org</a> |
|
62 |
* for further information. |
|
63 |
* </blockquote> |
|
64 |
* |
|
65 |
* <p>This class wraps a SAX1 {@link org.xml.sax.Parser Parser} |
|
66 |
* and makes it act as a SAX2 {@link org.xml.sax.XMLReader XMLReader}, |
|
67 |
* with feature, property, and Namespace support. Note |
|
68 |
* that it is not possible to report {@link org.xml.sax.ContentHandler#skippedEntity |
|
69 |
* skippedEntity} events, since SAX1 does not make that information available.</p> |
|
70 |
* |
|
71 |
* <p>This adapter does not test for duplicate Namespace-qualified |
|
72 |
* attribute names.</p> |
|
73 |
* |
|
25262 | 74 |
* @since 1.4, SAX 2.0 |
12005 | 75 |
* @author David Megginson |
16953 | 76 |
* @version 2.0.1 (sax2r2) |
12005 | 77 |
* @see org.xml.sax.helpers.XMLReaderAdapter |
78 |
* @see org.xml.sax.XMLReader |
|
79 |
* @see org.xml.sax.Parser |
|
80 |
*/ |
|
45853
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
25868
diff
changeset
|
81 |
@SuppressWarnings("deprecation") |
12005 | 82 |
public class ParserAdapter implements XMLReader, DocumentHandler |
83 |
{ |
|
84 |
||
85 |
//////////////////////////////////////////////////////////////////// |
|
86 |
// Constructors. |
|
87 |
//////////////////////////////////////////////////////////////////// |
|
88 |
||
89 |
||
90 |
/** |
|
91 |
* Construct a new parser adapter. |
|
92 |
* |
|
93 |
* <p>Use the "org.xml.sax.parser" property to locate the |
|
94 |
* embedded SAX1 driver.</p> |
|
95 |
* |
|
96 |
* @exception SAXException If the embedded driver |
|
97 |
* cannot be instantiated or if the |
|
98 |
* org.xml.sax.parser property is not specified. |
|
99 |
*/ |
|
100 |
public ParserAdapter () |
|
101 |
throws SAXException |
|
102 |
{ |
|
103 |
super(); |
|
104 |
||
47312 | 105 |
String driver = SecuritySupport.getSystemProperty("org.xml.sax.parser"); |
12005 | 106 |
|
107 |
try { |
|
108 |
setup(ParserFactory.makeParser()); |
|
109 |
} catch (ClassNotFoundException e1) { |
|
110 |
throw new |
|
111 |
SAXException("Cannot find SAX1 driver class " + |
|
112 |
driver, e1); |
|
113 |
} catch (IllegalAccessException e2) { |
|
114 |
throw new |
|
115 |
SAXException("SAX1 driver class " + |
|
116 |
driver + |
|
117 |
" found but cannot be loaded", e2); |
|
118 |
} catch (InstantiationException e3) { |
|
119 |
throw new |
|
120 |
SAXException("SAX1 driver class " + |
|
121 |
driver + |
|
122 |
" loaded but cannot be instantiated", e3); |
|
123 |
} catch (ClassCastException e4) { |
|
124 |
throw new |
|
125 |
SAXException("SAX1 driver class " + |
|
126 |
driver + |
|
127 |
" does not implement org.xml.sax.Parser"); |
|
128 |
} catch (NullPointerException e5) { |
|
129 |
throw new |
|
130 |
SAXException("System property org.xml.sax.parser not specified"); |
|
131 |
} |
|
132 |
} |
|
133 |
||
134 |
||
135 |
/** |
|
136 |
* Construct a new parser adapter. |
|
137 |
* |
|
138 |
* <p>Note that the embedded parser cannot be changed once the |
|
139 |
* adapter is created; to embed a different parser, allocate |
|
140 |
* a new ParserAdapter.</p> |
|
141 |
* |
|
142 |
* @param parser The SAX1 parser to embed. |
|
143 |
* @exception java.lang.NullPointerException If the parser parameter |
|
144 |
* is null. |
|
145 |
*/ |
|
146 |
public ParserAdapter (Parser parser) |
|
147 |
{ |
|
148 |
super(); |
|
149 |
setup(parser); |
|
150 |
} |
|
151 |
||
152 |
||
153 |
/** |
|
154 |
* Internal setup method. |
|
155 |
* |
|
156 |
* @param parser The embedded parser. |
|
157 |
* @exception java.lang.NullPointerException If the parser parameter |
|
158 |
* is null. |
|
159 |
*/ |
|
160 |
private void setup (Parser parser) |
|
161 |
{ |
|
162 |
if (parser == null) { |
|
163 |
throw new |
|
164 |
NullPointerException("Parser argument must not be null"); |
|
165 |
} |
|
166 |
this.parser = parser; |
|
167 |
atts = new AttributesImpl(); |
|
168 |
nsSupport = new NamespaceSupport(); |
|
169 |
attAdapter = new AttributeListAdapter(); |
|
170 |
} |
|
171 |
||
172 |
||
173 |
||
174 |
//////////////////////////////////////////////////////////////////// |
|
175 |
// Implementation of org.xml.sax.XMLReader. |
|
176 |
//////////////////////////////////////////////////////////////////// |
|
177 |
||
178 |
||
179 |
// |
|
180 |
// Internal constants for the sake of convenience. |
|
181 |
// |
|
182 |
private final static String FEATURES = "http://xml.org/sax/features/"; |
|
183 |
private final static String NAMESPACES = FEATURES + "namespaces"; |
|
184 |
private final static String NAMESPACE_PREFIXES = FEATURES + "namespace-prefixes"; |
|
185 |
private final static String XMLNS_URIs = FEATURES + "xmlns-uris"; |
|
186 |
||
187 |
||
188 |
/** |
|
189 |
* Set a feature flag for the parser. |
|
190 |
* |
|
191 |
* <p>The only features recognized are namespaces and |
|
192 |
* namespace-prefixes.</p> |
|
193 |
* |
|
194 |
* @param name The feature name, as a complete URI. |
|
195 |
* @param value The requested feature value. |
|
196 |
* @exception SAXNotRecognizedException If the feature |
|
197 |
* can't be assigned or retrieved. |
|
198 |
* @exception SAXNotSupportedException If the feature |
|
199 |
* can't be assigned that value. |
|
200 |
* @see org.xml.sax.XMLReader#setFeature |
|
201 |
*/ |
|
202 |
public void setFeature (String name, boolean value) |
|
203 |
throws SAXNotRecognizedException, SAXNotSupportedException |
|
204 |
{ |
|
205 |
if (name.equals(NAMESPACES)) { |
|
206 |
checkNotParsing("feature", name); |
|
207 |
namespaces = value; |
|
208 |
if (!namespaces && !prefixes) { |
|
209 |
prefixes = true; |
|
210 |
} |
|
211 |
} else if (name.equals(NAMESPACE_PREFIXES)) { |
|
212 |
checkNotParsing("feature", name); |
|
213 |
prefixes = value; |
|
214 |
if (!prefixes && !namespaces) { |
|
215 |
namespaces = true; |
|
216 |
} |
|
217 |
} else if (name.equals(XMLNS_URIs)) { |
|
218 |
checkNotParsing("feature", name); |
|
219 |
uris = value; |
|
220 |
} else { |
|
221 |
throw new SAXNotRecognizedException("Feature: " + name); |
|
222 |
} |
|
223 |
} |
|
224 |
||
225 |
||
226 |
/** |
|
227 |
* Check a parser feature flag. |
|
228 |
* |
|
229 |
* <p>The only features recognized are namespaces and |
|
230 |
* namespace-prefixes.</p> |
|
231 |
* |
|
232 |
* @param name The feature name, as a complete URI. |
|
233 |
* @return The current feature value. |
|
234 |
* @exception SAXNotRecognizedException If the feature |
|
235 |
* value can't be assigned or retrieved. |
|
236 |
* @exception SAXNotSupportedException If the |
|
237 |
* feature is not currently readable. |
|
238 |
* @see org.xml.sax.XMLReader#setFeature |
|
239 |
*/ |
|
240 |
public boolean getFeature (String name) |
|
241 |
throws SAXNotRecognizedException, SAXNotSupportedException |
|
242 |
{ |
|
243 |
if (name.equals(NAMESPACES)) { |
|
244 |
return namespaces; |
|
245 |
} else if (name.equals(NAMESPACE_PREFIXES)) { |
|
246 |
return prefixes; |
|
247 |
} else if (name.equals(XMLNS_URIs)) { |
|
248 |
return uris; |
|
249 |
} else { |
|
250 |
throw new SAXNotRecognizedException("Feature: " + name); |
|
251 |
} |
|
252 |
} |
|
253 |
||
254 |
||
255 |
/** |
|
256 |
* Set a parser property. |
|
257 |
* |
|
258 |
* <p>No properties are currently recognized.</p> |
|
259 |
* |
|
260 |
* @param name The property name. |
|
261 |
* @param value The property value. |
|
262 |
* @exception SAXNotRecognizedException If the property |
|
263 |
* value can't be assigned or retrieved. |
|
264 |
* @exception SAXNotSupportedException If the property |
|
265 |
* can't be assigned that value. |
|
266 |
* @see org.xml.sax.XMLReader#setProperty |
|
267 |
*/ |
|
268 |
public void setProperty (String name, Object value) |
|
269 |
throws SAXNotRecognizedException, SAXNotSupportedException |
|
270 |
{ |
|
271 |
throw new SAXNotRecognizedException("Property: " + name); |
|
272 |
} |
|
273 |
||
274 |
||
275 |
/** |
|
276 |
* Get a parser property. |
|
277 |
* |
|
278 |
* <p>No properties are currently recognized.</p> |
|
279 |
* |
|
280 |
* @param name The property name. |
|
281 |
* @return The property value. |
|
282 |
* @exception SAXNotRecognizedException If the property |
|
283 |
* value can't be assigned or retrieved. |
|
284 |
* @exception SAXNotSupportedException If the property |
|
285 |
* value is not currently readable. |
|
286 |
* @see org.xml.sax.XMLReader#getProperty |
|
287 |
*/ |
|
288 |
public Object getProperty (String name) |
|
289 |
throws SAXNotRecognizedException, SAXNotSupportedException |
|
290 |
{ |
|
291 |
throw new SAXNotRecognizedException("Property: " + name); |
|
292 |
} |
|
293 |
||
294 |
||
295 |
/** |
|
296 |
* Set the entity resolver. |
|
297 |
* |
|
298 |
* @param resolver The new entity resolver. |
|
299 |
* @see org.xml.sax.XMLReader#setEntityResolver |
|
300 |
*/ |
|
301 |
public void setEntityResolver (EntityResolver resolver) |
|
302 |
{ |
|
303 |
entityResolver = resolver; |
|
304 |
} |
|
305 |
||
306 |
||
307 |
/** |
|
308 |
* Return the current entity resolver. |
|
309 |
* |
|
310 |
* @return The current entity resolver, or null if none was supplied. |
|
311 |
* @see org.xml.sax.XMLReader#getEntityResolver |
|
312 |
*/ |
|
313 |
public EntityResolver getEntityResolver () |
|
314 |
{ |
|
315 |
return entityResolver; |
|
316 |
} |
|
317 |
||
318 |
||
319 |
/** |
|
320 |
* Set the DTD handler. |
|
321 |
* |
|
322 |
* @param handler the new DTD handler |
|
323 |
* @see org.xml.sax.XMLReader#setEntityResolver |
|
324 |
*/ |
|
325 |
public void setDTDHandler (DTDHandler handler) |
|
326 |
{ |
|
327 |
dtdHandler = handler; |
|
328 |
} |
|
329 |
||
330 |
||
331 |
/** |
|
332 |
* Return the current DTD handler. |
|
333 |
* |
|
334 |
* @return the current DTD handler, or null if none was supplied |
|
335 |
* @see org.xml.sax.XMLReader#getEntityResolver |
|
336 |
*/ |
|
337 |
public DTDHandler getDTDHandler () |
|
338 |
{ |
|
339 |
return dtdHandler; |
|
340 |
} |
|
341 |
||
342 |
||
343 |
/** |
|
344 |
* Set the content handler. |
|
345 |
* |
|
346 |
* @param handler the new content handler |
|
347 |
* @see org.xml.sax.XMLReader#setEntityResolver |
|
348 |
*/ |
|
349 |
public void setContentHandler (ContentHandler handler) |
|
350 |
{ |
|
351 |
contentHandler = handler; |
|
352 |
} |
|
353 |
||
354 |
||
355 |
/** |
|
356 |
* Return the current content handler. |
|
357 |
* |
|
358 |
* @return The current content handler, or null if none was supplied. |
|
359 |
* @see org.xml.sax.XMLReader#getEntityResolver |
|
360 |
*/ |
|
361 |
public ContentHandler getContentHandler () |
|
362 |
{ |
|
363 |
return contentHandler; |
|
364 |
} |
|
365 |
||
366 |
||
367 |
/** |
|
368 |
* Set the error handler. |
|
369 |
* |
|
370 |
* @param handler The new error handler. |
|
371 |
* @see org.xml.sax.XMLReader#setEntityResolver |
|
372 |
*/ |
|
373 |
public void setErrorHandler (ErrorHandler handler) |
|
374 |
{ |
|
375 |
errorHandler = handler; |
|
376 |
} |
|
377 |
||
378 |
||
379 |
/** |
|
380 |
* Return the current error handler. |
|
381 |
* |
|
382 |
* @return The current error handler, or null if none was supplied. |
|
383 |
* @see org.xml.sax.XMLReader#getEntityResolver |
|
384 |
*/ |
|
385 |
public ErrorHandler getErrorHandler () |
|
386 |
{ |
|
387 |
return errorHandler; |
|
388 |
} |
|
389 |
||
390 |
||
391 |
/** |
|
392 |
* Parse an XML document. |
|
393 |
* |
|
394 |
* @param systemId The absolute URL of the document. |
|
395 |
* @exception java.io.IOException If there is a problem reading |
|
396 |
* the raw content of the document. |
|
397 |
* @exception SAXException If there is a problem |
|
398 |
* processing the document. |
|
399 |
* @see #parse(org.xml.sax.InputSource) |
|
400 |
* @see org.xml.sax.Parser#parse(java.lang.String) |
|
401 |
*/ |
|
402 |
public void parse (String systemId) |
|
403 |
throws IOException, SAXException |
|
404 |
{ |
|
405 |
parse(new InputSource(systemId)); |
|
406 |
} |
|
407 |
||
408 |
||
409 |
/** |
|
410 |
* Parse an XML document. |
|
411 |
* |
|
412 |
* @param input An input source for the document. |
|
413 |
* @exception java.io.IOException If there is a problem reading |
|
414 |
* the raw content of the document. |
|
415 |
* @exception SAXException If there is a problem |
|
416 |
* processing the document. |
|
417 |
* @see #parse(java.lang.String) |
|
418 |
* @see org.xml.sax.Parser#parse(org.xml.sax.InputSource) |
|
419 |
*/ |
|
420 |
public void parse (InputSource input) |
|
421 |
throws IOException, SAXException |
|
422 |
{ |
|
423 |
if (parsing) { |
|
424 |
throw new SAXException("Parser is already in use"); |
|
425 |
} |
|
426 |
setupParser(); |
|
427 |
parsing = true; |
|
428 |
try { |
|
429 |
parser.parse(input); |
|
430 |
} finally { |
|
431 |
parsing = false; |
|
432 |
} |
|
433 |
parsing = false; |
|
434 |
} |
|
435 |
||
436 |
||
437 |
||
438 |
//////////////////////////////////////////////////////////////////// |
|
439 |
// Implementation of org.xml.sax.DocumentHandler. |
|
440 |
//////////////////////////////////////////////////////////////////// |
|
441 |
||
442 |
||
443 |
/** |
|
444 |
* Adapter implementation method; do not call. |
|
445 |
* Adapt a SAX1 document locator event. |
|
446 |
* |
|
447 |
* @param locator A document locator. |
|
448 |
* @see org.xml.sax.ContentHandler#setDocumentLocator |
|
449 |
*/ |
|
450 |
public void setDocumentLocator (Locator locator) |
|
451 |
{ |
|
452 |
this.locator = locator; |
|
453 |
if (contentHandler != null) { |
|
454 |
contentHandler.setDocumentLocator(locator); |
|
455 |
} |
|
456 |
} |
|
457 |
||
458 |
||
459 |
/** |
|
460 |
* Adapter implementation method; do not call. |
|
461 |
* Adapt a SAX1 start document event. |
|
462 |
* |
|
463 |
* @exception SAXException The client may raise a |
|
464 |
* processing exception. |
|
465 |
* @see org.xml.sax.DocumentHandler#startDocument |
|
466 |
*/ |
|
467 |
public void startDocument () |
|
468 |
throws SAXException |
|
469 |
{ |
|
470 |
if (contentHandler != null) { |
|
471 |
contentHandler.startDocument(); |
|
472 |
} |
|
473 |
} |
|
474 |
||
475 |
||
476 |
/** |
|
477 |
* Adapter implementation method; do not call. |
|
478 |
* Adapt a SAX1 end document event. |
|
479 |
* |
|
480 |
* @exception SAXException The client may raise a |
|
481 |
* processing exception. |
|
482 |
* @see org.xml.sax.DocumentHandler#endDocument |
|
483 |
*/ |
|
484 |
public void endDocument () |
|
485 |
throws SAXException |
|
486 |
{ |
|
487 |
if (contentHandler != null) { |
|
488 |
contentHandler.endDocument(); |
|
489 |
} |
|
490 |
} |
|
491 |
||
492 |
||
493 |
/** |
|
494 |
* Adapter implementation method; do not call. |
|
495 |
* Adapt a SAX1 startElement event. |
|
496 |
* |
|
497 |
* <p>If necessary, perform Namespace processing.</p> |
|
498 |
* |
|
499 |
* @param qName The qualified (prefixed) name. |
|
500 |
* @param qAtts The XML attribute list (with qnames). |
|
501 |
* @exception SAXException The client may raise a |
|
502 |
* processing exception. |
|
503 |
*/ |
|
504 |
public void startElement (String qName, AttributeList qAtts) |
|
505 |
throws SAXException |
|
506 |
{ |
|
507 |
// These are exceptions from the |
|
508 |
// first pass; they should be |
|
509 |
// ignored if there's a second pass, |
|
510 |
// but reported otherwise. |
|
511 |
Vector exceptions = null; |
|
512 |
||
513 |
// If we're not doing Namespace |
|
514 |
// processing, dispatch this quickly. |
|
515 |
if (!namespaces) { |
|
516 |
if (contentHandler != null) { |
|
517 |
attAdapter.setAttributeList(qAtts); |
|
518 |
contentHandler.startElement("", "", qName.intern(), |
|
519 |
attAdapter); |
|
520 |
} |
|
521 |
return; |
|
522 |
} |
|
523 |
||
524 |
||
525 |
// OK, we're doing Namespace processing. |
|
526 |
nsSupport.pushContext(); |
|
527 |
int length = qAtts.getLength(); |
|
528 |
||
529 |
// First pass: handle NS decls |
|
530 |
for (int i = 0; i < length; i++) { |
|
531 |
String attQName = qAtts.getName(i); |
|
532 |
||
533 |
if (!attQName.startsWith("xmlns")) |
|
534 |
continue; |
|
535 |
// Could be a declaration... |
|
536 |
String prefix; |
|
537 |
int n = attQName.indexOf(':'); |
|
538 |
||
539 |
// xmlns=... |
|
540 |
if (n == -1 && attQName.length () == 5) { |
|
541 |
prefix = ""; |
|
542 |
} else if (n != 5) { |
|
543 |
// XML namespaces spec doesn't discuss "xmlnsf:oo" |
|
544 |
// (and similarly named) attributes ... at most, warn |
|
545 |
continue; |
|
546 |
} else // xmlns:foo=... |
|
547 |
prefix = attQName.substring(n+1); |
|
548 |
||
549 |
String value = qAtts.getValue(i); |
|
550 |
if (!nsSupport.declarePrefix(prefix, value)) { |
|
551 |
reportError("Illegal Namespace prefix: " + prefix); |
|
552 |
continue; |
|
553 |
} |
|
554 |
if (contentHandler != null) |
|
555 |
contentHandler.startPrefixMapping(prefix, value); |
|
556 |
} |
|
557 |
||
558 |
// Second pass: copy all relevant |
|
559 |
// attributes into the SAX2 AttributeList |
|
560 |
// using updated prefix bindings |
|
561 |
atts.clear(); |
|
562 |
for (int i = 0; i < length; i++) { |
|
563 |
String attQName = qAtts.getName(i); |
|
564 |
String type = qAtts.getType(i); |
|
565 |
String value = qAtts.getValue(i); |
|
566 |
||
567 |
// Declaration? |
|
568 |
if (attQName.startsWith("xmlns")) { |
|
569 |
String prefix; |
|
570 |
int n = attQName.indexOf(':'); |
|
571 |
||
572 |
if (n == -1 && attQName.length () == 5) { |
|
573 |
prefix = ""; |
|
574 |
} else if (n != 5) { |
|
575 |
// XML namespaces spec doesn't discuss "xmlnsf:oo" |
|
576 |
// (and similarly named) attributes ... ignore |
|
577 |
prefix = null; |
|
578 |
} else { |
|
579 |
prefix = attQName.substring(6); |
|
580 |
} |
|
581 |
// Yes, decl: report or prune |
|
582 |
if (prefix != null) { |
|
583 |
if (prefixes) { |
|
584 |
if (uris) |
|
585 |
// note funky case: localname can be null |
|
586 |
// when declaring the default prefix, and |
|
587 |
// yet the uri isn't null. |
|
588 |
atts.addAttribute (nsSupport.XMLNS, prefix, |
|
589 |
attQName.intern(), type, value); |
|
590 |
else |
|
591 |
atts.addAttribute ("", "", |
|
592 |
attQName.intern(), type, value); |
|
593 |
} |
|
594 |
continue; |
|
595 |
} |
|
596 |
} |
|
597 |
||
598 |
// Not a declaration -- report |
|
599 |
try { |
|
600 |
String attName[] = processName(attQName, true, true); |
|
601 |
atts.addAttribute(attName[0], attName[1], attName[2], |
|
602 |
type, value); |
|
603 |
} catch (SAXException e) { |
|
604 |
if (exceptions == null) |
|
605 |
exceptions = new Vector(); |
|
606 |
exceptions.addElement(e); |
|
607 |
atts.addAttribute("", attQName, attQName, type, value); |
|
608 |
} |
|
609 |
} |
|
610 |
||
611 |
// now handle the deferred exception reports |
|
612 |
if (exceptions != null && errorHandler != null) { |
|
613 |
for (int i = 0; i < exceptions.size(); i++) |
|
614 |
errorHandler.error((SAXParseException) |
|
615 |
(exceptions.elementAt(i))); |
|
616 |
} |
|
617 |
||
618 |
// OK, finally report the event. |
|
619 |
if (contentHandler != null) { |
|
620 |
String name[] = processName(qName, false, false); |
|
621 |
contentHandler.startElement(name[0], name[1], name[2], atts); |
|
622 |
} |
|
623 |
} |
|
624 |
||
625 |
||
626 |
/** |
|
627 |
* Adapter implementation method; do not call. |
|
628 |
* Adapt a SAX1 end element event. |
|
629 |
* |
|
630 |
* @param qName The qualified (prefixed) name. |
|
631 |
* @exception SAXException The client may raise a |
|
632 |
* processing exception. |
|
633 |
* @see org.xml.sax.DocumentHandler#endElement |
|
634 |
*/ |
|
635 |
public void endElement (String qName) |
|
636 |
throws SAXException |
|
637 |
{ |
|
638 |
// If we're not doing Namespace |
|
639 |
// processing, dispatch this quickly. |
|
640 |
if (!namespaces) { |
|
641 |
if (contentHandler != null) { |
|
642 |
contentHandler.endElement("", "", qName.intern()); |
|
643 |
} |
|
644 |
return; |
|
645 |
} |
|
646 |
||
647 |
// Split the name. |
|
648 |
String names[] = processName(qName, false, false); |
|
649 |
if (contentHandler != null) { |
|
650 |
contentHandler.endElement(names[0], names[1], names[2]); |
|
651 |
Enumeration prefixes = nsSupport.getDeclaredPrefixes(); |
|
652 |
while (prefixes.hasMoreElements()) { |
|
653 |
String prefix = (String)prefixes.nextElement(); |
|
654 |
contentHandler.endPrefixMapping(prefix); |
|
655 |
} |
|
656 |
} |
|
657 |
nsSupport.popContext(); |
|
658 |
} |
|
659 |
||
660 |
||
661 |
/** |
|
662 |
* Adapter implementation method; do not call. |
|
663 |
* Adapt a SAX1 characters event. |
|
664 |
* |
|
665 |
* @param ch An array of characters. |
|
666 |
* @param start The starting position in the array. |
|
667 |
* @param length The number of characters to use. |
|
668 |
* @exception SAXException The client may raise a |
|
669 |
* processing exception. |
|
670 |
* @see org.xml.sax.DocumentHandler#characters |
|
671 |
*/ |
|
672 |
public void characters (char ch[], int start, int length) |
|
673 |
throws SAXException |
|
674 |
{ |
|
675 |
if (contentHandler != null) { |
|
676 |
contentHandler.characters(ch, start, length); |
|
677 |
} |
|
678 |
} |
|
679 |
||
680 |
||
681 |
/** |
|
682 |
* Adapter implementation method; do not call. |
|
683 |
* Adapt a SAX1 ignorable whitespace event. |
|
684 |
* |
|
685 |
* @param ch An array of characters. |
|
686 |
* @param start The starting position in the array. |
|
687 |
* @param length The number of characters to use. |
|
688 |
* @exception SAXException The client may raise a |
|
689 |
* processing exception. |
|
690 |
* @see org.xml.sax.DocumentHandler#ignorableWhitespace |
|
691 |
*/ |
|
692 |
public void ignorableWhitespace (char ch[], int start, int length) |
|
693 |
throws SAXException |
|
694 |
{ |
|
695 |
if (contentHandler != null) { |
|
696 |
contentHandler.ignorableWhitespace(ch, start, length); |
|
697 |
} |
|
698 |
} |
|
699 |
||
700 |
||
701 |
/** |
|
702 |
* Adapter implementation method; do not call. |
|
703 |
* Adapt a SAX1 processing instruction event. |
|
704 |
* |
|
705 |
* @param target The processing instruction target. |
|
706 |
* @param data The remainder of the processing instruction |
|
707 |
* @exception SAXException The client may raise a |
|
708 |
* processing exception. |
|
709 |
* @see org.xml.sax.DocumentHandler#processingInstruction |
|
710 |
*/ |
|
711 |
public void processingInstruction (String target, String data) |
|
712 |
throws SAXException |
|
713 |
{ |
|
714 |
if (contentHandler != null) { |
|
715 |
contentHandler.processingInstruction(target, data); |
|
716 |
} |
|
717 |
} |
|
718 |
||
719 |
||
720 |
||
721 |
//////////////////////////////////////////////////////////////////// |
|
722 |
// Internal utility methods. |
|
723 |
//////////////////////////////////////////////////////////////////// |
|
724 |
||
725 |
||
726 |
/** |
|
727 |
* Initialize the parser before each run. |
|
728 |
*/ |
|
729 |
private void setupParser () |
|
730 |
{ |
|
731 |
// catch an illegal "nonsense" state. |
|
732 |
if (!prefixes && !namespaces) |
|
733 |
throw new IllegalStateException (); |
|
734 |
||
735 |
nsSupport.reset(); |
|
736 |
if (uris) |
|
737 |
nsSupport.setNamespaceDeclUris (true); |
|
738 |
||
739 |
if (entityResolver != null) { |
|
740 |
parser.setEntityResolver(entityResolver); |
|
741 |
} |
|
742 |
if (dtdHandler != null) { |
|
743 |
parser.setDTDHandler(dtdHandler); |
|
744 |
} |
|
745 |
if (errorHandler != null) { |
|
746 |
parser.setErrorHandler(errorHandler); |
|
747 |
} |
|
748 |
parser.setDocumentHandler(this); |
|
749 |
locator = null; |
|
750 |
} |
|
751 |
||
752 |
||
753 |
/** |
|
754 |
* Process a qualified (prefixed) name. |
|
755 |
* |
|
756 |
* <p>If the name has an undeclared prefix, use only the qname |
|
757 |
* and make an ErrorHandler.error callback in case the app is |
|
758 |
* interested.</p> |
|
759 |
* |
|
760 |
* @param qName The qualified (prefixed) name. |
|
761 |
* @param isAttribute true if this is an attribute name. |
|
762 |
* @return The name split into three parts. |
|
763 |
* @exception SAXException The client may throw |
|
764 |
* an exception if there is an error callback. |
|
765 |
*/ |
|
766 |
private String [] processName (String qName, boolean isAttribute, |
|
767 |
boolean useException) |
|
768 |
throws SAXException |
|
769 |
{ |
|
770 |
String parts[] = nsSupport.processName(qName, nameParts, |
|
771 |
isAttribute); |
|
772 |
if (parts == null) { |
|
773 |
if (useException) |
|
774 |
throw makeException("Undeclared prefix: " + qName); |
|
775 |
reportError("Undeclared prefix: " + qName); |
|
776 |
parts = new String[3]; |
|
777 |
parts[0] = parts[1] = ""; |
|
778 |
parts[2] = qName.intern(); |
|
779 |
} |
|
780 |
return parts; |
|
781 |
} |
|
782 |
||
783 |
||
784 |
/** |
|
785 |
* Report a non-fatal error. |
|
786 |
* |
|
787 |
* @param message The error message. |
|
788 |
* @exception SAXException The client may throw |
|
789 |
* an exception. |
|
790 |
*/ |
|
791 |
void reportError (String message) |
|
792 |
throws SAXException |
|
793 |
{ |
|
794 |
if (errorHandler != null) |
|
795 |
errorHandler.error(makeException(message)); |
|
796 |
} |
|
797 |
||
798 |
||
799 |
/** |
|
800 |
* Construct an exception for the current context. |
|
801 |
* |
|
802 |
* @param message The error message. |
|
803 |
*/ |
|
804 |
private SAXParseException makeException (String message) |
|
805 |
{ |
|
806 |
if (locator != null) { |
|
807 |
return new SAXParseException(message, locator); |
|
808 |
} else { |
|
809 |
return new SAXParseException(message, null, null, -1, -1); |
|
810 |
} |
|
811 |
} |
|
812 |
||
813 |
||
814 |
/** |
|
815 |
* Throw an exception if we are parsing. |
|
816 |
* |
|
817 |
* <p>Use this method to detect illegal feature or |
|
818 |
* property changes.</p> |
|
819 |
* |
|
820 |
* @param type The type of thing (feature or property). |
|
821 |
* @param name The feature or property name. |
|
822 |
* @exception SAXNotSupportedException If a |
|
823 |
* document is currently being parsed. |
|
824 |
*/ |
|
825 |
private void checkNotParsing (String type, String name) |
|
826 |
throws SAXNotSupportedException |
|
827 |
{ |
|
828 |
if (parsing) { |
|
829 |
throw new SAXNotSupportedException("Cannot change " + |
|
830 |
type + ' ' + |
|
831 |
name + " while parsing"); |
|
832 |
||
833 |
} |
|
834 |
} |
|
835 |
||
836 |
||
837 |
||
838 |
//////////////////////////////////////////////////////////////////// |
|
839 |
// Internal state. |
|
840 |
//////////////////////////////////////////////////////////////////// |
|
841 |
||
842 |
private NamespaceSupport nsSupport; |
|
843 |
private AttributeListAdapter attAdapter; |
|
844 |
||
845 |
private boolean parsing = false; |
|
846 |
private String nameParts[] = new String[3]; |
|
847 |
||
848 |
private Parser parser = null; |
|
849 |
||
850 |
private AttributesImpl atts = null; |
|
851 |
||
852 |
// Features |
|
853 |
private boolean namespaces = true; |
|
854 |
private boolean prefixes = false; |
|
855 |
private boolean uris = false; |
|
856 |
||
857 |
// Properties |
|
858 |
||
859 |
// Handlers |
|
860 |
Locator locator; |
|
861 |
||
862 |
EntityResolver entityResolver = null; |
|
863 |
DTDHandler dtdHandler = null; |
|
864 |
ContentHandler contentHandler = null; |
|
865 |
ErrorHandler errorHandler = null; |
|
866 |
||
867 |
||
868 |
||
869 |
//////////////////////////////////////////////////////////////////// |
|
870 |
// Inner class to wrap an AttributeList when not doing NS proc. |
|
871 |
//////////////////////////////////////////////////////////////////// |
|
872 |
||
873 |
||
874 |
/** |
|
875 |
* Adapt a SAX1 AttributeList as a SAX2 Attributes object. |
|
876 |
* |
|
877 |
* <p>This class is in the Public Domain, and comes with NO |
|
878 |
* WARRANTY of any kind.</p> |
|
879 |
* |
|
880 |
* <p>This wrapper class is used only when Namespace support |
|
881 |
* is disabled -- it provides pretty much a direct mapping |
|
882 |
* from SAX1 to SAX2, except that names and types are |
|
883 |
* interned whenever requested.</p> |
|
884 |
*/ |
|
885 |
final class AttributeListAdapter implements Attributes |
|
886 |
{ |
|
887 |
||
888 |
/** |
|
889 |
* Construct a new adapter. |
|
890 |
*/ |
|
891 |
AttributeListAdapter () |
|
892 |
{ |
|
893 |
} |
|
894 |
||
895 |
||
896 |
/** |
|
897 |
* Set the embedded AttributeList. |
|
898 |
* |
|
899 |
* <p>This method must be invoked before any of the others |
|
900 |
* can be used.</p> |
|
901 |
* |
|
902 |
* @param The SAX1 attribute list (with qnames). |
|
903 |
*/ |
|
904 |
void setAttributeList (AttributeList qAtts) |
|
905 |
{ |
|
906 |
this.qAtts = qAtts; |
|
907 |
} |
|
908 |
||
909 |
||
910 |
/** |
|
911 |
* Return the length of the attribute list. |
|
912 |
* |
|
913 |
* @return The number of attributes in the list. |
|
914 |
* @see org.xml.sax.Attributes#getLength |
|
915 |
*/ |
|
916 |
public int getLength () |
|
917 |
{ |
|
918 |
return qAtts.getLength(); |
|
919 |
} |
|
920 |
||
921 |
||
922 |
/** |
|
923 |
* Return the Namespace URI of the specified attribute. |
|
924 |
* |
|
925 |
* @param The attribute's index. |
|
926 |
* @return Always the empty string. |
|
927 |
* @see org.xml.sax.Attributes#getURI |
|
928 |
*/ |
|
929 |
public String getURI (int i) |
|
930 |
{ |
|
931 |
return ""; |
|
932 |
} |
|
933 |
||
934 |
||
935 |
/** |
|
936 |
* Return the local name of the specified attribute. |
|
937 |
* |
|
938 |
* @param The attribute's index. |
|
939 |
* @return Always the empty string. |
|
940 |
* @see org.xml.sax.Attributes#getLocalName |
|
941 |
*/ |
|
942 |
public String getLocalName (int i) |
|
943 |
{ |
|
944 |
return ""; |
|
945 |
} |
|
946 |
||
947 |
||
948 |
/** |
|
949 |
* Return the qualified (prefixed) name of the specified attribute. |
|
950 |
* |
|
951 |
* @param The attribute's index. |
|
952 |
* @return The attribute's qualified name, internalized. |
|
953 |
*/ |
|
954 |
public String getQName (int i) |
|
955 |
{ |
|
956 |
return qAtts.getName(i).intern(); |
|
957 |
} |
|
958 |
||
959 |
||
960 |
/** |
|
961 |
* Return the type of the specified attribute. |
|
962 |
* |
|
963 |
* @param The attribute's index. |
|
964 |
* @return The attribute's type as an internalized string. |
|
965 |
*/ |
|
966 |
public String getType (int i) |
|
967 |
{ |
|
968 |
return qAtts.getType(i).intern(); |
|
969 |
} |
|
970 |
||
971 |
||
972 |
/** |
|
973 |
* Return the value of the specified attribute. |
|
974 |
* |
|
975 |
* @param The attribute's index. |
|
976 |
* @return The attribute's value. |
|
977 |
*/ |
|
978 |
public String getValue (int i) |
|
979 |
{ |
|
980 |
return qAtts.getValue(i); |
|
981 |
} |
|
982 |
||
983 |
||
984 |
/** |
|
985 |
* Look up an attribute index by Namespace name. |
|
986 |
* |
|
987 |
* @param uri The Namespace URI or the empty string. |
|
988 |
* @param localName The local name. |
|
989 |
* @return The attributes index, or -1 if none was found. |
|
990 |
* @see org.xml.sax.Attributes#getIndex(java.lang.String,java.lang.String) |
|
991 |
*/ |
|
992 |
public int getIndex (String uri, String localName) |
|
993 |
{ |
|
994 |
return -1; |
|
995 |
} |
|
996 |
||
997 |
||
998 |
/** |
|
999 |
* Look up an attribute index by qualified (prefixed) name. |
|
1000 |
* |
|
1001 |
* @param qName The qualified name. |
|
1002 |
* @return The attributes index, or -1 if none was found. |
|
1003 |
* @see org.xml.sax.Attributes#getIndex(java.lang.String) |
|
1004 |
*/ |
|
1005 |
public int getIndex (String qName) |
|
1006 |
{ |
|
1007 |
int max = atts.getLength(); |
|
1008 |
for (int i = 0; i < max; i++) { |
|
1009 |
if (qAtts.getName(i).equals(qName)) { |
|
1010 |
return i; |
|
1011 |
} |
|
1012 |
} |
|
1013 |
return -1; |
|
1014 |
} |
|
1015 |
||
1016 |
||
1017 |
/** |
|
1018 |
* Look up the type of an attribute by Namespace name. |
|
1019 |
* |
|
1020 |
* @param uri The Namespace URI |
|
1021 |
* @param localName The local name. |
|
1022 |
* @return The attribute's type as an internalized string. |
|
1023 |
*/ |
|
1024 |
public String getType (String uri, String localName) |
|
1025 |
{ |
|
1026 |
return null; |
|
1027 |
} |
|
1028 |
||
1029 |
||
1030 |
/** |
|
1031 |
* Look up the type of an attribute by qualified (prefixed) name. |
|
1032 |
* |
|
1033 |
* @param qName The qualified name. |
|
1034 |
* @return The attribute's type as an internalized string. |
|
1035 |
*/ |
|
1036 |
public String getType (String qName) |
|
1037 |
{ |
|
1038 |
return qAtts.getType(qName).intern(); |
|
1039 |
} |
|
1040 |
||
1041 |
||
1042 |
/** |
|
1043 |
* Look up the value of an attribute by Namespace name. |
|
1044 |
* |
|
1045 |
* @param uri The Namespace URI |
|
1046 |
* @param localName The local name. |
|
1047 |
* @return The attribute's value. |
|
1048 |
*/ |
|
1049 |
public String getValue (String uri, String localName) |
|
1050 |
{ |
|
1051 |
return null; |
|
1052 |
} |
|
1053 |
||
1054 |
||
1055 |
/** |
|
1056 |
* Look up the value of an attribute by qualified (prefixed) name. |
|
1057 |
* |
|
1058 |
* @param qName The qualified name. |
|
1059 |
* @return The attribute's value. |
|
1060 |
*/ |
|
1061 |
public String getValue (String qName) |
|
1062 |
{ |
|
1063 |
return qAtts.getValue(qName); |
|
1064 |
} |
|
1065 |
||
1066 |
private AttributeList qAtts; |
|
1067 |
} |
|
1068 |
} |
|
1069 |
||
1070 |
// end of ParserAdapter.java |