node-apk
Version:
A library to parse Android application manifest and signature
69 lines (68 loc) • 2.88 kB
JavaScript
;
/*
* Copyright (c) 2019 XdevL. All rights reserved.
*
* This work is licensed under the terms of the MIT license.
* For a copy, see <https://opensource.org/licenses/MIT>.
*/
Object.defineProperty(exports, "__esModule", { value: true });
var common_1 = require("./common");
var XmlAttribute = /** @class */ (function () {
function XmlAttribute(source, stringPool) {
source.getCursorAndMove(4); // nameSpace
this.name = stringPool.values[source.readInt()];
source.getCursorAndMove(4); // rawValue
this.value = (0, common_1.parseResourceValue)(source, stringPool);
}
return XmlAttribute;
}());
var XmlElement = /** @class */ (function () {
function XmlElement(source, stringPool) {
this.attributes = {};
this.children = {};
if (stringPool) {
source.getCursorAndMove(4); // namespace
this.tag = stringPool.values[source.readInt()];
var attributeStart = source.readUShort();
var attributeSize = source.readUShort();
var attributeCount = source.readUShort();
source.moveAt(attributeStart);
for (var i = 0; i < attributeCount; ++i) {
var attr = new XmlAttribute(source.source(attributeSize), stringPool);
this.attributes[attr.name] = attr.value;
}
}
else {
var chunk = new common_1.Chunk(source, common_1.ChunkType.XML);
stringPool = new common_1.StringPool(new common_1.Chunk(chunk.chunkSource, common_1.ChunkType.STRING_POOL));
this.tag = "xml";
XmlElement.parseChildren(this, chunk, stringPool);
}
}
XmlElement.parseChildren = function (parent, root, stringPool) {
var maxOffset = root.chunkSize - root.headerSize;
while (root.chunkSource.getCursorAndMove(0) < maxOffset) {
var chunk = new common_1.Chunk(root.chunkSource);
switch (chunk.type) {
case common_1.ChunkType.XML_START_ELEMENT: {
var child = new XmlElement(chunk.chunkSource, stringPool);
parent.children[child.tag] = parent.children[child.tag] || [];
parent.children[child.tag].push(child);
XmlElement.parseChildren(child, root, stringPool);
break;
}
case common_1.ChunkType.XML_START_NAMESPACE:
XmlElement.parseChildren(parent, root, stringPool);
break;
case common_1.ChunkType.XML_END_ELEMENT:
case common_1.ChunkType.XML_END_NAMESPACE:
return;
}
}
};
XmlElement.prototype.toString = function () {
return JSON.stringify(this, null, 4);
};
return XmlElement;
}());
exports.default = XmlElement;