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

📄 xmlpullparser.java

📁 j2me is based on j2mepolish, client & server for mobile application. menu sample
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                            + (char) delimiter);

                delimiter = ' ';
            }

            int i = (this.attributeCount++) << 1;

            this.attributes = ensureCapacity(this.attributes, i + 4);

            this.attributes[i++] = attrName;

            int p = this.txtPos;
            pushText(delimiter);

            this.attributes[i] = pop(p);

            if (delimiter != ' ')
                read(); // skip endquote
        }
    }

    /** result: isWhitespace; if the setName parameter is set,
    the name of the entity is stored in "name" */

    public final boolean pushEntity() throws IOException {

        read(); // &

        int pos = this.txtPos;

        while (!this.eof && this.peek0 != ';')
            push(read());

        String code = pop(pos);

        read();

        if (code.length() > 0 && code.charAt(0) == '#') {
            int c =
                (code.charAt(1) == 'x'
                    ? Integer.parseInt(code.substring(2), 16)
                    : Integer.parseInt(code.substring(1)));
            push(c);
            return c <= ' ';
        }

        String result = (String) this.entityMap.get(code);
        boolean whitespace = true;

        if (result == null)
            result = "&" + code + ";";

        for (int i = 0; i < result.length(); i++) {
            char c = result.charAt(i);
            if (c > ' ')
                whitespace = false;
            push(c);
        }

        return whitespace;
    }

    /** types:
    '<': parse to any token (for nextToken ())
    '"': parse to quote
    ' ': parse to whitespace or '>'
    */

    private final boolean pushText(int delimiter)
        throws IOException {

        boolean whitespace = true;
        int next = this.peek0;

        while (!this.eof
            && next != delimiter) { // covers eof, '<', '"'

            if (delimiter == ' ')
                if (next <= ' ' || next == '>')
                    break;

            if (next == '&') {
                if (!pushEntity())
                    whitespace = false;

            }
            else {
                if (next > ' ')
                    whitespace = false;

                push(read());
            }

            next = this.peek0;
        }

        return whitespace;
    }

    //--------------- public part starts here... ---------------


    public XmlPullParser(Reader reader) throws IOException {
        this.reader = reader;

        this.peek0 = reader.read();
        this.peek1 = reader.read();

        this.eof = this.peek0 == -1;

        this.entityMap = new Hashtable();
        this.entityMap.put("amp", "&");
        this.entityMap.put("apos", "'");
        this.entityMap.put("gt", ">");
        this.entityMap.put("lt", "<");
        this.entityMap.put("quot", "\"");

        this.line = 1;
        this.column = 1;
    }

    public void defineCharacterEntity(
        String entity,
        String value) {
        this.entityMap.put(entity, value);
    }

    public int getDepth() {
        return this.depth;
    }

    public String getPositionDescription() {

        StringBuffer buf =
            new StringBuffer(
                this.type < this.TYPES.length ? this.TYPES[this.type] : "Other");

        buf.append(" @" + this.line + ":" + this.column + ": ");

        if (this.type == START_TAG || this.type == END_TAG) {
            buf.append('<');
            if (this.type == END_TAG)
                buf.append('/');

            buf.append(this.name);
            buf.append('>');
        }
        else if (this.isWhitespace)
            buf.append("[whitespace]");
        else
            buf.append(getText());

        return buf.toString();
    }

    public int getLineNumber() {
        return this.line;
    }

    public int getColumnNumber() {
        return this.column;
    }

    public boolean isWhitespace() {
        return this.isWhitespace;
    }

    /* (non-Javadoc)
     * @see de.enough.polish.xml.SimplePullParser#getText()
     */
    public String getText() {

        if (this.text == null)
            this.text = pop(0);

        return this.text;
    }

    /* (non-Javadoc)
     * @see de.enough.polish.xml.SimplePullParser#getName()
     */
    public String getName() {
        return this.name;
    }

    public boolean isEmptyElementTag() {
        return this.degenerated;
    }

    /* (non-Javadoc)
     * @see de.enough.polish.xml.SimplePullParser#getAttributeCount()
     */
    public int getAttributeCount() {
        return this.attributeCount;
    }

    /* (non-Javadoc)
     * @see de.enough.polish.xml.SimplePullParser#getAttributeName(int)
     */
    public String getAttributeName(int index) {
        if (index >= this.attributeCount)
            throw new IndexOutOfBoundsException();
        return this.attributes[index << 1];
    }

    /* (non-Javadoc)
     * @see de.enough.polish.xml.SimplePullParser#getAttributeValue(int)
     */
    public String getAttributeValue(int index) {
        if (index >= this.attributeCount)
            throw new IndexOutOfBoundsException();
        return this.attributes[(index << 1) + 1];
    }

    /* (non-Javadoc)
     * @see de.enough.polish.xml.SimplePullParser#getAttributeValue(java.lang.String)
     */
    public String getAttributeValue(String name) {

        for (int i = (this.attributeCount << 1) - 2;
            i >= 0;
            i -= 2) {
            if (this.attributes[i].equals(name))
                return this.attributes[i + 1];
        }

        return null;
    }

    public int getType() {
        return this.type;
    }

    /* (non-Javadoc)
     * @see de.enough.polish.xml.SimplePullParser#next()
     */
    public int next() {

        try
        {
      
        if (this.degenerated) {
            this.type = END_TAG;
            this.degenerated = false;
            this.depth--;
            return this.type;
        }

        this.txtPos = 0;
        this.isWhitespace = true;

        do {
            this.attributeCount = 0;

            this.name = null;
            this.text = null;
            this.type = peekType();

            switch (this.type) {

                case ENTITY_REF :
                    this.isWhitespace &= pushEntity();
                    this.type = TEXT;
                    break;

                case START_TAG :
                    parseStartTag();
                    break;

                case END_TAG :
                    parseEndTag();
                    break;

                case END_DOCUMENT :
                    break;

                case TEXT :
                    this.isWhitespace &= pushText('<');
                    break;

                case CDSECT :
                    parseLegacy(true);
                    this.isWhitespace = false;
                    this.type = TEXT;
                    break;

                default :
                    parseLegacy(false);
            }
        }
        while (this.type > TEXT
            || this.type == TEXT
            && peekType() >= TEXT);

        this.isWhitespace &= this.type == TEXT;
        
        }
        catch (IOException e)
        {
          this.type = END_DOCUMENT;
        }
        
        return this.type;
    }

    //-----------------------------------------------------------------------------
    // utility methods to mak XML parsing easier ...

    /**
     * test if the current event is of the given type and if the
     * name do match. null will match any namespace
     * and any name. If the current event is TEXT with isWhitespace()=
     * true, and the required type is not TEXT, next () is called prior
     * to the test. If the test is not passed, an exception is
     * thrown. The exception text indicates the parser position,
     * the expected event and the current event (not meeting the
     * requirement.
     *
     * <p>essentially it does this
     * <pre>
     *  if (getType() == TEXT && type != TEXT && isWhitespace ())
     *    next ();
     *
     *  if (type != getType
     *  || (name != null && !name.equals (getName ())
     *     throw new XmlPullParserException ( "....");
     * </pre>
     */
    public void require(int type, String name)
        throws IOException {

        if (this.type == TEXT && type != TEXT && isWhitespace())
            next();

        if (type != this.type
            || (name != null && !name.equals(getName())))
            exception("expected: " + this.TYPES[type] + "/" + name);
    }

    /**
     * If the current event is text, the value of getText is
     * returned and next() is called. Otherwise, an empty
     * String ("") is returned. Useful for reading element
     * content without needing to performing an additional
     * check if the element is empty.
     *
     * <p>essentially it does this
     * <pre>
     *   if (getType != TEXT) return ""
     *    String result = getText ();
     *    next ();
     *    return result;
     *  </pre>
     */

    public String readText() throws IOException {

        if (this.type != TEXT)
            return "";

        String result = getText();
        next();
        return result;
    }
}

⌨️ 快捷键说明

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