⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmlparser.java

📁 pastry的java实现的2.0b版
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
   * @see #END_DOCUMENT   */  public int next() throws XmlPullParserException, IOException {    char next = 0;    try {      next = current();    } catch (EOFException e) {      return END_DOCUMENT;    }    switch (next) {      case '<':        int result = parseTag();        if (result == START_DOCUMENT) {          return next();        } else {          return result;        }      case '/':        if (this.inTag) {          return parseEndTag((String) tags.pop());        } else {          return parseText();        }      default:        return parseText();    }  }  /**   * ----- INTERNAL PARSING METHODS -----   *   * @exception IOException DESCRIBE THE EXCEPTION   */  /**   * Internal method which actually fills the buffer   *   * @exception IOException DESCRIBE THE EXCEPTION   */  protected void fillBuffer() throws IOException {    // process the mark    if (marked != null) {      this.marked.append(buffer, 0, bufferPosition);    } else if (mark != -1) {      this.marked = new CharArrayBuffer(buffer, mark, bufferPosition - mark);    }    int read = reader.read(buffer);    if (read > 0) {      bufferLimit = read;      bufferPosition = 0;    }  }  /**   * Method which returns the current char in the buffer   *   * @return The current char   * @exception IOException DESCRIBE THE EXCEPTION   */  protected char current() throws IOException {    if (bufferPosition == bufferLimit) {      fillBuffer();    }    if (bufferPosition == bufferLimit) {      throw new EOFException();    }    return buffer[bufferPosition];  }  /**   * Method which steps forward in the buffer   */  protected void step() {    bufferPosition++;  }  /**   * Sets the mark   */  protected void mark() {    this.mark = bufferPosition;  }  /**   * Unsets the mark   *   * @return DESCRIBE THE RETURN VALUE   */  protected String unmark() {    try {      if (this.marked != null) {        this.marked.append(buffer, 0, bufferPosition);        return cache.get(marked.getBuffer(), 0, marked.getLength());      } else {        return cache.get(buffer, mark, bufferPosition - mark);      }    } finally {      this.mark = -1;      this.marked = null;    }  }  /**   * Internal method which clears the list of attributes   */  protected void clearAttributes() {    this.numAttributes = 0;  }  /**   * Internal method which adds an attributes   *   * @param key The feature to be added to the Attribute attribute   * @param value The feature to be added to the Attribute attribute   */  protected void addAttribute(String key, String value) {    if (numAttributes == attributeKeys.length) {      throw new RuntimeException("More than " + attributeKeys.length + " attributes encountered - unsupported!");    }    attributeKeys[numAttributes] = key;    attributeValues[numAttributes] = value;    numAttributes++;  }  /**   * An assertion method   *   * @param c DESCRIBE THE PARAMETER   * @exception XmlPullParserException DESCRIBE THE EXCEPTION   * @exception IOException DESCRIBE THE EXCEPTION   */  protected void expect(char c) throws XmlPullParserException, IOException {    if (current() != c) {      throw new XmlPullParserException("Expected character '" + c + "' got '" + current() + "'");    } else {      step();    }  }  /**   * Internal method which checks for existence   *   * @param chars The chars to check for   * @param c DESCRIBE THE PARAMETER   * @return DESCRIBE THE RETURN VALUE   */  protected boolean contains(char[] chars, char c) {    for (int i = 0; i < chars.length; i++) {      if (chars[i] == c) {        return true;      }    }    return false;  }  /**   * Method which parses and returns up to the next token   *   * @param chars DESCRIBE THE PARAMETER   * @return The token   * @exception IOException DESCRIBE THE EXCEPTION   */  protected String parseUntil(char[] chars) throws IOException {    mark();    while (true) {      if (contains(chars, current())) {        break;      } else {        step();      }    }    return unmark();  }  /**   * Method which parses and returns up to the next token   *   * @param c DESCRIBE THE PARAMETER   * @return The token   * @exception IOException DESCRIBE THE EXCEPTION   */  protected String parseUntil(char c) throws IOException {    mark();    while (true) {      if (current() == c) {        break;      } else {        step();      }    }    return unmark();  }  /**   * Method which parses up to the next token   *   * @param chars DESCRIBE THE PARAMETER   * @exception IOException DESCRIBE THE EXCEPTION   */  protected void parseUntilNot(char[] chars) throws IOException {    while (true) {      if (!contains(chars, current())) {        break;      } else {        step();      }    }  }  /**   * Method which parses an end tag of the form <tag />   *   * @param tag DESCRIBE THE PARAMETER   * @return DESCRIBE THE RETURN VALUE   * @exception XmlPullParserException DESCRIBE THE EXCEPTION   * @exception IOException DESCRIBE THE EXCEPTION   */  protected int parseEndTag(String tag) throws XmlPullParserException, IOException {    expect('/');    expect('>');    this.name = tag;    this.inTag = false;//    System.out.println("Parsed default END tag '" + this.name + "'");    return END_TAG;  }  /**   * Internal method which parses a tag   *   * @return DESCRIBE THE RETURN VALUE   * @exception XmlPullParserException DESCRIBE THE EXCEPTION   * @exception IOException DESCRIBE THE EXCEPTION   */  protected int parseTag() throws XmlPullParserException, IOException {    expect('<');    switch (current()) {      case '/':        return parseEndTag();      case '?':        return parseDocumentTag();      default:        return parseStartTag();    }  }  /**   * Method which parses an end tag of the form <tag />   *   * @return DESCRIBE THE RETURN VALUE   * @exception XmlPullParserException DESCRIBE THE EXCEPTION   * @exception IOException DESCRIBE THE EXCEPTION   */  protected int parseEndTag() throws XmlPullParserException, IOException {    expect('/');    parseUntilNot(WHITESPACE);    clearAttributes();    this.name = parseUntil(WHITESPACE_OR_TAG_END);    tags.pop();    this.inTag = false;    parseUntilNot(WHITESPACE);    expect('>');//    System.out.println("Parsed END tag '" + this.name + "'");    return END_TAG;  }  /**   * Method which parses a start tag   *   * @return DESCRIBE THE RETURN VALUE   * @exception XmlPullParserException DESCRIBE THE EXCEPTION   * @exception IOException DESCRIBE THE EXCEPTION   */  protected int parseStartTag() throws XmlPullParserException, IOException {    parseUntilNot(WHITESPACE);    this.name = parseUntil(WHITESPACE_OR_TAG_END);    tags.push(this.name);    parseUntilNot(WHITESPACE);//    System.out.println("Parsed START tag '" + this.name + "'");    parseAttributes();    parseUntilNot(WHITESPACE);    if (current() != '/') {      expect('>');      this.inTag = false;    } else {      this.inTag = true;    }    return START_TAG;  }  /**   * Method which parses a document tag   *   * @return DESCRIBE THE RETURN VALUE   * @exception XmlPullParserException DESCRIBE THE EXCEPTION   * @exception IOException DESCRIBE THE EXCEPTION   */  protected int parseDocumentTag() throws XmlPullParserException, IOException {    expect('?');    parseUntilNot(WHITESPACE);    String type = parseUntil(WHITESPACE_OR_TAG_END);    if (!(type.toLowerCase().equals("xml"))) {      throw new XmlPullParserException("This does not appear to be an XML document - found '" + type + "'!");    }    parseUntilNot(WHITESPACE);    parseAttributes();    clearAttributes();    parseUntilNot(WHITESPACE);    this.inTag = false;    expect('?');    expect('>');//    System.out.println("Parsed START DOCUMENT tag '" + type + "'");    return START_DOCUMENT;  }  /**   * Method which parses all of the attributes of a start tag   *   * @exception XmlPullParserException DESCRIBE THE EXCEPTION   * @exception IOException DESCRIBE THE EXCEPTION   */  protected void parseAttributes() throws XmlPullParserException, IOException {    clearAttributes();    while (true) {      parseUntilNot(WHITESPACE);      if (contains(TAG_END, current())) {        return;      } else {        String key = parseUntil(WHITESPACE_OR_EQUALS);        parseUntilNot(WHITESPACE);        expect('=');        parseUntilNot(WHITESPACE);        String value = null;        char quote = current();        if (contains(QUOTE, quote)) {          expect(quote);          value = convert(parseUntil(quote));          expect(quote);        } else {          value = convert(parseUntil(WHITESPACE_OR_TAG_END));        }//        System.out.println("Parsed ATTRIBUTE '" + key + "' -> '"  + value + "'");        addAttribute(key, value);      }    }  }  /**   * Method which parses an end tag of the form <tag />   *   * @return DESCRIBE THE RETURN VALUE   * @exception XmlPullParserException DESCRIBE THE EXCEPTION   * @exception IOException DESCRIBE THE EXCEPTION   */  protected int parseText() throws XmlPullParserException, IOException {    clearAttributes();    this.text = convert(parseUntil('<'));    this.inTag = false;    return TEXT;  }  /**   * Internal method which deconverts all of the HTML/XML entities like &amp,   * &gt, &lt, etc...   *   * @param string The string to convert   * @return The result   */  protected String convert(String string) {    if (string.indexOf('&') < 0) {      return string;    }    for (int i = 0; i < ENTITIES.length; i++) {      string = string.replaceAll(ENTITIES[i][0], ENTITIES[i][1]);    }    return string;  }  /**   * DESCRIBE THE METHOD   *   * @param entityName DESCRIBE THE PARAMETER   * @param replacementText DESCRIBE THE PARAMETER   * @exception XmlPullParserException DESCRIBE THE EXCEPTION   */  public void defineEntityReplacementText(String entityName, String replacementText) throws XmlPullParserException {    throw new UnsupportedOperationException();  }  /**   * DESCRIBE THE METHOD   *   * @return DESCRIBE THE RETURN VALUE   * @exception XmlPullParserException DESCRIBE THE EXCEPTION   * @exception IOException DESCRIBE THE EXCEPTION   */  public int nextToken() throws XmlPullParserException, IOException {    throw new UnsupportedOperationException();  }  /**   * DESCRIBE THE METHOD   *   * @param type DESCRIBE THE PARAMETER   * @param namespace DESCRIBE THE PARAMETER   * @param name DESCRIBE THE PARAMETER   * @exception XmlPullParserException DESCRIBE THE EXCEPTION   * @exception IOException DESCRIBE THE EXCEPTION   */  public void require(int type, String namespace, String name) throws XmlPullParserException, IOException {    throw new UnsupportedOperationException();  }  /**   * DESCRIBE THE METHOD   *   * @return DESCRIBE THE RETURN VALUE   * @exception XmlPullParserException DESCRIBE THE EXCEPTION   * @exception IOException DESCRIBE THE EXCEPTION   */  public String nextText() throws XmlPullParserException, IOException {    throw new UnsupportedOperationException();  }  /**   * DESCRIBE THE METHOD   *   * @return DESCRIBE THE RETURN VALUE   * @exception XmlPullParserException DESCRIBE THE EXCEPTION   * @exception IOException DESCRIBE THE EXCEPTION   */  public int nextTag() throws XmlPullParserException, IOException {    throw new UnsupportedOperationException();  }  /**   * ----- INTERNAL CLASS -----   *   * @version $Id: pretty.settings 2305 2005-03-11 20:22:33Z jeffh $   * @author jeffh   */  /**   * This class implements a char array buffer   *   * @version $Id: pretty.settings 2305 2005-03-11 20:22:33Z jeffh $   * @author jeffh   */  public class CharArrayBuffer {    /**     * The internal buffer     */    protected char[] buffer;    /**     * The markers     */    protected int length;    /**     * The default initial capacity     */    public final static int DEFAULT_CAPACITY = 32;    /**     * Constructor     *     * @param chars DESCRIBE THE PARAMETER     * @param length DESCRIBE THE PARAMETER     * @param off DESCRIBE THE PARAMETER     */    public CharArrayBuffer(char[] chars, int length, int off) {      this.buffer = new char[DEFAULT_CAPACITY];      this.length = 0;      append(chars, length, off);    }    /**     * Returns the internal array     *     * @return The array     */    public char[] getBuffer() {      return buffer;    }    /**     * Returns the length     *     * @return The length     */    public int getLength() {      return length;    }    /**     * Appends some more chars!     *     * @param chars DESCRIBE THE PARAMETER     * @param off DESCRIBE THE PARAMETER     * @param len DESCRIBE THE PARAMETER     */    public void append(char[] chars, int off, int len) {      while (length + len > buffer.length) {        expandBuffer();      }      System.arraycopy(chars, off, buffer, length, len);      length += len;    }    /**     * Expands the buffer     */    protected void expandBuffer() {      char[] newbuffer = new char[buffer.length * 2];      System.arraycopy(buffer, 0, newbuffer, 0, length);      this.buffer = newbuffer;    }  }}

⌨️ 快捷键说明

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