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

📄 wbxmlparser.java

📁 本文档讲解了OTA的概念
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            
            id = readByte();
            while (id > 128 || id == Wbxml.ENTITY
            || id == Wbxml.STR_I || id == Wbxml.STR_T
            || (id >= Wbxml.EXT_I_0 && id <= Wbxml.EXT_I_2)
            || (id >= Wbxml.EXT_T_0 && id <= Wbxml.EXT_T_2)) {
                
                switch (id) {
                    case Wbxml.ENTITY:
                        value.append((char)readInt());
                        break;
                        
                    case Wbxml.STR_I:
                        value.append(readStrI());
                        break;
                        
                    case Wbxml.EXT_I_0:
                    case Wbxml.EXT_I_1:
                    case Wbxml.EXT_I_2:
                    case Wbxml.EXT_T_0:
                    case Wbxml.EXT_T_1:
                    case Wbxml.EXT_T_2:
                    case Wbxml.EXT_0:
                    case Wbxml.EXT_1:
                    case Wbxml.EXT_2:
                    case Wbxml.OPAQUE:
                        
                        ParseEvent e = parseWapExtension(id);
                        if (!(e.getType() != Xml.TEXT
                        && e.getType() != Xml.WHITESPACE))
                            throw new RuntimeException("parse WapExtension must return Text Event in order to work inside Attributes!");
                        
                        value.append(e.getText());
                        
                        break;
                        
                    case Wbxml.STR_T:
                        value.append(readStrT());
                        break;
                        
                    default:
                        value.append(resolveId(attrValueTable, id));
                }
                
                id = readByte();
            }
            
            result.addElement(new Attribute(null, name, value.toString()));
        }
        
        return result;
    }
    
    String resolveId(String[] tab, int id) throws IOException {
        int idx = (id & 0x07f) - 5;
        if (idx == -1)
            return readStrT();
        if (idx < 0 || tab == null
        || idx >= tab.length || tab[idx] == null)
            throw new IOException("id " + id + " undef.");
        
        return tab[idx];
    }
    
    StartTag parseElement(int id) throws IOException {
        
        String tag = resolveId(tagTable, id & 0x03f);
        
        String ns = null;
        
        Vector attributes = null;
        
        // load the attributes if appropriate
        if((id & 128) != 0) {
            attributes=readAttr();
        }
        
        // now pickup the current namespace
        if(documentPubID!=null) {
            if(documentPubID!=currentPubID) {
                if(currentPubID==null) { currentPubID=documentPubID; }
                ns=currentPubID.getNameSpace();
            }
        }
        
        if(ns!=null) {
            if(attributes==null) {
                attributes=new Vector();
            }
            int i=0;
            Attribute attr=null;
            for(i=0;i<attributes.size();i++) {
                attr = (Attribute)attributes.elementAt(i);
                if(attr.getName().equals("xmlns")) {
                    break;
                }
            }
            
            // if i less than the length of the Vector than we've
            // found a namespace
            if(i<attributes.size()) {
                attributes.remove(i);
                attributes.insertElementAt(new Attribute(null, "xmlns", ns),i);
            }
            else {
                attributes.insertElementAt(new Attribute(null, "xmlns", ns),0);
            }
            
        }
        
        // ok, now let's care about attrs etc
        
        try {
            current = new StartTag
            (current, // previous
            ns, // namespace
            tag, // name
            attributes, // attributes
            (id & 64) == 0, // degenerated
            processNamespaces);  // processing
        }
        catch (Exception e) {
            throw new ParseException(null, e, -1, -1);
        }
        
        return current;
    }
    
    int readByte() throws IOException {
        int i = in.read();
        if (i == -1)
            throw new IOException("Unexpected EOF");
        return i;
    }
    
    int readInt() throws IOException {
        int result = 0;
        int i;
        
        do {
            i = readByte();
            result = (result << 7) | (i & 0x7f);
        }
        while ((i & 0x80) != 0);
        
        return result;
    }
    
    String readStrI() throws IOException {
        StringBuffer buf = new StringBuffer();
        boolean wsp = true;
        while (true) {
            int i = in.read();
            if (i == -1)
                throw new IOException("Unexpected EOF");
            if (i == 0)
                break;
            if (i > 32)
                wsp = false;
            buf.append((char)i);
        }
        this.whitespace = wsp;
        return buf.toString();
    }
    
    String readStrT() throws IOException {
        int pos = readInt();
        return extractFromStrT(pos);
    }
    
    private String extractFromStrT(int pos) {
        int end = stringTable.indexOf('\0', pos);
        
        end=end<0?stringTable.length():end;
        
        return stringTable.substring(pos, end);
    }
    
    /**
     * Sets the tag table for a given page.
     *	The first string in the array defines tag 5, the second tag 6 etc.
     *  Currently, only page 0 is supported
     *
     * @param page the page referring to
     * @param tagTable a table filled with tags
     */
    public void setTagTable(int page, String [] tagTable)  {
        otherTagTables.put(new Integer(page),tagTable);
        if(page == 0){
            this.tagTable = tagTable;
        }
    }
    
    
    /**
     * Sets the attribute start Table for a given page.
     *	The first string in the array defines attribute
     *  5, the second attribute 6 etc.
     *  Please use the character '=' (without quote!) as delimiter
     *  between the attribute name and the (start of the) value
     *
     * @param page the page referring to
     * @param attrStartTable a table filled with attribute names
     */
    public void setAttrStartTable(int page, String [] attrStartTable) {
        otherAttrStartTables.put(new Integer(page),attrStartTable);
        if(page == 0){
            this.attrStartTable = attrStartTable;
        }
    }
    
    /**
     * Sets the attribute value Table for a given page.
     *	The first string in the array defines attribute value 0x85,
     *  the second attribute value 0x86 etc.
     *
     * @param page the page referring to
     * @param attrValueTable a table filled with attribute values
     */
    public void setAttrValueTable(int page, String [] attrValueTable) {
        otherAttrValueTables.put(new Integer(page),attrValueTable);
        if(page == 0){
            this.attrValueTable = attrValueTable;
        }
    }
    
    /* Public Identifier information lookup */
    private HashMap myPublicIDMap=null;
    private HashMap myPublicIDCodeMap=null;
    private HashMap myPublicIDIndexMap=null;
    
    void addPublicIDEntry(PublicIDEntry anEntry) {
        if(myPublicIDMap==null) { myPublicIDMap=new HashMap(); }
        if(myPublicIDCodeMap==null) { myPublicIDCodeMap=new HashMap(); }
        if(myPublicIDIndexMap==null) { myPublicIDIndexMap=new HashMap(); }
        
        if(anEntry.getPublicIdentifier()!=null) {
            myPublicIDMap.put(anEntry.getPublicIdentifier().toUpperCase(), anEntry);
        }
        
        if(anEntry.getPublicIDCode()!=null) {
            myPublicIDMap.put(anEntry.getPublicIDCode(), anEntry);
        }
        
        myPublicIDIndexMap.put(new Integer(anEntry.getStrTableIndex()), anEntry);
        
    }
    
    public PublicIDEntry getPublicIDEntry(String anIdentifier) {
        return (PublicIDEntry) myPublicIDMap.get(anIdentifier.toUpperCase());
    }
    
    public PublicIDEntry getPublicIDEntryByCode(int aCode) {
        return (PublicIDEntry) myPublicIDCodeMap.get(new Integer(aCode));
    }
    
    public PublicIDEntry getPublicIDEntryByIndex(int anIndex) {
        
        return (PublicIDEntry) myPublicIDIndexMap.get(new Integer(anIndex));
    }
    
}






⌨️ 快捷键说明

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