finally { reset(); if (opened) in.close(); }
  public synchronized void parse(InputSource input)    throws IOException, SAXException  {    reset();    String systemId = input.getSystemId();    InputStream in = input.getByteStream();    boolean opened = false;    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();        opened = true;        parser = new XMLParser(in, systemId,                               validating,                               namespaceAware,                               coalescing,                               replaceERefs,                               externalEntities,                               supportDTD,                               baseAware,                               stringInterning,                               this,                               this);      }    reader = parser;    baseURI = systemId;        if (xIncludeAware)      reader = new XIncludeFilter(parser, systemId, namespaceAware,                                  validating, true);        if (contentHandler != null)      contentHandler.setDocumentLocator(this);    boolean startDocumentDone = false;    try      {        while (parser.hasNext())          {            int event = parser.next();            if (baseAware)              baseURI = parser.getXMLBase();            switch (event)              {              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();                    if (isIgnorableWhitespace(parser, b, false))                      contentHandler.ignorableWhitespace(b, 0, b.length);                    else                    contentHandler.characters(b, 0, b.length);                  }                break;              case XMLStreamConstants.CDATA:                if (lexicalHandler != null)                  lexicalHandler.startCDATA();                if (contentHandler != null)                  {                    char[] b = reader.getTextCharacters();                    if (isIgnorableWhitespace(parser, b, true))                      contentHandler.ignorableWhitespace(b, 0, b.length);                    else                    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 = "";                      }                    else                      {                        int nc = reader.getNamespaceCount();                        for (int i = 0; i < nc; i++)                          {                            String nsuri = reader.getNamespaceURI(i);                            String nsprefix = reader.getNamespacePrefix(i);                            if ("xml".equals(nsprefix))                              continue;                            contentHandler.startPrefixMapping(nsprefix, nsuri);                          }                      }                    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);                    if (namespaceAware)                      {                        int nc = reader.getNamespaceCount();                        for (int i = 0; i < nc; i++)                          {                            String nsprefix = reader.getNamespacePrefix(i);                            if ("xml".equals(nsprefix))                              continue;                            contentHandler.endPrefixMapping(nsprefix);                          }                      }                  }                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();                    if (data == null)                      data = "";                    contentHandler.processingInstruction(target, data);                  }                break;              case XMLStreamConstants.START_ENTITY:                if (lexicalHandler != null)                  {                    String name = reader.getText();                    lexicalHandler.startEntity(name);                  }                break;              case XMLStreamConstants.END_ENTITY:                if (lexicalHandler != null)                  {                    String name = reader.getText();                    lexicalHandler.endEntity(name);                  }                break;              case XMLStreamConstants.START_DOCUMENT:                encoding = reader.getEncoding();                xmlVersion = reader.getVersion();                xmlStandalone = reader.isStandalone();                xmlEncoding = reader.getCharacterEncodingScheme();                if (contentHandler != null)                  contentHandler.startDocument();                startDocumentDone = true;                break;              case XMLStreamConstants.END_DOCUMENT:                if (contentHandler != null)                  contentHandler.endDocument();                break;              case XMLStreamConstants.DTD:                XMLParser.Doctype doctype = parser.doctype;                if (lexicalHandler != null)                  {                    String rootName = doctype.rootName;                    String publicId = doctype.publicId;                    String systemId2 = doctype.systemId;                    lexicalHandler.startDTD(rootName, publicId, systemId2);                  }                for (Iterator i = doctype.entryIterator(); i.hasNext(); )                  {                    String entry = (String) i.next();                    char c = entry.charAt(0);                    String name = entry.substring(1);                    if ('E' == c)                  {                        // Element decl                        if (declHandler != null)                      {                            XMLParser.ContentModel model =                              doctype.getElementModel(name);                            declHandler.elementDecl(name, model.text);                      }                      }                    else if ('A' == c)                      {                        // Attlist decl                        if (declHandler != null)                      {                            for (Iterator j = doctype.attlistIterator(name);                             j.hasNext(); )                          {                            Map.Entry att = (Map.Entry) j.next();                                String aname = (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(name, aname,                                                      type, mode, value);                          }                      }                      }                    else if ('e' == c)                      {                        // Entity decl                        Object entity = doctype.getEntity(name);                        if (entity instanceof String)                          {                            if (declHandler != null)                              declHandler.internalEntityDecl(name,                                                             (String) entity);                          }                        else                          {                            XMLParser.ExternalIds ids =                              (XMLParser.ExternalIds) entity;                            if (ids.notationName != null)                              {                                if (dtdHandler != null)                                  {                                    String pub = ids.publicId;                                    String url = ids.systemId;                                    String not = ids.notationName;                                    dtdHandler.unparsedEntityDecl(name,                                                                  pub,                                                                  url,                                                                  not);                                  }                              }                            else                              {                                if (declHandler != null)                                  {                                    String pub = ids.publicId;                                    String url = ids.systemId;                                    declHandler.externalEntityDecl(name,                                                                   pub,                                                                   url);                                  }                              }                          }                      }                    else if ('n' == c)                      {                        // Notation decl                        if (dtdHandler != null)                          {                            XMLParser.ExternalIds ids =                              doctype.getNotation(name);                            String pub = ids.publicId;                            String url = ids.systemId;                            dtdHandler.notationDecl(name, pub, url);                          }                      }                    else if ('c' == c)                      {                        // Comment                        if (lexicalHandler != null)                          {                            String comment = doctype.getComment(name);                            char[] b = comment.toCharArray();                            lexicalHandler.comment(b, 0, b.length);                          }                      }                    else if ('p' == c)                      {                        // Processing instruction                        if (contentHandler != null)                          {                            String[] pi = doctype.getPI(name);                            String target = pi[0];                            String data = pi[1];                            if (data == null)                              data = "";                            contentHandler.processingInstruction(target, data);                          }                      }                  }                if (lexicalHandler != null)                  lexicalHandler.endDTD();              }          }      }    catch (Exception e)      {        if (!startDocumentDone && contentHandler != null)          contentHandler.startDocument();        SAXParseException e2 = new SAXParseException(e.getMessage(), this);        e2.initCause(e);        if (errorHandler != null)          errorHandler.fatalError(e2);        if (contentHandler != null)          contentHandler.endDocument();        if (e instanceof IOException)          throw (IOException) e;        else        throw e2;      }    finally      {        reset();        if (opened)          in.close();      }  }