ResourceTable.cpp revision a01a9374fd386f3a8773528d7a49bc5315492dff
1// 2// Copyright 2006 The Android Open Source Project 3// 4// Build resource files from raw assets. 5// 6 7#include "ResourceTable.h" 8 9#include "XMLNode.h" 10#include "ResourceFilter.h" 11#include "ResourceIdCache.h" 12 13#include <androidfw/ResourceTypes.h> 14#include <utils/ByteOrder.h> 15#include <stdarg.h> 16 17#define NOISY(x) //x 18 19status_t compileXmlFile(const sp<AaptAssets>& assets, 20 const sp<AaptFile>& target, 21 ResourceTable* table, 22 int options) 23{ 24 sp<XMLNode> root = XMLNode::parse(target); 25 if (root == NULL) { 26 return UNKNOWN_ERROR; 27 } 28 29 return compileXmlFile(assets, root, target, table, options); 30} 31 32status_t compileXmlFile(const sp<AaptAssets>& assets, 33 const sp<AaptFile>& target, 34 const sp<AaptFile>& outTarget, 35 ResourceTable* table, 36 int options) 37{ 38 sp<XMLNode> root = XMLNode::parse(target); 39 if (root == NULL) { 40 return UNKNOWN_ERROR; 41 } 42 43 return compileXmlFile(assets, root, outTarget, table, options); 44} 45 46status_t compileXmlFile(const sp<AaptAssets>& assets, 47 const sp<XMLNode>& root, 48 const sp<AaptFile>& target, 49 ResourceTable* table, 50 int options) 51{ 52 if ((options&XML_COMPILE_STRIP_WHITESPACE) != 0) { 53 root->removeWhitespace(true, NULL); 54 } else if ((options&XML_COMPILE_COMPACT_WHITESPACE) != 0) { 55 root->removeWhitespace(false, NULL); 56 } 57 58 if ((options&XML_COMPILE_UTF8) != 0) { 59 root->setUTF8(true); 60 } 61 62 bool hasErrors = false; 63 64 if ((options&XML_COMPILE_ASSIGN_ATTRIBUTE_IDS) != 0) { 65 status_t err = root->assignResourceIds(assets, table); 66 if (err != NO_ERROR) { 67 hasErrors = true; 68 } 69 } 70 71 status_t err = root->parseValues(assets, table); 72 if (err != NO_ERROR) { 73 hasErrors = true; 74 } 75 76 if (hasErrors) { 77 return UNKNOWN_ERROR; 78 } 79 80 NOISY(printf("Input XML Resource:\n")); 81 NOISY(root->print()); 82 err = root->flatten(target, 83 (options&XML_COMPILE_STRIP_COMMENTS) != 0, 84 (options&XML_COMPILE_STRIP_RAW_VALUES) != 0); 85 if (err != NO_ERROR) { 86 return err; 87 } 88 89 NOISY(printf("Output XML Resource:\n")); 90 NOISY(ResXMLTree tree; 91 tree.setTo(target->getData(), target->getSize()); 92 printXMLBlock(&tree)); 93 94 target->setCompressionMethod(ZipEntry::kCompressDeflated); 95 96 return err; 97} 98 99#undef NOISY 100#define NOISY(x) //x 101 102struct flag_entry 103{ 104 const char16_t* name; 105 size_t nameLen; 106 uint32_t value; 107 const char* description; 108}; 109 110static const char16_t referenceArray[] = 111 { 'r', 'e', 'f', 'e', 'r', 'e', 'n', 'c', 'e' }; 112static const char16_t stringArray[] = 113 { 's', 't', 'r', 'i', 'n', 'g' }; 114static const char16_t integerArray[] = 115 { 'i', 'n', 't', 'e', 'g', 'e', 'r' }; 116static const char16_t booleanArray[] = 117 { 'b', 'o', 'o', 'l', 'e', 'a', 'n' }; 118static const char16_t colorArray[] = 119 { 'c', 'o', 'l', 'o', 'r' }; 120static const char16_t floatArray[] = 121 { 'f', 'l', 'o', 'a', 't' }; 122static const char16_t dimensionArray[] = 123 { 'd', 'i', 'm', 'e', 'n', 's', 'i', 'o', 'n' }; 124static const char16_t fractionArray[] = 125 { 'f', 'r', 'a', 'c', 't', 'i', 'o', 'n' }; 126static const char16_t enumArray[] = 127 { 'e', 'n', 'u', 'm' }; 128static const char16_t flagsArray[] = 129 { 'f', 'l', 'a', 'g', 's' }; 130 131static const flag_entry gFormatFlags[] = { 132 { referenceArray, sizeof(referenceArray)/2, ResTable_map::TYPE_REFERENCE, 133 "a reference to another resource, in the form \"<code>@[+][<i>package</i>:]<i>type</i>:<i>name</i></code>\"\n" 134 "or to a theme attribute in the form \"<code>?[<i>package</i>:][<i>type</i>:]<i>name</i></code>\"."}, 135 { stringArray, sizeof(stringArray)/2, ResTable_map::TYPE_STRING, 136 "a string value, using '\\\\;' to escape characters such as '\\\\n' or '\\\\uxxxx' for a unicode character." }, 137 { integerArray, sizeof(integerArray)/2, ResTable_map::TYPE_INTEGER, 138 "an integer value, such as \"<code>100</code>\"." }, 139 { booleanArray, sizeof(booleanArray)/2, ResTable_map::TYPE_BOOLEAN, 140 "a boolean value, either \"<code>true</code>\" or \"<code>false</code>\"." }, 141 { colorArray, sizeof(colorArray)/2, ResTable_map::TYPE_COLOR, 142 "a color value, in the form of \"<code>#<i>rgb</i></code>\", \"<code>#<i>argb</i></code>\",\n" 143 "\"<code>#<i>rrggbb</i></code>\", or \"<code>#<i>aarrggbb</i></code>\"." }, 144 { floatArray, sizeof(floatArray)/2, ResTable_map::TYPE_FLOAT, 145 "a floating point value, such as \"<code>1.2</code>\"."}, 146 { dimensionArray, sizeof(dimensionArray)/2, ResTable_map::TYPE_DIMENSION, 147 "a dimension value, which is a floating point number appended with a unit such as \"<code>14.5sp</code>\".\n" 148 "Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size),\n" 149 "in (inches), mm (millimeters)." }, 150 { fractionArray, sizeof(fractionArray)/2, ResTable_map::TYPE_FRACTION, 151 "a fractional value, which is a floating point number appended with either % or %p, such as \"<code>14.5%</code>\".\n" 152 "The % suffix always means a percentage of the base size; the optional %p suffix provides a size relative to\n" 153 "some parent container." }, 154 { enumArray, sizeof(enumArray)/2, ResTable_map::TYPE_ENUM, NULL }, 155 { flagsArray, sizeof(flagsArray)/2, ResTable_map::TYPE_FLAGS, NULL }, 156 { NULL, 0, 0, NULL } 157}; 158 159static const char16_t suggestedArray[] = { 's', 'u', 'g', 'g', 'e', 's', 't', 'e', 'd' }; 160 161static const flag_entry l10nRequiredFlags[] = { 162 { suggestedArray, sizeof(suggestedArray)/2, ResTable_map::L10N_SUGGESTED, NULL }, 163 { NULL, 0, 0, NULL } 164}; 165 166static const char16_t nulStr[] = { 0 }; 167 168static uint32_t parse_flags(const char16_t* str, size_t len, 169 const flag_entry* flags, bool* outError = NULL) 170{ 171 while (len > 0 && isspace(*str)) { 172 str++; 173 len--; 174 } 175 while (len > 0 && isspace(str[len-1])) { 176 len--; 177 } 178 179 const char16_t* const end = str + len; 180 uint32_t value = 0; 181 182 while (str < end) { 183 const char16_t* div = str; 184 while (div < end && *div != '|') { 185 div++; 186 } 187 188 const flag_entry* cur = flags; 189 while (cur->name) { 190 if (strzcmp16(cur->name, cur->nameLen, str, div-str) == 0) { 191 value |= cur->value; 192 break; 193 } 194 cur++; 195 } 196 197 if (!cur->name) { 198 if (outError) *outError = true; 199 return 0; 200 } 201 202 str = div < end ? div+1 : div; 203 } 204 205 if (outError) *outError = false; 206 return value; 207} 208 209static String16 mayOrMust(int type, int flags) 210{ 211 if ((type&(~flags)) == 0) { 212 return String16("<p>Must"); 213 } 214 215 return String16("<p>May"); 216} 217 218static void appendTypeInfo(ResourceTable* outTable, const String16& pkg, 219 const String16& typeName, const String16& ident, int type, 220 const flag_entry* flags) 221{ 222 bool hadType = false; 223 while (flags->name) { 224 if ((type&flags->value) != 0 && flags->description != NULL) { 225 String16 fullMsg(mayOrMust(type, flags->value)); 226 fullMsg.append(String16(" be ")); 227 fullMsg.append(String16(flags->description)); 228 outTable->appendTypeComment(pkg, typeName, ident, fullMsg); 229 hadType = true; 230 } 231 flags++; 232 } 233 if (hadType && (type&ResTable_map::TYPE_REFERENCE) == 0) { 234 outTable->appendTypeComment(pkg, typeName, ident, 235 String16("<p>This may also be a reference to a resource (in the form\n" 236 "\"<code>@[<i>package</i>:]<i>type</i>:<i>name</i></code>\") or\n" 237 "theme attribute (in the form\n" 238 "\"<code>?[<i>package</i>:][<i>type</i>:]<i>name</i></code>\")\n" 239 "containing a value of this type.")); 240 } 241} 242 243struct PendingAttribute 244{ 245 const String16 myPackage; 246 const SourcePos sourcePos; 247 const bool appendComment; 248 int32_t type; 249 String16 ident; 250 String16 comment; 251 bool hasErrors; 252 bool added; 253 254 PendingAttribute(String16 _package, const sp<AaptFile>& in, 255 ResXMLTree& block, bool _appendComment) 256 : myPackage(_package) 257 , sourcePos(in->getPrintableSource(), block.getLineNumber()) 258 , appendComment(_appendComment) 259 , type(ResTable_map::TYPE_ANY) 260 , hasErrors(false) 261 , added(false) 262 { 263 } 264 265 status_t createIfNeeded(ResourceTable* outTable) 266 { 267 if (added || hasErrors) { 268 return NO_ERROR; 269 } 270 added = true; 271 272 String16 attr16("attr"); 273 274 if (outTable->hasBagOrEntry(myPackage, attr16, ident)) { 275 sourcePos.error("Attribute \"%s\" has already been defined\n", 276 String8(ident).string()); 277 hasErrors = true; 278 return UNKNOWN_ERROR; 279 } 280 281 char numberStr[16]; 282 sprintf(numberStr, "%d", type); 283 status_t err = outTable->addBag(sourcePos, myPackage, 284 attr16, ident, String16(""), 285 String16("^type"), 286 String16(numberStr), NULL, NULL); 287 if (err != NO_ERROR) { 288 hasErrors = true; 289 return err; 290 } 291 outTable->appendComment(myPackage, attr16, ident, comment, appendComment); 292 //printf("Attribute %s comment: %s\n", String8(ident).string(), 293 // String8(comment).string()); 294 return err; 295 } 296}; 297 298static status_t compileAttribute(const sp<AaptFile>& in, 299 ResXMLTree& block, 300 const String16& myPackage, 301 ResourceTable* outTable, 302 String16* outIdent = NULL, 303 bool inStyleable = false) 304{ 305 PendingAttribute attr(myPackage, in, block, inStyleable); 306 307 const String16 attr16("attr"); 308 const String16 id16("id"); 309 310 // Attribute type constants. 311 const String16 enum16("enum"); 312 const String16 flag16("flag"); 313 314 ResXMLTree::event_code_t code; 315 size_t len; 316 status_t err; 317 318 ssize_t identIdx = block.indexOfAttribute(NULL, "name"); 319 if (identIdx >= 0) { 320 attr.ident = String16(block.getAttributeStringValue(identIdx, &len)); 321 if (outIdent) { 322 *outIdent = attr.ident; 323 } 324 } else { 325 attr.sourcePos.error("A 'name' attribute is required for <attr>\n"); 326 attr.hasErrors = true; 327 } 328 329 attr.comment = String16( 330 block.getComment(&len) ? block.getComment(&len) : nulStr); 331 332 ssize_t typeIdx = block.indexOfAttribute(NULL, "format"); 333 if (typeIdx >= 0) { 334 String16 typeStr = String16(block.getAttributeStringValue(typeIdx, &len)); 335 attr.type = parse_flags(typeStr.string(), typeStr.size(), gFormatFlags); 336 if (attr.type == 0) { 337 attr.sourcePos.error("Tag <attr> 'format' attribute value \"%s\" not valid\n", 338 String8(typeStr).string()); 339 attr.hasErrors = true; 340 } 341 attr.createIfNeeded(outTable); 342 } else if (!inStyleable) { 343 // Attribute definitions outside of styleables always define the 344 // attribute as a generic value. 345 attr.createIfNeeded(outTable); 346 } 347 348 //printf("Attribute %s: type=0x%08x\n", String8(attr.ident).string(), attr.type); 349 350 ssize_t minIdx = block.indexOfAttribute(NULL, "min"); 351 if (minIdx >= 0) { 352 String16 val = String16(block.getAttributeStringValue(minIdx, &len)); 353 if (!ResTable::stringToInt(val.string(), val.size(), NULL)) { 354 attr.sourcePos.error("Tag <attr> 'min' attribute must be a number, not \"%s\"\n", 355 String8(val).string()); 356 attr.hasErrors = true; 357 } 358 attr.createIfNeeded(outTable); 359 if (!attr.hasErrors) { 360 err = outTable->addBag(attr.sourcePos, myPackage, attr16, attr.ident, 361 String16(""), String16("^min"), String16(val), NULL, NULL); 362 if (err != NO_ERROR) { 363 attr.hasErrors = true; 364 } 365 } 366 } 367 368 ssize_t maxIdx = block.indexOfAttribute(NULL, "max"); 369 if (maxIdx >= 0) { 370 String16 val = String16(block.getAttributeStringValue(maxIdx, &len)); 371 if (!ResTable::stringToInt(val.string(), val.size(), NULL)) { 372 attr.sourcePos.error("Tag <attr> 'max' attribute must be a number, not \"%s\"\n", 373 String8(val).string()); 374 attr.hasErrors = true; 375 } 376 attr.createIfNeeded(outTable); 377 if (!attr.hasErrors) { 378 err = outTable->addBag(attr.sourcePos, myPackage, attr16, attr.ident, 379 String16(""), String16("^max"), String16(val), NULL, NULL); 380 attr.hasErrors = true; 381 } 382 } 383 384 if ((minIdx >= 0 || maxIdx >= 0) && (attr.type&ResTable_map::TYPE_INTEGER) == 0) { 385 attr.sourcePos.error("Tag <attr> must have format=integer attribute if using max or min\n"); 386 attr.hasErrors = true; 387 } 388 389 ssize_t l10nIdx = block.indexOfAttribute(NULL, "localization"); 390 if (l10nIdx >= 0) { 391 const uint16_t* str = block.getAttributeStringValue(l10nIdx, &len); 392 bool error; 393 uint32_t l10n_required = parse_flags(str, len, l10nRequiredFlags, &error); 394 if (error) { 395 attr.sourcePos.error("Tag <attr> 'localization' attribute value \"%s\" not valid\n", 396 String8(str).string()); 397 attr.hasErrors = true; 398 } 399 attr.createIfNeeded(outTable); 400 if (!attr.hasErrors) { 401 char buf[11]; 402 sprintf(buf, "%d", l10n_required); 403 err = outTable->addBag(attr.sourcePos, myPackage, attr16, attr.ident, 404 String16(""), String16("^l10n"), String16(buf), NULL, NULL); 405 if (err != NO_ERROR) { 406 attr.hasErrors = true; 407 } 408 } 409 } 410 411 String16 enumOrFlagsComment; 412 413 while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) { 414 if (code == ResXMLTree::START_TAG) { 415 uint32_t localType = 0; 416 if (strcmp16(block.getElementName(&len), enum16.string()) == 0) { 417 localType = ResTable_map::TYPE_ENUM; 418 } else if (strcmp16(block.getElementName(&len), flag16.string()) == 0) { 419 localType = ResTable_map::TYPE_FLAGS; 420 } else { 421 SourcePos(in->getPrintableSource(), block.getLineNumber()) 422 .error("Tag <%s> can not appear inside <attr>, only <enum> or <flag>\n", 423 String8(block.getElementName(&len)).string()); 424 return UNKNOWN_ERROR; 425 } 426 427 attr.createIfNeeded(outTable); 428 429 if (attr.type == ResTable_map::TYPE_ANY) { 430 // No type was explicitly stated, so supplying enum tags 431 // implicitly creates an enum or flag. 432 attr.type = 0; 433 } 434 435 if ((attr.type&(ResTable_map::TYPE_ENUM|ResTable_map::TYPE_FLAGS)) == 0) { 436 // Wasn't originally specified as an enum, so update its type. 437 attr.type |= localType; 438 if (!attr.hasErrors) { 439 char numberStr[16]; 440 sprintf(numberStr, "%d", attr.type); 441 err = outTable->addBag(SourcePos(in->getPrintableSource(), block.getLineNumber()), 442 myPackage, attr16, attr.ident, String16(""), 443 String16("^type"), String16(numberStr), NULL, NULL, true); 444 if (err != NO_ERROR) { 445 attr.hasErrors = true; 446 } 447 } 448 } else if ((uint32_t)(attr.type&(ResTable_map::TYPE_ENUM|ResTable_map::TYPE_FLAGS)) != localType) { 449 if (localType == ResTable_map::TYPE_ENUM) { 450 SourcePos(in->getPrintableSource(), block.getLineNumber()) 451 .error("<enum> attribute can not be used inside a flags format\n"); 452 attr.hasErrors = true; 453 } else { 454 SourcePos(in->getPrintableSource(), block.getLineNumber()) 455 .error("<flag> attribute can not be used inside a enum format\n"); 456 attr.hasErrors = true; 457 } 458 } 459 460 String16 itemIdent; 461 ssize_t itemIdentIdx = block.indexOfAttribute(NULL, "name"); 462 if (itemIdentIdx >= 0) { 463 itemIdent = String16(block.getAttributeStringValue(itemIdentIdx, &len)); 464 } else { 465 SourcePos(in->getPrintableSource(), block.getLineNumber()) 466 .error("A 'name' attribute is required for <enum> or <flag>\n"); 467 attr.hasErrors = true; 468 } 469 470 String16 value; 471 ssize_t valueIdx = block.indexOfAttribute(NULL, "value"); 472 if (valueIdx >= 0) { 473 value = String16(block.getAttributeStringValue(valueIdx, &len)); 474 } else { 475 SourcePos(in->getPrintableSource(), block.getLineNumber()) 476 .error("A 'value' attribute is required for <enum> or <flag>\n"); 477 attr.hasErrors = true; 478 } 479 if (!attr.hasErrors && !ResTable::stringToInt(value.string(), value.size(), NULL)) { 480 SourcePos(in->getPrintableSource(), block.getLineNumber()) 481 .error("Tag <enum> or <flag> 'value' attribute must be a number," 482 " not \"%s\"\n", 483 String8(value).string()); 484 attr.hasErrors = true; 485 } 486 487 // Make sure an id is defined for this enum/flag identifier... 488 if (!attr.hasErrors && !outTable->hasBagOrEntry(itemIdent, &id16, &myPackage)) { 489 err = outTable->startBag(SourcePos(in->getPrintableSource(), block.getLineNumber()), 490 myPackage, id16, itemIdent, String16(), NULL); 491 if (err != NO_ERROR) { 492 attr.hasErrors = true; 493 } 494 } 495 496 if (!attr.hasErrors) { 497 if (enumOrFlagsComment.size() == 0) { 498 enumOrFlagsComment.append(mayOrMust(attr.type, 499 ResTable_map::TYPE_ENUM|ResTable_map::TYPE_FLAGS)); 500 enumOrFlagsComment.append((attr.type&ResTable_map::TYPE_ENUM) 501 ? String16(" be one of the following constant values.") 502 : String16(" be one or more (separated by '|') of the following constant values.")); 503 enumOrFlagsComment.append(String16("</p>\n<table>\n" 504 "<colgroup align=\"left\" />\n" 505 "<colgroup align=\"left\" />\n" 506 "<colgroup align=\"left\" />\n" 507 "<tr><th>Constant</th><th>Value</th><th>Description</th></tr>")); 508 } 509 510 enumOrFlagsComment.append(String16("\n<tr><td><code>")); 511 enumOrFlagsComment.append(itemIdent); 512 enumOrFlagsComment.append(String16("</code></td><td>")); 513 enumOrFlagsComment.append(value); 514 enumOrFlagsComment.append(String16("</td><td>")); 515 if (block.getComment(&len)) { 516 enumOrFlagsComment.append(String16(block.getComment(&len))); 517 } 518 enumOrFlagsComment.append(String16("</td></tr>")); 519 520 err = outTable->addBag(SourcePos(in->getPrintableSource(), block.getLineNumber()), 521 myPackage, 522 attr16, attr.ident, String16(""), 523 itemIdent, value, NULL, NULL, false, true); 524 if (err != NO_ERROR) { 525 attr.hasErrors = true; 526 } 527 } 528 } else if (code == ResXMLTree::END_TAG) { 529 if (strcmp16(block.getElementName(&len), attr16.string()) == 0) { 530 break; 531 } 532 if ((attr.type&ResTable_map::TYPE_ENUM) != 0) { 533 if (strcmp16(block.getElementName(&len), enum16.string()) != 0) { 534 SourcePos(in->getPrintableSource(), block.getLineNumber()) 535 .error("Found tag </%s> where </enum> is expected\n", 536 String8(block.getElementName(&len)).string()); 537 return UNKNOWN_ERROR; 538 } 539 } else { 540 if (strcmp16(block.getElementName(&len), flag16.string()) != 0) { 541 SourcePos(in->getPrintableSource(), block.getLineNumber()) 542 .error("Found tag </%s> where </flag> is expected\n", 543 String8(block.getElementName(&len)).string()); 544 return UNKNOWN_ERROR; 545 } 546 } 547 } 548 } 549 550 if (!attr.hasErrors && attr.added) { 551 appendTypeInfo(outTable, myPackage, attr16, attr.ident, attr.type, gFormatFlags); 552 } 553 554 if (!attr.hasErrors && enumOrFlagsComment.size() > 0) { 555 enumOrFlagsComment.append(String16("\n</table>")); 556 outTable->appendTypeComment(myPackage, attr16, attr.ident, enumOrFlagsComment); 557 } 558 559 560 return NO_ERROR; 561} 562 563bool localeIsDefined(const ResTable_config& config) 564{ 565 return config.locale == 0; 566} 567 568status_t parseAndAddBag(Bundle* bundle, 569 const sp<AaptFile>& in, 570 ResXMLTree* block, 571 const ResTable_config& config, 572 const String16& myPackage, 573 const String16& curType, 574 const String16& ident, 575 const String16& parentIdent, 576 const String16& itemIdent, 577 int32_t curFormat, 578 bool isFormatted, 579 const String16& product, 580 bool pseudolocalize, 581 const bool overwrite, 582 ResourceTable* outTable) 583{ 584 status_t err; 585 const String16 item16("item"); 586 587 String16 str; 588 Vector<StringPool::entry_style_span> spans; 589 err = parseStyledString(bundle, in->getPrintableSource().string(), 590 block, item16, &str, &spans, isFormatted, 591 pseudolocalize); 592 if (err != NO_ERROR) { 593 return err; 594 } 595 596 NOISY(printf("Adding resource bag entry l=%c%c c=%c%c orien=%d d=%d " 597 " pid=%s, bag=%s, id=%s: %s\n", 598 config.language[0], config.language[1], 599 config.country[0], config.country[1], 600 config.orientation, config.density, 601 String8(parentIdent).string(), 602 String8(ident).string(), 603 String8(itemIdent).string(), 604 String8(str).string())); 605 606 err = outTable->addBag(SourcePos(in->getPrintableSource(), block->getLineNumber()), 607 myPackage, curType, ident, parentIdent, itemIdent, str, 608 &spans, &config, overwrite, false, curFormat); 609 return err; 610} 611 612/* 613 * Returns true if needle is one of the elements in the comma-separated list 614 * haystack, false otherwise. 615 */ 616bool isInProductList(const String16& needle, const String16& haystack) { 617 const char16_t *needle2 = needle.string(); 618 const char16_t *haystack2 = haystack.string(); 619 size_t needlesize = needle.size(); 620 621 while (*haystack2 != '\0') { 622 if (strncmp16(haystack2, needle2, needlesize) == 0) { 623 if (haystack2[needlesize] == '\0' || haystack2[needlesize] == ',') { 624 return true; 625 } 626 } 627 628 while (*haystack2 != '\0' && *haystack2 != ',') { 629 haystack2++; 630 } 631 if (*haystack2 == ',') { 632 haystack2++; 633 } 634 } 635 636 return false; 637} 638 639/* 640 * A simple container that holds a resource type and name. It is ordered first by type then 641 * by name. 642 */ 643struct type_ident_pair_t { 644 String16 type; 645 String16 ident; 646 647 type_ident_pair_t() { }; 648 type_ident_pair_t(const String16& t, const String16& i) : type(t), ident(i) { } 649 type_ident_pair_t(const type_ident_pair_t& o) : type(o.type), ident(o.ident) { } 650 inline bool operator < (const type_ident_pair_t& o) const { 651 int cmp = compare_type(type, o.type); 652 if (cmp < 0) { 653 return true; 654 } else if (cmp > 0) { 655 return false; 656 } else { 657 return strictly_order_type(ident, o.ident); 658 } 659 } 660}; 661 662 663status_t parseAndAddEntry(Bundle* bundle, 664 const sp<AaptFile>& in, 665 ResXMLTree* block, 666 const ResTable_config& config, 667 const String16& myPackage, 668 const String16& curType, 669 const String16& ident, 670 const String16& curTag, 671 bool curIsStyled, 672 int32_t curFormat, 673 bool isFormatted, 674 const String16& product, 675 bool pseudolocalize, 676 const bool overwrite, 677 KeyedVector<type_ident_pair_t, bool>* skippedResourceNames, 678 ResourceTable* outTable) 679{ 680 status_t err; 681 682 String16 str; 683 Vector<StringPool::entry_style_span> spans; 684 err = parseStyledString(bundle, in->getPrintableSource().string(), block, 685 curTag, &str, curIsStyled ? &spans : NULL, 686 isFormatted, pseudolocalize); 687 688 if (err < NO_ERROR) { 689 return err; 690 } 691 692 /* 693 * If a product type was specified on the command line 694 * and also in the string, and the two are not the same, 695 * return without adding the string. 696 */ 697 698 const char *bundleProduct = bundle->getProduct(); 699 if (bundleProduct == NULL) { 700 bundleProduct = ""; 701 } 702 703 if (product.size() != 0) { 704 /* 705 * If the command-line-specified product is empty, only "default" 706 * matches. Other variants are skipped. This is so generation 707 * of the R.java file when the product is not known is predictable. 708 */ 709 710 if (bundleProduct[0] == '\0') { 711 if (strcmp16(String16("default").string(), product.string()) != 0) { 712 /* 713 * This string has a product other than 'default'. Do not add it, 714 * but record it so that if we do not see the same string with 715 * product 'default' or no product, then report an error. 716 */ 717 skippedResourceNames->replaceValueFor( 718 type_ident_pair_t(curType, ident), true); 719 return NO_ERROR; 720 } 721 } else { 722 /* 723 * The command-line product is not empty. 724 * If the product for this string is on the command-line list, 725 * it matches. "default" also matches, but only if nothing 726 * else has matched already. 727 */ 728 729 if (isInProductList(product, String16(bundleProduct))) { 730 ; 731 } else if (strcmp16(String16("default").string(), product.string()) == 0 && 732 !outTable->hasBagOrEntry(myPackage, curType, ident, config)) { 733 ; 734 } else { 735 return NO_ERROR; 736 } 737 } 738 } 739 740 NOISY(printf("Adding resource entry l=%c%c c=%c%c orien=%d d=%d id=%s: %s\n", 741 config.language[0], config.language[1], 742 config.country[0], config.country[1], 743 config.orientation, config.density, 744 String8(ident).string(), String8(str).string())); 745 746 err = outTable->addEntry(SourcePos(in->getPrintableSource(), block->getLineNumber()), 747 myPackage, curType, ident, str, &spans, &config, 748 false, curFormat, overwrite); 749 750 return err; 751} 752 753status_t compileResourceFile(Bundle* bundle, 754 const sp<AaptAssets>& assets, 755 const sp<AaptFile>& in, 756 const ResTable_config& defParams, 757 const bool overwrite, 758 ResourceTable* outTable) 759{ 760 ResXMLTree block; 761 status_t err = parseXMLResource(in, &block, false, true); 762 if (err != NO_ERROR) { 763 return err; 764 } 765 766 // Top-level tag. 767 const String16 resources16("resources"); 768 769 // Identifier declaration tags. 770 const String16 declare_styleable16("declare-styleable"); 771 const String16 attr16("attr"); 772 773 // Data creation organizational tags. 774 const String16 string16("string"); 775 const String16 drawable16("drawable"); 776 const String16 color16("color"); 777 const String16 bool16("bool"); 778 const String16 integer16("integer"); 779 const String16 dimen16("dimen"); 780 const String16 fraction16("fraction"); 781 const String16 style16("style"); 782 const String16 plurals16("plurals"); 783 const String16 array16("array"); 784 const String16 string_array16("string-array"); 785 const String16 integer_array16("integer-array"); 786 const String16 public16("public"); 787 const String16 public_padding16("public-padding"); 788 const String16 private_symbols16("private-symbols"); 789 const String16 java_symbol16("java-symbol"); 790 const String16 add_resource16("add-resource"); 791 const String16 skip16("skip"); 792 const String16 eat_comment16("eat-comment"); 793 794 // Data creation tags. 795 const String16 bag16("bag"); 796 const String16 item16("item"); 797 798 // Attribute type constants. 799 const String16 enum16("enum"); 800 801 // plural values 802 const String16 other16("other"); 803 const String16 quantityOther16("^other"); 804 const String16 zero16("zero"); 805 const String16 quantityZero16("^zero"); 806 const String16 one16("one"); 807 const String16 quantityOne16("^one"); 808 const String16 two16("two"); 809 const String16 quantityTwo16("^two"); 810 const String16 few16("few"); 811 const String16 quantityFew16("^few"); 812 const String16 many16("many"); 813 const String16 quantityMany16("^many"); 814 815 // useful attribute names and special values 816 const String16 name16("name"); 817 const String16 translatable16("translatable"); 818 const String16 formatted16("formatted"); 819 const String16 false16("false"); 820 821 const String16 myPackage(assets->getPackage()); 822 823 bool hasErrors = false; 824 825 bool fileIsTranslatable = true; 826 if (strstr(in->getPrintableSource().string(), "donottranslate") != NULL) { 827 fileIsTranslatable = false; 828 } 829 830 DefaultKeyedVector<String16, uint32_t> nextPublicId(0); 831 832 // Stores the resource names that were skipped. Typically this happens when 833 // AAPT is invoked without a product specified and a resource has no 834 // 'default' product attribute. 835 KeyedVector<type_ident_pair_t, bool> skippedResourceNames; 836 837 ResXMLTree::event_code_t code; 838 do { 839 code = block.next(); 840 } while (code == ResXMLTree::START_NAMESPACE); 841 842 size_t len; 843 if (code != ResXMLTree::START_TAG) { 844 SourcePos(in->getPrintableSource(), block.getLineNumber()).error( 845 "No start tag found\n"); 846 return UNKNOWN_ERROR; 847 } 848 if (strcmp16(block.getElementName(&len), resources16.string()) != 0) { 849 SourcePos(in->getPrintableSource(), block.getLineNumber()).error( 850 "Invalid start tag %s\n", String8(block.getElementName(&len)).string()); 851 return UNKNOWN_ERROR; 852 } 853 854 ResTable_config curParams(defParams); 855 856 ResTable_config pseudoParams(curParams); 857 pseudoParams.language[0] = 'z'; 858 pseudoParams.language[1] = 'z'; 859 pseudoParams.country[0] = 'Z'; 860 pseudoParams.country[1] = 'Z'; 861 862 while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) { 863 if (code == ResXMLTree::START_TAG) { 864 const String16* curTag = NULL; 865 String16 curType; 866 int32_t curFormat = ResTable_map::TYPE_ANY; 867 bool curIsBag = false; 868 bool curIsBagReplaceOnOverwrite = false; 869 bool curIsStyled = false; 870 bool curIsPseudolocalizable = false; 871 bool curIsFormatted = fileIsTranslatable; 872 bool localHasErrors = false; 873 874 if (strcmp16(block.getElementName(&len), skip16.string()) == 0) { 875 while ((code=block.next()) != ResXMLTree::END_DOCUMENT 876 && code != ResXMLTree::BAD_DOCUMENT) { 877 if (code == ResXMLTree::END_TAG) { 878 if (strcmp16(block.getElementName(&len), skip16.string()) == 0) { 879 break; 880 } 881 } 882 } 883 continue; 884 885 } else if (strcmp16(block.getElementName(&len), eat_comment16.string()) == 0) { 886 while ((code=block.next()) != ResXMLTree::END_DOCUMENT 887 && code != ResXMLTree::BAD_DOCUMENT) { 888 if (code == ResXMLTree::END_TAG) { 889 if (strcmp16(block.getElementName(&len), eat_comment16.string()) == 0) { 890 break; 891 } 892 } 893 } 894 continue; 895 896 } else if (strcmp16(block.getElementName(&len), public16.string()) == 0) { 897 SourcePos srcPos(in->getPrintableSource(), block.getLineNumber()); 898 899 String16 type; 900 ssize_t typeIdx = block.indexOfAttribute(NULL, "type"); 901 if (typeIdx < 0) { 902 srcPos.error("A 'type' attribute is required for <public>\n"); 903 hasErrors = localHasErrors = true; 904 } 905 type = String16(block.getAttributeStringValue(typeIdx, &len)); 906 907 String16 name; 908 ssize_t nameIdx = block.indexOfAttribute(NULL, "name"); 909 if (nameIdx < 0) { 910 srcPos.error("A 'name' attribute is required for <public>\n"); 911 hasErrors = localHasErrors = true; 912 } 913 name = String16(block.getAttributeStringValue(nameIdx, &len)); 914 915 uint32_t ident = 0; 916 ssize_t identIdx = block.indexOfAttribute(NULL, "id"); 917 if (identIdx >= 0) { 918 const char16_t* identStr = block.getAttributeStringValue(identIdx, &len); 919 Res_value identValue; 920 if (!ResTable::stringToInt(identStr, len, &identValue)) { 921 srcPos.error("Given 'id' attribute is not an integer: %s\n", 922 String8(block.getAttributeStringValue(identIdx, &len)).string()); 923 hasErrors = localHasErrors = true; 924 } else { 925 ident = identValue.data; 926 nextPublicId.replaceValueFor(type, ident+1); 927 } 928 } else if (nextPublicId.indexOfKey(type) < 0) { 929 srcPos.error("No 'id' attribute supplied <public>," 930 " and no previous id defined in this file.\n"); 931 hasErrors = localHasErrors = true; 932 } else if (!localHasErrors) { 933 ident = nextPublicId.valueFor(type); 934 nextPublicId.replaceValueFor(type, ident+1); 935 } 936 937 if (!localHasErrors) { 938 err = outTable->addPublic(srcPos, myPackage, type, name, ident); 939 if (err < NO_ERROR) { 940 hasErrors = localHasErrors = true; 941 } 942 } 943 if (!localHasErrors) { 944 sp<AaptSymbols> symbols = assets->getSymbolsFor(String8("R")); 945 if (symbols != NULL) { 946 symbols = symbols->addNestedSymbol(String8(type), srcPos); 947 } 948 if (symbols != NULL) { 949 symbols->makeSymbolPublic(String8(name), srcPos); 950 String16 comment( 951 block.getComment(&len) ? block.getComment(&len) : nulStr); 952 symbols->appendComment(String8(name), comment, srcPos); 953 } else { 954 srcPos.error("Unable to create symbols!\n"); 955 hasErrors = localHasErrors = true; 956 } 957 } 958 959 while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) { 960 if (code == ResXMLTree::END_TAG) { 961 if (strcmp16(block.getElementName(&len), public16.string()) == 0) { 962 break; 963 } 964 } 965 } 966 continue; 967 968 } else if (strcmp16(block.getElementName(&len), public_padding16.string()) == 0) { 969 SourcePos srcPos(in->getPrintableSource(), block.getLineNumber()); 970 971 String16 type; 972 ssize_t typeIdx = block.indexOfAttribute(NULL, "type"); 973 if (typeIdx < 0) { 974 srcPos.error("A 'type' attribute is required for <public-padding>\n"); 975 hasErrors = localHasErrors = true; 976 } 977 type = String16(block.getAttributeStringValue(typeIdx, &len)); 978 979 String16 name; 980 ssize_t nameIdx = block.indexOfAttribute(NULL, "name"); 981 if (nameIdx < 0) { 982 srcPos.error("A 'name' attribute is required for <public-padding>\n"); 983 hasErrors = localHasErrors = true; 984 } 985 name = String16(block.getAttributeStringValue(nameIdx, &len)); 986 987 uint32_t start = 0; 988 ssize_t startIdx = block.indexOfAttribute(NULL, "start"); 989 if (startIdx >= 0) { 990 const char16_t* startStr = block.getAttributeStringValue(startIdx, &len); 991 Res_value startValue; 992 if (!ResTable::stringToInt(startStr, len, &startValue)) { 993 srcPos.error("Given 'start' attribute is not an integer: %s\n", 994 String8(block.getAttributeStringValue(startIdx, &len)).string()); 995 hasErrors = localHasErrors = true; 996 } else { 997 start = startValue.data; 998 } 999 } else if (nextPublicId.indexOfKey(type) < 0) { 1000 srcPos.error("No 'start' attribute supplied <public-padding>," 1001 " and no previous id defined in this file.\n"); 1002 hasErrors = localHasErrors = true; 1003 } else if (!localHasErrors) { 1004 start = nextPublicId.valueFor(type); 1005 } 1006 1007 uint32_t end = 0; 1008 ssize_t endIdx = block.indexOfAttribute(NULL, "end"); 1009 if (endIdx >= 0) { 1010 const char16_t* endStr = block.getAttributeStringValue(endIdx, &len); 1011 Res_value endValue; 1012 if (!ResTable::stringToInt(endStr, len, &endValue)) { 1013 srcPos.error("Given 'end' attribute is not an integer: %s\n", 1014 String8(block.getAttributeStringValue(endIdx, &len)).string()); 1015 hasErrors = localHasErrors = true; 1016 } else { 1017 end = endValue.data; 1018 } 1019 } else { 1020 srcPos.error("No 'end' attribute supplied <public-padding>\n"); 1021 hasErrors = localHasErrors = true; 1022 } 1023 1024 if (end >= start) { 1025 nextPublicId.replaceValueFor(type, end+1); 1026 } else { 1027 srcPos.error("Padding start '%ul' is after end '%ul'\n", 1028 start, end); 1029 hasErrors = localHasErrors = true; 1030 } 1031 1032 String16 comment( 1033 block.getComment(&len) ? block.getComment(&len) : nulStr); 1034 for (uint32_t curIdent=start; curIdent<=end; curIdent++) { 1035 if (localHasErrors) { 1036 break; 1037 } 1038 String16 curName(name); 1039 char buf[64]; 1040 sprintf(buf, "%d", (int)(end-curIdent+1)); 1041 curName.append(String16(buf)); 1042 1043 err = outTable->addEntry(srcPos, myPackage, type, curName, 1044 String16("padding"), NULL, &curParams, false, 1045 ResTable_map::TYPE_STRING, overwrite); 1046 if (err < NO_ERROR) { 1047 hasErrors = localHasErrors = true; 1048 break; 1049 } 1050 err = outTable->addPublic(srcPos, myPackage, type, 1051 curName, curIdent); 1052 if (err < NO_ERROR) { 1053 hasErrors = localHasErrors = true; 1054 break; 1055 } 1056 sp<AaptSymbols> symbols = assets->getSymbolsFor(String8("R")); 1057 if (symbols != NULL) { 1058 symbols = symbols->addNestedSymbol(String8(type), srcPos); 1059 } 1060 if (symbols != NULL) { 1061 symbols->makeSymbolPublic(String8(curName), srcPos); 1062 symbols->appendComment(String8(curName), comment, srcPos); 1063 } else { 1064 srcPos.error("Unable to create symbols!\n"); 1065 hasErrors = localHasErrors = true; 1066 } 1067 } 1068 1069 while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) { 1070 if (code == ResXMLTree::END_TAG) { 1071 if (strcmp16(block.getElementName(&len), public_padding16.string()) == 0) { 1072 break; 1073 } 1074 } 1075 } 1076 continue; 1077 1078 } else if (strcmp16(block.getElementName(&len), private_symbols16.string()) == 0) { 1079 String16 pkg; 1080 ssize_t pkgIdx = block.indexOfAttribute(NULL, "package"); 1081 if (pkgIdx < 0) { 1082 SourcePos(in->getPrintableSource(), block.getLineNumber()).error( 1083 "A 'package' attribute is required for <private-symbols>\n"); 1084 hasErrors = localHasErrors = true; 1085 } 1086 pkg = String16(block.getAttributeStringValue(pkgIdx, &len)); 1087 if (!localHasErrors) { 1088 assets->setSymbolsPrivatePackage(String8(pkg)); 1089 } 1090 1091 while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) { 1092 if (code == ResXMLTree::END_TAG) { 1093 if (strcmp16(block.getElementName(&len), private_symbols16.string()) == 0) { 1094 break; 1095 } 1096 } 1097 } 1098 continue; 1099 1100 } else if (strcmp16(block.getElementName(&len), java_symbol16.string()) == 0) { 1101 SourcePos srcPos(in->getPrintableSource(), block.getLineNumber()); 1102 1103 String16 type; 1104 ssize_t typeIdx = block.indexOfAttribute(NULL, "type"); 1105 if (typeIdx < 0) { 1106 srcPos.error("A 'type' attribute is required for <public>\n"); 1107 hasErrors = localHasErrors = true; 1108 } 1109 type = String16(block.getAttributeStringValue(typeIdx, &len)); 1110 1111 String16 name; 1112 ssize_t nameIdx = block.indexOfAttribute(NULL, "name"); 1113 if (nameIdx < 0) { 1114 srcPos.error("A 'name' attribute is required for <public>\n"); 1115 hasErrors = localHasErrors = true; 1116 } 1117 name = String16(block.getAttributeStringValue(nameIdx, &len)); 1118 1119 sp<AaptSymbols> symbols = assets->getJavaSymbolsFor(String8("R")); 1120 if (symbols != NULL) { 1121 symbols = symbols->addNestedSymbol(String8(type), srcPos); 1122 } 1123 if (symbols != NULL) { 1124 symbols->makeSymbolJavaSymbol(String8(name), srcPos); 1125 String16 comment( 1126 block.getComment(&len) ? block.getComment(&len) : nulStr); 1127 symbols->appendComment(String8(name), comment, srcPos); 1128 } else { 1129 srcPos.error("Unable to create symbols!\n"); 1130 hasErrors = localHasErrors = true; 1131 } 1132 1133 while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) { 1134 if (code == ResXMLTree::END_TAG) { 1135 if (strcmp16(block.getElementName(&len), java_symbol16.string()) == 0) { 1136 break; 1137 } 1138 } 1139 } 1140 continue; 1141 1142 1143 } else if (strcmp16(block.getElementName(&len), add_resource16.string()) == 0) { 1144 SourcePos srcPos(in->getPrintableSource(), block.getLineNumber()); 1145 1146 String16 typeName; 1147 ssize_t typeIdx = block.indexOfAttribute(NULL, "type"); 1148 if (typeIdx < 0) { 1149 srcPos.error("A 'type' attribute is required for <add-resource>\n"); 1150 hasErrors = localHasErrors = true; 1151 } 1152 typeName = String16(block.getAttributeStringValue(typeIdx, &len)); 1153 1154 String16 name; 1155 ssize_t nameIdx = block.indexOfAttribute(NULL, "name"); 1156 if (nameIdx < 0) { 1157 srcPos.error("A 'name' attribute is required for <add-resource>\n"); 1158 hasErrors = localHasErrors = true; 1159 } 1160 name = String16(block.getAttributeStringValue(nameIdx, &len)); 1161 1162 outTable->canAddEntry(srcPos, myPackage, typeName, name); 1163 1164 while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) { 1165 if (code == ResXMLTree::END_TAG) { 1166 if (strcmp16(block.getElementName(&len), add_resource16.string()) == 0) { 1167 break; 1168 } 1169 } 1170 } 1171 continue; 1172 1173 } else if (strcmp16(block.getElementName(&len), declare_styleable16.string()) == 0) { 1174 SourcePos srcPos(in->getPrintableSource(), block.getLineNumber()); 1175 1176 String16 ident; 1177 ssize_t identIdx = block.indexOfAttribute(NULL, "name"); 1178 if (identIdx < 0) { 1179 srcPos.error("A 'name' attribute is required for <declare-styleable>\n"); 1180 hasErrors = localHasErrors = true; 1181 } 1182 ident = String16(block.getAttributeStringValue(identIdx, &len)); 1183 1184 sp<AaptSymbols> symbols = assets->getSymbolsFor(String8("R")); 1185 if (!localHasErrors) { 1186 if (symbols != NULL) { 1187 symbols = symbols->addNestedSymbol(String8("styleable"), srcPos); 1188 } 1189 sp<AaptSymbols> styleSymbols = symbols; 1190 if (symbols != NULL) { 1191 symbols = symbols->addNestedSymbol(String8(ident), srcPos); 1192 } 1193 if (symbols == NULL) { 1194 srcPos.error("Unable to create symbols!\n"); 1195 return UNKNOWN_ERROR; 1196 } 1197 1198 String16 comment( 1199 block.getComment(&len) ? block.getComment(&len) : nulStr); 1200 styleSymbols->appendComment(String8(ident), comment, srcPos); 1201 } else { 1202 symbols = NULL; 1203 } 1204 1205 while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) { 1206 if (code == ResXMLTree::START_TAG) { 1207 if (strcmp16(block.getElementName(&len), skip16.string()) == 0) { 1208 while ((code=block.next()) != ResXMLTree::END_DOCUMENT 1209 && code != ResXMLTree::BAD_DOCUMENT) { 1210 if (code == ResXMLTree::END_TAG) { 1211 if (strcmp16(block.getElementName(&len), skip16.string()) == 0) { 1212 break; 1213 } 1214 } 1215 } 1216 continue; 1217 } else if (strcmp16(block.getElementName(&len), eat_comment16.string()) == 0) { 1218 while ((code=block.next()) != ResXMLTree::END_DOCUMENT 1219 && code != ResXMLTree::BAD_DOCUMENT) { 1220 if (code == ResXMLTree::END_TAG) { 1221 if (strcmp16(block.getElementName(&len), eat_comment16.string()) == 0) { 1222 break; 1223 } 1224 } 1225 } 1226 continue; 1227 } else if (strcmp16(block.getElementName(&len), attr16.string()) != 0) { 1228 SourcePos(in->getPrintableSource(), block.getLineNumber()).error( 1229 "Tag <%s> can not appear inside <declare-styleable>, only <attr>\n", 1230 String8(block.getElementName(&len)).string()); 1231 return UNKNOWN_ERROR; 1232 } 1233 1234 String16 comment( 1235 block.getComment(&len) ? block.getComment(&len) : nulStr); 1236 String16 itemIdent; 1237 err = compileAttribute(in, block, myPackage, outTable, &itemIdent, true); 1238 if (err != NO_ERROR) { 1239 hasErrors = localHasErrors = true; 1240 } 1241 1242 if (symbols != NULL) { 1243 SourcePos srcPos(String8(in->getPrintableSource()), block.getLineNumber()); 1244 symbols->addSymbol(String8(itemIdent), 0, srcPos); 1245 symbols->appendComment(String8(itemIdent), comment, srcPos); 1246 //printf("Attribute %s comment: %s\n", String8(itemIdent).string(), 1247 // String8(comment).string()); 1248 } 1249 } else if (code == ResXMLTree::END_TAG) { 1250 if (strcmp16(block.getElementName(&len), declare_styleable16.string()) == 0) { 1251 break; 1252 } 1253 1254 SourcePos(in->getPrintableSource(), block.getLineNumber()).error( 1255 "Found tag </%s> where </attr> is expected\n", 1256 String8(block.getElementName(&len)).string()); 1257 return UNKNOWN_ERROR; 1258 } 1259 } 1260 continue; 1261 1262 } else if (strcmp16(block.getElementName(&len), attr16.string()) == 0) { 1263 err = compileAttribute(in, block, myPackage, outTable, NULL); 1264 if (err != NO_ERROR) { 1265 hasErrors = true; 1266 } 1267 continue; 1268 1269 } else if (strcmp16(block.getElementName(&len), item16.string()) == 0) { 1270 curTag = &item16; 1271 ssize_t attri = block.indexOfAttribute(NULL, "type"); 1272 if (attri >= 0) { 1273 curType = String16(block.getAttributeStringValue(attri, &len)); 1274 ssize_t formatIdx = block.indexOfAttribute(NULL, "format"); 1275 if (formatIdx >= 0) { 1276 String16 formatStr = String16(block.getAttributeStringValue( 1277 formatIdx, &len)); 1278 curFormat = parse_flags(formatStr.string(), formatStr.size(), 1279 gFormatFlags); 1280 if (curFormat == 0) { 1281 SourcePos(in->getPrintableSource(), block.getLineNumber()).error( 1282 "Tag <item> 'format' attribute value \"%s\" not valid\n", 1283 String8(formatStr).string()); 1284 hasErrors = localHasErrors = true; 1285 } 1286 } 1287 } else { 1288 SourcePos(in->getPrintableSource(), block.getLineNumber()).error( 1289 "A 'type' attribute is required for <item>\n"); 1290 hasErrors = localHasErrors = true; 1291 } 1292 curIsStyled = true; 1293 } else if (strcmp16(block.getElementName(&len), string16.string()) == 0) { 1294 // Note the existence and locale of every string we process 1295 char rawLocale[RESTABLE_MAX_LOCALE_LEN]; 1296 curParams.getBcp47Locale(rawLocale); 1297 String8 locale(rawLocale); 1298 String16 name; 1299 String16 translatable; 1300 String16 formatted; 1301 1302 size_t n = block.getAttributeCount(); 1303 for (size_t i = 0; i < n; i++) { 1304 size_t length; 1305 const uint16_t* attr = block.getAttributeName(i, &length); 1306 if (strcmp16(attr, name16.string()) == 0) { 1307 name.setTo(block.getAttributeStringValue(i, &length)); 1308 } else if (strcmp16(attr, translatable16.string()) == 0) { 1309 translatable.setTo(block.getAttributeStringValue(i, &length)); 1310 } else if (strcmp16(attr, formatted16.string()) == 0) { 1311 formatted.setTo(block.getAttributeStringValue(i, &length)); 1312 } 1313 } 1314 1315 if (name.size() > 0) { 1316 if (translatable == false16) { 1317 curIsFormatted = false; 1318 // Untranslatable strings must only exist in the default [empty] locale 1319 if (locale.size() > 0) { 1320 SourcePos(in->getPrintableSource(), block.getLineNumber()).warning( 1321 "string '%s' marked untranslatable but exists in locale '%s'\n", 1322 String8(name).string(), 1323 locale.string()); 1324 // hasErrors = localHasErrors = true; 1325 } else { 1326 // Intentionally empty block: 1327 // 1328 // Don't add untranslatable strings to the localization table; that 1329 // way if we later see localizations of them, they'll be flagged as 1330 // having no default translation. 1331 } 1332 } else { 1333 outTable->addLocalization( 1334 name, 1335 locale, 1336 SourcePos(in->getPrintableSource(), block.getLineNumber())); 1337 } 1338 1339 if (formatted == false16) { 1340 curIsFormatted = false; 1341 } 1342 } 1343 1344 curTag = &string16; 1345 curType = string16; 1346 curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_STRING; 1347 curIsStyled = true; 1348 curIsPseudolocalizable = (translatable != false16); 1349 } else if (strcmp16(block.getElementName(&len), drawable16.string()) == 0) { 1350 curTag = &drawable16; 1351 curType = drawable16; 1352 curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_COLOR; 1353 } else if (strcmp16(block.getElementName(&len), color16.string()) == 0) { 1354 curTag = &color16; 1355 curType = color16; 1356 curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_COLOR; 1357 } else if (strcmp16(block.getElementName(&len), bool16.string()) == 0) { 1358 curTag = &bool16; 1359 curType = bool16; 1360 curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_BOOLEAN; 1361 } else if (strcmp16(block.getElementName(&len), integer16.string()) == 0) { 1362 curTag = &integer16; 1363 curType = integer16; 1364 curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_INTEGER; 1365 } else if (strcmp16(block.getElementName(&len), dimen16.string()) == 0) { 1366 curTag = &dimen16; 1367 curType = dimen16; 1368 curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_DIMENSION; 1369 } else if (strcmp16(block.getElementName(&len), fraction16.string()) == 0) { 1370 curTag = &fraction16; 1371 curType = fraction16; 1372 curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_FRACTION; 1373 } else if (strcmp16(block.getElementName(&len), bag16.string()) == 0) { 1374 curTag = &bag16; 1375 curIsBag = true; 1376 ssize_t attri = block.indexOfAttribute(NULL, "type"); 1377 if (attri >= 0) { 1378 curType = String16(block.getAttributeStringValue(attri, &len)); 1379 } else { 1380 SourcePos(in->getPrintableSource(), block.getLineNumber()).error( 1381 "A 'type' attribute is required for <bag>\n"); 1382 hasErrors = localHasErrors = true; 1383 } 1384 } else if (strcmp16(block.getElementName(&len), style16.string()) == 0) { 1385 curTag = &style16; 1386 curType = style16; 1387 curIsBag = true; 1388 } else if (strcmp16(block.getElementName(&len), plurals16.string()) == 0) { 1389 curTag = &plurals16; 1390 curType = plurals16; 1391 curIsBag = true; 1392 } else if (strcmp16(block.getElementName(&len), array16.string()) == 0) { 1393 curTag = &array16; 1394 curType = array16; 1395 curIsBag = true; 1396 curIsBagReplaceOnOverwrite = true; 1397 ssize_t formatIdx = block.indexOfAttribute(NULL, "format"); 1398 if (formatIdx >= 0) { 1399 String16 formatStr = String16(block.getAttributeStringValue( 1400 formatIdx, &len)); 1401 curFormat = parse_flags(formatStr.string(), formatStr.size(), 1402 gFormatFlags); 1403 if (curFormat == 0) { 1404 SourcePos(in->getPrintableSource(), block.getLineNumber()).error( 1405 "Tag <array> 'format' attribute value \"%s\" not valid\n", 1406 String8(formatStr).string()); 1407 hasErrors = localHasErrors = true; 1408 } 1409 } 1410 } else if (strcmp16(block.getElementName(&len), string_array16.string()) == 0) { 1411 // Check whether these strings need valid formats. 1412 // (simplified form of what string16 does above) 1413 size_t n = block.getAttributeCount(); 1414 1415 // Pseudolocalizable by default, unless this string array isn't 1416 // translatable. 1417 curIsPseudolocalizable = true; 1418 for (size_t i = 0; i < n; i++) { 1419 size_t length; 1420 const uint16_t* attr = block.getAttributeName(i, &length); 1421 if (strcmp16(attr, translatable16.string()) == 0) { 1422 const uint16_t* value = block.getAttributeStringValue(i, &length); 1423 if (strcmp16(value, false16.string()) == 0) { 1424 curIsPseudolocalizable = false; 1425 } 1426 } 1427 1428 if (strcmp16(attr, formatted16.string()) == 0) { 1429 const uint16_t* value = block.getAttributeStringValue(i, &length); 1430 if (strcmp16(value, false16.string()) == 0) { 1431 curIsFormatted = false; 1432 } 1433 } 1434 } 1435 1436 curTag = &string_array16; 1437 curType = array16; 1438 curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_STRING; 1439 curIsBag = true; 1440 curIsBagReplaceOnOverwrite = true; 1441 } else if (strcmp16(block.getElementName(&len), integer_array16.string()) == 0) { 1442 curTag = &integer_array16; 1443 curType = array16; 1444 curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_INTEGER; 1445 curIsBag = true; 1446 curIsBagReplaceOnOverwrite = true; 1447 } else { 1448 SourcePos(in->getPrintableSource(), block.getLineNumber()).error( 1449 "Found tag %s where item is expected\n", 1450 String8(block.getElementName(&len)).string()); 1451 return UNKNOWN_ERROR; 1452 } 1453 1454 String16 ident; 1455 ssize_t identIdx = block.indexOfAttribute(NULL, "name"); 1456 if (identIdx >= 0) { 1457 ident = String16(block.getAttributeStringValue(identIdx, &len)); 1458 } else { 1459 SourcePos(in->getPrintableSource(), block.getLineNumber()).error( 1460 "A 'name' attribute is required for <%s>\n", 1461 String8(*curTag).string()); 1462 hasErrors = localHasErrors = true; 1463 } 1464 1465 String16 product; 1466 identIdx = block.indexOfAttribute(NULL, "product"); 1467 if (identIdx >= 0) { 1468 product = String16(block.getAttributeStringValue(identIdx, &len)); 1469 } 1470 1471 String16 comment(block.getComment(&len) ? block.getComment(&len) : nulStr); 1472 1473 if (curIsBag) { 1474 // Figure out the parent of this bag... 1475 String16 parentIdent; 1476 ssize_t parentIdentIdx = block.indexOfAttribute(NULL, "parent"); 1477 if (parentIdentIdx >= 0) { 1478 parentIdent = String16(block.getAttributeStringValue(parentIdentIdx, &len)); 1479 } else { 1480 ssize_t sep = ident.findLast('.'); 1481 if (sep >= 0) { 1482 parentIdent.setTo(ident, sep); 1483 } 1484 } 1485 1486 if (!localHasErrors) { 1487 err = outTable->startBag(SourcePos(in->getPrintableSource(), 1488 block.getLineNumber()), myPackage, curType, ident, 1489 parentIdent, &curParams, 1490 overwrite, curIsBagReplaceOnOverwrite); 1491 if (err != NO_ERROR) { 1492 hasErrors = localHasErrors = true; 1493 } 1494 } 1495 1496 ssize_t elmIndex = 0; 1497 char elmIndexStr[14]; 1498 while ((code=block.next()) != ResXMLTree::END_DOCUMENT 1499 && code != ResXMLTree::BAD_DOCUMENT) { 1500 1501 if (code == ResXMLTree::START_TAG) { 1502 if (strcmp16(block.getElementName(&len), item16.string()) != 0) { 1503 SourcePos(in->getPrintableSource(), block.getLineNumber()).error( 1504 "Tag <%s> can not appear inside <%s>, only <item>\n", 1505 String8(block.getElementName(&len)).string(), 1506 String8(*curTag).string()); 1507 return UNKNOWN_ERROR; 1508 } 1509 1510 String16 itemIdent; 1511 if (curType == array16) { 1512 sprintf(elmIndexStr, "^index_%d", (int)elmIndex++); 1513 itemIdent = String16(elmIndexStr); 1514 } else if (curType == plurals16) { 1515 ssize_t itemIdentIdx = block.indexOfAttribute(NULL, "quantity"); 1516 if (itemIdentIdx >= 0) { 1517 String16 quantity16(block.getAttributeStringValue(itemIdentIdx, &len)); 1518 if (quantity16 == other16) { 1519 itemIdent = quantityOther16; 1520 } 1521 else if (quantity16 == zero16) { 1522 itemIdent = quantityZero16; 1523 } 1524 else if (quantity16 == one16) { 1525 itemIdent = quantityOne16; 1526 } 1527 else if (quantity16 == two16) { 1528 itemIdent = quantityTwo16; 1529 } 1530 else if (quantity16 == few16) { 1531 itemIdent = quantityFew16; 1532 } 1533 else if (quantity16 == many16) { 1534 itemIdent = quantityMany16; 1535 } 1536 else { 1537 SourcePos(in->getPrintableSource(), block.getLineNumber()).error( 1538 "Illegal 'quantity' attribute is <item> inside <plurals>\n"); 1539 hasErrors = localHasErrors = true; 1540 } 1541 } else { 1542 SourcePos(in->getPrintableSource(), block.getLineNumber()).error( 1543 "A 'quantity' attribute is required for <item> inside <plurals>\n"); 1544 hasErrors = localHasErrors = true; 1545 } 1546 } else { 1547 ssize_t itemIdentIdx = block.indexOfAttribute(NULL, "name"); 1548 if (itemIdentIdx >= 0) { 1549 itemIdent = String16(block.getAttributeStringValue(itemIdentIdx, &len)); 1550 } else { 1551 SourcePos(in->getPrintableSource(), block.getLineNumber()).error( 1552 "A 'name' attribute is required for <item>\n"); 1553 hasErrors = localHasErrors = true; 1554 } 1555 } 1556 1557 ResXMLParser::ResXMLPosition parserPosition; 1558 block.getPosition(&parserPosition); 1559 1560 err = parseAndAddBag(bundle, in, &block, curParams, myPackage, curType, 1561 ident, parentIdent, itemIdent, curFormat, curIsFormatted, 1562 product, false, overwrite, outTable); 1563 if (err == NO_ERROR) { 1564 if (curIsPseudolocalizable && localeIsDefined(curParams) 1565 && bundle->getPseudolocalize()) { 1566 // pseudolocalize here 1567#if 1 1568 block.setPosition(parserPosition); 1569 err = parseAndAddBag(bundle, in, &block, pseudoParams, myPackage, 1570 curType, ident, parentIdent, itemIdent, curFormat, 1571 curIsFormatted, product, true, overwrite, outTable); 1572#endif 1573 } 1574 } 1575 if (err != NO_ERROR) { 1576 hasErrors = localHasErrors = true; 1577 } 1578 } else if (code == ResXMLTree::END_TAG) { 1579 if (strcmp16(block.getElementName(&len), curTag->string()) != 0) { 1580 SourcePos(in->getPrintableSource(), block.getLineNumber()).error( 1581 "Found tag </%s> where </%s> is expected\n", 1582 String8(block.getElementName(&len)).string(), 1583 String8(*curTag).string()); 1584 return UNKNOWN_ERROR; 1585 } 1586 break; 1587 } 1588 } 1589 } else { 1590 ResXMLParser::ResXMLPosition parserPosition; 1591 block.getPosition(&parserPosition); 1592 1593 err = parseAndAddEntry(bundle, in, &block, curParams, myPackage, curType, ident, 1594 *curTag, curIsStyled, curFormat, curIsFormatted, 1595 product, false, overwrite, &skippedResourceNames, outTable); 1596 1597 if (err < NO_ERROR) { // Why err < NO_ERROR instead of err != NO_ERROR? 1598 hasErrors = localHasErrors = true; 1599 } 1600 else if (err == NO_ERROR) { 1601 if (curIsPseudolocalizable && localeIsDefined(curParams) 1602 && bundle->getPseudolocalize()) { 1603 // pseudolocalize here 1604 block.setPosition(parserPosition); 1605 err = parseAndAddEntry(bundle, in, &block, pseudoParams, myPackage, curType, 1606 ident, *curTag, curIsStyled, curFormat, 1607 curIsFormatted, product, 1608 true, overwrite, &skippedResourceNames, outTable); 1609 if (err != NO_ERROR) { 1610 hasErrors = localHasErrors = true; 1611 } 1612 } 1613 } 1614 } 1615 1616#if 0 1617 if (comment.size() > 0) { 1618 printf("Comment for @%s:%s/%s: %s\n", String8(myPackage).string(), 1619 String8(curType).string(), String8(ident).string(), 1620 String8(comment).string()); 1621 } 1622#endif 1623 if (!localHasErrors) { 1624 outTable->appendComment(myPackage, curType, ident, comment, false); 1625 } 1626 } 1627 else if (code == ResXMLTree::END_TAG) { 1628 if (strcmp16(block.getElementName(&len), resources16.string()) != 0) { 1629 SourcePos(in->getPrintableSource(), block.getLineNumber()).error( 1630 "Unexpected end tag %s\n", String8(block.getElementName(&len)).string()); 1631 return UNKNOWN_ERROR; 1632 } 1633 } 1634 else if (code == ResXMLTree::START_NAMESPACE || code == ResXMLTree::END_NAMESPACE) { 1635 } 1636 else if (code == ResXMLTree::TEXT) { 1637 if (isWhitespace(block.getText(&len))) { 1638 continue; 1639 } 1640 SourcePos(in->getPrintableSource(), block.getLineNumber()).error( 1641 "Found text \"%s\" where item tag is expected\n", 1642 String8(block.getText(&len)).string()); 1643 return UNKNOWN_ERROR; 1644 } 1645 } 1646 1647 // For every resource defined, there must be exist one variant with a product attribute 1648 // set to 'default' (or no product attribute at all). 1649 // We check to see that for every resource that was ignored because of a mismatched 1650 // product attribute, some product variant of that resource was processed. 1651 for (size_t i = 0; i < skippedResourceNames.size(); i++) { 1652 if (skippedResourceNames[i]) { 1653 const type_ident_pair_t& p = skippedResourceNames.keyAt(i); 1654 if (!outTable->hasBagOrEntry(myPackage, p.type, p.ident)) { 1655 const char* bundleProduct = 1656 (bundle->getProduct() == NULL) ? "" : bundle->getProduct(); 1657 fprintf(stderr, "In resource file %s: %s\n", 1658 in->getPrintableSource().string(), 1659 curParams.toString().string()); 1660 1661 fprintf(stderr, "\t%s '%s' does not match product %s.\n" 1662 "\tYou may have forgotten to include a 'default' product variant" 1663 " of the resource.\n", 1664 String8(p.type).string(), String8(p.ident).string(), 1665 bundleProduct[0] == 0 ? "default" : bundleProduct); 1666 return UNKNOWN_ERROR; 1667 } 1668 } 1669 } 1670 1671 return hasErrors ? UNKNOWN_ERROR : NO_ERROR; 1672} 1673 1674ResourceTable::ResourceTable(Bundle* bundle, const String16& assetsPackage) 1675 : mAssetsPackage(assetsPackage), mNextPackageId(1), mHaveAppPackage(false), 1676 mIsAppPackage(!bundle->getExtending()), 1677 mNumLocal(0), 1678 mBundle(bundle) 1679{ 1680} 1681 1682status_t ResourceTable::addIncludedResources(Bundle* bundle, const sp<AaptAssets>& assets) 1683{ 1684 status_t err = assets->buildIncludedResources(bundle); 1685 if (err != NO_ERROR) { 1686 return err; 1687 } 1688 1689 // For future reference to included resources. 1690 mAssets = assets; 1691 1692 const ResTable& incl = assets->getIncludedResources(); 1693 1694 // Retrieve all the packages. 1695 const size_t N = incl.getBasePackageCount(); 1696 for (size_t phase=0; phase<2; phase++) { 1697 for (size_t i=0; i<N; i++) { 1698 String16 name(incl.getBasePackageName(i)); 1699 uint32_t id = incl.getBasePackageId(i); 1700 // First time through: only add base packages (id 1701 // is not 0); second time through add the other 1702 // packages. 1703 if (phase != 0) { 1704 if (id != 0) { 1705 // Skip base packages -- already one. 1706 id = 0; 1707 } else { 1708 // Assign a dynamic id. 1709 id = mNextPackageId; 1710 } 1711 } else if (id != 0) { 1712 if (id == 127) { 1713 if (mHaveAppPackage) { 1714 fprintf(stderr, "Included resources have two application packages!\n"); 1715 return UNKNOWN_ERROR; 1716 } 1717 mHaveAppPackage = true; 1718 } 1719 if (mNextPackageId > id) { 1720 fprintf(stderr, "Included base package ID %d already in use!\n", id); 1721 return UNKNOWN_ERROR; 1722 } 1723 } 1724 if (id != 0) { 1725 NOISY(printf("Including package %s with ID=%d\n", 1726 String8(name).string(), id)); 1727 sp<Package> p = new Package(name, id); 1728 mPackages.add(name, p); 1729 mOrderedPackages.add(p); 1730 1731 if (id >= mNextPackageId) { 1732 mNextPackageId = id+1; 1733 } 1734 } 1735 } 1736 } 1737 1738 // Every resource table always has one first entry, the bag attributes. 1739 const SourcePos unknown(String8("????"), 0); 1740 sp<Type> attr = getType(mAssetsPackage, String16("attr"), unknown); 1741 1742 return NO_ERROR; 1743} 1744 1745status_t ResourceTable::addPublic(const SourcePos& sourcePos, 1746 const String16& package, 1747 const String16& type, 1748 const String16& name, 1749 const uint32_t ident) 1750{ 1751 uint32_t rid = mAssets->getIncludedResources() 1752 .identifierForName(name.string(), name.size(), 1753 type.string(), type.size(), 1754 package.string(), package.size()); 1755 if (rid != 0) { 1756 sourcePos.error("Error declaring public resource %s/%s for included package %s\n", 1757 String8(type).string(), String8(name).string(), 1758 String8(package).string()); 1759 return UNKNOWN_ERROR; 1760 } 1761 1762 sp<Type> t = getType(package, type, sourcePos); 1763 if (t == NULL) { 1764 return UNKNOWN_ERROR; 1765 } 1766 return t->addPublic(sourcePos, name, ident); 1767} 1768 1769status_t ResourceTable::addEntry(const SourcePos& sourcePos, 1770 const String16& package, 1771 const String16& type, 1772 const String16& name, 1773 const String16& value, 1774 const Vector<StringPool::entry_style_span>* style, 1775 const ResTable_config* params, 1776 const bool doSetIndex, 1777 const int32_t format, 1778 const bool overwrite) 1779{ 1780 // Check for adding entries in other packages... for now we do 1781 // nothing. We need to do the right thing here to support skinning. 1782 uint32_t rid = mAssets->getIncludedResources() 1783 .identifierForName(name.string(), name.size(), 1784 type.string(), type.size(), 1785 package.string(), package.size()); 1786 if (rid != 0) { 1787 return NO_ERROR; 1788 } 1789 1790#if 0 1791 if (name == String16("left")) { 1792 printf("Adding entry left: file=%s, line=%d, type=%s, value=%s\n", 1793 sourcePos.file.string(), sourcePos.line, String8(type).string(), 1794 String8(value).string()); 1795 } 1796#endif 1797 1798 sp<Entry> e = getEntry(package, type, name, sourcePos, overwrite, 1799 params, doSetIndex); 1800 if (e == NULL) { 1801 return UNKNOWN_ERROR; 1802 } 1803 status_t err = e->setItem(sourcePos, value, style, format, overwrite); 1804 if (err == NO_ERROR) { 1805 mNumLocal++; 1806 } 1807 return err; 1808} 1809 1810status_t ResourceTable::startBag(const SourcePos& sourcePos, 1811 const String16& package, 1812 const String16& type, 1813 const String16& name, 1814 const String16& bagParent, 1815 const ResTable_config* params, 1816 bool overlay, 1817 bool replace, bool isId) 1818{ 1819 status_t result = NO_ERROR; 1820 1821 // Check for adding entries in other packages... for now we do 1822 // nothing. We need to do the right thing here to support skinning. 1823 uint32_t rid = mAssets->getIncludedResources() 1824 .identifierForName(name.string(), name.size(), 1825 type.string(), type.size(), 1826 package.string(), package.size()); 1827 if (rid != 0) { 1828 return NO_ERROR; 1829 } 1830 1831#if 0 1832 if (name == String16("left")) { 1833 printf("Adding bag left: file=%s, line=%d, type=%s\n", 1834 sourcePos.file.striing(), sourcePos.line, String8(type).string()); 1835 } 1836#endif 1837 if (overlay && !mBundle->getAutoAddOverlay() && !hasBagOrEntry(package, type, name)) { 1838 bool canAdd = false; 1839 sp<Package> p = mPackages.valueFor(package); 1840 if (p != NULL) { 1841 sp<Type> t = p->getTypes().valueFor(type); 1842 if (t != NULL) { 1843 if (t->getCanAddEntries().indexOf(name) >= 0) { 1844 canAdd = true; 1845 } 1846 } 1847 } 1848 if (!canAdd) { 1849 sourcePos.error("Resource does not already exist in overlay at '%s'; use <add-resource> to add.\n", 1850 String8(name).string()); 1851 return UNKNOWN_ERROR; 1852 } 1853 } 1854 sp<Entry> e = getEntry(package, type, name, sourcePos, overlay, params); 1855 if (e == NULL) { 1856 return UNKNOWN_ERROR; 1857 } 1858 1859 // If a parent is explicitly specified, set it. 1860 if (bagParent.size() > 0) { 1861 e->setParent(bagParent); 1862 } 1863 1864 if ((result = e->makeItABag(sourcePos)) != NO_ERROR) { 1865 return result; 1866 } 1867 1868 if (overlay && replace) { 1869 return e->emptyBag(sourcePos); 1870 } 1871 return result; 1872} 1873 1874status_t ResourceTable::addBag(const SourcePos& sourcePos, 1875 const String16& package, 1876 const String16& type, 1877 const String16& name, 1878 const String16& bagParent, 1879 const String16& bagKey, 1880 const String16& value, 1881 const Vector<StringPool::entry_style_span>* style, 1882 const ResTable_config* params, 1883 bool replace, bool isId, const int32_t format) 1884{ 1885 // Check for adding entries in other packages... for now we do 1886 // nothing. We need to do the right thing here to support skinning. 1887 uint32_t rid = mAssets->getIncludedResources() 1888 .identifierForName(name.string(), name.size(), 1889 type.string(), type.size(), 1890 package.string(), package.size()); 1891 if (rid != 0) { 1892 return NO_ERROR; 1893 } 1894 1895#if 0 1896 if (name == String16("left")) { 1897 printf("Adding bag left: file=%s, line=%d, type=%s\n", 1898 sourcePos.file.striing(), sourcePos.line, String8(type).string()); 1899 } 1900#endif 1901 sp<Entry> e = getEntry(package, type, name, sourcePos, replace, params); 1902 if (e == NULL) { 1903 return UNKNOWN_ERROR; 1904 } 1905 1906 // If a parent is explicitly specified, set it. 1907 if (bagParent.size() > 0) { 1908 e->setParent(bagParent); 1909 } 1910 1911 const bool first = e->getBag().indexOfKey(bagKey) < 0; 1912 status_t err = e->addToBag(sourcePos, bagKey, value, style, replace, isId, format); 1913 if (err == NO_ERROR && first) { 1914 mNumLocal++; 1915 } 1916 return err; 1917} 1918 1919bool ResourceTable::hasBagOrEntry(const String16& package, 1920 const String16& type, 1921 const String16& name) const 1922{ 1923 // First look for this in the included resources... 1924 uint32_t rid = mAssets->getIncludedResources() 1925 .identifierForName(name.string(), name.size(), 1926 type.string(), type.size(), 1927 package.string(), package.size()); 1928 if (rid != 0) { 1929 return true; 1930 } 1931 1932 sp<Package> p = mPackages.valueFor(package); 1933 if (p != NULL) { 1934 sp<Type> t = p->getTypes().valueFor(type); 1935 if (t != NULL) { 1936 sp<ConfigList> c = t->getConfigs().valueFor(name); 1937 if (c != NULL) return true; 1938 } 1939 } 1940 1941 return false; 1942} 1943 1944bool ResourceTable::hasBagOrEntry(const String16& package, 1945 const String16& type, 1946 const String16& name, 1947 const ResTable_config& config) const 1948{ 1949 // First look for this in the included resources... 1950 uint32_t rid = mAssets->getIncludedResources() 1951 .identifierForName(name.string(), name.size(), 1952 type.string(), type.size(), 1953 package.string(), package.size()); 1954 if (rid != 0) { 1955 return true; 1956 } 1957 1958 sp<Package> p = mPackages.valueFor(package); 1959 if (p != NULL) { 1960 sp<Type> t = p->getTypes().valueFor(type); 1961 if (t != NULL) { 1962 sp<ConfigList> c = t->getConfigs().valueFor(name); 1963 if (c != NULL) { 1964 sp<Entry> e = c->getEntries().valueFor(config); 1965 if (e != NULL) { 1966 return true; 1967 } 1968 } 1969 } 1970 } 1971 1972 return false; 1973} 1974 1975bool ResourceTable::hasBagOrEntry(const String16& ref, 1976 const String16* defType, 1977 const String16* defPackage) 1978{ 1979 String16 package, type, name; 1980 if (!ResTable::expandResourceRef(ref.string(), ref.size(), &package, &type, &name, 1981 defType, defPackage ? defPackage:&mAssetsPackage, NULL)) { 1982 return false; 1983 } 1984 return hasBagOrEntry(package, type, name); 1985} 1986 1987bool ResourceTable::appendComment(const String16& package, 1988 const String16& type, 1989 const String16& name, 1990 const String16& comment, 1991 bool onlyIfEmpty) 1992{ 1993 if (comment.size() <= 0) { 1994 return true; 1995 } 1996 1997 sp<Package> p = mPackages.valueFor(package); 1998 if (p != NULL) { 1999 sp<Type> t = p->getTypes().valueFor(type); 2000 if (t != NULL) { 2001 sp<ConfigList> c = t->getConfigs().valueFor(name); 2002 if (c != NULL) { 2003 c->appendComment(comment, onlyIfEmpty); 2004 return true; 2005 } 2006 } 2007 } 2008 return false; 2009} 2010 2011bool ResourceTable::appendTypeComment(const String16& package, 2012 const String16& type, 2013 const String16& name, 2014 const String16& comment) 2015{ 2016 if (comment.size() <= 0) { 2017 return true; 2018 } 2019 2020 sp<Package> p = mPackages.valueFor(package); 2021 if (p != NULL) { 2022 sp<Type> t = p->getTypes().valueFor(type); 2023 if (t != NULL) { 2024 sp<ConfigList> c = t->getConfigs().valueFor(name); 2025 if (c != NULL) { 2026 c->appendTypeComment(comment); 2027 return true; 2028 } 2029 } 2030 } 2031 return false; 2032} 2033 2034void ResourceTable::canAddEntry(const SourcePos& pos, 2035 const String16& package, const String16& type, const String16& name) 2036{ 2037 sp<Type> t = getType(package, type, pos); 2038 if (t != NULL) { 2039 t->canAddEntry(name); 2040 } 2041} 2042 2043size_t ResourceTable::size() const { 2044 return mPackages.size(); 2045} 2046 2047size_t ResourceTable::numLocalResources() const { 2048 return mNumLocal; 2049} 2050 2051bool ResourceTable::hasResources() const { 2052 return mNumLocal > 0; 2053} 2054 2055sp<AaptFile> ResourceTable::flatten(Bundle* bundle) 2056{ 2057 sp<AaptFile> data = new AaptFile(String8(), AaptGroupEntry(), String8()); 2058 status_t err = flatten(bundle, data); 2059 return err == NO_ERROR ? data : NULL; 2060} 2061 2062inline uint32_t ResourceTable::getResId(const sp<Package>& p, 2063 const sp<Type>& t, 2064 uint32_t nameId) 2065{ 2066 return makeResId(p->getAssignedId(), t->getIndex(), nameId); 2067} 2068 2069uint32_t ResourceTable::getResId(const String16& package, 2070 const String16& type, 2071 const String16& name, 2072 bool onlyPublic) const 2073{ 2074 uint32_t id = ResourceIdCache::lookup(package, type, name, onlyPublic); 2075 if (id != 0) return id; // cache hit 2076 2077 sp<Package> p = mPackages.valueFor(package); 2078 if (p == NULL) return 0; 2079 2080 // First look for this in the included resources... 2081 uint32_t specFlags = 0; 2082 uint32_t rid = mAssets->getIncludedResources() 2083 .identifierForName(name.string(), name.size(), 2084 type.string(), type.size(), 2085 package.string(), package.size(), 2086 &specFlags); 2087 if (rid != 0) { 2088 if (onlyPublic) { 2089 if ((specFlags & ResTable_typeSpec::SPEC_PUBLIC) == 0) { 2090 return 0; 2091 } 2092 } 2093 2094 if (Res_INTERNALID(rid)) { 2095 return ResourceIdCache::store(package, type, name, onlyPublic, rid); 2096 } 2097 return ResourceIdCache::store(package, type, name, onlyPublic, 2098 Res_MAKEID(p->getAssignedId()-1, Res_GETTYPE(rid), Res_GETENTRY(rid))); 2099 } 2100 2101 sp<Type> t = p->getTypes().valueFor(type); 2102 if (t == NULL) return 0; 2103 sp<ConfigList> c = t->getConfigs().valueFor(name); 2104 if (c == NULL) return 0; 2105 int32_t ei = c->getEntryIndex(); 2106 if (ei < 0) return 0; 2107 2108 return ResourceIdCache::store(package, type, name, onlyPublic, 2109 getResId(p, t, ei)); 2110} 2111 2112uint32_t ResourceTable::getResId(const String16& ref, 2113 const String16* defType, 2114 const String16* defPackage, 2115 const char** outErrorMsg, 2116 bool onlyPublic) const 2117{ 2118 String16 package, type, name; 2119 bool refOnlyPublic = true; 2120 if (!ResTable::expandResourceRef( 2121 ref.string(), ref.size(), &package, &type, &name, 2122 defType, defPackage ? defPackage:&mAssetsPackage, 2123 outErrorMsg, &refOnlyPublic)) { 2124 NOISY(printf("Expanding resource: ref=%s\n", 2125 String8(ref).string())); 2126 NOISY(printf("Expanding resource: defType=%s\n", 2127 defType ? String8(*defType).string() : "NULL")); 2128 NOISY(printf("Expanding resource: defPackage=%s\n", 2129 defPackage ? String8(*defPackage).string() : "NULL")); 2130 NOISY(printf("Expanding resource: ref=%s\n", String8(ref).string())); 2131 NOISY(printf("Expanded resource: p=%s, t=%s, n=%s, res=0\n", 2132 String8(package).string(), String8(type).string(), 2133 String8(name).string())); 2134 return 0; 2135 } 2136 uint32_t res = getResId(package, type, name, onlyPublic && refOnlyPublic); 2137 NOISY(printf("Expanded resource: p=%s, t=%s, n=%s, res=%d\n", 2138 String8(package).string(), String8(type).string(), 2139 String8(name).string(), res)); 2140 if (res == 0) { 2141 if (outErrorMsg) 2142 *outErrorMsg = "No resource found that matches the given name"; 2143 } 2144 return res; 2145} 2146 2147bool ResourceTable::isValidResourceName(const String16& s) 2148{ 2149 const char16_t* p = s.string(); 2150 bool first = true; 2151 while (*p) { 2152 if ((*p >= 'a' && *p <= 'z') 2153 || (*p >= 'A' && *p <= 'Z') 2154 || *p == '_' 2155 || (!first && *p >= '0' && *p <= '9')) { 2156 first = false; 2157 p++; 2158 continue; 2159 } 2160 return false; 2161 } 2162 return true; 2163} 2164 2165bool ResourceTable::stringToValue(Res_value* outValue, StringPool* pool, 2166 const String16& str, 2167 bool preserveSpaces, bool coerceType, 2168 uint32_t attrID, 2169 const Vector<StringPool::entry_style_span>* style, 2170 String16* outStr, void* accessorCookie, 2171 uint32_t attrType, const String8* configTypeName, 2172 const ConfigDescription* config) 2173{ 2174 String16 finalStr; 2175 2176 bool res = true; 2177 if (style == NULL || style->size() == 0) { 2178 // Text is not styled so it can be any type... let's figure it out. 2179 res = mAssets->getIncludedResources() 2180 .stringToValue(outValue, &finalStr, str.string(), str.size(), preserveSpaces, 2181 coerceType, attrID, NULL, &mAssetsPackage, this, 2182 accessorCookie, attrType); 2183 } else { 2184 // Styled text can only be a string, and while collecting the style 2185 // information we have already processed that string! 2186 outValue->size = sizeof(Res_value); 2187 outValue->res0 = 0; 2188 outValue->dataType = outValue->TYPE_STRING; 2189 outValue->data = 0; 2190 finalStr = str; 2191 } 2192 2193 if (!res) { 2194 return false; 2195 } 2196 2197 if (outValue->dataType == outValue->TYPE_STRING) { 2198 // Should do better merging styles. 2199 if (pool) { 2200 String8 configStr; 2201 if (config != NULL) { 2202 configStr = config->toString(); 2203 } else { 2204 configStr = "(null)"; 2205 } 2206 NOISY(printf("Adding to pool string style #%d config %s: %s\n", 2207 style != NULL ? style->size() : 0, 2208 configStr.string(), String8(finalStr).string())); 2209 if (style != NULL && style->size() > 0) { 2210 outValue->data = pool->add(finalStr, *style, configTypeName, config); 2211 } else { 2212 outValue->data = pool->add(finalStr, true, configTypeName, config); 2213 } 2214 } else { 2215 // Caller will fill this in later. 2216 outValue->data = 0; 2217 } 2218 2219 if (outStr) { 2220 *outStr = finalStr; 2221 } 2222 2223 } 2224 2225 return true; 2226} 2227 2228uint32_t ResourceTable::getCustomResource( 2229 const String16& package, const String16& type, const String16& name) const 2230{ 2231 //printf("getCustomResource: %s %s %s\n", String8(package).string(), 2232 // String8(type).string(), String8(name).string()); 2233 sp<Package> p = mPackages.valueFor(package); 2234 if (p == NULL) return 0; 2235 sp<Type> t = p->getTypes().valueFor(type); 2236 if (t == NULL) return 0; 2237 sp<ConfigList> c = t->getConfigs().valueFor(name); 2238 if (c == NULL) return 0; 2239 int32_t ei = c->getEntryIndex(); 2240 if (ei < 0) return 0; 2241 return getResId(p, t, ei); 2242} 2243 2244uint32_t ResourceTable::getCustomResourceWithCreation( 2245 const String16& package, const String16& type, const String16& name, 2246 const bool createIfNotFound) 2247{ 2248 uint32_t resId = getCustomResource(package, type, name); 2249 if (resId != 0 || !createIfNotFound) { 2250 return resId; 2251 } 2252 String16 value("false"); 2253 2254 status_t status = addEntry(mCurrentXmlPos, package, type, name, value, NULL, NULL, true); 2255 if (status == NO_ERROR) { 2256 resId = getResId(package, type, name); 2257 return resId; 2258 } 2259 return 0; 2260} 2261 2262uint32_t ResourceTable::getRemappedPackage(uint32_t origPackage) const 2263{ 2264 return origPackage; 2265} 2266 2267bool ResourceTable::getAttributeType(uint32_t attrID, uint32_t* outType) 2268{ 2269 //printf("getAttributeType #%08x\n", attrID); 2270 Res_value value; 2271 if (getItemValue(attrID, ResTable_map::ATTR_TYPE, &value)) { 2272 //printf("getAttributeType #%08x (%s): #%08x\n", attrID, 2273 // String8(getEntry(attrID)->getName()).string(), value.data); 2274 *outType = value.data; 2275 return true; 2276 } 2277 return false; 2278} 2279 2280bool ResourceTable::getAttributeMin(uint32_t attrID, uint32_t* outMin) 2281{ 2282 //printf("getAttributeMin #%08x\n", attrID); 2283 Res_value value; 2284 if (getItemValue(attrID, ResTable_map::ATTR_MIN, &value)) { 2285 *outMin = value.data; 2286 return true; 2287 } 2288 return false; 2289} 2290 2291bool ResourceTable::getAttributeMax(uint32_t attrID, uint32_t* outMax) 2292{ 2293 //printf("getAttributeMax #%08x\n", attrID); 2294 Res_value value; 2295 if (getItemValue(attrID, ResTable_map::ATTR_MAX, &value)) { 2296 *outMax = value.data; 2297 return true; 2298 } 2299 return false; 2300} 2301 2302uint32_t ResourceTable::getAttributeL10N(uint32_t attrID) 2303{ 2304 //printf("getAttributeL10N #%08x\n", attrID); 2305 Res_value value; 2306 if (getItemValue(attrID, ResTable_map::ATTR_L10N, &value)) { 2307 return value.data; 2308 } 2309 return ResTable_map::L10N_NOT_REQUIRED; 2310} 2311 2312bool ResourceTable::getLocalizationSetting() 2313{ 2314 return mBundle->getRequireLocalization(); 2315} 2316 2317void ResourceTable::reportError(void* accessorCookie, const char* fmt, ...) 2318{ 2319 if (accessorCookie != NULL && fmt != NULL) { 2320 AccessorCookie* ac = (AccessorCookie*)accessorCookie; 2321 int retval=0; 2322 char buf[1024]; 2323 va_list ap; 2324 va_start(ap, fmt); 2325 retval = vsnprintf(buf, sizeof(buf), fmt, ap); 2326 va_end(ap); 2327 ac->sourcePos.error("Error: %s (at '%s' with value '%s').\n", 2328 buf, ac->attr.string(), ac->value.string()); 2329 } 2330} 2331 2332bool ResourceTable::getAttributeKeys( 2333 uint32_t attrID, Vector<String16>* outKeys) 2334{ 2335 sp<const Entry> e = getEntry(attrID); 2336 if (e != NULL) { 2337 const size_t N = e->getBag().size(); 2338 for (size_t i=0; i<N; i++) { 2339 const String16& key = e->getBag().keyAt(i); 2340 if (key.size() > 0 && key.string()[0] != '^') { 2341 outKeys->add(key); 2342 } 2343 } 2344 return true; 2345 } 2346 return false; 2347} 2348 2349bool ResourceTable::getAttributeEnum( 2350 uint32_t attrID, const char16_t* name, size_t nameLen, 2351 Res_value* outValue) 2352{ 2353 //printf("getAttributeEnum #%08x %s\n", attrID, String8(name, nameLen).string()); 2354 String16 nameStr(name, nameLen); 2355 sp<const Entry> e = getEntry(attrID); 2356 if (e != NULL) { 2357 const size_t N = e->getBag().size(); 2358 for (size_t i=0; i<N; i++) { 2359 //printf("Comparing %s to %s\n", String8(name, nameLen).string(), 2360 // String8(e->getBag().keyAt(i)).string()); 2361 if (e->getBag().keyAt(i) == nameStr) { 2362 return getItemValue(attrID, e->getBag().valueAt(i).bagKeyId, outValue); 2363 } 2364 } 2365 } 2366 return false; 2367} 2368 2369bool ResourceTable::getAttributeFlags( 2370 uint32_t attrID, const char16_t* name, size_t nameLen, 2371 Res_value* outValue) 2372{ 2373 outValue->dataType = Res_value::TYPE_INT_HEX; 2374 outValue->data = 0; 2375 2376 //printf("getAttributeFlags #%08x %s\n", attrID, String8(name, nameLen).string()); 2377 String16 nameStr(name, nameLen); 2378 sp<const Entry> e = getEntry(attrID); 2379 if (e != NULL) { 2380 const size_t N = e->getBag().size(); 2381 2382 const char16_t* end = name + nameLen; 2383 const char16_t* pos = name; 2384 while (pos < end) { 2385 const char16_t* start = pos; 2386 while (pos < end && *pos != '|') { 2387 pos++; 2388 } 2389 2390 String16 nameStr(start, pos-start); 2391 size_t i; 2392 for (i=0; i<N; i++) { 2393 //printf("Comparing \"%s\" to \"%s\"\n", String8(nameStr).string(), 2394 // String8(e->getBag().keyAt(i)).string()); 2395 if (e->getBag().keyAt(i) == nameStr) { 2396 Res_value val; 2397 bool got = getItemValue(attrID, e->getBag().valueAt(i).bagKeyId, &val); 2398 if (!got) { 2399 return false; 2400 } 2401 //printf("Got value: 0x%08x\n", val.data); 2402 outValue->data |= val.data; 2403 break; 2404 } 2405 } 2406 2407 if (i >= N) { 2408 // Didn't find this flag identifier. 2409 return false; 2410 } 2411 pos++; 2412 } 2413 2414 return true; 2415 } 2416 return false; 2417} 2418 2419status_t ResourceTable::assignResourceIds() 2420{ 2421 const size_t N = mOrderedPackages.size(); 2422 size_t pi; 2423 status_t firstError = NO_ERROR; 2424 2425 // First generate all bag attributes and assign indices. 2426 for (pi=0; pi<N; pi++) { 2427 sp<Package> p = mOrderedPackages.itemAt(pi); 2428 if (p == NULL || p->getTypes().size() == 0) { 2429 // Empty, skip! 2430 continue; 2431 } 2432 2433 status_t err = p->applyPublicTypeOrder(); 2434 if (err != NO_ERROR && firstError == NO_ERROR) { 2435 firstError = err; 2436 } 2437 2438 // Generate attributes... 2439 const size_t N = p->getOrderedTypes().size(); 2440 size_t ti; 2441 for (ti=0; ti<N; ti++) { 2442 sp<Type> t = p->getOrderedTypes().itemAt(ti); 2443 if (t == NULL) { 2444 continue; 2445 } 2446 const size_t N = t->getOrderedConfigs().size(); 2447 for (size_t ci=0; ci<N; ci++) { 2448 sp<ConfigList> c = t->getOrderedConfigs().itemAt(ci); 2449 if (c == NULL) { 2450 continue; 2451 } 2452 const size_t N = c->getEntries().size(); 2453 for (size_t ei=0; ei<N; ei++) { 2454 sp<Entry> e = c->getEntries().valueAt(ei); 2455 if (e == NULL) { 2456 continue; 2457 } 2458 status_t err = e->generateAttributes(this, p->getName()); 2459 if (err != NO_ERROR && firstError == NO_ERROR) { 2460 firstError = err; 2461 } 2462 } 2463 } 2464 } 2465 2466 const SourcePos unknown(String8("????"), 0); 2467 sp<Type> attr = p->getType(String16("attr"), unknown); 2468 2469 // Assign indices... 2470 for (ti=0; ti<N; ti++) { 2471 sp<Type> t = p->getOrderedTypes().itemAt(ti); 2472 if (t == NULL) { 2473 continue; 2474 } 2475 err = t->applyPublicEntryOrder(); 2476 if (err != NO_ERROR && firstError == NO_ERROR) { 2477 firstError = err; 2478 } 2479 2480 const size_t N = t->getOrderedConfigs().size(); 2481 t->setIndex(ti+1); 2482 2483 LOG_ALWAYS_FATAL_IF(ti == 0 && attr != t, 2484 "First type is not attr!"); 2485 2486 for (size_t ei=0; ei<N; ei++) { 2487 sp<ConfigList> c = t->getOrderedConfigs().itemAt(ei); 2488 if (c == NULL) { 2489 continue; 2490 } 2491 c->setEntryIndex(ei); 2492 } 2493 } 2494 2495 // Assign resource IDs to keys in bags... 2496 for (ti=0; ti<N; ti++) { 2497 sp<Type> t = p->getOrderedTypes().itemAt(ti); 2498 if (t == NULL) { 2499 continue; 2500 } 2501 const size_t N = t->getOrderedConfigs().size(); 2502 for (size_t ci=0; ci<N; ci++) { 2503 sp<ConfigList> c = t->getOrderedConfigs().itemAt(ci); 2504 //printf("Ordered config #%d: %p\n", ci, c.get()); 2505 const size_t N = c->getEntries().size(); 2506 for (size_t ei=0; ei<N; ei++) { 2507 sp<Entry> e = c->getEntries().valueAt(ei); 2508 if (e == NULL) { 2509 continue; 2510 } 2511 status_t err = e->assignResourceIds(this, p->getName()); 2512 if (err != NO_ERROR && firstError == NO_ERROR) { 2513 firstError = err; 2514 } 2515 } 2516 } 2517 } 2518 } 2519 return firstError; 2520} 2521 2522status_t ResourceTable::addSymbols(const sp<AaptSymbols>& outSymbols) { 2523 const size_t N = mOrderedPackages.size(); 2524 size_t pi; 2525 2526 for (pi=0; pi<N; pi++) { 2527 sp<Package> p = mOrderedPackages.itemAt(pi); 2528 if (p->getTypes().size() == 0) { 2529 // Empty, skip! 2530 continue; 2531 } 2532 2533 const size_t N = p->getOrderedTypes().size(); 2534 size_t ti; 2535 2536 for (ti=0; ti<N; ti++) { 2537 sp<Type> t = p->getOrderedTypes().itemAt(ti); 2538 if (t == NULL) { 2539 continue; 2540 } 2541 const size_t N = t->getOrderedConfigs().size(); 2542 sp<AaptSymbols> typeSymbols; 2543 typeSymbols = outSymbols->addNestedSymbol(String8(t->getName()), t->getPos()); 2544 for (size_t ci=0; ci<N; ci++) { 2545 sp<ConfigList> c = t->getOrderedConfigs().itemAt(ci); 2546 if (c == NULL) { 2547 continue; 2548 } 2549 uint32_t rid = getResId(p, t, ci); 2550 if (rid == 0) { 2551 return UNKNOWN_ERROR; 2552 } 2553 if (Res_GETPACKAGE(rid) == (size_t)(p->getAssignedId()-1)) { 2554 typeSymbols->addSymbol(String8(c->getName()), rid, c->getPos()); 2555 2556 String16 comment(c->getComment()); 2557 typeSymbols->appendComment(String8(c->getName()), comment, c->getPos()); 2558 //printf("Type symbol [%08x] %s comment: %s\n", rid, 2559 // String8(c->getName()).string(), String8(comment).string()); 2560 comment = c->getTypeComment(); 2561 typeSymbols->appendTypeComment(String8(c->getName()), comment); 2562 } else { 2563#if 0 2564 printf("**** NO MATCH: 0x%08x vs 0x%08x\n", 2565 Res_GETPACKAGE(rid), p->getAssignedId()); 2566#endif 2567 } 2568 } 2569 } 2570 } 2571 return NO_ERROR; 2572} 2573 2574 2575void 2576ResourceTable::addLocalization(const String16& name, const String8& locale, const SourcePos& src) 2577{ 2578 mLocalizations[name][locale] = src; 2579} 2580 2581 2582/*! 2583 * Flag various sorts of localization problems. '+' indicates checks already implemented; 2584 * '-' indicates checks that will be implemented in the future. 2585 * 2586 * + A localized string for which no default-locale version exists => warning 2587 * + A string for which no version in an explicitly-requested locale exists => warning 2588 * + A localized translation of an translateable="false" string => warning 2589 * - A localized string not provided in every locale used by the table 2590 */ 2591status_t 2592ResourceTable::validateLocalizations(void) 2593{ 2594 status_t err = NO_ERROR; 2595 const String8 defaultLocale; 2596 2597 // For all strings... 2598 for (map<String16, map<String8, SourcePos> >::iterator nameIter = mLocalizations.begin(); 2599 nameIter != mLocalizations.end(); 2600 nameIter++) { 2601 const map<String8, SourcePos>& configSrcMap = nameIter->second; 2602 2603 // Look for strings with no default localization 2604 if (configSrcMap.count(defaultLocale) == 0) { 2605 SourcePos().warning("string '%s' has no default translation.", 2606 String8(nameIter->first).string()); 2607 if (mBundle->getVerbose()) { 2608 for (map<String8, SourcePos>::const_iterator locales = configSrcMap.begin(); 2609 locales != configSrcMap.end(); 2610 locales++) { 2611 locales->second.printf("locale %s found", locales->first.string()); 2612 } 2613 } 2614 // !!! TODO: throw an error here in some circumstances 2615 } 2616 2617 // Check that all requested localizations are present for this string 2618 if (mBundle->getConfigurations() != NULL && mBundle->getRequireLocalization()) { 2619 const char* allConfigs = mBundle->getConfigurations(); 2620 const char* start = allConfigs; 2621 const char* comma; 2622 2623 set<String8> missingConfigs; 2624 AaptLocaleValue locale; 2625 do { 2626 String8 config; 2627 comma = strchr(start, ','); 2628 if (comma != NULL) { 2629 config.setTo(start, comma - start); 2630 start = comma + 1; 2631 } else { 2632 config.setTo(start); 2633 } 2634 2635 if (!locale.initFromFilterString(config)) { 2636 continue; 2637 } 2638 2639 // don't bother with the pseudolocale "zz_ZZ" 2640 if (config != "zz_ZZ") { 2641 if (configSrcMap.find(config) == configSrcMap.end()) { 2642 // okay, no specific localization found. it's possible that we are 2643 // requiring a specific regional localization [e.g. de_DE] but there is an 2644 // available string in the generic language localization [e.g. de]; 2645 // consider that string to have fulfilled the localization requirement. 2646 String8 region(config.string(), 2); 2647 if (configSrcMap.find(region) == configSrcMap.end() && 2648 configSrcMap.count(defaultLocale) == 0) { 2649 missingConfigs.insert(config); 2650 } 2651 } 2652 } 2653 } while (comma != NULL); 2654 2655 if (!missingConfigs.empty()) { 2656 String8 configStr; 2657 for (set<String8>::iterator iter = missingConfigs.begin(); 2658 iter != missingConfigs.end(); 2659 iter++) { 2660 configStr.appendFormat(" %s", iter->string()); 2661 } 2662 SourcePos().warning("string '%s' is missing %u required localizations:%s", 2663 String8(nameIter->first).string(), 2664 (unsigned int)missingConfigs.size(), 2665 configStr.string()); 2666 } 2667 } 2668 } 2669 2670 return err; 2671} 2672 2673status_t ResourceTable::flatten(Bundle* bundle, const sp<AaptFile>& dest) 2674{ 2675 ResourceFilter filter; 2676 status_t err = filter.parse(bundle->getConfigurations()); 2677 if (err != NO_ERROR) { 2678 return err; 2679 } 2680 2681 const ConfigDescription nullConfig; 2682 2683 const size_t N = mOrderedPackages.size(); 2684 size_t pi; 2685 2686 const static String16 mipmap16("mipmap"); 2687 2688 bool useUTF8 = !bundle->getUTF16StringsOption(); 2689 2690 // Iterate through all data, collecting all values (strings, 2691 // references, etc). 2692 StringPool valueStrings(useUTF8); 2693 Vector<sp<Entry> > allEntries; 2694 for (pi=0; pi<N; pi++) { 2695 sp<Package> p = mOrderedPackages.itemAt(pi); 2696 if (p->getTypes().size() == 0) { 2697 // Empty, skip! 2698 continue; 2699 } 2700 2701 StringPool typeStrings(useUTF8); 2702 StringPool keyStrings(useUTF8); 2703 2704 const size_t N = p->getOrderedTypes().size(); 2705 for (size_t ti=0; ti<N; ti++) { 2706 sp<Type> t = p->getOrderedTypes().itemAt(ti); 2707 if (t == NULL) { 2708 typeStrings.add(String16("<empty>"), false); 2709 continue; 2710 } 2711 const String16 typeName(t->getName()); 2712 typeStrings.add(typeName, false); 2713 2714 // This is a hack to tweak the sorting order of the final strings, 2715 // to put stuff that is generally not language-specific first. 2716 String8 configTypeName(typeName); 2717 if (configTypeName == "drawable" || configTypeName == "layout" 2718 || configTypeName == "color" || configTypeName == "anim" 2719 || configTypeName == "interpolator" || configTypeName == "animator" 2720 || configTypeName == "xml" || configTypeName == "menu" 2721 || configTypeName == "mipmap" || configTypeName == "raw") { 2722 configTypeName = "1complex"; 2723 } else { 2724 configTypeName = "2value"; 2725 } 2726 2727 const bool filterable = (typeName != mipmap16); 2728 2729 const size_t N = t->getOrderedConfigs().size(); 2730 for (size_t ci=0; ci<N; ci++) { 2731 sp<ConfigList> c = t->getOrderedConfigs().itemAt(ci); 2732 if (c == NULL) { 2733 continue; 2734 } 2735 const size_t N = c->getEntries().size(); 2736 for (size_t ei=0; ei<N; ei++) { 2737 ConfigDescription config = c->getEntries().keyAt(ei); 2738 if (filterable && !filter.match(config)) { 2739 continue; 2740 } 2741 sp<Entry> e = c->getEntries().valueAt(ei); 2742 if (e == NULL) { 2743 continue; 2744 } 2745 e->setNameIndex(keyStrings.add(e->getName(), true)); 2746 2747 // If this entry has no values for other configs, 2748 // and is the default config, then it is special. Otherwise 2749 // we want to add it with the config info. 2750 ConfigDescription* valueConfig = NULL; 2751 if (N != 1 || config == nullConfig) { 2752 valueConfig = &config; 2753 } 2754 2755 status_t err = e->prepareFlatten(&valueStrings, this, 2756 &configTypeName, &config); 2757 if (err != NO_ERROR) { 2758 return err; 2759 } 2760 allEntries.add(e); 2761 } 2762 } 2763 } 2764 2765 p->setTypeStrings(typeStrings.createStringBlock()); 2766 p->setKeyStrings(keyStrings.createStringBlock()); 2767 } 2768 2769 if (bundle->getOutputAPKFile() != NULL) { 2770 // Now we want to sort the value strings for better locality. This will 2771 // cause the positions of the strings to change, so we need to go back 2772 // through out resource entries and update them accordingly. Only need 2773 // to do this if actually writing the output file. 2774 valueStrings.sortByConfig(); 2775 for (pi=0; pi<allEntries.size(); pi++) { 2776 allEntries[pi]->remapStringValue(&valueStrings); 2777 } 2778 } 2779 2780 ssize_t strAmt = 0; 2781 2782 // Now build the array of package chunks. 2783 Vector<sp<AaptFile> > flatPackages; 2784 for (pi=0; pi<N; pi++) { 2785 sp<Package> p = mOrderedPackages.itemAt(pi); 2786 if (p->getTypes().size() == 0) { 2787 // Empty, skip! 2788 continue; 2789 } 2790 2791 const size_t N = p->getTypeStrings().size(); 2792 2793 const size_t baseSize = sizeof(ResTable_package); 2794 2795 // Start the package data. 2796 sp<AaptFile> data = new AaptFile(String8(), AaptGroupEntry(), String8()); 2797 ResTable_package* header = (ResTable_package*)data->editData(baseSize); 2798 if (header == NULL) { 2799 fprintf(stderr, "ERROR: out of memory creating ResTable_package\n"); 2800 return NO_MEMORY; 2801 } 2802 memset(header, 0, sizeof(*header)); 2803 header->header.type = htods(RES_TABLE_PACKAGE_TYPE); 2804 header->header.headerSize = htods(sizeof(*header)); 2805 header->id = htodl(p->getAssignedId()); 2806 strcpy16_htod(header->name, p->getName().string()); 2807 2808 // Write the string blocks. 2809 const size_t typeStringsStart = data->getSize(); 2810 sp<AaptFile> strFile = p->getTypeStringsData(); 2811 ssize_t amt = data->writeData(strFile->getData(), strFile->getSize()); 2812 #if PRINT_STRING_METRICS 2813 fprintf(stderr, "**** type strings: %d\n", amt); 2814 #endif 2815 strAmt += amt; 2816 if (amt < 0) { 2817 return amt; 2818 } 2819 const size_t keyStringsStart = data->getSize(); 2820 strFile = p->getKeyStringsData(); 2821 amt = data->writeData(strFile->getData(), strFile->getSize()); 2822 #if PRINT_STRING_METRICS 2823 fprintf(stderr, "**** key strings: %d\n", amt); 2824 #endif 2825 strAmt += amt; 2826 if (amt < 0) { 2827 return amt; 2828 } 2829 2830 // Build the type chunks inside of this package. 2831 for (size_t ti=0; ti<N; ti++) { 2832 // Retrieve them in the same order as the type string block. 2833 size_t len; 2834 String16 typeName(p->getTypeStrings().stringAt(ti, &len)); 2835 sp<Type> t = p->getTypes().valueFor(typeName); 2836 LOG_ALWAYS_FATAL_IF(t == NULL && typeName != String16("<empty>"), 2837 "Type name %s not found", 2838 String8(typeName).string()); 2839 2840 const bool filterable = (typeName != mipmap16); 2841 2842 const size_t N = t != NULL ? t->getOrderedConfigs().size() : 0; 2843 2844 // Until a non-NO_ENTRY value has been written for a resource, 2845 // that resource is invalid; validResources[i] represents 2846 // the item at t->getOrderedConfigs().itemAt(i). 2847 Vector<bool> validResources; 2848 validResources.insertAt(false, 0, N); 2849 2850 // First write the typeSpec chunk, containing information about 2851 // each resource entry in this type. 2852 { 2853 const size_t typeSpecSize = sizeof(ResTable_typeSpec) + sizeof(uint32_t)*N; 2854 const size_t typeSpecStart = data->getSize(); 2855 ResTable_typeSpec* tsHeader = (ResTable_typeSpec*) 2856 (((uint8_t*)data->editData(typeSpecStart+typeSpecSize)) + typeSpecStart); 2857 if (tsHeader == NULL) { 2858 fprintf(stderr, "ERROR: out of memory creating ResTable_typeSpec\n"); 2859 return NO_MEMORY; 2860 } 2861 memset(tsHeader, 0, sizeof(*tsHeader)); 2862 tsHeader->header.type = htods(RES_TABLE_TYPE_SPEC_TYPE); 2863 tsHeader->header.headerSize = htods(sizeof(*tsHeader)); 2864 tsHeader->header.size = htodl(typeSpecSize); 2865 tsHeader->id = ti+1; 2866 tsHeader->entryCount = htodl(N); 2867 2868 uint32_t* typeSpecFlags = (uint32_t*) 2869 (((uint8_t*)data->editData()) 2870 + typeSpecStart + sizeof(ResTable_typeSpec)); 2871 memset(typeSpecFlags, 0, sizeof(uint32_t)*N); 2872 2873 for (size_t ei=0; ei<N; ei++) { 2874 sp<ConfigList> cl = t->getOrderedConfigs().itemAt(ei); 2875 if (cl->getPublic()) { 2876 typeSpecFlags[ei] |= htodl(ResTable_typeSpec::SPEC_PUBLIC); 2877 } 2878 const size_t CN = cl->getEntries().size(); 2879 for (size_t ci=0; ci<CN; ci++) { 2880 if (filterable && !filter.match(cl->getEntries().keyAt(ci))) { 2881 continue; 2882 } 2883 for (size_t cj=ci+1; cj<CN; cj++) { 2884 if (filterable && !filter.match(cl->getEntries().keyAt(cj))) { 2885 continue; 2886 } 2887 typeSpecFlags[ei] |= htodl( 2888 cl->getEntries().keyAt(ci).diff(cl->getEntries().keyAt(cj))); 2889 } 2890 } 2891 } 2892 } 2893 2894 // We need to write one type chunk for each configuration for 2895 // which we have entries in this type. 2896 const size_t NC = t->getUniqueConfigs().size(); 2897 2898 const size_t typeSize = sizeof(ResTable_type) + sizeof(uint32_t)*N; 2899 2900 for (size_t ci=0; ci<NC; ci++) { 2901 ConfigDescription config = t->getUniqueConfigs().itemAt(ci); 2902 2903 NOISY(printf("Writing config %d config: imsi:%d/%d lang:%c%c cnt:%c%c " 2904 "orien:%d ui:%d touch:%d density:%d key:%d inp:%d nav:%d sz:%dx%d " 2905 "sw%ddp w%ddp h%ddp dir:%d\n", 2906 ti+1, 2907 config.mcc, config.mnc, 2908 config.language[0] ? config.language[0] : '-', 2909 config.language[1] ? config.language[1] : '-', 2910 config.country[0] ? config.country[0] : '-', 2911 config.country[1] ? config.country[1] : '-', 2912 config.orientation, 2913 config.uiMode, 2914 config.touchscreen, 2915 config.density, 2916 config.keyboard, 2917 config.inputFlags, 2918 config.navigation, 2919 config.screenWidth, 2920 config.screenHeight, 2921 config.smallestScreenWidthDp, 2922 config.screenWidthDp, 2923 config.screenHeightDp, 2924 config.layoutDirection)); 2925 2926 if (filterable && !filter.match(config)) { 2927 continue; 2928 } 2929 2930 const size_t typeStart = data->getSize(); 2931 2932 ResTable_type* tHeader = (ResTable_type*) 2933 (((uint8_t*)data->editData(typeStart+typeSize)) + typeStart); 2934 if (tHeader == NULL) { 2935 fprintf(stderr, "ERROR: out of memory creating ResTable_type\n"); 2936 return NO_MEMORY; 2937 } 2938 2939 memset(tHeader, 0, sizeof(*tHeader)); 2940 tHeader->header.type = htods(RES_TABLE_TYPE_TYPE); 2941 tHeader->header.headerSize = htods(sizeof(*tHeader)); 2942 tHeader->id = ti+1; 2943 tHeader->entryCount = htodl(N); 2944 tHeader->entriesStart = htodl(typeSize); 2945 tHeader->config = config; 2946 NOISY(printf("Writing type %d config: imsi:%d/%d lang:%c%c cnt:%c%c " 2947 "orien:%d ui:%d touch:%d density:%d key:%d inp:%d nav:%d sz:%dx%d " 2948 "sw%ddp w%ddp h%ddp dir:%d\n", 2949 ti+1, 2950 tHeader->config.mcc, tHeader->config.mnc, 2951 tHeader->config.language[0] ? tHeader->config.language[0] : '-', 2952 tHeader->config.language[1] ? tHeader->config.language[1] : '-', 2953 tHeader->config.country[0] ? tHeader->config.country[0] : '-', 2954 tHeader->config.country[1] ? tHeader->config.country[1] : '-', 2955 tHeader->config.orientation, 2956 tHeader->config.uiMode, 2957 tHeader->config.touchscreen, 2958 tHeader->config.density, 2959 tHeader->config.keyboard, 2960 tHeader->config.inputFlags, 2961 tHeader->config.navigation, 2962 tHeader->config.screenWidth, 2963 tHeader->config.screenHeight, 2964 tHeader->config.smallestScreenWidthDp, 2965 tHeader->config.screenWidthDp, 2966 tHeader->config.screenHeightDp, 2967 tHeader->config.layoutDirection)); 2968 tHeader->config.swapHtoD(); 2969 2970 // Build the entries inside of this type. 2971 for (size_t ei=0; ei<N; ei++) { 2972 sp<ConfigList> cl = t->getOrderedConfigs().itemAt(ei); 2973 sp<Entry> e = cl->getEntries().valueFor(config); 2974 2975 // Set the offset for this entry in its type. 2976 uint32_t* index = (uint32_t*) 2977 (((uint8_t*)data->editData()) 2978 + typeStart + sizeof(ResTable_type)); 2979 if (e != NULL) { 2980 index[ei] = htodl(data->getSize()-typeStart-typeSize); 2981 2982 // Create the entry. 2983 ssize_t amt = e->flatten(bundle, data, cl->getPublic()); 2984 if (amt < 0) { 2985 return amt; 2986 } 2987 validResources.editItemAt(ei) = true; 2988 } else { 2989 index[ei] = htodl(ResTable_type::NO_ENTRY); 2990 } 2991 } 2992 2993 // Fill in the rest of the type information. 2994 tHeader = (ResTable_type*) 2995 (((uint8_t*)data->editData()) + typeStart); 2996 tHeader->header.size = htodl(data->getSize()-typeStart); 2997 } 2998 2999 bool missing_entry = false; 3000 const char* log_prefix = bundle->getErrorOnMissingConfigEntry() ? 3001 "error" : "warning"; 3002 for (size_t i = 0; i < N; ++i) { 3003 if (!validResources[i]) { 3004 sp<ConfigList> c = t->getOrderedConfigs().itemAt(i); 3005 fprintf(stderr, "%s: no entries written for %s/%s\n", log_prefix, 3006 String8(typeName).string(), String8(c->getName()).string()); 3007 missing_entry = true; 3008 } 3009 } 3010 if (bundle->getErrorOnMissingConfigEntry() && missing_entry) { 3011 fprintf(stderr, "Error: Missing entries, quit!\n"); 3012 return NOT_ENOUGH_DATA; 3013 } 3014 } 3015 3016 // Fill in the rest of the package information. 3017 header = (ResTable_package*)data->editData(); 3018 header->header.size = htodl(data->getSize()); 3019 header->typeStrings = htodl(typeStringsStart); 3020 header->lastPublicType = htodl(p->getTypeStrings().size()); 3021 header->keyStrings = htodl(keyStringsStart); 3022 header->lastPublicKey = htodl(p->getKeyStrings().size()); 3023 3024 flatPackages.add(data); 3025 } 3026 3027 // And now write out the final chunks. 3028 const size_t dataStart = dest->getSize(); 3029 3030 { 3031 // blah 3032 ResTable_header header; 3033 memset(&header, 0, sizeof(header)); 3034 header.header.type = htods(RES_TABLE_TYPE); 3035 header.header.headerSize = htods(sizeof(header)); 3036 header.packageCount = htodl(flatPackages.size()); 3037 status_t err = dest->writeData(&header, sizeof(header)); 3038 if (err != NO_ERROR) { 3039 fprintf(stderr, "ERROR: out of memory creating ResTable_header\n"); 3040 return err; 3041 } 3042 } 3043 3044 ssize_t strStart = dest->getSize(); 3045 err = valueStrings.writeStringBlock(dest); 3046 if (err != NO_ERROR) { 3047 return err; 3048 } 3049 3050 ssize_t amt = (dest->getSize()-strStart); 3051 strAmt += amt; 3052 #if PRINT_STRING_METRICS 3053 fprintf(stderr, "**** value strings: %d\n", amt); 3054 fprintf(stderr, "**** total strings: %d\n", strAmt); 3055 #endif 3056 3057 for (pi=0; pi<flatPackages.size(); pi++) { 3058 err = dest->writeData(flatPackages[pi]->getData(), 3059 flatPackages[pi]->getSize()); 3060 if (err != NO_ERROR) { 3061 fprintf(stderr, "ERROR: out of memory creating package chunk for ResTable_header\n"); 3062 return err; 3063 } 3064 } 3065 3066 ResTable_header* header = (ResTable_header*) 3067 (((uint8_t*)dest->getData()) + dataStart); 3068 header->header.size = htodl(dest->getSize() - dataStart); 3069 3070 NOISY(aout << "Resource table:" 3071 << HexDump(dest->getData(), dest->getSize()) << endl); 3072 3073 #if PRINT_STRING_METRICS 3074 fprintf(stderr, "**** total resource table size: %d / %d%% strings\n", 3075 dest->getSize(), (strAmt*100)/dest->getSize()); 3076 #endif 3077 3078 return NO_ERROR; 3079} 3080 3081void ResourceTable::writePublicDefinitions(const String16& package, FILE* fp) 3082{ 3083 fprintf(fp, 3084 "<!-- This file contains <public> resource definitions for all\n" 3085 " resources that were generated from the source data. -->\n" 3086 "\n" 3087 "<resources>\n"); 3088 3089 writePublicDefinitions(package, fp, true); 3090 writePublicDefinitions(package, fp, false); 3091 3092 fprintf(fp, 3093 "\n" 3094 "</resources>\n"); 3095} 3096 3097void ResourceTable::writePublicDefinitions(const String16& package, FILE* fp, bool pub) 3098{ 3099 bool didHeader = false; 3100 3101 sp<Package> pkg = mPackages.valueFor(package); 3102 if (pkg != NULL) { 3103 const size_t NT = pkg->getOrderedTypes().size(); 3104 for (size_t i=0; i<NT; i++) { 3105 sp<Type> t = pkg->getOrderedTypes().itemAt(i); 3106 if (t == NULL) { 3107 continue; 3108 } 3109 3110 bool didType = false; 3111 3112 const size_t NC = t->getOrderedConfigs().size(); 3113 for (size_t j=0; j<NC; j++) { 3114 sp<ConfigList> c = t->getOrderedConfigs().itemAt(j); 3115 if (c == NULL) { 3116 continue; 3117 } 3118 3119 if (c->getPublic() != pub) { 3120 continue; 3121 } 3122 3123 if (!didType) { 3124 fprintf(fp, "\n"); 3125 didType = true; 3126 } 3127 if (!didHeader) { 3128 if (pub) { 3129 fprintf(fp," <!-- PUBLIC SECTION. These resources have been declared public.\n"); 3130 fprintf(fp," Changes to these definitions will break binary compatibility. -->\n\n"); 3131 } else { 3132 fprintf(fp," <!-- PRIVATE SECTION. These resources have not been declared public.\n"); 3133 fprintf(fp," You can make them public my moving these lines into a file in res/values. -->\n\n"); 3134 } 3135 didHeader = true; 3136 } 3137 if (!pub) { 3138 const size_t NE = c->getEntries().size(); 3139 for (size_t k=0; k<NE; k++) { 3140 const SourcePos& pos = c->getEntries().valueAt(k)->getPos(); 3141 if (pos.file != "") { 3142 fprintf(fp," <!-- Declared at %s:%d -->\n", 3143 pos.file.string(), pos.line); 3144 } 3145 } 3146 } 3147 fprintf(fp, " <public type=\"%s\" name=\"%s\" id=\"0x%08x\" />\n", 3148 String8(t->getName()).string(), 3149 String8(c->getName()).string(), 3150 getResId(pkg, t, c->getEntryIndex())); 3151 } 3152 } 3153 } 3154} 3155 3156ResourceTable::Item::Item(const SourcePos& _sourcePos, 3157 bool _isId, 3158 const String16& _value, 3159 const Vector<StringPool::entry_style_span>* _style, 3160 int32_t _format) 3161 : sourcePos(_sourcePos) 3162 , isId(_isId) 3163 , value(_value) 3164 , format(_format) 3165 , bagKeyId(0) 3166 , evaluating(false) 3167{ 3168 if (_style) { 3169 style = *_style; 3170 } 3171} 3172 3173status_t ResourceTable::Entry::makeItABag(const SourcePos& sourcePos) 3174{ 3175 if (mType == TYPE_BAG) { 3176 return NO_ERROR; 3177 } 3178 if (mType == TYPE_UNKNOWN) { 3179 mType = TYPE_BAG; 3180 return NO_ERROR; 3181 } 3182 sourcePos.error("Resource entry %s is already defined as a single item.\n" 3183 "%s:%d: Originally defined here.\n", 3184 String8(mName).string(), 3185 mItem.sourcePos.file.string(), mItem.sourcePos.line); 3186 return UNKNOWN_ERROR; 3187} 3188 3189status_t ResourceTable::Entry::setItem(const SourcePos& sourcePos, 3190 const String16& value, 3191 const Vector<StringPool::entry_style_span>* style, 3192 int32_t format, 3193 const bool overwrite) 3194{ 3195 Item item(sourcePos, false, value, style); 3196 3197 if (mType == TYPE_BAG) { 3198 const Item& item(mBag.valueAt(0)); 3199 sourcePos.error("Resource entry %s is already defined as a bag.\n" 3200 "%s:%d: Originally defined here.\n", 3201 String8(mName).string(), 3202 item.sourcePos.file.string(), item.sourcePos.line); 3203 return UNKNOWN_ERROR; 3204 } 3205 if ( (mType != TYPE_UNKNOWN) && (overwrite == false) ) { 3206 sourcePos.error("Resource entry %s is already defined.\n" 3207 "%s:%d: Originally defined here.\n", 3208 String8(mName).string(), 3209 mItem.sourcePos.file.string(), mItem.sourcePos.line); 3210 return UNKNOWN_ERROR; 3211 } 3212 3213 mType = TYPE_ITEM; 3214 mItem = item; 3215 mItemFormat = format; 3216 return NO_ERROR; 3217} 3218 3219status_t ResourceTable::Entry::addToBag(const SourcePos& sourcePos, 3220 const String16& key, const String16& value, 3221 const Vector<StringPool::entry_style_span>* style, 3222 bool replace, bool isId, int32_t format) 3223{ 3224 status_t err = makeItABag(sourcePos); 3225 if (err != NO_ERROR) { 3226 return err; 3227 } 3228 3229 Item item(sourcePos, isId, value, style, format); 3230 3231 // XXX NOTE: there is an error if you try to have a bag with two keys, 3232 // one an attr and one an id, with the same name. Not something we 3233 // currently ever have to worry about. 3234 ssize_t origKey = mBag.indexOfKey(key); 3235 if (origKey >= 0) { 3236 if (!replace) { 3237 const Item& item(mBag.valueAt(origKey)); 3238 sourcePos.error("Resource entry %s already has bag item %s.\n" 3239 "%s:%d: Originally defined here.\n", 3240 String8(mName).string(), String8(key).string(), 3241 item.sourcePos.file.string(), item.sourcePos.line); 3242 return UNKNOWN_ERROR; 3243 } 3244 //printf("Replacing %s with %s\n", 3245 // String8(mBag.valueFor(key).value).string(), String8(value).string()); 3246 mBag.replaceValueFor(key, item); 3247 } 3248 3249 mBag.add(key, item); 3250 return NO_ERROR; 3251} 3252 3253status_t ResourceTable::Entry::emptyBag(const SourcePos& sourcePos) 3254{ 3255 status_t err = makeItABag(sourcePos); 3256 if (err != NO_ERROR) { 3257 return err; 3258 } 3259 3260 mBag.clear(); 3261 return NO_ERROR; 3262} 3263 3264status_t ResourceTable::Entry::generateAttributes(ResourceTable* table, 3265 const String16& package) 3266{ 3267 const String16 attr16("attr"); 3268 const String16 id16("id"); 3269 const size_t N = mBag.size(); 3270 for (size_t i=0; i<N; i++) { 3271 const String16& key = mBag.keyAt(i); 3272 const Item& it = mBag.valueAt(i); 3273 if (it.isId) { 3274 if (!table->hasBagOrEntry(key, &id16, &package)) { 3275 String16 value("false"); 3276 status_t err = table->addEntry(SourcePos(String8("<generated>"), 0), package, 3277 id16, key, value); 3278 if (err != NO_ERROR) { 3279 return err; 3280 } 3281 } 3282 } else if (!table->hasBagOrEntry(key, &attr16, &package)) { 3283 3284#if 1 3285// fprintf(stderr, "ERROR: Bag attribute '%s' has not been defined.\n", 3286// String8(key).string()); 3287// const Item& item(mBag.valueAt(i)); 3288// fprintf(stderr, "Referenced from file %s line %d\n", 3289// item.sourcePos.file.string(), item.sourcePos.line); 3290// return UNKNOWN_ERROR; 3291#else 3292 char numberStr[16]; 3293 sprintf(numberStr, "%d", ResTable_map::TYPE_ANY); 3294 status_t err = table->addBag(SourcePos("<generated>", 0), package, 3295 attr16, key, String16(""), 3296 String16("^type"), 3297 String16(numberStr), NULL, NULL); 3298 if (err != NO_ERROR) { 3299 return err; 3300 } 3301#endif 3302 } 3303 } 3304 return NO_ERROR; 3305} 3306 3307status_t ResourceTable::Entry::assignResourceIds(ResourceTable* table, 3308 const String16& package) 3309{ 3310 bool hasErrors = false; 3311 3312 if (mType == TYPE_BAG) { 3313 const char* errorMsg; 3314 const String16 style16("style"); 3315 const String16 attr16("attr"); 3316 const String16 id16("id"); 3317 mParentId = 0; 3318 if (mParent.size() > 0) { 3319 mParentId = table->getResId(mParent, &style16, NULL, &errorMsg); 3320 if (mParentId == 0) { 3321 mPos.error("Error retrieving parent for item: %s '%s'.\n", 3322 errorMsg, String8(mParent).string()); 3323 hasErrors = true; 3324 } 3325 } 3326 const size_t N = mBag.size(); 3327 for (size_t i=0; i<N; i++) { 3328 const String16& key = mBag.keyAt(i); 3329 Item& it = mBag.editValueAt(i); 3330 it.bagKeyId = table->getResId(key, 3331 it.isId ? &id16 : &attr16, NULL, &errorMsg); 3332 //printf("Bag key of %s: #%08x\n", String8(key).string(), it.bagKeyId); 3333 if (it.bagKeyId == 0) { 3334 it.sourcePos.error("Error: %s: %s '%s'.\n", errorMsg, 3335 String8(it.isId ? id16 : attr16).string(), 3336 String8(key).string()); 3337 hasErrors = true; 3338 } 3339 } 3340 } 3341 return hasErrors ? UNKNOWN_ERROR : NO_ERROR; 3342} 3343 3344status_t ResourceTable::Entry::prepareFlatten(StringPool* strings, ResourceTable* table, 3345 const String8* configTypeName, const ConfigDescription* config) 3346{ 3347 if (mType == TYPE_ITEM) { 3348 Item& it = mItem; 3349 AccessorCookie ac(it.sourcePos, String8(mName), String8(it.value)); 3350 if (!table->stringToValue(&it.parsedValue, strings, 3351 it.value, false, true, 0, 3352 &it.style, NULL, &ac, mItemFormat, 3353 configTypeName, config)) { 3354 return UNKNOWN_ERROR; 3355 } 3356 } else if (mType == TYPE_BAG) { 3357 const size_t N = mBag.size(); 3358 for (size_t i=0; i<N; i++) { 3359 const String16& key = mBag.keyAt(i); 3360 Item& it = mBag.editValueAt(i); 3361 AccessorCookie ac(it.sourcePos, String8(key), String8(it.value)); 3362 if (!table->stringToValue(&it.parsedValue, strings, 3363 it.value, false, true, it.bagKeyId, 3364 &it.style, NULL, &ac, it.format, 3365 configTypeName, config)) { 3366 return UNKNOWN_ERROR; 3367 } 3368 } 3369 } else { 3370 mPos.error("Error: entry %s is not a single item or a bag.\n", 3371 String8(mName).string()); 3372 return UNKNOWN_ERROR; 3373 } 3374 return NO_ERROR; 3375} 3376 3377status_t ResourceTable::Entry::remapStringValue(StringPool* strings) 3378{ 3379 if (mType == TYPE_ITEM) { 3380 Item& it = mItem; 3381 if (it.parsedValue.dataType == Res_value::TYPE_STRING) { 3382 it.parsedValue.data = strings->mapOriginalPosToNewPos(it.parsedValue.data); 3383 } 3384 } else if (mType == TYPE_BAG) { 3385 const size_t N = mBag.size(); 3386 for (size_t i=0; i<N; i++) { 3387 Item& it = mBag.editValueAt(i); 3388 if (it.parsedValue.dataType == Res_value::TYPE_STRING) { 3389 it.parsedValue.data = strings->mapOriginalPosToNewPos(it.parsedValue.data); 3390 } 3391 } 3392 } else { 3393 mPos.error("Error: entry %s is not a single item or a bag.\n", 3394 String8(mName).string()); 3395 return UNKNOWN_ERROR; 3396 } 3397 return NO_ERROR; 3398} 3399 3400ssize_t ResourceTable::Entry::flatten(Bundle* bundle, const sp<AaptFile>& data, bool isPublic) 3401{ 3402 size_t amt = 0; 3403 ResTable_entry header; 3404 memset(&header, 0, sizeof(header)); 3405 header.size = htods(sizeof(header)); 3406 const type ty = this != NULL ? mType : TYPE_ITEM; 3407 if (this != NULL) { 3408 if (ty == TYPE_BAG) { 3409 header.flags |= htods(header.FLAG_COMPLEX); 3410 } 3411 if (isPublic) { 3412 header.flags |= htods(header.FLAG_PUBLIC); 3413 } 3414 header.key.index = htodl(mNameIndex); 3415 } 3416 if (ty != TYPE_BAG) { 3417 status_t err = data->writeData(&header, sizeof(header)); 3418 if (err != NO_ERROR) { 3419 fprintf(stderr, "ERROR: out of memory creating ResTable_entry\n"); 3420 return err; 3421 } 3422 3423 const Item& it = mItem; 3424 Res_value par; 3425 memset(&par, 0, sizeof(par)); 3426 par.size = htods(it.parsedValue.size); 3427 par.dataType = it.parsedValue.dataType; 3428 par.res0 = it.parsedValue.res0; 3429 par.data = htodl(it.parsedValue.data); 3430 #if 0 3431 printf("Writing item (%s): type=%d, data=0x%x, res0=0x%x\n", 3432 String8(mName).string(), it.parsedValue.dataType, 3433 it.parsedValue.data, par.res0); 3434 #endif 3435 err = data->writeData(&par, it.parsedValue.size); 3436 if (err != NO_ERROR) { 3437 fprintf(stderr, "ERROR: out of memory creating Res_value\n"); 3438 return err; 3439 } 3440 amt += it.parsedValue.size; 3441 } else { 3442 size_t N = mBag.size(); 3443 size_t i; 3444 // Create correct ordering of items. 3445 KeyedVector<uint32_t, const Item*> items; 3446 for (i=0; i<N; i++) { 3447 const Item& it = mBag.valueAt(i); 3448 items.add(it.bagKeyId, &it); 3449 } 3450 N = items.size(); 3451 3452 ResTable_map_entry mapHeader; 3453 memcpy(&mapHeader, &header, sizeof(header)); 3454 mapHeader.size = htods(sizeof(mapHeader)); 3455 mapHeader.parent.ident = htodl(mParentId); 3456 mapHeader.count = htodl(N); 3457 status_t err = data->writeData(&mapHeader, sizeof(mapHeader)); 3458 if (err != NO_ERROR) { 3459 fprintf(stderr, "ERROR: out of memory creating ResTable_entry\n"); 3460 return err; 3461 } 3462 3463 for (i=0; i<N; i++) { 3464 const Item& it = *items.valueAt(i); 3465 ResTable_map map; 3466 map.name.ident = htodl(it.bagKeyId); 3467 map.value.size = htods(it.parsedValue.size); 3468 map.value.dataType = it.parsedValue.dataType; 3469 map.value.res0 = it.parsedValue.res0; 3470 map.value.data = htodl(it.parsedValue.data); 3471 err = data->writeData(&map, sizeof(map)); 3472 if (err != NO_ERROR) { 3473 fprintf(stderr, "ERROR: out of memory creating Res_value\n"); 3474 return err; 3475 } 3476 amt += sizeof(map); 3477 } 3478 } 3479 return amt; 3480} 3481 3482void ResourceTable::ConfigList::appendComment(const String16& comment, 3483 bool onlyIfEmpty) 3484{ 3485 if (comment.size() <= 0) { 3486 return; 3487 } 3488 if (onlyIfEmpty && mComment.size() > 0) { 3489 return; 3490 } 3491 if (mComment.size() > 0) { 3492 mComment.append(String16("\n")); 3493 } 3494 mComment.append(comment); 3495} 3496 3497void ResourceTable::ConfigList::appendTypeComment(const String16& comment) 3498{ 3499 if (comment.size() <= 0) { 3500 return; 3501 } 3502 if (mTypeComment.size() > 0) { 3503 mTypeComment.append(String16("\n")); 3504 } 3505 mTypeComment.append(comment); 3506} 3507 3508status_t ResourceTable::Type::addPublic(const SourcePos& sourcePos, 3509 const String16& name, 3510 const uint32_t ident) 3511{ 3512 #if 0 3513 int32_t entryIdx = Res_GETENTRY(ident); 3514 if (entryIdx < 0) { 3515 sourcePos.error("Public resource %s/%s has an invalid 0 identifier (0x%08x).\n", 3516 String8(mName).string(), String8(name).string(), ident); 3517 return UNKNOWN_ERROR; 3518 } 3519 #endif 3520 3521 int32_t typeIdx = Res_GETTYPE(ident); 3522 if (typeIdx >= 0) { 3523 typeIdx++; 3524 if (mPublicIndex > 0 && mPublicIndex != typeIdx) { 3525 sourcePos.error("Public resource %s/%s has conflicting type codes for its" 3526 " public identifiers (0x%x vs 0x%x).\n", 3527 String8(mName).string(), String8(name).string(), 3528 mPublicIndex, typeIdx); 3529 return UNKNOWN_ERROR; 3530 } 3531 mPublicIndex = typeIdx; 3532 } 3533 3534 if (mFirstPublicSourcePos == NULL) { 3535 mFirstPublicSourcePos = new SourcePos(sourcePos); 3536 } 3537 3538 if (mPublic.indexOfKey(name) < 0) { 3539 mPublic.add(name, Public(sourcePos, String16(), ident)); 3540 } else { 3541 Public& p = mPublic.editValueFor(name); 3542 if (p.ident != ident) { 3543 sourcePos.error("Public resource %s/%s has conflicting public identifiers" 3544 " (0x%08x vs 0x%08x).\n" 3545 "%s:%d: Originally defined here.\n", 3546 String8(mName).string(), String8(name).string(), p.ident, ident, 3547 p.sourcePos.file.string(), p.sourcePos.line); 3548 return UNKNOWN_ERROR; 3549 } 3550 } 3551 3552 return NO_ERROR; 3553} 3554 3555void ResourceTable::Type::canAddEntry(const String16& name) 3556{ 3557 mCanAddEntries.add(name); 3558} 3559 3560sp<ResourceTable::Entry> ResourceTable::Type::getEntry(const String16& entry, 3561 const SourcePos& sourcePos, 3562 const ResTable_config* config, 3563 bool doSetIndex, 3564 bool overlay, 3565 bool autoAddOverlay) 3566{ 3567 int pos = -1; 3568 sp<ConfigList> c = mConfigs.valueFor(entry); 3569 if (c == NULL) { 3570 if (overlay && !autoAddOverlay && mCanAddEntries.indexOf(entry) < 0) { 3571 sourcePos.error("Resource at %s appears in overlay but not" 3572 " in the base package; use <add-resource> to add.\n", 3573 String8(entry).string()); 3574 return NULL; 3575 } 3576 c = new ConfigList(entry, sourcePos); 3577 mConfigs.add(entry, c); 3578 pos = (int)mOrderedConfigs.size(); 3579 mOrderedConfigs.add(c); 3580 if (doSetIndex) { 3581 c->setEntryIndex(pos); 3582 } 3583 } 3584 3585 ConfigDescription cdesc; 3586 if (config) cdesc = *config; 3587 3588 sp<Entry> e = c->getEntries().valueFor(cdesc); 3589 if (e == NULL) { 3590 if (config != NULL) { 3591 NOISY(printf("New entry at %s:%d: imsi:%d/%d lang:%c%c cnt:%c%c " 3592 "orien:%d touch:%d density:%d key:%d inp:%d nav:%d sz:%dx%d " 3593 "sw%ddp w%ddp h%ddp dir:%d\n", 3594 sourcePos.file.string(), sourcePos.line, 3595 config->mcc, config->mnc, 3596 config->language[0] ? config->language[0] : '-', 3597 config->language[1] ? config->language[1] : '-', 3598 config->country[0] ? config->country[0] : '-', 3599 config->country[1] ? config->country[1] : '-', 3600 config->orientation, 3601 config->touchscreen, 3602 config->density, 3603 config->keyboard, 3604 config->inputFlags, 3605 config->navigation, 3606 config->screenWidth, 3607 config->screenHeight, 3608 config->smallestScreenWidthDp, 3609 config->screenWidthDp, 3610 config->screenHeightDp, 3611 config->layoutDirection)); 3612 } else { 3613 NOISY(printf("New entry at %s:%d: NULL config\n", 3614 sourcePos.file.string(), sourcePos.line)); 3615 } 3616 e = new Entry(entry, sourcePos); 3617 c->addEntry(cdesc, e); 3618 /* 3619 if (doSetIndex) { 3620 if (pos < 0) { 3621 for (pos=0; pos<(int)mOrderedConfigs.size(); pos++) { 3622 if (mOrderedConfigs[pos] == c) { 3623 break; 3624 } 3625 } 3626 if (pos >= (int)mOrderedConfigs.size()) { 3627 sourcePos.error("Internal error: config not found in mOrderedConfigs when adding entry"); 3628 return NULL; 3629 } 3630 } 3631 e->setEntryIndex(pos); 3632 } 3633 */ 3634 } 3635 3636 mUniqueConfigs.add(cdesc); 3637 3638 return e; 3639} 3640 3641status_t ResourceTable::Type::applyPublicEntryOrder() 3642{ 3643 size_t N = mOrderedConfigs.size(); 3644 Vector<sp<ConfigList> > origOrder(mOrderedConfigs); 3645 bool hasError = false; 3646 3647 size_t i; 3648 for (i=0; i<N; i++) { 3649 mOrderedConfigs.replaceAt(NULL, i); 3650 } 3651 3652 const size_t NP = mPublic.size(); 3653 //printf("Ordering %d configs from %d public defs\n", N, NP); 3654 size_t j; 3655 for (j=0; j<NP; j++) { 3656 const String16& name = mPublic.keyAt(j); 3657 const Public& p = mPublic.valueAt(j); 3658 int32_t idx = Res_GETENTRY(p.ident); 3659 //printf("Looking for entry \"%s\"/\"%s\" (0x%08x) in %d...\n", 3660 // String8(mName).string(), String8(name).string(), p.ident, N); 3661 bool found = false; 3662 for (i=0; i<N; i++) { 3663 sp<ConfigList> e = origOrder.itemAt(i); 3664 //printf("#%d: \"%s\"\n", i, String8(e->getName()).string()); 3665 if (e->getName() == name) { 3666 if (idx >= (int32_t)mOrderedConfigs.size()) { 3667 p.sourcePos.error("Public entry identifier 0x%x entry index " 3668 "is larger than available symbols (index %d, total symbols %d).\n", 3669 p.ident, idx, mOrderedConfigs.size()); 3670 hasError = true; 3671 } else if (mOrderedConfigs.itemAt(idx) == NULL) { 3672 e->setPublic(true); 3673 e->setPublicSourcePos(p.sourcePos); 3674 mOrderedConfigs.replaceAt(e, idx); 3675 origOrder.removeAt(i); 3676 N--; 3677 found = true; 3678 break; 3679 } else { 3680 sp<ConfigList> oe = mOrderedConfigs.itemAt(idx); 3681 3682 p.sourcePos.error("Multiple entry names declared for public entry" 3683 " identifier 0x%x in type %s (%s vs %s).\n" 3684 "%s:%d: Originally defined here.", 3685 idx+1, String8(mName).string(), 3686 String8(oe->getName()).string(), 3687 String8(name).string(), 3688 oe->getPublicSourcePos().file.string(), 3689 oe->getPublicSourcePos().line); 3690 hasError = true; 3691 } 3692 } 3693 } 3694 3695 if (!found) { 3696 p.sourcePos.error("Public symbol %s/%s declared here is not defined.", 3697 String8(mName).string(), String8(name).string()); 3698 hasError = true; 3699 } 3700 } 3701 3702 //printf("Copying back in %d non-public configs, have %d\n", N, origOrder.size()); 3703 3704 if (N != origOrder.size()) { 3705 printf("Internal error: remaining private symbol count mismatch\n"); 3706 N = origOrder.size(); 3707 } 3708 3709 j = 0; 3710 for (i=0; i<N; i++) { 3711 sp<ConfigList> e = origOrder.itemAt(i); 3712 // There will always be enough room for the remaining entries. 3713 while (mOrderedConfigs.itemAt(j) != NULL) { 3714 j++; 3715 } 3716 mOrderedConfigs.replaceAt(e, j); 3717 j++; 3718 } 3719 3720 return hasError ? UNKNOWN_ERROR : NO_ERROR; 3721} 3722 3723ResourceTable::Package::Package(const String16& name, ssize_t includedId) 3724 : mName(name), mIncludedId(includedId), 3725 mTypeStringsMapping(0xffffffff), 3726 mKeyStringsMapping(0xffffffff) 3727{ 3728} 3729 3730sp<ResourceTable::Type> ResourceTable::Package::getType(const String16& type, 3731 const SourcePos& sourcePos, 3732 bool doSetIndex) 3733{ 3734 sp<Type> t = mTypes.valueFor(type); 3735 if (t == NULL) { 3736 t = new Type(type, sourcePos); 3737 mTypes.add(type, t); 3738 mOrderedTypes.add(t); 3739 if (doSetIndex) { 3740 // For some reason the type's index is set to one plus the index 3741 // in the mOrderedTypes list, rather than just the index. 3742 t->setIndex(mOrderedTypes.size()); 3743 } 3744 } 3745 return t; 3746} 3747 3748status_t ResourceTable::Package::setTypeStrings(const sp<AaptFile>& data) 3749{ 3750 mTypeStringsData = data; 3751 status_t err = setStrings(data, &mTypeStrings, &mTypeStringsMapping); 3752 if (err != NO_ERROR) { 3753 fprintf(stderr, "ERROR: Type string data is corrupt!\n"); 3754 } 3755 return err; 3756} 3757 3758status_t ResourceTable::Package::setKeyStrings(const sp<AaptFile>& data) 3759{ 3760 mKeyStringsData = data; 3761 status_t err = setStrings(data, &mKeyStrings, &mKeyStringsMapping); 3762 if (err != NO_ERROR) { 3763 fprintf(stderr, "ERROR: Key string data is corrupt!\n"); 3764 } 3765 return err; 3766} 3767 3768status_t ResourceTable::Package::setStrings(const sp<AaptFile>& data, 3769 ResStringPool* strings, 3770 DefaultKeyedVector<String16, uint32_t>* mappings) 3771{ 3772 if (data->getData() == NULL) { 3773 return UNKNOWN_ERROR; 3774 } 3775 3776 NOISY(aout << "Setting restable string pool: " 3777 << HexDump(data->getData(), data->getSize()) << endl); 3778 3779 status_t err = strings->setTo(data->getData(), data->getSize()); 3780 if (err == NO_ERROR) { 3781 const size_t N = strings->size(); 3782 for (size_t i=0; i<N; i++) { 3783 size_t len; 3784 mappings->add(String16(strings->stringAt(i, &len)), i); 3785 } 3786 } 3787 return err; 3788} 3789 3790status_t ResourceTable::Package::applyPublicTypeOrder() 3791{ 3792 size_t N = mOrderedTypes.size(); 3793 Vector<sp<Type> > origOrder(mOrderedTypes); 3794 3795 size_t i; 3796 for (i=0; i<N; i++) { 3797 mOrderedTypes.replaceAt(NULL, i); 3798 } 3799 3800 for (i=0; i<N; i++) { 3801 sp<Type> t = origOrder.itemAt(i); 3802 int32_t idx = t->getPublicIndex(); 3803 if (idx > 0) { 3804 idx--; 3805 while (idx >= (int32_t)mOrderedTypes.size()) { 3806 mOrderedTypes.add(); 3807 } 3808 if (mOrderedTypes.itemAt(idx) != NULL) { 3809 sp<Type> ot = mOrderedTypes.itemAt(idx); 3810 t->getFirstPublicSourcePos().error("Multiple type names declared for public type" 3811 " identifier 0x%x (%s vs %s).\n" 3812 "%s:%d: Originally defined here.", 3813 idx, String8(ot->getName()).string(), 3814 String8(t->getName()).string(), 3815 ot->getFirstPublicSourcePos().file.string(), 3816 ot->getFirstPublicSourcePos().line); 3817 return UNKNOWN_ERROR; 3818 } 3819 mOrderedTypes.replaceAt(t, idx); 3820 origOrder.removeAt(i); 3821 i--; 3822 N--; 3823 } 3824 } 3825 3826 size_t j=0; 3827 for (i=0; i<N; i++) { 3828 sp<Type> t = origOrder.itemAt(i); 3829 // There will always be enough room for the remaining types. 3830 while (mOrderedTypes.itemAt(j) != NULL) { 3831 j++; 3832 } 3833 mOrderedTypes.replaceAt(t, j); 3834 } 3835 3836 return NO_ERROR; 3837} 3838 3839sp<ResourceTable::Package> ResourceTable::getPackage(const String16& package) 3840{ 3841 sp<Package> p = mPackages.valueFor(package); 3842 if (p == NULL) { 3843 if (mIsAppPackage) { 3844 if (mHaveAppPackage) { 3845 fprintf(stderr, "Adding multiple application package resources; only one is allowed.\n" 3846 "Use -x to create extended resources.\n"); 3847 return NULL; 3848 } 3849 mHaveAppPackage = true; 3850 p = new Package(package, 127); 3851 } else { 3852 p = new Package(package, mNextPackageId); 3853 } 3854 //printf("*** NEW PACKAGE: \"%s\" id=%d\n", 3855 // String8(package).string(), p->getAssignedId()); 3856 mPackages.add(package, p); 3857 mOrderedPackages.add(p); 3858 mNextPackageId++; 3859 } 3860 return p; 3861} 3862 3863sp<ResourceTable::Type> ResourceTable::getType(const String16& package, 3864 const String16& type, 3865 const SourcePos& sourcePos, 3866 bool doSetIndex) 3867{ 3868 sp<Package> p = getPackage(package); 3869 if (p == NULL) { 3870 return NULL; 3871 } 3872 return p->getType(type, sourcePos, doSetIndex); 3873} 3874 3875sp<ResourceTable::Entry> ResourceTable::getEntry(const String16& package, 3876 const String16& type, 3877 const String16& name, 3878 const SourcePos& sourcePos, 3879 bool overlay, 3880 const ResTable_config* config, 3881 bool doSetIndex) 3882{ 3883 sp<Type> t = getType(package, type, sourcePos, doSetIndex); 3884 if (t == NULL) { 3885 return NULL; 3886 } 3887 return t->getEntry(name, sourcePos, config, doSetIndex, overlay, mBundle->getAutoAddOverlay()); 3888} 3889 3890sp<const ResourceTable::Entry> ResourceTable::getEntry(uint32_t resID, 3891 const ResTable_config* config) const 3892{ 3893 int pid = Res_GETPACKAGE(resID)+1; 3894 const size_t N = mOrderedPackages.size(); 3895 size_t i; 3896 sp<Package> p; 3897 for (i=0; i<N; i++) { 3898 sp<Package> check = mOrderedPackages[i]; 3899 if (check->getAssignedId() == pid) { 3900 p = check; 3901 break; 3902 } 3903 3904 } 3905 if (p == NULL) { 3906 fprintf(stderr, "warning: Package not found for resource #%08x\n", resID); 3907 return NULL; 3908 } 3909 3910 int tid = Res_GETTYPE(resID); 3911 if (tid < 0 || tid >= (int)p->getOrderedTypes().size()) { 3912 fprintf(stderr, "warning: Type not found for resource #%08x\n", resID); 3913 return NULL; 3914 } 3915 sp<Type> t = p->getOrderedTypes()[tid]; 3916 3917 int eid = Res_GETENTRY(resID); 3918 if (eid < 0 || eid >= (int)t->getOrderedConfigs().size()) { 3919 fprintf(stderr, "warning: Entry not found for resource #%08x\n", resID); 3920 return NULL; 3921 } 3922 3923 sp<ConfigList> c = t->getOrderedConfigs()[eid]; 3924 if (c == NULL) { 3925 fprintf(stderr, "warning: Entry not found for resource #%08x\n", resID); 3926 return NULL; 3927 } 3928 3929 ConfigDescription cdesc; 3930 if (config) cdesc = *config; 3931 sp<Entry> e = c->getEntries().valueFor(cdesc); 3932 if (c == NULL) { 3933 fprintf(stderr, "warning: Entry configuration not found for resource #%08x\n", resID); 3934 return NULL; 3935 } 3936 3937 return e; 3938} 3939 3940const ResourceTable::Item* ResourceTable::getItem(uint32_t resID, uint32_t attrID) const 3941{ 3942 sp<const Entry> e = getEntry(resID); 3943 if (e == NULL) { 3944 return NULL; 3945 } 3946 3947 const size_t N = e->getBag().size(); 3948 for (size_t i=0; i<N; i++) { 3949 const Item& it = e->getBag().valueAt(i); 3950 if (it.bagKeyId == 0) { 3951 fprintf(stderr, "warning: ID not yet assigned to '%s' in bag '%s'\n", 3952 String8(e->getName()).string(), 3953 String8(e->getBag().keyAt(i)).string()); 3954 } 3955 if (it.bagKeyId == attrID) { 3956 return ⁢ 3957 } 3958 } 3959 3960 return NULL; 3961} 3962 3963bool ResourceTable::getItemValue( 3964 uint32_t resID, uint32_t attrID, Res_value* outValue) 3965{ 3966 const Item* item = getItem(resID, attrID); 3967 3968 bool res = false; 3969 if (item != NULL) { 3970 if (item->evaluating) { 3971 sp<const Entry> e = getEntry(resID); 3972 const size_t N = e->getBag().size(); 3973 size_t i; 3974 for (i=0; i<N; i++) { 3975 if (&e->getBag().valueAt(i) == item) { 3976 break; 3977 } 3978 } 3979 fprintf(stderr, "warning: Circular reference detected in key '%s' of bag '%s'\n", 3980 String8(e->getName()).string(), 3981 String8(e->getBag().keyAt(i)).string()); 3982 return false; 3983 } 3984 item->evaluating = true; 3985 res = stringToValue(outValue, NULL, item->value, false, false, item->bagKeyId); 3986 NOISY( 3987 if (res) { 3988 printf("getItemValue of #%08x[#%08x] (%s): type=#%08x, data=#%08x\n", 3989 resID, attrID, String8(getEntry(resID)->getName()).string(), 3990 outValue->dataType, outValue->data); 3991 } else { 3992 printf("getItemValue of #%08x[#%08x]: failed\n", 3993 resID, attrID); 3994 } 3995 ); 3996 item->evaluating = false; 3997 } 3998 return res; 3999} 4000