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

📄 kxmlparser.java

📁 KXML一个基于j2me的xml解析器
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        while (true) {            int i = read();            switch (i) {                case -1 :                    error(UNEXPECTED_EOF);                    return;                case '\'' :                    quoted = !quoted;                    break;                case '<' :                    if (!quoted)                        nesting++;                    break;                case '>' :                    if (!quoted) {                        if ((--nesting) == 0)                            return;                    }                    break;            }            if (push)                push(i);        }    }    /* precondition: &lt;/ consumed */    private final void parseEndTag()        throws IOException, XmlPullParserException {        read(); // '<'        read(); // '/'        name = readName();        skip();        read('>');        int sp = (depth - 1) << 2;        if (depth == 0) {            error("element stack empty");            type = COMMENT;            return;        }        if (!name.equals(elementStack[sp + 3])) {            error("expected: /" + elementStack[sp + 3] + " read: " + name);			// become case insensitive in relaxed mode            int probe = sp;            while (probe >= 0 && !name.toLowerCase().equals(elementStack[probe + 3].toLowerCase())) {                stackMismatch++;                probe -= 4;            }            if (probe < 0) {                stackMismatch = 0;                //			text = "unexpected end tag ignored";                type = COMMENT;                return;            }        }        namespace = elementStack[sp];        prefix = elementStack[sp + 1];        name = elementStack[sp + 2];    }    private final int peekType() throws IOException {        switch (peek(0)) {            case -1 :                return END_DOCUMENT;            case '&' :                return ENTITY_REF;            case '<' :                switch (peek(1)) {                    case '/' :                        return END_TAG;                    case '?' :                    case '!' :                        return LEGACY;                    default :                        return START_TAG;                }            default :                return TEXT;        }    }    private final String get(int pos) {        return new String(txtBuf, pos, txtPos - pos);    }    /*    private final String pop (int pos) {    String result = new String (txtBuf, pos, txtPos - pos);    txtPos = pos;    return result;    }    */    private final void push(int c) {        isWhitespace &= c <= ' ';        if (txtPos == txtBuf.length) {            char[] bigger = new char[txtPos * 4 / 3 + 4];            System.arraycopy(txtBuf, 0, bigger, 0, txtPos);            txtBuf = bigger;        }        txtBuf[txtPos++] = (char) c;    }    /** Sets name and attributes */    private final void parseStartTag(boolean xmldecl)        throws IOException, XmlPullParserException {        if (!xmldecl)            read();        name = readName();        attributeCount = 0;        while (true) {            skip();            int c = peek(0);            if (xmldecl) {                if (c == '?') {                    read();                    read('>');                    return;                }            }            else {                if (c == '/') {                    degenerated = true;                    read();                    skip();                    read('>');                    break;                }                if (c == '>' && !xmldecl) {                    read();                    break;                }            }            if (c == -1) {                error(UNEXPECTED_EOF);                //type = COMMENT;                return;            }            String attrName = readName();            if (attrName.length() == 0) {                error("attr name expected");               //type = COMMENT;                break;            }            int i = (attributeCount++) << 2;            attributes = ensureCapacity(attributes, i + 4);            attributes[i++] = "";            attributes[i++] = null;            attributes[i++] = attrName;            skip();            if (peek(0) != '=') {				error("Attr.value missing f. "+attrName);                attributes[i] = "1";            }            else {                read('=');                skip();                int delimiter = peek(0);                if (delimiter != '\'' && delimiter != '"') {                    error("attr value delimiter missing!");                    delimiter = ' ';                }				else 					read();				                int p = txtPos;                pushText(delimiter, true);                attributes[i] = get(p);                txtPos = p;                if (delimiter != ' ')                    read(); // skip endquote            }        }        int sp = depth++ << 2;        elementStack = ensureCapacity(elementStack, sp + 4);        elementStack[sp + 3] = name;        if (depth >= nspCounts.length) {            int[] bigger = new int[depth + 4];            System.arraycopy(nspCounts, 0, bigger, 0, nspCounts.length);            nspCounts = bigger;        }        nspCounts[depth] = nspCounts[depth - 1];        /*        		if(!relaxed){                for (int i = attributeCount - 1; i > 0; i--) {                    for (int j = 0; j < i; j++) {                        if (getAttributeName(i).equals(getAttributeName(j)))                            exception("Duplicate Attribute: " + getAttributeName(i));                    }                }        		}        */        if (processNsp)            adjustNsp();        else            namespace = "";        elementStack[sp] = namespace;        elementStack[sp + 1] = prefix;        elementStack[sp + 2] = name;    }    /**      * result: isWhitespace; if the setName parameter is set,     * the name of the entity is stored in "name" */    private final void pushEntity()        throws IOException, XmlPullParserException {        push(read()); // &                        int pos = txtPos;        while (true) {            int c = read();            if (c == ';')                break;            if (c < 128                && (c < '0' || c > '9')                && (c < 'a' || c > 'z')                && (c < 'A' || c > 'Z')                && c != '_'                && c != '-'                && c != '#') {            	if(!relaxed){            		error("unterminated entity ref");            	}                //; ends with:"+(char)c);                           if (c != -1)                    push(c);                return;            }            push(c);        }        String code = get(pos);        txtPos = pos - 1;        if (token && type == ENTITY_REF){            name = code;        }        if (code.charAt(0) == '#') {            int c =                (code.charAt(1) == 'x'                    ? Integer.parseInt(code.substring(2), 16)                    : Integer.parseInt(code.substring(1)));            push(c);            return;        }        String result = (String) entityMap.get(code);        unresolved = result == null;        if (unresolved) {            if (!token)                error("unresolved: &" + code + ";");        }        else {            for (int i = 0; i < result.length(); i++)                push(result.charAt(i));        }    }    /** types:    '<': parse to any token (for nextToken ())    '"': parse to quote    ' ': parse to whitespace or '>'    */    private final void pushText(int delimiter, boolean resolveEntities)        throws IOException, XmlPullParserException {        int next = peek(0);        int cbrCount = 0;        while (next != -1 && next != delimiter) { // covers eof, '<', '"'            if (delimiter == ' ')                if (next <= ' ' || next == '>')                    break;            if (next == '&') {                if (!resolveEntities)                    break;                pushEntity();            }            else if (next == '\n' && type == START_TAG) {                read();                push(' ');            }            else                push(read());            if (next == '>' && cbrCount >= 2 && delimiter != ']')                error("Illegal: ]]>");            if (next == ']')                cbrCount++;            else                cbrCount = 0;            next = peek(0);        }    }    private final void read(char c)        throws IOException, XmlPullParserException {        int a = read();        if (a != c)            error("expected: '" + c + "' actual: '" + ((char) a) + "'");    }    private final int read() throws IOException {        int result;        if (peekCount == 0)            result = peek(0);        else {            result = peek[0];            peek[0] = peek[1];        }        //		else {        //			result = peek[0];         //			System.arraycopy (peek, 1, peek, 0, peekCount-1);        //		}        peekCount--;        column++;        if (result == '\n') {            line++;            column = 1;        }        return result;    }    /** Does never read more than needed */    private final int peek(int pos) throws IOException {        while (pos >= peekCount) {            int nw;            if (srcBuf.length <= 1)                nw = reader.read();            else if (srcPos < srcCount)                nw = srcBuf[srcPos++];            else {                srcCount = reader.read(srcBuf, 0, srcBuf.length);                if (srcCount <= 0)                    nw = -1;                else                    nw = srcBuf[0];                srcPos = 1;            }            if (nw == '\r') {                wasCR = true;                peek[peekCount++] = '\n';            }            else {                if (nw == '\n') {                    if (!wasCR)                        peek[peekCount++] = '\n';                }                else                    peek[peekCount++] = nw;                wasCR = false;            }        }        return peek[pos];    }    private final String readName()        throws IOException, XmlPullParserException {        int pos = txtPos;        int c = peek(0);        if ((c < 'a' || c > 'z')            && (c < 'A' || c > 'Z')            && c != '_'            && c != ':'            && c < 0x0c0            && !relaxed)            error("name expected");        do {            push(read());            c = peek(0);        }        while ((c >= 'a' && c <= 'z')            || (c >= 'A' && c <= 'Z')            || (c >= '0' && c <= '9')            || c == '_'            || c == '-'            || c == ':'            || c == '.'            || c >= 0x0b7);        String result = get(pos);        txtPos = pos;        return result;    }    private final void skip() throws IOException {        while (true) {            int c = peek(0);            if (c > ' ' || c == -1)                break;            read();        }    }    //  public part starts here...    public void setInput(Reader reader) throws XmlPullParserException {        this.reader = reader;        line = 1;        column = 0;        type = START_DOCUMENT;        name = null;        namespace = null;        degenerated = false;        attributeCount = -1;        encoding = null;        version = null;        standalone = null;        if (reader == null)            return;

⌨️ 快捷键说明

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