1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
|
/* Copyright (c) 2002,2003,2004 Stefan Haustein, Oberhausen, Rhld., Germany
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE. */
// Contributors: Bjorn Aadland, Chris Bartley, Nicola Fankhauser,
// Victor Havin, Christian Kurzke, Bogdan Onoiu,
// Jain Sanjay, David Santoro.
package org.kxml2.wap;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.util.Hashtable;
import java.util.Vector;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
public class WbxmlParser implements XmlPullParser {
public static final int WAP_EXTENSION = 64;
static final private String UNEXPECTED_EOF =
"Unexpected EOF";
static final private String ILLEGAL_TYPE =
"Wrong event type";
private InputStream in;
private int TAG_TABLE = 0;
private int ATTR_START_TABLE = 1;
private int ATTR_VALUE_TABLE = 2;
private String[] attrStartTable;
private String[] attrValueTable;
private String[] tagTable;
private byte[] stringTable;
private Hashtable cacheStringTable = null;
private boolean processNsp;
private int depth;
private String[] elementStack = new String[16];
private String[] nspStack = new String[8];
private int[] nspCounts = new int[4];
private int attributeCount;
private String[] attributes = new String[16];
private int nextId = -2;
private Vector tables = new Vector();
int version;
int publicIdentifierId;
int charSet;
// StartTag current;
// ParseEvent next;
private String prefix;
private String namespace;
private String name;
private String text;
//private String encoding;
private Object wapExtensionData;
private int wapExtensionCode;
private int type;
private int codePage;
private boolean degenerated;
private boolean isWhitespace;
private String encoding = null;
public boolean getFeature(String feature) {
if (XmlPullParser
.FEATURE_PROCESS_NAMESPACES
.equals(feature))
return processNsp;
else
return false;
}
public String getInputEncoding() {
// should return someting depending on charSet here!!!!!
return encoding;
}
public void defineEntityReplacementText(
String entity,
String value)
throws XmlPullParserException {
// just ignore, has no effect
}
public Object getProperty(String property) {
return null;
}
public int getNamespaceCount(int depth) {
if (depth > this.depth)
throw new IndexOutOfBoundsException();
return nspCounts[depth];
}
public String getNamespacePrefix(int pos) {
return nspStack[pos << 1];
}
public String getNamespaceUri(int pos) {
return nspStack[(pos << 1) + 1];
}
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 getDepth() {
return depth;
}
public String getPositionDescription() {
StringBuffer buf =
new StringBuffer(
type < TYPES.length ? TYPES[type] : "unknown");
buf.append(' ');
if (type == START_TAG || type == END_TAG) {
if (degenerated)
buf.append("(empty) ");
buf.append('<');
if (type == END_TAG)
buf.append('/');
if (prefix != null)
buf.append("{" + namespace + "}" + prefix + ":");
buf.append(name);
int cnt = attributeCount << 2;
for (int i = 0; i < cnt; i += 4) {
buf.append(' ');
if (attributes[i + 1] != null)
buf.append(
"{"
+ attributes[i]
+ "}"
+ attributes[i
+ 1]
+ ":");
buf.append(
attributes[i
+ 2]
+ "='"
+ attributes[i
+ 3]
+ "'");
}
buf.append('>');
}
else if (type == IGNORABLE_WHITESPACE);
else if (type != TEXT)
buf.append(getText());
else if (isWhitespace)
buf.append("(whitespace)");
else {
String text = getText();
if (text.length() > 16)
text = text.substring(0, 16) + "...";
buf.append(text);
}
return buf.toString();
}
public int getLineNumber() {
return -1;
}
public int getColumnNumber() {
return -1;
}
public boolean isWhitespace()
throws XmlPullParserException {
if (type != TEXT
&& type != IGNORABLE_WHITESPACE
&& type != CDSECT)
exception(ILLEGAL_TYPE);
return isWhitespace;
}
public String getText() {
return text;
}
public char[] getTextCharacters(int[] poslen) {
if (type >= TEXT) {
poslen[0] = 0;
poslen[1] = text.length();
char[] buf = new char[text.length()];
text.getChars(0, text.length(), buf, 0);
return buf;
}
poslen[0] = -1;
poslen[1] = -1;
return null;
}
public String getNamespace() {
return namespace;
}
public String getName() {
return name;
}
public String getPrefix() {
return prefix;
}
public boolean isEmptyElementTag()
throws XmlPullParserException {
if (type != START_TAG)
exception(ILLEGAL_TYPE);
return degenerated;
}
public int getAttributeCount() {
return attributeCount;
}
public String getAttributeType(int index) {
return "CDATA";
}
public boolean isAttributeDefault(int index) {
return false;
}
public String getAttributeNamespace(int index) {
if (index >= attributeCount)
throw new IndexOutOfBoundsException();
return attributes[index << 2];
}
public String getAttributeName(int index) {
if (index >= attributeCount)
throw new IndexOutOfBoundsException();
return attributes[(index << 2) + 2];
}
public String getAttributePrefix(int index) {
if (index >= attributeCount)
throw new IndexOutOfBoundsException();
return attributes[(index << 2) + 1];
}
public String getAttributeValue(int index) {
if (index >= attributeCount)
throw new IndexOutOfBoundsException();
return attributes[(index << 2) + 3];
}
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;
}
public int getEventType() throws XmlPullParserException {
return type;
}
public int next() throws XmlPullParserException, IOException {
isWhitespace = true;
int minType = 9999;
while (true) {
String save = text;
nextImpl();
if (type < minType)
minType = type;
if (minType > CDSECT) continue; // no "real" event so far
if (minType >= TEXT) { // text, see if accumulate
if (save != null) text = text == null ? save : save + text;
switch(peekId()) {
case Wbxml.ENTITY:
case Wbxml.STR_I:
case Wbxml.LITERAL:
case Wbxml.LITERAL_C:
case Wbxml.LITERAL_A:
case Wbxml.LITERAL_AC: continue;
}
}
break;
}
type = minType;
if (type > TEXT)
type = TEXT;
return type;
}
public int nextToken() throws XmlPullParserException, IOException {
isWhitespace = true;
nextImpl();
return type;
}
public int nextTag() throws XmlPullParserException, IOException {
next();
if (type == TEXT && isWhitespace)
next();
if (type != END_TAG && type != START_TAG)
exception("unexpected type");
return type;
}
public String nextText() throws XmlPullParserException, IOException {
if (type != START_TAG)
exception("precondition: START_TAG");
next();
String result;
if (type == TEXT) {
result = getText();
next();
}
else
result = "";
if (type != END_TAG)
exception("END_TAG expected");
return result;
}
public void require(int type, String namespace, String name)
throws XmlPullParserException, IOException {
if (type != this.type
|| (namespace != null && !namespace.equals(getNamespace()))
|| (name != null && !name.equals(getName())))
exception(
"expected: " + TYPES[type] + " {" + namespace + "}" + name);
}
public void setInput(Reader reader) throws XmlPullParserException {
exception("InputStream required");
}
public void setInput(InputStream in, String enc)
throws XmlPullParserException {
this.in = in;
try {
version = readByte();
publicIdentifierId = readInt();
if (publicIdentifierId == 0)
readInt();
int charset = readInt(); // skip charset
if (null == enc){
switch (charset){
case 4: encoding = "ISO-8859-1"; break;
case 106: encoding = "UTF-8"; break;
// add more if you need them
// http://www.iana.org/assignments/character-sets
// case MIBenum: encoding = Name break;
default: throw new UnsupportedEncodingException(""+charset);
}
}else{
encoding = enc;
}
int strTabSize = readInt();
stringTable = new byte[strTabSize];
int ok = 0;
while(ok < strTabSize){
int cnt = in.read(stringTable, ok, strTabSize - ok);
if(cnt <= 0) break;
ok += cnt;
}
selectPage(0, true);
selectPage(0, false);
}
catch (IOException e) {
exception("Illegal input format");
}
}
public void setFeature(String feature, boolean value)
throws XmlPullParserException {
if (XmlPullParser.FEATURE_PROCESS_NAMESPACES.equals(feature))
processNsp = value;
else
exception("unsupported feature: " + feature);
}
public void setProperty(String property, Object value)
throws XmlPullParserException {
throw new XmlPullParserException("unsupported property: " + property);
}
// ---------------------- private / internal methods
private final boolean adjustNsp()
throws XmlPullParserException {
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(""))
exception("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)
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 {
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);
if (this.wapExtensionData != null) {
type = TEXT;
text = (String) this.wapExtensionData;
this.wapExtensionData = null;
}
break;
}
case Wbxml.PI :
throw new RuntimeException("PI curr. not supp.");
// readPI;
// break;
case Wbxml.STR_T :
{
type = TEXT;
text = readStrT();
}
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 :
int attId = readInt();
wapExtensionData = resolveAttributeId(attrValueTable, attId);
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();
}
int opData = this.parseOpaqueData(buf);
wapExtensionData = new Integer(opData).toString();
//byte[] temp = {0x2, 0x58};
//logger.info("Op wapExtensionData: " + this.parseOpaqueData(temp));
} // case OPAQUE
break;
default:
exception("illegal id: "+id);
} // SWITCH
}
private int parseOpaqueData(byte[] in) {
int result = 0;
/*
for (int i = 0 ; i < in.length; i++) {
int right = 1;
for (int j = 0; j < (in.length - i - 1); j++) {
right *= 256;
}
result += in[i] * right;
}
*/
for (int i = 0 ; i < in.length; i++) {
result += in[i];
for (int j = 0; j < (in.length - i - 1); j++) {
result = result << 8;
}
}
return result;
}
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];
}
String resolveAttributeId(String[] attrTable, int id) {
if (id > attrTable.length)
return null;
return attrTable[id];
}
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 {
ByteArrayOutputStream buf = new ByteArrayOutputStream();
boolean wsp = true;
while (true){
int i = in.read();
if (i == 0){
break;
}
if (i == -1){
throw new IOException(UNEXPECTED_EOF);
}
if (i > 32){
wsp = false;
}
buf.write(i);
}
isWhitespace = wsp;
String result = new String(buf.toByteArray(), encoding);
buf.close();
return formatString(result);
}
String formatString(String str) {
String result = str.replaceAll("&", "&");
return result;
}
String readStrT() throws IOException {
int pos = readInt();
// As the main reason of stringTable is compression we build a cache of Strings
// stringTable is supposed to help create Strings from parts which means some cache hit rate
// This will help to minimize the Strings created when invoking readStrT() repeatedly
if (cacheStringTable == null){
//Lazy init if device is not using StringTable but inline 0x03 strings
cacheStringTable = new Hashtable();
}
String forReturn = (String) cacheStringTable.get(new Integer(pos));
if (forReturn == null){
int end = pos;
while(end < stringTable.length && stringTable[end] != '\0'){
end++;
}
forReturn = new String(stringTable, pos, end-pos, encoding);
cacheStringTable.put(new Integer(pos), forReturn);
}
return forReturn;
}
/**
* 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. 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.
*/
public void setAttrValueTable(
int page,
String[] table) {
setTable(page, ATTR_VALUE_TABLE, table);
}
}
|