/**
   * parse
   *
   * @param inputSource
   * @exception org.xml.sax.SAXException
   * @exception java.io.IOException
   */
  public void parse(InputSource inputSource) throws SAXException, IOException {

    // parse document
    try {
      XMLInputSource xmlInputSource =
          new XMLInputSource(inputSource.getPublicId(), inputSource.getSystemId(), null);
      xmlInputSource.setByteStream(inputSource.getByteStream());
      xmlInputSource.setCharacterStream(inputSource.getCharacterStream());
      xmlInputSource.setEncoding(inputSource.getEncoding());
      parse(xmlInputSource);
    }

    // wrap XNI exceptions as SAX exceptions
    catch (XMLParseException e) {
      Exception ex = e.getException();
      if (ex == null) {
        // must be a parser exception; mine it for locator info and throw
        // a SAXParseException
        LocatorImpl locatorImpl = new LocatorImpl();
        locatorImpl.setPublicId(e.getPublicId());
        locatorImpl.setSystemId(e.getExpandedSystemId());
        locatorImpl.setLineNumber(e.getLineNumber());
        locatorImpl.setColumnNumber(e.getColumnNumber());
        throw new SAXParseException(e.getMessage(), locatorImpl);
      }
      if (ex instanceof SAXException) {
        // why did we create an XMLParseException?
        throw (SAXException) ex;
      }
      if (ex instanceof IOException) {
        throw (IOException) ex;
      }
      throw new SAXException(ex);
    } catch (XNIException e) {
      Exception ex = e.getException();
      if (ex == null) {
        throw new SAXException(e.getMessage());
      }
      if (ex instanceof SAXException) {
        throw (SAXException) ex;
      }
      if (ex instanceof IOException) {
        throw (IOException) ex;
      }
      throw new SAXException(ex);
    }
  } // parse(InputSource)
Пример #2
0
  /**
   * Resolves an external parsed entity. If the entity cannot be resolved, this method should return
   * null.
   *
   * @param resourceIdentifier description of the resource to be revsoved
   * @throws XNIException Thrown on general error.
   * @throws IOException Thrown if resolved entity stream cannot be opened or some other i/o error
   *     occurs.
   */
  public XMLInputSource resolveEntity(XMLResourceIdentifier resourceIdentifier)
      throws XNIException, IOException {
    // resolve entity using DOM entity resolver
    if (fEntityResolver != null) {
      // For entity resolution the type of the resource would be  XML TYPE
      // DOM L3 LS spec mention only the XML 1.0 recommendation right now
      LSInput inputSource =
          resourceIdentifier == null
              ? fEntityResolver.resolveResource(null, null, null, null, null)
              : fEntityResolver.resolveResource(
                  getType(resourceIdentifier),
                  resourceIdentifier.getNamespace(),
                  resourceIdentifier.getPublicId(),
                  resourceIdentifier.getLiteralSystemId(),
                  resourceIdentifier.getBaseSystemId());
      if (inputSource != null) {
        String publicId = inputSource.getPublicId();
        String systemId = inputSource.getSystemId();
        String baseSystemId = inputSource.getBaseURI();
        InputStream byteStream = inputSource.getByteStream();
        Reader charStream = inputSource.getCharacterStream();
        String encoding = inputSource.getEncoding();
        String data = inputSource.getStringData();

        /**
         * An LSParser looks at inputs specified in LSInput in the following order: characterStream,
         * byteStream, stringData, systemId, publicId.
         */
        XMLInputSource xmlInputSource = new XMLInputSource(publicId, systemId, baseSystemId);

        if (charStream != null) {
          xmlInputSource.setCharacterStream(charStream);
        } else if (byteStream != null) {
          xmlInputSource.setByteStream((InputStream) byteStream);
        } else if (data != null && data.length() != 0) {
          xmlInputSource.setCharacterStream(new StringReader(data));
        }
        xmlInputSource.setEncoding(encoding);
        return xmlInputSource;
      }
    }

    // unable to resolve entity
    return null;
  } // resolveEntity(String,String,String):XMLInputSource