2 // Copyright 2006 The Android Open Source Project
4 // Build resource files from raw assets.
7 #include "ResourceTable.h"
11 #include "ResourceFilter.h"
12 #include "ResourceIdCache.h"
13 #include "SdkConstants.h"
16 #include <androidfw/ResourceTypes.h>
17 #include <utils/ByteOrder.h>
18 #include <utils/TypeHelpers.h>
21 // SSIZE: mingw does not have signed size_t == ssize_t.
22 // STATUST: mingw does seem to redefine UNKNOWN_ERROR from our enum value, so a cast is necessary.
27 # define SSIZE(x) (signed size_t)x
28 # define STATUST(x) (status_t)x
31 // Set to true for noisy debug output.
32 static const bool kIsDebug = false;
34 #if PRINT_STRING_METRICS
35 static const bool kPrintStringMetrics = true;
37 static const bool kPrintStringMetrics = false;
40 static const char* kAttrPrivateType = "^attr-private";
42 status_t compileXmlFile(const Bundle* bundle,
43 const sp<AaptAssets>& assets,
44 const String16& resourceName,
45 const sp<AaptFile>& target,
49 sp<XMLNode> root = XMLNode::parse(target);
54 return compileXmlFile(bundle, assets, resourceName, root, target, table, options);
57 status_t compileXmlFile(const Bundle* bundle,
58 const sp<AaptAssets>& assets,
59 const String16& resourceName,
60 const sp<AaptFile>& target,
61 const sp<AaptFile>& outTarget,
65 sp<XMLNode> root = XMLNode::parse(target);
70 return compileXmlFile(bundle, assets, resourceName, root, outTarget, table, options);
73 status_t compileXmlFile(const Bundle* bundle,
74 const sp<AaptAssets>& assets,
75 const String16& resourceName,
76 const sp<XMLNode>& root,
77 const sp<AaptFile>& target,
81 if ((options&XML_COMPILE_STRIP_WHITESPACE) != 0) {
82 root->removeWhitespace(true, NULL);
83 } else if ((options&XML_COMPILE_COMPACT_WHITESPACE) != 0) {
84 root->removeWhitespace(false, NULL);
87 if ((options&XML_COMPILE_UTF8) != 0) {
91 if (table->processBundleFormat(bundle, resourceName, target, root) != NO_ERROR) {
95 bool hasErrors = false;
96 if ((options&XML_COMPILE_ASSIGN_ATTRIBUTE_IDS) != 0) {
97 status_t err = root->assignResourceIds(assets, table);
98 if (err != NO_ERROR) {
103 if ((options&XML_COMPILE_PARSE_VALUES) != 0) {
104 status_t err = root->parseValues(assets, table);
105 if (err != NO_ERROR) {
111 return UNKNOWN_ERROR;
114 if (table->modifyForCompat(bundle, resourceName, target, root) != NO_ERROR) {
115 return UNKNOWN_ERROR;
119 printf("Input XML Resource:\n");
122 status_t err = root->flatten(target,
123 (options&XML_COMPILE_STRIP_COMMENTS) != 0,
124 (options&XML_COMPILE_STRIP_RAW_VALUES) != 0);
125 if (err != NO_ERROR) {
130 printf("Output XML Resource:\n");
132 tree.setTo(target->getData(), target->getSize());
133 printXMLBlock(&tree);
136 target->setCompressionMethod(ZipEntry::kCompressDeflated);
143 const char16_t* name;
146 const char* description;
149 static const char16_t referenceArray[] =
150 { 'r', 'e', 'f', 'e', 'r', 'e', 'n', 'c', 'e' };
151 static const char16_t stringArray[] =
152 { 's', 't', 'r', 'i', 'n', 'g' };
153 static const char16_t integerArray[] =
154 { 'i', 'n', 't', 'e', 'g', 'e', 'r' };
155 static const char16_t booleanArray[] =
156 { 'b', 'o', 'o', 'l', 'e', 'a', 'n' };
157 static const char16_t colorArray[] =
158 { 'c', 'o', 'l', 'o', 'r' };
159 static const char16_t floatArray[] =
160 { 'f', 'l', 'o', 'a', 't' };
161 static const char16_t dimensionArray[] =
162 { 'd', 'i', 'm', 'e', 'n', 's', 'i', 'o', 'n' };
163 static const char16_t fractionArray[] =
164 { 'f', 'r', 'a', 'c', 't', 'i', 'o', 'n' };
165 static const char16_t enumArray[] =
166 { 'e', 'n', 'u', 'm' };
167 static const char16_t flagsArray[] =
168 { 'f', 'l', 'a', 'g', 's' };
170 static const flag_entry gFormatFlags[] = {
171 { referenceArray, sizeof(referenceArray)/2, ResTable_map::TYPE_REFERENCE,
172 "a reference to another resource, in the form \"<code>@[+][<i>package</i>:]<i>type</i>:<i>name</i></code>\"\n"
173 "or to a theme attribute in the form \"<code>?[<i>package</i>:][<i>type</i>:]<i>name</i></code>\"."},
174 { stringArray, sizeof(stringArray)/2, ResTable_map::TYPE_STRING,
175 "a string value, using '\\\\;' to escape characters such as '\\\\n' or '\\\\uxxxx' for a unicode character." },
176 { integerArray, sizeof(integerArray)/2, ResTable_map::TYPE_INTEGER,
177 "an integer value, such as \"<code>100</code>\"." },
178 { booleanArray, sizeof(booleanArray)/2, ResTable_map::TYPE_BOOLEAN,
179 "a boolean value, either \"<code>true</code>\" or \"<code>false</code>\"." },
180 { colorArray, sizeof(colorArray)/2, ResTable_map::TYPE_COLOR,
181 "a color value, in the form of \"<code>#<i>rgb</i></code>\", \"<code>#<i>argb</i></code>\",\n"
182 "\"<code>#<i>rrggbb</i></code>\", or \"<code>#<i>aarrggbb</i></code>\"." },
183 { floatArray, sizeof(floatArray)/2, ResTable_map::TYPE_FLOAT,
184 "a floating point value, such as \"<code>1.2</code>\"."},
185 { dimensionArray, sizeof(dimensionArray)/2, ResTable_map::TYPE_DIMENSION,
186 "a dimension value, which is a floating point number appended with a unit such as \"<code>14.5sp</code>\".\n"
187 "Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size),\n"
188 "in (inches), mm (millimeters)." },
189 { fractionArray, sizeof(fractionArray)/2, ResTable_map::TYPE_FRACTION,
190 "a fractional value, which is a floating point number appended with either % or %p, such as \"<code>14.5%</code>\".\n"
191 "The % suffix always means a percentage of the base size; the optional %p suffix provides a size relative to\n"
192 "some parent container." },
193 { enumArray, sizeof(enumArray)/2, ResTable_map::TYPE_ENUM, NULL },
194 { flagsArray, sizeof(flagsArray)/2, ResTable_map::TYPE_FLAGS, NULL },
198 static const char16_t suggestedArray[] = { 's', 'u', 'g', 'g', 'e', 's', 't', 'e', 'd' };
200 static const flag_entry l10nRequiredFlags[] = {
201 { suggestedArray, sizeof(suggestedArray)/2, ResTable_map::L10N_SUGGESTED, NULL },
205 static const char16_t nulStr[] = { 0 };
207 static uint32_t parse_flags(const char16_t* str, size_t len,
208 const flag_entry* flags, bool* outError = NULL)
210 while (len > 0 && isspace(*str)) {
214 while (len > 0 && isspace(str[len-1])) {
218 const char16_t* const end = str + len;
222 const char16_t* div = str;
223 while (div < end && *div != '|') {
227 const flag_entry* cur = flags;
229 if (strzcmp16(cur->name, cur->nameLen, str, div-str) == 0) {
237 if (outError) *outError = true;
241 str = div < end ? div+1 : div;
244 if (outError) *outError = false;
248 static String16 mayOrMust(int type, int flags)
250 if ((type&(~flags)) == 0) {
251 return String16("<p>Must");
254 return String16("<p>May");
257 static void appendTypeInfo(ResourceTable* outTable, const String16& pkg,
258 const String16& typeName, const String16& ident, int type,
259 const flag_entry* flags)
261 bool hadType = false;
262 while (flags->name) {
263 if ((type&flags->value) != 0 && flags->description != NULL) {
264 String16 fullMsg(mayOrMust(type, flags->value));
265 fullMsg.append(String16(" be "));
266 fullMsg.append(String16(flags->description));
267 outTable->appendTypeComment(pkg, typeName, ident, fullMsg);
272 if (hadType && (type&ResTable_map::TYPE_REFERENCE) == 0) {
273 outTable->appendTypeComment(pkg, typeName, ident,
274 String16("<p>This may also be a reference to a resource (in the form\n"
275 "\"<code>@[<i>package</i>:]<i>type</i>:<i>name</i></code>\") or\n"
276 "theme attribute (in the form\n"
277 "\"<code>?[<i>package</i>:][<i>type</i>:]<i>name</i></code>\")\n"
278 "containing a value of this type."));
282 struct PendingAttribute
284 const String16 myPackage;
285 const SourcePos sourcePos;
286 const bool appendComment;
293 PendingAttribute(String16 _package, const sp<AaptFile>& in,
294 ResXMLTree& block, bool _appendComment)
295 : myPackage(_package)
296 , sourcePos(in->getPrintableSource(), block.getLineNumber())
297 , appendComment(_appendComment)
298 , type(ResTable_map::TYPE_ANY)
304 status_t createIfNeeded(ResourceTable* outTable)
306 if (added || hasErrors) {
311 if (!outTable->makeAttribute(myPackage, ident, sourcePos, type, comment, appendComment)) {
313 return UNKNOWN_ERROR;
319 static status_t compileAttribute(const sp<AaptFile>& in,
321 const String16& myPackage,
322 ResourceTable* outTable,
323 String16* outIdent = NULL,
324 bool inStyleable = false)
326 PendingAttribute attr(myPackage, in, block, inStyleable);
328 const String16 attr16("attr");
329 const String16 id16("id");
331 // Attribute type constants.
332 const String16 enum16("enum");
333 const String16 flag16("flag");
335 ResXMLTree::event_code_t code;
339 ssize_t identIdx = block.indexOfAttribute(NULL, "name");
341 attr.ident = String16(block.getAttributeStringValue(identIdx, &len));
343 *outIdent = attr.ident;
346 attr.sourcePos.error("A 'name' attribute is required for <attr>\n");
347 attr.hasErrors = true;
350 attr.comment = String16(
351 block.getComment(&len) ? block.getComment(&len) : nulStr);
353 ssize_t typeIdx = block.indexOfAttribute(NULL, "format");
355 String16 typeStr = String16(block.getAttributeStringValue(typeIdx, &len));
356 attr.type = parse_flags(typeStr.string(), typeStr.size(), gFormatFlags);
357 if (attr.type == 0) {
358 attr.sourcePos.error("Tag <attr> 'format' attribute value \"%s\" not valid\n",
359 String8(typeStr).string());
360 attr.hasErrors = true;
362 attr.createIfNeeded(outTable);
363 } else if (!inStyleable) {
364 // Attribute definitions outside of styleables always define the
365 // attribute as a generic value.
366 attr.createIfNeeded(outTable);
369 //printf("Attribute %s: type=0x%08x\n", String8(attr.ident).string(), attr.type);
371 ssize_t minIdx = block.indexOfAttribute(NULL, "min");
373 String16 val = String16(block.getAttributeStringValue(minIdx, &len));
374 if (!ResTable::stringToInt(val.string(), val.size(), NULL)) {
375 attr.sourcePos.error("Tag <attr> 'min' attribute must be a number, not \"%s\"\n",
376 String8(val).string());
377 attr.hasErrors = true;
379 attr.createIfNeeded(outTable);
380 if (!attr.hasErrors) {
381 err = outTable->addBag(attr.sourcePos, myPackage, attr16, attr.ident,
382 String16(""), String16("^min"), String16(val), NULL, NULL);
383 if (err != NO_ERROR) {
384 attr.hasErrors = true;
389 ssize_t maxIdx = block.indexOfAttribute(NULL, "max");
391 String16 val = String16(block.getAttributeStringValue(maxIdx, &len));
392 if (!ResTable::stringToInt(val.string(), val.size(), NULL)) {
393 attr.sourcePos.error("Tag <attr> 'max' attribute must be a number, not \"%s\"\n",
394 String8(val).string());
395 attr.hasErrors = true;
397 attr.createIfNeeded(outTable);
398 if (!attr.hasErrors) {
399 err = outTable->addBag(attr.sourcePos, myPackage, attr16, attr.ident,
400 String16(""), String16("^max"), String16(val), NULL, NULL);
401 attr.hasErrors = true;
405 if ((minIdx >= 0 || maxIdx >= 0) && (attr.type&ResTable_map::TYPE_INTEGER) == 0) {
406 attr.sourcePos.error("Tag <attr> must have format=integer attribute if using max or min\n");
407 attr.hasErrors = true;
410 ssize_t l10nIdx = block.indexOfAttribute(NULL, "localization");
412 const char16_t* str = block.getAttributeStringValue(l10nIdx, &len);
414 uint32_t l10n_required = parse_flags(str, len, l10nRequiredFlags, &error);
416 attr.sourcePos.error("Tag <attr> 'localization' attribute value \"%s\" not valid\n",
417 String8(str).string());
418 attr.hasErrors = true;
420 attr.createIfNeeded(outTable);
421 if (!attr.hasErrors) {
423 sprintf(buf, "%d", l10n_required);
424 err = outTable->addBag(attr.sourcePos, myPackage, attr16, attr.ident,
425 String16(""), String16("^l10n"), String16(buf), NULL, NULL);
426 if (err != NO_ERROR) {
427 attr.hasErrors = true;
432 String16 enumOrFlagsComment;
434 while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) {
435 if (code == ResXMLTree::START_TAG) {
436 uint32_t localType = 0;
437 if (strcmp16(block.getElementName(&len), enum16.string()) == 0) {
438 localType = ResTable_map::TYPE_ENUM;
439 } else if (strcmp16(block.getElementName(&len), flag16.string()) == 0) {
440 localType = ResTable_map::TYPE_FLAGS;
442 SourcePos(in->getPrintableSource(), block.getLineNumber())
443 .error("Tag <%s> can not appear inside <attr>, only <enum> or <flag>\n",
444 String8(block.getElementName(&len)).string());
445 return UNKNOWN_ERROR;
448 attr.createIfNeeded(outTable);
450 if (attr.type == ResTable_map::TYPE_ANY) {
451 // No type was explicitly stated, so supplying enum tags
452 // implicitly creates an enum or flag.
456 if ((attr.type&(ResTable_map::TYPE_ENUM|ResTable_map::TYPE_FLAGS)) == 0) {
457 // Wasn't originally specified as an enum, so update its type.
458 attr.type |= localType;
459 if (!attr.hasErrors) {
461 sprintf(numberStr, "%d", attr.type);
462 err = outTable->addBag(SourcePos(in->getPrintableSource(), block.getLineNumber()),
463 myPackage, attr16, attr.ident, String16(""),
464 String16("^type"), String16(numberStr), NULL, NULL, true);
465 if (err != NO_ERROR) {
466 attr.hasErrors = true;
469 } else if ((uint32_t)(attr.type&(ResTable_map::TYPE_ENUM|ResTable_map::TYPE_FLAGS)) != localType) {
470 if (localType == ResTable_map::TYPE_ENUM) {
471 SourcePos(in->getPrintableSource(), block.getLineNumber())
472 .error("<enum> attribute can not be used inside a flags format\n");
473 attr.hasErrors = true;
475 SourcePos(in->getPrintableSource(), block.getLineNumber())
476 .error("<flag> attribute can not be used inside a enum format\n");
477 attr.hasErrors = true;
482 ssize_t itemIdentIdx = block.indexOfAttribute(NULL, "name");
483 if (itemIdentIdx >= 0) {
484 itemIdent = String16(block.getAttributeStringValue(itemIdentIdx, &len));
486 SourcePos(in->getPrintableSource(), block.getLineNumber())
487 .error("A 'name' attribute is required for <enum> or <flag>\n");
488 attr.hasErrors = true;
492 ssize_t valueIdx = block.indexOfAttribute(NULL, "value");
494 value = String16(block.getAttributeStringValue(valueIdx, &len));
496 SourcePos(in->getPrintableSource(), block.getLineNumber())
497 .error("A 'value' attribute is required for <enum> or <flag>\n");
498 attr.hasErrors = true;
500 if (!attr.hasErrors && !ResTable::stringToInt(value.string(), value.size(), NULL)) {
501 SourcePos(in->getPrintableSource(), block.getLineNumber())
502 .error("Tag <enum> or <flag> 'value' attribute must be a number,"
504 String8(value).string());
505 attr.hasErrors = true;
508 if (!attr.hasErrors) {
509 if (enumOrFlagsComment.size() == 0) {
510 enumOrFlagsComment.append(mayOrMust(attr.type,
511 ResTable_map::TYPE_ENUM|ResTable_map::TYPE_FLAGS));
512 enumOrFlagsComment.append((attr.type&ResTable_map::TYPE_ENUM)
513 ? String16(" be one of the following constant values.")
514 : String16(" be one or more (separated by '|') of the following constant values."));
515 enumOrFlagsComment.append(String16("</p>\n<table>\n"
516 "<colgroup align=\"left\" />\n"
517 "<colgroup align=\"left\" />\n"
518 "<colgroup align=\"left\" />\n"
519 "<tr><th>Constant</th><th>Value</th><th>Description</th></tr>"));
522 enumOrFlagsComment.append(String16("\n<tr><td><code>"));
523 enumOrFlagsComment.append(itemIdent);
524 enumOrFlagsComment.append(String16("</code></td><td>"));
525 enumOrFlagsComment.append(value);
526 enumOrFlagsComment.append(String16("</td><td>"));
527 if (block.getComment(&len)) {
528 enumOrFlagsComment.append(String16(block.getComment(&len)));
530 enumOrFlagsComment.append(String16("</td></tr>"));
532 err = outTable->addBag(SourcePos(in->getPrintableSource(), block.getLineNumber()),
534 attr16, attr.ident, String16(""),
535 itemIdent, value, NULL, NULL, false, true);
536 if (err != NO_ERROR) {
537 attr.hasErrors = true;
540 } else if (code == ResXMLTree::END_TAG) {
541 if (strcmp16(block.getElementName(&len), attr16.string()) == 0) {
544 if ((attr.type&ResTable_map::TYPE_ENUM) != 0) {
545 if (strcmp16(block.getElementName(&len), enum16.string()) != 0) {
546 SourcePos(in->getPrintableSource(), block.getLineNumber())
547 .error("Found tag </%s> where </enum> is expected\n",
548 String8(block.getElementName(&len)).string());
549 return UNKNOWN_ERROR;
552 if (strcmp16(block.getElementName(&len), flag16.string()) != 0) {
553 SourcePos(in->getPrintableSource(), block.getLineNumber())
554 .error("Found tag </%s> where </flag> is expected\n",
555 String8(block.getElementName(&len)).string());
556 return UNKNOWN_ERROR;
562 if (!attr.hasErrors && attr.added) {
563 appendTypeInfo(outTable, myPackage, attr16, attr.ident, attr.type, gFormatFlags);
566 if (!attr.hasErrors && enumOrFlagsComment.size() > 0) {
567 enumOrFlagsComment.append(String16("\n</table>"));
568 outTable->appendTypeComment(myPackage, attr16, attr.ident, enumOrFlagsComment);
575 bool localeIsDefined(const ResTable_config& config)
577 return config.locale == 0;
580 status_t parseAndAddBag(Bundle* bundle,
581 const sp<AaptFile>& in,
583 const ResTable_config& config,
584 const String16& myPackage,
585 const String16& curType,
586 const String16& ident,
587 const String16& parentIdent,
588 const String16& itemIdent,
591 const String16& /* product */,
592 PseudolocalizationMethod pseudolocalize,
593 const bool overwrite,
594 ResourceTable* outTable)
597 const String16 item16("item");
600 Vector<StringPool::entry_style_span> spans;
601 err = parseStyledString(bundle, in->getPrintableSource().string(),
602 block, item16, &str, &spans, isFormatted,
604 if (err != NO_ERROR) {
609 printf("Adding resource bag entry l=%c%c c=%c%c orien=%d d=%d "
610 " pid=%s, bag=%s, id=%s: %s\n",
611 config.language[0], config.language[1],
612 config.country[0], config.country[1],
613 config.orientation, config.density,
614 String8(parentIdent).string(),
615 String8(ident).string(),
616 String8(itemIdent).string(),
617 String8(str).string());
620 err = outTable->addBag(SourcePos(in->getPrintableSource(), block->getLineNumber()),
621 myPackage, curType, ident, parentIdent, itemIdent, str,
622 &spans, &config, overwrite, false, curFormat);
627 * Returns true if needle is one of the elements in the comma-separated list
628 * haystack, false otherwise.
630 bool isInProductList(const String16& needle, const String16& haystack) {
631 const char16_t *needle2 = needle.string();
632 const char16_t *haystack2 = haystack.string();
633 size_t needlesize = needle.size();
635 while (*haystack2 != '\0') {
636 if (strncmp16(haystack2, needle2, needlesize) == 0) {
637 if (haystack2[needlesize] == '\0' || haystack2[needlesize] == ',') {
642 while (*haystack2 != '\0' && *haystack2 != ',') {
645 if (*haystack2 == ',') {
654 * A simple container that holds a resource type and name. It is ordered first by type then
657 struct type_ident_pair_t {
661 type_ident_pair_t() { };
662 type_ident_pair_t(const String16& t, const String16& i) : type(t), ident(i) { }
663 type_ident_pair_t(const type_ident_pair_t& o) : type(o.type), ident(o.ident) { }
664 inline bool operator < (const type_ident_pair_t& o) const {
665 int cmp = compare_type(type, o.type);
668 } else if (cmp > 0) {
671 return strictly_order_type(ident, o.ident);
677 status_t parseAndAddEntry(Bundle* bundle,
678 const sp<AaptFile>& in,
680 const ResTable_config& config,
681 const String16& myPackage,
682 const String16& curType,
683 const String16& ident,
684 const String16& curTag,
688 const String16& product,
689 PseudolocalizationMethod pseudolocalize,
690 const bool overwrite,
691 KeyedVector<type_ident_pair_t, bool>* skippedResourceNames,
692 ResourceTable* outTable)
697 Vector<StringPool::entry_style_span> spans;
698 err = parseStyledString(bundle, in->getPrintableSource().string(), block,
699 curTag, &str, curIsStyled ? &spans : NULL,
700 isFormatted, pseudolocalize);
702 if (err < NO_ERROR) {
707 * If a product type was specified on the command line
708 * and also in the string, and the two are not the same,
709 * return without adding the string.
712 const char *bundleProduct = bundle->getProduct();
713 if (bundleProduct == NULL) {
717 if (product.size() != 0) {
719 * If the command-line-specified product is empty, only "default"
720 * matches. Other variants are skipped. This is so generation
721 * of the R.java file when the product is not known is predictable.
724 if (bundleProduct[0] == '\0') {
725 if (strcmp16(String16("default").string(), product.string()) != 0) {
727 * This string has a product other than 'default'. Do not add it,
728 * but record it so that if we do not see the same string with
729 * product 'default' or no product, then report an error.
731 skippedResourceNames->replaceValueFor(
732 type_ident_pair_t(curType, ident), true);
737 * The command-line product is not empty.
738 * If the product for this string is on the command-line list,
739 * it matches. "default" also matches, but only if nothing
740 * else has matched already.
743 if (isInProductList(product, String16(bundleProduct))) {
745 } else if (strcmp16(String16("default").string(), product.string()) == 0 &&
746 !outTable->hasBagOrEntry(myPackage, curType, ident, config)) {
755 printf("Adding resource entry l=%c%c c=%c%c orien=%d d=%d id=%s: %s\n",
756 config.language[0], config.language[1],
757 config.country[0], config.country[1],
758 config.orientation, config.density,
759 String8(ident).string(), String8(str).string());
762 err = outTable->addEntry(SourcePos(in->getPrintableSource(), block->getLineNumber()),
763 myPackage, curType, ident, str, &spans, &config,
764 false, curFormat, overwrite);
769 status_t compileResourceFile(Bundle* bundle,
770 const sp<AaptAssets>& assets,
771 const sp<AaptFile>& in,
772 const ResTable_config& defParams,
773 const bool overwrite,
774 ResourceTable* outTable)
777 status_t err = parseXMLResource(in, &block, false, true);
778 if (err != NO_ERROR) {
783 const String16 resources16("resources");
785 // Identifier declaration tags.
786 const String16 declare_styleable16("declare-styleable");
787 const String16 attr16("attr");
789 // Data creation organizational tags.
790 const String16 string16("string");
791 const String16 drawable16("drawable");
792 const String16 color16("color");
793 const String16 bool16("bool");
794 const String16 integer16("integer");
795 const String16 dimen16("dimen");
796 const String16 fraction16("fraction");
797 const String16 style16("style");
798 const String16 plurals16("plurals");
799 const String16 array16("array");
800 const String16 string_array16("string-array");
801 const String16 integer_array16("integer-array");
802 const String16 public16("public");
803 const String16 public_padding16("public-padding");
804 const String16 private_symbols16("private-symbols");
805 const String16 java_symbol16("java-symbol");
806 const String16 add_resource16("add-resource");
807 const String16 skip16("skip");
808 const String16 eat_comment16("eat-comment");
810 // Data creation tags.
811 const String16 bag16("bag");
812 const String16 item16("item");
814 // Attribute type constants.
815 const String16 enum16("enum");
818 const String16 other16("other");
819 const String16 quantityOther16("^other");
820 const String16 zero16("zero");
821 const String16 quantityZero16("^zero");
822 const String16 one16("one");
823 const String16 quantityOne16("^one");
824 const String16 two16("two");
825 const String16 quantityTwo16("^two");
826 const String16 few16("few");
827 const String16 quantityFew16("^few");
828 const String16 many16("many");
829 const String16 quantityMany16("^many");
831 // useful attribute names and special values
832 const String16 name16("name");
833 const String16 translatable16("translatable");
834 const String16 formatted16("formatted");
835 const String16 false16("false");
837 const String16 myPackage(assets->getPackage());
839 bool hasErrors = false;
841 bool fileIsTranslatable = true;
842 if (strstr(in->getPrintableSource().string(), "donottranslate") != NULL) {
843 fileIsTranslatable = false;
846 DefaultKeyedVector<String16, uint32_t> nextPublicId(0);
848 // Stores the resource names that were skipped. Typically this happens when
849 // AAPT is invoked without a product specified and a resource has no
850 // 'default' product attribute.
851 KeyedVector<type_ident_pair_t, bool> skippedResourceNames;
853 ResXMLTree::event_code_t code;
856 } while (code == ResXMLTree::START_NAMESPACE);
859 if (code != ResXMLTree::START_TAG) {
860 SourcePos(in->getPrintableSource(), block.getLineNumber()).error(
861 "No start tag found\n");
862 return UNKNOWN_ERROR;
864 if (strcmp16(block.getElementName(&len), resources16.string()) != 0) {
865 SourcePos(in->getPrintableSource(), block.getLineNumber()).error(
866 "Invalid start tag %s\n", String8(block.getElementName(&len)).string());
867 return UNKNOWN_ERROR;
870 ResTable_config curParams(defParams);
872 ResTable_config pseudoParams(curParams);
873 pseudoParams.language[0] = 'e';
874 pseudoParams.language[1] = 'n';
875 pseudoParams.country[0] = 'X';
876 pseudoParams.country[1] = 'A';
878 ResTable_config pseudoBidiParams(curParams);
879 pseudoBidiParams.language[0] = 'a';
880 pseudoBidiParams.language[1] = 'r';
881 pseudoBidiParams.country[0] = 'X';
882 pseudoBidiParams.country[1] = 'B';
884 // We should skip resources for pseudolocales if they were
885 // already added automatically. This is a fix for a transition period when
886 // manually pseudolocalized resources may be expected.
887 // TODO: remove this check after next SDK version release.
888 if ((bundle->getPseudolocalize() & PSEUDO_ACCENTED &&
889 curParams.locale == pseudoParams.locale) ||
890 (bundle->getPseudolocalize() & PSEUDO_BIDI &&
891 curParams.locale == pseudoBidiParams.locale)) {
892 SourcePos(in->getPrintableSource(), 0).warning(
893 "Resource file %s is skipped as pseudolocalization"
894 " was done automatically.",
895 in->getPrintableSource().string());
899 while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) {
900 if (code == ResXMLTree::START_TAG) {
901 const String16* curTag = NULL;
904 int32_t curFormat = ResTable_map::TYPE_ANY;
905 bool curIsBag = false;
906 bool curIsBagReplaceOnOverwrite = false;
907 bool curIsStyled = false;
908 bool curIsPseudolocalizable = false;
909 bool curIsFormatted = fileIsTranslatable;
910 bool localHasErrors = false;
912 if (strcmp16(block.getElementName(&len), skip16.string()) == 0) {
913 while ((code=block.next()) != ResXMLTree::END_DOCUMENT
914 && code != ResXMLTree::BAD_DOCUMENT) {
915 if (code == ResXMLTree::END_TAG) {
916 if (strcmp16(block.getElementName(&len), skip16.string()) == 0) {
923 } else if (strcmp16(block.getElementName(&len), eat_comment16.string()) == 0) {
924 while ((code=block.next()) != ResXMLTree::END_DOCUMENT
925 && code != ResXMLTree::BAD_DOCUMENT) {
926 if (code == ResXMLTree::END_TAG) {
927 if (strcmp16(block.getElementName(&len), eat_comment16.string()) == 0) {
934 } else if (strcmp16(block.getElementName(&len), public16.string()) == 0) {
935 SourcePos srcPos(in->getPrintableSource(), block.getLineNumber());
938 ssize_t typeIdx = block.indexOfAttribute(NULL, "type");
940 srcPos.error("A 'type' attribute is required for <public>\n");
941 hasErrors = localHasErrors = true;
943 type = String16(block.getAttributeStringValue(typeIdx, &len));
946 ssize_t nameIdx = block.indexOfAttribute(NULL, "name");
948 srcPos.error("A 'name' attribute is required for <public>\n");
949 hasErrors = localHasErrors = true;
951 name = String16(block.getAttributeStringValue(nameIdx, &len));
954 ssize_t identIdx = block.indexOfAttribute(NULL, "id");
956 const char16_t* identStr = block.getAttributeStringValue(identIdx, &len);
957 Res_value identValue;
958 if (!ResTable::stringToInt(identStr, len, &identValue)) {
959 srcPos.error("Given 'id' attribute is not an integer: %s\n",
960 String8(block.getAttributeStringValue(identIdx, &len)).string());
961 hasErrors = localHasErrors = true;
963 ident = identValue.data;
964 nextPublicId.replaceValueFor(type, ident+1);
966 } else if (nextPublicId.indexOfKey(type) < 0) {
967 srcPos.error("No 'id' attribute supplied <public>,"
968 " and no previous id defined in this file.\n");
969 hasErrors = localHasErrors = true;
970 } else if (!localHasErrors) {
971 ident = nextPublicId.valueFor(type);
972 nextPublicId.replaceValueFor(type, ident+1);
975 if (!localHasErrors) {
976 err = outTable->addPublic(srcPos, myPackage, type, name, ident);
977 if (err < NO_ERROR) {
978 hasErrors = localHasErrors = true;
981 if (!localHasErrors) {
982 sp<AaptSymbols> symbols = assets->getSymbolsFor(String8("R"));
983 if (symbols != NULL) {
984 symbols = symbols->addNestedSymbol(String8(type), srcPos);
986 if (symbols != NULL) {
987 symbols->makeSymbolPublic(String8(name), srcPos);
989 block.getComment(&len) ? block.getComment(&len) : nulStr);
990 symbols->appendComment(String8(name), comment, srcPos);
992 srcPos.error("Unable to create symbols!\n");
993 hasErrors = localHasErrors = true;
997 while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) {
998 if (code == ResXMLTree::END_TAG) {
999 if (strcmp16(block.getElementName(&len), public16.string()) == 0) {
1006 } else if (strcmp16(block.getElementName(&len), public_padding16.string()) == 0) {
1007 SourcePos srcPos(in->getPrintableSource(), block.getLineNumber());
1010 ssize_t typeIdx = block.indexOfAttribute(NULL, "type");
1012 srcPos.error("A 'type' attribute is required for <public-padding>\n");
1013 hasErrors = localHasErrors = true;
1015 type = String16(block.getAttributeStringValue(typeIdx, &len));
1018 ssize_t nameIdx = block.indexOfAttribute(NULL, "name");
1020 srcPos.error("A 'name' attribute is required for <public-padding>\n");
1021 hasErrors = localHasErrors = true;
1023 name = String16(block.getAttributeStringValue(nameIdx, &len));
1026 ssize_t startIdx = block.indexOfAttribute(NULL, "start");
1027 if (startIdx >= 0) {
1028 const char16_t* startStr = block.getAttributeStringValue(startIdx, &len);
1029 Res_value startValue;
1030 if (!ResTable::stringToInt(startStr, len, &startValue)) {
1031 srcPos.error("Given 'start' attribute is not an integer: %s\n",
1032 String8(block.getAttributeStringValue(startIdx, &len)).string());
1033 hasErrors = localHasErrors = true;
1035 start = startValue.data;
1037 } else if (nextPublicId.indexOfKey(type) < 0) {
1038 srcPos.error("No 'start' attribute supplied <public-padding>,"
1039 " and no previous id defined in this file.\n");
1040 hasErrors = localHasErrors = true;
1041 } else if (!localHasErrors) {
1042 start = nextPublicId.valueFor(type);
1046 ssize_t endIdx = block.indexOfAttribute(NULL, "end");
1048 const char16_t* endStr = block.getAttributeStringValue(endIdx, &len);
1050 if (!ResTable::stringToInt(endStr, len, &endValue)) {
1051 srcPos.error("Given 'end' attribute is not an integer: %s\n",
1052 String8(block.getAttributeStringValue(endIdx, &len)).string());
1053 hasErrors = localHasErrors = true;
1055 end = endValue.data;
1058 srcPos.error("No 'end' attribute supplied <public-padding>\n");
1059 hasErrors = localHasErrors = true;
1063 nextPublicId.replaceValueFor(type, end+1);
1065 srcPos.error("Padding start '%ul' is after end '%ul'\n",
1067 hasErrors = localHasErrors = true;
1071 block.getComment(&len) ? block.getComment(&len) : nulStr);
1072 for (uint32_t curIdent=start; curIdent<=end; curIdent++) {
1073 if (localHasErrors) {
1076 String16 curName(name);
1078 sprintf(buf, "%d", (int)(end-curIdent+1));
1079 curName.append(String16(buf));
1081 err = outTable->addEntry(srcPos, myPackage, type, curName,
1082 String16("padding"), NULL, &curParams, false,
1083 ResTable_map::TYPE_STRING, overwrite);
1084 if (err < NO_ERROR) {
1085 hasErrors = localHasErrors = true;
1088 err = outTable->addPublic(srcPos, myPackage, type,
1090 if (err < NO_ERROR) {
1091 hasErrors = localHasErrors = true;
1094 sp<AaptSymbols> symbols = assets->getSymbolsFor(String8("R"));
1095 if (symbols != NULL) {
1096 symbols = symbols->addNestedSymbol(String8(type), srcPos);
1098 if (symbols != NULL) {
1099 symbols->makeSymbolPublic(String8(curName), srcPos);
1100 symbols->appendComment(String8(curName), comment, srcPos);
1102 srcPos.error("Unable to create symbols!\n");
1103 hasErrors = localHasErrors = true;
1107 while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) {
1108 if (code == ResXMLTree::END_TAG) {
1109 if (strcmp16(block.getElementName(&len), public_padding16.string()) == 0) {
1116 } else if (strcmp16(block.getElementName(&len), private_symbols16.string()) == 0) {
1118 ssize_t pkgIdx = block.indexOfAttribute(NULL, "package");
1120 SourcePos(in->getPrintableSource(), block.getLineNumber()).error(
1121 "A 'package' attribute is required for <private-symbols>\n");
1122 hasErrors = localHasErrors = true;
1124 pkg = String16(block.getAttributeStringValue(pkgIdx, &len));
1125 if (!localHasErrors) {
1126 SourcePos(in->getPrintableSource(), block.getLineNumber()).warning(
1127 "<private-symbols> is deprecated. Use the command line flag "
1128 "--private-symbols instead.\n");
1129 if (assets->havePrivateSymbols()) {
1130 SourcePos(in->getPrintableSource(), block.getLineNumber()).warning(
1131 "private symbol package already specified. Ignoring...\n");
1133 assets->setSymbolsPrivatePackage(String8(pkg));
1137 while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) {
1138 if (code == ResXMLTree::END_TAG) {
1139 if (strcmp16(block.getElementName(&len), private_symbols16.string()) == 0) {
1146 } else if (strcmp16(block.getElementName(&len), java_symbol16.string()) == 0) {
1147 SourcePos srcPos(in->getPrintableSource(), block.getLineNumber());
1150 ssize_t typeIdx = block.indexOfAttribute(NULL, "type");
1152 srcPos.error("A 'type' attribute is required for <public>\n");
1153 hasErrors = localHasErrors = true;
1155 type = String16(block.getAttributeStringValue(typeIdx, &len));
1158 ssize_t nameIdx = block.indexOfAttribute(NULL, "name");
1160 srcPos.error("A 'name' attribute is required for <public>\n");
1161 hasErrors = localHasErrors = true;
1163 name = String16(block.getAttributeStringValue(nameIdx, &len));
1165 sp<AaptSymbols> symbols = assets->getJavaSymbolsFor(String8("R"));
1166 if (symbols != NULL) {
1167 symbols = symbols->addNestedSymbol(String8(type), srcPos);
1169 if (symbols != NULL) {
1170 symbols->makeSymbolJavaSymbol(String8(name), srcPos);
1172 block.getComment(&len) ? block.getComment(&len) : nulStr);
1173 symbols->appendComment(String8(name), comment, srcPos);
1175 srcPos.error("Unable to create symbols!\n");
1176 hasErrors = localHasErrors = true;
1179 while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) {
1180 if (code == ResXMLTree::END_TAG) {
1181 if (strcmp16(block.getElementName(&len), java_symbol16.string()) == 0) {
1189 } else if (strcmp16(block.getElementName(&len), add_resource16.string()) == 0) {
1190 SourcePos srcPos(in->getPrintableSource(), block.getLineNumber());
1193 ssize_t typeIdx = block.indexOfAttribute(NULL, "type");
1195 srcPos.error("A 'type' attribute is required for <add-resource>\n");
1196 hasErrors = localHasErrors = true;
1198 typeName = String16(block.getAttributeStringValue(typeIdx, &len));
1201 ssize_t nameIdx = block.indexOfAttribute(NULL, "name");
1203 srcPos.error("A 'name' attribute is required for <add-resource>\n");
1204 hasErrors = localHasErrors = true;
1206 name = String16(block.getAttributeStringValue(nameIdx, &len));
1208 outTable->canAddEntry(srcPos, myPackage, typeName, name);
1210 while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) {
1211 if (code == ResXMLTree::END_TAG) {
1212 if (strcmp16(block.getElementName(&len), add_resource16.string()) == 0) {
1219 } else if (strcmp16(block.getElementName(&len), declare_styleable16.string()) == 0) {
1220 SourcePos srcPos(in->getPrintableSource(), block.getLineNumber());
1223 ssize_t identIdx = block.indexOfAttribute(NULL, "name");
1225 srcPos.error("A 'name' attribute is required for <declare-styleable>\n");
1226 hasErrors = localHasErrors = true;
1228 ident = String16(block.getAttributeStringValue(identIdx, &len));
1230 sp<AaptSymbols> symbols = assets->getSymbolsFor(String8("R"));
1231 if (!localHasErrors) {
1232 if (symbols != NULL) {
1233 symbols = symbols->addNestedSymbol(String8("styleable"), srcPos);
1235 sp<AaptSymbols> styleSymbols = symbols;
1236 if (symbols != NULL) {
1237 symbols = symbols->addNestedSymbol(String8(ident), srcPos);
1239 if (symbols == NULL) {
1240 srcPos.error("Unable to create symbols!\n");
1241 return UNKNOWN_ERROR;
1245 block.getComment(&len) ? block.getComment(&len) : nulStr);
1246 styleSymbols->appendComment(String8(ident), comment, srcPos);
1251 while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) {
1252 if (code == ResXMLTree::START_TAG) {
1253 if (strcmp16(block.getElementName(&len), skip16.string()) == 0) {
1254 while ((code=block.next()) != ResXMLTree::END_DOCUMENT
1255 && code != ResXMLTree::BAD_DOCUMENT) {
1256 if (code == ResXMLTree::END_TAG) {
1257 if (strcmp16(block.getElementName(&len), skip16.string()) == 0) {
1263 } else if (strcmp16(block.getElementName(&len), eat_comment16.string()) == 0) {
1264 while ((code=block.next()) != ResXMLTree::END_DOCUMENT
1265 && code != ResXMLTree::BAD_DOCUMENT) {
1266 if (code == ResXMLTree::END_TAG) {
1267 if (strcmp16(block.getElementName(&len), eat_comment16.string()) == 0) {
1273 } else if (strcmp16(block.getElementName(&len), attr16.string()) != 0) {
1274 SourcePos(in->getPrintableSource(), block.getLineNumber()).error(
1275 "Tag <%s> can not appear inside <declare-styleable>, only <attr>\n",
1276 String8(block.getElementName(&len)).string());
1277 return UNKNOWN_ERROR;
1281 block.getComment(&len) ? block.getComment(&len) : nulStr);
1283 err = compileAttribute(in, block, myPackage, outTable, &itemIdent, true);
1284 if (err != NO_ERROR) {
1285 hasErrors = localHasErrors = true;
1288 if (symbols != NULL) {
1289 SourcePos srcPos(String8(in->getPrintableSource()), block.getLineNumber());
1290 symbols->addSymbol(String8(itemIdent), 0, srcPos);
1291 symbols->appendComment(String8(itemIdent), comment, srcPos);
1292 //printf("Attribute %s comment: %s\n", String8(itemIdent).string(),
1293 // String8(comment).string());
1295 } else if (code == ResXMLTree::END_TAG) {
1296 if (strcmp16(block.getElementName(&len), declare_styleable16.string()) == 0) {
1300 SourcePos(in->getPrintableSource(), block.getLineNumber()).error(
1301 "Found tag </%s> where </attr> is expected\n",
1302 String8(block.getElementName(&len)).string());
1303 return UNKNOWN_ERROR;
1308 } else if (strcmp16(block.getElementName(&len), attr16.string()) == 0) {
1309 err = compileAttribute(in, block, myPackage, outTable, NULL);
1310 if (err != NO_ERROR) {
1315 } else if (strcmp16(block.getElementName(&len), item16.string()) == 0) {
1317 ssize_t attri = block.indexOfAttribute(NULL, "type");
1319 curType = String16(block.getAttributeStringValue(attri, &len));
1320 ssize_t nameIdx = block.indexOfAttribute(NULL, "name");
1322 curName = String16(block.getAttributeStringValue(nameIdx, &len));
1324 ssize_t formatIdx = block.indexOfAttribute(NULL, "format");
1325 if (formatIdx >= 0) {
1326 String16 formatStr = String16(block.getAttributeStringValue(
1328 curFormat = parse_flags(formatStr.string(), formatStr.size(),
1330 if (curFormat == 0) {
1331 SourcePos(in->getPrintableSource(), block.getLineNumber()).error(
1332 "Tag <item> 'format' attribute value \"%s\" not valid\n",
1333 String8(formatStr).string());
1334 hasErrors = localHasErrors = true;
1338 SourcePos(in->getPrintableSource(), block.getLineNumber()).error(
1339 "A 'type' attribute is required for <item>\n");
1340 hasErrors = localHasErrors = true;
1343 } else if (strcmp16(block.getElementName(&len), string16.string()) == 0) {
1344 // Note the existence and locale of every string we process
1345 char rawLocale[RESTABLE_MAX_LOCALE_LEN];
1346 curParams.getBcp47Locale(rawLocale);
1347 String8 locale(rawLocale);
1349 String16 translatable;
1352 size_t n = block.getAttributeCount();
1353 for (size_t i = 0; i < n; i++) {
1355 const char16_t* attr = block.getAttributeName(i, &length);
1356 if (strcmp16(attr, name16.string()) == 0) {
1357 name.setTo(block.getAttributeStringValue(i, &length));
1358 } else if (strcmp16(attr, translatable16.string()) == 0) {
1359 translatable.setTo(block.getAttributeStringValue(i, &length));
1360 } else if (strcmp16(attr, formatted16.string()) == 0) {
1361 formatted.setTo(block.getAttributeStringValue(i, &length));
1365 if (name.size() > 0) {
1366 if (locale.size() == 0) {
1367 outTable->addDefaultLocalization(name);
1369 if (translatable == false16) {
1370 curIsFormatted = false;
1371 // Untranslatable strings must only exist in the default [empty] locale
1372 if (locale.size() > 0) {
1373 SourcePos(in->getPrintableSource(), block.getLineNumber()).warning(
1374 "string '%s' marked untranslatable but exists in locale '%s'\n",
1375 String8(name).string(),
1377 // hasErrors = localHasErrors = true;
1379 // Intentionally empty block:
1381 // Don't add untranslatable strings to the localization table; that
1382 // way if we later see localizations of them, they'll be flagged as
1383 // having no default translation.
1386 outTable->addLocalization(
1389 SourcePos(in->getPrintableSource(), block.getLineNumber()));
1392 if (formatted == false16) {
1393 curIsFormatted = false;
1399 curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_STRING;
1401 curIsPseudolocalizable = fileIsTranslatable && (translatable != false16);
1402 } else if (strcmp16(block.getElementName(&len), drawable16.string()) == 0) {
1403 curTag = &drawable16;
1404 curType = drawable16;
1405 curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_COLOR;
1406 } else if (strcmp16(block.getElementName(&len), color16.string()) == 0) {
1409 curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_COLOR;
1410 } else if (strcmp16(block.getElementName(&len), bool16.string()) == 0) {
1413 curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_BOOLEAN;
1414 } else if (strcmp16(block.getElementName(&len), integer16.string()) == 0) {
1415 curTag = &integer16;
1416 curType = integer16;
1417 curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_INTEGER;
1418 } else if (strcmp16(block.getElementName(&len), dimen16.string()) == 0) {
1421 curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_DIMENSION;
1422 } else if (strcmp16(block.getElementName(&len), fraction16.string()) == 0) {
1423 curTag = &fraction16;
1424 curType = fraction16;
1425 curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_FRACTION;
1426 } else if (strcmp16(block.getElementName(&len), bag16.string()) == 0) {
1429 ssize_t attri = block.indexOfAttribute(NULL, "type");
1431 curType = String16(block.getAttributeStringValue(attri, &len));
1433 SourcePos(in->getPrintableSource(), block.getLineNumber()).error(
1434 "A 'type' attribute is required for <bag>\n");
1435 hasErrors = localHasErrors = true;
1437 } else if (strcmp16(block.getElementName(&len), style16.string()) == 0) {
1441 } else if (strcmp16(block.getElementName(&len), plurals16.string()) == 0) {
1442 curTag = &plurals16;
1443 curType = plurals16;
1445 curIsPseudolocalizable = fileIsTranslatable;
1446 } else if (strcmp16(block.getElementName(&len), array16.string()) == 0) {
1450 curIsBagReplaceOnOverwrite = true;
1451 ssize_t formatIdx = block.indexOfAttribute(NULL, "format");
1452 if (formatIdx >= 0) {
1453 String16 formatStr = String16(block.getAttributeStringValue(
1455 curFormat = parse_flags(formatStr.string(), formatStr.size(),
1457 if (curFormat == 0) {
1458 SourcePos(in->getPrintableSource(), block.getLineNumber()).error(
1459 "Tag <array> 'format' attribute value \"%s\" not valid\n",
1460 String8(formatStr).string());
1461 hasErrors = localHasErrors = true;
1464 } else if (strcmp16(block.getElementName(&len), string_array16.string()) == 0) {
1465 // Check whether these strings need valid formats.
1466 // (simplified form of what string16 does above)
1467 bool isTranslatable = false;
1468 size_t n = block.getAttributeCount();
1470 // Pseudolocalizable by default, unless this string array isn't
1472 for (size_t i = 0; i < n; i++) {
1474 const char16_t* attr = block.getAttributeName(i, &length);
1475 if (strcmp16(attr, formatted16.string()) == 0) {
1476 const char16_t* value = block.getAttributeStringValue(i, &length);
1477 if (strcmp16(value, false16.string()) == 0) {
1478 curIsFormatted = false;
1480 } else if (strcmp16(attr, translatable16.string()) == 0) {
1481 const char16_t* value = block.getAttributeStringValue(i, &length);
1482 if (strcmp16(value, false16.string()) == 0) {
1483 isTranslatable = false;
1488 curTag = &string_array16;
1490 curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_STRING;
1492 curIsBagReplaceOnOverwrite = true;
1493 curIsPseudolocalizable = isTranslatable && fileIsTranslatable;
1494 } else if (strcmp16(block.getElementName(&len), integer_array16.string()) == 0) {
1495 curTag = &integer_array16;
1497 curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_INTEGER;
1499 curIsBagReplaceOnOverwrite = true;
1501 SourcePos(in->getPrintableSource(), block.getLineNumber()).error(
1502 "Found tag %s where item is expected\n",
1503 String8(block.getElementName(&len)).string());
1504 return UNKNOWN_ERROR;
1508 ssize_t identIdx = block.indexOfAttribute(NULL, "name");
1509 if (identIdx >= 0) {
1510 ident = String16(block.getAttributeStringValue(identIdx, &len));
1512 SourcePos(in->getPrintableSource(), block.getLineNumber()).error(
1513 "A 'name' attribute is required for <%s>\n",
1514 String8(*curTag).string());
1515 hasErrors = localHasErrors = true;
1519 identIdx = block.indexOfAttribute(NULL, "product");
1520 if (identIdx >= 0) {
1521 product = String16(block.getAttributeStringValue(identIdx, &len));
1524 String16 comment(block.getComment(&len) ? block.getComment(&len) : nulStr);
1527 // Figure out the parent of this bag...
1528 String16 parentIdent;
1529 ssize_t parentIdentIdx = block.indexOfAttribute(NULL, "parent");
1530 if (parentIdentIdx >= 0) {
1531 parentIdent = String16(block.getAttributeStringValue(parentIdentIdx, &len));
1533 ssize_t sep = ident.findLast('.');
1535 parentIdent.setTo(ident, sep);
1539 if (!localHasErrors) {
1540 err = outTable->startBag(SourcePos(in->getPrintableSource(),
1541 block.getLineNumber()), myPackage, curType, ident,
1542 parentIdent, &curParams,
1543 overwrite, curIsBagReplaceOnOverwrite);
1544 if (err != NO_ERROR) {
1545 hasErrors = localHasErrors = true;
1549 ssize_t elmIndex = 0;
1550 char elmIndexStr[14];
1551 while ((code=block.next()) != ResXMLTree::END_DOCUMENT
1552 && code != ResXMLTree::BAD_DOCUMENT) {
1554 if (code == ResXMLTree::START_TAG) {
1555 if (strcmp16(block.getElementName(&len), item16.string()) != 0) {
1556 SourcePos(in->getPrintableSource(), block.getLineNumber()).error(
1557 "Tag <%s> can not appear inside <%s>, only <item>\n",
1558 String8(block.getElementName(&len)).string(),
1559 String8(*curTag).string());
1560 return UNKNOWN_ERROR;
1564 if (curType == array16) {
1565 sprintf(elmIndexStr, "^index_%d", (int)elmIndex++);
1566 itemIdent = String16(elmIndexStr);
1567 } else if (curType == plurals16) {
1568 ssize_t itemIdentIdx = block.indexOfAttribute(NULL, "quantity");
1569 if (itemIdentIdx >= 0) {
1570 String16 quantity16(block.getAttributeStringValue(itemIdentIdx, &len));
1571 if (quantity16 == other16) {
1572 itemIdent = quantityOther16;
1574 else if (quantity16 == zero16) {
1575 itemIdent = quantityZero16;
1577 else if (quantity16 == one16) {
1578 itemIdent = quantityOne16;
1580 else if (quantity16 == two16) {
1581 itemIdent = quantityTwo16;
1583 else if (quantity16 == few16) {
1584 itemIdent = quantityFew16;
1586 else if (quantity16 == many16) {
1587 itemIdent = quantityMany16;
1590 SourcePos(in->getPrintableSource(), block.getLineNumber()).error(
1591 "Illegal 'quantity' attribute is <item> inside <plurals>\n");
1592 hasErrors = localHasErrors = true;
1595 SourcePos(in->getPrintableSource(), block.getLineNumber()).error(
1596 "A 'quantity' attribute is required for <item> inside <plurals>\n");
1597 hasErrors = localHasErrors = true;
1600 ssize_t itemIdentIdx = block.indexOfAttribute(NULL, "name");
1601 if (itemIdentIdx >= 0) {
1602 itemIdent = String16(block.getAttributeStringValue(itemIdentIdx, &len));
1604 SourcePos(in->getPrintableSource(), block.getLineNumber()).error(
1605 "A 'name' attribute is required for <item>\n");
1606 hasErrors = localHasErrors = true;
1610 ResXMLParser::ResXMLPosition parserPosition;
1611 block.getPosition(&parserPosition);
1613 err = parseAndAddBag(bundle, in, &block, curParams, myPackage, curType,
1614 ident, parentIdent, itemIdent, curFormat, curIsFormatted,
1615 product, NO_PSEUDOLOCALIZATION, overwrite, outTable);
1616 if (err == NO_ERROR) {
1617 if (curIsPseudolocalizable && localeIsDefined(curParams)
1618 && bundle->getPseudolocalize() > 0) {
1619 // pseudolocalize here
1620 if ((PSEUDO_ACCENTED & bundle->getPseudolocalize()) ==
1622 block.setPosition(parserPosition);
1623 err = parseAndAddBag(bundle, in, &block, pseudoParams, myPackage,
1624 curType, ident, parentIdent, itemIdent, curFormat,
1625 curIsFormatted, product, PSEUDO_ACCENTED,
1626 overwrite, outTable);
1628 if ((PSEUDO_BIDI & bundle->getPseudolocalize()) ==
1630 block.setPosition(parserPosition);
1631 err = parseAndAddBag(bundle, in, &block, pseudoBidiParams, myPackage,
1632 curType, ident, parentIdent, itemIdent, curFormat,
1633 curIsFormatted, product, PSEUDO_BIDI,
1634 overwrite, outTable);
1638 if (err != NO_ERROR) {
1639 hasErrors = localHasErrors = true;
1641 } else if (code == ResXMLTree::END_TAG) {
1642 if (strcmp16(block.getElementName(&len), curTag->string()) != 0) {
1643 SourcePos(in->getPrintableSource(), block.getLineNumber()).error(
1644 "Found tag </%s> where </%s> is expected\n",
1645 String8(block.getElementName(&len)).string(),
1646 String8(*curTag).string());
1647 return UNKNOWN_ERROR;
1653 ResXMLParser::ResXMLPosition parserPosition;
1654 block.getPosition(&parserPosition);
1656 err = parseAndAddEntry(bundle, in, &block, curParams, myPackage, curType, ident,
1657 *curTag, curIsStyled, curFormat, curIsFormatted,
1658 product, NO_PSEUDOLOCALIZATION, overwrite, &skippedResourceNames, outTable);
1660 if (err < NO_ERROR) { // Why err < NO_ERROR instead of err != NO_ERROR?
1661 hasErrors = localHasErrors = true;
1663 else if (err == NO_ERROR) {
1664 if (curType == string16 && !curParams.language[0] && !curParams.country[0]) {
1665 outTable->addDefaultLocalization(curName);
1667 if (curIsPseudolocalizable && localeIsDefined(curParams)
1668 && bundle->getPseudolocalize() > 0) {
1669 // pseudolocalize here
1670 if ((PSEUDO_ACCENTED & bundle->getPseudolocalize()) ==
1672 block.setPosition(parserPosition);
1673 err = parseAndAddEntry(bundle, in, &block, pseudoParams, myPackage, curType,
1674 ident, *curTag, curIsStyled, curFormat,
1675 curIsFormatted, product,
1676 PSEUDO_ACCENTED, overwrite, &skippedResourceNames, outTable);
1678 if ((PSEUDO_BIDI & bundle->getPseudolocalize()) ==
1680 block.setPosition(parserPosition);
1681 err = parseAndAddEntry(bundle, in, &block, pseudoBidiParams,
1682 myPackage, curType, ident, *curTag, curIsStyled, curFormat,
1683 curIsFormatted, product,
1684 PSEUDO_BIDI, overwrite, &skippedResourceNames, outTable);
1686 if (err != NO_ERROR) {
1687 hasErrors = localHasErrors = true;
1694 if (comment.size() > 0) {
1695 printf("Comment for @%s:%s/%s: %s\n", String8(myPackage).string(),
1696 String8(curType).string(), String8(ident).string(),
1697 String8(comment).string());
1700 if (!localHasErrors) {
1701 outTable->appendComment(myPackage, curType, ident, comment, false);
1704 else if (code == ResXMLTree::END_TAG) {
1705 if (strcmp16(block.getElementName(&len), resources16.string()) != 0) {
1706 SourcePos(in->getPrintableSource(), block.getLineNumber()).error(
1707 "Unexpected end tag %s\n", String8(block.getElementName(&len)).string());
1708 return UNKNOWN_ERROR;
1711 else if (code == ResXMLTree::START_NAMESPACE || code == ResXMLTree::END_NAMESPACE) {
1713 else if (code == ResXMLTree::TEXT) {
1714 if (isWhitespace(block.getText(&len))) {
1717 SourcePos(in->getPrintableSource(), block.getLineNumber()).error(
1718 "Found text \"%s\" where item tag is expected\n",
1719 String8(block.getText(&len)).string());
1720 return UNKNOWN_ERROR;
1724 // For every resource defined, there must be exist one variant with a product attribute
1725 // set to 'default' (or no product attribute at all).
1726 // We check to see that for every resource that was ignored because of a mismatched
1727 // product attribute, some product variant of that resource was processed.
1728 for (size_t i = 0; i < skippedResourceNames.size(); i++) {
1729 if (skippedResourceNames[i]) {
1730 const type_ident_pair_t& p = skippedResourceNames.keyAt(i);
1731 if (!outTable->hasBagOrEntry(myPackage, p.type, p.ident)) {
1732 const char* bundleProduct =
1733 (bundle->getProduct() == NULL) ? "" : bundle->getProduct();
1734 fprintf(stderr, "In resource file %s: %s\n",
1735 in->getPrintableSource().string(),
1736 curParams.toString().string());
1738 fprintf(stderr, "\t%s '%s' does not match product %s.\n"
1739 "\tYou may have forgotten to include a 'default' product variant"
1740 " of the resource.\n",
1741 String8(p.type).string(), String8(p.ident).string(),
1742 bundleProduct[0] == 0 ? "default" : bundleProduct);
1743 return UNKNOWN_ERROR;
1748 return hasErrors ? STATUST(UNKNOWN_ERROR) : NO_ERROR;
1751 ResourceTable::ResourceTable(Bundle* bundle, const String16& assetsPackage, ResourceTable::PackageType type)
1752 : mAssetsPackage(assetsPackage)
1753 , mPackageType(type)
1758 ssize_t packageId = -1;
1759 switch (mPackageType) {
1777 sp<Package> package = new Package(mAssetsPackage, packageId);
1778 mPackages.add(assetsPackage, package);
1779 mOrderedPackages.add(package);
1781 // Every resource table always has one first entry, the bag attributes.
1782 const SourcePos unknown(String8("????"), 0);
1783 getType(mAssetsPackage, String16("attr"), unknown);
1786 static uint32_t findLargestTypeIdForPackage(const ResTable& table, const String16& packageName) {
1787 const size_t basePackageCount = table.getBasePackageCount();
1788 for (size_t i = 0; i < basePackageCount; i++) {
1789 if (packageName == table.getBasePackageName(i)) {
1790 return table.getLastTypeIdForPackage(i);
1796 status_t ResourceTable::addIncludedResources(Bundle* bundle, const sp<AaptAssets>& assets)
1798 status_t err = assets->buildIncludedResources(bundle);
1799 if (err != NO_ERROR) {
1804 mTypeIdOffset = findLargestTypeIdForPackage(assets->getIncludedResources(), mAssetsPackage);
1806 const String8& featureAfter = bundle->getFeatureAfterPackage();
1807 if (!featureAfter.isEmpty()) {
1808 AssetManager featureAssetManager;
1809 if (!featureAssetManager.addAssetPath(featureAfter, NULL)) {
1810 fprintf(stderr, "ERROR: Feature package '%s' not found.\n",
1811 featureAfter.string());
1812 return UNKNOWN_ERROR;
1815 const ResTable& featureTable = featureAssetManager.getResources(false);
1816 mTypeIdOffset = std::max(mTypeIdOffset,
1817 findLargestTypeIdForPackage(featureTable, mAssetsPackage));
1823 status_t ResourceTable::addPublic(const SourcePos& sourcePos,
1824 const String16& package,
1825 const String16& type,
1826 const String16& name,
1827 const uint32_t ident)
1829 uint32_t rid = mAssets->getIncludedResources()
1830 .identifierForName(name.string(), name.size(),
1831 type.string(), type.size(),
1832 package.string(), package.size());
1834 sourcePos.error("Error declaring public resource %s/%s for included package %s\n",
1835 String8(type).string(), String8(name).string(),
1836 String8(package).string());
1837 return UNKNOWN_ERROR;
1840 sp<Type> t = getType(package, type, sourcePos);
1842 return UNKNOWN_ERROR;
1844 return t->addPublic(sourcePos, name, ident);
1847 status_t ResourceTable::addEntry(const SourcePos& sourcePos,
1848 const String16& package,
1849 const String16& type,
1850 const String16& name,
1851 const String16& value,
1852 const Vector<StringPool::entry_style_span>* style,
1853 const ResTable_config* params,
1854 const bool doSetIndex,
1855 const int32_t format,
1856 const bool overwrite)
1858 uint32_t rid = mAssets->getIncludedResources()
1859 .identifierForName(name.string(), name.size(),
1860 type.string(), type.size(),
1861 package.string(), package.size());
1863 sourcePos.error("Resource entry %s/%s is already defined in package %s.",
1864 String8(type).string(), String8(name).string(), String8(package).string());
1865 return UNKNOWN_ERROR;
1868 sp<Entry> e = getEntry(package, type, name, sourcePos, overwrite,
1869 params, doSetIndex);
1871 return UNKNOWN_ERROR;
1873 status_t err = e->setItem(sourcePos, value, style, format, overwrite);
1874 if (err == NO_ERROR) {
1880 status_t ResourceTable::startBag(const SourcePos& sourcePos,
1881 const String16& package,
1882 const String16& type,
1883 const String16& name,
1884 const String16& bagParent,
1885 const ResTable_config* params,
1887 bool replace, bool /* isId */)
1889 status_t result = NO_ERROR;
1891 // Check for adding entries in other packages... for now we do
1892 // nothing. We need to do the right thing here to support skinning.
1893 uint32_t rid = mAssets->getIncludedResources()
1894 .identifierForName(name.string(), name.size(),
1895 type.string(), type.size(),
1896 package.string(), package.size());
1898 sourcePos.error("Resource entry %s/%s is already defined in package %s.",
1899 String8(type).string(), String8(name).string(), String8(package).string());
1900 return UNKNOWN_ERROR;
1903 if (overlay && !mBundle->getAutoAddOverlay() && !hasBagOrEntry(package, type, name)) {
1904 bool canAdd = false;
1905 sp<Package> p = mPackages.valueFor(package);
1907 sp<Type> t = p->getTypes().valueFor(type);
1909 if (t->getCanAddEntries().indexOf(name) >= 0) {
1915 sourcePos.error("Resource does not already exist in overlay at '%s'; use <add-resource> to add.\n",
1916 String8(name).string());
1917 return UNKNOWN_ERROR;
1920 sp<Entry> e = getEntry(package, type, name, sourcePos, overlay, params);
1922 return UNKNOWN_ERROR;
1925 // If a parent is explicitly specified, set it.
1926 if (bagParent.size() > 0) {
1927 e->setParent(bagParent);
1930 if ((result = e->makeItABag(sourcePos)) != NO_ERROR) {
1934 if (overlay && replace) {
1935 return e->emptyBag(sourcePos);
1940 status_t ResourceTable::addBag(const SourcePos& sourcePos,
1941 const String16& package,
1942 const String16& type,
1943 const String16& name,
1944 const String16& bagParent,
1945 const String16& bagKey,
1946 const String16& value,
1947 const Vector<StringPool::entry_style_span>* style,
1948 const ResTable_config* params,
1949 bool replace, bool isId, const int32_t format)
1951 // Check for adding entries in other packages... for now we do
1952 // nothing. We need to do the right thing here to support skinning.
1953 uint32_t rid = mAssets->getIncludedResources()
1954 .identifierForName(name.string(), name.size(),
1955 type.string(), type.size(),
1956 package.string(), package.size());
1962 if (name == String16("left")) {
1963 printf("Adding bag left: file=%s, line=%d, type=%s\n",
1964 sourcePos.file.striing(), sourcePos.line, String8(type).string());
1967 sp<Entry> e = getEntry(package, type, name, sourcePos, replace, params);
1969 return UNKNOWN_ERROR;
1972 // If a parent is explicitly specified, set it.
1973 if (bagParent.size() > 0) {
1974 e->setParent(bagParent);
1977 const bool first = e->getBag().indexOfKey(bagKey) < 0;
1978 status_t err = e->addToBag(sourcePos, bagKey, value, style, replace, isId, format);
1979 if (err == NO_ERROR && first) {
1985 bool ResourceTable::hasBagOrEntry(const String16& package,
1986 const String16& type,
1987 const String16& name) const
1989 // First look for this in the included resources...
1990 uint32_t rid = mAssets->getIncludedResources()
1991 .identifierForName(name.string(), name.size(),
1992 type.string(), type.size(),
1993 package.string(), package.size());
1998 sp<Package> p = mPackages.valueFor(package);
2000 sp<Type> t = p->getTypes().valueFor(type);
2002 sp<ConfigList> c = t->getConfigs().valueFor(name);
2003 if (c != NULL) return true;
2010 bool ResourceTable::hasBagOrEntry(const String16& package,
2011 const String16& type,
2012 const String16& name,
2013 const ResTable_config& config) const
2015 // First look for this in the included resources...
2016 uint32_t rid = mAssets->getIncludedResources()
2017 .identifierForName(name.string(), name.size(),
2018 type.string(), type.size(),
2019 package.string(), package.size());
2024 sp<Package> p = mPackages.valueFor(package);
2026 sp<Type> t = p->getTypes().valueFor(type);
2028 sp<ConfigList> c = t->getConfigs().valueFor(name);
2030 sp<Entry> e = c->getEntries().valueFor(config);
2041 bool ResourceTable::hasBagOrEntry(const String16& ref,
2042 const String16* defType,
2043 const String16* defPackage)
2045 String16 package, type, name;
2046 if (!ResTable::expandResourceRef(ref.string(), ref.size(), &package, &type, &name,
2047 defType, defPackage ? defPackage:&mAssetsPackage, NULL)) {
2050 return hasBagOrEntry(package, type, name);
2053 bool ResourceTable::appendComment(const String16& package,
2054 const String16& type,
2055 const String16& name,
2056 const String16& comment,
2059 if (comment.size() <= 0) {
2063 sp<Package> p = mPackages.valueFor(package);
2065 sp<Type> t = p->getTypes().valueFor(type);
2067 sp<ConfigList> c = t->getConfigs().valueFor(name);
2069 c->appendComment(comment, onlyIfEmpty);
2077 bool ResourceTable::appendTypeComment(const String16& package,
2078 const String16& type,
2079 const String16& name,
2080 const String16& comment)
2082 if (comment.size() <= 0) {
2086 sp<Package> p = mPackages.valueFor(package);
2088 sp<Type> t = p->getTypes().valueFor(type);
2090 sp<ConfigList> c = t->getConfigs().valueFor(name);
2092 c->appendTypeComment(comment);
2100 bool ResourceTable::makeAttribute(const String16& package,
2101 const String16& name,
2102 const SourcePos& source,
2104 const String16& comment,
2105 bool shouldAppendComment) {
2106 const String16 attr16("attr");
2108 // First look for this in the included resources...
2109 uint32_t rid = mAssets->getIncludedResources()
2110 .identifierForName(name.string(), name.size(),
2111 attr16.string(), attr16.size(),
2112 package.string(), package.size());
2114 source.error("Attribute \"%s\" has already been defined", String8(name).string());
2118 sp<ResourceTable::Entry> entry = getEntry(package, attr16, name, source, false);
2119 if (entry == NULL) {
2120 source.error("Failed to create entry attr/%s", String8(name).string());
2124 if (entry->makeItABag(source) != NO_ERROR) {
2128 const String16 formatKey16("^type");
2129 const String16 formatValue16(String8::format("%d", format));
2131 ssize_t idx = entry->getBag().indexOfKey(formatKey16);
2133 // We have already set a format for this attribute, check if they are different.
2134 // We allow duplicate attribute definitions so long as they are identical.
2135 // This is to ensure inter-operation with libraries that define the same generic attribute.
2136 const Item& formatItem = entry->getBag().valueAt(idx);
2137 if ((format & (ResTable_map::TYPE_ENUM | ResTable_map::TYPE_FLAGS)) ||
2138 formatItem.value != formatValue16) {
2139 source.error("Attribute \"%s\" already defined with incompatible format.\n"
2140 "%s:%d: Original attribute defined here.",
2141 String8(name).string(), formatItem.sourcePos.file.string(),
2142 formatItem.sourcePos.line);
2146 entry->addToBag(source, formatKey16, formatValue16);
2147 // Increment the number of resources we have. This is used to determine if we should
2148 // even generate a resource table.
2151 appendComment(package, attr16, name, comment, shouldAppendComment);
2155 void ResourceTable::canAddEntry(const SourcePos& pos,
2156 const String16& package, const String16& type, const String16& name)
2158 sp<Type> t = getType(package, type, pos);
2160 t->canAddEntry(name);
2164 size_t ResourceTable::size() const {
2165 return mPackages.size();
2168 size_t ResourceTable::numLocalResources() const {
2172 bool ResourceTable::hasResources() const {
2173 return mNumLocal > 0;
2176 sp<AaptFile> ResourceTable::flatten(Bundle* bundle, const sp<const ResourceFilter>& filter,
2179 sp<AaptFile> data = new AaptFile(String8(), AaptGroupEntry(), String8());
2180 status_t err = flatten(bundle, filter, data, isBase);
2181 return err == NO_ERROR ? data : NULL;
2184 inline uint32_t ResourceTable::getResId(const sp<Package>& p,
2188 return makeResId(p->getAssignedId(), t->getIndex(), nameId);
2191 uint32_t ResourceTable::getResId(const String16& package,
2192 const String16& type,
2193 const String16& name,
2194 bool onlyPublic) const
2196 uint32_t id = ResourceIdCache::lookup(package, type, name, onlyPublic);
2197 if (id != 0) return id; // cache hit
2199 // First look for this in the included resources...
2200 uint32_t specFlags = 0;
2201 uint32_t rid = mAssets->getIncludedResources()
2202 .identifierForName(name.string(), name.size(),
2203 type.string(), type.size(),
2204 package.string(), package.size(),
2208 if ((specFlags & ResTable_typeSpec::SPEC_PUBLIC) == 0) {
2213 return ResourceIdCache::store(package, type, name, onlyPublic, rid);
2216 sp<Package> p = mPackages.valueFor(package);
2217 if (p == NULL) return 0;
2218 sp<Type> t = p->getTypes().valueFor(type);
2219 if (t == NULL) return 0;
2220 sp<ConfigList> c = t->getConfigs().valueFor(name);
2222 if (type != String16("attr")) {
2225 t = p->getTypes().valueFor(String16(kAttrPrivateType));
2226 if (t == NULL) return 0;
2227 c = t->getConfigs().valueFor(name);
2228 if (c == NULL) return 0;
2230 int32_t ei = c->getEntryIndex();
2231 if (ei < 0) return 0;
2233 return ResourceIdCache::store(package, type, name, onlyPublic,
2234 getResId(p, t, ei));
2237 uint32_t ResourceTable::getResId(const String16& ref,
2238 const String16* defType,
2239 const String16* defPackage,
2240 const char** outErrorMsg,
2241 bool onlyPublic) const
2243 String16 package, type, name;
2244 bool refOnlyPublic = true;
2245 if (!ResTable::expandResourceRef(
2246 ref.string(), ref.size(), &package, &type, &name,
2247 defType, defPackage ? defPackage:&mAssetsPackage,
2248 outErrorMsg, &refOnlyPublic)) {
2250 printf("Expanding resource: ref=%s\n", String8(ref).string());
2251 printf("Expanding resource: defType=%s\n",
2252 defType ? String8(*defType).string() : "NULL");
2253 printf("Expanding resource: defPackage=%s\n",
2254 defPackage ? String8(*defPackage).string() : "NULL");
2255 printf("Expanding resource: ref=%s\n", String8(ref).string());
2256 printf("Expanded resource: p=%s, t=%s, n=%s, res=0\n",
2257 String8(package).string(), String8(type).string(),
2258 String8(name).string());
2262 uint32_t res = getResId(package, type, name, onlyPublic && refOnlyPublic);
2264 printf("Expanded resource: p=%s, t=%s, n=%s, res=%d\n",
2265 String8(package).string(), String8(type).string(),
2266 String8(name).string(), res);
2270 *outErrorMsg = "No resource found that matches the given name";
2275 bool ResourceTable::isValidResourceName(const String16& s)
2277 const char16_t* p = s.string();
2280 if ((*p >= 'a' && *p <= 'z')
2281 || (*p >= 'A' && *p <= 'Z')
2283 || (!first && *p >= '0' && *p <= '9')) {
2293 bool ResourceTable::stringToValue(Res_value* outValue, StringPool* pool,
2294 const String16& str,
2295 bool preserveSpaces, bool coerceType,
2297 const Vector<StringPool::entry_style_span>* style,
2298 String16* outStr, void* accessorCookie,
2299 uint32_t attrType, const String8* configTypeName,
2300 const ConfigDescription* config)
2305 if (style == NULL || style->size() == 0) {
2306 // Text is not styled so it can be any type... let's figure it out.
2307 res = mAssets->getIncludedResources()
2308 .stringToValue(outValue, &finalStr, str.string(), str.size(), preserveSpaces,
2309 coerceType, attrID, NULL, &mAssetsPackage, this,
2310 accessorCookie, attrType);
2312 // Styled text can only be a string, and while collecting the style
2313 // information we have already processed that string!
2314 outValue->size = sizeof(Res_value);
2316 outValue->dataType = outValue->TYPE_STRING;
2325 if (outValue->dataType == outValue->TYPE_STRING) {
2326 // Should do better merging styles.
2329 if (config != NULL) {
2330 configStr = config->toString();
2332 configStr = "(null)";
2335 printf("Adding to pool string style #%zu config %s: %s\n",
2336 style != NULL ? style->size() : 0U,
2337 configStr.string(), String8(finalStr).string());
2339 if (style != NULL && style->size() > 0) {
2340 outValue->data = pool->add(finalStr, *style, configTypeName, config);
2342 outValue->data = pool->add(finalStr, true, configTypeName, config);
2345 // Caller will fill this in later.
2358 uint32_t ResourceTable::getCustomResource(
2359 const String16& package, const String16& type, const String16& name) const
2361 //printf("getCustomResource: %s %s %s\n", String8(package).string(),
2362 // String8(type).string(), String8(name).string());
2363 sp<Package> p = mPackages.valueFor(package);
2364 if (p == NULL) return 0;
2365 sp<Type> t = p->getTypes().valueFor(type);
2366 if (t == NULL) return 0;
2367 sp<ConfigList> c = t->getConfigs().valueFor(name);
2369 if (type != String16("attr")) {
2372 t = p->getTypes().valueFor(String16(kAttrPrivateType));
2373 if (t == NULL) return 0;
2374 c = t->getConfigs().valueFor(name);
2375 if (c == NULL) return 0;
2377 int32_t ei = c->getEntryIndex();
2378 if (ei < 0) return 0;
2379 return getResId(p, t, ei);
2382 uint32_t ResourceTable::getCustomResourceWithCreation(
2383 const String16& package, const String16& type, const String16& name,
2384 const bool createIfNotFound)
2386 uint32_t resId = getCustomResource(package, type, name);
2387 if (resId != 0 || !createIfNotFound) {
2391 if (mAssetsPackage != package) {
2392 mCurrentXmlPos.error("creating resource for external package %s: %s/%s.",
2393 String8(package).string(), String8(type).string(), String8(name).string());
2394 if (package == String16("android")) {
2395 mCurrentXmlPos.printf("did you mean to use @+id instead of @+android:id?");
2400 String16 value("false");
2401 status_t status = addEntry(mCurrentXmlPos, package, type, name, value, NULL, NULL, true);
2402 if (status == NO_ERROR) {
2403 resId = getResId(package, type, name);
2409 uint32_t ResourceTable::getRemappedPackage(uint32_t origPackage) const
2414 bool ResourceTable::getAttributeType(uint32_t attrID, uint32_t* outType)
2416 //printf("getAttributeType #%08x\n", attrID);
2418 if (getItemValue(attrID, ResTable_map::ATTR_TYPE, &value)) {
2419 //printf("getAttributeType #%08x (%s): #%08x\n", attrID,
2420 // String8(getEntry(attrID)->getName()).string(), value.data);
2421 *outType = value.data;
2427 bool ResourceTable::getAttributeMin(uint32_t attrID, uint32_t* outMin)
2429 //printf("getAttributeMin #%08x\n", attrID);
2431 if (getItemValue(attrID, ResTable_map::ATTR_MIN, &value)) {
2432 *outMin = value.data;
2438 bool ResourceTable::getAttributeMax(uint32_t attrID, uint32_t* outMax)
2440 //printf("getAttributeMax #%08x\n", attrID);
2442 if (getItemValue(attrID, ResTable_map::ATTR_MAX, &value)) {
2443 *outMax = value.data;
2449 uint32_t ResourceTable::getAttributeL10N(uint32_t attrID)
2451 //printf("getAttributeL10N #%08x\n", attrID);
2453 if (getItemValue(attrID, ResTable_map::ATTR_L10N, &value)) {
2456 return ResTable_map::L10N_NOT_REQUIRED;
2459 bool ResourceTable::getLocalizationSetting()
2461 return mBundle->getRequireLocalization();
2464 void ResourceTable::reportError(void* accessorCookie, const char* fmt, ...)
2466 if (accessorCookie != NULL && fmt != NULL) {
2467 AccessorCookie* ac = (AccessorCookie*)accessorCookie;
2472 retval = vsnprintf(buf, sizeof(buf), fmt, ap);
2474 ac->sourcePos.error("Error: %s (at '%s' with value '%s').\n",
2475 buf, ac->attr.string(), ac->value.string());
2479 bool ResourceTable::getAttributeKeys(
2480 uint32_t attrID, Vector<String16>* outKeys)
2482 sp<const Entry> e = getEntry(attrID);
2484 const size_t N = e->getBag().size();
2485 for (size_t i=0; i<N; i++) {
2486 const String16& key = e->getBag().keyAt(i);
2487 if (key.size() > 0 && key.string()[0] != '^') {
2496 bool ResourceTable::getAttributeEnum(
2497 uint32_t attrID, const char16_t* name, size_t nameLen,
2498 Res_value* outValue)
2500 //printf("getAttributeEnum #%08x %s\n", attrID, String8(name, nameLen).string());
2501 String16 nameStr(name, nameLen);
2502 sp<const Entry> e = getEntry(attrID);
2504 const size_t N = e->getBag().size();
2505 for (size_t i=0; i<N; i++) {
2506 //printf("Comparing %s to %s\n", String8(name, nameLen).string(),
2507 // String8(e->getBag().keyAt(i)).string());
2508 if (e->getBag().keyAt(i) == nameStr) {
2509 return getItemValue(attrID, e->getBag().valueAt(i).bagKeyId, outValue);
2516 bool ResourceTable::getAttributeFlags(
2517 uint32_t attrID, const char16_t* name, size_t nameLen,
2518 Res_value* outValue)
2520 outValue->dataType = Res_value::TYPE_INT_HEX;
2523 //printf("getAttributeFlags #%08x %s\n", attrID, String8(name, nameLen).string());
2524 String16 nameStr(name, nameLen);
2525 sp<const Entry> e = getEntry(attrID);
2527 const size_t N = e->getBag().size();
2529 const char16_t* end = name + nameLen;
2530 const char16_t* pos = name;
2532 const char16_t* start = pos;
2533 while (pos < end && *pos != '|') {
2537 String16 nameStr(start, pos-start);
2539 for (i=0; i<N; i++) {
2540 //printf("Comparing \"%s\" to \"%s\"\n", String8(nameStr).string(),
2541 // String8(e->getBag().keyAt(i)).string());
2542 if (e->getBag().keyAt(i) == nameStr) {
2544 bool got = getItemValue(attrID, e->getBag().valueAt(i).bagKeyId, &val);
2548 //printf("Got value: 0x%08x\n", val.data);
2549 outValue->data |= val.data;
2555 // Didn't find this flag identifier.
2566 status_t ResourceTable::assignResourceIds()
2568 const size_t N = mOrderedPackages.size();
2570 status_t firstError = NO_ERROR;
2572 // First generate all bag attributes and assign indices.
2573 for (pi=0; pi<N; pi++) {
2574 sp<Package> p = mOrderedPackages.itemAt(pi);
2575 if (p == NULL || p->getTypes().size() == 0) {
2580 if (mPackageType == System) {
2581 p->movePrivateAttrs();
2584 // This has no sense for packages being built as AppFeature (aka with a non-zero offset).
2585 status_t err = p->applyPublicTypeOrder();
2586 if (err != NO_ERROR && firstError == NO_ERROR) {
2590 // Generate attributes...
2591 const size_t N = p->getOrderedTypes().size();
2593 for (ti=0; ti<N; ti++) {
2594 sp<Type> t = p->getOrderedTypes().itemAt(ti);
2598 const size_t N = t->getOrderedConfigs().size();
2599 for (size_t ci=0; ci<N; ci++) {
2600 sp<ConfigList> c = t->getOrderedConfigs().itemAt(ci);
2604 const size_t N = c->getEntries().size();
2605 for (size_t ei=0; ei<N; ei++) {
2606 sp<Entry> e = c->getEntries().valueAt(ei);
2610 status_t err = e->generateAttributes(this, p->getName());
2611 if (err != NO_ERROR && firstError == NO_ERROR) {
2618 uint32_t typeIdOffset = 0;
2619 if (mPackageType == AppFeature && p->getName() == mAssetsPackage) {
2620 typeIdOffset = mTypeIdOffset;
2623 const SourcePos unknown(String8("????"), 0);
2624 sp<Type> attr = p->getType(String16("attr"), unknown);
2626 // Assign indices...
2627 const size_t typeCount = p->getOrderedTypes().size();
2628 for (size_t ti = 0; ti < typeCount; ti++) {
2629 sp<Type> t = p->getOrderedTypes().itemAt(ti);
2634 err = t->applyPublicEntryOrder();
2635 if (err != NO_ERROR && firstError == NO_ERROR) {
2639 const size_t N = t->getOrderedConfigs().size();
2640 t->setIndex(ti + 1 + typeIdOffset);
2642 LOG_ALWAYS_FATAL_IF(ti == 0 && attr != t,
2643 "First type is not attr!");
2645 for (size_t ei=0; ei<N; ei++) {
2646 sp<ConfigList> c = t->getOrderedConfigs().itemAt(ei);
2650 c->setEntryIndex(ei);
2655 // Assign resource IDs to keys in bags...
2656 for (size_t ti = 0; ti < typeCount; ti++) {
2657 sp<Type> t = p->getOrderedTypes().itemAt(ti);
2662 const size_t N = t->getOrderedConfigs().size();
2663 for (size_t ci=0; ci<N; ci++) {
2664 sp<ConfigList> c = t->getOrderedConfigs().itemAt(ci);
2668 //printf("Ordered config #%d: %p\n", ci, c.get());
2669 const size_t N = c->getEntries().size();
2670 for (size_t ei=0; ei<N; ei++) {
2671 sp<Entry> e = c->getEntries().valueAt(ei);
2675 status_t err = e->assignResourceIds(this, p->getName());
2676 if (err != NO_ERROR && firstError == NO_ERROR) {
2686 status_t ResourceTable::addSymbols(const sp<AaptSymbols>& outSymbols,
2687 bool skipSymbolsWithoutDefaultLocalization) {
2688 const size_t N = mOrderedPackages.size();
2689 const String8 defaultLocale;
2690 const String16 stringType("string");
2693 for (pi=0; pi<N; pi++) {
2694 sp<Package> p = mOrderedPackages.itemAt(pi);
2695 if (p->getTypes().size() == 0) {
2700 const size_t N = p->getOrderedTypes().size();
2703 for (ti=0; ti<N; ti++) {
2704 sp<Type> t = p->getOrderedTypes().itemAt(ti);
2709 const size_t N = t->getOrderedConfigs().size();
2710 sp<AaptSymbols> typeSymbols;
2711 if (t->getName() == String16(kAttrPrivateType)) {
2712 typeSymbols = outSymbols->addNestedSymbol(String8("attr"), t->getPos());
2714 typeSymbols = outSymbols->addNestedSymbol(String8(t->getName()), t->getPos());
2717 if (typeSymbols == NULL) {
2718 return UNKNOWN_ERROR;
2721 for (size_t ci=0; ci<N; ci++) {
2722 sp<ConfigList> c = t->getOrderedConfigs().itemAt(ci);
2726 uint32_t rid = getResId(p, t, ci);
2728 return UNKNOWN_ERROR;
2730 if (Res_GETPACKAGE(rid) + 1 == p->getAssignedId()) {
2732 if (skipSymbolsWithoutDefaultLocalization &&
2733 t->getName() == stringType) {
2735 // Don't generate symbols for strings without a default localization.
2736 if (mHasDefaultLocalization.find(c->getName())
2737 == mHasDefaultLocalization.end()) {
2738 // printf("Skip symbol [%08x] %s\n", rid,
2739 // String8(c->getName()).string());
2744 typeSymbols->addSymbol(String8(c->getName()), rid, c->getPos());
2746 String16 comment(c->getComment());
2747 typeSymbols->appendComment(String8(c->getName()), comment, c->getPos());
2748 //printf("Type symbol [%08x] %s comment: %s\n", rid,
2749 // String8(c->getName()).string(), String8(comment).string());
2750 comment = c->getTypeComment();
2751 typeSymbols->appendTypeComment(String8(c->getName()), comment);
2761 ResourceTable::addLocalization(const String16& name, const String8& locale, const SourcePos& src)
2763 mLocalizations[name][locale] = src;
2767 ResourceTable::addDefaultLocalization(const String16& name)
2769 mHasDefaultLocalization.insert(name);
2774 * Flag various sorts of localization problems. '+' indicates checks already implemented;
2775 * '-' indicates checks that will be implemented in the future.
2777 * + A localized string for which no default-locale version exists => warning
2778 * + A string for which no version in an explicitly-requested locale exists => warning
2779 * + A localized translation of an translateable="false" string => warning
2780 * - A localized string not provided in every locale used by the table
2783 ResourceTable::validateLocalizations(void)
2785 status_t err = NO_ERROR;
2786 const String8 defaultLocale;
2788 // For all strings...
2789 for (const auto& nameIter : mLocalizations) {
2790 const std::map<String8, SourcePos>& configSrcMap = nameIter.second;
2792 // Look for strings with no default localization
2793 if (configSrcMap.count(defaultLocale) == 0) {
2794 SourcePos().warning("string '%s' has no default translation.",
2795 String8(nameIter.first).string());
2796 if (mBundle->getVerbose()) {
2797 for (const auto& locale : configSrcMap) {
2798 locale.second.printf("locale %s found", locale.first.string());
2801 // !!! TODO: throw an error here in some circumstances
2804 // Check that all requested localizations are present for this string
2805 if (mBundle->getConfigurations().size() > 0 && mBundle->getRequireLocalization()) {
2806 const char* allConfigs = mBundle->getConfigurations().string();
2807 const char* start = allConfigs;
2810 std::set<String8> missingConfigs;
2811 AaptLocaleValue locale;
2814 comma = strchr(start, ',');
2815 if (comma != NULL) {
2816 config.setTo(start, comma - start);
2819 config.setTo(start);
2822 if (!locale.initFromFilterString(config)) {
2826 // don't bother with the pseudolocale "en_XA" or "ar_XB"
2827 if (config != "en_XA" && config != "ar_XB") {
2828 if (configSrcMap.find(config) == configSrcMap.end()) {
2829 // okay, no specific localization found. it's possible that we are
2830 // requiring a specific regional localization [e.g. de_DE] but there is an
2831 // available string in the generic language localization [e.g. de];
2832 // consider that string to have fulfilled the localization requirement.
2833 String8 region(config.string(), 2);
2834 if (configSrcMap.find(region) == configSrcMap.end() &&
2835 configSrcMap.count(defaultLocale) == 0) {
2836 missingConfigs.insert(config);
2840 } while (comma != NULL);
2842 if (!missingConfigs.empty()) {
2844 for (const auto& iter : missingConfigs) {
2845 configStr.appendFormat(" %s", iter.string());
2847 SourcePos().warning("string '%s' is missing %u required localizations:%s",
2848 String8(nameIter.first).string(),
2849 (unsigned int)missingConfigs.size(),
2850 configStr.string());
2858 status_t ResourceTable::flatten(Bundle* bundle, const sp<const ResourceFilter>& filter,
2859 const sp<AaptFile>& dest,
2862 const ConfigDescription nullConfig;
2864 const size_t N = mOrderedPackages.size();
2867 const static String16 mipmap16("mipmap");
2869 bool useUTF8 = !bundle->getUTF16StringsOption();
2871 // The libraries this table references.
2872 Vector<sp<Package> > libraryPackages;
2873 const ResTable& table = mAssets->getIncludedResources();
2874 const size_t basePackageCount = table.getBasePackageCount();
2875 for (size_t i = 0; i < basePackageCount; i++) {
2876 size_t packageId = table.getBasePackageId(i);
2877 String16 packageName(table.getBasePackageName(i));
2878 if (packageId > 0x01 && packageId != 0x7f &&
2879 packageName != String16("android")) {
2880 libraryPackages.add(sp<Package>(new Package(packageName, packageId)));
2884 // Iterate through all data, collecting all values (strings,
2885 // references, etc).
2886 StringPool valueStrings(useUTF8);
2887 Vector<sp<Entry> > allEntries;
2888 for (pi=0; pi<N; pi++) {
2889 sp<Package> p = mOrderedPackages.itemAt(pi);
2890 if (p->getTypes().size() == 0) {
2894 StringPool typeStrings(useUTF8);
2895 StringPool keyStrings(useUTF8);
2897 ssize_t stringsAdded = 0;
2898 const size_t N = p->getOrderedTypes().size();
2899 for (size_t ti=0; ti<N; ti++) {
2900 sp<Type> t = p->getOrderedTypes().itemAt(ti);
2902 typeStrings.add(String16("<empty>"), false);
2907 while (stringsAdded < t->getIndex() - 1) {
2908 typeStrings.add(String16("<empty>"), false);
2912 const String16 typeName(t->getName());
2913 typeStrings.add(typeName, false);
2916 // This is a hack to tweak the sorting order of the final strings,
2917 // to put stuff that is generally not language-specific first.
2918 String8 configTypeName(typeName);
2919 if (configTypeName == "drawable" || configTypeName == "layout"
2920 || configTypeName == "color" || configTypeName == "anim"
2921 || configTypeName == "interpolator" || configTypeName == "animator"
2922 || configTypeName == "xml" || configTypeName == "menu"
2923 || configTypeName == "mipmap" || configTypeName == "raw") {
2924 configTypeName = "1complex";
2926 configTypeName = "2value";
2929 // mipmaps don't get filtered, so they will
2930 // allways end up in the base. Make sure they
2931 // don't end up in a split.
2932 if (typeName == mipmap16 && !isBase) {
2936 const bool filterable = (typeName != mipmap16);
2938 const size_t N = t->getOrderedConfigs().size();
2939 for (size_t ci=0; ci<N; ci++) {
2940 sp<ConfigList> c = t->getOrderedConfigs().itemAt(ci);
2944 const size_t N = c->getEntries().size();
2945 for (size_t ei=0; ei<N; ei++) {
2946 ConfigDescription config = c->getEntries().keyAt(ei);
2947 if (filterable && !filter->match(config)) {
2950 sp<Entry> e = c->getEntries().valueAt(ei);
2954 e->setNameIndex(keyStrings.add(e->getName(), true));
2956 // If this entry has no values for other configs,
2957 // and is the default config, then it is special. Otherwise
2958 // we want to add it with the config info.
2959 ConfigDescription* valueConfig = NULL;
2960 if (N != 1 || config == nullConfig) {
2961 valueConfig = &config;
2964 status_t err = e->prepareFlatten(&valueStrings, this,
2965 &configTypeName, &config);
2966 if (err != NO_ERROR) {
2974 p->setTypeStrings(typeStrings.createStringBlock());
2975 p->setKeyStrings(keyStrings.createStringBlock());
2978 if (bundle->getOutputAPKFile() != NULL) {
2979 // Now we want to sort the value strings for better locality. This will
2980 // cause the positions of the strings to change, so we need to go back
2981 // through out resource entries and update them accordingly. Only need
2982 // to do this if actually writing the output file.
2983 valueStrings.sortByConfig();
2984 for (pi=0; pi<allEntries.size(); pi++) {
2985 allEntries[pi]->remapStringValue(&valueStrings);
2991 // Now build the array of package chunks.
2992 Vector<sp<AaptFile> > flatPackages;
2993 for (pi=0; pi<N; pi++) {
2994 sp<Package> p = mOrderedPackages.itemAt(pi);
2995 if (p->getTypes().size() == 0) {
3000 const size_t N = p->getTypeStrings().size();
3002 const size_t baseSize = sizeof(ResTable_package);
3004 // Start the package data.
3005 sp<AaptFile> data = new AaptFile(String8(), AaptGroupEntry(), String8());
3006 ResTable_package* header = (ResTable_package*)data->editData(baseSize);
3007 if (header == NULL) {
3008 fprintf(stderr, "ERROR: out of memory creating ResTable_package\n");
3011 memset(header, 0, sizeof(*header));
3012 header->header.type = htods(RES_TABLE_PACKAGE_TYPE);
3013 header->header.headerSize = htods(sizeof(*header));
3014 header->id = htodl(static_cast<uint32_t>(p->getAssignedId()));
3015 strcpy16_htod(header->name, p->getName().string());
3017 // Write the string blocks.
3018 const size_t typeStringsStart = data->getSize();
3019 sp<AaptFile> strFile = p->getTypeStringsData();
3020 ssize_t amt = data->writeData(strFile->getData(), strFile->getSize());
3021 if (kPrintStringMetrics) {
3022 fprintf(stderr, "**** type strings: %zd\n", SSIZE(amt));
3028 const size_t keyStringsStart = data->getSize();
3029 strFile = p->getKeyStringsData();
3030 amt = data->writeData(strFile->getData(), strFile->getSize());
3031 if (kPrintStringMetrics) {
3032 fprintf(stderr, "**** key strings: %zd\n", SSIZE(amt));
3040 status_t err = flattenLibraryTable(data, libraryPackages);
3041 if (err != NO_ERROR) {
3042 fprintf(stderr, "ERROR: failed to write library table\n");
3047 // Build the type chunks inside of this package.
3048 for (size_t ti=0; ti<N; ti++) {
3049 // Retrieve them in the same order as the type string block.
3051 String16 typeName(p->getTypeStrings().stringAt(ti, &len));
3052 sp<Type> t = p->getTypes().valueFor(typeName);
3053 LOG_ALWAYS_FATAL_IF(t == NULL && typeName != String16("<empty>"),
3054 "Type name %s not found",
3055 String8(typeName).string());
3059 const bool filterable = (typeName != mipmap16);
3060 const bool skipEntireType = (typeName == mipmap16 && !isBase);
3062 const size_t N = t != NULL ? t->getOrderedConfigs().size() : 0;
3064 // Until a non-NO_ENTRY value has been written for a resource,
3065 // that resource is invalid; validResources[i] represents
3066 // the item at t->getOrderedConfigs().itemAt(i).
3067 Vector<bool> validResources;
3068 validResources.insertAt(false, 0, N);
3070 // First write the typeSpec chunk, containing information about
3071 // each resource entry in this type.
3073 const size_t typeSpecSize = sizeof(ResTable_typeSpec) + sizeof(uint32_t)*N;
3074 const size_t typeSpecStart = data->getSize();
3075 ResTable_typeSpec* tsHeader = (ResTable_typeSpec*)
3076 (((uint8_t*)data->editData(typeSpecStart+typeSpecSize)) + typeSpecStart);
3077 if (tsHeader == NULL) {
3078 fprintf(stderr, "ERROR: out of memory creating ResTable_typeSpec\n");
3081 memset(tsHeader, 0, sizeof(*tsHeader));
3082 tsHeader->header.type = htods(RES_TABLE_TYPE_SPEC_TYPE);
3083 tsHeader->header.headerSize = htods(sizeof(*tsHeader));
3084 tsHeader->header.size = htodl(typeSpecSize);
3085 tsHeader->id = ti+1;
3086 tsHeader->entryCount = htodl(N);
3088 uint32_t* typeSpecFlags = (uint32_t*)
3089 (((uint8_t*)data->editData())
3090 + typeSpecStart + sizeof(ResTable_typeSpec));
3091 memset(typeSpecFlags, 0, sizeof(uint32_t)*N);
3093 for (size_t ei=0; ei<N; ei++) {
3094 sp<ConfigList> cl = t->getOrderedConfigs().itemAt(ei);
3099 if (cl->getPublic()) {
3100 typeSpecFlags[ei] |= htodl(ResTable_typeSpec::SPEC_PUBLIC);
3103 if (skipEntireType) {
3107 const size_t CN = cl->getEntries().size();
3108 for (size_t ci=0; ci<CN; ci++) {
3109 if (filterable && !filter->match(cl->getEntries().keyAt(ci))) {
3112 for (size_t cj=ci+1; cj<CN; cj++) {
3113 if (filterable && !filter->match(cl->getEntries().keyAt(cj))) {
3116 typeSpecFlags[ei] |= htodl(
3117 cl->getEntries().keyAt(ci).diff(cl->getEntries().keyAt(cj)));
3123 if (skipEntireType) {
3127 // We need to write one type chunk for each configuration for
3128 // which we have entries in this type.
3129 SortedVector<ConfigDescription> uniqueConfigs;
3131 uniqueConfigs = t->getUniqueConfigs();
3134 const size_t typeSize = sizeof(ResTable_type) + sizeof(uint32_t)*N;
3136 const size_t NC = uniqueConfigs.size();
3137 for (size_t ci=0; ci<NC; ci++) {
3138 const ConfigDescription& config = uniqueConfigs[ci];
3141 printf("Writing config %zu config: imsi:%d/%d lang:%c%c cnt:%c%c "
3142 "orien:%d ui:%d touch:%d density:%d key:%d inp:%d nav:%d sz:%dx%d "
3143 "sw%ddp w%ddp h%ddp layout:%d\n",
3145 config.mcc, config.mnc,
3146 config.language[0] ? config.language[0] : '-',
3147 config.language[1] ? config.language[1] : '-',
3148 config.country[0] ? config.country[0] : '-',
3149 config.country[1] ? config.country[1] : '-',
3158 config.screenHeight,
3159 config.smallestScreenWidthDp,
3160 config.screenWidthDp,
3161 config.screenHeightDp,
3162 config.screenLayout);
3165 if (filterable && !filter->match(config)) {
3169 const size_t typeStart = data->getSize();
3171 ResTable_type* tHeader = (ResTable_type*)
3172 (((uint8_t*)data->editData(typeStart+typeSize)) + typeStart);
3173 if (tHeader == NULL) {
3174 fprintf(stderr, "ERROR: out of memory creating ResTable_type\n");
3178 memset(tHeader, 0, sizeof(*tHeader));
3179 tHeader->header.type = htods(RES_TABLE_TYPE_TYPE);
3180 tHeader->header.headerSize = htods(sizeof(*tHeader));
3182 tHeader->entryCount = htodl(N);
3183 tHeader->entriesStart = htodl(typeSize);
3184 tHeader->config = config;
3186 printf("Writing type %zu config: imsi:%d/%d lang:%c%c cnt:%c%c "
3187 "orien:%d ui:%d touch:%d density:%d key:%d inp:%d nav:%d sz:%dx%d "
3188 "sw%ddp w%ddp h%ddp layout:%d\n",
3190 tHeader->config.mcc, tHeader->config.mnc,
3191 tHeader->config.language[0] ? tHeader->config.language[0] : '-',
3192 tHeader->config.language[1] ? tHeader->config.language[1] : '-',
3193 tHeader->config.country[0] ? tHeader->config.country[0] : '-',
3194 tHeader->config.country[1] ? tHeader->config.country[1] : '-',
3195 tHeader->config.orientation,
3196 tHeader->config.uiMode,
3197 tHeader->config.touchscreen,
3198 tHeader->config.density,
3199 tHeader->config.keyboard,
3200 tHeader->config.inputFlags,
3201 tHeader->config.navigation,
3202 tHeader->config.screenWidth,
3203 tHeader->config.screenHeight,
3204 tHeader->config.smallestScreenWidthDp,
3205 tHeader->config.screenWidthDp,
3206 tHeader->config.screenHeightDp,
3207 tHeader->config.screenLayout);
3209 tHeader->config.swapHtoD();
3211 // Build the entries inside of this type.
3212 for (size_t ei=0; ei<N; ei++) {
3213 sp<ConfigList> cl = t->getOrderedConfigs().itemAt(ei);
3216 e = cl->getEntries().valueFor(config);
3219 // Set the offset for this entry in its type.
3220 uint32_t* index = (uint32_t*)
3221 (((uint8_t*)data->editData())
3222 + typeStart + sizeof(ResTable_type));
3224 index[ei] = htodl(data->getSize()-typeStart-typeSize);
3226 // Create the entry.
3227 ssize_t amt = e->flatten(bundle, data, cl->getPublic());
3231 validResources.editItemAt(ei) = true;
3233 index[ei] = htodl(ResTable_type::NO_ENTRY);
3237 // Fill in the rest of the type information.
3238 tHeader = (ResTable_type*)
3239 (((uint8_t*)data->editData()) + typeStart);
3240 tHeader->header.size = htodl(data->getSize()-typeStart);
3243 // If we're building splits, then each invocation of the flattening
3244 // step will have 'missing' entries. Don't warn/error for this case.
3245 if (bundle->getSplitConfigurations().isEmpty()) {
3246 bool missing_entry = false;
3247 const char* log_prefix = bundle->getErrorOnMissingConfigEntry() ?
3248 "error" : "warning";
3249 for (size_t i = 0; i < N; ++i) {
3250 if (!validResources[i]) {
3251 sp<ConfigList> c = t->getOrderedConfigs().itemAt(i);
3253 fprintf(stderr, "%s: no entries written for %s/%s (0x%08zx)\n", log_prefix,
3254 String8(typeName).string(), String8(c->getName()).string(),
3255 Res_MAKEID(p->getAssignedId() - 1, ti, i));
3257 missing_entry = true;
3260 if (bundle->getErrorOnMissingConfigEntry() && missing_entry) {
3261 fprintf(stderr, "Error: Missing entries, quit!\n");
3262 return NOT_ENOUGH_DATA;
3267 // Fill in the rest of the package information.
3268 header = (ResTable_package*)data->editData();
3269 header->header.size = htodl(data->getSize());
3270 header->typeStrings = htodl(typeStringsStart);
3271 header->lastPublicType = htodl(p->getTypeStrings().size());
3272 header->keyStrings = htodl(keyStringsStart);
3273 header->lastPublicKey = htodl(p->getKeyStrings().size());
3275 flatPackages.add(data);
3278 // And now write out the final chunks.
3279 const size_t dataStart = dest->getSize();
3283 ResTable_header header;
3284 memset(&header, 0, sizeof(header));
3285 header.header.type = htods(RES_TABLE_TYPE);
3286 header.header.headerSize = htods(sizeof(header));
3287 header.packageCount = htodl(flatPackages.size());
3288 status_t err = dest->writeData(&header, sizeof(header));
3289 if (err != NO_ERROR) {
3290 fprintf(stderr, "ERROR: out of memory creating ResTable_header\n");
3295 ssize_t strStart = dest->getSize();
3296 status_t err = valueStrings.writeStringBlock(dest);
3297 if (err != NO_ERROR) {
3301 ssize_t amt = (dest->getSize()-strStart);
3303 if (kPrintStringMetrics) {
3304 fprintf(stderr, "**** value strings: %zd\n", SSIZE(amt));
3305 fprintf(stderr, "**** total strings: %zd\n", SSIZE(strAmt));
3308 for (pi=0; pi<flatPackages.size(); pi++) {
3309 err = dest->writeData(flatPackages[pi]->getData(),
3310 flatPackages[pi]->getSize());
3311 if (err != NO_ERROR) {
3312 fprintf(stderr, "ERROR: out of memory creating package chunk for ResTable_header\n");
3317 ResTable_header* header = (ResTable_header*)
3318 (((uint8_t*)dest->getData()) + dataStart);
3319 header->header.size = htodl(dest->getSize() - dataStart);
3321 if (kPrintStringMetrics) {
3322 fprintf(stderr, "**** total resource table size: %zu / %zu%% strings\n",
3323 dest->getSize(), (size_t)(strAmt*100)/dest->getSize());
3329 status_t ResourceTable::flattenLibraryTable(const sp<AaptFile>& dest, const Vector<sp<Package> >& libs) {
3330 // Write out the library table if necessary
3331 if (libs.size() > 0) {
3333 fprintf(stderr, "Writing library reference table\n");
3336 const size_t libStart = dest->getSize();
3337 const size_t count = libs.size();
3338 ResTable_lib_header* libHeader = (ResTable_lib_header*) dest->editDataInRange(
3339 libStart, sizeof(ResTable_lib_header));
3341 memset(libHeader, 0, sizeof(*libHeader));
3342 libHeader->header.type = htods(RES_TABLE_LIBRARY_TYPE);
3343 libHeader->header.headerSize = htods(sizeof(*libHeader));
3344 libHeader->header.size = htodl(sizeof(*libHeader) + (sizeof(ResTable_lib_entry) * count));
3345 libHeader->count = htodl(count);
3347 // Write the library entries
3348 for (size_t i = 0; i < count; i++) {
3349 const size_t entryStart = dest->getSize();
3350 sp<Package> libPackage = libs[i];
3352 fprintf(stderr, " Entry %s -> 0x%02x\n",
3353 String8(libPackage->getName()).string(),
3354 (uint8_t)libPackage->getAssignedId());
3357 ResTable_lib_entry* entry = (ResTable_lib_entry*) dest->editDataInRange(
3358 entryStart, sizeof(ResTable_lib_entry));
3359 memset(entry, 0, sizeof(*entry));
3360 entry->packageId = htodl(libPackage->getAssignedId());
3361 strcpy16_htod(entry->packageName, libPackage->getName().string());
3367 void ResourceTable::writePublicDefinitions(const String16& package, FILE* fp)
3370 "<!-- This file contains <public> resource definitions for all\n"
3371 " resources that were generated from the source data. -->\n"
3375 writePublicDefinitions(package, fp, true);
3376 writePublicDefinitions(package, fp, false);
3383 void ResourceTable::writePublicDefinitions(const String16& package, FILE* fp, bool pub)
3385 bool didHeader = false;
3387 sp<Package> pkg = mPackages.valueFor(package);
3389 const size_t NT = pkg->getOrderedTypes().size();
3390 for (size_t i=0; i<NT; i++) {
3391 sp<Type> t = pkg->getOrderedTypes().itemAt(i);
3396 bool didType = false;
3398 const size_t NC = t->getOrderedConfigs().size();
3399 for (size_t j=0; j<NC; j++) {
3400 sp<ConfigList> c = t->getOrderedConfigs().itemAt(j);
3405 if (c->getPublic() != pub) {
3415 fprintf(fp," <!-- PUBLIC SECTION. These resources have been declared public.\n");
3416 fprintf(fp," Changes to these definitions will break binary compatibility. -->\n\n");
3418 fprintf(fp," <!-- PRIVATE SECTION. These resources have not been declared public.\n");
3419 fprintf(fp," You can make them public my moving these lines into a file in res/values. -->\n\n");
3424 const size_t NE = c->getEntries().size();
3425 for (size_t k=0; k<NE; k++) {
3426 const SourcePos& pos = c->getEntries().valueAt(k)->getPos();
3427 if (pos.file != "") {
3428 fprintf(fp," <!-- Declared at %s:%d -->\n",
3429 pos.file.string(), pos.line);
3433 fprintf(fp, " <public type=\"%s\" name=\"%s\" id=\"0x%08x\" />\n",
3434 String8(t->getName()).string(),
3435 String8(c->getName()).string(),
3436 getResId(pkg, t, c->getEntryIndex()));
3442 ResourceTable::Item::Item(const SourcePos& _sourcePos,
3444 const String16& _value,
3445 const Vector<StringPool::entry_style_span>* _style,
3447 : sourcePos(_sourcePos)
3459 ResourceTable::Entry::Entry(const Entry& entry)
3461 , mName(entry.mName)
3462 , mParent(entry.mParent)
3463 , mType(entry.mType)
3464 , mItem(entry.mItem)
3465 , mItemFormat(entry.mItemFormat)
3467 , mNameIndex(entry.mNameIndex)
3468 , mParentId(entry.mParentId)
3469 , mPos(entry.mPos) {}
3471 ResourceTable::Entry& ResourceTable::Entry::operator=(const Entry& entry) {
3472 mName = entry.mName;
3473 mParent = entry.mParent;
3474 mType = entry.mType;
3475 mItem = entry.mItem;
3476 mItemFormat = entry.mItemFormat;
3478 mNameIndex = entry.mNameIndex;
3479 mParentId = entry.mParentId;
3484 status_t ResourceTable::Entry::makeItABag(const SourcePos& sourcePos)
3486 if (mType == TYPE_BAG) {
3489 if (mType == TYPE_UNKNOWN) {
3493 sourcePos.error("Resource entry %s is already defined as a single item.\n"
3494 "%s:%d: Originally defined here.\n",
3495 String8(mName).string(),
3496 mItem.sourcePos.file.string(), mItem.sourcePos.line);
3497 return UNKNOWN_ERROR;
3500 status_t ResourceTable::Entry::setItem(const SourcePos& sourcePos,
3501 const String16& value,
3502 const Vector<StringPool::entry_style_span>* style,
3504 const bool overwrite)
3506 Item item(sourcePos, false, value, style);
3508 if (mType == TYPE_BAG) {
3509 if (mBag.size() == 0) {
3510 sourcePos.error("Resource entry %s is already defined as a bag.",
3511 String8(mName).string());
3513 const Item& item(mBag.valueAt(0));
3514 sourcePos.error("Resource entry %s is already defined as a bag.\n"
3515 "%s:%d: Originally defined here.\n",
3516 String8(mName).string(),
3517 item.sourcePos.file.string(), item.sourcePos.line);
3519 return UNKNOWN_ERROR;
3521 if ( (mType != TYPE_UNKNOWN) && (overwrite == false) ) {
3522 sourcePos.error("Resource entry %s is already defined.\n"
3523 "%s:%d: Originally defined here.\n",
3524 String8(mName).string(),
3525 mItem.sourcePos.file.string(), mItem.sourcePos.line);
3526 return UNKNOWN_ERROR;
3531 mItemFormat = format;
3535 status_t ResourceTable::Entry::addToBag(const SourcePos& sourcePos,
3536 const String16& key, const String16& value,
3537 const Vector<StringPool::entry_style_span>* style,
3538 bool replace, bool isId, int32_t format)
3540 status_t err = makeItABag(sourcePos);
3541 if (err != NO_ERROR) {
3545 Item item(sourcePos, isId, value, style, format);
3547 // XXX NOTE: there is an error if you try to have a bag with two keys,
3548 // one an attr and one an id, with the same name. Not something we
3549 // currently ever have to worry about.
3550 ssize_t origKey = mBag.indexOfKey(key);
3553 const Item& item(mBag.valueAt(origKey));
3554 sourcePos.error("Resource entry %s already has bag item %s.\n"
3555 "%s:%d: Originally defined here.\n",
3556 String8(mName).string(), String8(key).string(),
3557 item.sourcePos.file.string(), item.sourcePos.line);
3558 return UNKNOWN_ERROR;
3560 //printf("Replacing %s with %s\n",
3561 // String8(mBag.valueFor(key).value).string(), String8(value).string());
3562 mBag.replaceValueFor(key, item);
3565 mBag.add(key, item);
3569 status_t ResourceTable::Entry::removeFromBag(const String16& key) {
3570 if (mType != Entry::TYPE_BAG) {
3574 if (mBag.removeItem(key) >= 0) {
3577 return UNKNOWN_ERROR;
3580 status_t ResourceTable::Entry::emptyBag(const SourcePos& sourcePos)
3582 status_t err = makeItABag(sourcePos);
3583 if (err != NO_ERROR) {
3591 status_t ResourceTable::Entry::generateAttributes(ResourceTable* table,
3592 const String16& package)
3594 const String16 attr16("attr");
3595 const String16 id16("id");
3596 const size_t N = mBag.size();
3597 for (size_t i=0; i<N; i++) {
3598 const String16& key = mBag.keyAt(i);
3599 const Item& it = mBag.valueAt(i);
3601 if (!table->hasBagOrEntry(key, &id16, &package)) {
3602 String16 value("false");
3604 fprintf(stderr, "Generating %s:id/%s\n",
3605 String8(package).string(),
3606 String8(key).string());
3608 status_t err = table->addEntry(SourcePos(String8("<generated>"), 0), package,
3610 if (err != NO_ERROR) {
3614 } else if (!table->hasBagOrEntry(key, &attr16, &package)) {
3617 // fprintf(stderr, "ERROR: Bag attribute '%s' has not been defined.\n",
3618 // String8(key).string());
3619 // const Item& item(mBag.valueAt(i));
3620 // fprintf(stderr, "Referenced from file %s line %d\n",
3621 // item.sourcePos.file.string(), item.sourcePos.line);
3622 // return UNKNOWN_ERROR;
3625 sprintf(numberStr, "%d", ResTable_map::TYPE_ANY);
3626 status_t err = table->addBag(SourcePos("<generated>", 0), package,
3627 attr16, key, String16(""),
3629 String16(numberStr), NULL, NULL);
3630 if (err != NO_ERROR) {
3639 status_t ResourceTable::Entry::assignResourceIds(ResourceTable* table,
3640 const String16& /* package */)
3642 bool hasErrors = false;
3644 if (mType == TYPE_BAG) {
3645 const char* errorMsg;
3646 const String16 style16("style");
3647 const String16 attr16("attr");
3648 const String16 id16("id");
3650 if (mParent.size() > 0) {
3651 mParentId = table->getResId(mParent, &style16, NULL, &errorMsg);
3652 if (mParentId == 0) {
3653 mPos.error("Error retrieving parent for item: %s '%s'.\n",
3654 errorMsg, String8(mParent).string());
3658 const size_t N = mBag.size();
3659 for (size_t i=0; i<N; i++) {
3660 const String16& key = mBag.keyAt(i);
3661 Item& it = mBag.editValueAt(i);
3662 it.bagKeyId = table->getResId(key,
3663 it.isId ? &id16 : &attr16, NULL, &errorMsg);
3664 //printf("Bag key of %s: #%08x\n", String8(key).string(), it.bagKeyId);
3665 if (it.bagKeyId == 0) {
3666 it.sourcePos.error("Error: %s: %s '%s'.\n", errorMsg,
3667 String8(it.isId ? id16 : attr16).string(),
3668 String8(key).string());
3673 return hasErrors ? STATUST(UNKNOWN_ERROR) : NO_ERROR;
3676 status_t ResourceTable::Entry::prepareFlatten(StringPool* strings, ResourceTable* table,
3677 const String8* configTypeName, const ConfigDescription* config)
3679 if (mType == TYPE_ITEM) {
3681 AccessorCookie ac(it.sourcePos, String8(mName), String8(it.value));
3682 if (!table->stringToValue(&it.parsedValue, strings,
3683 it.value, false, true, 0,
3684 &it.style, NULL, &ac, mItemFormat,
3685 configTypeName, config)) {
3686 return UNKNOWN_ERROR;
3688 } else if (mType == TYPE_BAG) {
3689 const size_t N = mBag.size();
3690 for (size_t i=0; i<N; i++) {
3691 const String16& key = mBag.keyAt(i);
3692 Item& it = mBag.editValueAt(i);
3693 AccessorCookie ac(it.sourcePos, String8(key), String8(it.value));
3694 if (!table->stringToValue(&it.parsedValue, strings,
3695 it.value, false, true, it.bagKeyId,
3696 &it.style, NULL, &ac, it.format,
3697 configTypeName, config)) {
3698 return UNKNOWN_ERROR;
3702 mPos.error("Error: entry %s is not a single item or a bag.\n",
3703 String8(mName).string());
3704 return UNKNOWN_ERROR;
3709 status_t ResourceTable::Entry::remapStringValue(StringPool* strings)
3711 if (mType == TYPE_ITEM) {
3713 if (it.parsedValue.dataType == Res_value::TYPE_STRING) {
3714 it.parsedValue.data = strings->mapOriginalPosToNewPos(it.parsedValue.data);
3716 } else if (mType == TYPE_BAG) {
3717 const size_t N = mBag.size();
3718 for (size_t i=0; i<N; i++) {
3719 Item& it = mBag.editValueAt(i);
3720 if (it.parsedValue.dataType == Res_value::TYPE_STRING) {
3721 it.parsedValue.data = strings->mapOriginalPosToNewPos(it.parsedValue.data);
3725 mPos.error("Error: entry %s is not a single item or a bag.\n",
3726 String8(mName).string());
3727 return UNKNOWN_ERROR;
3732 ssize_t ResourceTable::Entry::flatten(Bundle* /* bundle */, const sp<AaptFile>& data, bool isPublic)
3735 ResTable_entry header;
3736 memset(&header, 0, sizeof(header));
3737 header.size = htods(sizeof(header));
3738 const type ty = mType;
3739 if (ty == TYPE_BAG) {
3740 header.flags |= htods(header.FLAG_COMPLEX);
3743 header.flags |= htods(header.FLAG_PUBLIC);
3745 header.key.index = htodl(mNameIndex);
3746 if (ty != TYPE_BAG) {
3747 status_t err = data->writeData(&header, sizeof(header));
3748 if (err != NO_ERROR) {
3749 fprintf(stderr, "ERROR: out of memory creating ResTable_entry\n");
3753 const Item& it = mItem;
3755 memset(&par, 0, sizeof(par));
3756 par.size = htods(it.parsedValue.size);
3757 par.dataType = it.parsedValue.dataType;
3758 par.res0 = it.parsedValue.res0;
3759 par.data = htodl(it.parsedValue.data);
3761 printf("Writing item (%s): type=%d, data=0x%x, res0=0x%x\n",
3762 String8(mName).string(), it.parsedValue.dataType,
3763 it.parsedValue.data, par.res0);
3765 err = data->writeData(&par, it.parsedValue.size);
3766 if (err != NO_ERROR) {
3767 fprintf(stderr, "ERROR: out of memory creating Res_value\n");
3770 amt += it.parsedValue.size;
3772 size_t N = mBag.size();
3774 // Create correct ordering of items.
3775 KeyedVector<uint32_t, const Item*> items;
3776 for (i=0; i<N; i++) {
3777 const Item& it = mBag.valueAt(i);
3778 items.add(it.bagKeyId, &it);
3782 ResTable_map_entry mapHeader;
3783 memcpy(&mapHeader, &header, sizeof(header));
3784 mapHeader.size = htods(sizeof(mapHeader));
3785 mapHeader.parent.ident = htodl(mParentId);
3786 mapHeader.count = htodl(N);
3787 status_t err = data->writeData(&mapHeader, sizeof(mapHeader));
3788 if (err != NO_ERROR) {
3789 fprintf(stderr, "ERROR: out of memory creating ResTable_entry\n");
3793 for (i=0; i<N; i++) {
3794 const Item& it = *items.valueAt(i);
3796 map.name.ident = htodl(it.bagKeyId);
3797 map.value.size = htods(it.parsedValue.size);
3798 map.value.dataType = it.parsedValue.dataType;
3799 map.value.res0 = it.parsedValue.res0;
3800 map.value.data = htodl(it.parsedValue.data);
3801 err = data->writeData(&map, sizeof(map));
3802 if (err != NO_ERROR) {
3803 fprintf(stderr, "ERROR: out of memory creating Res_value\n");
3812 void ResourceTable::ConfigList::appendComment(const String16& comment,
3815 if (comment.size() <= 0) {
3818 if (onlyIfEmpty && mComment.size() > 0) {
3821 if (mComment.size() > 0) {
3822 mComment.append(String16("\n"));
3824 mComment.append(comment);
3827 void ResourceTable::ConfigList::appendTypeComment(const String16& comment)
3829 if (comment.size() <= 0) {
3832 if (mTypeComment.size() > 0) {
3833 mTypeComment.append(String16("\n"));
3835 mTypeComment.append(comment);
3838 status_t ResourceTable::Type::addPublic(const SourcePos& sourcePos,
3839 const String16& name,
3840 const uint32_t ident)
3843 int32_t entryIdx = Res_GETENTRY(ident);
3845 sourcePos.error("Public resource %s/%s has an invalid 0 identifier (0x%08x).\n",
3846 String8(mName).string(), String8(name).string(), ident);
3847 return UNKNOWN_ERROR;
3851 int32_t typeIdx = Res_GETTYPE(ident);
3854 if (mPublicIndex > 0 && mPublicIndex != typeIdx) {
3855 sourcePos.error("Public resource %s/%s has conflicting type codes for its"
3856 " public identifiers (0x%x vs 0x%x).\n",
3857 String8(mName).string(), String8(name).string(),
3858 mPublicIndex, typeIdx);
3859 return UNKNOWN_ERROR;
3861 mPublicIndex = typeIdx;
3864 if (mFirstPublicSourcePos == NULL) {
3865 mFirstPublicSourcePos = new SourcePos(sourcePos);
3868 if (mPublic.indexOfKey(name) < 0) {
3869 mPublic.add(name, Public(sourcePos, String16(), ident));
3871 Public& p = mPublic.editValueFor(name);
3872 if (p.ident != ident) {
3873 sourcePos.error("Public resource %s/%s has conflicting public identifiers"
3874 " (0x%08x vs 0x%08x).\n"
3875 "%s:%d: Originally defined here.\n",
3876 String8(mName).string(), String8(name).string(), p.ident, ident,
3877 p.sourcePos.file.string(), p.sourcePos.line);
3878 return UNKNOWN_ERROR;
3885 void ResourceTable::Type::canAddEntry(const String16& name)
3887 mCanAddEntries.add(name);
3890 sp<ResourceTable::Entry> ResourceTable::Type::getEntry(const String16& entry,
3891 const SourcePos& sourcePos,
3892 const ResTable_config* config,
3895 bool autoAddOverlay)
3898 sp<ConfigList> c = mConfigs.valueFor(entry);
3900 if (overlay && !autoAddOverlay && mCanAddEntries.indexOf(entry) < 0) {
3901 sourcePos.error("Resource at %s appears in overlay but not"
3902 " in the base package; use <add-resource> to add.\n",
3903 String8(entry).string());
3906 c = new ConfigList(entry, sourcePos);
3907 mConfigs.add(entry, c);
3908 pos = (int)mOrderedConfigs.size();
3909 mOrderedConfigs.add(c);
3911 c->setEntryIndex(pos);
3915 ConfigDescription cdesc;
3916 if (config) cdesc = *config;
3918 sp<Entry> e = c->getEntries().valueFor(cdesc);
3921 if (config != NULL) {
3922 printf("New entry at %s:%d: imsi:%d/%d lang:%c%c cnt:%c%c "
3923 "orien:%d touch:%d density:%d key:%d inp:%d nav:%d sz:%dx%d "
3924 "sw%ddp w%ddp h%ddp layout:%d\n",
3925 sourcePos.file.string(), sourcePos.line,
3926 config->mcc, config->mnc,
3927 config->language[0] ? config->language[0] : '-',
3928 config->language[1] ? config->language[1] : '-',
3929 config->country[0] ? config->country[0] : '-',
3930 config->country[1] ? config->country[1] : '-',
3931 config->orientation,
3932 config->touchscreen,
3937 config->screenWidth,
3938 config->screenHeight,
3939 config->smallestScreenWidthDp,
3940 config->screenWidthDp,
3941 config->screenHeightDp,
3942 config->screenLayout);
3944 printf("New entry at %s:%d: NULL config\n",
3945 sourcePos.file.string(), sourcePos.line);
3948 e = new Entry(entry, sourcePos);
3949 c->addEntry(cdesc, e);
3953 for (pos=0; pos<(int)mOrderedConfigs.size(); pos++) {
3954 if (mOrderedConfigs[pos] == c) {
3958 if (pos >= (int)mOrderedConfigs.size()) {
3959 sourcePos.error("Internal error: config not found in mOrderedConfigs when adding entry");
3963 e->setEntryIndex(pos);
3971 sp<ResourceTable::ConfigList> ResourceTable::Type::removeEntry(const String16& entry) {
3972 ssize_t idx = mConfigs.indexOfKey(entry);
3977 sp<ConfigList> removed = mConfigs.valueAt(idx);
3978 mConfigs.removeItemsAt(idx);
3980 Vector<sp<ConfigList> >::iterator iter = std::find(
3981 mOrderedConfigs.begin(), mOrderedConfigs.end(), removed);
3982 if (iter != mOrderedConfigs.end()) {
3983 mOrderedConfigs.erase(iter);
3986 mPublic.removeItem(entry);
3990 SortedVector<ConfigDescription> ResourceTable::Type::getUniqueConfigs() const {
3991 SortedVector<ConfigDescription> unique;
3992 const size_t entryCount = mOrderedConfigs.size();
3993 for (size_t i = 0; i < entryCount; i++) {
3994 if (mOrderedConfigs[i] == NULL) {
3997 const DefaultKeyedVector<ConfigDescription, sp<Entry> >& configs =
3998 mOrderedConfigs[i]->getEntries();
3999 const size_t configCount = configs.size();
4000 for (size_t j = 0; j < configCount; j++) {
4001 unique.add(configs.keyAt(j));
4007 status_t ResourceTable::Type::applyPublicEntryOrder()
4009 size_t N = mOrderedConfigs.size();
4010 Vector<sp<ConfigList> > origOrder(mOrderedConfigs);
4011 bool hasError = false;
4014 for (i=0; i<N; i++) {
4015 mOrderedConfigs.replaceAt(NULL, i);
4018 const size_t NP = mPublic.size();
4019 //printf("Ordering %d configs from %d public defs\n", N, NP);
4021 for (j=0; j<NP; j++) {
4022 const String16& name = mPublic.keyAt(j);
4023 const Public& p = mPublic.valueAt(j);
4024 int32_t idx = Res_GETENTRY(p.ident);
4025 //printf("Looking for entry \"%s\"/\"%s\" (0x%08x) in %d...\n",
4026 // String8(mName).string(), String8(name).string(), p.ident, N);
4028 for (i=0; i<N; i++) {
4029 sp<ConfigList> e = origOrder.itemAt(i);
4030 //printf("#%d: \"%s\"\n", i, String8(e->getName()).string());
4031 if (e->getName() == name) {
4032 if (idx >= (int32_t)mOrderedConfigs.size()) {
4033 mOrderedConfigs.resize(idx + 1);
4036 if (mOrderedConfigs.itemAt(idx) == NULL) {
4038 e->setPublicSourcePos(p.sourcePos);
4039 mOrderedConfigs.replaceAt(e, idx);
4040 origOrder.removeAt(i);
4045 sp<ConfigList> oe = mOrderedConfigs.itemAt(idx);
4047 p.sourcePos.error("Multiple entry names declared for public entry"
4048 " identifier 0x%x in type %s (%s vs %s).\n"
4049 "%s:%d: Originally defined here.",
4050 idx+1, String8(mName).string(),
4051 String8(oe->getName()).string(),
4052 String8(name).string(),
4053 oe->getPublicSourcePos().file.string(),
4054 oe->getPublicSourcePos().line);
4061 p.sourcePos.error("Public symbol %s/%s declared here is not defined.",
4062 String8(mName).string(), String8(name).string());
4067 //printf("Copying back in %d non-public configs, have %d\n", N, origOrder.size());
4069 if (N != origOrder.size()) {
4070 printf("Internal error: remaining private symbol count mismatch\n");
4071 N = origOrder.size();
4075 for (i=0; i<N; i++) {
4076 sp<ConfigList> e = origOrder.itemAt(i);
4077 // There will always be enough room for the remaining entries.
4078 while (mOrderedConfigs.itemAt(j) != NULL) {
4081 mOrderedConfigs.replaceAt(e, j);
4085 return hasError ? STATUST(UNKNOWN_ERROR) : NO_ERROR;
4088 ResourceTable::Package::Package(const String16& name, size_t packageId)
4089 : mName(name), mPackageId(packageId),
4090 mTypeStringsMapping(0xffffffff),
4091 mKeyStringsMapping(0xffffffff)
4095 sp<ResourceTable::Type> ResourceTable::Package::getType(const String16& type,
4096 const SourcePos& sourcePos,
4099 sp<Type> t = mTypes.valueFor(type);
4101 t = new Type(type, sourcePos);
4102 mTypes.add(type, t);
4103 mOrderedTypes.add(t);
4105 // For some reason the type's index is set to one plus the index
4106 // in the mOrderedTypes list, rather than just the index.
4107 t->setIndex(mOrderedTypes.size());
4113 status_t ResourceTable::Package::setTypeStrings(const sp<AaptFile>& data)
4115 status_t err = setStrings(data, &mTypeStrings, &mTypeStringsMapping);
4116 if (err != NO_ERROR) {
4117 fprintf(stderr, "ERROR: Type string data is corrupt!\n");
4121 // Retain a reference to the new data after we've successfully replaced
4122 // all uses of the old reference (in setStrings() ).
4123 mTypeStringsData = data;
4127 status_t ResourceTable::Package::setKeyStrings(const sp<AaptFile>& data)
4129 status_t err = setStrings(data, &mKeyStrings, &mKeyStringsMapping);
4130 if (err != NO_ERROR) {
4131 fprintf(stderr, "ERROR: Key string data is corrupt!\n");
4135 // Retain a reference to the new data after we've successfully replaced
4136 // all uses of the old reference (in setStrings() ).
4137 mKeyStringsData = data;
4141 status_t ResourceTable::Package::setStrings(const sp<AaptFile>& data,
4142 ResStringPool* strings,
4143 DefaultKeyedVector<String16, uint32_t>* mappings)
4145 if (data->getData() == NULL) {
4146 return UNKNOWN_ERROR;
4149 status_t err = strings->setTo(data->getData(), data->getSize());
4150 if (err == NO_ERROR) {
4151 const size_t N = strings->size();
4152 for (size_t i=0; i<N; i++) {
4154 mappings->add(String16(strings->stringAt(i, &len)), i);
4160 status_t ResourceTable::Package::applyPublicTypeOrder()
4162 size_t N = mOrderedTypes.size();
4163 Vector<sp<Type> > origOrder(mOrderedTypes);
4166 for (i=0; i<N; i++) {
4167 mOrderedTypes.replaceAt(NULL, i);
4170 for (i=0; i<N; i++) {
4171 sp<Type> t = origOrder.itemAt(i);
4172 int32_t idx = t->getPublicIndex();
4175 while (idx >= (int32_t)mOrderedTypes.size()) {
4176 mOrderedTypes.add();
4178 if (mOrderedTypes.itemAt(idx) != NULL) {
4179 sp<Type> ot = mOrderedTypes.itemAt(idx);
4180 t->getFirstPublicSourcePos().error("Multiple type names declared for public type"
4181 " identifier 0x%x (%s vs %s).\n"
4182 "%s:%d: Originally defined here.",
4183 idx, String8(ot->getName()).string(),
4184 String8(t->getName()).string(),
4185 ot->getFirstPublicSourcePos().file.string(),
4186 ot->getFirstPublicSourcePos().line);
4187 return UNKNOWN_ERROR;
4189 mOrderedTypes.replaceAt(t, idx);
4190 origOrder.removeAt(i);
4197 for (i=0; i<N; i++) {
4198 sp<Type> t = origOrder.itemAt(i);
4199 // There will always be enough room for the remaining types.
4200 while (mOrderedTypes.itemAt(j) != NULL) {
4203 mOrderedTypes.replaceAt(t, j);
4209 void ResourceTable::Package::movePrivateAttrs() {
4210 sp<Type> attr = mTypes.valueFor(String16("attr"));
4216 Vector<sp<ConfigList> > privateAttrs;
4218 bool hasPublic = false;
4219 const Vector<sp<ConfigList> >& configs = attr->getOrderedConfigs();
4220 const size_t configCount = configs.size();
4221 for (size_t i = 0; i < configCount; i++) {
4222 if (configs[i] == NULL) {
4226 if (attr->isPublic(configs[i]->getName())) {
4229 privateAttrs.add(configs[i]);
4233 // Only if we have public attributes do we create a separate type for
4234 // private attributes.
4239 // Create a new type for private attributes.
4240 sp<Type> privateAttrType = getType(String16(kAttrPrivateType), SourcePos());
4242 const size_t privateAttrCount = privateAttrs.size();
4243 for (size_t i = 0; i < privateAttrCount; i++) {
4244 const sp<ConfigList>& cl = privateAttrs[i];
4246 // Remove the private attributes from their current type.
4247 attr->removeEntry(cl->getName());
4249 // Add it to the new type.
4250 const DefaultKeyedVector<ConfigDescription, sp<Entry> >& entries = cl->getEntries();
4251 const size_t entryCount = entries.size();
4252 for (size_t j = 0; j < entryCount; j++) {
4253 const sp<Entry>& oldEntry = entries[j];
4254 sp<Entry> entry = privateAttrType->getEntry(
4255 cl->getName(), oldEntry->getPos(), &entries.keyAt(j));
4259 // Move the symbols to the new type.
4264 sp<ResourceTable::Package> ResourceTable::getPackage(const String16& package)
4266 if (package != mAssetsPackage) {
4269 return mPackages.valueFor(package);
4272 sp<ResourceTable::Type> ResourceTable::getType(const String16& package,
4273 const String16& type,
4274 const SourcePos& sourcePos,
4277 sp<Package> p = getPackage(package);
4281 return p->getType(type, sourcePos, doSetIndex);
4284 sp<ResourceTable::Entry> ResourceTable::getEntry(const String16& package,
4285 const String16& type,
4286 const String16& name,
4287 const SourcePos& sourcePos,
4289 const ResTable_config* config,
4292 sp<Type> t = getType(package, type, sourcePos, doSetIndex);
4296 return t->getEntry(name, sourcePos, config, doSetIndex, overlay, mBundle->getAutoAddOverlay());
4299 sp<ResourceTable::ConfigList> ResourceTable::getConfigList(const String16& package,
4300 const String16& type, const String16& name) const
4302 const size_t packageCount = mOrderedPackages.size();
4303 for (size_t pi = 0; pi < packageCount; pi++) {
4304 const sp<Package>& p = mOrderedPackages[pi];
4305 if (p == NULL || p->getName() != package) {
4309 const Vector<sp<Type> >& types = p->getOrderedTypes();
4310 const size_t typeCount = types.size();
4311 for (size_t ti = 0; ti < typeCount; ti++) {
4312 const sp<Type>& t = types[ti];
4313 if (t == NULL || t->getName() != type) {
4317 const Vector<sp<ConfigList> >& configs = t->getOrderedConfigs();
4318 const size_t configCount = configs.size();
4319 for (size_t ci = 0; ci < configCount; ci++) {
4320 const sp<ConfigList>& cl = configs[ci];
4321 if (cl == NULL || cl->getName() != name) {
4332 sp<const ResourceTable::Entry> ResourceTable::getEntry(uint32_t resID,
4333 const ResTable_config* config) const
4335 size_t pid = Res_GETPACKAGE(resID)+1;
4336 const size_t N = mOrderedPackages.size();
4338 for (size_t i = 0; i < N; i++) {
4339 sp<Package> check = mOrderedPackages[i];
4340 if (check->getAssignedId() == pid) {
4347 fprintf(stderr, "warning: Package not found for resource #%08x\n", resID);
4351 int tid = Res_GETTYPE(resID);
4352 if (tid < 0 || tid >= (int)p->getOrderedTypes().size()) {
4353 fprintf(stderr, "warning: Type not found for resource #%08x\n", resID);
4356 sp<Type> t = p->getOrderedTypes()[tid];
4358 int eid = Res_GETENTRY(resID);
4359 if (eid < 0 || eid >= (int)t->getOrderedConfigs().size()) {
4360 fprintf(stderr, "warning: Entry not found for resource #%08x\n", resID);
4364 sp<ConfigList> c = t->getOrderedConfigs()[eid];
4366 fprintf(stderr, "warning: Entry not found for resource #%08x\n", resID);
4370 ConfigDescription cdesc;
4371 if (config) cdesc = *config;
4372 sp<Entry> e = c->getEntries().valueFor(cdesc);
4374 fprintf(stderr, "warning: Entry configuration not found for resource #%08x\n", resID);
4381 const ResourceTable::Item* ResourceTable::getItem(uint32_t resID, uint32_t attrID) const
4383 sp<const Entry> e = getEntry(resID);
4388 const size_t N = e->getBag().size();
4389 for (size_t i=0; i<N; i++) {
4390 const Item& it = e->getBag().valueAt(i);
4391 if (it.bagKeyId == 0) {
4392 fprintf(stderr, "warning: ID not yet assigned to '%s' in bag '%s'\n",
4393 String8(e->getName()).string(),
4394 String8(e->getBag().keyAt(i)).string());
4396 if (it.bagKeyId == attrID) {
4404 bool ResourceTable::getItemValue(
4405 uint32_t resID, uint32_t attrID, Res_value* outValue)
4407 const Item* item = getItem(resID, attrID);
4411 if (item->evaluating) {
4412 sp<const Entry> e = getEntry(resID);
4413 const size_t N = e->getBag().size();
4415 for (i=0; i<N; i++) {
4416 if (&e->getBag().valueAt(i) == item) {
4420 fprintf(stderr, "warning: Circular reference detected in key '%s' of bag '%s'\n",
4421 String8(e->getName()).string(),
4422 String8(e->getBag().keyAt(i)).string());
4425 item->evaluating = true;
4426 res = stringToValue(outValue, NULL, item->value, false, false, item->bagKeyId);
4429 printf("getItemValue of #%08x[#%08x] (%s): type=#%08x, data=#%08x\n",
4430 resID, attrID, String8(getEntry(resID)->getName()).string(),
4431 outValue->dataType, outValue->data);
4433 printf("getItemValue of #%08x[#%08x]: failed\n",
4437 item->evaluating = false;
4443 * Returns the SDK version at which the attribute was
4444 * made public, or -1 if the resource ID is not an attribute
4447 int ResourceTable::getPublicAttributeSdkLevel(uint32_t attrId) const {
4448 if (Res_GETPACKAGE(attrId) + 1 != 0x01 || Res_GETTYPE(attrId) + 1 != 0x01) {
4453 if (!mAssets->getIncludedResources().getResourceFlags(attrId, &specFlags)) {
4457 if ((specFlags & ResTable_typeSpec::SPEC_PUBLIC) == 0) {
4461 const size_t entryId = Res_GETENTRY(attrId);
4462 if (entryId <= 0x021c) {
4464 } else if (entryId <= 0x021d) {
4466 } else if (entryId <= 0x0269) {
4468 } else if (entryId <= 0x028d) {
4470 } else if (entryId <= 0x02ad) {
4472 } else if (entryId <= 0x02b3) {
4473 return SDK_ECLAIR_0_1;
4474 } else if (entryId <= 0x02b5) {
4475 return SDK_ECLAIR_MR1;
4476 } else if (entryId <= 0x02bd) {
4478 } else if (entryId <= 0x02cb) {
4479 return SDK_GINGERBREAD;
4480 } else if (entryId <= 0x0361) {
4481 return SDK_HONEYCOMB;
4482 } else if (entryId <= 0x0366) {
4483 return SDK_HONEYCOMB_MR1;
4484 } else if (entryId <= 0x03a6) {
4485 return SDK_HONEYCOMB_MR2;
4486 } else if (entryId <= 0x03ae) {
4487 return SDK_JELLY_BEAN;
4488 } else if (entryId <= 0x03cc) {
4489 return SDK_JELLY_BEAN_MR1;
4490 } else if (entryId <= 0x03da) {
4491 return SDK_JELLY_BEAN_MR2;
4492 } else if (entryId <= 0x03f1) {
4494 } else if (entryId <= 0x03f6) {
4495 return SDK_KITKAT_WATCH;
4496 } else if (entryId <= 0x04ce) {
4497 return SDK_LOLLIPOP;
4499 // Anything else is marked as defined in
4500 // SDK_LOLLIPOP_MR1 since after this
4501 // version no attribute compat work
4502 // needs to be done.
4503 return SDK_LOLLIPOP_MR1;
4508 * First check the Manifest, then check the command line flag.
4510 static int getMinSdkVersion(const Bundle* bundle) {
4511 if (bundle->getManifestMinSdkVersion() != NULL && strlen(bundle->getManifestMinSdkVersion()) > 0) {
4512 return atoi(bundle->getManifestMinSdkVersion());
4513 } else if (bundle->getMinSdkVersion() != NULL && strlen(bundle->getMinSdkVersion()) > 0) {
4514 return atoi(bundle->getMinSdkVersion());
4519 bool ResourceTable::shouldGenerateVersionedResource(
4520 const sp<ResourceTable::ConfigList>& configList,
4521 const ConfigDescription& sourceConfig,
4522 const int sdkVersionToGenerate) {
4523 assert(sdkVersionToGenerate > sourceConfig.sdkVersion);
4524 const DefaultKeyedVector<ConfigDescription, sp<ResourceTable::Entry>>& entries
4525 = configList->getEntries();
4526 ssize_t idx = entries.indexOfKey(sourceConfig);
4528 // The source config came from this list, so it should be here.
4531 // The next configuration either only varies in sdkVersion, or it is completely different
4532 // and therefore incompatible. If it is incompatible, we must generate the versioned resource.
4534 // NOTE: The ordering of configurations takes sdkVersion as higher precedence than other
4535 // qualifiers, so we need to iterate through the entire list to be sure there
4536 // are no higher sdk level versions of this resource.
4537 ConfigDescription tempConfig(sourceConfig);
4538 for (size_t i = static_cast<size_t>(idx) + 1; i < entries.size(); i++) {
4539 const ConfigDescription& nextConfig = entries.keyAt(i);
4540 tempConfig.sdkVersion = nextConfig.sdkVersion;
4541 if (tempConfig == nextConfig) {
4542 // The two configs are the same, check the sdk version.
4543 return sdkVersionToGenerate < nextConfig.sdkVersion;
4547 // No match was found, so we should generate the versioned resource.
4552 * Modifies the entries in the resource table to account for compatibility
4553 * issues with older versions of Android.
4555 * This primarily handles the issue of private/public attribute clashes
4556 * in framework resources.
4558 * AAPT has traditionally assigned resource IDs to public attributes,
4559 * and then followed those public definitions with private attributes.
4562 * | 0x01010234 | attr/color
4563 * | 0x01010235 | attr/background
4566 * | 0x01010236 | attr/secret
4567 * | 0x01010237 | attr/shhh
4569 * Each release, when attributes are added, they take the place of the private
4570 * attributes and the private attributes are shifted down again.
4573 * | 0x01010234 | attr/color
4574 * | 0x01010235 | attr/background
4575 * | 0x01010236 | attr/shinyNewAttr
4576 * | 0x01010237 | attr/highlyValuedFeature
4579 * | 0x01010238 | attr/secret
4580 * | 0x01010239 | attr/shhh
4582 * Platform code may look for private attributes set in a theme. If an app
4583 * compiled against a newer version of the platform uses a new public
4584 * attribute that happens to have the same ID as the private attribute
4585 * the older platform is expecting, then the behavior is undefined.
4587 * We get around this by detecting any newly defined attributes (in L),
4588 * copy the resource into a -v21 qualified resource, and delete the
4589 * attribute from the original resource. This ensures that older platforms
4590 * don't see the new attribute, but when running on L+ platforms, the
4591 * attribute will be respected.
4593 status_t ResourceTable::modifyForCompat(const Bundle* bundle) {
4594 const int minSdk = getMinSdkVersion(bundle);
4595 if (minSdk >= SDK_LOLLIPOP_MR1) {
4596 // Lollipop MR1 and up handles public attributes differently, no
4597 // need to do any compat modifications.
4601 const String16 attr16("attr");
4603 const size_t packageCount = mOrderedPackages.size();
4604 for (size_t pi = 0; pi < packageCount; pi++) {
4605 sp<Package> p = mOrderedPackages.itemAt(pi);
4606 if (p == NULL || p->getTypes().size() == 0) {
4611 const size_t typeCount = p->getOrderedTypes().size();
4612 for (size_t ti = 0; ti < typeCount; ti++) {
4613 sp<Type> t = p->getOrderedTypes().itemAt(ti);
4618 const size_t configCount = t->getOrderedConfigs().size();
4619 for (size_t ci = 0; ci < configCount; ci++) {
4620 sp<ConfigList> c = t->getOrderedConfigs().itemAt(ci);
4625 Vector<key_value_pair_t<ConfigDescription, sp<Entry> > > entriesToAdd;
4626 const DefaultKeyedVector<ConfigDescription, sp<Entry> >& entries =
4628 const size_t entryCount = entries.size();
4629 for (size_t ei = 0; ei < entryCount; ei++) {
4630 sp<Entry> e = entries.valueAt(ei);
4631 if (e == NULL || e->getType() != Entry::TYPE_BAG) {
4635 const ConfigDescription& config = entries.keyAt(ei);
4636 if (config.sdkVersion >= SDK_LOLLIPOP_MR1) {
4640 KeyedVector<int, Vector<String16> > attributesToRemove;
4641 const KeyedVector<String16, Item>& bag = e->getBag();
4642 const size_t bagCount = bag.size();
4643 for (size_t bi = 0; bi < bagCount; bi++) {
4644 const uint32_t attrId = getResId(bag.keyAt(bi), &attr16);
4645 const int sdkLevel = getPublicAttributeSdkLevel(attrId);
4646 if (sdkLevel > 1 && sdkLevel > config.sdkVersion && sdkLevel > minSdk) {
4647 AaptUtil::appendValue(attributesToRemove, sdkLevel, bag.keyAt(bi));
4651 if (attributesToRemove.isEmpty()) {
4655 const size_t sdkCount = attributesToRemove.size();
4656 for (size_t i = 0; i < sdkCount; i++) {
4657 const int sdkLevel = attributesToRemove.keyAt(i);
4659 if (!shouldGenerateVersionedResource(c, config, sdkLevel)) {
4660 // There is a style that will override this generated one.
4664 // Duplicate the entry under the same configuration
4665 // but with sdkVersion == sdkLevel.
4666 ConfigDescription newConfig(config);
4667 newConfig.sdkVersion = sdkLevel;
4669 sp<Entry> newEntry = new Entry(*e);
4671 // Remove all items that have a higher SDK level than
4672 // the one we are synthesizing.
4673 for (size_t j = 0; j < sdkCount; j++) {
4678 if (attributesToRemove.keyAt(j) > sdkLevel) {
4679 const size_t attrCount = attributesToRemove[j].size();
4680 for (size_t k = 0; k < attrCount; k++) {
4681 newEntry->removeFromBag(attributesToRemove[j][k]);
4686 entriesToAdd.add(key_value_pair_t<ConfigDescription, sp<Entry> >(
4687 newConfig, newEntry));
4690 // Remove the attribute from the original.
4691 for (size_t i = 0; i < attributesToRemove.size(); i++) {
4692 for (size_t j = 0; j < attributesToRemove[i].size(); j++) {
4693 e->removeFromBag(attributesToRemove[i][j]);
4698 const size_t entriesToAddCount = entriesToAdd.size();
4699 for (size_t i = 0; i < entriesToAddCount; i++) {
4700 assert(entries.indexOfKey(entriesToAdd[i].key) < 0);
4702 if (bundle->getVerbose()) {
4703 entriesToAdd[i].value->getPos()
4704 .printf("using v%d attributes; synthesizing resource %s:%s/%s for configuration %s.",
4705 entriesToAdd[i].key.sdkVersion,
4706 String8(p->getName()).string(),
4707 String8(t->getName()).string(),
4708 String8(entriesToAdd[i].value->getName()).string(),
4709 entriesToAdd[i].key.toString().string());
4712 sp<Entry> newEntry = t->getEntry(c->getName(),
4713 entriesToAdd[i].value->getPos(),
4714 &entriesToAdd[i].key);
4716 *newEntry = *entriesToAdd[i].value;
4724 status_t ResourceTable::modifyForCompat(const Bundle* bundle,
4725 const String16& resourceName,
4726 const sp<AaptFile>& target,
4727 const sp<XMLNode>& root) {
4728 const String16 vector16("vector");
4729 const String16 animatedVector16("animated-vector");
4731 const int minSdk = getMinSdkVersion(bundle);
4732 if (minSdk >= SDK_LOLLIPOP_MR1) {
4733 // Lollipop MR1 and up handles public attributes differently, no
4734 // need to do any compat modifications.
4738 const ConfigDescription config(target->getGroupEntry().toParams());
4739 if (target->getResourceType() == "" || config.sdkVersion >= SDK_LOLLIPOP_MR1) {
4740 // Skip resources that have no type (AndroidManifest.xml) or are already version qualified
4741 // with v21 or higher.
4745 sp<XMLNode> newRoot = NULL;
4746 int sdkVersionToGenerate = SDK_LOLLIPOP_MR1;
4748 Vector<sp<XMLNode> > nodesToVisit;
4749 nodesToVisit.push(root);
4750 while (!nodesToVisit.isEmpty()) {
4751 sp<XMLNode> node = nodesToVisit.top();
4754 if (bundle->getNoVersionVectors() && (node->getElementName() == vector16 ||
4755 node->getElementName() == animatedVector16)) {
4756 // We were told not to version vector tags, so skip the children here.
4760 const Vector<XMLNode::attribute_entry>& attrs = node->getAttributes();
4761 for (size_t i = 0; i < attrs.size(); i++) {
4762 const XMLNode::attribute_entry& attr = attrs[i];
4763 const int sdkLevel = getPublicAttributeSdkLevel(attr.nameResId);
4764 if (sdkLevel > 1 && sdkLevel > config.sdkVersion && sdkLevel > minSdk) {
4765 if (newRoot == NULL) {
4766 newRoot = root->clone();
4769 // Find the smallest sdk version that we need to synthesize for
4770 // and do that one. Subsequent versions will be processed on
4772 sdkVersionToGenerate = std::min(sdkLevel, sdkVersionToGenerate);
4774 if (bundle->getVerbose()) {
4775 SourcePos(node->getFilename(), node->getStartLineNumber()).printf(
4776 "removing attribute %s%s%s from <%s>",
4777 String8(attr.ns).string(),
4778 (attr.ns.size() == 0 ? "" : ":"),
4779 String8(attr.name).string(),
4780 String8(node->getElementName()).string());
4782 node->removeAttribute(i);
4787 // Schedule a visit to the children.
4788 const Vector<sp<XMLNode> >& children = node->getChildren();
4789 const size_t childCount = children.size();
4790 for (size_t i = 0; i < childCount; i++) {
4791 nodesToVisit.push(children[i]);
4795 if (newRoot == NULL) {
4799 // Look to see if we already have an overriding v21 configuration.
4800 sp<ConfigList> cl = getConfigList(String16(mAssets->getPackage()),
4801 String16(target->getResourceType()), resourceName);
4802 if (shouldGenerateVersionedResource(cl, config, sdkVersionToGenerate)) {
4803 // We don't have an overriding entry for v21, so we must duplicate this one.
4804 ConfigDescription newConfig(config);
4805 newConfig.sdkVersion = sdkVersionToGenerate;
4806 sp<AaptFile> newFile = new AaptFile(target->getSourceFile(),
4807 AaptGroupEntry(newConfig), target->getResourceType());
4808 String8 resPath = String8::format("res/%s/%s.xml",
4809 newFile->getGroupEntry().toDirName(target->getResourceType()).string(),
4810 String8(resourceName).string());
4811 resPath.convertToResPath();
4813 // Add a resource table entry.
4814 if (bundle->getVerbose()) {
4815 SourcePos(target->getSourceFile(), -1).printf(
4816 "using v%d attributes; synthesizing resource %s:%s/%s for configuration %s.",
4817 newConfig.sdkVersion,
4818 mAssets->getPackage().string(),
4819 newFile->getResourceType().string(),
4820 String8(resourceName).string(),
4821 newConfig.toString().string());
4824 addEntry(SourcePos(),
4825 String16(mAssets->getPackage()),
4826 String16(target->getResourceType()),
4832 // Schedule this to be compiled.
4833 CompileResourceWorkItem item;
4834 item.resourceName = resourceName;
4835 item.resPath = resPath;
4836 item.file = newFile;
4837 item.xmlRoot = newRoot;
4838 item.needsCompiling = false; // This step occurs after we parse/assign, so we don't need
4840 mWorkQueue.push(item);
4845 void ResourceTable::getDensityVaryingResources(
4846 KeyedVector<Symbol, Vector<SymbolDefinition> >& resources) {
4847 const ConfigDescription nullConfig;
4849 const size_t packageCount = mOrderedPackages.size();
4850 for (size_t p = 0; p < packageCount; p++) {
4851 const Vector<sp<Type> >& types = mOrderedPackages[p]->getOrderedTypes();
4852 const size_t typeCount = types.size();
4853 for (size_t t = 0; t < typeCount; t++) {
4854 const Vector<sp<ConfigList> >& configs = types[t]->getOrderedConfigs();
4855 const size_t configCount = configs.size();
4856 for (size_t c = 0; c < configCount; c++) {
4857 const DefaultKeyedVector<ConfigDescription, sp<Entry> >& configEntries
4858 = configs[c]->getEntries();
4859 const size_t configEntryCount = configEntries.size();
4860 for (size_t ce = 0; ce < configEntryCount; ce++) {
4861 const ConfigDescription& config = configEntries.keyAt(ce);
4862 if (AaptConfig::isDensityOnly(config)) {
4863 // This configuration only varies with regards to density.
4864 const Symbol symbol(
4865 mOrderedPackages[p]->getName(),
4866 types[t]->getName(),
4867 configs[c]->getName(),
4868 getResId(mOrderedPackages[p], types[t],
4869 configs[c]->getEntryIndex()));
4871 const sp<Entry>& entry = configEntries.valueAt(ce);
4872 AaptUtil::appendValue(resources, symbol,
4873 SymbolDefinition(symbol, config, entry->getPos()));
4881 static String16 buildNamespace(const String16& package) {
4882 return String16("http://schemas.android.com/apk/res/") + package;
4885 static sp<XMLNode> findOnlyChildElement(const sp<XMLNode>& parent) {
4886 const Vector<sp<XMLNode> >& children = parent->getChildren();
4887 sp<XMLNode> onlyChild;
4888 for (size_t i = 0; i < children.size(); i++) {
4889 if (children[i]->getType() != XMLNode::TYPE_CDATA) {
4890 if (onlyChild != NULL) {
4893 onlyChild = children[i];
4900 * Detects use of the `bundle' format and extracts nested resources into their own top level
4901 * resources. The bundle format looks like this:
4903 * <!-- res/drawable/bundle.xml -->
4904 * <animated-vector xmlns:aapt="http://schemas.android.com/aapt">
4905 * <aapt:attr name="android:drawable">
4906 * <vector android:width="60dp"
4907 * android:height="60dp">
4908 * <path android:name="v"
4909 * android:fillColor="#000000"
4910 * android:pathData="M300,70 l 0,-70 70,..." />
4913 * </animated-vector>
4915 * When AAPT sees the <aapt:attr> tag, it will extract its single element and its children
4916 * into a new high-level resource, assigning it a name and ID. Then value of the `name`
4917 * attribute must be a resource attribute. That resource attribute is inserted into the parent
4918 * with the reference to the extracted resource as the value.
4920 * <!-- res/drawable/bundle.xml -->
4921 * <animated-vector android:drawable="@drawable/bundle_1.xml">
4922 * </animated-vector>
4924 * <!-- res/drawable/bundle_1.xml -->
4925 * <vector android:width="60dp"
4926 * android:height="60dp">
4927 * <path android:name="v"
4928 * android:fillColor="#000000"
4929 * android:pathData="M300,70 l 0,-70 70,..." />
4932 status_t ResourceTable::processBundleFormat(const Bundle* bundle,
4933 const String16& resourceName,
4934 const sp<AaptFile>& target,
4935 const sp<XMLNode>& root) {
4936 Vector<sp<XMLNode> > namespaces;
4937 if (root->getType() == XMLNode::TYPE_NAMESPACE) {
4938 namespaces.push(root);
4940 return processBundleFormatImpl(bundle, resourceName, target, root, &namespaces);
4943 status_t ResourceTable::processBundleFormatImpl(const Bundle* bundle,
4944 const String16& resourceName,
4945 const sp<AaptFile>& target,
4946 const sp<XMLNode>& parent,
4947 Vector<sp<XMLNode> >* namespaces) {
4948 const String16 kAaptNamespaceUri16("http://schemas.android.com/aapt");
4949 const String16 kName16("name");
4950 const String16 kAttr16("attr");
4951 const String16 kAssetPackage16(mAssets->getPackage());
4953 Vector<sp<XMLNode> >& children = parent->getChildren();
4954 for (size_t i = 0; i < children.size(); i++) {
4955 const sp<XMLNode>& child = children[i];
4957 if (child->getType() == XMLNode::TYPE_CDATA) {
4959 } else if (child->getType() == XMLNode::TYPE_NAMESPACE) {
4960 namespaces->push(child);
4963 if (child->getElementNamespace() != kAaptNamespaceUri16 ||
4964 child->getElementName() != kAttr16) {
4965 status_t result = processBundleFormatImpl(bundle, resourceName, target, child,
4967 if (result != NO_ERROR) {
4971 if (child->getType() == XMLNode::TYPE_NAMESPACE) {
4977 // This is the <aapt:attr> tag. Look for the 'name' attribute.
4978 SourcePos source(child->getFilename(), child->getStartLineNumber());
4980 sp<XMLNode> nestedRoot = findOnlyChildElement(child);
4981 if (nestedRoot == NULL) {
4982 source.error("<%s:%s> must have exactly one child element",
4983 String8(child->getElementNamespace()).string(),
4984 String8(child->getElementName()).string());
4985 return UNKNOWN_ERROR;
4988 // Find the special attribute 'parent-attr'. This attribute's value contains
4989 // the resource attribute for which this element should be assigned in the parent.
4990 const XMLNode::attribute_entry* attr = child->getAttribute(String16(), kName16);
4992 source.error("inline resource definition must specify an attribute via 'name'");
4993 return UNKNOWN_ERROR;
4996 // Parse the attribute name.
4997 const char* errorMsg = NULL;
4998 String16 attrPackage, attrType, attrName;
4999 bool result = ResTable::expandResourceRef(attr->string.string(),
5000 attr->string.size(),
5001 &attrPackage, &attrType, &attrName,
5002 &kAttr16, &kAssetPackage16,
5005 source.error("invalid attribute name for 'name': %s", errorMsg);
5006 return UNKNOWN_ERROR;
5009 if (attrType != kAttr16) {
5010 // The value of the 'name' attribute must be an attribute reference.
5011 source.error("value of 'name' must be an attribute reference.");
5012 return UNKNOWN_ERROR;
5015 // Generate a name for this nested resource and try to add it to the table.
5016 // We do this in a loop because the name may be taken, in which case we will
5017 // increment a suffix until we succeed.
5018 String8 nestedResourceName;
5019 String8 nestedResourcePath;
5022 // This child element will be extracted into its own resource file.
5023 // Generate a name and path for it from its parent.
5024 nestedResourceName = String8::format("%s_%d",
5025 String8(resourceName).string(), suffix++);
5026 nestedResourcePath = String8::format("res/%s/%s.xml",
5027 target->getGroupEntry().toDirName(target->getResourceType())
5029 nestedResourceName.string());
5031 // Lookup or create the entry for this name.
5032 sp<Entry> entry = getEntry(kAssetPackage16,
5033 String16(target->getResourceType()),
5034 String16(nestedResourceName),
5037 &target->getGroupEntry().toParams(),
5039 if (entry == NULL) {
5040 return UNKNOWN_ERROR;
5043 if (entry->getType() == Entry::TYPE_UNKNOWN) {
5044 // The value for this resource has never been set,
5045 // meaning we're good!
5046 entry->setItem(source, String16(nestedResourcePath));
5050 // We failed (name already exists), so try with a different name
5051 // (increment the suffix).
5054 if (bundle->getVerbose()) {
5055 source.printf("generating nested resource %s:%s/%s",
5056 mAssets->getPackage().string(), target->getResourceType().string(),
5057 nestedResourceName.string());
5060 // Build the attribute reference and assign it to the parent.
5061 String16 nestedResourceRef = String16(String8::format("@%s:%s/%s",
5062 mAssets->getPackage().string(), target->getResourceType().string(),
5063 nestedResourceName.string()));
5065 String16 attrNs = buildNamespace(attrPackage);
5066 if (parent->getAttribute(attrNs, attrName) != NULL) {
5067 SourcePos(parent->getFilename(), parent->getStartLineNumber())
5068 .error("parent of nested resource already defines attribute '%s:%s'",
5069 String8(attrPackage).string(), String8(attrName).string());
5070 return UNKNOWN_ERROR;
5073 // Add the reference to the inline resource.
5074 parent->addAttribute(attrNs, attrName, nestedResourceRef);
5076 // Remove the <aapt:attr> child element from here.
5077 children.removeAt(i);
5080 // Append all namespace declarations that we've seen on this branch in the XML tree
5081 // to this resource.
5082 // We do this because the order of namespace declarations and prefix usage is determined
5083 // by the developer and we do not want to override any decisions. Be conservative.
5084 for (size_t nsIndex = namespaces->size(); nsIndex > 0; nsIndex--) {
5085 const sp<XMLNode>& ns = namespaces->itemAt(nsIndex - 1);
5086 sp<XMLNode> newNs = XMLNode::newNamespace(ns->getFilename(), ns->getNamespacePrefix(),
5087 ns->getNamespaceUri());
5088 newNs->addChild(nestedRoot);
5092 // Schedule compilation of the nested resource.
5093 CompileResourceWorkItem workItem;
5094 workItem.resPath = nestedResourcePath;
5095 workItem.resourceName = String16(nestedResourceName);
5096 workItem.xmlRoot = nestedRoot;
5097 workItem.file = new AaptFile(target->getSourceFile(), target->getGroupEntry(),
5098 target->getResourceType());
5099 mWorkQueue.push(workItem);