`
fonter
  • 浏览: 869386 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

开源一个J2ME解析类

    博客分类:
  • J2ME
阅读更多

此类由KXML进化而来,用于手机解析HTML,XML,TXT,XHTML,WML等文档,支持CDATA,支持Text Extractor

 

如果在平时的开发用得上它,请保留作者和出处,谢谢!

 

package Core;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Hashtable;

/**
 * @author fonter
 * http://fonter.iteye.com
 * 此类用于解析HTML,XML,TXT,XHTML,WML等文档,支持CDATA,支持Text Extractor
 */
public class HtmlInputStreamReader {
    
    
    private Reader reader;
    private boolean unresolved;
    private boolean processNsp = true;
    private boolean token;
    private boolean wasCR;
    private String encoding;
    private char[] srcBuf;
    private Hashtable entityMap;
    private boolean relaxed = true;
    private boolean degenerated;
    private String[] attributes = new String[16];
    private int type;
    static final private String UNEXPECTED_EOF = "Unexpected EOF";
    //static final private String ILLEGAL_TYPE = "Wrong event type";
    public static final String NO_NAMESPACE = "";
    public static final int START_DOCUMENT = 0;
    public static final int END_DOCUMENT = 1;
    public static final int START_TAG = 2;
    public static final int END_TAG = 3;
    public static final int TEXT = 4;
    public static final int CDSECT = 5;
    public static final int ENTITY_REF = 6;
    public static final int IGNORABLE_WHITESPACE = 7;
    public static final int PROCESSING_INSTRUCTION = 8;
    public static final int COMMENT = 9;
    public static final int DOCDECL = 10;
    public static final int LEGACY = 999;
    public static final int XML_DECL = 998;
    private String[] nspStack = new String[8];
    private int[] nspCounts = new int[4];
    private String version;
    private Boolean standalone;
    private char[] txtBuf = new char[128];
    private int txtPos;
    private String error;
    private int srcLength;
    private int srcPos;
    private int srcCount;
    
    private int stackMismatch = 0;
    private String namespace;
    private String prefix;
    private String name;
    private String[] elementStack = new String[16];
    
    private int line;
    private int column;
    
    private int[] peek = new int[2];
    private int peekCount;
    private boolean isWhitespace;
    private int attributeCount;
    
    private int depth;
    
    public HtmlInputStreamReader() throws IOException{
        srcBuf = new char[Runtime.getRuntime().freeMemory() >= 1048576 ? 8192 : 128];
    }
    
    
    public void setInput(Reader reader) throws IOException {
        this.reader = reader;
        line = 1;
        column = 0;
        type = START_DOCUMENT;
        name = null;
        namespace = null;
        degenerated = false;
        attributeCount = -1;
        encoding = null;
        version = null;
        standalone = null;
        srcLength = 0;
        
        if (reader == null)
            return;
        
        srcPos = 0;
        srcCount = 0;
        peekCount = 0;
        depth = 0;
        
        entityMap = new Hashtable();
        entityMap.put("amp", "&");
        entityMap.put("apos", "'");
        entityMap.put("gt", ">");
        entityMap.put("lt", "<");
        entityMap.put("quot", "\"");
        entityMap.put("copy", "\251");
        entityMap.put("reg", "\256");
        entityMap.put("yen", "\245");
    }
    
    
    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 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 void error(String desc){
        exception(desc);
    }
    
    private final void exception(String desc){
        System.out.println(desc);
    }
    
    public final void nextImpl() throws IOException{
        
        if (reader == null)
            exception("No Input specified");
        
        if (type == END_TAG)
            depth--;
        
        while (true) {
            attributeCount = -1;
            
            // degenerated needs to be handled before error because of possible
            // processor expectations(!)
            
            if (degenerated) {
                degenerated = false;
                type = END_TAG;
                return;
            }
            
            
            if (error != null) {
                for (int i = 0; i < error.length(); i++)
                    push(error.charAt(i));
                //				text = error;
                error = null;
                type = COMMENT;
                return;
            }
            
            
            if (relaxed
                    && (stackMismatch > 0 || (peek(0) == -1 && depth > 0))) {
                int sp = (depth - 1) << 2;
                type = END_TAG;
                namespace = elementStack[sp];
                prefix = elementStack[sp + 1];
                name = elementStack[sp + 2];
                if (stackMismatch != 1)
                    error = "missing end tag /" + name + " inserted";
                if (stackMismatch > 0)
                    stackMismatch--;
                return;
            }
            
            prefix = null;
            name = null;
            namespace = null;
            //            text = null;
            
            type = peekType();
            //System.out.println("Markup:"+type);
            
            switch (type) {
                
                case ENTITY_REF :
                    pushEntity();
                    return;
                    
                case START_TAG :
                    parseStartTag(false);
                    return;
                    
                case END_TAG :
                    parseEndTag();
                    return;
                    
                case END_DOCUMENT :
                    return;
                    
                case TEXT :
                    pushText('<', !token);
                    if (depth == 0) {
                        if (isWhitespace)
                            type = IGNORABLE_WHITESPACE;
                        // make exception switchable for instances.chg... !!!!
                        //	else
                        //    exception ("text '"+getText ()+"' not allowed outside root element");
                    }
                    return;
                    
                default :
                    type = parseLegacy(token);
                    if (type != XML_DECL)
                        return;
            }
        }
    }
    
    // boolean isEND(){
    //	return isEOF;
    //}
    
    public String getInputEncoding() {
        return encoding;
    }
    
    public String getText() {
        return type < TEXT
                || (type == ENTITY_REF && unresolved) ? null : get(0);
    }
    //text Extractor
    public String getTextExtractor() {
        //String s = get(0).;
        StringBuffer sb = new StringBuffer();
        return type < TEXT
                || (type == ENTITY_REF && unresolved) ? null : appendCollapseWhiteSpace(sb,get(0)).toString();
    }
    
    private static final char[] WHITESPACE={' ','\n','\r','\t','\f','\u200B'};
    
    public static final boolean isWhiteSpace(final char ch) {
        for (int i=0; i<WHITESPACE.length; i++)
            if (ch==WHITESPACE[i]) return true;
        return false;
    }
    
    static final StringBuffer appendCollapseWhiteSpace(StringBuffer sb, String text) {
        final int textLength=text.length();
        int i=0;
        boolean firstWasWhiteSpace=false;
        while (true) {
            if (i>=textLength) return sb;
            if (!isWhiteSpace(text.charAt(i))) break;
            i++;
        }
        do {
            final char ch = text.charAt(i++);
            if (isWhiteSpace(ch)) {
                firstWasWhiteSpace =true;
            } else {
                if (firstWasWhiteSpace) {
                    sb.append(' ');
                    firstWasWhiteSpace =false;
                }
                sb.append(ch);
            }
        } while (i<textLength);
        return sb;
    }
    
    public int getEventType(){
        return type;
    }
    
    private final void parseEndTag()
    throws IOException{
        
        read(); // '<'
        read(); // '/'
        name = readName();
        //System.out.println("EndTag:"+name);
        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 parseLegacy(boolean push)
    throws IOException{
        
        String req = "";
        int term;
        int result;
        int prev = 0;
        
        read(); // <
        int c = read();
        if (c == '?') {
            if ((peek(0) == 'x' || peek(0) == 'X')
            && (peek(1) == 'm' || peek(1) == 'M')) {
                
                if (push) {
                    push(peek(0));
                    push(peek(1));
                }
                read();
                read();
                
                if ((peek(0) == 'l' || peek(0) == 'L') && peek(1) <= ' ') {
                    
                    if (line != 1 || column > 4)
                        error("PI must not start with xml");
                    
                    parseStartTag(true);
                    
                    if (attributeCount < 1 || !"version".equals(attributes[2]))
                        error("version expected");
                    
                    version = attributes[3];
                    
                    int pos = 1;
                    
                    if (pos < attributeCount
                            && "encoding".equals(attributes[2 + 4])) {
                        encoding = attributes[3 + 4];
                        pos++;
                    }
                    
                    if (pos < attributeCount
                            && "standalone".equals(attributes[4 * pos + 2])) {
                        String st = attributes[3 + 4 * pos];
                        if ("yes".equals(st))
                            standalone = new Boolean(true);
                        else if ("no".equals(st))
                            standalone = new Boolean(false);
                        else
                            error("illegal standalone value: " + st);
                        pos++;
                    }
                    
                    if (pos != attributeCount)
                        error("illegal xmldecl");
                    
                    isWhitespace = true;
                    txtPos = 0;
                    
                    return XML_DECL;
                }
            }
            
        /*            int c0 = read ();
                    int c1 = read ();
                    int */
            
            term = '?';
            result = PROCESSING_INSTRUCTION;
        } else if (c == '!') {
            if (peek(0) == '-') {
                result = COMMENT;
                req = "--";
                term = '-';
            } else if (peek(0) == '[') {
                result = CDSECT;
                req = "[CDATA[";
                term = ']';
                push = true;
            } else {
                result = DOCDECL;
                req = "DOCTYPE";
                term = -1;
            }
        } else {
            error("illegal: <" + c);
            return COMMENT;
        }
        for (int i = 0; i < req.length(); i++)
            read(req.charAt(i));
        
        if (result == DOCDECL)
            parseDoctype(push);
        else {
            while (true) {
                c = read();
                if (c == -1){
                    error(UNEXPECTED_EOF);
                    return COMMENT;
                }
                
                if (push)
                    push(c);
                
                if ((term == '?' || c == term)
                && peek(0) == term
                        && peek(1) == '>')
                    break;
                
                prev = c;
            }
            
            if (term == '-' && prev == '-')
                error("illegal comment delimiter: --->");
            
            read();
            read();
            
            if (push && term != '?')
                txtPos--;
            
        }
        return result;
    }
    private final String readName()
    throws IOException{
        
        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 String get(int pos) {
        return new String(txtBuf, pos, txtPos - pos);
    }
    
    private final void skip() throws IOException {
        
        while (true) {
            int c = peek(0);
            if (c > ' ' || c == -1)
                break;
            read();
        }
    }
    private final void parseDoctype(boolean push)
    throws IOException{
        int nesting = 1;
        boolean quoted = false;
        
// read();
        
        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);
        }
    }
    private final void pushText(int delimiter, boolean resolveEntities)
    throws IOException{
        
        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 pushEntity()
    throws IOException{
        
        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));
        }
    }
    
    
    private final void parseStartTag(boolean xmldecl)
    throws IOException{
        
        if (!xmldecl)
            read();
        name = readName();
        //System.out.println("StartTag:"+name);
        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");
                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);
                String skdkfk = get(p);
                attributes[i] = skdkfk;
                System.out.println("attributes:"+skdkfk);
                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;
    }
    
    
    
    private final boolean adjustNsp(){
        
        boolean any = false;
        
        for (int i = 0; i < attributeCount << 2; i += 4) {
            // * 4 - 4; i >= 0; i -= 4) {
            
            String attrName = attributes[i + 2];
            int cut = attrName.indexOf(':');
            String prefix;
            
            if (cut != -1) {
                prefix = attrName.substring(0, cut);
                attrName = attrName.substring(cut + 1);
            } else if (attrName.equals("xmlns")) {
                prefix = attrName;
                attrName = null;
            } else
                continue;
            
            if (!prefix.equals("xmlns")) {
                any = true;
            } else {
                int j = (nspCounts[depth]++) << 1;
                
                nspStack = ensureCapacity(nspStack, j + 2);
                nspStack[j] = attrName;
                nspStack[j + 1] = attributes[i + 3];
                
                if (attrName != null && attributes[i + 3].equals(""))
                    error("illegal empty namespace");
                
                //  prefixMap = new PrefixMap (prefixMap, attrName, attr.getValue ());
                
                //System.out.println (prefixMap);
                
                System.arraycopy(
                        attributes,
                        i + 4,
                        attributes,
                        i,
                        ((--attributeCount) << 2) - i);
                
                i -= 4;
            }
        }
        
        if (any) {
            for (int i = (attributeCount << 2) - 4; i >= 0; i -= 4) {
                
                String attrName = attributes[i + 2];
                int cut = attrName.indexOf(':');
                
                if (cut == 0 && !relaxed)
                    throw new RuntimeException(
                            "illegal attribute name: " + attrName + " at " + this);
                
                else if (cut != -1) {
                    String attrPrefix = attrName.substring(0, cut);
                    
                    attrName = attrName.substring(cut + 1);
                    
                    String attrNs = getNamespace(attrPrefix);
                    
                    if (attrNs == null && !relaxed)
                        throw new RuntimeException(
                                "Undefined Prefix: " + attrPrefix + " in " + this);
                    
                    attributes[i] = attrNs;
                    attributes[i + 1] = attrPrefix;
                    attributes[i + 2] = attrName;
                    
                    /*
                                        if (!relaxed) {
                                            for (int j = (attributeCount << 2) - 4; j > i; j -= 4)
                                                if (attrName.equals(attributes[j + 2])
                                                    && attrNs.equals(attributes[j]))
                                                    exception(
                                                        "Duplicate Attribute: {"
                                                            + attrNs
                                                            + "}"
                                                            + attrName);
                                        }
                     */
                }
            }
        }
        
        int cut = name.indexOf(':');
        
        if (cut == 0)
            error("illegal tag name: " + name);
        
        if (cut != -1) {
            prefix = name.substring(0, cut);
            name = name.substring(cut + 1);
        }
        
        this.namespace = getNamespace(prefix);
        
        if (this.namespace == null) {
            if (prefix != null)
                error("undefined prefix: " + prefix);
            this.namespace = NO_NAMESPACE;
        }
        
        return any;
    }
    //获取命名空间
    public String getNamespace(String prefix) {
        
        if ("xml".equals(prefix))
            return "http://www.w3.org/XML/1998/namespace";
        if ("xmlns".equals(prefix))
            return "http://www.w3.org/2000/xmlns/";
        
        for (int i = (getNamespaceCount(depth) << 1) - 2; i >= 0; i -= 2) {
            if (prefix == null) {
                if (nspStack[i] == null)
                    return nspStack[i + 1];
            } else if (prefix.equals(nspStack[i]))
                return nspStack[i + 1];
        }
        return null;
    }
    
    public int getNamespaceCount(int depth) {
        if (depth > this.depth)
            throw new IndexOutOfBoundsException();
        return nspCounts[depth];
    }
    
    
    
    
    
    private final void read(char c)throws IOException{
        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++;
        srcLength++;
        
        if (result == '\n') {
            
            line++;
            column = 1;
        }
        
        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;
    }
    
    private final String[] ensureCapacity(String[] arr, int required) {
        if (arr.length >= required)
            return arr;
        String[] bigger = new String[required + 16];
        System.arraycopy(arr, 0, bigger, 0, arr.length);
        return bigger;
    }
    //设置编码
    public void setInput(InputStream is, String _enc) throws IOException {
        
        srcPos = 0;
        srcCount = 0;
        String enc = _enc;
        
        if (is == null)
            throw new IllegalArgumentException();
        
        try {
            
            if (enc == null) {
                // read four bytes
                
                int chk = 0;
                
                while (srcCount < 4) {
                    int i = is.read();
                    srcLength++;
                    if (i == -1)
                        break;
                    chk = (chk << 8) | i;
                    srcBuf[srcCount++] = (char) i;
                }
                System.out.println(chk);
                if (srcCount == 4) {
                    switch (chk) {
                        case 0x00000FEFF :
                            enc = "UTF-32BE";
                            srcCount = 0;
                            break;
                            
                        case 0x0FFFE0000 :
                            enc = "UTF-32LE";
                            srcCount = 0;
                            break;
                            
                        case 0x03c :
                            enc = "UTF-32BE";
                            srcBuf[0] = '<';
                            srcCount = 1;
                            break;
                            
                        case 0x03c000000 :
                            enc = "UTF-32LE";
                            srcBuf[0] = '<';
                            srcCount = 1;
                            break;
                            
                        case 0x0003c003f :
                            enc = "UTF-16BE";
                            srcBuf[0] = '<';
                            srcBuf[1] = '?';
                            srcCount = 2;
                            break;
                            // 这是我加上去的----------------------------------
                        case 0x3c68746d:
                            //System.out.println("ssdesdfdf");
                            enc = "gb2312";
                            srcBuf[0] = '<';
                            //srcBuf[1] = '?';
                            srcCount = 1;
                            break;
                            
                        case 0xd0a3c3f:
                            enc = "UTF-8";
                            srcBuf[0] = '<';
                            srcBuf[1] = '?';
                            srcCount = 2;
                            break;
                            //-------------------------------------------
                            
                            
                        case 0x03c003f00 :
                            enc = "UTF-16LE";
                            srcBuf[0] = '<';
                            srcBuf[1] = '!';
                            srcCount = 2;
                            break;
                        case 0xa0a3c21:
                            enc = "UTF-8";
                            srcBuf[0] = '<';
                            srcBuf[1] = '!';
                            srcCount = 2;
                            break;
                            //case 0x03c21444f:
                            //enc = "gb2312";
                            //srcBuf[0] = '<';
                            //srcBuf[1] = '!';
                            //srcCount = 2;
                            //break;
                            
                        case 0x03c3f786d :
                            while (true) {
                                
                                int i = is.read();
                                srcLength++;
                                if (i == -1)
                                    break;
                                srcBuf[srcCount++] = (char) i;
                                if (i == '>') {
                                    String s = new String(srcBuf, 0, srcCount);
                                    int i0 = s.indexOf("encoding");
                                    if (i0 != -1) {
                                        while (s.charAt(i0) != '"'
                                                && s.charAt(i0) != '\'')
                                            i0++;
                                        char deli = s.charAt(i0++);
                                        int i1 = s.indexOf(deli, i0);
                                        enc = s.substring(i0, i1);
                                    }
                                    if(enc == null)
                                        enc = "UTF-8";
                                    break;
                                }
                            }
                            
                        default :
                            if ((chk & 0x0ffff0000) == 0x0FEFF0000) {
                                enc = "UTF-16BE";
                                srcBuf[0] =
                                        (char) ((srcBuf[2] << 8) | srcBuf[3]);
                                srcCount = 1;
                            } else if ((chk & 0x0ffff0000) == 0x0fffe0000) {
                                enc = "UTF-16LE";
                                srcBuf[0] =
                                        (char) ((srcBuf[3] << 8) | srcBuf[2]);
                                srcCount = 1;
                            } else if ((chk & 0x0ffffff00) == 0x0EFBBBF00) {
                                enc = "UTF-8";
                                srcBuf[0] = srcBuf[3];
                                srcCount = 1;
                            }
                    }
                }
            }
            System.out.println(enc);
            //if (enc == null)
            //enc = "gb2312";
            
            
            int sc = srcCount;
            if (enc == null)
                setInput(new InputStreamReader(is));
            else
                setInput(new InputStreamReader(is, enc));
            encoding = _enc;
            srcCount = sc;
        } catch (Exception e) {
            throw new IOException();
        }
        
        
    }
    
    public int next() throws IOException {
        
        txtPos = 0;
        isWhitespace = true;
        int minType = 9999;
        token = false;
        
        do {
            nextImpl();
            if (type < minType)
                minType = type;
            //	    if (curr <= TEXT) type = curr;
        }
        while (minType > ENTITY_REF // ignorable
                || (minType >= TEXT && peekType() >= TEXT));
        
        type = minType;
        if (type > TEXT)
            type = TEXT;
        
        return type;
    }
    
    public int getLength(){
        return srcLength;
    }
    
    //获取标签名
    public String getTagName(){
        return name;
    }
    
    //获取标签属性
    public String getAttributeValue(String namespace, String name) {
        
        for (int i = (attributeCount << 2) - 4; i >= 0; i -= 4) {
            if (attributes[i + 2].equals(name)
            && (namespace == null || attributes[i].equals(namespace)))
                return attributes[i + 3];
        }
        
        return null;
    }
    
}

 

 

 

 导读

J2ME Tabbed Menu http://fonter.iteye.com/blog/409982
手机中的重定向问题及处理 http://fonter.iteye.com/blog/400836
J2ME如何通过cmwap直接访问互连网 http://fonter.iteye.com/blog/400868
安装JAD时提示JAD无效原因之一 http://fonter.iteye.com/blog/400888
J2ME网络交互之优化 http://fonter.iteye.com/blog/405137
解决Eclipse无法调试J2ME程序的配置方法 http://fonter.iteye.com/blog/405697

J2ME模拟器加载RMS时突然失效的原因 http://fonter.iteye.com/blog/407576

J2ME飞信协议分析(初稿) http://fonter.iteye.com/blog/408385

分享到:
评论
2 楼 stillhere 2013-08-08  
这个能解析包含中文及中文符号的XML文件么?楼主强大了!
1 楼 best-blue 2011-07-22  
路过,对于能分享代码,分享经验的都要赞一个 

相关推荐

    1945 j2me开源程序

    《1945 J2ME 开源程序:深入解析经典飞行射击游戏的移动实现》 在移动设备尚未普及高性能3D图形的时代,J2ME(Java Micro Edition)作为一款跨平台的开发工具,广泛应用于手机游戏开发。其中,1945是一款经典的飞行...

    J2ME RPG游戏解析

    J2ME是Java平台的一个子集,专门设计用于移动设备和嵌入式系统,使得开发者能够在这些平台上创建丰富的交互式应用程序,包括游戏。 一、J2ME基础 J2ME由几个配置和Profile组成,其中最常用于游戏开发的是MIDP ...

    手机浏览器源码(j2me)

    总结,J2ME手机浏览器源码是一个复杂的系统,涵盖了网络通信、解析渲染、用户交互等多个技术领域。学习和理解这个源码可以提升开发者对于移动设备上的Web技术理解,以及如何在资源受限的环境中实现高效能的应用。

    j2ME_MP3播放器

    3. **JLayer库**:JLayerME0.1.3是基于JLayer的一个版本,JLayer是一个开源的Java MP3解码库。它实现了ID3标签解析和MP3帧解码,使得开发者可以在J2ME环境中播放MP3文件。在项目中,我们需要集成这个库,并通过它的...

    J2ME工具篇.rar

    2. **Eclipse**:Eclipse也是一个广泛使用的开源IDE,通过安装J2ME插件(如WTP - Web Tools Platform或J2ME Wireless Toolkit插件),可以扩展为J2ME开发环境。Eclipse的插件系统允许开发者根据需要自定义工作环境,...

    自己写的一个J2me版的火车时刻表,不完善

    标题中的“自己写的一个J2me版的火车时刻表,不完善”表明这是一个基于Java ME(J2ME)平台开发的简易火车时刻表应用。Java ME是Java的一种轻量级版本,主要用于移动设备和嵌入式设备上的应用程序开发。这个项目可能...

    j2me电子课程表源码

    J2ME(Java 2 Micro Edition)是Java平台的一个子集,主要用于移动设备、嵌入式系统和其他计算资源有限的环境。本项目是一个基于J2ME的电子课程表应用,利用RMS(Record Management System,记录管理系统)来存储和...

    Android代码-安卓J2ME模拟器

    **Android代码 - 安卓J2ME模拟器** 在移动开发领域,J2ME(Java 2 Micro Edition)曾是广泛应用于早期...同时,对于开发者来说,它也是一个学习和研究Android平台上的模拟技术、跨平台移植以及旧代码复用的宝贵资源。

    J2ME 本地视频播放器

    综上所述,"J2ME 本地视频播放器"是一个综合了文件访问、快速加载、解码、用户界面设计、网络播放和性能优化等多个技术领域的项目。开发者需要深入理解J2ME平台的特性,并利用各种开源工具和库来克服限制,提供优质...

    一个基于ECLIPSE的J2ME模拟手机发信息源码

    "一个基于ECLIPSE的J2ME模拟手机发信息源码"这一标题揭示了我们要探讨的核心内容。它指出这是一个利用Eclipse集成开发环境(IDE)实现的J2ME(Java 2 Micro Edition)项目,主要用于模拟手机发送短信的功能。J2ME是...

    lib-j2me.zip_j2me lib

    描述中提到的"java - j2me游戏开发必备",进一步强调了这个库在J2ME游戏开发中的核心地位,而"开源游戏引擎lib9"则揭示了一个名为Lib9的开源引擎,它是专为简化J2ME游戏制作而设计的。 J2ME,作为Java平台的一个...

    J2me2DGame-开源

    本项目“J2me2DGame-开源”就是一个基于J2ME的2D游戏库,它为开发者提供了构建2D游戏所需的各种工具和接口,简化了游戏开发流程。开源软件的特点使得这个项目具有高度的透明性和可定制性,开发者可以根据自己的需求...

    跟我学制作Pak文件j2me.rar_"www.matrix.org.cn"_j2me_j2me matrix.jar_j2me

    Pak文件本质上是一个二进制文件,包含了多个资源文件的索引和实际数据。索引部分记录了每个资源文件在Pak文件中的位置和大小,而数据部分则存放了这些文件的实际内容。通过这种方式,开发者可以将多个资源合并到一个...

    超级小巧,可在J2ME手机上流畅运行的JavaScript引擎

    标签 "工具" 则意味着这个引擎是一个实用的开发工具,帮助开发者在J2ME环境中执行JavaScript脚本,可能包括解析、编译和执行等核心功能。这样的工具对于移动设备上的轻量级应用或者游戏开发尤其有用。 压缩包中的...

    应用于j2me的java mpeg4解码程序

    Java ME(J2ME)是Java平台的一个子集,主要用于嵌入式设备和移动设备的开发,例如手机和智能手表。在给定的资源中,我们聚焦于在J2ME环境中实现MPEG-4视频解码的程序。MPEG-4是一种高效的数字视频编码标准,广泛...

    基于J2ME_MobileSVG的校园地图设计与实现

    TinyLine SDK是一个开源项目,专门为J2ME平台设计,能够高效地解析和渲染MobileSVG文件。 3. **XML属性文件的解析**:地图的属性数据采用XML文件存储,客户端通过KXML 2.0开发包进行解析。KXML是一款轻量级的XML...

    J2ME版的GVmaker模拟器

    NetBeans是一个开源的集成开发环境,支持多种编程语言,包括Java,对Java EE和Java ME应用开发有着优秀的支持。 **核心组成部分解析** 1. **build.xml**: 这是Ant构建脚本,Ant是Apache组织提供的一个Java项目构建...

    VideoMIDlet.rar_j2me_j2me VideoMidlet_j2me player_java vedio pla

    1. **解码器支持**:J2ME本身并不提供视频解码功能,因此开发者需要找到合适的开源库或者自定义实现来解析不同的视频格式,如MPEG-4、3GP等,这些格式在移动设备上较为常见。 2. **流媒体处理**:如果视频来自网络...

    J2ME+servlet+mysql查询数据

    【J2ME】:Java Micro Edition,简称J2ME,是Java平台的一个子集,主要用于嵌入式设备和移动设备的开发,如手机、智能家电等。它提供了基础的类库和API,允许开发者创建轻量级的应用程序。 【Servlet】:Servlet是...

    J2ME 手机程序源代码

    J2ME(Java 2 Micro Edition)是Java平台的一个子集,专为资源有限的嵌入式设备,尤其是移动设备如早期的智能手机和平板电脑设计。J2ME提供了丰富的API,允许开发者创建丰富的用户界面、网络通信和多媒体功能。在...

Global site tag (gtag.js) - Google Analytics