/
XmlManifestReader.java
1489 lines (1316 loc) · 36.8 KB
/
XmlManifestReader.java
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
/*
* Copyright 2008 Android4ME
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package androidx.pluginmgr.utils;
import android.util.TypedValue;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import java.io.EOFException;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
/**
*
* Read xml document from Android's binary xml file.
* 之所以不用反射直接调用而将它抠出来是因为这样能够保证稳定性.
*
*
*/
class XmlManifestReader {
public static final String DEFAULT_XML = "AndroidManifest.xml";
private XmlManifestReader() {
}
public static String getManifestXMLFromAPK(String apkPath) {
ZipFile file = null;
String rs = null;
try {
File apkFile = new File(apkPath);
file = new ZipFile(apkFile, ZipFile.OPEN_READ);
ZipEntry entry = file.getEntry(DEFAULT_XML);
rs = getManifestXMLFromAPK(file, entry);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (file != null) {
try {
file.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return rs;
}
public static String getManifestXMLFromAPK(ZipFile file, ZipEntry entry) {
StringBuilder xmlSb = new StringBuilder(100);
XmlResourceParser parser = null;
try {
parser = new XmlResourceParser();
parser.open(file.getInputStream(entry));
StringBuilder sb = new StringBuilder(10);
final String indentStep = " ";
int type;
while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
switch (type) {
case XmlPullParser.START_DOCUMENT: {
log(xmlSb, "<?xml version=\"1.0\" encoding=\"utf-8\"?>");
break;
}
case XmlPullParser.START_TAG: {
log(false, xmlSb, "%s<%s%s", sb,
getNamespacePrefix(parser.getPrefix()),
parser.getName());
sb.append(indentStep);
int namespaceCountBefore = parser.getNamespaceCount(parser
.getDepth() - 1);
int namespaceCount = parser.getNamespaceCount(parser
.getDepth());
for (int i = namespaceCountBefore; i != namespaceCount; ++i) {
log(xmlSb, "%sxmlns:%s=\"%s\"",
i == namespaceCountBefore ? " " : sb,
parser.getNamespacePrefix(i),
parser.getNamespaceUri(i));
}
for (int i = 0, size = parser.getAttributeCount(); i != size; ++i) {
log(false,
xmlSb,
"%s%s%s=\"%s\"",
" ",
getNamespacePrefix(parser.getAttributePrefix(i)),
parser.getAttributeName(i),
getAttributeValue(parser, i));
}
// log("%s>",sb);
log(xmlSb, ">");
break;
}
case XmlPullParser.END_TAG: {
sb.setLength(sb.length() - indentStep.length());
log(xmlSb, "%s</%s%s>", sb,
getNamespacePrefix(parser.getPrefix()),
parser.getName());
break;
}
case XmlPullParser.TEXT: {
log(xmlSb, "%s%s", sb, parser.getText());
break;
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
parser.close();
}
return xmlSb.toString();
}
private static String getNamespacePrefix(String prefix) {
if (prefix == null || prefix.length() == 0) {
return "";
}
return prefix + ":";
}
private static String getAttributeValue(XmlResourceParser parser, int index) {
int type = parser.getAttributeValueType(index);
int data = parser.getAttributeValueData(index);
if (type == TypedValue.TYPE_STRING) {
return parser.getAttributeValue(index);
}
if (type == TypedValue.TYPE_ATTRIBUTE) {
return String.format("?%s%08X", getPackage(data), data);
}
if (type == TypedValue.TYPE_REFERENCE) {
return String.format("@%s%08X", getPackage(data), data);
}
if (type == TypedValue.TYPE_FLOAT) {
return String.valueOf(Float.intBitsToFloat(data));
}
if (type == TypedValue.TYPE_INT_HEX) {
return String.format("0x%08X", data);
}
if (type == TypedValue.TYPE_INT_BOOLEAN) {
return data != 0 ? "true" : "false";
}
if (type == TypedValue.TYPE_DIMENSION) {
return Float.toString(complexToFloat(data))
+ DIMENSION_UNITS[data & TypedValue.COMPLEX_UNIT_MASK];
}
if (type == TypedValue.TYPE_FRACTION) {
return Float.toString(complexToFloat(data))
+ FRACTION_UNITS[data & TypedValue.COMPLEX_UNIT_MASK];
}
if (type >= TypedValue.TYPE_FIRST_COLOR_INT
&& type <= TypedValue.TYPE_LAST_COLOR_INT) {
return String.format("#%08X", data);
}
if (type >= TypedValue.TYPE_FIRST_INT
&& type <= TypedValue.TYPE_LAST_INT) {
return String.valueOf(data);
}
return String.format("<0x%X, type 0x%02X>", data, type);
}
private static String getPackage(int id) {
if (id >>> 24 == 1) {
return "android:";
}
return "";
}
private static void log(StringBuilder xmlSb, String format,
Object... arguments) {
log(true, xmlSb, format, arguments);
}
private static void log(boolean newLine, StringBuilder xmlSb,
String format, Object... arguments) {
// System.out.printf(format,arguments);
// if(newLine) System.out.println();
xmlSb.append(String.format(format, arguments));
if (newLine)
xmlSb.append("\n");
}
// ///////////////////////////////// ILLEGAL STUFF, DONT LOOK :)
private static float complexToFloat(int complex) {
return (float) (complex & 0xFFFFFF00) * RADIX_MULTS[(complex >> 4) & 3];
}
private static final float RADIX_MULTS[] = { 0.00390625F, 3.051758E-005F,
1.192093E-007F, 4.656613E-010F };
private static final String DIMENSION_UNITS[] = { "px", "dip", "sp", "pt",
"in", "mm", "", "" };
private static final String FRACTION_UNITS[] = { "%", "%p", "", "", "", "",
"", "" };
}
/**
* @author Dmitry Skiba
*
* Binary xml files parser.
*
* Parser has only two states: (1) Operational state, which parser
* obtains after first successful call to next() and retains until
* open(), close(), or failed call to next(). (2) Closed state, which
* parser obtains after open(), close(), or failed call to next(). In
* this state methods return invalid values or throw exceptions.
*
* TODO: * check all methods in closed state
*
*/
class XmlResourceParser implements android.content.res.XmlResourceParser {
public XmlResourceParser() {
resetEventInfo();
}
public void open(InputStream stream) {
close();
if (stream != null) {
m_reader = new IntReader(stream, false);
}
}
public void close() {
if (!m_operational) {
return;
}
m_operational = false;
m_reader.close();
m_reader = null;
m_strings = null;
m_resourceIDs = null;
m_namespaces.reset();
resetEventInfo();
}
public static final void readCheckType(IntReader reader, int expectedType)
throws IOException {
int type = reader.readInt();
if (type != expectedType) {
throw new IOException("Expected chunk of type 0x"
+ Integer.toHexString(expectedType) + ", read 0x"
+ Integer.toHexString(type) + ".");
}
}
// ///////////////////////////////// iteration
public int next() throws XmlPullParserException, IOException {
if (m_reader == null) {
throw new XmlPullParserException("Parser is not opened.", this,
null);
}
try {
doNext();
return m_event;
} catch (IOException e) {
close();
throw e;
}
}
public int nextToken() throws XmlPullParserException, IOException {
return next();
}
public int nextTag() throws XmlPullParserException, IOException {
int eventType = next();
if (eventType == TEXT && isWhitespace()) {
eventType = next();
}
if (eventType != START_TAG && eventType != END_TAG) {
throw new XmlPullParserException("Expected start or end tag.",
this, null);
}
return eventType;
}
public String nextText() throws XmlPullParserException, IOException {
if (getEventType() != START_TAG) {
throw new XmlPullParserException(
"Parser must be on START_TAG to read next text.", this,
null);
}
int eventType = next();
if (eventType == TEXT) {
String result = getText();
eventType = next();
if (eventType != END_TAG) {
throw new XmlPullParserException(
"Event TEXT must be immediately followed by END_TAG.",
this, null);
}
return result;
} else if (eventType == END_TAG) {
return "";
} else {
throw new XmlPullParserException(
"Parser must be on START_TAG or TEXT to read text.", this,
null);
}
}
public void require(int type, String namespace, String name)
throws XmlPullParserException, IOException {
if (type != getEventType()
|| (namespace != null && !namespace.equals(getNamespace()))
|| (name != null && !name.equals(getName()))) {
throw new XmlPullParserException(TYPES[type] + " is expected.",
this, null);
}
}
public int getDepth() {
return m_namespaces.getDepth() - 1;
}
public int getEventType() throws XmlPullParserException {
return m_event;
}
public int getLineNumber() {
return m_lineNumber;
}
public String getName() {
if (m_name == -1 || (m_event != START_TAG && m_event != END_TAG)) {
return null;
}
return m_strings.getString(m_name);
}
public String getText() {
if (m_name == -1 || m_event != TEXT) {
return null;
}
return m_strings.getString(m_name);
}
public char[] getTextCharacters(int[] holderForStartAndLength) {
String text = getText();
if (text == null) {
return null;
}
holderForStartAndLength[0] = 0;
holderForStartAndLength[1] = text.length();
char[] chars = new char[text.length()];
text.getChars(0, text.length(), chars, 0);
return chars;
}
public String getNamespace() {
return m_strings.getString(m_namespaceUri);
}
public String getPrefix() {
int prefix = m_namespaces.findPrefix(m_namespaceUri);
return m_strings.getString(prefix);
}
public String getPositionDescription() {
return "XML line #" + getLineNumber();
}
public int getNamespaceCount(int depth) throws XmlPullParserException {
return m_namespaces.getAccumulatedCount(depth);
}
public String getNamespacePrefix(int pos) throws XmlPullParserException {
int prefix = m_namespaces.getPrefix(pos);
return m_strings.getString(prefix);
}
public String getNamespaceUri(int pos) throws XmlPullParserException {
int uri = m_namespaces.getUri(pos);
return m_strings.getString(uri);
}
// ///////////////////////////////// attributes
public String getClassAttribute() {
if (m_classAttribute == -1) {
return null;
}
int offset = getAttributeOffset(m_classAttribute);
int value = m_attributes[offset + ATTRIBUTE_IX_VALUE_STRING];
return m_strings.getString(value);
}
public String getIdAttribute() {
if (m_idAttribute == -1) {
return null;
}
int offset = getAttributeOffset(m_idAttribute);
int value = m_attributes[offset + ATTRIBUTE_IX_VALUE_STRING];
return m_strings.getString(value);
}
public int getIdAttributeResourceValue(int defaultValue) {
if (m_idAttribute == -1) {
return defaultValue;
}
int offset = getAttributeOffset(m_idAttribute);
int valueType = m_attributes[offset + ATTRIBUTE_IX_VALUE_TYPE];
if (valueType != TypedValue.TYPE_REFERENCE) {
return defaultValue;
}
return m_attributes[offset + ATTRIBUTE_IX_VALUE_DATA];
}
public int getStyleAttribute() {
if (m_styleAttribute == -1) {
return 0;
}
int offset = getAttributeOffset(m_styleAttribute);
return m_attributes[offset + ATTRIBUTE_IX_VALUE_DATA];
}
public int getAttributeCount() {
if (m_event != START_TAG) {
return -1;
}
return m_attributes.length / ATTRIBUTE_LENGHT;
}
public String getAttributeNamespace(int index) {
int offset = getAttributeOffset(index);
int namespace = m_attributes[offset + ATTRIBUTE_IX_NAMESPACE_URI];
if (namespace == -1) {
return "";
}
return m_strings.getString(namespace);
}
public String getAttributePrefix(int index) {
int offset = getAttributeOffset(index);
int uri = m_attributes[offset + ATTRIBUTE_IX_NAMESPACE_URI];
int prefix = m_namespaces.findPrefix(uri);
if (prefix == -1) {
return "";
}
return m_strings.getString(prefix);
}
public String getAttributeName(int index) {
int offset = getAttributeOffset(index);
int name = m_attributes[offset + ATTRIBUTE_IX_NAME];
if (name == -1) {
return "";
}
return m_strings.getString(name);
}
public int getAttributeNameResource(int index) {
int offset = getAttributeOffset(index);
int name = m_attributes[offset + ATTRIBUTE_IX_NAME];
if (m_resourceIDs == null || name < 0 || name >= m_resourceIDs.length) {
return 0;
}
return m_resourceIDs[name];
}
public int getAttributeValueType(int index) {
int offset = getAttributeOffset(index);
return m_attributes[offset + ATTRIBUTE_IX_VALUE_TYPE];
}
public int getAttributeValueData(int index) {
int offset = getAttributeOffset(index);
return m_attributes[offset + ATTRIBUTE_IX_VALUE_DATA];
}
@SuppressWarnings("unused")
public String getAttributeValue(int index) {
int offset = getAttributeOffset(index);
int valueType = m_attributes[offset + ATTRIBUTE_IX_VALUE_TYPE];
if (valueType == TypedValue.TYPE_STRING) {
int valueString = m_attributes[offset + ATTRIBUTE_IX_VALUE_STRING];
return m_strings.getString(valueString);
}
int valueData = m_attributes[offset + ATTRIBUTE_IX_VALUE_DATA];
return "";// TypedValue.coerceToString(valueType,valueData);
}
public boolean getAttributeBooleanValue(int index, boolean defaultValue) {
return getAttributeIntValue(index, defaultValue ? 1 : 0) != 0;
}
public float getAttributeFloatValue(int index, float defaultValue) {
int offset = getAttributeOffset(index);
int valueType = m_attributes[offset + ATTRIBUTE_IX_VALUE_TYPE];
if (valueType == TypedValue.TYPE_FLOAT) {
int valueData = m_attributes[offset + ATTRIBUTE_IX_VALUE_DATA];
return Float.intBitsToFloat(valueData);
}
return defaultValue;
}
public int getAttributeIntValue(int index, int defaultValue) {
int offset = getAttributeOffset(index);
int valueType = m_attributes[offset + ATTRIBUTE_IX_VALUE_TYPE];
if (valueType >= TypedValue.TYPE_FIRST_INT
&& valueType <= TypedValue.TYPE_LAST_INT) {
return m_attributes[offset + ATTRIBUTE_IX_VALUE_DATA];
}
return defaultValue;
}
public int getAttributeUnsignedIntValue(int index, int defaultValue) {
return getAttributeIntValue(index, defaultValue);
}
public int getAttributeResourceValue(int index, int defaultValue) {
int offset = getAttributeOffset(index);
int valueType = m_attributes[offset + ATTRIBUTE_IX_VALUE_TYPE];
if (valueType == TypedValue.TYPE_REFERENCE) {
return m_attributes[offset + ATTRIBUTE_IX_VALUE_DATA];
}
return defaultValue;
}
public String getAttributeValue(String namespace, String attribute) {
int index = findAttribute(namespace, attribute);
if (index == -1) {
return null;
}
return getAttributeValue(index);
}
public boolean getAttributeBooleanValue(String namespace, String attribute,
boolean defaultValue) {
int index = findAttribute(namespace, attribute);
if (index == -1) {
return defaultValue;
}
return getAttributeBooleanValue(index, defaultValue);
}
public float getAttributeFloatValue(String namespace, String attribute,
float defaultValue) {
int index = findAttribute(namespace, attribute);
if (index == -1) {
return defaultValue;
}
return getAttributeFloatValue(index, defaultValue);
}
public int getAttributeIntValue(String namespace, String attribute,
int defaultValue) {
int index = findAttribute(namespace, attribute);
if (index == -1) {
return defaultValue;
}
return getAttributeIntValue(index, defaultValue);
}
public int getAttributeUnsignedIntValue(String namespace, String attribute,
int defaultValue) {
int index = findAttribute(namespace, attribute);
if (index == -1) {
return defaultValue;
}
return getAttributeUnsignedIntValue(index, defaultValue);
}
public int getAttributeResourceValue(String namespace, String attribute,
int defaultValue) {
int index = findAttribute(namespace, attribute);
if (index == -1) {
return defaultValue;
}
return getAttributeResourceValue(index, defaultValue);
}
public int getAttributeListValue(int index, String[] options,
int defaultValue) {
// TODO implement
return 0;
}
public int getAttributeListValue(String namespace, String attribute,
String[] options, int defaultValue) {
// TODO implement
return 0;
}
public String getAttributeType(int index) {
return "CDATA";
}
public boolean isAttributeDefault(int index) {
return false;
}
// ///////////////////////////////// dummies
public void setInput(InputStream stream, String inputEncoding)
throws XmlPullParserException {
throw new XmlPullParserException(E_NOT_SUPPORTED);
}
public void setInput(Reader reader) throws XmlPullParserException {
throw new XmlPullParserException(E_NOT_SUPPORTED);
}
public String getInputEncoding() {
return null;
}
public int getColumnNumber() {
return -1;
}
public boolean isEmptyElementTag() throws XmlPullParserException {
return false;
}
public boolean isWhitespace() throws XmlPullParserException {
return false;
}
public void defineEntityReplacementText(String entityName,
String replacementText) throws XmlPullParserException {
throw new XmlPullParserException(E_NOT_SUPPORTED);
}
public String getNamespace(String prefix) {
throw new RuntimeException(E_NOT_SUPPORTED);
}
public Object getProperty(String name) {
return null;
}
public void setProperty(String name, Object value)
throws XmlPullParserException {
throw new XmlPullParserException(E_NOT_SUPPORTED);
}
public boolean getFeature(String feature) {
return false;
}
public void setFeature(String name, boolean value)
throws XmlPullParserException {
throw new XmlPullParserException(E_NOT_SUPPORTED);
}
// final void fetchAttributes(int[] styleableIDs,TypedArray result) {
// result.resetIndices();
// if (m_attributes==null || m_resourceIDs==null) {
// return;
// }
// boolean needStrings=false;
// for (int i=0,e=styleableIDs.length;i!=e;++i) {
// int id=styleableIDs[i];
// for (int o=0;o!=m_attributes.length;o+=ATTRIBUTE_LENGHT) {
// int name=m_attributes[o+ATTRIBUTE_IX_NAME];
// if (name>=m_resourceIDs.length ||
// m_resourceIDs[name]!=id)
// {
// continue;
// }
// int valueType=m_attributes[o+ATTRIBUTE_IX_VALUE_TYPE];
// int valueData;
// int assetCookie;
// if (valueType==TypedValue.TYPE_STRING) {
// valueData=m_attributes[o+ATTRIBUTE_IX_VALUE_STRING];
// assetCookie=-1;
// needStrings=true;
// } else {
// valueData=m_attributes[o+ATTRIBUTE_IX_VALUE_DATA];
// assetCookie=0;
// }
// result.addValue(i,valueType,valueData,assetCookie,id,0);
// }
// }
// if (needStrings) {
// result.setStrings(m_strings);
// }
// }
final StringBlock getStrings() {
return m_strings;
}
// /////////////////////////////////
private final int getAttributeOffset(int index) {
if (m_event != START_TAG) {
throw new IndexOutOfBoundsException(
"Current event is not START_TAG.");
}
int offset = index * 5;
if (offset >= m_attributes.length) {
throw new IndexOutOfBoundsException("Invalid attribute index ("
+ index + ").");
}
return offset;
}
private final int findAttribute(String namespace, String attribute) {
if (m_strings == null || attribute == null) {
return -1;
}
int name = m_strings.find(attribute);
if (name == -1) {
return -1;
}
int uri = (namespace != null) ? m_strings.find(namespace) : -1;
for (int o = 0; o != m_attributes.length; ++o) {
if (name == m_attributes[o + ATTRIBUTE_IX_NAME]
&& (uri == -1 || uri == m_attributes[o
+ ATTRIBUTE_IX_NAMESPACE_URI])) {
return o / ATTRIBUTE_LENGHT;
}
}
return -1;
}
private final void resetEventInfo() {
m_event = -1;
m_lineNumber = -1;
m_name = -1;
m_namespaceUri = -1;
m_attributes = null;
m_idAttribute = -1;
m_classAttribute = -1;
m_styleAttribute = -1;
}
private final void doNext() throws IOException {
// Delayed initialization.
if (m_strings == null) {
readCheckType(m_reader, CHUNK_AXML_FILE);
/* chunkSize */m_reader.skipInt();
m_strings = StringBlock.read(m_reader);
m_namespaces.increaseDepth();
m_operational = true;
}
if (m_event == END_DOCUMENT) {
return;
}
int event = m_event;
resetEventInfo();
while (true) {
if (m_decreaseDepth) {
m_decreaseDepth = false;
m_namespaces.decreaseDepth();
}
// Fake END_DOCUMENT event.
if (event == END_TAG && m_namespaces.getDepth() == 1
&& m_namespaces.getCurrentCount() == 0) {
m_event = END_DOCUMENT;
break;
}
int chunkType;
if (event == START_DOCUMENT) {
// Fake event, see CHUNK_XML_START_TAG handler.
chunkType = CHUNK_XML_START_TAG;
} else {
chunkType = m_reader.readInt();
}
if (chunkType == CHUNK_RESOURCEIDS) {
int chunkSize = m_reader.readInt();
if (chunkSize < 8 || (chunkSize % 4) != 0) {
throw new IOException("Invalid resource ids size ("
+ chunkSize + ").");
}
m_resourceIDs = m_reader.readIntArray(chunkSize / 4 - 2);
continue;
}
if (chunkType < CHUNK_XML_FIRST || chunkType > CHUNK_XML_LAST) {
throw new IOException("Invalid chunk type (" + chunkType + ").");
}
// Fake START_DOCUMENT event.
if (chunkType == CHUNK_XML_START_TAG && event == -1) {
m_event = START_DOCUMENT;
break;
}
// Common header.
/* chunkSize */m_reader.skipInt();
int lineNumber = m_reader.readInt();
/* 0xFFFFFFFF */m_reader.skipInt();
if (chunkType == CHUNK_XML_START_NAMESPACE
|| chunkType == CHUNK_XML_END_NAMESPACE) {
if (chunkType == CHUNK_XML_START_NAMESPACE) {
int prefix = m_reader.readInt();
int uri = m_reader.readInt();
m_namespaces.push(prefix, uri);
} else {
/* prefix */m_reader.skipInt();
/* uri */m_reader.skipInt();
m_namespaces.pop();
}
continue;
}
m_lineNumber = lineNumber;
if (chunkType == CHUNK_XML_START_TAG) {
m_namespaceUri = m_reader.readInt();
m_name = m_reader.readInt();
/* flags? */m_reader.skipInt();
int attributeCount = m_reader.readInt();
m_idAttribute = (attributeCount >>> 16) - 1;
attributeCount &= 0xFFFF;
m_classAttribute = m_reader.readInt();
m_styleAttribute = (m_classAttribute >>> 16) - 1;
m_classAttribute = (m_classAttribute & 0xFFFF) - 1;
m_attributes = m_reader.readIntArray(attributeCount
* ATTRIBUTE_LENGHT);
for (int i = ATTRIBUTE_IX_VALUE_TYPE; i < m_attributes.length;) {
m_attributes[i] = (m_attributes[i] >>> 24);
i += ATTRIBUTE_LENGHT;
}
m_namespaces.increaseDepth();
m_event = START_TAG;
break;
}
if (chunkType == CHUNK_XML_END_TAG) {
m_namespaceUri = m_reader.readInt();
m_name = m_reader.readInt();
m_event = END_TAG;
m_decreaseDepth = true;
break;
}
if (chunkType == CHUNK_XML_TEXT) {
m_name = m_reader.readInt();
/* ? */m_reader.skipInt();
/* ? */m_reader.skipInt();
m_event = TEXT;
break;
}
}
}
// ///////////////////////////////// data
/*
* All values are essentially indices, e.g. m_name is an index of name in
* m_strings.
*/
private IntReader m_reader;
private boolean m_operational = false;
private StringBlock m_strings;
private int[] m_resourceIDs;
private NamespaceStack m_namespaces = new NamespaceStack();
private boolean m_decreaseDepth;
private int m_event;
private int m_lineNumber;
private int m_name;
private int m_namespaceUri;
private int[] m_attributes;
private int m_idAttribute;
private int m_classAttribute;
private int m_styleAttribute;
private static final String E_NOT_SUPPORTED = "Method is not supported.";
private static final int ATTRIBUTE_IX_NAMESPACE_URI = 0,
ATTRIBUTE_IX_NAME = 1, ATTRIBUTE_IX_VALUE_STRING = 2,
ATTRIBUTE_IX_VALUE_TYPE = 3, ATTRIBUTE_IX_VALUE_DATA = 4,
ATTRIBUTE_LENGHT = 5;
private static final int CHUNK_AXML_FILE = 0x00080003,
CHUNK_RESOURCEIDS = 0x00080180, CHUNK_XML_FIRST = 0x00100100,
CHUNK_XML_START_NAMESPACE = 0x00100100,
CHUNK_XML_END_NAMESPACE = 0x00100101,
CHUNK_XML_START_TAG = 0x00100102, CHUNK_XML_END_TAG = 0x00100103,
CHUNK_XML_TEXT = 0x00100104, CHUNK_XML_LAST = 0x00100104;
}
final class IntReader {
public IntReader() {
}
public IntReader(InputStream stream, boolean bigEndian) {
reset(stream, bigEndian);
}
public final void reset(InputStream stream, boolean bigEndian) {
m_stream = stream;
m_bigEndian = bigEndian;
m_position = 0;
}
public final void close() {
if (m_stream == null) {
return;
}
try {
m_stream.close();
} catch (IOException e) {
}
reset(null, false);
}
public final InputStream getStream() {
return m_stream;
}
public final boolean isBigEndian() {
return m_bigEndian;
}
public final void setBigEndian(boolean bigEndian) {
m_bigEndian = bigEndian;
}
public final int readByte() throws IOException {
return readInt(1);
}
public final int readShort() throws IOException {
return readInt(2);
}
public final int readInt() throws IOException {
return readInt(4);
}
public final int readInt(int length) throws IOException {
if (length < 0 || length > 4) {
throw new IllegalArgumentException();
}
int result = 0;
if (m_bigEndian) {
for (int i = (length - 1) * 8; i >= 0; i -= 8) {
int b = m_stream.read();
if (b == -1) {
throw new EOFException();
}
m_position += 1;
result |= (b << i);
}
} else {
length *= 8;
for (int i = 0; i != length; i += 8) {
int b = m_stream.read();
if (b == -1) {
throw new EOFException();
}
m_position += 1;
result |= (b << i);
}
}
return result;
}
public final int[] readIntArray(int length) throws IOException {
int[] array = new int[length];
readIntArray(array, 0, length);
return array;
}
public final void readIntArray(int[] array, int offset, int length)
throws IOException {
for (; length > 0; length -= 1) {
array[offset++] = readInt();