📄 wbxmlparser.java
字号:
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) 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) throw new RuntimeException( "Undefined Prefix: " + attrPrefix + " in " + this); attributes[i] = attrNs; attributes[i + 1] = attrPrefix; attributes[i + 2] = attrName; 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) exception("illegal tag name: " + name); else if (cut != -1) { prefix = name.substring(0, cut); name = name.substring(cut + 1); } this.namespace = getNamespace(prefix); if (this.namespace == null) { if (prefix != null) exception("undefined prefix: " + prefix); this.namespace = NO_NAMESPACE; } return any; } private final void setTable(int page, int type, String[] table) { if(stringTable != null){ throw new RuntimeException("setXxxTable must be called before setInput!"); } while(tables.size() < 3*page +3){ tables.addElement(null); } tables.setElementAt(table, page*3+type); } private final void exception(String desc) throws XmlPullParserException { throw new XmlPullParserException(desc, this, null); } private void selectPage(int nr, boolean tags) throws XmlPullParserException{ if(tables.size() == 0 && nr == 0) return; if(nr*3 > tables.size()) exception("Code Page "+nr+" undefined!"); if(tags) tagTable = (String[]) tables.elementAt(nr * 3 + TAG_TABLE); else { attrStartTable = (String[]) tables.elementAt(nr * 3 + ATTR_START_TABLE); attrValueTable = (String[]) tables.elementAt(nr * 3 + ATTR_VALUE_TABLE); } } private final void nextImpl() throws IOException, XmlPullParserException { String s; if (type == END_TAG) { depth--; } if (degenerated) { type = XmlPullParser.END_TAG; degenerated = false; return; } text = null; prefix = null; name = null; int id = peekId (); while(id == Wbxml.SWITCH_PAGE){ nextId = -2; selectPage(readByte(), true); id = peekId(); } nextId = -2; switch (id) { case -1 : type = XmlPullParser.END_DOCUMENT; break; case Wbxml.END : { int sp = (depth - 1) << 2; type = END_TAG; namespace = elementStack[sp]; prefix = elementStack[sp + 1]; name = elementStack[sp + 2]; } break; case Wbxml.ENTITY : { type = ENTITY_REF; char c = (char) readInt(); text = "" + c; name = "#" + ((int) c); } break; case Wbxml.STR_I : type = TEXT; text = 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 : parseWapExtension(id); break; case Wbxml.PI : throw new RuntimeException("PI curr. not supp."); // readPI; // break; case Wbxml.STR_T : { type = TEXT; int pos = readInt(); int end = stringTable.indexOf('\0', pos); text = stringTable.substring(pos, end); } break; default : parseElement(id); } // } // while (next == null); // return next; } public void parseWapExtension(int id) throws IOException, XmlPullParserException { type = WAP_EXTENSION; wapExtensionCode = id; switch (id) { case Wbxml.EXT_I_0 : case Wbxml.EXT_I_1 : case Wbxml.EXT_I_2 : wapExtensionData = readStrI(); break; case Wbxml.EXT_T_0 : case Wbxml.EXT_T_1 : case Wbxml.EXT_T_2 : wapExtensionData = new Integer(readInt()); break; case Wbxml.EXT_0 : case Wbxml.EXT_1 : case Wbxml.EXT_2 : break; case Wbxml.OPAQUE : { int len = readInt(); byte[] buf = new byte[len]; for (int i = 0; i < len; i++) // enhance with blockread! buf[i] = (byte) readByte(); wapExtensionData = buf; } // case OPAQUE break; default: exception("illegal id: "+id); } // SWITCH } public void readAttr() throws IOException, XmlPullParserException { int id = readByte(); int i = 0; while (id != 1) { while(id == Wbxml.SWITCH_PAGE){ selectPage(readByte(), false); id = readByte(); } String name = resolveId(attrStartTable, id); StringBuffer value; int cut = name.indexOf('='); if (cut == -1) value = new StringBuffer(); else { value = new StringBuffer(name.substring(cut + 1)); name = name.substring(0, cut); } id = readByte(); while (id > 128 || id == Wbxml.SWITCH_PAGE || 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.SWITCH_PAGE : selectPage(readByte(), false); break; 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 : throw new RuntimeException("wap extension in attr not supported yet"); /* 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()); //value.append (handleExtension (id)); // skip EXT in ATTR //break; */ case Wbxml.STR_T : value.append(readStrT()); break; default : value.append( resolveId(attrValueTable, id)); } id = readByte(); } attributes = ensureCapacity(attributes, i + 4); attributes[i++] = ""; attributes[i++] = null; attributes[i++] = name; attributes[i++] = value.toString(); attributeCount++; } } private int peekId () throws IOException { if (nextId == -2) { nextId = in.read (); } return nextId; } 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]; } void parseElement(int id) throws IOException, XmlPullParserException { type = START_TAG; name = resolveId(tagTable, id & 0x03f); attributeCount = 0; if ((id & 128) != 0) { readAttr(); } degenerated = (id & 64) == 0; int sp = depth++ << 2; // transfer to element stack 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]; 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 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; } 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); } isWhitespace = wsp; return buf.toString(); } String readStrT() throws IOException { int pos = readInt(); int end = stringTable.indexOf('\0', pos); 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. */ public void setTagTable(int page, String[] table) { setTable(page, TAG_TABLE, table); // this.tagTable = tagTable; // if (page != 0) // throw new RuntimeException("code pages curr. not supp."); } /** Sets the attribute start Table for a given page. * The first string in the array defines attribute * 5, the second attribute 6 etc. * Currently, only page 0 is supported. Please use the * character '=' (without quote!) as delimiter * between the attribute name and the (start of the) value */ public void setAttrStartTable( int page, String[] table) { setTable(page, ATTR_START_TABLE, table); } /** 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. * Currently, only page 0 is supported. */ public void setAttrValueTable( int page, String[] table) { setTable(page, ATTR_VALUE_TABLE, table); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -