abstractparser.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 929 行 · 第 1/2 页

JAVA
929
字号
  {    init();        if (_searchPath == null) {      if (source.getSystemId() != null)        _searchPath = Vfs.lookup(source.getSystemId()).getParent();    }    _systemId = source.getSystemId();    _publicId = source.getPublicId();    ReadStream stream;    String encoding = null;    if (source.getByteStream() != null) {      stream = Vfs.openRead(source.getByteStream());      encoding = source.getEncoding();    }    else if (source.getCharacterStream() != null) {      encoding = "UTF-8";      _isStaticEncoding = true;      stream = Vfs.openRead(source.getCharacterStream());    }    else if (source.getSystemId() != null) {      InputStream is = openStream(source.getSystemId(),				  source.getPublicId(),				  null,				  true);      stream = Vfs.openRead(is);      encoding = source.getEncoding();    }    else      throw new FileNotFoundException(L.l("invalid InputSource"));    if (encoding != null)      stream.setEncoding(encoding);    try {      parseInt(stream);    } finally {      stream.close();    }  }    /**   * SAX parsing from an InputStream   *   * @param is stream containing the XML   */  public void parse(InputStream is)    throws IOException, SAXException  {    init();    _systemId = "stream";        if (is instanceof ReadStream) {      Path path = ((ReadStream) is).getPath();      _systemId = path.getURL();      _filename = path.getUserPath();            if (_searchPath != null) {      }      else if (path != null)        _searchPath = path.getParent();      parseInt((ReadStream) is);    }    else {      ReadStream rs = VfsStream.openRead(is);      try {        parseInt(rs);      } finally {        if (rs != is)          rs.close();      }    }  }    /**   * SAX parsing from an InputStream   *   * @param is stream containing the XML   */  public void parse(InputStream is, String systemId)    throws IOException, SAXException  {    init();        parseImpl(is, systemId);  }    /**   * SAX parsing from an InputStream   *   * @param is stream containing the XML   */  public void parseImpl(InputStream is, String systemId)    throws IOException, SAXException  {    if (is instanceof ReadStream) {      Path path = ((ReadStream) is).getPath();            if (_searchPath != null) {      }      else if (path != null) {        _searchPath = path.getParent();        if (systemId != null)          _searchPath = _searchPath.lookup(systemId).getParent();      }      else if (systemId != null)        _searchPath = Vfs.lookup(systemId).getParent();      if (systemId == null) {        systemId = path.getURL();        _filename = ((ReadStream) is).getUserPath();      }      else        _filename = systemId;      _systemId = systemId;            parseInt((ReadStream) is);    }    else {      if (systemId == null) {        _systemId = "anonymous.xml";      }      else {        _searchPath = Vfs.lookup(systemId).getParent();        _systemId = systemId;      }      ReadStream rs = VfsStream.openRead(is);      try {        parseInt(rs);      } finally {        if (rs != is)          rs.close();      }    }  }  /**   * SAX parsing from a file path   *   * @param systemId path to the file containing the XML   */  public void parse(String systemId)    throws IOException, SAXException  {    InputStream is = openTopStream(systemId, null);    try {      parse(is);    } finally {      is.close();    }  }    /**   * SAX parsing from a VFS path   */  public void parse(Path path)    throws IOException, SAXException  {    init();        if (_searchPath == null)      _searchPath = path.getParent();        ReadStream is = path.openRead();    try {      parseInt(is);    } finally {      is.close();    }  }  /**   * SAX parsing from a string.   *   * @param string string containing the XML   */  public void parseString(String string)    throws IOException, SAXException  {    init();        ReadStream is = Vfs.openString(string);    try {      parseInt(is);    } finally {      is.close();    }  }    /**   * Parses a document from a SAX InputSource   *   * @param source SAX InputSource containing the XML data.   */  public Document parseDocument(InputSource source)    throws IOException, SAXException  {    init();        QDocument doc = new QDocument();    if (_builder == null)      _builder = new DOMBuilder();    _builder.init(doc);    setOwner(doc);        doc.setSystemId(source.getSystemId());    _builder.setSystemId(source.getSystemId());    _builder.setStrictXML(_strictXml);    _builder.setCoalescing(_isCoalescing);    _builder.setSkipWhitespace(_skipWhitespace);    _contentHandler = _builder;    parse(source);    return doc;  }  /**   * Parses a document from system path.   *   * @param systemId path to the XML data.   */  public Document parseDocument(String systemId)    throws IOException, SAXException  {    InputStream is = openTopStream(systemId, null);    try {      return parseDocument(is);    } finally {      is.close();    }  }  /**   * Parses a document from a VFS path   *   * @param path the VFS path containing the XML document.   */  public Document parseDocument(Path path)    throws IOException, SAXException  {    if (_searchPath == null)      _searchPath = path.getParent();        ReadStream is = path.openRead();    try {      return parseDocument(is);    } finally {      is.close();    }  }  /**   * Parses an input stream into a DOM document   *   * @param is the input stream containing the XML   *   * @return the parsed document.   */  public Document parseDocument(InputStream is)    throws IOException, SAXException  {    return parseDocument(is, null);  }  /**   * Parses an input stream into a DOM document   *   * @param is the input stream containing the XML   * @param systemId the URL of the stream.   *   * @return the parsed document.   */  public Document parseDocument(InputStream is, String systemId)    throws IOException, SAXException  {    init();    QDocument doc = new QDocument();    parseDocument(doc, is, systemId);    return doc;  }  public void parseDocument(QDocument doc, InputStream is, String systemId)    throws IOException, SAXException  {    _owner = doc;    if (_builder == null)      _builder = new DOMBuilder();    _builder.init(_owner);    _builder.setSystemId(systemId);    _builder.setCoalescing(_isCoalescing);    _builder.setSkipWhitespace(_skipWhitespace);    _contentHandler = _builder;    parseImpl(is, systemId);  }  /**   * Parses a string into a DOM document   *   * @param string the string containing the XML   */  public Document parseDocumentString(String string)    throws IOException, SAXException  {    ReadStream is = Vfs.openString(string);    try {      _isStaticEncoding = true;      return parseDocument(is);    } finally {      is.close();    }  }  /**   * Looks up an input stream from the system id.   */  public InputStream openStream(String systemId, String publicId)    throws IOException, SAXException  {    return openStream(systemId, publicId, _entityResolver, false);  }  /**   * Looks up an input stream from the system id.   */  public InputStream openTopStream(String systemId, String publicId)    throws IOException, SAXException  {    return openStream(systemId, publicId, _entityResolver, true);  }  /**   * Looks up an input stream from the system id.   */  public InputStream openStream(String systemId, String publicId,				EntityResolver entityResolver)    throws IOException, SAXException  {    return openStream(systemId, publicId, entityResolver, false);  }  /**   * Looks up an input stream from the system id.   */  protected InputStream openStream(String systemId, String publicId,				   EntityResolver entityResolver,				   boolean isTop)    throws IOException, SAXException  {    int colon = systemId.indexOf(':');    int slash = systemId.indexOf('/');        boolean isAbsolute = colon > 0 && (colon < slash || slash < 0);        if (slash == 0 || ! isAbsolute) {      Path pwd;      if (_searchPath != null)        pwd = _searchPath;      else        pwd = Vfs.lookup(systemId).getParent();            String newId = pwd.lookup(systemId).getURL();      if (! newId.startsWith("error:"))        systemId = newId;      else {        int tail = _systemId.lastIndexOf('/');        if (tail >= 0)          systemId = _systemId.substring(0, tail + 1) + systemId;      }    }    // xml/03c5 -- must be after the normalization    if (entityResolver != null) {      InputSource source = entityResolver.resolveEntity(publicId, systemId);      if (source != null) {        _filename = systemId;        _systemId = systemId;        return openSource(source);      }    }    int ch;    if (CauchoSystem.isWindows() && systemId.startsWith("file:") &&        systemId.length() > 7 && systemId.charAt(6) == ':' &&        (((ch = systemId.charAt(5)) >= 'a' && ch <= 'z') ||         ch >= 'A' && ch <= 'Z')) {      colon = 1;      isAbsolute = false;      systemId = "/" + systemId.substring(5);    }    if (! isTop &&	isAbsolute && ! systemId.startsWith("file:") &&        ! systemId.startsWith("jar:") &&        ! (colon == 1 && CauchoSystem.isWindows())) {      throw new RemoteURLException(L.l("URL `{0}' was not opened because it is a remote URL.  Any URL scheme other than file: must be handled by a custom entity resolver.",                                       systemId));    }    else if (_searchPath != null) {      return _searchPath.lookup(systemId).openRead();    }    else      return Vfs.lookup(systemId).openRead();  }  /**   * Opens the source   */  protected InputStream openSource(InputSource source)    throws IOException, SAXException  {    if (source.getByteStream() != null) {      return source.getByteStream();    }    else if (source.getCharacterStream() != null) {      return Vfs.openRead(source.getCharacterStream());    }    else if (source.getSystemId() != null) {      return Vfs.openRead(source.getSystemId());    }    else      throw new FileNotFoundException(L.l("invalid InputSource {0}", source));  }  /**   * Parse the document from a read stream.   *   * @param is read stream to parse from.   *   * @return The parsed document.   */  abstract Document parseInt(ReadStream is)    throws IOException, SAXException;    static {    _attrTypes.put("CDATA", "CDATA");    _attrTypes.put("ID", "ID");    _attrTypes.put("IDREF", "IDREF");    _attrTypes.put("IDREFS", "IDREFS");    _attrTypes.put("ENTITY", "ENTITY");    _attrTypes.put("ENTITIES", "ENTITIES");    _attrTypes.put("NMTOKEN", "NMTOKEN");    _attrTypes.put("NMTOKENS", "NMTOKENS");  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?