finally { if (opened) in.close(); reset(); }
  public synchronized void parse(InputSource input)    throws IOException, SAXException  {    reset();    String systemId = input.getSystemId();    InputStream in = input.getByteStream();    if (in != null)      parser = new XMLParser(in, systemId,                             validating,                             namespaceAware,                             coalescing,                             replaceERefs,                             externalEntities,                             supportDTD,                             baseAware,                             stringInterning,                             this,                             this);    else      {        Reader r = input.getCharacterStream();        if (r != null)          parser = new XMLParser(r, systemId,                                 validating,                                 namespaceAware,                                 coalescing,                                 replaceERefs,                                 externalEntities,                                 supportDTD,                                 baseAware,                                 stringInterning,                                 this,                                 this);      }    if (parser == null)      {        if (systemId == null)          throw new SAXException("No stream or system ID specified");        systemId = XMLParser.absolutize(null, systemId);        in = new URL(systemId).openStream();        parser = new XMLParser(in, systemId,                               validating,                               namespaceAware,                               coalescing,                               replaceERefs,                               externalEntities,                               supportDTD,                               baseAware,                               stringInterning,                               this,                               this);      }    reader = parser;        if (xIncludeAware)      reader = new XIncludeFilter(parser, systemId, namespaceAware,                                  validating, true);        if (contentHandler != null)      contentHandler.setDocumentLocator(this);    try      {        while (parser.hasNext())          {            switch (parser.next())              {              case XMLStreamConstants.CHARACTERS:                if (contentHandler != null)                  {                    char[] b = reader.getTextCharacters();                    contentHandler.characters(b, 0, b.length);                  }                break;              case XMLStreamConstants.SPACE:                if (contentHandler != null)                  {                    char[] b = reader.getTextCharacters();                    // TODO determine whether whitespace is ignorable                    contentHandler.characters(b, 0, b.length);                  }                break;              case XMLStreamConstants.CDATA:                if (lexicalHandler != null)                  lexicalHandler.startCDATA();                if (contentHandler != null)                  {                    char[] b = reader.getTextCharacters();                    // TODO determine whether whitespace and ignorable                    contentHandler.characters(b, 0, b.length);                  }                if (lexicalHandler != null)                  lexicalHandler.endCDATA();                break;              case XMLStreamConstants.START_ELEMENT:                if (contentHandler != null)                  {                    QName name = reader.getName();                    String uri = name.getNamespaceURI();                    String localName = name.getLocalPart();                    String prefix = name.getPrefix();                    String qName = localName;                    if (!"".equals(prefix))                      qName = prefix + ":" + localName;                    if (!namespaceAware)                      {                        uri = "";                        localName = "";                      }                    contentHandler.startElement(uri, localName, qName, this);                  }                break;              case XMLStreamConstants.END_ELEMENT:                if (contentHandler != null)                  {                    QName name = reader.getName();                    String uri = name.getNamespaceURI();                    String localName = name.getLocalPart();                    String prefix = name.getPrefix();                    String qName = localName;                    if (!"".equals(prefix))                      qName = prefix + ":" + localName;                    if (!namespaceAware)                      {                        uri = "";                        localName = "";                      }                    contentHandler.endElement(uri, localName, qName);                  }                break;              case XMLStreamConstants.COMMENT:                if (lexicalHandler != null)                  {                    char[] b = reader.getTextCharacters();                    lexicalHandler.comment(b, 0, b.length);                  }                break;              case XMLStreamConstants.PROCESSING_INSTRUCTION:                if (contentHandler != null)                  {                    String target = reader.getPITarget();                    String data = reader.getPIData();                    contentHandler.processingInstruction(target, data);                  }                break;              case XMLStreamConstants.START_DOCUMENT:                encoding = reader.getEncoding();                xmlVersion = reader.getVersion();                xmlStandalone = reader.isStandalone();                if (contentHandler != null)                  contentHandler.startDocument();                break;              case XMLStreamConstants.END_DOCUMENT:                if (contentHandler != null)                  contentHandler.endDocument();                break;              case XMLStreamConstants.DTD:                if (lexicalHandler != null)                  {                    String rootName = parser.doctype.rootName;                    String publicId = parser.doctype.publicId;                    String systemId2 = parser.doctype.systemId;                    lexicalHandler.startDTD(rootName, publicId, systemId2);                  }                if (declHandler != null)                  {                    for (Iterator i = parser.doctype.elements.entrySet().iterator();                         i.hasNext(); )                      {                        Map.Entry entry = (Map.Entry) i.next();                        String name = (String) entry.getKey();                        String model = (String) entry.getValue();                        declHandler.elementDecl(name, model);                      }                    for (Iterator i = parser.doctype.attlists.entrySet().iterator();                         i.hasNext(); )                      {                        Map.Entry entry = (Map.Entry) i.next();                        String elementName = (String) entry.getKey();                        Map attlist = (Map) entry.getValue();                        for (Iterator j = attlist.entrySet().iterator();                             j.hasNext(); )                          {                            Map.Entry att = (Map.Entry) j.next();                            String name = (String) att.getKey();                            XMLParser.AttributeDecl decl =                              (XMLParser.AttributeDecl) att.getValue();                            String type = decl.type;                            String value = decl.value;                            String mode = null;                            switch (decl.valueType)                              {                              case XMLParser.ATTRIBUTE_DEFAULT_FIXED:                                mode = "#FIXED";                                break;                              case XMLParser.ATTRIBUTE_DEFAULT_REQUIRED:                                mode = "#REQUIRED";                                break;                              case XMLParser.ATTRIBUTE_DEFAULT_IMPLIED:                                mode = "#IMPLIED";                                break;                              }                            declHandler.attributeDecl(elementName, name,                                                      type, mode, value);                          }                      }                    // TODO entity declarations                  }                if (lexicalHandler != null)                  lexicalHandler.endDTD();              }          }        reset();      }    catch (XMLStreamException e)      {        SAXParseException e2 = new SAXParseException(e.getMessage(), this);        e2.initCause(e);        throw e2;      }  }