| // |
| // Copyright 2006 The Android Open Source Project |
| // |
| // Build resource files from raw assets. |
| // |
| |
| #include "ResourceTable.h" |
| |
| #include "AaptUtil.h" |
| #include "XMLNode.h" |
| #include "ResourceFilter.h" |
| #include "ResourceIdCache.h" |
| #include "SdkConstants.h" |
| |
| #include <algorithm> |
| #include <androidfw/ResourceTypes.h> |
| #include <utils/ByteOrder.h> |
| #include <utils/TypeHelpers.h> |
| #include <stdarg.h> |
| |
| // SSIZE: mingw does not have signed size_t == ssize_t. |
| // STATUST: mingw does seem to redefine UNKNOWN_ERROR from our enum value, so a cast is necessary. |
| #if !defined(_WIN32) |
| # define SSIZE(x) x |
| # define STATUST(x) x |
| #else |
| # define SSIZE(x) (signed size_t)x |
| # define STATUST(x) (status_t)x |
| #endif |
| |
| // Set to true for noisy debug output. |
| static const bool kIsDebug = false; |
| |
| #if PRINT_STRING_METRICS |
| static const bool kPrintStringMetrics = true; |
| #else |
| static const bool kPrintStringMetrics = false; |
| #endif |
| |
| static const char* kAttrPrivateType = "^attr-private"; |
| |
| status_t compileXmlFile(const Bundle* bundle, |
| const sp<AaptAssets>& assets, |
| const String16& resourceName, |
| const sp<AaptFile>& target, |
| ResourceTable* table, |
| int options) |
| { |
| sp<XMLNode> root = XMLNode::parse(target); |
| if (root == NULL) { |
| return UNKNOWN_ERROR; |
| } |
| |
| return compileXmlFile(bundle, assets, resourceName, root, target, table, options); |
| } |
| |
| status_t compileXmlFile(const Bundle* bundle, |
| const sp<AaptAssets>& assets, |
| const String16& resourceName, |
| const sp<AaptFile>& target, |
| const sp<AaptFile>& outTarget, |
| ResourceTable* table, |
| int options) |
| { |
| sp<XMLNode> root = XMLNode::parse(target); |
| if (root == NULL) { |
| return UNKNOWN_ERROR; |
| } |
| |
| return compileXmlFile(bundle, assets, resourceName, root, outTarget, table, options); |
| } |
| |
| status_t compileXmlFile(const Bundle* bundle, |
| const sp<AaptAssets>& assets, |
| const String16& resourceName, |
| const sp<XMLNode>& root, |
| const sp<AaptFile>& target, |
| ResourceTable* table, |
| int options) |
| { |
| if ((options&XML_COMPILE_STRIP_WHITESPACE) != 0) { |
| root->removeWhitespace(true, NULL); |
| } else if ((options&XML_COMPILE_COMPACT_WHITESPACE) != 0) { |
| root->removeWhitespace(false, NULL); |
| } |
| |
| if ((options&XML_COMPILE_UTF8) != 0) { |
| root->setUTF8(true); |
| } |
| |
| bool hasErrors = false; |
| |
| if ((options&XML_COMPILE_ASSIGN_ATTRIBUTE_IDS) != 0) { |
| status_t err = root->assignResourceIds(assets, table); |
| if (err != NO_ERROR) { |
| hasErrors = true; |
| } |
| } |
| |
| status_t err = root->parseValues(assets, table); |
| if (err != NO_ERROR) { |
| hasErrors = true; |
| } |
| |
| if (hasErrors) { |
| return UNKNOWN_ERROR; |
| } |
| |
| if (table->modifyForCompat(bundle, resourceName, target, root) != NO_ERROR) { |
| return UNKNOWN_ERROR; |
| } |
| |
| if (kIsDebug) { |
| printf("Input XML Resource:\n"); |
| root->print(); |
| } |
| err = root->flatten(target, |
| (options&XML_COMPILE_STRIP_COMMENTS) != 0, |
| (options&XML_COMPILE_STRIP_RAW_VALUES) != 0); |
| if (err != NO_ERROR) { |
| return err; |
| } |
| |
| if (kIsDebug) { |
| printf("Output XML Resource:\n"); |
| ResXMLTree tree; |
| tree.setTo(target->getData(), target->getSize()); |
| printXMLBlock(&tree); |
| } |
| |
| target->setCompressionMethod(ZipEntry::kCompressDeflated); |
| |
| return err; |
| } |
| |
| struct flag_entry |
| { |
| const char16_t* name; |
| size_t nameLen; |
| uint32_t value; |
| const char* description; |
| }; |
| |
| static const char16_t referenceArray[] = |
| { 'r', 'e', 'f', 'e', 'r', 'e', 'n', 'c', 'e' }; |
| static const char16_t stringArray[] = |
| { 's', 't', 'r', 'i', 'n', 'g' }; |
| static const char16_t integerArray[] = |
| { 'i', 'n', 't', 'e', 'g', 'e', 'r' }; |
| static const char16_t booleanArray[] = |
| { 'b', 'o', 'o', 'l', 'e', 'a', 'n' }; |
| static const char16_t colorArray[] = |
| { 'c', 'o', 'l', 'o', 'r' }; |
| static const char16_t floatArray[] = |
| { 'f', 'l', 'o', 'a', 't' }; |
| static const char16_t dimensionArray[] = |
| { 'd', 'i', 'm', 'e', 'n', 's', 'i', 'o', 'n' }; |
| static const char16_t fractionArray[] = |
| { 'f', 'r', 'a', 'c', 't', 'i', 'o', 'n' }; |
| static const char16_t enumArray[] = |
| { 'e', 'n', 'u', 'm' }; |
| static const char16_t flagsArray[] = |
| { 'f', 'l', 'a', 'g', 's' }; |
| |
| static const flag_entry gFormatFlags[] = { |
| { referenceArray, sizeof(referenceArray)/2, ResTable_map::TYPE_REFERENCE, |
| "a reference to another resource, in the form \"<code>@[+][<i>package</i>:]<i>type</i>:<i>name</i></code>\"\n" |
| "or to a theme attribute in the form \"<code>?[<i>package</i>:][<i>type</i>:]<i>name</i></code>\"."}, |
| { stringArray, sizeof(stringArray)/2, ResTable_map::TYPE_STRING, |
| "a string value, using '\\\\;' to escape characters such as '\\\\n' or '\\\\uxxxx' for a unicode character." }, |
| { integerArray, sizeof(integerArray)/2, ResTable_map::TYPE_INTEGER, |
| "an integer value, such as \"<code>100</code>\"." }, |
| { booleanArray, sizeof(booleanArray)/2, ResTable_map::TYPE_BOOLEAN, |
| "a boolean value, either \"<code>true</code>\" or \"<code>false</code>\"." }, |
| { colorArray, sizeof(colorArray)/2, ResTable_map::TYPE_COLOR, |
| "a color value, in the form of \"<code>#<i>rgb</i></code>\", \"<code>#<i>argb</i></code>\",\n" |
| "\"<code>#<i>rrggbb</i></code>\", or \"<code>#<i>aarrggbb</i></code>\"." }, |
| { floatArray, sizeof(floatArray)/2, ResTable_map::TYPE_FLOAT, |
| "a floating point value, such as \"<code>1.2</code>\"."}, |
| { dimensionArray, sizeof(dimensionArray)/2, ResTable_map::TYPE_DIMENSION, |
| "a dimension value, which is a floating point number appended with a unit such as \"<code>14.5sp</code>\".\n" |
| "Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size),\n" |
| "in (inches), mm (millimeters)." }, |
| { fractionArray, sizeof(fractionArray)/2, ResTable_map::TYPE_FRACTION, |
| "a fractional value, which is a floating point number appended with either % or %p, such as \"<code>14.5%</code>\".\n" |
| "The % suffix always means a percentage of the base size; the optional %p suffix provides a size relative to\n" |
| "some parent container." }, |
| { enumArray, sizeof(enumArray)/2, ResTable_map::TYPE_ENUM, NULL }, |
| { flagsArray, sizeof(flagsArray)/2, ResTable_map::TYPE_FLAGS, NULL }, |
| { NULL, 0, 0, NULL } |
| }; |
| |
| static const char16_t suggestedArray[] = { 's', 'u', 'g', 'g', 'e', 's', 't', 'e', 'd' }; |
| |
| static const flag_entry l10nRequiredFlags[] = { |
| { suggestedArray, sizeof(suggestedArray)/2, ResTable_map::L10N_SUGGESTED, NULL }, |
| { NULL, 0, 0, NULL } |
| }; |
| |
| static const char16_t nulStr[] = { 0 }; |
| |
| static uint32_t parse_flags(const char16_t* str, size_t len, |
| const flag_entry* flags, bool* outError = NULL) |
| { |
| while (len > 0 && isspace(*str)) { |
| str++; |
| len--; |
| } |
| while (len > 0 && isspace(str[len-1])) { |
| len--; |
| } |
| |
| const char16_t* const end = str + len; |
| uint32_t value = 0; |
| |
| while (str < end) { |
| const char16_t* div = str; |
| while (div < end && *div != '|') { |
| div++; |
| } |
| |
| const flag_entry* cur = flags; |
| while (cur->name) { |
| if (strzcmp16(cur->name, cur->nameLen, str, div-str) == 0) { |
| value |= cur->value; |
| break; |
| } |
| cur++; |
| } |
| |
| if (!cur->name) { |
| if (outError) *outError = true; |
| return 0; |
| } |
| |
| str = div < end ? div+1 : div; |
| } |
| |
| if (outError) *outError = false; |
| return value; |
| } |
| |
| static String16 mayOrMust(int type, int flags) |
| { |
| if ((type&(~flags)) == 0) { |
| return String16("<p>Must"); |
| } |
| |
| return String16("<p>May"); |
| } |
| |
| static void appendTypeInfo(ResourceTable* outTable, const String16& pkg, |
| const String16& typeName, const String16& ident, int type, |
| const flag_entry* flags) |
| { |
| bool hadType = false; |
| while (flags->name) { |
| if ((type&flags->value) != 0 && flags->description != NULL) { |
| String16 fullMsg(mayOrMust(type, flags->value)); |
| fullMsg.append(String16(" be ")); |
| fullMsg.append(String16(flags->description)); |
| outTable->appendTypeComment(pkg, typeName, ident, fullMsg); |
| hadType = true; |
| } |
| flags++; |
| } |
| if (hadType && (type&ResTable_map::TYPE_REFERENCE) == 0) { |
| outTable->appendTypeComment(pkg, typeName, ident, |
| String16("<p>This may also be a reference to a resource (in the form\n" |
| "\"<code>@[<i>package</i>:]<i>type</i>:<i>name</i></code>\") or\n" |
| "theme attribute (in the form\n" |
| "\"<code>?[<i>package</i>:][<i>type</i>:]<i>name</i></code>\")\n" |
| "containing a value of this type.")); |
| } |
| } |
| |
| struct PendingAttribute |
| { |
| const String16 myPackage; |
| const SourcePos sourcePos; |
| const bool appendComment; |
| int32_t type; |
| String16 ident; |
| String16 comment; |
| bool hasErrors; |
| bool added; |
| |
| PendingAttribute(String16 _package, const sp<AaptFile>& in, |
| ResXMLTree& block, bool _appendComment) |
| : myPackage(_package) |
| , sourcePos(in->getPrintableSource(), block.getLineNumber()) |
| , appendComment(_appendComment) |
| , type(ResTable_map::TYPE_ANY) |
| , hasErrors(false) |
| , added(false) |
| { |
| } |
| |
| status_t createIfNeeded(ResourceTable* outTable) |
| { |
| if (added || hasErrors) { |
| return NO_ERROR; |
| } |
| added = true; |
| |
| String16 attr16("attr"); |
| |
| if (outTable->hasBagOrEntry(myPackage, attr16, ident)) { |
| sourcePos.error("Attribute \"%s\" has already been defined\n", |
| String8(ident).string()); |
| hasErrors = true; |
| return UNKNOWN_ERROR; |
| } |
| |
| char numberStr[16]; |
| sprintf(numberStr, "%d", type); |
| status_t err = outTable->addBag(sourcePos, myPackage, |
| attr16, ident, String16(""), |
| String16("^type"), |
| String16(numberStr), NULL, NULL); |
| if (err != NO_ERROR) { |
| hasErrors = true; |
| return err; |
| } |
| outTable->appendComment(myPackage, attr16, ident, comment, appendComment); |
| //printf("Attribute %s comment: %s\n", String8(ident).string(), |
| // String8(comment).string()); |
| return err; |
| } |
| }; |
| |
| static status_t compileAttribute(const sp<AaptFile>& in, |
| ResXMLTree& block, |
| const String16& myPackage, |
| ResourceTable* outTable, |
| String16* outIdent = NULL, |
| bool inStyleable = false) |
| { |
| PendingAttribute attr(myPackage, in, block, inStyleable); |
| |
| const String16 attr16("attr"); |
| const String16 id16("id"); |
| |
| // Attribute type constants. |
| const String16 enum16("enum"); |
| const String16 flag16("flag"); |
| |
| ResXMLTree::event_code_t code; |
| size_t len; |
| status_t err; |
| |
| ssize_t identIdx = block.indexOfAttribute(NULL, "name"); |
| if (identIdx >= 0) { |
| attr.ident = String16(block.getAttributeStringValue(identIdx, &len)); |
| if (outIdent) { |
| *outIdent = attr.ident; |
| } |
| } else { |
| attr.sourcePos.error("A 'name' attribute is required for <attr>\n"); |
| attr.hasErrors = true; |
| } |
| |
| attr.comment = String16( |
| block.getComment(&len) ? block.getComment(&len) : nulStr); |
| |
| ssize_t typeIdx = block.indexOfAttribute(NULL, "format"); |
| if (typeIdx >= 0) { |
| String16 typeStr = String16(block.getAttributeStringValue(typeIdx, &len)); |
| attr.type = parse_flags(typeStr.string(), typeStr.size(), gFormatFlags); |
| if (attr.type == 0) { |
| attr.sourcePos.error("Tag <attr> 'format' attribute value \"%s\" not valid\n", |
| String8(typeStr).string()); |
| attr.hasErrors = true; |
| } |
| attr.createIfNeeded(outTable); |
| } else if (!inStyleable) { |
| // Attribute definitions outside of styleables always define the |
| // attribute as a generic value. |
| attr.createIfNeeded(outTable); |
| } |
| |
| //printf("Attribute %s: type=0x%08x\n", String8(attr.ident).string(), attr.type); |
| |
| ssize_t minIdx = block.indexOfAttribute(NULL, "min"); |
| if (minIdx >= 0) { |
| String16 val = String16(block.getAttributeStringValue(minIdx, &len)); |
| if (!ResTable::stringToInt(val.string(), val.size(), NULL)) { |
| attr.sourcePos.error("Tag <attr> 'min' attribute must be a number, not \"%s\"\n", |
| String8(val).string()); |
| attr.hasErrors = true; |
| } |
| attr.createIfNeeded(outTable); |
| if (!attr.hasErrors) { |
| err = outTable->addBag(attr.sourcePos, myPackage, attr16, attr.ident, |
| String16(""), String16("^min"), String16(val), NULL, NULL); |
| if (err != NO_ERROR) { |
| attr.hasErrors = true; |
| } |
| } |
| } |
| |
| ssize_t maxIdx = block.indexOfAttribute(NULL, "max"); |
| if (maxIdx >= 0) { |
| String16 val = String16(block.getAttributeStringValue(maxIdx, &len)); |
| if (!ResTable::stringToInt(val.string(), val.size(), NULL)) { |
| attr.sourcePos.error("Tag <attr> 'max' attribute must be a number, not \"%s\"\n", |
| String8(val).string()); |
| attr.hasErrors = true; |
| } |
| attr.createIfNeeded(outTable); |
| if (!attr.hasErrors) { |
| err = outTable->addBag(attr.sourcePos, myPackage, attr16, attr.ident, |
| String16(""), String16("^max"), String16(val), NULL, NULL); |
| attr.hasErrors = true; |
| } |
| } |
| |
| if ((minIdx >= 0 || maxIdx >= 0) && (attr.type&ResTable_map::TYPE_INTEGER) == 0) { |
| attr.sourcePos.error("Tag <attr> must have format=integer attribute if using max or min\n"); |
| attr.hasErrors = true; |
| } |
| |
| ssize_t l10nIdx = block.indexOfAttribute(NULL, "localization"); |
| if (l10nIdx >= 0) { |
| const char16_t* str = block.getAttributeStringValue(l10nIdx, &len); |
| bool error; |
| uint32_t l10n_required = parse_flags(str, len, l10nRequiredFlags, &error); |
| if (error) { |
| attr.sourcePos.error("Tag <attr> 'localization' attribute value \"%s\" not valid\n", |
| String8(str).string()); |
| attr.hasErrors = true; |
| } |
| attr.createIfNeeded(outTable); |
| if (!attr.hasErrors) { |
| char buf[11]; |
| sprintf(buf, "%d", l10n_required); |
| err = outTable->addBag(attr.sourcePos, myPackage, attr16, attr.ident, |
| String16(""), String16("^l10n"), String16(buf), NULL, NULL); |
| if (err != NO_ERROR) { |
| attr.hasErrors = true; |
| } |
| } |
| } |
| |
| String16 enumOrFlagsComment; |
| |
| while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) { |
| if (code == ResXMLTree::START_TAG) { |
| uint32_t localType = 0; |
| if (strcmp16(block.getElementName(&len), enum16.string()) == 0) { |
| localType = ResTable_map::TYPE_ENUM; |
| } else if (strcmp16(block.getElementName(&len), flag16.string()) == 0) { |
| localType = ResTable_map::TYPE_FLAGS; |
| } else { |
| SourcePos(in->getPrintableSource(), block.getLineNumber()) |
| .error("Tag <%s> can not appear inside <attr>, only <enum> or <flag>\n", |
| String8(block.getElementName(&len)).string()); |
| return UNKNOWN_ERROR; |
| } |
| |
| attr.createIfNeeded(outTable); |
| |
| if (attr.type == ResTable_map::TYPE_ANY) { |
| // No type was explicitly stated, so supplying enum tags |
| // implicitly creates an enum or flag. |
| attr.type = 0; |
| } |
| |
| if ((attr.type&(ResTable_map::TYPE_ENUM|ResTable_map::TYPE_FLAGS)) == 0) { |
| // Wasn't originally specified as an enum, so update its type. |
| attr.type |= localType; |
| if (!attr.hasErrors) { |
| char numberStr[16]; |
| sprintf(numberStr, "%d", attr.type); |
| err = outTable->addBag(SourcePos(in->getPrintableSource(), block.getLineNumber()), |
| myPackage, attr16, attr.ident, String16(""), |
| String16("^type"), String16(numberStr), NULL, NULL, true); |
| if (err != NO_ERROR) { |
| attr.hasErrors = true; |
| } |
| } |
| } else if ((uint32_t)(attr.type&(ResTable_map::TYPE_ENUM|ResTable_map::TYPE_FLAGS)) != localType) { |
| if (localType == ResTable_map::TYPE_ENUM) { |
| SourcePos(in->getPrintableSource(), block.getLineNumber()) |
| .error("<enum> attribute can not be used inside a flags format\n"); |
| attr.hasErrors = true; |
| } else { |
| SourcePos(in->getPrintableSource(), block.getLineNumber()) |
| .error("<flag> attribute can not be used inside a enum format\n"); |
| attr.hasErrors = true; |
| } |
| } |
| |
| String16 itemIdent; |
| ssize_t itemIdentIdx = block.indexOfAttribute(NULL, "name"); |
| if (itemIdentIdx >= 0) { |
| itemIdent = String16(block.getAttributeStringValue(itemIdentIdx, &len)); |
| } else { |
| SourcePos(in->getPrintableSource(), block.getLineNumber()) |
| .error("A 'name' attribute is required for <enum> or <flag>\n"); |
| attr.hasErrors = true; |
| } |
| |
| String16 value; |
| ssize_t valueIdx = block.indexOfAttribute(NULL, "value"); |
| if (valueIdx >= 0) { |
| value = String16(block.getAttributeStringValue(valueIdx, &len)); |
| } else { |
| SourcePos(in->getPrintableSource(), block.getLineNumber()) |
| .error("A 'value' attribute is required for <enum> or <flag>\n"); |
| attr.hasErrors = true; |
| } |
| if (!attr.hasErrors && !ResTable::stringToInt(value.string(), value.size(), NULL)) { |
| SourcePos(in->getPrintableSource(), block.getLineNumber()) |
| .error("Tag <enum> or <flag> 'value' attribute must be a number," |
| " not \"%s\"\n", |
| String8(value).string()); |
| attr.hasErrors = true; |
| } |
| |
| if (!attr.hasErrors) { |
| if (enumOrFlagsComment.size() == 0) { |
| enumOrFlagsComment.append(mayOrMust(attr.type, |
| ResTable_map::TYPE_ENUM|ResTable_map::TYPE_FLAGS)); |
| enumOrFlagsComment.append((attr.type&ResTable_map::TYPE_ENUM) |
| ? String16(" be one of the following constant values.") |
| : String16(" be one or more (separated by '|') of the following constant values.")); |
| enumOrFlagsComment.append(String16("</p>\n<table>\n" |
| "<colgroup align=\"left\" />\n" |
| "<colgroup align=\"left\" />\n" |
| "<colgroup align=\"left\" />\n" |
| "<tr><th>Constant</th><th>Value</th><th>Description</th></tr>")); |
| } |
| |
| enumOrFlagsComment.append(String16("\n<tr><td><code>")); |
| enumOrFlagsComment.append(itemIdent); |
| enumOrFlagsComment.append(String16("</code></td><td>")); |
| enumOrFlagsComment.append(value); |
| enumOrFlagsComment.append(String16("</td><td>")); |
| if (block.getComment(&len)) { |
| enumOrFlagsComment.append(String16(block.getComment(&len))); |
| } |
| enumOrFlagsComment.append(String16("</td></tr>")); |
| |
| err = outTable->addBag(SourcePos(in->getPrintableSource(), block.getLineNumber()), |
| myPackage, |
| attr16, attr.ident, String16(""), |
| itemIdent, value, NULL, NULL, false, true); |
| if (err != NO_ERROR) { |
| attr.hasErrors = true; |
| } |
| } |
| } else if (code == ResXMLTree::END_TAG) { |
| if (strcmp16(block.getElementName(&len), attr16.string()) == 0) { |
| break; |
| } |
| if ((attr.type&ResTable_map::TYPE_ENUM) != 0) { |
| if (strcmp16(block.getElementName(&len), enum16.string()) != 0) { |
| SourcePos(in->getPrintableSource(), block.getLineNumber()) |
| .error("Found tag </%s> where </enum> is expected\n", |
| String8(block.getElementName(&len)).string()); |
| return UNKNOWN_ERROR; |
| } |
| } else { |
| if (strcmp16(block.getElementName(&len), flag16.string()) != 0) { |
| SourcePos(in->getPrintableSource(), block.getLineNumber()) |
| .error("Found tag </%s> where </flag> is expected\n", |
| String8(block.getElementName(&len)).string()); |
| return UNKNOWN_ERROR; |
| } |
| } |
| } |
| } |
| |
| if (!attr.hasErrors && attr.added) { |
| appendTypeInfo(outTable, myPackage, attr16, attr.ident, attr.type, gFormatFlags); |
| } |
| |
| if (!attr.hasErrors && enumOrFlagsComment.size() > 0) { |
| enumOrFlagsComment.append(String16("\n</table>")); |
| outTable->appendTypeComment(myPackage, attr16, attr.ident, enumOrFlagsComment); |
| } |
| |
| |
| return NO_ERROR; |
| } |
| |
| bool localeIsDefined(const ResTable_config& config) |
| { |
| return config.locale == 0; |
| } |
| |
| status_t parseAndAddBag(Bundle* bundle, |
| const sp<AaptFile>& in, |
| ResXMLTree* block, |
| const ResTable_config& config, |
| const String16& myPackage, |
| const String16& curType, |
| const String16& ident, |
| const String16& parentIdent, |
| const String16& itemIdent, |
| int32_t curFormat, |
| bool isFormatted, |
| const String16& /* product */, |
| PseudolocalizationMethod pseudolocalize, |
| const bool overwrite, |
| ResourceTable* outTable) |
| { |
| status_t err; |
| const String16 item16("item"); |
| |
| String16 str; |
| Vector<StringPool::entry_style_span> spans; |
| err = parseStyledString(bundle, in->getPrintableSource().string(), |
| block, item16, &str, &spans, isFormatted, |
| pseudolocalize); |
| if (err != NO_ERROR) { |
| return err; |
| } |
| |
| if (kIsDebug) { |
| printf("Adding resource bag entry l=%c%c c=%c%c orien=%d d=%d " |
| " pid=%s, bag=%s, id=%s: %s\n", |
| config.language[0], config.language[1], |
| config.country[0], config.country[1], |
| config.orientation, config.density, |
| String8(parentIdent).string(), |
| String8(ident).string(), |
| String8(itemIdent).string(), |
| String8(str).string()); |
| } |
| |
| err = outTable->addBag(SourcePos(in->getPrintableSource(), block->getLineNumber()), |
| myPackage, curType, ident, parentIdent, itemIdent, str, |
| &spans, &config, overwrite, false, curFormat); |
| return err; |
| } |
| |
| /* |
| * Returns true if needle is one of the elements in the comma-separated list |
| * haystack, false otherwise. |
| */ |
| bool isInProductList(const String16& needle, const String16& haystack) { |
| const char16_t *needle2 = needle.string(); |
| const char16_t *haystack2 = haystack.string(); |
| size_t needlesize = needle.size(); |
| |
| while (*haystack2 != '\0') { |
| if (strncmp16(haystack2, needle2, needlesize) == 0) { |
| if (haystack2[needlesize] == '\0' || haystack2[needlesize] == ',') { |
| return true; |
| } |
| } |
| |
| while (*haystack2 != '\0' && *haystack2 != ',') { |
| haystack2++; |
| } |
| if (*haystack2 == ',') { |
| haystack2++; |
| } |
| } |
| |
| return false; |
| } |
| |
| /* |
| * A simple container that holds a resource type and name. It is ordered first by type then |
| * by name. |
| */ |
| struct type_ident_pair_t { |
| String16 type; |
| String16 ident; |
| |
| type_ident_pair_t() { }; |
| type_ident_pair_t(const String16& t, const String16& i) : type(t), ident(i) { } |
| type_ident_pair_t(const type_ident_pair_t& o) : type(o.type), ident(o.ident) { } |
| inline bool operator < (const type_ident_pair_t& o) const { |
| int cmp = compare_type(type, o.type); |
| if (cmp < 0) { |
| return true; |
| } else if (cmp > 0) { |
| return false; |
| } else { |
| return strictly_order_type(ident, o.ident); |
| } |
| } |
| }; |
| |
| |
| status_t parseAndAddEntry(Bundle* bundle, |
| const sp<AaptFile>& in, |
| ResXMLTree* block, |
| const ResTable_config& config, |
| const String16& myPackage, |
| const String16& curType, |
| const String16& ident, |
| const String16& curTag, |
| bool curIsStyled, |
| int32_t curFormat, |
| bool isFormatted, |
| const String16& product, |
| PseudolocalizationMethod pseudolocalize, |
| const bool overwrite, |
| KeyedVector<type_ident_pair_t, bool>* skippedResourceNames, |
| ResourceTable* outTable) |
| { |
| status_t err; |
| |
| String16 str; |
| Vector<StringPool::entry_style_span> spans; |
| err = parseStyledString(bundle, in->getPrintableSource().string(), block, |
| curTag, &str, curIsStyled ? &spans : NULL, |
| isFormatted, pseudolocalize); |
| |
| if (err < NO_ERROR) { |
| return err; |
| } |
| |
| /* |
| * If a product type was specified on the command line |
| * and also in the string, and the two are not the same, |
| * return without adding the string. |
| */ |
| |
| const char *bundleProduct = bundle->getProduct(); |
| if (bundleProduct == NULL) { |
| bundleProduct = ""; |
| } |
| |
| if (product.size() != 0) { |
| /* |
| * If the command-line-specified product is empty, only "default" |
| * matches. Other variants are skipped. This is so generation |
| * of the R.java file when the product is not known is predictable. |
| */ |
| |
| if (bundleProduct[0] == '\0') { |
| if (strcmp16(String16("default").string(), product.string()) != 0) { |
| /* |
| * This string has a product other than 'default'. Do not add it, |
| * but record it so that if we do not see the same string with |
| * product 'default' or no product, then report an error. |
| */ |
| skippedResourceNames->replaceValueFor( |
| type_ident_pair_t(curType, ident), true); |
| return NO_ERROR; |
| } |
| } else { |
| /* |
| * The command-line product is not empty. |
| * If the product for this string is on the command-line list, |
| * it matches. "default" also matches, but only if nothing |
| * else has matched already. |
| */ |
| |
| if (isInProductList(product, String16(bundleProduct))) { |
| ; |
| } else if (strcmp16(String16("default").string(), product.string()) == 0 && |
| !outTable->hasBagOrEntry(myPackage, curType, ident, config)) { |
| ; |
| } else { |
| return NO_ERROR; |
| } |
| } |
| } |
| |
| if (kIsDebug) { |
| printf("Adding resource entry l=%c%c c=%c%c orien=%d d=%d id=%s: %s\n", |
| config.language[0], config.language[1], |
| config.country[0], config.country[1], |
| config.orientation, config.density, |
| String8(ident).string(), String8(str).string()); |
| } |
| |
| err = outTable->addEntry(SourcePos(in->getPrintableSource(), block->getLineNumber()), |
| myPackage, curType, ident, str, &spans, &config, |
| false, curFormat, overwrite); |
| |
| return err; |
| } |
| |
| status_t compileResourceFile(Bundle* bundle, |
| const sp<AaptAssets>& assets, |
| const sp<AaptFile>& in, |
| const ResTable_config& defParams, |
| const bool overwrite, |
| ResourceTable* outTable) |
| { |
| ResXMLTree block; |
| status_t err = parseXMLResource(in, &block, false, true); |
| if (err != NO_ERROR) { |
| return err; |
| } |
| |
| // Top-level tag. |
| const String16 resources16("resources"); |
| |
| // Identifier declaration tags. |
| const String16 declare_styleable16("declare-styleable"); |
| const String16 attr16("attr"); |
| |
| // Data creation organizational tags. |
| const String16 string16("string"); |
| const String16 drawable16("drawable"); |
| const String16 color16("color"); |
| const String16 bool16("bool"); |
| const String16 integer16("integer"); |
| const String16 dimen16("dimen"); |
| const String16 fraction16("fraction"); |
| const String16 style16("style"); |
| const String16 plurals16("plurals"); |
| const String16 array16("array"); |
| const String16 string_array16("string-array"); |
| const String16 integer_array16("integer-array"); |
| const String16 public16("public"); |
| const String16 public_padding16("public-padding"); |
| const String16 private_symbols16("private-symbols"); |
| const String16 java_symbol16("java-symbol"); |
| const String16 add_resource16("add-resource"); |
| const String16 skip16("skip"); |
| const String16 eat_comment16("eat-comment"); |
| |
| // Data creation tags. |
| const String16 bag16("bag"); |
| const String16 item16("item"); |
| |
| // Attribute type constants. |
| const String16 enum16("enum"); |
| |
| // plural values |
| const String16 other16("other"); |
| const String16 quantityOther16("^other"); |
| const String16 zero16("zero"); |
| const String16 quantityZero16("^zero"); |
| const String16 one16("one"); |
| const String16 quantityOne16("^one"); |
| const String16 two16("two"); |
| const String16 quantityTwo16("^two"); |
| const String16 few16("few"); |
| const String16 quantityFew16("^few"); |
| const String16 many16("many"); |
| const String16 quantityMany16("^many"); |
| |
| // useful attribute names and special values |
| const String16 name16("name"); |
| const String16 translatable16("translatable"); |
| const String16 formatted16("formatted"); |
| const String16 false16("false"); |
| |
| const String16 myPackage(assets->getPackage()); |
| |
| bool hasErrors = false; |
| |
| bool fileIsTranslatable = true; |
| if (strstr(in->getPrintableSource().string(), "donottranslate") != NULL) { |
| fileIsTranslatable = false; |
| } |
| |
| DefaultKeyedVector<String16, uint32_t> nextPublicId(0); |
| |
| // Stores the resource names that were skipped. Typically this happens when |
| // AAPT is invoked without a product specified and a resource has no |
| // 'default' product attribute. |
| KeyedVector<type_ident_pair_t, bool> skippedResourceNames; |
| |
| ResXMLTree::event_code_t code; |
| do { |
| code = block.next(); |
| } while (code == ResXMLTree::START_NAMESPACE); |
| |
| size_t len; |
| if (code != ResXMLTree::START_TAG) { |
| SourcePos(in->getPrintableSource(), block.getLineNumber()).error( |
| "No start tag found\n"); |
| return UNKNOWN_ERROR; |
| } |
| if (strcmp16(block.getElementName(&len), resources16.string()) != 0) { |
| SourcePos(in->getPrintableSource(), block.getLineNumber()).error( |
| "Invalid start tag %s\n", String8(block.getElementName(&len)).string()); |
| return UNKNOWN_ERROR; |
| } |
| |
| ResTable_config curParams(defParams); |
| |
| ResTable_config pseudoParams(curParams); |
| pseudoParams.language[0] = 'e'; |
| pseudoParams.language[1] = 'n'; |
| pseudoParams.country[0] = 'X'; |
| pseudoParams.country[1] = 'A'; |
| |
| ResTable_config pseudoBidiParams(curParams); |
| pseudoBidiParams.language[0] = 'a'; |
| pseudoBidiParams.language[1] = 'r'; |
| pseudoBidiParams.country[0] = 'X'; |
| pseudoBidiParams.country[1] = 'B'; |
| |
| // We should skip resources for pseudolocales if they were |
| // already added automatically. This is a fix for a transition period when |
| // manually pseudolocalized resources may be expected. |
| // TODO: remove this check after next SDK version release. |
| if ((bundle->getPseudolocalize() & PSEUDO_ACCENTED && |
| curParams.locale == pseudoParams.locale) || |
| (bundle->getPseudolocalize() & PSEUDO_BIDI && |
| curParams.locale == pseudoBidiParams.locale)) { |
| SourcePos(in->getPrintableSource(), 0).warning( |
| "Resource file %s is skipped as pseudolocalization" |
| " was done automatically.", |
| in->getPrintableSource().string()); |
| return NO_ERROR; |
| } |
| |
| while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) { |
| if (code == ResXMLTree::START_TAG) { |
| const String16* curTag = NULL; |
| String16 curType; |
| String16 curName; |
| int32_t curFormat = ResTable_map::TYPE_ANY; |
| bool curIsBag = false; |
| bool curIsBagReplaceOnOverwrite = false; |
| bool curIsStyled = false; |
| bool curIsPseudolocalizable = false; |
| bool curIsFormatted = fileIsTranslatable; |
| bool localHasErrors = false; |
| |
| if (strcmp16(block.getElementName(&len), skip16.string()) == 0) { |
| while ((code=block.next()) != ResXMLTree::END_DOCUMENT |
| && code != ResXMLTree::BAD_DOCUMENT) { |
| if (code == ResXMLTree::END_TAG) { |
| if (strcmp16(block.getElementName(&len), skip16.string()) == 0) { |
| break; |
| } |
| } |
| } |
| continue; |
| |
| } else if (strcmp16(block.getElementName(&len), eat_comment16.string()) == 0) { |
| while ((code=block.next()) != ResXMLTree::END_DOCUMENT |
| && code != ResXMLTree::BAD_DOCUMENT) { |
| if (code == ResXMLTree::END_TAG) { |
| if (strcmp16(block.getElementName(&len), eat_comment16.string()) == 0) { |
| break; |
| } |
| } |
| } |
| continue; |
| |
| } else if (strcmp16(block.getElementName(&len), public16.string()) == 0) { |
| SourcePos srcPos(in->getPrintableSource(), block.getLineNumber()); |
| |
| String16 type; |
| ssize_t typeIdx = block.indexOfAttribute(NULL, "type"); |
| if (typeIdx < 0) { |
| srcPos.error("A 'type' attribute is required for <public>\n"); |
| hasErrors = localHasErrors = true; |
| } |
| type = String16(block.getAttributeStringValue(typeIdx, &len)); |
| |
| String16 name; |
| ssize_t nameIdx = block.indexOfAttribute(NULL, "name"); |
| if (nameIdx < 0) { |
| srcPos.error("A 'name' attribute is required for <public>\n"); |
| hasErrors = localHasErrors = true; |
| } |
| name = String16(block.getAttributeStringValue(nameIdx, &len)); |
| |
| uint32_t ident = 0; |
| ssize_t identIdx = block.indexOfAttribute(NULL, "id"); |
| if (identIdx >= 0) { |
| const char16_t* identStr = block.getAttributeStringValue(identIdx, &len); |
| Res_value identValue; |
| if (!ResTable::stringToInt(identStr, len, &identValue)) { |
| srcPos.error("Given 'id' attribute is not an integer: %s\n", |
| String8(block.getAttributeStringValue(identIdx, &len)).string()); |
| hasErrors = localHasErrors = true; |
| } else { |
| ident = identValue.data; |
| nextPublicId.replaceValueFor(type, ident+1); |
| } |
| } else if (nextPublicId.indexOfKey(type) < 0) { |
| srcPos.error("No 'id' attribute supplied <public>," |
| " and no previous id defined in this file.\n"); |
| hasErrors = localHasErrors = true; |
| } else if (!localHasErrors) { |
| ident = nextPublicId.valueFor(type); |
| nextPublicId.replaceValueFor(type, ident+1); |
| } |
| |
| if (!localHasErrors) { |
| err = outTable->addPublic(srcPos, myPackage, type, name, ident); |
| if (err < NO_ERROR) { |
| hasErrors = localHasErrors = true; |
| } |
| } |
| if (!localHasErrors) { |
| sp<AaptSymbols> symbols = assets->getSymbolsFor(String8("R")); |
| if (symbols != NULL) { |
| symbols = symbols->addNestedSymbol(String8(type), srcPos); |
| } |
| if (symbols != NULL) { |
| symbols->makeSymbolPublic(String8(name), srcPos); |
| String16 comment( |
| block.getComment(&len) ? block.getComment(&len) : nulStr); |
| symbols->appendComment(String8(name), comment, srcPos); |
| } else { |
| srcPos.error("Unable to create symbols!\n"); |
| hasErrors = localHasErrors = true; |
| } |
| } |
| |
| while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) { |
| if (code == ResXMLTree::END_TAG) { |
| if (strcmp16(block.getElementName(&len), public16.string()) == 0) { |
| break; |
| } |
| } |
| } |
| continue; |
| |
| } else if (strcmp16(block.getElementName(&len), public_padding16.string()) == 0) { |
| SourcePos srcPos(in->getPrintableSource(), block.getLineNumber()); |
| |
| String16 type; |
| ssize_t typeIdx = block.indexOfAttribute(NULL, "type"); |
| if (typeIdx < 0) { |
| srcPos.error("A 'type' attribute is required for <public-padding>\n"); |
| hasErrors = localHasErrors = true; |
| } |
| type = String16(block.getAttributeStringValue(typeIdx, &len)); |
| |
| String16 name; |
| ssize_t nameIdx = block.indexOfAttribute(NULL, "name"); |
| if (nameIdx < 0) { |
| srcPos.error("A 'name' attribute is required for <public-padding>\n"); |
| hasErrors = localHasErrors = true; |
| } |
| name = String16(block.getAttributeStringValue(nameIdx, &len)); |
| |
| uint32_t start = 0; |
| ssize_t startIdx = block.indexOfAttribute(NULL, "start"); |
| if (startIdx >= 0) { |
| const char16_t* startStr = block.getAttributeStringValue(startIdx, &len); |
| Res_value startValue; |
| if (!ResTable::stringToInt(startStr, len, &startValue)) { |
| srcPos.error("Given 'start' attribute is not an integer: %s\n", |
| String8(block.getAttributeStringValue(startIdx, &len)).string()); |
| hasErrors = localHasErrors = true; |
| } else { |
| start = startValue.data; |
| } |
| } else if (nextPublicId.indexOfKey(type) < 0) { |
| srcPos.error("No 'start' attribute supplied <public-padding>," |
| " and no previous id defined in this file.\n"); |
| hasErrors = localHasErrors = true; |
| } else if (!localHasErrors) { |
| start = nextPublicId.valueFor(type); |
| } |
| |
| uint32_t end = 0; |
| ssize_t endIdx = block.indexOfAttribute(NULL, "end"); |
| if (endIdx >= 0) { |
| const char16_t* endStr = block.getAttributeStringValue(endIdx, &len); |
| Res_value endValue; |
| if (!ResTable::stringToInt(endStr, len, &endValue)) { |
| srcPos.error("Given 'end' attribute is not an integer: %s\n", |
| String8(block.getAttributeStringValue(endIdx, &len)).string()); |
| hasErrors = localHasErrors = true; |
| } else { |
| end = endValue.data; |
| } |
| } else { |
| srcPos.error("No 'end' attribute supplied <public-padding>\n"); |
| hasErrors = localHasErrors = true; |
| } |
| |
| if (end >= start) { |
| nextPublicId.replaceValueFor(type, end+1); |
| } else { |
| srcPos.error("Padding start '%ul' is after end '%ul'\n", |
| start, end); |
| hasErrors = localHasErrors = true; |
| } |
| |
| String16 comment( |
| block.getComment(&len) ? block.getComment(&len) : nulStr); |
| for (uint32_t curIdent=start; curIdent<=end; curIdent++) { |
| if (localHasErrors) { |
| break; |
| } |
| String16 curName(name); |
| char buf[64]; |
| sprintf(buf, "%d", (int)(end-curIdent+1)); |
| curName.append(String16(buf)); |
| |
| err = outTable->addEntry(srcPos, myPackage, type, curName, |
| String16("padding"), NULL, &curParams, false, |
| ResTable_map::TYPE_STRING, overwrite); |
| if (err < NO_ERROR) { |
| hasErrors = localHasErrors = true; |
| break; |
| } |
| err = outTable->addPublic(srcPos, myPackage, type, |
| curName, curIdent); |
| if (err < NO_ERROR) { |
| hasErrors = localHasErrors = true; |
| break; |
| } |
| sp<AaptSymbols> symbols = assets->getSymbolsFor(String8("R")); |
| if (symbols != NULL) { |
| symbols = symbols->addNestedSymbol(String8(type), srcPos); |
| } |
| if (symbols != NULL) { |
| symbols->makeSymbolPublic(String8(curName), srcPos); |
| symbols->appendComment(String8(curName), comment, srcPos); |
| } else { |
| srcPos.error("Unable to create symbols!\n"); |
| hasErrors = localHasErrors = true; |
| } |
| } |
| |
| while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) { |
| if (code == ResXMLTree::END_TAG) { |
| if (strcmp16(block.getElementName(&len), public_padding16.string()) == 0) { |
| break; |
| } |
| } |
| } |
| continue; |
| |
| } else if (strcmp16(block.getElementName(&len), private_symbols16.string()) == 0) { |
| String16 pkg; |
| ssize_t pkgIdx = block.indexOfAttribute(NULL, "package"); |
| if (pkgIdx < 0) { |
| SourcePos(in->getPrintableSource(), block.getLineNumber()).error( |
| "A 'package' attribute is required for <private-symbols>\n"); |
| hasErrors = localHasErrors = true; |
| } |
| pkg = String16(block.getAttributeStringValue(pkgIdx, &len)); |
| if (!localHasErrors) { |
| assets->setSymbolsPrivatePackage(String8(pkg)); |
| } |
| |
| while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) { |
| if (code == ResXMLTree::END_TAG) { |
| if (strcmp16(block.getElementName(&len), private_symbols16.string()) == 0) { |
| break; |
| } |
| } |
| } |
| continue; |
| |
| } else if (strcmp16(block.getElementName(&len), java_symbol16.string()) == 0) { |
| SourcePos srcPos(in->getPrintableSource(), block.getLineNumber()); |
| |
| String16 type; |
| ssize_t typeIdx = block.indexOfAttribute(NULL, "type"); |
| if (typeIdx < 0) { |
| srcPos.error("A 'type' attribute is required for <public>\n"); |
| hasErrors = localHasErrors = true; |
| } |
| type = String16(block.getAttributeStringValue(typeIdx, &len)); |
| |
| String16 name; |
| ssize_t nameIdx = block.indexOfAttribute(NULL, "name"); |
| if (nameIdx < 0) { |
| srcPos.error("A 'name' attribute is required for <public>\n"); |
| hasErrors = localHasErrors = true; |
| } |
| name = String16(block.getAttributeStringValue(nameIdx, &len)); |
| |
| sp<AaptSymbols> symbols = assets->getJavaSymbolsFor(String8("R")); |
| if (symbols != NULL) { |
| symbols = symbols->addNestedSymbol(String8(type), srcPos); |
| } |
| if (symbols != NULL) { |
| symbols->makeSymbolJavaSymbol(String8(name), srcPos); |
| String16 comment( |
| block.getComment(&len) ? block.getComment(&len) : nulStr); |
| symbols->appendComment(String8(name), comment, srcPos); |
| } else { |
| srcPos.error("Unable to create symbols!\n"); |
| hasErrors = localHasErrors = true; |
| } |
| |
| while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) { |
| if (code == ResXMLTree::END_TAG) { |
| if (strcmp16(block.getElementName(&len), java_symbol16.string()) == 0) { |
| break; |
| } |
| } |
| } |
| continue; |
| |
| |
| } else if (strcmp16(block.getElementName(&len), add_resource16.string()) == 0) { |
| SourcePos srcPos(in->getPrintableSource(), block.getLineNumber()); |
| |
| String16 typeName; |
| ssize_t typeIdx = block.indexOfAttribute(NULL, "type"); |
| if (typeIdx < 0) { |
| srcPos.error("A 'type' attribute is required for <add-resource>\n"); |
| hasErrors = localHasErrors = true; |
| } |
| typeName = String16(block.getAttributeStringValue(typeIdx, &len)); |
| |
| String16 name; |
| ssize_t nameIdx = block.indexOfAttribute(NULL, "name"); |
| if (nameIdx < 0) { |
| srcPos.error("A 'name' attribute is required for <add-resource>\n"); |
| hasErrors = localHasErrors = true; |
| } |
| name = String16(block.getAttributeStringValue(nameIdx, &len)); |
| |
| outTable->canAddEntry(srcPos, myPackage, typeName, name); |
| |
| while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) { |
| if (code == ResXMLTree::END_TAG) { |
| if (strcmp16(block.getElementName(&len), add_resource16.string()) == 0) { |
| break; |
| } |
| } |
| } |
| continue; |
| |
| } else if (strcmp16(block.getElementName(&len), declare_styleable16.string()) == 0) { |
| SourcePos srcPos(in->getPrintableSource(), block.getLineNumber()); |
| |
| String16 ident; |
| ssize_t identIdx = block.indexOfAttribute(NULL, "name"); |
| if (identIdx < 0) { |
| srcPos.error("A 'name' attribute is required for <declare-styleable>\n"); |
| hasErrors = localHasErrors = true; |
| } |
| ident = String16(block.getAttributeStringValue(identIdx, &len)); |
| |
| sp<AaptSymbols> symbols = assets->getSymbolsFor(String8("R")); |
| if (!localHasErrors) { |
| if (symbols != NULL) { |
| symbols = symbols->addNestedSymbol(String8("styleable"), srcPos); |
| } |
| sp<AaptSymbols> styleSymbols = symbols; |
| if (symbols != NULL) { |
| symbols = symbols->addNestedSymbol(String8(ident), srcPos); |
| } |
| if (symbols == NULL) { |
| srcPos.error("Unable to create symbols!\n"); |
| return UNKNOWN_ERROR; |
| } |
| |
| String16 comment( |
| block.getComment(&len) ? block.getComment(&len) : nulStr); |
| styleSymbols->appendComment(String8(ident), comment, srcPos); |
| } else { |
| symbols = NULL; |
| } |
| |
| while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) { |
| if (code == ResXMLTree::START_TAG) { |
| if (strcmp16(block.getElementName(&len), skip16.string()) == 0) { |
| while ((code=block.next()) != ResXMLTree::END_DOCUMENT |
| && code != ResXMLTree::BAD_DOCUMENT) { |
| if (code == ResXMLTree::END_TAG) { |
| if (strcmp16(block.getElementName(&len), skip16.string()) == 0) { |
| break; |
| } |
| } |
| } |
| continue; |
| } else if (strcmp16(block.getElementName(&len), eat_comment16.string()) == 0) { |
| while ((code=block.next()) != ResXMLTree::END_DOCUMENT |
| && code != ResXMLTree::BAD_DOCUMENT) { |
| if (code == ResXMLTree::END_TAG) { |
| if (strcmp16(block.getElementName(&len), eat_comment16.string()) == 0) { |
| break; |
| } |
| } |
| } |
| continue; |
| } else if (strcmp16(block.getElementName(&len), attr16.string()) != 0) { |
| SourcePos(in->getPrintableSource(), block.getLineNumber()).error( |
| "Tag <%s> can not appear inside <declare-styleable>, only <attr>\n", |
| String8(block.getElementName(&len)).string()); |
| return UNKNOWN_ERROR; |
| } |
| |
| String16 comment( |
| block.getComment(&len) ? block.getComment(&len) : nulStr); |
| String16 itemIdent; |
| err = compileAttribute(in, block, myPackage, outTable, &itemIdent, true); |
| if (err != NO_ERROR) { |
| hasErrors = localHasErrors = true; |
| } |
| |
| if (symbols != NULL) { |
| SourcePos srcPos(String8(in->getPrintableSource()), block.getLineNumber()); |
| symbols->addSymbol(String8(itemIdent), 0, srcPos); |
| symbols->appendComment(String8(itemIdent), comment, srcPos); |
| //printf("Attribute %s comment: %s\n", String8(itemIdent).string(), |
| // String8(comment).string()); |
| } |
| } else if (code == ResXMLTree::END_TAG) { |
| if (strcmp16(block.getElementName(&len), declare_styleable16.string()) == 0) { |
| break; |
| } |
| |
| SourcePos(in->getPrintableSource(), block.getLineNumber()).error( |
| "Found tag </%s> where </attr> is expected\n", |
| String8(block.getElementName(&len)).string()); |
| return UNKNOWN_ERROR; |
| } |
| } |
| continue; |
| |
| } else if (strcmp16(block.getElementName(&len), attr16.string()) == 0) { |
| err = compileAttribute(in, block, myPackage, outTable, NULL); |
| if (err != NO_ERROR) { |
| hasErrors = true; |
| } |
| continue; |
| |
| } else if (strcmp16(block.getElementName(&len), item16.string()) == 0) { |
| curTag = &item16; |
| ssize_t attri = block.indexOfAttribute(NULL, "type"); |
| if (attri >= 0) { |
| curType = String16(block.getAttributeStringValue(attri, &len)); |
| ssize_t nameIdx = block.indexOfAttribute(NULL, "name"); |
| if (nameIdx >= 0) { |
| curName = String16(block.getAttributeStringValue(nameIdx, &len)); |
| } |
| ssize_t formatIdx = block.indexOfAttribute(NULL, "format"); |
| if (formatIdx >= 0) { |
| String16 formatStr = String16(block.getAttributeStringValue( |
| formatIdx, &len)); |
| curFormat = parse_flags(formatStr.string(), formatStr.size(), |
| gFormatFlags); |
| if (curFormat == 0) { |
| SourcePos(in->getPrintableSource(), block.getLineNumber()).error( |
| "Tag <item> 'format' attribute value \"%s\" not valid\n", |
| String8(formatStr).string()); |
| hasErrors = localHasErrors = true; |
| } |
| } |
| } else { |
| SourcePos(in->getPrintableSource(), block.getLineNumber()).error( |
| "A 'type' attribute is required for <item>\n"); |
| hasErrors = localHasErrors = true; |
| } |
| curIsStyled = true; |
| } else if (strcmp16(block.getElementName(&len), string16.string()) == 0) { |
| // Note the existence and locale of every string we process |
| char rawLocale[RESTABLE_MAX_LOCALE_LEN]; |
| curParams.getBcp47Locale(rawLocale); |
| String8 locale(rawLocale); |
| String16 name; |
| String16 translatable; |
| String16 formatted; |
| |
| size_t n = block.getAttributeCount(); |
| for (size_t i = 0; i < n; i++) { |
| size_t length; |
| const char16_t* attr = block.getAttributeName(i, &length); |
| if (strcmp16(attr, name16.string()) == 0) { |
| name.setTo(block.getAttributeStringValue(i, &length)); |
| } else if (strcmp16(attr, translatable16.string()) == 0) { |
| translatable.setTo(block.getAttributeStringValue(i, &length)); |
| } else if (strcmp16(attr, formatted16.string()) == 0) { |
| formatted.setTo(block.getAttributeStringValue(i, &length)); |
| } |
| } |
| |
| if (name.size() > 0) { |
| if (locale.size() == 0) { |
| outTable->addDefaultLocalization(name); |
| } |
| if (translatable == false16) { |
| curIsFormatted = false; |
| // Untranslatable strings must only exist in the default [empty] locale |
| if (locale.size() > 0) { |
| SourcePos(in->getPrintableSource(), block.getLineNumber()).warning( |
| "string '%s' marked untranslatable but exists in locale '%s'\n", |
| String8(name).string(), |
| locale.string()); |
| // hasErrors = localHasErrors = true; |
| } else { |
| // Intentionally empty block: |
| // |
| // Don't add untranslatable strings to the localization table; that |
| // way if we later see localizations of them, they'll be flagged as |
| // having no default translation. |
| } |
| } else { |
| outTable->addLocalization( |
| name, |
| locale, |
| SourcePos(in->getPrintableSource(), block.getLineNumber())); |
| } |
| |
| if (formatted == false16) { |
| curIsFormatted = false; |
| } |
| } |
| |
| curTag = &string16; |
| curType = string16; |
| curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_STRING; |
| curIsStyled = true; |
| curIsPseudolocalizable = fileIsTranslatable && (translatable != false16); |
| } else if (strcmp16(block.getElementName(&len), drawable16.string()) == 0) { |
| curTag = &drawable16; |
| curType = drawable16; |
| curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_COLOR; |
| } else if (strcmp16(block.getElementName(&len), color16.string()) == 0) { |
| curTag = &color16; |
| curType = color16; |
| curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_COLOR; |
| } else if (strcmp16(block.getElementName(&len), bool16.string()) == 0) { |
| curTag = &bool16; |
| curType = bool16; |
| curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_BOOLEAN; |
| } else if (strcmp16(block.getElementName(&len), integer16.string()) == 0) { |
| curTag = &integer16; |
| curType = integer16; |
| curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_INTEGER; |
| } else if (strcmp16(block.getElementName(&len), dimen16.string()) == 0) { |
| curTag = &dimen16; |
| curType = dimen16; |
| curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_DIMENSION; |
| } else if (strcmp16(block.getElementName(&len), fraction16.string()) == 0) { |
| curTag = &fraction16; |
| curType = fraction16; |
| curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_FRACTION; |
| } else if (strcmp16(block.getElementName(&len), bag16.string()) == 0) { |
| curTag = &bag16; |
| curIsBag = true; |
| ssize_t attri = block.indexOfAttribute(NULL, "type"); |
| if (attri >= 0) { |
| curType = String16(block.getAttributeStringValue(attri, &len)); |
| } else { |
| SourcePos(in->getPrintableSource(), block.getLineNumber()).error( |
| "A 'type' attribute is required for <bag>\n"); |
| hasErrors = localHasErrors = true; |
| } |
| } else if (strcmp16(block.getElementName(&len), style16.string()) == 0) { |
| curTag = &style16; |
| curType = style16; |
| curIsBag = true; |
| } else if (strcmp16(block.getElementName(&len), plurals16.string()) == 0) { |
| curTag = &plurals16; |
| curType = plurals16; |
| curIsBag = true; |
| curIsPseudolocalizable = fileIsTranslatable; |
| } else if (strcmp16(block.getElementName(&len), array16.string()) == 0) { |
| curTag = &array16; |
| curType = array16; |
| curIsBag = true; |
| curIsBagReplaceOnOverwrite = true; |
| ssize_t formatIdx = block.indexOfAttribute(NULL, "format"); |
| if (formatIdx >= 0) { |
| String16 formatStr = String16(block.getAttributeStringValue( |
| formatIdx, &len)); |
| curFormat = parse_flags(formatStr.string(), formatStr.size(), |
| gFormatFlags); |
| if (curFormat == 0) { |
| SourcePos(in->getPrintableSource(), block.getLineNumber()).error( |
| "Tag <array> 'format' attribute value \"%s\" not valid\n", |
| String8(formatStr).string()); |
| hasErrors = localHasErrors = true; |
| } |
| } |
| } else if (strcmp16(block.getElementName(&len), string_array16.string()) == 0) { |
| // Check whether these strings need valid formats. |
| // (simplified form of what string16 does above) |
| bool isTranslatable = false; |
| size_t n = block.getAttributeCount(); |
| |
| // Pseudolocalizable by default, unless this string array isn't |
| // translatable. |
| for (size_t i = 0; i < n; i++) { |
| size_t length; |
| const char16_t* attr = block.getAttributeName(i, &length); |
| if (strcmp16(attr, formatted16.string()) == 0) { |
| const char16_t* value = block.getAttributeStringValue(i, &length); |
| if (strcmp16(value, false16.string()) == 0) { |
| curIsFormatted = false; |
| } |
| } else if (strcmp16(attr, translatable16.string()) == 0) { |
| const char16_t* value = block.getAttributeStringValue(i, &length); |
| if (strcmp16(value, false16.string()) == 0) { |
| isTranslatable = false; |
| } |
| } |
| } |
| |
| curTag = &string_array16; |
| curType = array16; |
| curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_STRING; |
| curIsBag = true; |
| curIsBagReplaceOnOverwrite = true; |
| curIsPseudolocalizable = isTranslatable && fileIsTranslatable; |
| } else if (strcmp16(block.getElementName(&len), integer_array16.string()) == 0) { |
| curTag = &integer_array16; |
| curType = array16; |
| curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_INTEGER; |
| curIsBag = true; |
| curIsBagReplaceOnOverwrite = true; |
| } else { |
| SourcePos(in->getPrintableSource(), block.getLineNumber()).error( |
| "Found tag %s where item is expected\n", |
| String8(block.getElementName(&len)).string()); |
| return UNKNOWN_ERROR; |
| } |
| |
| String16 ident; |
| ssize_t identIdx = block.indexOfAttribute(NULL, "name"); |
| if (identIdx >= 0) { |
| ident = String16(block.getAttributeStringValue(identIdx, &len)); |
| } else { |
| SourcePos(in->getPrintableSource(), block.getLineNumber()).error( |
| "A 'name' attribute is required for <%s>\n", |
| String8(*curTag).string()); |
| hasErrors = localHasErrors = true; |
| } |
| |
| String16 product; |
| identIdx = block.indexOfAttribute(NULL, "product"); |
| if (identIdx >= 0) { |
| product = String16(block.getAttributeStringValue(identIdx, &len)); |
| } |
| |
| String16 comment(block.getComment(&len) ? block.getComment(&len) : nulStr); |
| |
| if (curIsBag) { |
| // Figure out the parent of this bag... |
| String16 parentIdent; |
| ssize_t parentIdentIdx = block.indexOfAttribute(NULL, "parent"); |
| if (parentIdentIdx >= 0) { |
| parentIdent = String16(block.getAttributeStringValue(parentIdentIdx, &len)); |
| } else { |
| ssize_t sep = ident.findLast('.'); |
| if (sep >= 0) { |
| parentIdent.setTo(ident, sep); |
| } |
| } |
| |
| if (!localHasErrors) { |
| err = outTable->startBag(SourcePos(in->getPrintableSource(), |
| block.getLineNumber()), myPackage, curType, ident, |
| parentIdent, &curParams, |
| overwrite, curIsBagReplaceOnOverwrite); |
| if (err != NO_ERROR) { |
| hasErrors = localHasErrors = true; |
| } |
| } |
| |
| ssize_t elmIndex = 0; |
| char elmIndexStr[14]; |
| while ((code=block.next()) != ResXMLTree::END_DOCUMENT |
| && code != ResXMLTree::BAD_DOCUMENT) { |
| |
| if (code == ResXMLTree::START_TAG) { |
| if (strcmp16(block.getElementName(&len), item16.string()) != 0) { |
| SourcePos(in->getPrintableSource(), block.getLineNumber()).error( |
| "Tag <%s> can not appear inside <%s>, only <item>\n", |
| String8(block.getElementName(&len)).string(), |
| String8(*curTag).string()); |
| return UNKNOWN_ERROR; |
| } |
| |
| String16 itemIdent; |
| if (curType == array16) { |
| sprintf(elmIndexStr, "^index_%d", (int)elmIndex++); |
| itemIdent = String16(elmIndexStr); |
| } else if (curType == plurals16) { |
| ssize_t itemIdentIdx = block.indexOfAttribute(NULL, "quantity"); |
| if (itemIdentIdx >= 0) { |
| String16 quantity16(block.getAttributeStringValue(itemIdentIdx, &len)); |
| if (quantity16 == other16) { |
| itemIdent = quantityOther16; |
| } |
| else if (quantity16 == zero16) { |
| itemIdent = quantityZero16; |
| } |
| else if (quantity16 == one16) { |
| itemIdent = quantityOne16; |
| } |
| else if (quantity16 == two16) { |
| itemIdent = quantityTwo16; |
| } |
| else if (quantity16 == few16) { |
| itemIdent = quantityFew16; |
| } |
| else if (quantity16 == many16) { |
| itemIdent = quantityMany16; |
| } |
| else { |
| SourcePos(in->getPrintableSource(), block.getLineNumber()).error( |
| "Illegal 'quantity' attribute is <item> inside <plurals>\n"); |
| hasErrors = localHasErrors = true; |
| } |
| } else { |
| SourcePos(in->getPrintableSource(), block.getLineNumber()).error( |
| "A 'quantity' attribute is required for <item> inside <plurals>\n"); |
| hasErrors = localHasErrors = true; |
| } |
| } else { |
| ssize_t itemIdentIdx = block.indexOfAttribute(NULL, "name"); |
| if (itemIdentIdx >= 0) { |
| itemIdent = String16(block.getAttributeStringValue(itemIdentIdx, &len)); |
| } else { |
| SourcePos(in->getPrintableSource(), block.getLineNumber()).error( |
| "A 'name' attribute is required for <item>\n"); |
| hasErrors = localHasErrors = true; |
| } |
| } |
| |
| ResXMLParser::ResXMLPosition parserPosition; |
| block.getPosition(&parserPosition); |
| |
| err = parseAndAddBag(bundle, in, &block, curParams, myPackage, curType, |
| ident, parentIdent, itemIdent, curFormat, curIsFormatted, |
| product, NO_PSEUDOLOCALIZATION, overwrite, outTable); |
| if (err == NO_ERROR) { |
| if (curIsPseudolocalizable && localeIsDefined(curParams) |
| && bundle->getPseudolocalize() > 0) { |
| // pseudolocalize here |
| if ((PSEUDO_ACCENTED & bundle->getPseudolocalize()) == |
| PSEUDO_ACCENTED) { |
| block.setPosition(parserPosition); |
| err = parseAndAddBag(bundle, in, &block, pseudoParams, myPackage, |
| curType, ident, parentIdent, itemIdent, curFormat, |
| curIsFormatted, product, PSEUDO_ACCENTED, |
| overwrite, outTable); |
| } |
| if ((PSEUDO_BIDI & bundle->getPseudolocalize()) == |
| PSEUDO_BIDI) { |
| block.setPosition(parserPosition); |
| err = parseAndAddBag(bundle, in, &block, pseudoBidiParams, myPackage, |
| curType, ident, parentIdent, itemIdent, curFormat, |
| curIsFormatted, product, PSEUDO_BIDI, |
| overwrite, outTable); |
| } |
| } |
| } |
| if (err != NO_ERROR) { |
| hasErrors = localHasErrors = true; |
| } |
| } else if (code == ResXMLTree::END_TAG) { |
| if (strcmp16(block.getElementName(&len), curTag->string()) != 0) { |
| SourcePos(in->getPrintableSource(), block.getLineNumber()).error( |
| "Found tag </%s> where </%s> is expected\n", |
| String8(block.getElementName(&len)).string(), |
| String8(*curTag).string()); |
| return UNKNOWN_ERROR; |
| } |
| break; |
| } |
| } |
| } else { |
| ResXMLParser::ResXMLPosition parserPosition; |
| block.getPosition(&parserPosition); |
| |
| err = parseAndAddEntry(bundle, in, &block, curParams, myPackage, curType, ident, |
| *curTag, curIsStyled, curFormat, curIsFormatted, |
| product, NO_PSEUDOLOCALIZATION, overwrite, &skippedResourceNames, outTable); |
| |
| if (err < NO_ERROR) { // Why err < NO_ERROR instead of err != NO_ERROR? |
| hasErrors = localHasErrors = true; |
| } |
| else if (err == NO_ERROR) { |
| if (curType == string16 && !curParams.language[0] && !curParams.country[0]) { |
| outTable->addDefaultLocalization(curName); |
| } |
| if (curIsPseudolocalizable && localeIsDefined(curParams) |
| && bundle->getPseudolocalize() > 0) { |
| // pseudolocalize here |
| if ((PSEUDO_ACCENTED & bundle->getPseudolocalize()) == |
| PSEUDO_ACCENTED) { |
| block.setPosition(parserPosition); |
| err = parseAndAddEntry(bundle, in, &block, pseudoParams, myPackage, curType, |
| ident, *curTag, curIsStyled, curFormat, |
| curIsFormatted, product, |
| PSEUDO_ACCENTED, overwrite, &skippedResourceNames, outTable); |
| } |
| if ((PSEUDO_BIDI & bundle->getPseudolocalize()) == |
| PSEUDO_BIDI) { |
| block.setPosition(parserPosition); |
| err = parseAndAddEntry(bundle, in, &block, pseudoBidiParams, |
| myPackage, curType, ident, *curTag, curIsStyled, curFormat, |
| curIsFormatted, product, |
| PSEUDO_BIDI, overwrite, &skippedResourceNames, outTable); |
| } |
| if (err != NO_ERROR) { |
| hasErrors = localHasErrors = true; |
| } |
| } |
| } |
| } |
| |
| #if 0 |
| if (comment.size() > 0) { |
| printf("Comment for @%s:%s/%s: %s\n", String8(myPackage).string(), |
| String8(curType).string(), String8(ident).string(), |
| String8(comment).string()); |
| } |
| #endif |
| if (!localHasErrors) { |
| outTable->appendComment(myPackage, curType, ident, comment, false); |
| } |
| } |
| else if (code == ResXMLTree::END_TAG) { |
| if (strcmp16(block.getElementName(&len), resources16.string()) != 0) { |
| SourcePos(in->getPrintableSource(), block.getLineNumber()).error( |
| "Unexpected end tag %s\n", String8(block.getElementName(&len)).string()); |
| return UNKNOWN_ERROR; |
| } |
| } |
| else if (code == ResXMLTree::START_NAMESPACE || code == ResXMLTree::END_NAMESPACE) { |
| } |
| else if (code == ResXMLTree::TEXT) { |
| if (isWhitespace(block.getText(&len))) { |
| continue; |
| } |
| SourcePos(in->getPrintableSource(), block.getLineNumber()).error( |
| "Found text \"%s\" where item tag is expected\n", |
| String8(block.getText(&len)).string()); |
| return UNKNOWN_ERROR; |
| } |
| } |
| |
| // For every resource defined, there must be exist one variant with a product attribute |
| // set to 'default' (or no product attribute at all). |
| // We check to see that for every resource that was ignored because of a mismatched |
| // product attribute, some product variant of that resource was processed. |
| for (size_t i = 0; i < skippedResourceNames.size(); i++) { |
| if (skippedResourceNames[i]) { |
| const type_ident_pair_t& p = skippedResourceNames.keyAt(i); |
| if (!outTable->hasBagOrEntry(myPackage, p.type, p.ident)) { |
| const char* bundleProduct = |
| (bundle->getProduct() == NULL) ? "" : bundle->getProduct(); |
| fprintf(stderr, "In resource file %s: %s\n", |
| in->getPrintableSource().string(), |
| curParams.toString().string()); |
| |
| fprintf(stderr, "\t%s '%s' does not match product %s.\n" |
| "\tYou may have forgotten to include a 'default' product variant" |
| " of the resource.\n", |
| String8(p.type).string(), String8(p.ident).string(), |
| bundleProduct[0] == 0 ? "default" : bundleProduct); |
| return UNKNOWN_ERROR; |
| } |
| } |
| } |
| |
| return hasErrors ? STATUST(UNKNOWN_ERROR) : NO_ERROR; |
| } |
| |
| ResourceTable::ResourceTable(Bundle* bundle, const String16& assetsPackage, ResourceTable::PackageType type) |
| : mAssetsPackage(assetsPackage) |
| , mPackageType(type) |
| , mTypeIdOffset(0) |
| , mNumLocal(0) |
| , mBundle(bundle) |
| { |
| ssize_t packageId = -1; |
| switch (mPackageType) { |
| case App: |
| case AppFeature: |
| packageId = 0x7f; |
| break; |
| |
| case System: |
| packageId = 0x01; |
| break; |
| |
| case SharedLibrary: |
| packageId = 0x00; |
| break; |
| |
| default: |
| assert(0); |
| break; |
| } |
| sp<Package> package = new Package(mAssetsPackage, packageId); |
| mPackages.add(assetsPackage, package); |
| mOrderedPackages.add(package); |
| |
| // Every resource table always has one first entry, the bag attributes. |
| const SourcePos unknown(String8("????"), 0); |
| getType(mAssetsPackage, String16("attr"), unknown); |
| } |
| |
| static uint32_t findLargestTypeIdForPackage(const ResTable& table, const String16& packageName) { |
| const size_t basePackageCount = table.getBasePackageCount(); |
| for (size_t i = 0; i < basePackageCount; i++) { |
| if (packageName == table.getBasePackageName(i)) { |
| return table.getLastTypeIdForPackage(i); |
| } |
| } |
| return 0; |
| } |
| |
| status_t ResourceTable::addIncludedResources(Bundle* bundle, const sp<AaptAssets>& assets) |
| { |
| status_t err = assets->buildIncludedResources(bundle); |
| if (err != NO_ERROR) { |
| return err; |
| } |
| |
| mAssets = assets; |
| mTypeIdOffset = findLargestTypeIdForPackage(assets->getIncludedResources(), mAssetsPackage); |
| |
| const String8& featureAfter = bundle->getFeatureAfterPackage(); |
| if (!featureAfter.isEmpty()) { |
| AssetManager featureAssetManager; |
| if (!featureAssetManager.addAssetPath(featureAfter, NULL)) { |
| fprintf(stderr, "ERROR: Feature package '%s' not found.\n", |
| featureAfter.string()); |
| return UNKNOWN_ERROR; |
| } |
| |
| const ResTable& featureTable = featureAssetManager.getResources(false); |
| mTypeIdOffset = std::max(mTypeIdOffset, |
| findLargestTypeIdForPackage(featureTable, mAssetsPackage)); |
| } |
| |
| return NO_ERROR; |
| } |
| |
| status_t ResourceTable::addPublic(const SourcePos& sourcePos, |
| const String16& package, |
| const String16& type, |
| const String16& name, |
| const uint32_t ident) |
| { |
| uint32_t rid = mAssets->getIncludedResources() |
| .identifierForName(name.string(), name.size(), |
| type.string(), type.size(), |
| package.string(), package.size()); |
| if (rid != 0) { |
| sourcePos.error("Error declaring public resource %s/%s for included package %s\n", |
| String8(type).string(), String8(name).string(), |
| String8(package).string()); |
| return UNKNOWN_ERROR; |
| } |
| |
| sp<Type> t = getType(package, type, sourcePos); |
| if (t == NULL) { |
| return UNKNOWN_ERROR; |
| } |
| return t->addPublic(sourcePos, name, ident); |
| } |
| |
| status_t ResourceTable::addEntry(const SourcePos& sourcePos, |
| const String16& package, |
| const String16& type, |
| const String16& name, |
| const String16& value, |
| const Vector<StringPool::entry_style_span>* style, |
| const ResTable_config* params, |
| const bool doSetIndex, |
| const int32_t format, |
| const bool overwrite) |
| { |
| uint32_t rid = mAssets->getIncludedResources() |
| .identifierForName(name.string(), name.size(), |
| type.string(), type.size(), |
| package.string(), package.size()); |
| if (rid != 0) { |
| sourcePos.error("Resource entry %s/%s is already defined in package %s.", |
| String8(type).string(), String8(name).string(), String8(package).string()); |
| return UNKNOWN_ERROR; |
| } |
| |
| sp<Entry> e = getEntry(package, type, name, sourcePos, overwrite, |
| params, doSetIndex); |
| if (e == NULL) { |
| return UNKNOWN_ERROR; |
| } |
| status_t err = e->setItem(sourcePos, value, style, format, overwrite); |
| if (err == NO_ERROR) { |
| mNumLocal++; |
| } |
| return err; |
| } |
| |
| status_t ResourceTable::startBag(const SourcePos& sourcePos, |
| const String16& package, |
| const String16& type, |
| const String16& name, |
| const String16& bagParent, |
| const ResTable_config* params, |
| bool overlay, |
| bool replace, bool /* isId */) |
| { |
| status_t result = NO_ERROR; |
| |
| // Check for adding entries in other packages... for now we do |
| // nothing. We need to do the right thing here to support skinning. |
| uint32_t rid = mAssets->getIncludedResources() |
| .identifierForName(name.string(), name.size(), |
| type.string(), type.size(), |
| package.string(), package.size()); |
| if (rid != 0) { |
| sourcePos.error("Resource entry %s/%s is already defined in package %s.", |
| String8(type).string(), String8(name).string(), String8(package).string()); |
| return UNKNOWN_ERROR; |
| } |
| |
| if (overlay && !mBundle->getAutoAddOverlay() && !hasBagOrEntry(package, type, name)) { |
| bool canAdd = false; |
| sp<Package> p = mPackages.valueFor(package); |
| if (p != NULL) { |
| sp<Type> t = p->getTypes().valueFor(type); |
| if (t != NULL) { |
| if (t->getCanAddEntries().indexOf(name) >= 0) { |
| canAdd = true; |
| } |
| } |
| } |
| if (!canAdd) { |
| sourcePos.error("Resource does not already exist in overlay at '%s'; use <add-resource> to add.\n", |
| String8(name).string()); |
| return UNKNOWN_ERROR; |
| } |
| } |
| sp<Entry> e = getEntry(package, type, name, sourcePos, overlay, params); |
| if (e == NULL) { |
| return UNKNOWN_ERROR; |
| } |
| |
| // If a parent is explicitly specified, set it. |
| if (bagParent.size() > 0) { |
| e->setParent(bagParent); |
| } |
| |
| if ((result = e->makeItABag(sourcePos)) != NO_ERROR) { |
| return result; |
| } |
| |
| if (overlay && replace) { |
| return e->emptyBag(sourcePos); |
| } |
| return result; |
| } |
| |
| status_t ResourceTable::addBag(const SourcePos& sourcePos, |
| const String16& package, |
| const String16& type, |
| const String16& name, |
| const String16& bagParent, |
| const String16& bagKey, |
| const String16& value, |
| const Vector<StringPool::entry_style_span>* style, |
| const ResTable_config* params, |
| bool replace, bool isId, const int32_t format) |
| { |
| // Check for adding entries in other packages... for now we do |
| // nothing. We need to do the right thing here to support skinning. |
| uint32_t rid = mAssets->getIncludedResources() |
| .identifierForName(name.string(), name.size(), |
| type.string(), type.size(), |
| package.string(), package.size()); |
| if (rid != 0) { |
| return NO_ERROR; |
| } |
| |
| #if 0 |
| if (name == String16("left")) { |
| printf("Adding bag left: file=%s, line=%d, type=%s\n", |
| sourcePos.file.striing(), sourcePos.line, String8(type).string()); |
| } |
| #endif |
| sp<Entry> e = getEntry(package, type, name, sourcePos, replace, params); |
| if (e == NULL) { |
| return UNKNOWN_ERROR; |
| } |
| |
| // If a parent is explicitly specified, set it. |
| if (bagParent.size() > 0) { |
| e->setParent(bagParent); |
| } |
| |
| const bool first = e->getBag().indexOfKey(bagKey) < 0; |
| status_t err = e->addToBag(sourcePos, bagKey, value, style, replace, isId, format); |
| if (err == NO_ERROR && first) { |
| mNumLocal++; |
| } |
| return err; |
| } |
| |
| bool ResourceTable::hasBagOrEntry(const String16& package, |
| const String16& type, |
| const String16& name) const |
| { |
| // First look for this in the included resources... |
| uint32_t rid = mAssets->getIncludedResources() |
| .identifierForName(name.string(), name.size(), |
| type.string(), type.size(), |
| package.string(), package.size()); |
| if (rid != 0) { |
| return true; |
| } |
| |
| sp<Package> p = mPackages.valueFor(package); |
| if (p != NULL) { |
| sp<Type> t = p->getTypes().valueFor(type); |
| if (t != NULL) { |
| sp<ConfigList> c = t->getConfigs().valueFor(name); |
| if (c != NULL) return true; |
| } |
| } |
| |
| return false; |
| } |
| |
| bool ResourceTable::hasBagOrEntry(const String16& package, |
| const String16& type, |
| const String16& name, |
| const ResTable_config& config) const |
| { |
| // First look for this in the included resources... |
| uint32_t rid = mAssets->getIncludedResources() |
| .identifierForName(name.string(), name.size(), |
| type.string(), type.size(), |
| package.string(), package.size()); |
| if (rid != 0) { |
| return true; |
| } |
| |
| sp<Package> p = mPackages.valueFor(package); |
| if (p != NULL) { |
| sp<Type> t = p->getTypes().valueFor(type); |
| if (t != NULL) { |
| sp<ConfigList> c = t->getConfigs().valueFor(name); |
| if (c != NULL) { |
| sp<Entry> e = c->getEntries().valueFor(config); |
| if (e != NULL) { |
| return true; |
| } |
| } |
| } |
| } |
| |
| return false; |
| } |
| |
| bool ResourceTable::hasBagOrEntry(const String16& ref, |
| const String16* defType, |
| const String16* defPackage) |
| { |
| String16 package, type, name; |
| if (!ResTable::expandResourceRef(ref.string(), ref.size(), &package, &type, &name, |
| defType, defPackage ? defPackage:&mAssetsPackage, NULL)) { |
| return false; |
| } |
| return hasBagOrEntry(package, type, name); |
| } |
| |
| bool ResourceTable::appendComment(const String16& package, |
| const String16& type, |
| const String16& name, |
| const String16& comment, |
| bool onlyIfEmpty) |
| { |
| if (comment.size() <= 0) { |
| return true; |
| } |
| |
| sp<Package> p = mPackages.valueFor(package); |
| if (p != NULL) { |
| sp<Type> t = p->getTypes().valueFor(type); |
| if (t != NULL) { |
| sp<ConfigList> c = t->getConfigs().valueFor(name); |
| if (c != NULL) { |
| c->appendComment(comment, onlyIfEmpty); |
| return true; |
| } |
| } |
| } |
| return false; |
| } |
| |
| bool ResourceTable::appendTypeComment(const String16& package, |
| const String16& type, |
| const String16& name, |
| const String16& comment) |
| { |
| if (comment.size() <= 0) { |
| return true; |
| } |
| |
| sp<Package> p = mPackages.valueFor(package); |
| if (p != NULL) { |
| sp<Type> t = p->getTypes().valueFor(type); |
| if (t != NULL) { |
| sp<ConfigList> c = t->getConfigs().valueFor(name); |
| if (c != NULL) { |
| c->appendTypeComment(comment); |
| return true; |
| } |
| } |
| } |
| return false; |
| } |
| |
| void ResourceTable::canAddEntry(const SourcePos& pos, |
| const String16& package, const String16& type, const String16& name) |
| { |
| sp<Type> t = getType(package, type, pos); |
| if (t != NULL) { |
| t->canAddEntry(name); |
| } |
| } |
| |
| size_t ResourceTable::size() const { |
| return mPackages.size(); |
| } |
| |
| size_t ResourceTable::numLocalResources() const { |
| return mNumLocal; |
| } |
| |
| bool ResourceTable::hasResources() const { |
| return mNumLocal > 0; |
| } |
| |
| sp<AaptFile> ResourceTable::flatten(Bundle* bundle, const sp<const ResourceFilter>& filter, |
| const bool isBase) |
| { |
| sp<AaptFile> data = new AaptFile(String8(), AaptGroupEntry(), String8()); |
| status_t err = flatten(bundle, filter, data, isBase); |
| return err == NO_ERROR ? data : NULL; |
| } |
| |
| inline uint32_t ResourceTable::getResId(const sp<Package>& p, |
| const sp<Type>& t, |
| uint32_t nameId) |
| { |
| return makeResId(p->getAssignedId(), t->getIndex(), nameId); |
| } |
| |
| uint32_t ResourceTable::getResId(const String16& package, |
| const String16& type, |
| const String16& name, |
| bool onlyPublic) const |
| { |
| uint32_t id = ResourceIdCache::lookup(package, type, name, onlyPublic); |
| if (id != 0) return id; // cache hit |
| |
| // First look for this in the included resources... |
| uint32_t specFlags = 0; |
| uint32_t rid = mAssets->getIncludedResources() |
| .identifierForName(name.string(), name.size(), |
| type.string(), type.size(), |
| package.string(), package.size(), |
| &specFlags); |
| if (rid != 0) { |
| if (onlyPublic) { |
| if ((specFlags & ResTable_typeSpec::SPEC_PUBLIC) == 0) { |
| return 0; |
| } |
| } |
| |
| return ResourceIdCache::store(package, type, name, onlyPublic, rid); |
| } |
| |
| sp<Package> p = mPackages.valueFor(package); |
| if (p == NULL) return 0; |
| sp<Type> t = p->getTypes().valueFor(type); |
| if (t == NULL) return 0; |
| sp<ConfigList> c = t->getConfigs().valueFor(name); |
| if (c == NULL) { |
| if (type != String16("attr")) { |
| return 0; |
| } |
| t = p->getTypes().valueFor(String16(kAttrPrivateType)); |
| if (t == NULL) return 0; |
| c = t->getConfigs().valueFor(name); |
| if (c == NULL) return 0; |
| } |
| int32_t ei = c->getEntryIndex(); |
| if (ei < 0) return 0; |
| |
| return ResourceIdCache::store(package, type, name, onlyPublic, |
| getResId(p, t, ei)); |
| } |
| |
| uint32_t ResourceTable::getResId(const String16& ref, |
| const String16* defType, |
| const String16* defPackage, |
| const char** outErrorMsg, |
| bool onlyPublic) const |
| { |
| String16 package, type, name; |
| bool refOnlyPublic = true; |
| if (!ResTable::expandResourceRef( |
| ref.string(), ref.size(), &package, &type, &name, |
| defType, defPackage ? defPackage:&mAssetsPackage, |
| outErrorMsg, &refOnlyPublic)) { |
| if (kIsDebug) { |
| printf("Expanding resource: ref=%s\n", String8(ref).string()); |
| printf("Expanding resource: defType=%s\n", |
| defType ? String8(*defType).string() : "NULL"); |
| printf("Expanding resource: defPackage=%s\n", |
| defPackage ? String8(*defPackage).string() : "NULL"); |
| printf("Expanding resource: ref=%s\n", String8(ref).string()); |
| printf("Expanded resource: p=%s, t=%s, n=%s, res=0\n", |
| String8(package).string(), String8(type).string(), |
| String8(name).string()); |
| } |
| return 0; |
| } |
| uint32_t res = getResId(package, type, name, onlyPublic && refOnlyPublic); |
| if (kIsDebug) { |
| printf("Expanded resource: p=%s, t=%s, n=%s, res=%d\n", |
| String8(package).string(), String8(type).string(), |
| String8(name).string(), res); |
| } |
| if (res == 0) { |
| if (outErrorMsg) |
| *outErrorMsg = "No resource found that matches the given name"; |
| } |
| return res; |
| } |
| |
| bool ResourceTable::isValidResourceName(const String16& s) |
| { |
| const char16_t* p = s.string(); |
| bool first = true; |
| while (*p) { |
| if ((*p >= 'a' && *p <= 'z') |
| || (*p >= 'A' && *p <= 'Z') |
| || *p == '_' |
| || (!first && *p >= '0' && *p <= '9')) { |
| first = false; |
| p++; |
| continue; |
| } |
| return false; |
| } |
| return true; |
| } |
| |
| bool ResourceTable::stringToValue(Res_value* outValue, StringPool* pool, |
| const String16& str, |
| bool preserveSpaces, bool coerceType, |
| uint32_t attrID, |
| const Vector<StringPool::entry_style_span>* style, |
| String16* outStr, void* accessorCookie, |
| uint32_t attrType, const String8* configTypeName, |
| const ConfigDescription* config) |
| { |
| String16 finalStr; |
| |
| bool res = true; |
| if (style == NULL || style->size() == 0) { |
| // Text is not styled so it can be any type... let's figure it out. |
| res = mAssets->getIncludedResources() |
| .stringToValue(outValue, &finalStr, str.string(), str.size(), preserveSpaces, |
| coerceType, attrID, NULL, &mAssetsPackage, this, |
| accessorCookie, attrType); |
| } else { |
| // Styled text can only be a string, and while collecting the style |
| // information we have already processed that string! |
| outValue->size = sizeof(Res_value); |
| outValue->res0 = 0; |
| outValue->dataType = outValue->TYPE_STRING; |
| outValue->data = 0; |
| finalStr = str; |
| } |
| |
| if (!res) { |
| return false; |
| } |
| |
| if (outValue->dataType == outValue->TYPE_STRING) { |
| // Should do better merging styles. |
| if (pool) { |
| String8 configStr; |
| if (config != NULL) { |
| configStr = config->toString(); |
| } else { |
| configStr = "(null)"; |
| } |
| if (kIsDebug) { |
| printf("Adding to pool string style #%zu config %s: %s\n", |
| style != NULL ? style->size() : 0U, |
| configStr.string(), String8(finalStr).string()); |
| } |
| if (style != NULL && style->size() > 0) { |
| outValue->data = pool->add(finalStr, *style, configTypeName, config); |
| } else { |
| outValue->data = pool->add(finalStr, true, configTypeName, config); |
| } |
| } else { |
| // Caller will fill this in later. |
| outValue->data = 0; |
| } |
| |
| if (outStr) { |
| *outStr = finalStr; |
| } |
| |
| } |
| |
| return true; |
| } |
| |
| uint32_t ResourceTable::getCustomResource( |
| const String16& package, const String16& type, const String16& name) const |
| { |
| //printf("getCustomResource: %s %s %s\n", String8(package).string(), |
| // String8(type).string(), String8(name).string()); |
| sp<Package> p = mPackages.valueFor(package); |
| if (p == NULL) return 0; |
| sp<Type> t = p->getTypes().valueFor(type); |
| if (t == NULL) return 0; |
| sp<ConfigList> c = t->getConfigs().valueFor(name); |
| if (c == NULL) { |
| if (type != String16("attr")) { |
| return 0; |
| } |
| t = p->getTypes().valueFor(String16(kAttrPrivateType)); |
| if (t == NULL) return 0; |
| c = t->getConfigs().valueFor(name); |
| if (c == NULL) return 0; |
| } |
| int32_t ei = c->getEntryIndex(); |
| if (ei < 0) return 0; |
| return getResId(p, t, ei); |
| } |
| |
| uint32_t ResourceTable::getCustomResourceWithCreation( |
| const String16& package, const String16& type, const String16& name, |
| const bool createIfNotFound) |
| { |
| uint32_t resId = getCustomResource(package, type, name); |
| if (resId != 0 || !createIfNotFound) { |
| return resId; |
| } |
| |
| if (mAssetsPackage != package) { |
| mCurrentXmlPos.error("creating resource for external package %s: %s/%s.", |
| String8(package).string(), String8(type).string(), String8(name).string()); |
| if (package == String16("android")) { |
| mCurrentXmlPos.printf("did you mean to use @+id instead of @+android:id?"); |
| } |
| return 0; |
| } |
| |
| String16 value("false"); |
| status_t status = addEntry(mCurrentXmlPos, package, type, name, value, NULL, NULL, true); |
| if (status == NO_ERROR) { |
| resId = getResId(package, type, name); |
| return resId; |
| } |
| return 0; |
| } |
| |
| uint32_t ResourceTable::getRemappedPackage(uint32_t origPackage) const |
| { |
| return origPackage; |
| } |
| |
| bool ResourceTable::getAttributeType(uint32_t attrID, uint32_t* outType) |
| { |
| //printf("getAttributeType #%08x\n", attrID); |
| Res_value value; |
| if (getItemValue(attrID, ResTable_map::ATTR_TYPE, &value)) { |
| //printf("getAttributeType #%08x (%s): #%08x\n", attrID, |
| // String8(getEntry(attrID)->getName()).string(), value.data); |
| *outType = value.data; |
| return true; |
| } |
| return false; |
| } |
| |
| bool ResourceTable::getAttributeMin(uint32_t attrID, uint32_t* outMin) |
| { |
| //printf("getAttributeMin #%08x\n", attrID); |
| Res_value value; |
| if (getItemValue(attrID, ResTable_map::ATTR_MIN, &value)) { |
| *outMin = value.data; |
| return true; |
| } |
| return false; |
| } |
| |
| bool ResourceTable::getAttributeMax(uint32_t attrID, uint32_t* outMax) |
| { |
| //printf("getAttributeMax #%08x\n", attrID); |
| Res_value value; |
| if (getItemValue(attrID, ResTable_map::ATTR_MAX, &value)) { |
| *outMax = value.data; |
| return true; |
| } |
| return false; |
| } |
| |
| uint32_t ResourceTable::getAttributeL10N(uint32_t attrID) |
| { |
| //printf("getAttributeL10N #%08x\n", attrID); |
| Res_value value; |
| if (getItemValue(attrID, ResTable_map::ATTR_L10N, &value)) { |
| return value.data; |
| } |
| return ResTable_map::L10N_NOT_REQUIRED; |
| } |
| |
| bool ResourceTable::getLocalizationSetting() |
| { |
| return mBundle->getRequireLocalization(); |
| } |
| |
| void ResourceTable::reportError(void* accessorCookie, const char* fmt, ...) |
| { |
| if (accessorCookie != NULL && fmt != NULL) { |
| AccessorCookie* ac = (AccessorCookie*)accessorCookie; |
| int retval=0; |
| char buf[1024]; |
| va_list ap; |
| va_start(ap, fmt); |
| retval = vsnprintf(buf, sizeof(buf), fmt, ap); |
| va_end(ap); |
| ac->sourcePos.error("Error: %s (at '%s' with value '%s').\n", |
| buf, ac->attr.string(), ac->value.string()); |
| } |
| } |
| |
| bool ResourceTable::getAttributeKeys( |
| uint32_t attrID, Vector<String16>* outKeys) |
| { |
| sp<const Entry> e = getEntry(attrID); |
| if (e != NULL) { |
| const size_t N = e->getBag().size(); |
| for (size_t i=0; i<N; i++) { |
| const String16& key = e->getBag().keyAt(i); |
| if (key.size() > 0 && key.string()[0] != '^') { |
| outKeys->add(key); |
| } |
| } |
| return true; |
| } |
| return false; |
| } |
| |
| bool ResourceTable::getAttributeEnum( |
| uint32_t attrID, const char16_t* name, size_t nameLen, |
| Res_value* outValue) |
| { |
| //printf("getAttributeEnum #%08x %s\n", attrID, String8(name, nameLen).string()); |
| String16 nameStr(name, nameLen); |
| sp<const Entry> e = getEntry(attrID); |
| if (e != NULL) { |
| const size_t N = e->getBag().size(); |
| for (size_t i=0; i<N; i++) { |
| //printf("Comparing %s to %s\n", String8(name, nameLen).string(), |
| // String8(e->getBag().keyAt(i)).string()); |
| if (e->getBag().keyAt(i) == nameStr) { |
| return getItemValue(attrID, e->getBag().valueAt(i).bagKeyId, outValue); |
| } |
| } |
| } |
| return false; |
| } |
| |
| bool ResourceTable::getAttributeFlags( |
| uint32_t attrID, const char16_t* name, size_t nameLen, |
| Res_value* outValue) |
| { |
| outValue->dataType = Res_value::TYPE_INT_HEX; |
| outValue->data = 0; |
| |
| //printf("getAttributeFlags #%08x %s\n", attrID, String8(name, nameLen).string()); |
| String16 nameStr(name, nameLen); |
| sp<const Entry> e = getEntry(attrID); |
| if (e != NULL) { |
| const size_t N = e->getBag().size(); |
| |
| const char16_t* end = name + nameLen; |
| const char16_t* pos = name; |
| while (pos < end) { |
| const char16_t* start = pos; |
| while (pos < end && *pos != '|') { |
| pos++; |
| } |
| |
| String16 nameStr(start, pos-start); |
| size_t i; |
| for (i=0; i<N; i++) { |
| //printf("Comparing \"%s\" to \"%s\"\n", String8(nameStr).string(), |
| // String8(e->getBag().keyAt(i)).string()); |
| if (e->getBag().keyAt(i) == nameStr) { |
| Res_value val; |
| bool got = getItemValue(attrID, e->getBag().valueAt(i).bagKeyId, &val); |
| if (!got) { |
| return false; |
| } |
| //printf("Got value: 0x%08x\n", val.data); |
| outValue->data |= val.data; |
| break; |
| } |
| } |
| |
| if (i >= N) { |
| // Didn't find this flag identifier. |
| return false; |
| } |
| pos++; |
| } |
| |
| return true; |
| } |
| return false; |
| } |
| |
| status_t ResourceTable::assignResourceIds() |
| { |
| const size_t N = mOrderedPackages.size(); |
| size_t pi; |
| status_t firstError = NO_ERROR; |
| |
| // First generate all bag attributes and assign indices. |
| for (pi=0; pi<N; pi++) { |
| sp<Package> p = mOrderedPackages.itemAt(pi); |
| if (p == NULL || p->getTypes().size() == 0) { |
| // Empty, skip! |
| continue; |
| } |
| |
| if (mPackageType == System) { |
| p->movePrivateAttrs(); |
| } |
| |
| // This has no sense for packages being built as AppFeature (aka with a non-zero offset). |
| status_t err = p->applyPublicTypeOrder(); |
| if (err != NO_ERROR && firstError == NO_ERROR) { |
| firstError = err; |
| } |
| |
| // Generate attributes... |
| const size_t N = p->getOrderedTypes().size(); |
| size_t ti; |
| for (ti=0; ti<N; ti++) { |
| sp<Type> t = p->getOrderedTypes().itemAt(ti); |
| if (t == NULL) { |
| continue; |
| } |
| const size_t N = t->getOrderedConfigs().size(); |
| for (size_t ci=0; ci<N; ci++) { |
| sp<ConfigList> c = t->getOrderedConfigs().itemAt(ci); |
| if (c == NULL) { |
| continue; |
| } |
| const size_t N = c->getEntries().size(); |
| for (size_t ei=0; ei<N; ei++) { |
| sp<Entry> e = c->getEntries().valueAt(ei); |
| if (e == NULL) { |
| continue; |
|