Skip to content

Commit e38fd9d

Browse files
committed
Merge pull request #48 from Leanplum/feature/fix-LP-4944
fix(ManifestParser) fix for parsing all nested namespaces
1 parent e8e9859 commit e38fd9d

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

AndroidSDK/src/com/leanplum/internal/LeanplumManifestParser.java

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class LeanplumManifestParser {
3535
// 3rd word: FFFFFFFF ??
3636
// 4th word: StringIndex of NameSpace name, or FFFFFFFF for default NS
3737
// 5th word: StringIndex of Element Name
38-
// (Note: 01011000 in 0th word means end of XML document, END_DOC_TAG).
38+
// (Note: 01011000 in 0th word means end of XML document, END_NAMESPACE_TAG).
3939

4040
// Start tags (not end tags) contain 3 more words:
4141
// 6th word: 14001400 meaning??
@@ -48,9 +48,11 @@ class LeanplumManifestParser {
4848
// 2nd word: StringIndex of Attribute Value, or FFFFFFF if ResourceId used
4949
// 3rd word: Flags?
5050
// 4th word: str ind of attr value again, or ResourceId of value.
51-
// END_DOC_TAG = 0x00100101;
51+
5252
private static final int START_TAG = 0x00100102;
5353
private static final int END_TAG = 0x00100103;
54+
private static final int START_NAMESPACE_TAG = 0x00100100;
55+
private static final int END_NAMESPACE_TAG = 0x00100101;
5456
private static final String SPACES = " ";
5557

5658
/**
@@ -79,15 +81,16 @@ static String decompressXml(byte[] xml) {
7981
// Step through the XML tree element tags and attributes.
8082
int off = scanForFirstStartTag(xml);
8183
int indent = 0;
82-
84+
// Counter of nested START_NAMESPACE_TAG. By default here will be one START_NAMESPACE_TAG.
85+
int startNamestaceTagCounter = 1;
8386
while (off < xml.length) {
8487
int tag0 = littleEndianValue(xml, off);
8588
int nameSi = littleEndianValue(xml, off + 5 * 4);
86-
if (tag0 == START_TAG) {
89+
if (tag0 == START_TAG) { // START_TAG.
8790
int numbAttrs = littleEndianValue(xml, off + 7 * 4); // Number of Attributes to follow.
88-
off += 9 * 4; // Skip over 6+3 words of START_TAG data
91+
off += 9 * 4; // Skip over 6+3 words of START_TAG data.
8992
String name = compXmlString(xml, sitOff, stOff, nameSi);
90-
// Look for the Attributes
93+
// Look for the Attributes.
9194
StringBuilder sb = new StringBuilder();
9295
for (int ii = 0; ii < numbAttrs; ii++) {
9396
int attrNameSi = littleEndianValue(xml, off + 4); // AttrName String Index.
@@ -103,12 +106,27 @@ static String decompressXml(byte[] xml) {
103106
}
104107
out += SPACES.substring(0, Math.min(indent * 2, SPACES.length())) + "<" + name + sb + ">";
105108
indent++;
106-
} else if (tag0 == END_TAG) {
109+
} else if (tag0 == END_TAG) { // END_TAG.
107110
indent--;
108111
off += 6 * 4; // Skip over 6 words of END_TAG data
109112
String name = compXmlString(xml, sitOff, stOff, nameSi);
110113
out += SPACES.substring(0, Math.min(indent * 2, SPACES.length())) + "</" + name + ">";
111-
114+
} else if (tag0 == START_NAMESPACE_TAG) { // START_NAMESPACE_TAG
115+
// Sometimes here can be nested group of START_NAMESPACE_TAG and END_NAMESPACE_TAG. We
116+
// should parse all of them.
117+
// Increase START_NAMESPACE_TAG counter.
118+
startNamestaceTagCounter++;
119+
off += 4 * 6; // Skip over 6 words of START_NAMESPACE_TAG.
120+
} else if (tag0 == END_NAMESPACE_TAG) { // END_NAMESPACE_TAG.
121+
// When we found END_NAMESPACE_TAG we should decrease START_NAMESPACE_TAG counter.
122+
startNamestaceTagCounter--;
123+
// Checks if we found END_NAMESPACE_TAG for all nested START_NAMESPACE_TAG.
124+
if (startNamestaceTagCounter == 0) {
125+
// Finish parsing of xml.
126+
break;
127+
}
128+
// If here is more START_NAMESPACE_TAG then skip over 6 words of START_NAMESPACE_TAG.
129+
off += 4 * 6;
112130
} else {
113131
break;
114132
}

0 commit comments

Comments
 (0)