Source Home >> Java Source 1.6.0 >> org.xml.sax.helpers.ParserAdapter V 0.09
  • 0001// ParserAdapter.java - adapt a SAX1 Parser to a SAX2 XMLReader.
  • 0002// http://www.saxproject.org
  • 0003// Written by David Megginson
  • 0004// NO WARRANTY! This class is in the public domain.
  • 0005// $Id: ParserAdapter.java,v 1.3 2004/11/03 22:53:09 jsuttor Exp $
  • 0006
  • 0007package org.xml.sax.helpers;
  • 0008
  • 0009import java.io.IOException;
  • 0010import java.util.Enumeration;
  • 0011import java.util.Vector;
  • 0012
  • 0013import org.xml.sax.Parser; // deprecated
  • 0014import org.xml.sax.InputSource;
  • 0015import org.xml.sax.Locator;
  • 0016import org.xml.sax.AttributeList; // deprecated
  • 0017import org.xml.sax.EntityResolver;
  • 0018import org.xml.sax.DTDHandler;
  • 0019import org.xml.sax.DocumentHandler; // deprecated
  • 0020import org.xml.sax.ErrorHandler;
  • 0021import org.xml.sax.SAXException;
  • 0022import org.xml.sax.SAXParseException;
  • 0023
  • 0024import org.xml.sax.XMLReader;
  • 0025import org.xml.sax.Attributes;
  • 0026import org.xml.sax.ContentHandler;
  • 0027import org.xml.sax.SAXNotRecognizedException;
  • 0028import org.xml.sax.SAXNotSupportedException;
  • 0029
  • 0030
  • 0031/**
  • 0032 * Adapt a SAX1 Parser as a SAX2 XMLReader.
  • 0033 *
  • 0034 * <blockquote>
  • 0035 * <em>This module, both source code and documentation, is in the
  • 0036 * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
  • 0037 * See <a href='http://www.saxproject.org'>http://www.saxproject.org</a>
  • 0038 * for further information.
  • 0039 * </blockquote>
  • 0040 *
  • 0041 * <p>This class wraps a SAX1 {@link org.xml.sax.Parser Parser}
  • 0042 * and makes it act as a SAX2 {@link org.xml.sax.XMLReader XMLReader},
  • 0043 * with feature, property, and Namespace support. Note
  • 0044 * that it is not possible to report {@link org.xml.sax.ContentHandler#skippedEntity
  • 0045 * skippedEntity} events, since SAX1 does not make that information available.</p>
  • 0046 *
  • 0047 * <p>This adapter does not test for duplicate Namespace-qualified
  • 0048 * attribute names.</p>
  • 0049 *
  • 0050 * @since SAX 2.0
  • 0051 * @author David Megginson
  • 0052 * @version 2.0.1 (sax2r2)
  • 0053 * @see org.xml.sax.helpers.XMLReaderAdapter
  • 0054 * @see org.xml.sax.XMLReader
  • 0055 * @see org.xml.sax.Parser
  • 0056 */
  • 0057public class ParserAdapter implements XMLReader, DocumentHandler
  • 0058{
  • 0059
  • 0060
  • 0061 ////////////////////////////////////////////////////////////////////
  • 0062 // Constructors.
  • 0063 ////////////////////////////////////////////////////////////////////
  • 0064
  • 0065
  • 0066 /**
  • 0067 * Construct a new parser adapter.
  • 0068 *
  • 0069 * <p>Use the "org.xml.sax.parser" property to locate the
  • 0070 * embedded SAX1 driver.</p>
  • 0071 *
  • 0072 * @exception SAXException If the embedded driver
  • 0073 * cannot be instantiated or if the
  • 0074 * org.xml.sax.parser property is not specified.
  • 0075 */
  • 0076 public ParserAdapter ()
  • 0077 throws SAXException
  • 0078 {
  • 0079 super();
  • 0080
  • 0081 String driver = System.getProperty("org.xml.sax.parser");
  • 0082
  • 0083 try {
  • 0084 setup(ParserFactory.makeParser());
  • 0085 } catch (ClassNotFoundException e1) {
  • 0086 throw new
  • 0087 SAXException("Cannot find SAX1 driver class " +
  • 0088 driver, e1);
  • 0089 } catch (IllegalAccessException e2) {
  • 0090 throw new
  • 0091 SAXException("SAX1 driver class " +
  • 0092 driver +
  • 0093 " found but cannot be loaded", e2);
  • 0094 } catch (InstantiationException e3) {
  • 0095 throw new
  • 0096 SAXException("SAX1 driver class " +
  • 0097 driver +
  • 0098 " loaded but cannot be instantiated", e3);
  • 0099 } catch (ClassCastException e4) {
  • 0100 throw new
  • 0101 SAXException("SAX1 driver class " +
  • 0102 driver +
  • 0103 " does not implement org.xml.sax.Parser");
  • 0104 } catch (NullPointerException e5) {
  • 0105 throw new
  • 0106 SAXException("System property org.xml.sax.parser not specified");
  • 0107 }
  • 0108 }
  • 0109
  • 0110
  • 0111 /**
  • 0112 * Construct a new parser adapter.
  • 0113 *
  • 0114 * <p>Note that the embedded parser cannot be changed once the
  • 0115 * adapter is created; to embed a different parser, allocate
  • 0116 * a new ParserAdapter.</p>
  • 0117 *
  • 0118 * @param parser The SAX1 parser to embed.
  • 0119 * @exception java.lang.NullPointerException If the parser parameter
  • 0120 * is null.
  • 0121 */
  • 0122 public ParserAdapter (Parser parser)
  • 0123 {
  • 0124 super();
  • 0125 setup(parser);
  • 0126 }
  • 0127
  • 0128
  • 0129 /**
  • 0130 * Internal setup method.
  • 0131 *
  • 0132 * @param parser The embedded parser.
  • 0133 * @exception java.lang.NullPointerException If the parser parameter
  • 0134 * is null.
  • 0135 */
  • 0136 private void setup (Parser parser)
  • 0137 {
  • 0138 if (parser == null) {
  • 0139 throw new
  • 0140 NullPointerException("Parser argument must not be null");
  • 0141 }
  • 0142 this.parser = parser;
  • 0143 atts = new AttributesImpl();
  • 0144 nsSupport = new NamespaceSupport();
  • 0145 attAdapter = new AttributeListAdapter();
  • 0146 }
  • 0147
  • 0148
  • 0149
  • 0150 ////////////////////////////////////////////////////////////////////
  • 0151 // Implementation of org.xml.sax.XMLReader.
  • 0152 ////////////////////////////////////////////////////////////////////
  • 0153
  • 0154
  • 0155 //
  • 0156 // Internal constants for the sake of convenience.
  • 0157 //
  • 0158 private final static String FEATURES = "http://xml.org/sax/features/";
  • 0159 private final static String NAMESPACES = FEATURES + "namespaces";
  • 0160 private final static String NAMESPACE_PREFIXES = FEATURES + "namespace-prefixes";
  • 0161 private final static String XMLNS_URIs = FEATURES + "xmlns-uris";
  • 0162
  • 0163
  • 0164 /**
  • 0165 * Set a feature flag for the parser.
  • 0166 *
  • 0167 * <p>The only features recognized are namespaces and
  • 0168 * namespace-prefixes.</p>
  • 0169 *
  • 0170 * @param name The feature name, as a complete URI.
  • 0171 * @param value The requested feature value.
  • 0172 * @exception SAXNotRecognizedException If the feature
  • 0173 * can't be assigned or retrieved.
  • 0174 * @exception SAXNotSupportedException If the feature
  • 0175 * can't be assigned that value.
  • 0176 * @see org.xml.sax.XMLReader#setFeature
  • 0177 */
  • 0178 public void setFeature (String name, boolean value)
  • 0179 throws SAXNotRecognizedException, SAXNotSupportedException
  • 0180 {
  • 0181 if (name.equals(NAMESPACES)) {
  • 0182 checkNotParsing("feature", name);
  • 0183 namespaces = value;
  • 0184 if (!namespaces && !prefixes) {
  • 0185 prefixes = true;
  • 0186 }
  • 0187 } else if (name.equals(NAMESPACE_PREFIXES)) {
  • 0188 checkNotParsing("feature", name);
  • 0189 prefixes = value;
  • 0190 if (!prefixes && !namespaces) {
  • 0191 namespaces = true;
  • 0192 }
  • 0193 } else if (name.equals(XMLNS_URIs)) {
  • 0194 checkNotParsing("feature", name);
  • 0195 uris = value;
  • 0196 } else {
  • 0197 throw new SAXNotRecognizedException("Feature: " + name);
  • 0198 }
  • 0199 }
  • 0200
  • 0201
  • 0202 /**
  • 0203 * Check a parser feature flag.
  • 0204 *
  • 0205 * <p>The only features recognized are namespaces and
  • 0206 * namespace-prefixes.</p>
  • 0207 *
  • 0208 * @param name The feature name, as a complete URI.
  • 0209 * @return The current feature value.
  • 0210 * @exception SAXNotRecognizedException If the feature
  • 0211 * value can't be assigned or retrieved.
  • 0212 * @exception SAXNotSupportedException If the
  • 0213 * feature is not currently readable.
  • 0214 * @see org.xml.sax.XMLReader#setFeature
  • 0215 */
  • 0216 public boolean getFeature (String name)
  • 0217 throws SAXNotRecognizedException, SAXNotSupportedException
  • 0218 {
  • 0219 if (name.equals(NAMESPACES)) {
  • 0220 return namespaces;
  • 0221 } else if (name.equals(NAMESPACE_PREFIXES)) {
  • 0222 return prefixes;
  • 0223 } else if (name.equals(XMLNS_URIs)) {
  • 0224 return uris;
  • 0225 } else {
  • 0226 throw new SAXNotRecognizedException("Feature: " + name);
  • 0227 }
  • 0228 }
  • 0229
  • 0230
  • 0231 /**
  • 0232 * Set a parser property.
  • 0233 *
  • 0234 * <p>No properties are currently recognized.</p>
  • 0235 *
  • 0236 * @param name The property name.
  • 0237 * @param value The property value.
  • 0238 * @exception SAXNotRecognizedException If the property
  • 0239 * value can't be assigned or retrieved.
  • 0240 * @exception SAXNotSupportedException If the property
  • 0241 * can't be assigned that value.
  • 0242 * @see org.xml.sax.XMLReader#setProperty
  • 0243 */
  • 0244 public void setProperty (String name, Object value)
  • 0245 throws SAXNotRecognizedException, SAXNotSupportedException
  • 0246 {
  • 0247 throw new SAXNotRecognizedException("Property: " + name);
  • 0248 }
  • 0249
  • 0250
  • 0251 /**
  • 0252 * Get a parser property.
  • 0253 *
  • 0254 * <p>No properties are currently recognized.</p>
  • 0255 *
  • 0256 * @param name The property name.
  • 0257 * @return The property value.
  • 0258 * @exception SAXNotRecognizedException If the property
  • 0259 * value can't be assigned or retrieved.
  • 0260 * @exception SAXNotSupportedException If the property
  • 0261 * value is not currently readable.
  • 0262 * @see org.xml.sax.XMLReader#getProperty
  • 0263 */
  • 0264 public Object getProperty (String name)
  • 0265 throws SAXNotRecognizedException, SAXNotSupportedException
  • 0266 {
  • 0267 throw new SAXNotRecognizedException("Property: " + name);
  • 0268 }
  • 0269
  • 0270
  • 0271 /**
  • 0272 * Set the entity resolver.
  • 0273 *
  • 0274 * @param resolver The new entity resolver.
  • 0275 * @see org.xml.sax.XMLReader#setEntityResolver
  • 0276 */
  • 0277 public void setEntityResolver (EntityResolver resolver)
  • 0278 {
  • 0279 entityResolver = resolver;
  • 0280 }
  • 0281
  • 0282
  • 0283 /**
  • 0284 * Return the current entity resolver.
  • 0285 *
  • 0286 * @return The current entity resolver, or null if none was supplied.
  • 0287 * @see org.xml.sax.XMLReader#getEntityResolver
  • 0288 */
  • 0289 public EntityResolver getEntityResolver ()
  • 0290 {
  • 0291 return entityResolver;
  • 0292 }
  • 0293
  • 0294
  • 0295 /**
  • 0296 * Set the DTD handler.
  • 0297 *
  • 0298 * @param handler the new DTD handler
  • 0299 * @see org.xml.sax.XMLReader#setEntityResolver
  • 0300 */
  • 0301 public void setDTDHandler (DTDHandler handler)
  • 0302 {
  • 0303 dtdHandler = handler;
  • 0304 }
  • 0305
  • 0306
  • 0307 /**
  • 0308 * Return the current DTD handler.
  • 0309 *
  • 0310 * @return the current DTD handler, or null if none was supplied
  • 0311 * @see org.xml.sax.XMLReader#getEntityResolver
  • 0312 */
  • 0313 public DTDHandler getDTDHandler ()
  • 0314 {
  • 0315 return dtdHandler;
  • 0316 }
  • 0317
  • 0318
  • 0319 /**
  • 0320 * Set the content handler.
  • 0321 *
  • 0322 * @param handler the new content handler
  • 0323 * @see org.xml.sax.XMLReader#setEntityResolver
  • 0324 */
  • 0325 public void setContentHandler (ContentHandler handler)
  • 0326 {
  • 0327 contentHandler = handler;
  • 0328 }
  • 0329
  • 0330
  • 0331 /**
  • 0332 * Return the current content handler.
  • 0333 *
  • 0334 * @return The current content handler, or null if none was supplied.
  • 0335 * @see org.xml.sax.XMLReader#getEntityResolver
  • 0336 */
  • 0337 public ContentHandler getContentHandler ()
  • 0338 {
  • 0339 return contentHandler;
  • 0340 }
  • 0341
  • 0342
  • 0343 /**
  • 0344 * Set the error handler.
  • 0345 *
  • 0346 * @param handler The new error handler.
  • 0347 * @see org.xml.sax.XMLReader#setEntityResolver
  • 0348 */
  • 0349 public void setErrorHandler (ErrorHandler handler)
  • 0350 {
  • 0351 errorHandler = handler;
  • 0352 }
  • 0353
  • 0354
  • 0355 /**
  • 0356 * Return the current error handler.
  • 0357 *
  • 0358 * @return The current error handler, or null if none was supplied.
  • 0359 * @see org.xml.sax.XMLReader#getEntityResolver
  • 0360 */
  • 0361 public ErrorHandler getErrorHandler ()
  • 0362 {
  • 0363 return errorHandler;
  • 0364 }
  • 0365
  • 0366
  • 0367 /**
  • 0368 * Parse an XML document.
  • 0369 *
  • 0370 * @param systemId The absolute URL of the document.
  • 0371 * @exception java.io.IOException If there is a problem reading
  • 0372 * the raw content of the document.
  • 0373 * @exception SAXException If there is a problem
  • 0374 * processing the document.
  • 0375 * @see #parse(org.xml.sax.InputSource)
  • 0376 * @see org.xml.sax.Parser#parse(java.lang.String)
  • 0377 */
  • 0378 public void parse (String systemId)
  • 0379 throws IOException, SAXException
  • 0380 {
  • 0381 parse(new InputSource(systemId));
  • 0382 }
  • 0383
  • 0384
  • 0385 /**
  • 0386 * Parse an XML document.
  • 0387 *
  • 0388 * @param input An input source for the document.
  • 0389 * @exception java.io.IOException If there is a problem reading
  • 0390 * the raw content of the document.
  • 0391 * @exception SAXException If there is a problem
  • 0392 * processing the document.
  • 0393 * @see #parse(java.lang.String)
  • 0394 * @see org.xml.sax.Parser#parse(org.xml.sax.InputSource)
  • 0395 */
  • 0396 public void parse (InputSource input)
  • 0397 throws IOException, SAXException
  • 0398 {
  • 0399 if (parsing) {
  • 0400 throw new SAXException("Parser is already in use");
  • 0401 }
  • 0402 setupParser();
  • 0403 parsing = true;
  • 0404 try {
  • 0405 parser.parse(input);
  • 0406 } finally {
  • 0407 parsing = false;
  • 0408 }
  • 0409 parsing = false;
  • 0410 }
  • 0411
  • 0412
  • 0413
  • 0414 ////////////////////////////////////////////////////////////////////
  • 0415 // Implementation of org.xml.sax.DocumentHandler.
  • 0416 ////////////////////////////////////////////////////////////////////
  • 0417
  • 0418
  • 0419 /**
  • 0420 * Adapter implementation method; do not call.
  • 0421 * Adapt a SAX1 document locator event.
  • 0422 *
  • 0423 * @param locator A document locator.
  • 0424 * @see org.xml.sax.ContentHandler#setDocumentLocator
  • 0425 */
  • 0426 public void setDocumentLocator (Locator locator)
  • 0427 {
  • 0428 this.locator = locator;
  • 0429 if (contentHandler != null) {
  • 0430 contentHandler.setDocumentLocator(locator);
  • 0431 }
  • 0432 }
  • 0433
  • 0434
  • 0435 /**
  • 0436 * Adapter implementation method; do not call.
  • 0437 * Adapt a SAX1 start document event.
  • 0438 *
  • 0439 * @exception SAXException The client may raise a
  • 0440 * processing exception.
  • 0441 * @see org.xml.sax.DocumentHandler#startDocument
  • 0442 */
  • 0443 public void startDocument ()
  • 0444 throws SAXException
  • 0445 {
  • 0446 if (contentHandler != null) {
  • 0447 contentHandler.startDocument();
  • 0448 }
  • 0449 }
  • 0450
  • 0451
  • 0452 /**
  • 0453 * Adapter implementation method; do not call.
  • 0454 * Adapt a SAX1 end document event.
  • 0455 *
  • 0456 * @exception SAXException The client may raise a
  • 0457 * processing exception.
  • 0458 * @see org.xml.sax.DocumentHandler#endDocument
  • 0459 */
  • 0460 public void endDocument ()
  • 0461 throws SAXException
  • 0462 {
  • 0463 if (contentHandler != null) {
  • 0464 contentHandler.endDocument();
  • 0465 }
  • 0466 }
  • 0467
  • 0468
  • 0469 /**
  • 0470 * Adapter implementation method; do not call.
  • 0471 * Adapt a SAX1 startElement event.
  • 0472 *
  • 0473 * <p>If necessary, perform Namespace processing.</p>
  • 0474 *
  • 0475 * @param qName The qualified (prefixed) name.
  • 0476 * @param qAtts The XML attribute list (with qnames).
  • 0477 * @exception SAXException The client may raise a
  • 0478 * processing exception.
  • 0479 */
  • 0480 public void startElement (String qName, AttributeList qAtts)
  • 0481 throws SAXException
  • 0482 {
  • 0483 // These are exceptions from the
  • 0484 // first pass; they should be
  • 0485 // ignored if there's a second pass,
  • 0486 // but reported otherwise.
  • 0487 Vector exceptions = null;
  • 0488
  • 0489 // If we're not doing Namespace
  • 0490 // processing, dispatch this quickly.
  • 0491 if (!namespaces) {
  • 0492 if (contentHandler != null) {
  • 0493 attAdapter.setAttributeList(qAtts);
  • 0494 contentHandler.startElement("", "", qName.intern(),
  • 0495 attAdapter);
  • 0496 }
  • 0497 return;
  • 0498 }
  • 0499
  • 0500
  • 0501 // OK, we're doing Namespace processing.
  • 0502 nsSupport.pushContext();
  • 0503 int length = qAtts.getLength();
  • 0504
  • 0505 // First pass: handle NS decls
  • 0506 for (int i = 0; i < length; i++) {
  • 0507 String attQName = qAtts.getName(i);
  • 0508
  • 0509 if (!attQName.startsWith("xmlns"))
  • 0510 continue;
  • 0511 // Could be a declaration...
  • 0512 String prefix;
  • 0513 int n = attQName.indexOf(':');
  • 0514
  • 0515 // xmlns=...
  • 0516 if (n == -1 && attQName.length () == 5) {
  • 0517 prefix = "";
  • 0518 } else if (n != 5) {
  • 0519 // XML namespaces spec doesn't discuss "xmlnsf:oo"
  • 0520 // (and similarly named) attributes ... at most, warn
  • 0521 continue;
  • 0522 } else // xmlns:foo=...
  • 0523 prefix = attQName.substring(n+1);
  • 0524
  • 0525 String value = qAtts.getValue(i);
  • 0526 if (!nsSupport.declarePrefix(prefix, value)) {
  • 0527 reportError("Illegal Namespace prefix: " + prefix);
  • 0528 continue;
  • 0529 }
  • 0530 if (contentHandler != null)
  • 0531 contentHandler.startPrefixMapping(prefix, value);
  • 0532 }
  • 0533
  • 0534 // Second pass: copy all relevant
  • 0535 // attributes into the SAX2 AttributeList
  • 0536 // using updated prefix bindings
  • 0537 atts.clear();
  • 0538 for (int i = 0; i < length; i++) {
  • 0539 String attQName = qAtts.getName(i);
  • 0540 String type = qAtts.getType(i);
  • 0541 String value = qAtts.getValue(i);
  • 0542
  • 0543 // Declaration?
  • 0544 if (attQName.startsWith("xmlns")) {
  • 0545 String prefix;
  • 0546 int n = attQName.indexOf(':');
  • 0547
  • 0548 if (n == -1 && attQName.length () == 5) {
  • 0549 prefix = "";
  • 0550 } else if (n != 5) {
  • 0551 // XML namespaces spec doesn't discuss "xmlnsf:oo"
  • 0552 // (and similarly named) attributes ... ignore
  • 0553 prefix = null;
  • 0554 } else {
  • 0555 prefix = attQName.substring(6);
  • 0556 }
  • 0557 // Yes, decl: report or prune
  • 0558 if (prefix != null) {
  • 0559 if (prefixes) {
  • 0560 if (uris)
  • 0561 // note funky case: localname can be null
  • 0562 // when declaring the default prefix, and
  • 0563 // yet the uri isn't null.
  • 0564 atts.addAttribute (nsSupport.XMLNS, prefix,
  • 0565 attQName.intern(), type, value);
  • 0566 else
  • 0567 atts.addAttribute ("", "",
  • 0568 attQName.intern(), type, value);
  • 0569 }
  • 0570 continue;
  • 0571 }
  • 0572 }
  • 0573
  • 0574 // Not a declaration -- report
  • 0575 try {
  • 0576 String attName[] = processName(attQName, true, true);
  • 0577 atts.addAttribute(attName[0], attName[1], attName[2],
  • 0578 type, value);
  • 0579 } catch (SAXException e) {
  • 0580 if (exceptions == null)
  • 0581 exceptions = new Vector();
  • 0582 exceptions.addElement(e);
  • 0583 atts.addAttribute("", attQName, attQName, type, value);
  • 0584 }
  • 0585 }
  • 0586
  • 0587 // now handle the deferred exception reports
  • 0588 if (exceptions != null && errorHandler != null) {
  • 0589 for (int i = 0; i < exceptions.size(); i++)
  • 0590 errorHandler.error((SAXParseException)
  • 0591 (exceptions.elementAt(i)));
  • 0592 }
  • 0593
  • 0594 // OK, finally report the event.
  • 0595 if (contentHandler != null) {
  • 0596 String name[] = processName(qName, false, false);
  • 0597 contentHandler.startElement(name[0], name[1], name[2], atts);
  • 0598 }
  • 0599 }
  • 0600
  • 0601
  • 0602 /**
  • 0603 * Adapter implementation method; do not call.
  • 0604 * Adapt a SAX1 end element event.
  • 0605 *
  • 0606 * @param qName The qualified (prefixed) name.
  • 0607 * @exception SAXException The client may raise a
  • 0608 * processing exception.
  • 0609 * @see org.xml.sax.DocumentHandler#endElement
  • 0610 */
  • 0611 public void endElement (String qName)
  • 0612 throws SAXException
  • 0613 {
  • 0614 // If we're not doing Namespace
  • 0615 // processing, dispatch this quickly.
  • 0616 if (!namespaces) {
  • 0617 if (contentHandler != null) {
  • 0618 contentHandler.endElement("", "", qName.intern());
  • 0619 }
  • 0620 return;
  • 0621 }
  • 0622
  • 0623 // Split the name.
  • 0624 String names[] = processName(qName, false, false);
  • 0625 if (contentHandler != null) {
  • 0626 contentHandler.endElement(names[0], names[1], names[2]);
  • 0627 Enumeration prefixes = nsSupport.getDeclaredPrefixes();
  • 0628 while (prefixes.hasMoreElements()) {
  • 0629 String prefix = (String)prefixes.nextElement();
  • 0630 contentHandler.endPrefixMapping(prefix);
  • 0631 }
  • 0632 }
  • 0633 nsSupport.popContext();
  • 0634 }
  • 0635
  • 0636
  • 0637 /**
  • 0638 * Adapter implementation method; do not call.
  • 0639 * Adapt a SAX1 characters event.
  • 0640 *
  • 0641 * @param ch An array of characters.
  • 0642 * @param start The starting position in the array.
  • 0643 * @param length The number of characters to use.
  • 0644 * @exception SAXException The client may raise a
  • 0645 * processing exception.
  • 0646 * @see org.xml.sax.DocumentHandler#characters
  • 0647 */
  • 0648 public void characters (char ch[], int start, int length)
  • 0649 throws SAXException
  • 0650 {
  • 0651 if (contentHandler != null) {
  • 0652 contentHandler.characters(ch, start, length);
  • 0653 }
  • 0654 }
  • 0655
  • 0656
  • 0657 /**
  • 0658 * Adapter implementation method; do not call.
  • 0659 * Adapt a SAX1 ignorable whitespace event.
  • 0660 *
  • 0661 * @param ch An array of characters.
  • 0662 * @param start The starting position in the array.
  • 0663 * @param length The number of characters to use.
  • 0664 * @exception SAXException The client may raise a
  • 0665 * processing exception.
  • 0666 * @see org.xml.sax.DocumentHandler#ignorableWhitespace
  • 0667 */
  • 0668 public void ignorableWhitespace (char ch[], int start, int length)
  • 0669 throws SAXException
  • 0670 {
  • 0671 if (contentHandler != null) {
  • 0672 contentHandler.ignorableWhitespace(ch, start, length);
  • 0673 }
  • 0674 }
  • 0675
  • 0676
  • 0677 /**
  • 0678 * Adapter implementation method; do not call.
  • 0679 * Adapt a SAX1 processing instruction event.
  • 0680 *
  • 0681 * @param target The processing instruction target.
  • 0682 * @param data The remainder of the processing instruction
  • 0683 * @exception SAXException The client may raise a
  • 0684 * processing exception.
  • 0685 * @see org.xml.sax.DocumentHandler#processingInstruction
  • 0686 */
  • 0687 public void processingInstruction (String target, String data)
  • 0688 throws SAXException
  • 0689 {
  • 0690 if (contentHandler != null) {
  • 0691 contentHandler.processingInstruction(target, data);
  • 0692 }
  • 0693 }
  • 0694
  • 0695
  • 0696
  • 0697 ////////////////////////////////////////////////////////////////////
  • 0698 // Internal utility methods.
  • 0699 ////////////////////////////////////////////////////////////////////
  • 0700
  • 0701
  • 0702 /**
  • 0703 * Initialize the parser before each run.
  • 0704 */
  • 0705 private void setupParser ()
  • 0706 {
  • 0707 // catch an illegal "nonsense" state.
  • 0708 if (!prefixes && !namespaces)
  • 0709 throw new IllegalStateException ();
  • 0710
  • 0711 nsSupport.reset();
  • 0712 if (uris)
  • 0713 nsSupport.setNamespaceDeclUris (true);
  • 0714
  • 0715 if (entityResolver != null) {
  • 0716 parser.setEntityResolver(entityResolver);
  • 0717 }
  • 0718 if (dtdHandler != null) {
  • 0719 parser.setDTDHandler(dtdHandler);
  • 0720 }
  • 0721 if (errorHandler != null) {
  • 0722 parser.setErrorHandler(errorHandler);
  • 0723 }
  • 0724 parser.setDocumentHandler(this);
  • 0725 locator = null;
  • 0726 }
  • 0727
  • 0728
  • 0729 /**
  • 0730 * Process a qualified (prefixed) name.
  • 0731 *
  • 0732 * <p>If the name has an undeclared prefix, use only the qname
  • 0733 * and make an ErrorHandler.error callback in case the app is
  • 0734 * interested.</p>
  • 0735 *
  • 0736 * @param qName The qualified (prefixed) name.
  • 0737 * @param isAttribute true if this is an attribute name.
  • 0738 * @return The name split into three parts.
  • 0739 * @exception SAXException The client may throw
  • 0740 * an exception if there is an error callback.
  • 0741 */
  • 0742 private String [] processName (String qName, boolean isAttribute,
  • 0743 boolean useException)
  • 0744 throws SAXException
  • 0745 {
  • 0746 String parts[] = nsSupport.processName(qName, nameParts,
  • 0747 isAttribute);
  • 0748 if (parts == null) {
  • 0749 if (useException)
  • 0750 throw makeException("Undeclared prefix: " + qName);
  • 0751 reportError("Undeclared prefix: " + qName);
  • 0752 parts = new String[3];
  • 0753 parts[0] = parts[1] = "";
  • 0754 parts[2] = qName.intern();
  • 0755 }
  • 0756 return parts;
  • 0757 }
  • 0758
  • 0759
  • 0760 /**
  • 0761 * Report a non-fatal error.
  • 0762 *
  • 0763 * @param message The error message.
  • 0764 * @exception SAXException The client may throw
  • 0765 * an exception.
  • 0766 */
  • 0767 void reportError (String message)
  • 0768 throws SAXException
  • 0769 {
  • 0770 if (errorHandler != null)
  • 0771 errorHandler.error(makeException(message));
  • 0772 }
  • 0773
  • 0774
  • 0775 /**
  • 0776 * Construct an exception for the current context.
  • 0777 *
  • 0778 * @param message The error message.
  • 0779 */
  • 0780 private SAXParseException makeException (String message)
  • 0781 {
  • 0782 if (locator != null) {
  • 0783 return new SAXParseException(message, locator);
  • 0784 } else {
  • 0785 return new SAXParseException(message, null, null, -1, -1);
  • 0786 }
  • 0787 }
  • 0788
  • 0789
  • 0790 /**
  • 0791 * Throw an exception if we are parsing.
  • 0792 *
  • 0793 * <p>Use this method to detect illegal feature or
  • 0794 * property changes.</p>
  • 0795 *
  • 0796 * @param type The type of thing (feature or property).
  • 0797 * @param name The feature or property name.
  • 0798 * @exception SAXNotSupportedException If a
  • 0799 * document is currently being parsed.
  • 0800 */
  • 0801 private void checkNotParsing (String type, String name)
  • 0802 throws SAXNotSupportedException
  • 0803 {
  • 0804 if (parsing) {
  • 0805 throw new SAXNotSupportedException("Cannot change " +
  • 0806 type + ' ' +
  • 0807 name + " while parsing");
  • 0808
  • 0809 }
  • 0810 }
  • 0811
  • 0812
  • 0813
  • 0814 ////////////////////////////////////////////////////////////////////
  • 0815 // Internal state.
  • 0816 ////////////////////////////////////////////////////////////////////
  • 0817
  • 0818 private NamespaceSupport nsSupport;
  • 0819 private AttributeListAdapter attAdapter;
  • 0820
  • 0821 private boolean parsing = false;
  • 0822 private String nameParts[] = new String[3];
  • 0823
  • 0824 private Parser parser = null;
  • 0825
  • 0826 private AttributesImpl atts = null;
  • 0827
  • 0828 // Features
  • 0829 private boolean namespaces = true;
  • 0830 private boolean prefixes = false;
  • 0831 private boolean uris = false;
  • 0832
  • 0833 // Properties
  • 0834
  • 0835 // Handlers
  • 0836 Locator locator;
  • 0837
  • 0838 EntityResolver entityResolver = null;
  • 0839 DTDHandler dtdHandler = null;
  • 0840 ContentHandler contentHandler = null;
  • 0841 ErrorHandler errorHandler = null;
  • 0842
  • 0843
  • 0844
  • 0845 ////////////////////////////////////////////////////////////////////
  • 0846 // Inner class to wrap an AttributeList when not doing NS proc.
  • 0847 ////////////////////////////////////////////////////////////////////
  • 0848
  • 0849
  • 0850 /**
  • 0851 * Adapt a SAX1 AttributeList as a SAX2 Attributes object.
  • 0852 *
  • 0853 * <p>This class is in the Public Domain, and comes with NO
  • 0854 * WARRANTY of any kind.</p>
  • 0855 *
  • 0856 * <p>This wrapper class is used only when Namespace support
  • 0857 * is disabled -- it provides pretty much a direct mapping
  • 0858 * from SAX1 to SAX2, except that names and types are
  • 0859 * interned whenever requested.</p>
  • 0860 */
  • 0861 final class AttributeListAdapter implements Attributes
  • 0862 {
  • 0863
  • 0864 /**
  • 0865 * Construct a new adapter.
  • 0866 */
  • 0867 AttributeListAdapter ()
  • 0868 {
  • 0869 }
  • 0870
  • 0871
  • 0872 /**
  • 0873 * Set the embedded AttributeList.
  • 0874 *
  • 0875 * <p>This method must be invoked before any of the others
  • 0876 * can be used.</p>
  • 0877 *
  • 0878 * @param The SAX1 attribute list (with qnames).
  • 0879 */
  • 0880 void setAttributeList (AttributeList qAtts)
  • 0881 {
  • 0882 this.qAtts = qAtts;
  • 0883 }
  • 0884
  • 0885
  • 0886 /**
  • 0887 * Return the length of the attribute list.
  • 0888 *
  • 0889 * @return The number of attributes in the list.
  • 0890 * @see org.xml.sax.Attributes#getLength
  • 0891 */
  • 0892 public int getLength ()
  • 0893 {
  • 0894 return qAtts.getLength();
  • 0895 }
  • 0896
  • 0897
  • 0898 /**
  • 0899 * Return the Namespace URI of the specified attribute.
  • 0900 *
  • 0901 * @param The attribute's index.
  • 0902 * @return Always the empty string.
  • 0903 * @see org.xml.sax.Attributes#getURI
  • 0904 */
  • 0905 public String getURI (int i)
  • 0906 {
  • 0907 return "";
  • 0908 }
  • 0909
  • 0910
  • 0911 /**
  • 0912 * Return the local name of the specified attribute.
  • 0913 *
  • 0914 * @param The attribute's index.
  • 0915 * @return Always the empty string.
  • 0916 * @see org.xml.sax.Attributes#getLocalName
  • 0917 */
  • 0918 public String getLocalName (int i)
  • 0919 {
  • 0920 return "";
  • 0921 }
  • 0922
  • 0923
  • 0924 /**
  • 0925 * Return the qualified (prefixed) name of the specified attribute.
  • 0926 *
  • 0927 * @param The attribute's index.
  • 0928 * @return The attribute's qualified name, internalized.
  • 0929 */
  • 0930 public String getQName (int i)
  • 0931 {
  • 0932 return qAtts.getName(i).intern();
  • 0933 }
  • 0934
  • 0935
  • 0936 /**
  • 0937 * Return the type of the specified attribute.
  • 0938 *
  • 0939 * @param The attribute's index.
  • 0940 * @return The attribute's type as an internalized string.
  • 0941 */
  • 0942 public String getType (int i)
  • 0943 {
  • 0944 return qAtts.getType(i).intern();
  • 0945 }
  • 0946
  • 0947
  • 0948 /**
  • 0949 * Return the value of the specified attribute.
  • 0950 *
  • 0951 * @param The attribute's index.
  • 0952 * @return The attribute's value.
  • 0953 */
  • 0954 public String getValue (int i)
  • 0955 {
  • 0956 return qAtts.getValue(i);
  • 0957 }
  • 0958
  • 0959
  • 0960 /**
  • 0961 * Look up an attribute index by Namespace name.
  • 0962 *
  • 0963 * @param uri The Namespace URI or the empty string.
  • 0964 * @param localName The local name.
  • 0965 * @return The attributes index, or -1 if none was found.
  • 0966 * @see org.xml.sax.Attributes#getIndex(java.lang.String,java.lang.String)
  • 0967 */
  • 0968 public int getIndex (String uri, String localName)
  • 0969 {
  • 0970 return -1;
  • 0971 }
  • 0972
  • 0973
  • 0974 /**
  • 0975 * Look up an attribute index by qualified (prefixed) name.
  • 0976 *
  • 0977 * @param qName The qualified name.
  • 0978 * @return The attributes index, or -1 if none was found.
  • 0979 * @see org.xml.sax.Attributes#getIndex(java.lang.String)
  • 0980 */
  • 0981 public int getIndex (String qName)
  • 0982 {
  • 0983 int max = atts.getLength();
  • 0984 for (int i = 0; i < max; i++) {
  • 0985 if (qAtts.getName(i).equals(qName)) {
  • 0986 return i;
  • 0987 }
  • 0988 }
  • 0989 return -1;
  • 0990 }
  • 0991
  • 0992
  • 0993 /**
  • 0994 * Look up the type of an attribute by Namespace name.
  • 0995 *
  • 0996 * @param uri The Namespace URI
  • 0997 * @param localName The local name.
  • 0998 * @return The attribute's type as an internalized string.
  • 0999 */
  • 1000 public String getType (String uri, String localName)
  • 1001 {
  • 1002 return null;
  • 1003 }
  • 1004
  • 1005
  • 1006 /**
  • 1007 * Look up the type of an attribute by qualified (prefixed) name.
  • 1008 *
  • 1009 * @param qName The qualified name.
  • 1010 * @return The attribute's type as an internalized string.
  • 1011 */
  • 1012 public String getType (String qName)
  • 1013 {
  • 1014 return qAtts.getType(qName).intern();
  • 1015 }
  • 1016
  • 1017
  • 1018 /**
  • 1019 * Look up the value of an attribute by Namespace name.
  • 1020 *
  • 1021 * @param uri The Namespace URI
  • 1022 * @param localName The local name.
  • 1023 * @return The attribute's value.
  • 1024 */
  • 1025 public String getValue (String uri, String localName)
  • 1026 {
  • 1027 return null;
  • 1028 }
  • 1029
  • 1030
  • 1031 /**
  • 1032 * Look up the value of an attribute by qualified (prefixed) name.
  • 1033 *
  • 1034 * @param qName The qualified name.
  • 1035 * @return The attribute's value.
  • 1036 */
  • 1037 public String getValue (String qName)
  • 1038 {
  • 1039 return qAtts.getValue(qName);
  • 1040 }
  • 1041
  • 1042 private AttributeList qAtts;
  • 1043 }
  • 1044}
  • 1045
  • 1046// end of ParserAdapter.java

文件:ParserAdapter.java
包名:org.xml.sax.helpers
类名:ParserAdapter
继承:
接口:[XMLReader][DocumentHandler]