Source for gnu.xml.stream.SAXParser

   1: /* SAXParser.java -- 
   2:    Copyright (C) 2005, 2006  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: package gnu.xml.stream;
  39: 
  40: import java.io.InputStream;
  41: import java.io.IOException;
  42: import java.io.Reader;
  43: import java.net.URL;
  44: import java.util.Iterator;
  45: import java.util.Map;
  46: import javax.xml.XMLConstants;
  47: import javax.xml.namespace.QName;
  48: import javax.xml.stream.Location;
  49: import javax.xml.stream.XMLEventReader;
  50: import javax.xml.stream.XMLReporter;
  51: import javax.xml.stream.XMLResolver;
  52: import javax.xml.stream.XMLStreamConstants;
  53: import javax.xml.stream.XMLStreamException;
  54: import javax.xml.stream.XMLStreamReader;
  55: 
  56: import org.xml.sax.ContentHandler;
  57: import org.xml.sax.DTDHandler;
  58: import org.xml.sax.EntityResolver;
  59: import org.xml.sax.ErrorHandler;
  60: import org.xml.sax.InputSource;
  61: import org.xml.sax.Parser;
  62: import org.xml.sax.SAXException;
  63: import org.xml.sax.SAXNotRecognizedException;
  64: import org.xml.sax.SAXNotSupportedException;
  65: import org.xml.sax.SAXParseException;
  66: import org.xml.sax.XMLReader;
  67: import org.xml.sax.ext.Attributes2;
  68: import org.xml.sax.ext.DeclHandler;
  69: import org.xml.sax.ext.EntityResolver2;
  70: import org.xml.sax.ext.LexicalHandler;
  71: import org.xml.sax.ext.Locator2;
  72: 
  73: /**
  74:  * JAXP SAX parser using an underlying StAX parser.
  75:  * This parser supports the following additional SAX features and
  76:  * properties:
  77:  * <table>
  78:  * <tr><th colspan='4'>Features</th></tr>
  79:  * <tr><td>http://gnu.org/sax/features/xml-base</td>
  80:  * <td colspan='2'>read/write</td>
  81:  * <td>Indicates or sets whether XML Base processing is enabled</td></tr>
  82:  * <tr><th colspan='4'>Properties</th></tr>
  83:  * <tr><td>http://gnu.org/sax/properties/base-uri</td>
  84:  * <td>read-only</td><td>String</td>
  85:  * <td>Returns the base URI of the current event</td></tr>
  86:  * <tr><td>http://gnu.org/sax/properties/document-xml-encoding</td>
  87:  * <td>read-only</td><td>String</td>
  88:  * <td>Returns the encoding specified in the XML declaration</td></tr>
  89:  * </table>
  90:  *
  91:  * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
  92:  */
  93: public class SAXParser
  94:   extends javax.xml.parsers.SAXParser
  95:   implements XMLReader, Attributes2, Locator2, XMLReporter, XMLResolver
  96: {
  97: 
  98:   ContentHandler contentHandler;
  99:   DeclHandler declHandler;
 100:   DTDHandler dtdHandler;
 101:   EntityResolver entityResolver;
 102:   ErrorHandler errorHandler;
 103:   LexicalHandler lexicalHandler;
 104: 
 105:   boolean validating = false;
 106:   boolean namespaceAware = true;
 107:   boolean xIncludeAware = false;
 108:   boolean stringInterning = true;
 109:   boolean coalescing = true;
 110:   boolean replaceERefs = true;
 111:   boolean externalEntities = true;
 112:   boolean supportDTD = true;
 113:   boolean baseAware = true;
 114: 
 115:   XMLParser parser;
 116:   XMLStreamReader reader;
 117:   String encoding;
 118:   String xmlVersion;
 119:   boolean xmlStandalone;
 120:   String xmlEncoding;
 121:   String baseURI;
 122: 
 123:   public SAXParser()
 124:   {
 125:   }
 126: 
 127:   SAXParser(boolean validating, boolean namespaceAware, boolean xIncludeAware)
 128:   {
 129:     this.validating = validating;
 130:     this.namespaceAware = namespaceAware;
 131:     this.xIncludeAware = xIncludeAware;
 132:   }
 133: 
 134:   // -- SAXParser --
 135:   
 136:   public Parser getParser()
 137:     throws SAXException
 138:   {
 139:     return null;
 140:   }
 141:   
 142:   public XMLReader getXMLReader()
 143:     throws SAXException
 144:   {
 145:     return this;
 146:   }
 147: 
 148:   public boolean isNamespaceAware()
 149:   {
 150:     return namespaceAware;
 151:   }
 152: 
 153:   public boolean isValidating()
 154:   {
 155:     return validating;
 156:   }
 157: 
 158:   public void setProperty(String name, Object value)
 159:     throws SAXNotRecognizedException, SAXNotSupportedException
 160:   {
 161:     if (parser != null)
 162:       throw new IllegalStateException("parsing in progress");
 163:     final String FEATURES = "http://xml.org/sax/features/";
 164:     final String PROPERTIES = "http://xml.org/sax/properties/";
 165:     final String GNU_FEATURES = "http://gnu.org/sax/features/";
 166:     if ((FEATURES + "namespaces").equals(name))
 167:       namespaceAware = Boolean.TRUE.equals(value);
 168:     else if ((FEATURES + "namespace-prefixes").equals(name))
 169:       {
 170:         // NOOP
 171:       }
 172:     else if ((FEATURES + "string-interning").equals(name))
 173:       stringInterning = Boolean.TRUE.equals(value);
 174:     else if ((FEATURES + "use-attributes2").equals(name))
 175:       {
 176:         // NOOP
 177:       }
 178:     else if ((FEATURES + "validation").equals(name))
 179:       validating = Boolean.TRUE.equals(value);
 180:     else if ((FEATURES + "external-general-entities").equals(name))
 181:       externalEntities = Boolean.TRUE.equals(value);
 182:     else if ((FEATURES + "external-parameter-entities").equals(name))
 183:       externalEntities = Boolean.TRUE.equals(value);
 184:     else if ((PROPERTIES + "declaration-handler").equals(name))
 185:       declHandler = (DeclHandler) value;
 186:     else if ((PROPERTIES + "lexical-handler").equals(name))
 187:       lexicalHandler = (LexicalHandler) value;
 188:     else if ((GNU_FEATURES + "xml-base").equals(name))
 189:       baseAware = Boolean.TRUE.equals(value);
 190:     else
 191:       throw new SAXNotSupportedException(name);
 192:   }
 193: 
 194:   public Object getProperty(String name)
 195:     throws SAXNotRecognizedException, SAXNotSupportedException
 196:   {
 197:     final String FEATURES = "http://xml.org/sax/features/";
 198:     final String PROPERTIES = "http://xml.org/sax/properties/";
 199:     final String GNU_FEATURES = "http://gnu.org/sax/features/";
 200:     final String GNU_PROPERTIES = "http://gnu.org/sax/properties/";
 201:     if ((GNU_FEATURES + "base-uri").equals(name))
 202:       return baseURI;
 203:     if ((FEATURES + "is-standalone").equals(name))
 204:       return xmlStandalone ? Boolean.TRUE : Boolean.FALSE;
 205:     if ((FEATURES + "namespaces").equals(name))
 206:       return namespaceAware ? Boolean.TRUE : Boolean.FALSE;
 207:     if ((FEATURES + "namespace-prefixes").equals(name))
 208:       return Boolean.TRUE;
 209:     if ((FEATURES + "string-interning").equals(name))
 210:       return stringInterning ? Boolean.TRUE : Boolean.FALSE;
 211:     if ((FEATURES + "use-attributes2").equals(name))
 212:       return Boolean.TRUE;
 213:     if ((FEATURES + "use-locator2").equals(name))
 214:       return Boolean.TRUE;
 215:     if ((FEATURES + "use-entity-resolver2").equals(name))
 216:       return Boolean.FALSE;
 217:     if ((FEATURES + "validation").equals(name))
 218:       return validating ? Boolean.TRUE : Boolean.FALSE;
 219:     if ((FEATURES + "external-general-entities").equals(name))
 220:       return externalEntities ? Boolean.TRUE : Boolean.FALSE;
 221:     if ((FEATURES + "external-parameter-entities").equals(name))
 222:       return externalEntities ? Boolean.TRUE : Boolean.FALSE;
 223:     if ((FEATURES + "xml-1.1").equals(name))
 224:       return Boolean.TRUE;
 225:     if ((PROPERTIES + "declaration-handler").equals(name))
 226:       return declHandler;
 227:     if ((PROPERTIES + "document-xml-version").equals(name))
 228:       return xmlVersion;
 229:     if ((PROPERTIES + "lexical-handler").equals(name))
 230:       return lexicalHandler;
 231:     if ((GNU_FEATURES + "xml-base").equals(name))
 232:       return baseAware ? Boolean.TRUE : Boolean.FALSE;
 233:     if ((GNU_PROPERTIES + "document-xml-encoding").equals(name))
 234:       return xmlEncoding;
 235:     throw new SAXNotSupportedException(name);
 236:   }
 237: 
 238:   public boolean isXIncludeAware()
 239:   {
 240:     return xIncludeAware;
 241:   }
 242: 
 243:   public void reset()
 244:   {
 245:     parser = null;
 246:     encoding = null;
 247:     xmlVersion = null;
 248:     xmlStandalone = false;
 249:   }
 250: 
 251:   // -- XMLReader --
 252: 
 253:   public boolean getFeature(String name)
 254:     throws SAXNotRecognizedException, SAXNotSupportedException
 255:   {
 256:     Object ret = getProperty(name);
 257:     if (ret instanceof Boolean)
 258:       return ((Boolean) ret).booleanValue();
 259:     throw new SAXNotSupportedException(name);
 260:   }
 261: 
 262:   public void setFeature(String name, boolean value)
 263:     throws SAXNotRecognizedException, SAXNotSupportedException
 264:   {
 265:     setProperty(name, value ? Boolean.TRUE : Boolean.FALSE);
 266:   }
 267: 
 268:   public void setEntityResolver(EntityResolver resolver)
 269:   {
 270:     entityResolver = resolver;
 271:   }
 272: 
 273:   public EntityResolver getEntityResolver()
 274:   {
 275:     return entityResolver;
 276:   }
 277: 
 278:   public void setDTDHandler(DTDHandler handler)
 279:   {
 280:     dtdHandler = handler;
 281:   }
 282: 
 283:   public DTDHandler getDTDHandler()
 284:   {
 285:     return dtdHandler;
 286:   }
 287: 
 288:   public void setContentHandler(ContentHandler handler)
 289:   {
 290:     contentHandler = handler;
 291:   }
 292: 
 293:   public ContentHandler getContentHandler()
 294:   {
 295:     return contentHandler;
 296:   }
 297: 
 298:   public void setErrorHandler(ErrorHandler handler)
 299:   {
 300:     errorHandler = handler;
 301:   }
 302: 
 303:   public ErrorHandler getErrorHandler()
 304:   {
 305:     return errorHandler;
 306:   }
 307: 
 308:   public synchronized void parse(InputSource input)
 309:     throws IOException, SAXException
 310:   {
 311:     reset();
 312:     String systemId = input.getSystemId();
 313:     InputStream in = input.getByteStream();
 314:     boolean opened = false;
 315:     if (in != null)
 316:       parser = new XMLParser(in, systemId,
 317:                              validating,
 318:                              namespaceAware,
 319:                              coalescing,
 320:                              replaceERefs,
 321:                              externalEntities,
 322:                              supportDTD,
 323:                              baseAware,
 324:                              stringInterning,
 325:                              true,
 326:                              this,
 327:                              this);
 328:     else
 329:       {
 330:         Reader r = input.getCharacterStream();
 331:         if (r != null)
 332:           parser = new XMLParser(r, systemId,
 333:                                  validating,
 334:                                  namespaceAware,
 335:                                  coalescing,
 336:                                  replaceERefs,
 337:                                  externalEntities,
 338:                                  supportDTD,
 339:                                  baseAware,
 340:                                  stringInterning,
 341:                                  true,
 342:                                  this,
 343:                                  this);
 344:       }
 345:     if (parser == null)
 346:       {
 347:         if (systemId == null)
 348:           throw new SAXException("No stream or system ID specified");
 349:         systemId = XMLParser.absolutize(null, systemId);
 350:         in = new URL(systemId).openStream();
 351:         opened = true;
 352:         parser = new XMLParser(in, systemId,
 353:                                validating,
 354:                                namespaceAware,
 355:                                coalescing,
 356:                                replaceERefs,
 357:                                externalEntities,
 358:                                supportDTD,
 359:                                baseAware,
 360:                                stringInterning,
 361:                                true,
 362:                                this,
 363:                                this);
 364:       }
 365:     reader = parser;
 366:     baseURI = systemId;
 367:     
 368:     if (xIncludeAware)
 369:       reader = new XIncludeFilter(parser, systemId, namespaceAware,
 370:                                   validating, true);
 371:     
 372:     if (contentHandler != null)
 373:       contentHandler.setDocumentLocator(this);
 374:     boolean startDocumentDone = false;
 375:     try
 376:       {
 377:         while (parser.hasNext())
 378:           {
 379:             int event = parser.next();
 380:             if (baseAware)
 381:               baseURI = parser.getXMLBase();
 382:             switch (event)
 383:               {
 384:               case XMLStreamConstants.CHARACTERS:
 385:                 if (contentHandler != null)
 386:                   {
 387:                     char[] b = reader.getTextCharacters();
 388:                     contentHandler.characters(b, 0, b.length);
 389:                   }
 390:                 break;
 391:               case XMLStreamConstants.SPACE:
 392:                 if (contentHandler != null)
 393:                   {
 394:                     char[] b = reader.getTextCharacters();
 395:                     if (isIgnorableWhitespace(parser, b, false))
 396:                       contentHandler.ignorableWhitespace(b, 0, b.length);
 397:                     else
 398:                       contentHandler.characters(b, 0, b.length);
 399:                   }
 400:                 break;
 401:               case XMLStreamConstants.CDATA:
 402:                 if (lexicalHandler != null)
 403:                   lexicalHandler.startCDATA();
 404:                 if (contentHandler != null)
 405:                   {
 406:                     char[] b = reader.getTextCharacters();
 407:                     if (isIgnorableWhitespace(parser, b, true))
 408:                       contentHandler.ignorableWhitespace(b, 0, b.length);
 409:                     else
 410:                       contentHandler.characters(b, 0, b.length);
 411:                   }
 412:                 if (lexicalHandler != null)
 413:                   lexicalHandler.endCDATA();
 414:                 break;
 415:               case XMLStreamConstants.START_ELEMENT:
 416:                 if (contentHandler != null)
 417:                   {
 418:                     QName name = reader.getName();
 419:                     String uri = name.getNamespaceURI();
 420:                     String localName = name.getLocalPart();
 421:                     String prefix = name.getPrefix();
 422:                     String qName = localName;
 423:                     if (!"".equals(prefix))
 424:                       qName = prefix + ":" + localName;
 425:                     if (!namespaceAware)
 426:                       {
 427:                         uri = "";
 428:                         localName = "";
 429:                       }
 430:                     else
 431:                       {
 432:                         int nc = reader.getNamespaceCount();
 433:                         for (int i = 0; i < nc; i++)
 434:                           {
 435:                             String nsuri = reader.getNamespaceURI(i);
 436:                             String nsprefix = reader.getNamespacePrefix(i);
 437:                             if ("xml".equals(nsprefix))
 438:                               continue;
 439:                             contentHandler.startPrefixMapping(nsprefix, nsuri);
 440:                           }
 441:                       }
 442:                     contentHandler.startElement(uri, localName, qName, this);
 443:                   }
 444:                 break;
 445:               case XMLStreamConstants.END_ELEMENT:
 446:                 if (contentHandler != null)
 447:                   {
 448:                     QName name = reader.getName();
 449:                     String uri = name.getNamespaceURI();
 450:                     String localName = name.getLocalPart();
 451:                     String prefix = name.getPrefix();
 452:                     String qName = localName;
 453:                     if (!"".equals(prefix))
 454:                       qName = prefix + ":" + localName;
 455:                     if (!namespaceAware)
 456:                       {
 457:                         uri = "";
 458:                         localName = "";
 459:                       }
 460:                     contentHandler.endElement(uri, localName, qName);
 461:                     if (namespaceAware)
 462:                       {
 463:                         int nc = reader.getNamespaceCount();
 464:                         for (int i = 0; i < nc; i++)
 465:                           {
 466:                             String nsprefix = reader.getNamespacePrefix(i);
 467:                             if ("xml".equals(nsprefix))
 468:                               continue;
 469:                             contentHandler.endPrefixMapping(nsprefix);
 470:                           }
 471:                       }
 472:                   }
 473:                 break;
 474:               case XMLStreamConstants.COMMENT:
 475:                 if (lexicalHandler != null)
 476:                   {
 477:                     char[] b = reader.getTextCharacters();
 478:                     lexicalHandler.comment(b, 0, b.length);
 479:                   }
 480:                 break;
 481:               case XMLStreamConstants.PROCESSING_INSTRUCTION:
 482:                 if (contentHandler != null)
 483:                   {
 484:                     String target = reader.getPITarget();
 485:                     String data = reader.getPIData();
 486:                     if (data == null)
 487:                       data = "";
 488:                     contentHandler.processingInstruction(target, data);
 489:                   }
 490:                 break;
 491:               case XMLParser.START_ENTITY:
 492:                 if (lexicalHandler != null)
 493:                   {
 494:                     String name = reader.getText();
 495:                     lexicalHandler.startEntity(name);
 496:                   }
 497:                 break;
 498:               case XMLParser.END_ENTITY:
 499:                 if (lexicalHandler != null)
 500:                   {
 501:                     String name = reader.getText();
 502:                     lexicalHandler.endEntity(name);
 503:                   }
 504:                 break;
 505:               case XMLStreamConstants.START_DOCUMENT:
 506:                 encoding = reader.getEncoding();
 507:                 xmlVersion = reader.getVersion();
 508:                 xmlStandalone = reader.isStandalone();
 509:                 xmlEncoding = reader.getCharacterEncodingScheme();
 510:                 if (contentHandler != null)
 511:                   contentHandler.startDocument();
 512:                 startDocumentDone = true;
 513:                 break;
 514:               case XMLStreamConstants.END_DOCUMENT:
 515:                 if (contentHandler != null)
 516:                   contentHandler.endDocument();
 517:                 break;
 518:               case XMLStreamConstants.DTD:
 519:                 XMLParser.Doctype doctype = parser.doctype;
 520:                 if (lexicalHandler != null)
 521:                   {
 522:                     String rootName = doctype.rootName;
 523:                     String publicId = doctype.publicId;
 524:                     String systemId2 = doctype.systemId;
 525:                     lexicalHandler.startDTD(rootName, publicId, systemId2);
 526:                   }
 527:                 for (Iterator i = doctype.entryIterator(); i.hasNext(); )
 528:                   {
 529:                     String entry = (String) i.next();
 530:                     char c = entry.charAt(0);
 531:                     String name = entry.substring(1);
 532:                     if ('E' == c)
 533:                       {
 534:                         // Element decl
 535:                         if (declHandler != null)
 536:                           {
 537:                             XMLParser.ContentModel model =
 538:                               doctype.getElementModel(name);
 539:                             declHandler.elementDecl(name, model.text);
 540:                           }
 541:                       }
 542:                     else if ('A' == c)
 543:                       {
 544:                         // Attlist decl
 545:                         if (declHandler != null)
 546:                           {
 547:                             for (Iterator j = doctype.attlistIterator(name);
 548:                                  j.hasNext(); )
 549:                               {
 550:                                 Map.Entry att = (Map.Entry) j.next();
 551:                                 String aname = (String) att.getKey();
 552:                                 XMLParser.AttributeDecl decl =
 553:                                   (XMLParser.AttributeDecl) att.getValue();
 554:                                 String type = decl.type;
 555:                                 String value = decl.value;
 556:                                 String mode = null;
 557:                                 switch (decl.valueType)
 558:                                   {
 559:                                   case XMLParser.ATTRIBUTE_DEFAULT_FIXED:
 560:                                     mode = "#FIXED";
 561:                                     break;
 562:                                   case XMLParser.ATTRIBUTE_DEFAULT_REQUIRED:
 563:                                     mode = "#REQUIRED";
 564:                                     break;
 565:                                   case XMLParser.ATTRIBUTE_DEFAULT_IMPLIED:
 566:                                     mode = "#IMPLIED";
 567:                                     break;
 568:                                   }
 569:                                 declHandler.attributeDecl(name, aname,
 570:                                                           type, mode, value);
 571:                               }
 572:                           }
 573:                       }
 574:                     else if ('e' == c)
 575:                       {
 576:                         // Entity decl
 577:                         Object entity = doctype.getEntity(name);
 578:                         if (entity instanceof String)
 579:                           {
 580:                             if (declHandler != null)
 581:                               declHandler.internalEntityDecl(name,
 582:                                                              (String) entity);
 583:                           }
 584:                         else
 585:                           {
 586:                             XMLParser.ExternalIds ids =
 587:                               (XMLParser.ExternalIds) entity;
 588:                             if (ids.notationName != null)
 589:                               {
 590:                                 if (dtdHandler != null)
 591:                                   {
 592:                                     String pub = ids.publicId;
 593:                                     String url = ids.systemId;
 594:                                     String not = ids.notationName;
 595:                                     dtdHandler.unparsedEntityDecl(name,
 596:                                                                   pub,
 597:                                                                   url,
 598:                                                                   not);
 599:                                   }
 600:                               }
 601:                             else
 602:                               {
 603:                                 if (declHandler != null)
 604:                                   {
 605:                                     String pub = ids.publicId;
 606:                                     String url = ids.systemId;
 607:                                     declHandler.externalEntityDecl(name,
 608:                                                                    pub,
 609:                                                                    url);
 610:                                   }
 611:                               }
 612:                           }
 613:                       }
 614:                     else if ('n' == c)
 615:                       {
 616:                         // Notation decl
 617:                         if (dtdHandler != null)
 618:                           {
 619:                             XMLParser.ExternalIds ids =
 620:                               doctype.getNotation(name);
 621:                             String pub = ids.publicId;
 622:                             String url = ids.systemId;
 623:                             dtdHandler.notationDecl(name, pub, url);
 624:                           }
 625:                       }
 626:                     else if ('c' == c)
 627:                       {
 628:                         // Comment
 629:                         if (lexicalHandler != null)
 630:                           {
 631:                             String comment = doctype.getComment(name);
 632:                             char[] b = comment.toCharArray();
 633:                             lexicalHandler.comment(b, 0, b.length);
 634:                           }
 635:                       }
 636:                     else if ('p' == c)
 637:                       {
 638:                         // Processing instruction
 639:                         if (contentHandler != null)
 640:                           {
 641:                             String[] pi = doctype.getPI(name);
 642:                             String target = pi[0];
 643:                             String data = pi[1];
 644:                             if (data == null)
 645:                               data = "";
 646:                             contentHandler.processingInstruction(target, data);
 647:                           }
 648:                       }
 649:                   }
 650:                 if (lexicalHandler != null)
 651:                   lexicalHandler.endDTD();
 652:               }
 653:           }
 654:         reset();
 655:         if (opened)
 656:           in.close();
 657:       }
 658:     catch (Exception e)
 659:       {
 660:         SAXParseException e2 = new SAXParseException(e.getMessage(), this);
 661:         e2.initCause(e);
 662:         try
 663:           {
 664:             if (!startDocumentDone && contentHandler != null)
 665:               contentHandler.startDocument();
 666:             if (errorHandler != null)
 667:               errorHandler.fatalError(e2);
 668:             if (contentHandler != null)
 669:               contentHandler.endDocument();
 670:           }
 671:         catch (SAXException sex)
 672:           {
 673:             // Ignored, we will rethrow the original exception.
 674:           }
 675:         reset();
 676:         if (opened)
 677:           in.close();
 678:         if (e instanceof SAXException)
 679:           throw (SAXException) e;
 680:         if (e instanceof IOException)
 681:           throw (IOException) e;
 682:         else
 683:           throw e2;
 684:       }
 685:   }
 686: 
 687:   /**
 688:    * Indicates whether the specified characters are ignorable whitespace.
 689:    */
 690:   private boolean isIgnorableWhitespace(XMLParser reader, char[] b,
 691:                                         boolean testCharacters)
 692:     throws XMLStreamException
 693:   {
 694:     XMLParser.Doctype doctype = reader.doctype;
 695:     if (doctype == null)
 696:       return false;
 697:     String currentElement = reader.getCurrentElement();
 698:     // check for xml:space
 699:     int ac = reader.getAttributeCount();
 700:     for (int i = 0; i < ac; i++)
 701:       {
 702:         QName aname = reader.getAttributeName(i);
 703:         if ("space".equals(aname.getLocalPart()) &&
 704:             XMLConstants.XML_NS_URI.equals(aname.getNamespaceURI()))
 705:           {
 706:             if ("preserve".equals(reader.getAttributeValue(i)))
 707:               return false;
 708:           }
 709:       }
 710:     XMLParser.ContentModel model = doctype.getElementModel(currentElement);
 711:     if (model == null || model.type != XMLParser.ContentModel.ELEMENT)
 712:       return false;
 713:     if (model.external && xmlStandalone)
 714:       return false;
 715:     boolean white = true;
 716:     if (testCharacters)
 717:       {
 718:         for (int i = 0; i < b.length; i++)
 719:           {
 720:             if (b[i] != ' ' && b[i] != '\t' && b[i] != '\n' && b[i] != '\r')
 721:               {
 722:                 white = false;
 723:                 break;
 724:               }
 725:           }
 726:       }
 727:     return white;
 728:   }
 729: 
 730:   public void parse(String systemId)
 731:     throws IOException, SAXException
 732:   {
 733:     parse(new InputSource(systemId));
 734:   }
 735: 
 736:   // -- Attributes2 --
 737: 
 738:   public int getIndex(String qName)
 739:   {
 740:     int len = reader.getAttributeCount();
 741:     for (int i = 0; i < len; i++)
 742:       {
 743:         QName q = reader.getAttributeName(i);
 744:         String localName = q.getLocalPart();
 745:         String prefix = q.getPrefix();
 746:         String qn = ("".equals(prefix)) ? localName : prefix + ":" + localName;
 747:         if (qName.equals(qn))
 748:           return i;
 749:       }
 750:     return -1;
 751:   }
 752: 
 753:   public int getIndex(String uri, String localName)
 754:   {
 755:     int len = reader.getAttributeCount();
 756:     for (int i = 0; i < len; i++)
 757:       {
 758:         QName q = reader.getAttributeName(i);
 759:         String ln = q.getLocalPart();
 760:         String u = q.getNamespaceURI();
 761:         if (u == null && uri != null)
 762:           continue;
 763:         if (u != null && !u.equals(uri))
 764:           continue;
 765:         if (ln.equals(localName))
 766:           return i;
 767:       }
 768:     return -1;
 769:   }
 770: 
 771:   public int getLength()
 772:   {
 773:     return reader.getAttributeCount();
 774:   }
 775: 
 776:   public String getLocalName(int index)
 777:   {
 778:     return reader.getAttributeLocalName(index);
 779:   }
 780: 
 781:   public String getQName(int index)
 782:   {
 783:     QName q = reader.getAttributeName(index);
 784:     String localName = q.getLocalPart();
 785:     String prefix = q.getPrefix();
 786:     return ("".equals(prefix)) ? localName : prefix + ":" + localName;
 787:   }
 788: 
 789:   public String getType(int index)
 790:   {
 791:     String ret = reader.getAttributeType(index);
 792:     // SAX doesn't permit ENUMERATION?
 793:     return ("ENUMERATION".equals(ret)) ? "NMTOKEN" : ret;
 794:   }
 795: 
 796:   public String getType(String qName)
 797:   {
 798:     int index = getIndex(qName);
 799:     return (index == -1) ? null : getType(index);
 800:   }
 801: 
 802:   public String getType(String uri, String localName)
 803:   {
 804:     int index = getIndex(uri, localName);
 805:     return (index == -1) ? null : getType(index);
 806:   }
 807: 
 808:   public String getURI(int index)
 809:   {
 810:     String ret = reader.getAttributeNamespace(index);
 811:     return (ret == null) ? "" : ret;
 812:   }
 813: 
 814:   public String getValue(int index)
 815:   {
 816:     return reader.getAttributeValue(index);
 817:   }
 818: 
 819:   public String getValue(String qName)
 820:   {
 821:     int index = getIndex(qName);
 822:     return (index == -1) ? null : getValue(index);
 823:   }
 824: 
 825:   public String getValue(String uri, String localName)
 826:   {
 827:     int index = getIndex(uri, localName);
 828:     return (index == -1) ? null : getValue(index);
 829:   }
 830: 
 831:   public boolean isDeclared(int index)
 832:   {
 833:     return parser.isAttributeDeclared(index);
 834:   }
 835: 
 836:   public boolean isDeclared(String qName)
 837:   {
 838:     int index = getIndex(qName);
 839:     return (index == -1) ? false : isDeclared(index);
 840:   }
 841: 
 842:   public boolean isDeclared(String uri, String localName)
 843:   {
 844:     int index = getIndex(uri, localName);
 845:     return (index == -1) ? false : isDeclared(index);
 846:   }
 847: 
 848:   public boolean isSpecified(int index)
 849:   {
 850:     return reader.isAttributeSpecified(index);
 851:   }
 852: 
 853:   public boolean isSpecified(String qName)
 854:   {
 855:     int index = getIndex(qName);
 856:     return (index == -1) ? false : isSpecified(index);
 857:   }
 858: 
 859:   public boolean isSpecified(String uri, String localName)
 860:   {
 861:     int index = getIndex(uri, localName);
 862:     return (index == -1) ? false : isSpecified(index);
 863:   }
 864: 
 865:   // -- Locator2 --
 866:   
 867:   public int getColumnNumber()
 868:   {
 869:     Location l = reader.getLocation();
 870:     return l.getColumnNumber();
 871:   }
 872: 
 873:   public int getLineNumber()
 874:   {
 875:     Location l = reader.getLocation();
 876:     return l.getLineNumber();
 877:   }
 878: 
 879:   public String getPublicId()
 880:   {
 881:     Location l = reader.getLocation();
 882:     return l.getPublicId();
 883:   }
 884: 
 885:   public String getSystemId()
 886:   {
 887:     Location l = reader.getLocation();
 888:     return l.getSystemId();
 889:   }
 890:   
 891:   public String getEncoding()
 892:   {
 893:     return encoding;
 894:   }
 895: 
 896:   public String getXMLVersion()
 897:   {
 898:     return xmlVersion;
 899:   }
 900: 
 901:   // -- XMLResolver --
 902:   
 903:   public Object resolveEntity(String publicId, String systemId,
 904:                               String baseURI, String namespace)
 905:     throws XMLStreamException
 906:   {
 907:     if (entityResolver != null)
 908:       {
 909:         try
 910:           {
 911:             InputSource input =
 912:               entityResolver.resolveEntity(publicId, systemId);
 913:             if (input != null)
 914:               {
 915:                 InputStream in = input.getByteStream();
 916:                 if (in == null)
 917:                   {
 918:                     String newSystemId = input.getSystemId();
 919:                     if (newSystemId != null && !newSystemId.equals(systemId))
 920:                       in = XMLParser.resolve(newSystemId);
 921:                   }
 922:                 return in;
 923:               }
 924:           }
 925:         catch (SAXException e)
 926:           {
 927:             XMLStreamException e2 = new XMLStreamException(e.getMessage());
 928:             e2.initCause(e);
 929:             throw e2;
 930:           }
 931:         catch (IOException e)
 932:           {
 933:             XMLStreamException e2 = new XMLStreamException(e.getMessage());
 934:             e2.initCause(e);
 935:             throw e2;
 936:           }
 937:       }
 938:     return null;
 939:   }
 940: 
 941:   public XMLEventReader resolveAsXMLEventReader(String uri)
 942:     throws XMLStreamException
 943:   {
 944:     // unused
 945:     return null;
 946:   }
 947: 
 948:   public XMLStreamReader resolveAsXMLStreamReader(String uri)
 949:     throws XMLStreamException
 950:   {
 951:     // unused
 952:     return null;
 953:   }
 954: 
 955:   // -- XMLReporter --
 956: 
 957:   public void report(String message, String errorType,
 958:                      Object relatedInformation, Location location)
 959:     throws XMLStreamException
 960:   {
 961:     if (errorHandler != null)
 962:       {
 963:         try
 964:           {
 965:             errorHandler.warning(new SAXParseException(message, this));
 966:           }
 967:         catch (SAXException e)
 968:           {
 969:             XMLStreamException e2 = new XMLStreamException(e.getMessage());
 970:             e2.initCause(e);
 971:             throw e2;
 972:           }
 973:       }
 974:   }
 975: 
 976:   public static void main(String[] args)
 977:     throws Exception
 978:   {
 979:     boolean validating = false;
 980:     boolean namespaceAware = false;
 981:     boolean xIncludeAware = false;
 982:     boolean expectCallbackClass = false;
 983:     String callbackClass = null;
 984:     int pos = 0;
 985:     while (pos < args.length && (args[pos].startsWith("-") || expectCallbackClass))
 986:       {
 987:         if ("-x".equals(args[pos]))
 988:           xIncludeAware = true;
 989:         else if ("-v".equals(args[pos]))
 990:           validating = true;
 991:         else if ("-n".equals(args[pos]))
 992:           namespaceAware = true;
 993:         else if ("-c".equals(args[pos]))
 994:           expectCallbackClass = true;
 995:         else if (expectCallbackClass)
 996:           {
 997:             callbackClass = args[pos];
 998:             expectCallbackClass = false;
 999:           }
1000:         pos++;
1001:       }
1002:     if (pos >= args.length || expectCallbackClass)
1003:       {
1004:         System.out.println("Syntax: SAXParser [-n] [-v] [-x] [-c <class>] <file> [<file2> [...]]");
1005:         System.out.println("\t-n: use namespace aware mode");
1006:         System.out.println("\t-v: use validating parser");
1007:         System.out.println("\t-x: use XInclude aware mode");
1008:         System.out.println("\t-c <class>: use specified class as callback handler (must have a no-arg public constructor)");
1009:         System.exit(2);
1010:       }
1011:     while (pos < args.length)
1012:       {
1013:         ContentHandler handler = null;
1014:         if (callbackClass != null)
1015:           {
1016:             Class t = Class.forName(callbackClass);
1017:             handler = (ContentHandler) t.newInstance();
1018:           }
1019:         else
1020:           handler = new org.xml.sax.helpers.DefaultHandler();
1021:         SAXParser parser = new SAXParser(validating, namespaceAware,
1022:                                          xIncludeAware);
1023:         InputSource input = new InputSource(args[pos]);
1024:         XMLReader reader = parser.getXMLReader();
1025:         reader.setContentHandler(handler);
1026:         reader.parse(input);
1027:         pos++;
1028:       }
1029:   }
1030:   
1031: }