ResourceTable.cpp revision 47843df11fe88b30abcac324173b6c15f16cc797
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 PseudolocalizationMethod 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 PseudolocalizationMethod 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] = 'e'; 858 pseudoParams.language[1] = 'n'; 859 pseudoParams.country[0] = 'X'; 860 pseudoParams.country[1] = 'A'; 861 862 ResTable_config pseudoBidiParams(curParams); 863 pseudoBidiParams.language[0] = 'a'; 864 pseudoBidiParams.language[1] = 'r'; 865 pseudoBidiParams.country[0] = 'X'; 866 pseudoBidiParams.country[1] = 'B'; 867 868 // We should skip resources for pseudolocales if they were 869 // already added automatically. This is a fix for a transition period when 870 // manually pseudolocalized resources may be expected. 871 // TODO: remove this check after next SDK version release. 872 if ((bundle->getPseudolocalize() & PSEUDO_ACCENTED && 873 curParams.locale == pseudoParams.locale) || 874 (bundle->getPseudolocalize() & PSEUDO_BIDI && 875 curParams.locale == pseudoBidiParams.locale)) { 876 SourcePos(in->getPrintableSource(), 0).warning( 877 "Resource file %s is skipped as pseudolocalization" 878 " was done automatically.", 879 in->getPrintableSource().string()); 880 return NO_ERROR; 881 } 882 883 while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) { 884 if (code == ResXMLTree::START_TAG) { 885 const String16* curTag = NULL; 886 String16 curType; 887 int32_t curFormat = ResTable_map::TYPE_ANY; 888 bool curIsBag = false; 889 bool curIsBagReplaceOnOverwrite = false; 890 bool curIsStyled = false; 891 bool curIsPseudolocalizable = false; 892 bool curIsFormatted = fileIsTranslatable; 893 bool localHasErrors = false; 894 895 if (strcmp16(block.getElementName(&len), skip16.string()) == 0) { 896 while ((code=block.next()) != ResXMLTree::END_DOCUMENT 897 && code != ResXMLTree::BAD_DOCUMENT) { 898 if (code == ResXMLTree::END_TAG) { 899 if (strcmp16(block.getElementName(&len), skip16.string()) == 0) { 900 break; 901 } 902 } 903 } 904 continue; 905 906 } else if (strcmp16(block.getElementName(&len), eat_comment16.string()) == 0) { 907 while ((code=block.next()) != ResXMLTree::END_DOCUMENT 908 && code != ResXMLTree::BAD_DOCUMENT) { 909 if (code == ResXMLTree::END_TAG) { 910 if (strcmp16(block.getElementName(&len), eat_comment16.string()) == 0) { 911 break; 912 } 913 } 914 } 915 continue; 916 917 } else if (strcmp16(block.getElementName(&len), public16.string()) == 0) { 918 SourcePos srcPos(in->getPrintableSource(), block.getLineNumber()); 919 920 String16 type; 921 ssize_t typeIdx = block.indexOfAttribute(NULL, "type"); 922 if (typeIdx < 0) { 923 srcPos.error("A 'type' attribute is required for <public>\n"); 924 hasErrors = localHasErrors = true; 925 } 926 type = String16(block.getAttributeStringValue(typeIdx, &len)); 927 928 String16 name; 929 ssize_t nameIdx = block.indexOfAttribute(NULL, "name"); 930 if (nameIdx < 0) { 931 srcPos.error("A 'name' attribute is required for <public>\n"); 932 hasErrors = localHasErrors = true; 933 } 934 name = String16(block.getAttributeStringValue(nameIdx, &len)); 935 936 uint32_t ident = 0; 937 ssize_t identIdx = block.indexOfAttribute(NULL, "id"); 938 if (identIdx >= 0) { 939 const char16_t* identStr = block.getAttributeStringValue(identIdx, &len); 940 Res_value identValue; 941 if (!ResTable::stringToInt(identStr, len, &identValue)) { 942 srcPos.error("Given 'id' attribute is not an integer: %s\n", 943 String8(block.getAttributeStringValue(identIdx, &len)).string()); 944 hasErrors = localHasErrors = true; 945 } else { 946 ident = identValue.data; 947 nextPublicId.replaceValueFor(type, ident+1); 948 } 949 } else if (nextPublicId.indexOfKey(type) < 0) { 950 srcPos.error("No 'id' attribute supplied <public>," 951 " and no previous id defined in this file.\n"); 952 hasErrors = localHasErrors = true; 953 } else if (!localHasErrors) { 954 ident = nextPublicId.valueFor(type); 955 nextPublicId.replaceValueFor(type, ident+1); 956 } 957 958 if (!localHasErrors) { 959 err = outTable->addPublic(srcPos, myPackage, type, name, ident); 960 if (err < NO_ERROR) { 961 hasErrors = localHasErrors = true; 962 } 963 } 964 if (!localHasErrors) { 965 sp<AaptSymbols> symbols = assets->getSymbolsFor(String8("R")); 966 if (symbols != NULL) { 967 symbols = symbols->addNestedSymbol(String8(type), srcPos); 968 } 969 if (symbols != NULL) { 970 symbols->makeSymbolPublic(String8(name), srcPos); 971 String16 comment( 972 block.getComment(&len) ? block.getComment(&len) : nulStr); 973 symbols->appendComment(String8(name), comment, srcPos); 974 } else { 975 srcPos.error("Unable to create symbols!\n"); 976 hasErrors = localHasErrors = true; 977 } 978 } 979 980 while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) { 981 if (code == ResXMLTree::END_TAG) { 982 if (strcmp16(block.getElementName(&len), public16.string()) == 0) { 983 break; 984 } 985 } 986 } 987 continue; 988 989 } else if (strcmp16(block.getElementName(&len), public_padding16.string()) == 0) { 990 SourcePos srcPos(in->getPrintableSource(), block.getLineNumber()); 991 992 String16 type; 993 ssize_t typeIdx = block.indexOfAttribute(NULL, "type"); 994 if (typeIdx < 0) { 995 srcPos.error("A 'type' attribute is required for <public-padding>\n"); 996 hasErrors = localHasErrors = true; 997 } 998 type = String16(block.getAttributeStringValue(typeIdx, &len)); 999 1000 String16 name; 1001 ssize_t nameIdx = block.indexOfAttribute(NULL, "name"); 1002 if (nameIdx < 0) { 1003 srcPos.error("A 'name' attribute is required for <public-padding>\n"); 1004 hasErrors = localHasErrors = true; 1005 } 1006 name = String16(block.getAttributeStringValue(nameIdx, &len)); 1007 1008 uint32_t start = 0; 1009 ssize_t startIdx = block.indexOfAttribute(NULL, "start"); 1010 if (startIdx >= 0) { 1011 const char16_t* startStr = block.getAttributeStringValue(startIdx, &len); 1012 Res_value startValue; 1013 if (!ResTable::stringToInt(startStr, len, &startValue)) { 1014 srcPos.error("Given 'start' attribute is not an integer: %s\n", 1015 String8(block.getAttributeStringValue(startIdx, &len)).string()); 1016 hasErrors = localHasErrors = true; 1017 } else { 1018 start = startValue.data; 1019 } 1020 } else if (nextPublicId.indexOfKey(type) < 0) { 1021 srcPos.error("No 'start' attribute supplied <public-padding>," 1022 " and no previous id defined in this file.\n"); 1023 hasErrors = localHasErrors = true; 1024 } else if (!localHasErrors) { 1025 start = nextPublicId.valueFor(type); 1026 } 1027 1028 uint32_t end = 0; 1029 ssize_t endIdx = block.indexOfAttribute(NULL, "end"); 1030 if (endIdx >= 0) { 1031 const char16_t* endStr = block.getAttributeStringValue(endIdx, &len); 1032 Res_value endValue; 1033 if (!ResTable::stringToInt(endStr, len, &endValue)) { 1034 srcPos.error("Given 'end' attribute is not an integer: %s\n", 1035 String8(block.getAttributeStringValue(endIdx, &len)).string()); 1036 hasErrors = localHasErrors = true; 1037 } else { 1038 end = endValue.data; 1039 } 1040 } else { 1041 srcPos.error("No 'end' attribute supplied <public-padding>\n"); 1042 hasErrors = localHasErrors = true; 1043 } 1044 1045 if (end >= start) { 1046 nextPublicId.replaceValueFor(type, end+1); 1047 } else { 1048 srcPos.error("Padding start '%ul' is after end '%ul'\n", 1049 start, end); 1050 hasErrors = localHasErrors = true; 1051 } 1052 1053 String16 comment( 1054 block.getComment(&len) ? block.getComment(&len) : nulStr); 1055 for (uint32_t curIdent=start; curIdent<=end; curIdent++) { 1056 if (localHasErrors) { 1057 break; 1058 } 1059 String16 curName(name); 1060 char buf[64]; 1061 sprintf(buf, "%d", (int)(end-curIdent+1)); 1062 curName.append(String16(buf)); 1063 1064 err = outTable->addEntry(srcPos, myPackage, type, curName, 1065 String16("padding"), NULL, &curParams, false, 1066 ResTable_map::TYPE_STRING, overwrite); 1067 if (err < NO_ERROR) { 1068 hasErrors = localHasErrors = true; 1069 break; 1070 } 1071 err = outTable->addPublic(srcPos, myPackage, type, 1072 curName, curIdent); 1073 if (err < NO_ERROR) { 1074 hasErrors = localHasErrors = true; 1075 break; 1076 } 1077 sp<AaptSymbols> symbols = assets->getSymbolsFor(String8("R")); 1078 if (symbols != NULL) { 1079 symbols = symbols->addNestedSymbol(String8(type), srcPos); 1080 } 1081 if (symbols != NULL) { 1082 symbols->makeSymbolPublic(String8(curName), srcPos); 1083 symbols->appendComment(String8(curName), comment, srcPos); 1084 } else { 1085 srcPos.error("Unable to create symbols!\n"); 1086 hasErrors = localHasErrors = true; 1087 } 1088 } 1089 1090 while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) { 1091 if (code == ResXMLTree::END_TAG) { 1092 if (strcmp16(block.getElementName(&len), public_padding16.string()) == 0) { 1093 break; 1094 } 1095 } 1096 } 1097 continue; 1098 1099 } else if (strcmp16(block.getElementName(&len), private_symbols16.string()) == 0) { 1100 String16 pkg; 1101 ssize_t pkgIdx = block.indexOfAttribute(NULL, "package"); 1102 if (pkgIdx < 0) { 1103 SourcePos(in->getPrintableSource(), block.getLineNumber()).error( 1104 "A 'package' attribute is required for <private-symbols>\n"); 1105 hasErrors = localHasErrors = true; 1106 } 1107 pkg = String16(block.getAttributeStringValue(pkgIdx, &len)); 1108 if (!localHasErrors) { 1109 assets->setSymbolsPrivatePackage(String8(pkg)); 1110 } 1111 1112 while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) { 1113 if (code == ResXMLTree::END_TAG) { 1114 if (strcmp16(block.getElementName(&len), private_symbols16.string()) == 0) { 1115 break; 1116 } 1117 } 1118 } 1119 continue; 1120 1121 } else if (strcmp16(block.getElementName(&len), java_symbol16.string()) == 0) { 1122 SourcePos srcPos(in->getPrintableSource(), block.getLineNumber()); 1123 1124 String16 type; 1125 ssize_t typeIdx = block.indexOfAttribute(NULL, "type"); 1126 if (typeIdx < 0) { 1127 srcPos.error("A 'type' attribute is required for <public>\n"); 1128 hasErrors = localHasErrors = true; 1129 } 1130 type = String16(block.getAttributeStringValue(typeIdx, &len)); 1131 1132 String16 name; 1133 ssize_t nameIdx = block.indexOfAttribute(NULL, "name"); 1134 if (nameIdx < 0) { 1135 srcPos.error("A 'name' attribute is required for <public>\n"); 1136 hasErrors = localHasErrors = true; 1137 } 1138 name = String16(block.getAttributeStringValue(nameIdx, &len)); 1139 1140 sp<AaptSymbols> symbols = assets->getJavaSymbolsFor(String8("R")); 1141 if (symbols != NULL) { 1142 symbols = symbols->addNestedSymbol(String8(type), srcPos); 1143 } 1144 if (symbols != NULL) { 1145 symbols->makeSymbolJavaSymbol(String8(name), srcPos); 1146 String16 comment( 1147 block.getComment(&len) ? block.getComment(&len) : nulStr); 1148 symbols->appendComment(String8(name), comment, srcPos); 1149 } else { 1150 srcPos.error("Unable to create symbols!\n"); 1151 hasErrors = localHasErrors = true; 1152 } 1153 1154 while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) { 1155 if (code == ResXMLTree::END_TAG) { 1156 if (strcmp16(block.getElementName(&len), java_symbol16.string()) == 0) { 1157 break; 1158 } 1159 } 1160 } 1161 continue; 1162 1163 1164 } else if (strcmp16(block.getElementName(&len), add_resource16.string()) == 0) { 1165 SourcePos srcPos(in->getPrintableSource(), block.getLineNumber()); 1166 1167 String16 typeName; 1168 ssize_t typeIdx = block.indexOfAttribute(NULL, "type"); 1169 if (typeIdx < 0) { 1170 srcPos.error("A 'type' attribute is required for <add-resource>\n"); 1171 hasErrors = localHasErrors = true; 1172 } 1173 typeName = String16(block.getAttributeStringValue(typeIdx, &len)); 1174 1175 String16 name; 1176 ssize_t nameIdx = block.indexOfAttribute(NULL, "name"); 1177 if (nameIdx < 0) { 1178 srcPos.error("A 'name' attribute is required for <add-resource>\n"); 1179 hasErrors = localHasErrors = true; 1180 } 1181 name = String16(block.getAttributeStringValue(nameIdx, &len)); 1182 1183 outTable->canAddEntry(srcPos, myPackage, typeName, name); 1184 1185 while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) { 1186 if (code == ResXMLTree::END_TAG) { 1187 if (strcmp16(block.getElementName(&len), add_resource16.string()) == 0) { 1188 break; 1189 } 1190 } 1191 } 1192 continue; 1193 1194 } else if (strcmp16(block.getElementName(&len), declare_styleable16.string()) == 0) { 1195 SourcePos srcPos(in->getPrintableSource(), block.getLineNumber()); 1196 1197 String16 ident; 1198 ssize_t identIdx = block.indexOfAttribute(NULL, "name"); 1199 if (identIdx < 0) { 1200 srcPos.error("A 'name' attribute is required for <declare-styleable>\n"); 1201 hasErrors = localHasErrors = true; 1202 } 1203 ident = String16(block.getAttributeStringValue(identIdx, &len)); 1204 1205 sp<AaptSymbols> symbols = assets->getSymbolsFor(String8("R")); 1206 if (!localHasErrors) { 1207 if (symbols != NULL) { 1208 symbols = symbols->addNestedSymbol(String8("styleable"), srcPos); 1209 } 1210 sp<AaptSymbols> styleSymbols = symbols; 1211 if (symbols != NULL) { 1212 symbols = symbols->addNestedSymbol(String8(ident), srcPos); 1213 } 1214 if (symbols == NULL) { 1215 srcPos.error("Unable to create symbols!\n"); 1216 return UNKNOWN_ERROR; 1217 } 1218 1219 String16 comment( 1220 block.getComment(&len) ? block.getComment(&len) : nulStr); 1221 styleSymbols->appendComment(String8(ident), comment, srcPos); 1222 } else { 1223 symbols = NULL; 1224 } 1225 1226 while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) { 1227 if (code == ResXMLTree::START_TAG) { 1228 if (strcmp16(block.getElementName(&len), skip16.string()) == 0) { 1229 while ((code=block.next()) != ResXMLTree::END_DOCUMENT 1230 && code != ResXMLTree::BAD_DOCUMENT) { 1231 if (code == ResXMLTree::END_TAG) { 1232 if (strcmp16(block.getElementName(&len), skip16.string()) == 0) { 1233 break; 1234 } 1235 } 1236 } 1237 continue; 1238 } else if (strcmp16(block.getElementName(&len), eat_comment16.string()) == 0) { 1239 while ((code=block.next()) != ResXMLTree::END_DOCUMENT 1240 && code != ResXMLTree::BAD_DOCUMENT) { 1241 if (code == ResXMLTree::END_TAG) { 1242 if (strcmp16(block.getElementName(&len), eat_comment16.string()) == 0) { 1243 break; 1244 } 1245 } 1246 } 1247 continue; 1248 } else if (strcmp16(block.getElementName(&len), attr16.string()) != 0) { 1249 SourcePos(in->getPrintableSource(), block.getLineNumber()).error( 1250 "Tag <%s> can not appear inside <declare-styleable>, only <attr>\n", 1251 String8(block.getElementName(&len)).string()); 1252 return UNKNOWN_ERROR; 1253 } 1254 1255 String16 comment( 1256 block.getComment(&len) ? block.getComment(&len) : nulStr); 1257 String16 itemIdent; 1258 err = compileAttribute(in, block, myPackage, outTable, &itemIdent, true); 1259 if (err != NO_ERROR) { 1260 hasErrors = localHasErrors = true; 1261 } 1262 1263 if (symbols != NULL) { 1264 SourcePos srcPos(String8(in->getPrintableSource()), block.getLineNumber()); 1265 symbols->addSymbol(String8(itemIdent), 0, srcPos); 1266 symbols->appendComment(String8(itemIdent), comment, srcPos); 1267 //printf("Attribute %s comment: %s\n", String8(itemIdent).string(), 1268 // String8(comment).string()); 1269 } 1270 } else if (code == ResXMLTree::END_TAG) { 1271 if (strcmp16(block.getElementName(&len), declare_styleable16.string()) == 0) { 1272 break; 1273 } 1274 1275 SourcePos(in->getPrintableSource(), block.getLineNumber()).error( 1276 "Found tag </%s> where </attr> is expected\n", 1277 String8(block.getElementName(&len)).string()); 1278 return UNKNOWN_ERROR; 1279 } 1280 } 1281 continue; 1282 1283 } else if (strcmp16(block.getElementName(&len), attr16.string()) == 0) { 1284 err = compileAttribute(in, block, myPackage, outTable, NULL); 1285 if (err != NO_ERROR) { 1286 hasErrors = true; 1287 } 1288 continue; 1289 1290 } else if (strcmp16(block.getElementName(&len), item16.string()) == 0) { 1291 curTag = &item16; 1292 ssize_t attri = block.indexOfAttribute(NULL, "type"); 1293 if (attri >= 0) { 1294 curType = String16(block.getAttributeStringValue(attri, &len)); 1295 ssize_t formatIdx = block.indexOfAttribute(NULL, "format"); 1296 if (formatIdx >= 0) { 1297 String16 formatStr = String16(block.getAttributeStringValue( 1298 formatIdx, &len)); 1299 curFormat = parse_flags(formatStr.string(), formatStr.size(), 1300 gFormatFlags); 1301 if (curFormat == 0) { 1302 SourcePos(in->getPrintableSource(), block.getLineNumber()).error( 1303 "Tag <item> 'format' attribute value \"%s\" not valid\n", 1304 String8(formatStr).string()); 1305 hasErrors = localHasErrors = true; 1306 } 1307 } 1308 } else { 1309 SourcePos(in->getPrintableSource(), block.getLineNumber()).error( 1310 "A 'type' attribute is required for <item>\n"); 1311 hasErrors = localHasErrors = true; 1312 } 1313 curIsStyled = true; 1314 } else if (strcmp16(block.getElementName(&len), string16.string()) == 0) { 1315 // Note the existence and locale of every string we process 1316 char rawLocale[RESTABLE_MAX_LOCALE_LEN]; 1317 curParams.getBcp47Locale(rawLocale); 1318 String8 locale(rawLocale); 1319 String16 name; 1320 String16 translatable; 1321 String16 formatted; 1322 1323 size_t n = block.getAttributeCount(); 1324 for (size_t i = 0; i < n; i++) { 1325 size_t length; 1326 const uint16_t* attr = block.getAttributeName(i, &length); 1327 if (strcmp16(attr, name16.string()) == 0) { 1328 name.setTo(block.getAttributeStringValue(i, &length)); 1329 } else if (strcmp16(attr, translatable16.string()) == 0) { 1330 translatable.setTo(block.getAttributeStringValue(i, &length)); 1331 } else if (strcmp16(attr, formatted16.string()) == 0) { 1332 formatted.setTo(block.getAttributeStringValue(i, &length)); 1333 } 1334 } 1335 1336 if (name.size() > 0) { 1337 if (translatable == false16) { 1338 curIsFormatted = false; 1339 // Untranslatable strings must only exist in the default [empty] locale 1340 if (locale.size() > 0) { 1341 SourcePos(in->getPrintableSource(), block.getLineNumber()).warning( 1342 "string '%s' marked untranslatable but exists in locale '%s'\n", 1343 String8(name).string(), 1344 locale.string()); 1345 // hasErrors = localHasErrors = true; 1346 } else { 1347 // Intentionally empty block: 1348 // 1349 // Don't add untranslatable strings to the localization table; that 1350 // way if we later see localizations of them, they'll be flagged as 1351 // having no default translation. 1352 } 1353 } else { 1354 outTable->addLocalization( 1355 name, 1356 locale, 1357 SourcePos(in->getPrintableSource(), block.getLineNumber())); 1358 } 1359 1360 if (formatted == false16) { 1361 curIsFormatted = false; 1362 } 1363 } 1364 1365 curTag = &string16; 1366 curType = string16; 1367 curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_STRING; 1368 curIsStyled = true; 1369 curIsPseudolocalizable = fileIsTranslatable && (translatable != false16); 1370 } else if (strcmp16(block.getElementName(&len), drawable16.string()) == 0) { 1371 curTag = &drawable16; 1372 curType = drawable16; 1373 curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_COLOR; 1374 } else if (strcmp16(block.getElementName(&len), color16.string()) == 0) { 1375 curTag = &color16; 1376 curType = color16; 1377 curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_COLOR; 1378 } else if (strcmp16(block.getElementName(&len), bool16.string()) == 0) { 1379 curTag = &bool16; 1380 curType = bool16; 1381 curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_BOOLEAN; 1382 } else if (strcmp16(block.getElementName(&len), integer16.string()) == 0) { 1383 curTag = &integer16; 1384 curType = integer16; 1385 curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_INTEGER; 1386 } else if (strcmp16(block.getElementName(&len), dimen16.string()) == 0) { 1387 curTag = &dimen16; 1388 curType = dimen16; 1389 curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_DIMENSION; 1390 } else if (strcmp16(block.getElementName(&len), fraction16.string()) == 0) { 1391 curTag = &fraction16; 1392 curType = fraction16; 1393 curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_FRACTION; 1394 } else if (strcmp16(block.getElementName(&len), bag16.string()) == 0) { 1395 curTag = &bag16; 1396 curIsBag = true; 1397 ssize_t attri = block.indexOfAttribute(NULL, "type"); 1398 if (attri >= 0) { 1399 curType = String16(block.getAttributeStringValue(attri, &len)); 1400 } else { 1401 SourcePos(in->getPrintableSource(), block.getLineNumber()).error( 1402 "A 'type' attribute is required for <bag>\n"); 1403 hasErrors = localHasErrors = true; 1404 } 1405 } else if (strcmp16(block.getElementName(&len), style16.string()) == 0) { 1406 curTag = &style16; 1407 curType = style16; 1408 curIsBag = true; 1409 } else if (strcmp16(block.getElementName(&len), plurals16.string()) == 0) { 1410 curTag = &plurals16; 1411 curType = plurals16; 1412 curIsBag = true; 1413 curIsPseudolocalizable = fileIsTranslatable; 1414 } else if (strcmp16(block.getElementName(&len), array16.string()) == 0) { 1415 curTag = &array16; 1416 curType = array16; 1417 curIsBag = true; 1418 curIsBagReplaceOnOverwrite = true; 1419 ssize_t formatIdx = block.indexOfAttribute(NULL, "format"); 1420 if (formatIdx >= 0) { 1421 String16 formatStr = String16(block.getAttributeStringValue( 1422 formatIdx, &len)); 1423 curFormat = parse_flags(formatStr.string(), formatStr.size(), 1424 gFormatFlags); 1425 if (curFormat == 0) { 1426 SourcePos(in->getPrintableSource(), block.getLineNumber()).error( 1427 "Tag <array> 'format' attribute value \"%s\" not valid\n", 1428 String8(formatStr).string()); 1429 hasErrors = localHasErrors = true; 1430 } 1431 } 1432 } else if (strcmp16(block.getElementName(&len), string_array16.string()) == 0) { 1433 // Check whether these strings need valid formats. 1434 // (simplified form of what string16 does above) 1435 bool isTranslatable = false; 1436 size_t n = block.getAttributeCount(); 1437 1438 // Pseudolocalizable by default, unless this string array isn't 1439 // translatable. 1440 for (size_t i = 0; i < n; i++) { 1441 size_t length; 1442 const uint16_t* attr = block.getAttributeName(i, &length); 1443 if (strcmp16(attr, formatted16.string()) == 0) { 1444 const uint16_t* value = block.getAttributeStringValue(i, &length); 1445 if (strcmp16(value, false16.string()) == 0) { 1446 curIsFormatted = false; 1447 } 1448 } else if (strcmp16(attr, translatable16.string()) == 0) { 1449 const uint16_t* value = block.getAttributeStringValue(i, &length); 1450 if (strcmp16(value, false16.string()) == 0) { 1451 isTranslatable = false; 1452 } 1453 } 1454 } 1455 1456 curTag = &string_array16; 1457 curType = array16; 1458 curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_STRING; 1459 curIsBag = true; 1460 curIsBagReplaceOnOverwrite = true; 1461 curIsPseudolocalizable = isTranslatable && fileIsTranslatable; 1462 } else if (strcmp16(block.getElementName(&len), integer_array16.string()) == 0) { 1463 curTag = &integer_array16; 1464 curType = array16; 1465 curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_INTEGER; 1466 curIsBag = true; 1467 curIsBagReplaceOnOverwrite = true; 1468 } else { 1469 SourcePos(in->getPrintableSource(), block.getLineNumber()).error( 1470 "Found tag %s where item is expected\n", 1471 String8(block.getElementName(&len)).string()); 1472 return UNKNOWN_ERROR; 1473 } 1474 1475 String16 ident; 1476 ssize_t identIdx = block.indexOfAttribute(NULL, "name"); 1477 if (identIdx >= 0) { 1478 ident = String16(block.getAttributeStringValue(identIdx, &len)); 1479 } else { 1480 SourcePos(in->getPrintableSource(), block.getLineNumber()).error( 1481 "A 'name' attribute is required for <%s>\n", 1482 String8(*curTag).string()); 1483 hasErrors = localHasErrors = true; 1484 } 1485 1486 String16 product; 1487 identIdx = block.indexOfAttribute(NULL, "product"); 1488 if (identIdx >= 0) { 1489 product = String16(block.getAttributeStringValue(identIdx, &len)); 1490 } 1491 1492 String16 comment(block.getComment(&len) ? block.getComment(&len) : nulStr); 1493 1494 if (curIsBag) { 1495 // Figure out the parent of this bag... 1496 String16 parentIdent; 1497 ssize_t parentIdentIdx = block.indexOfAttribute(NULL, "parent"); 1498 if (parentIdentIdx >= 0) { 1499 parentIdent = String16(block.getAttributeStringValue(parentIdentIdx, &len)); 1500 } else { 1501 ssize_t sep = ident.findLast('.'); 1502 if (sep >= 0) { 1503 parentIdent.setTo(ident, sep); 1504 } 1505 } 1506 1507 if (!localHasErrors) { 1508 err = outTable->startBag(SourcePos(in->getPrintableSource(), 1509 block.getLineNumber()), myPackage, curType, ident, 1510 parentIdent, &curParams, 1511 overwrite, curIsBagReplaceOnOverwrite); 1512 if (err != NO_ERROR) { 1513 hasErrors = localHasErrors = true; 1514 } 1515 } 1516 1517 ssize_t elmIndex = 0; 1518 char elmIndexStr[14]; 1519 while ((code=block.next()) != ResXMLTree::END_DOCUMENT 1520 && code != ResXMLTree::BAD_DOCUMENT) { 1521 1522 if (code == ResXMLTree::START_TAG) { 1523 if (strcmp16(block.getElementName(&len), item16.string()) != 0) { 1524 SourcePos(in->getPrintableSource(), block.getLineNumber()).error( 1525 "Tag <%s> can not appear inside <%s>, only <item>\n", 1526 String8(block.getElementName(&len)).string(), 1527 String8(*curTag).string()); 1528 return UNKNOWN_ERROR; 1529 } 1530 1531 String16 itemIdent; 1532 if (curType == array16) { 1533 sprintf(elmIndexStr, "^index_%d", (int)elmIndex++); 1534 itemIdent = String16(elmIndexStr); 1535 } else if (curType == plurals16) { 1536 ssize_t itemIdentIdx = block.indexOfAttribute(NULL, "quantity"); 1537 if (itemIdentIdx >= 0) { 1538 String16 quantity16(block.getAttributeStringValue(itemIdentIdx, &len)); 1539 if (quantity16 == other16) { 1540 itemIdent = quantityOther16; 1541 } 1542 else if (quantity16 == zero16) { 1543 itemIdent = quantityZero16; 1544 } 1545 else if (quantity16 == one16) { 1546 itemIdent = quantityOne16; 1547 } 1548 else if (quantity16 == two16) { 1549 itemIdent = quantityTwo16; 1550 } 1551 else if (quantity16 == few16) { 1552 itemIdent = quantityFew16; 1553 } 1554 else if (quantity16 == many16) { 1555 itemIdent = quantityMany16; 1556 } 1557 else { 1558 SourcePos(in->getPrintableSource(), block.getLineNumber()).error( 1559 "Illegal 'quantity' attribute is <item> inside <plurals>\n"); 1560 hasErrors = localHasErrors = true; 1561 } 1562 } else { 1563 SourcePos(in->getPrintableSource(), block.getLineNumber()).error( 1564 "A 'quantity' attribute is required for <item> inside <plurals>\n"); 1565 hasErrors = localHasErrors = true; 1566 } 1567 } else { 1568 ssize_t itemIdentIdx = block.indexOfAttribute(NULL, "name"); 1569 if (itemIdentIdx >= 0) { 1570 itemIdent = String16(block.getAttributeStringValue(itemIdentIdx, &len)); 1571 } else { 1572 SourcePos(in->getPrintableSource(), block.getLineNumber()).error( 1573 "A 'name' attribute is required for <item>\n"); 1574 hasErrors = localHasErrors = true; 1575 } 1576 } 1577 1578 ResXMLParser::ResXMLPosition parserPosition; 1579 block.getPosition(&parserPosition); 1580 1581 err = parseAndAddBag(bundle, in, &block, curParams, myPackage, curType, 1582 ident, parentIdent, itemIdent, curFormat, curIsFormatted, 1583 product, NO_PSEUDOLOCALIZATION, overwrite, outTable); 1584 if (err == NO_ERROR) { 1585 if (curIsPseudolocalizable && localeIsDefined(curParams) 1586 && bundle->getPseudolocalize() > 0) { 1587 // pseudolocalize here 1588 if ((PSEUDO_ACCENTED & bundle->getPseudolocalize()) == 1589 PSEUDO_ACCENTED) { 1590 block.setPosition(parserPosition); 1591 err = parseAndAddBag(bundle, in, &block, pseudoParams, myPackage, 1592 curType, ident, parentIdent, itemIdent, curFormat, 1593 curIsFormatted, product, PSEUDO_ACCENTED, 1594 overwrite, outTable); 1595 } 1596 if ((PSEUDO_BIDI & bundle->getPseudolocalize()) == 1597 PSEUDO_BIDI) { 1598 block.setPosition(parserPosition); 1599 err = parseAndAddBag(bundle, in, &block, pseudoBidiParams, myPackage, 1600 curType, ident, parentIdent, itemIdent, curFormat, 1601 curIsFormatted, product, PSEUDO_BIDI, 1602 overwrite, outTable); 1603 } 1604 } 1605 } 1606 if (err != NO_ERROR) { 1607 hasErrors = localHasErrors = true; 1608 } 1609 } else if (code == ResXMLTree::END_TAG) { 1610 if (strcmp16(block.getElementName(&len), curTag->string()) != 0) { 1611 SourcePos(in->getPrintableSource(), block.getLineNumber()).error( 1612 "Found tag </%s> where </%s> is expected\n", 1613 String8(block.getElementName(&len)).string(), 1614 String8(*curTag).string()); 1615 return UNKNOWN_ERROR; 1616 } 1617 break; 1618 } 1619 } 1620 } else { 1621 ResXMLParser::ResXMLPosition parserPosition; 1622 block.getPosition(&parserPosition); 1623 1624 err = parseAndAddEntry(bundle, in, &block, curParams, myPackage, curType, ident, 1625 *curTag, curIsStyled, curFormat, curIsFormatted, 1626 product, NO_PSEUDOLOCALIZATION, overwrite, &skippedResourceNames, outTable); 1627 1628 if (err < NO_ERROR) { // Why err < NO_ERROR instead of err != NO_ERROR? 1629 hasErrors = localHasErrors = true; 1630 } 1631 else if (err == NO_ERROR) { 1632 if (curIsPseudolocalizable && localeIsDefined(curParams) 1633 && bundle->getPseudolocalize() > 0) { 1634 // pseudolocalize here 1635 if ((PSEUDO_ACCENTED & bundle->getPseudolocalize()) == 1636 PSEUDO_ACCENTED) { 1637 block.setPosition(parserPosition); 1638 err = parseAndAddEntry(bundle, in, &block, pseudoParams, myPackage, curType, 1639 ident, *curTag, curIsStyled, curFormat, 1640 curIsFormatted, product, 1641 PSEUDO_ACCENTED, overwrite, &skippedResourceNames, outTable); 1642 } 1643 if ((PSEUDO_BIDI & bundle->getPseudolocalize()) == 1644 PSEUDO_BIDI) { 1645 block.setPosition(parserPosition); 1646 err = parseAndAddEntry(bundle, in, &block, pseudoBidiParams, 1647 myPackage, curType, ident, *curTag, curIsStyled, curFormat, 1648 curIsFormatted, product, 1649 PSEUDO_BIDI, overwrite, &skippedResourceNames, outTable); 1650 } 1651 if (err != NO_ERROR) { 1652 hasErrors = localHasErrors = true; 1653 } 1654 } 1655 } 1656 } 1657 1658#if 0 1659 if (comment.size() > 0) { 1660 printf("Comment for @%s:%s/%s: %s\n", String8(myPackage).string(), 1661 String8(curType).string(), String8(ident).string(), 1662 String8(comment).string()); 1663 } 1664#endif 1665 if (!localHasErrors) { 1666 outTable->appendComment(myPackage, curType, ident, comment, false); 1667 } 1668 } 1669 else if (code == ResXMLTree::END_TAG) { 1670 if (strcmp16(block.getElementName(&len), resources16.string()) != 0) { 1671 SourcePos(in->getPrintableSource(), block.getLineNumber()).error( 1672 "Unexpected end tag %s\n", String8(block.getElementName(&len)).string()); 1673 return UNKNOWN_ERROR; 1674 } 1675 } 1676 else if (code == ResXMLTree::START_NAMESPACE || code == ResXMLTree::END_NAMESPACE) { 1677 } 1678 else if (code == ResXMLTree::TEXT) { 1679 if (isWhitespace(block.getText(&len))) { 1680 continue; 1681 } 1682 SourcePos(in->getPrintableSource(), block.getLineNumber()).error( 1683 "Found text \"%s\" where item tag is expected\n", 1684 String8(block.getText(&len)).string()); 1685 return UNKNOWN_ERROR; 1686 } 1687 } 1688 1689 // For every resource defined, there must be exist one variant with a product attribute 1690 // set to 'default' (or no product attribute at all). 1691 // We check to see that for every resource that was ignored because of a mismatched 1692 // product attribute, some product variant of that resource was processed. 1693 for (size_t i = 0; i < skippedResourceNames.size(); i++) { 1694 if (skippedResourceNames[i]) { 1695 const type_ident_pair_t& p = skippedResourceNames.keyAt(i); 1696 if (!outTable->hasBagOrEntry(myPackage, p.type, p.ident)) { 1697 const char* bundleProduct = 1698 (bundle->getProduct() == NULL) ? "" : bundle->getProduct(); 1699 fprintf(stderr, "In resource file %s: %s\n", 1700 in->getPrintableSource().string(), 1701 curParams.toString().string()); 1702 1703 fprintf(stderr, "\t%s '%s' does not match product %s.\n" 1704 "\tYou may have forgotten to include a 'default' product variant" 1705 " of the resource.\n", 1706 String8(p.type).string(), String8(p.ident).string(), 1707 bundleProduct[0] == 0 ? "default" : bundleProduct); 1708 return UNKNOWN_ERROR; 1709 } 1710 } 1711 } 1712 1713 return hasErrors ? UNKNOWN_ERROR : NO_ERROR; 1714} 1715 1716ResourceTable::ResourceTable(Bundle* bundle, const String16& assetsPackage) 1717 : mAssetsPackage(assetsPackage), mNextPackageId(1), mHaveAppPackage(false), 1718 mIsAppPackage(!bundle->getExtending()), 1719 mNumLocal(0), 1720 mBundle(bundle) 1721{ 1722} 1723 1724status_t ResourceTable::addIncludedResources(Bundle* bundle, const sp<AaptAssets>& assets) 1725{ 1726 status_t err = assets->buildIncludedResources(bundle); 1727 if (err != NO_ERROR) { 1728 return err; 1729 } 1730 1731 // For future reference to included resources. 1732 mAssets = assets; 1733 1734 const ResTable& incl = assets->getIncludedResources(); 1735 1736 // Retrieve all the packages. 1737 const size_t N = incl.getBasePackageCount(); 1738 for (size_t phase=0; phase<2; phase++) { 1739 for (size_t i=0; i<N; i++) { 1740 String16 name(incl.getBasePackageName(i)); 1741 uint32_t id = incl.getBasePackageId(i); 1742 // First time through: only add base packages (id 1743 // is not 0); second time through add the other 1744 // packages. 1745 if (phase != 0) { 1746 if (id != 0) { 1747 // Skip base packages -- already one. 1748 id = 0; 1749 } else { 1750 // Assign a dynamic id. 1751 id = mNextPackageId; 1752 } 1753 } else if (id != 0) { 1754 if (id == 127) { 1755 if (mHaveAppPackage) { 1756 fprintf(stderr, "Included resources have two application packages!\n"); 1757 return UNKNOWN_ERROR; 1758 } 1759 mHaveAppPackage = true; 1760 } 1761 if (mNextPackageId > id) { 1762 fprintf(stderr, "Included base package ID %d already in use!\n", id); 1763 return UNKNOWN_ERROR; 1764 } 1765 } 1766 if (id != 0) { 1767 NOISY(printf("Including package %s with ID=%d\n", 1768 String8(name).string(), id)); 1769 sp<Package> p = new Package(name, id); 1770 mPackages.add(name, p); 1771 mOrderedPackages.add(p); 1772 1773 if (id >= mNextPackageId) { 1774 mNextPackageId = id+1; 1775 } 1776 } 1777 } 1778 } 1779 1780 // Every resource table always has one first entry, the bag attributes. 1781 const SourcePos unknown(String8("????"), 0); 1782 sp<Type> attr = getType(mAssetsPackage, String16("attr"), unknown); 1783 1784 return NO_ERROR; 1785} 1786 1787status_t ResourceTable::addPublic(const SourcePos& sourcePos, 1788 const String16& package, 1789 const String16& type, 1790 const String16& name, 1791 const uint32_t ident) 1792{ 1793 uint32_t rid = mAssets->getIncludedResources() 1794 .identifierForName(name.string(), name.size(), 1795 type.string(), type.size(), 1796 package.string(), package.size()); 1797 if (rid != 0) { 1798 sourcePos.error("Error declaring public resource %s/%s for included package %s\n", 1799 String8(type).string(), String8(name).string(), 1800 String8(package).string()); 1801 return UNKNOWN_ERROR; 1802 } 1803 1804 sp<Type> t = getType(package, type, sourcePos); 1805 if (t == NULL) { 1806 return UNKNOWN_ERROR; 1807 } 1808 return t->addPublic(sourcePos, name, ident); 1809} 1810 1811status_t ResourceTable::addEntry(const SourcePos& sourcePos, 1812 const String16& package, 1813 const String16& type, 1814 const String16& name, 1815 const String16& value, 1816 const Vector<StringPool::entry_style_span>* style, 1817 const ResTable_config* params, 1818 const bool doSetIndex, 1819 const int32_t format, 1820 const bool overwrite) 1821{ 1822 // Check for adding entries in other packages... for now we do 1823 // nothing. We need to do the right thing here to support skinning. 1824 uint32_t rid = mAssets->getIncludedResources() 1825 .identifierForName(name.string(), name.size(), 1826 type.string(), type.size(), 1827 package.string(), package.size()); 1828 if (rid != 0) { 1829 return NO_ERROR; 1830 } 1831 1832#if 0 1833 if (name == String16("left")) { 1834 printf("Adding entry left: file=%s, line=%d, type=%s, value=%s\n", 1835 sourcePos.file.string(), sourcePos.line, String8(type).string(), 1836 String8(value).string()); 1837 } 1838#endif 1839 1840 sp<Entry> e = getEntry(package, type, name, sourcePos, overwrite, 1841 params, doSetIndex); 1842 if (e == NULL) { 1843 return UNKNOWN_ERROR; 1844 } 1845 status_t err = e->setItem(sourcePos, value, style, format, overwrite); 1846 if (err == NO_ERROR) { 1847 mNumLocal++; 1848 } 1849 return err; 1850} 1851 1852status_t ResourceTable::startBag(const SourcePos& sourcePos, 1853 const String16& package, 1854 const String16& type, 1855 const String16& name, 1856 const String16& bagParent, 1857 const ResTable_config* params, 1858 bool overlay, 1859 bool replace, bool isId) 1860{ 1861 status_t result = NO_ERROR; 1862 1863 // Check for adding entries in other packages... for now we do 1864 // nothing. We need to do the right thing here to support skinning. 1865 uint32_t rid = mAssets->getIncludedResources() 1866 .identifierForName(name.string(), name.size(), 1867 type.string(), type.size(), 1868 package.string(), package.size()); 1869 if (rid != 0) { 1870 return NO_ERROR; 1871 } 1872 1873#if 0 1874 if (name == String16("left")) { 1875 printf("Adding bag left: file=%s, line=%d, type=%s\n", 1876 sourcePos.file.striing(), sourcePos.line, String8(type).string()); 1877 } 1878#endif 1879 if (overlay && !mBundle->getAutoAddOverlay() && !hasBagOrEntry(package, type, name)) { 1880 bool canAdd = false; 1881 sp<Package> p = mPackages.valueFor(package); 1882 if (p != NULL) { 1883 sp<Type> t = p->getTypes().valueFor(type); 1884 if (t != NULL) { 1885 if (t->getCanAddEntries().indexOf(name) >= 0) { 1886 canAdd = true; 1887 } 1888 } 1889 } 1890 if (!canAdd) { 1891 sourcePos.error("Resource does not already exist in overlay at '%s'; use <add-resource> to add.\n", 1892 String8(name).string()); 1893 return UNKNOWN_ERROR; 1894 } 1895 } 1896 sp<Entry> e = getEntry(package, type, name, sourcePos, overlay, params); 1897 if (e == NULL) { 1898 return UNKNOWN_ERROR; 1899 } 1900 1901 // If a parent is explicitly specified, set it. 1902 if (bagParent.size() > 0) { 1903 e->setParent(bagParent); 1904 } 1905 1906 if ((result = e->makeItABag(sourcePos)) != NO_ERROR) { 1907 return result; 1908 } 1909 1910 if (overlay && replace) { 1911 return e->emptyBag(sourcePos); 1912 } 1913 return result; 1914} 1915 1916status_t ResourceTable::addBag(const SourcePos& sourcePos, 1917 const String16& package, 1918 const String16& type, 1919 const String16& name, 1920 const String16& bagParent, 1921 const String16& bagKey, 1922 const String16& value, 1923 const Vector<StringPool::entry_style_span>* style, 1924 const ResTable_config* params, 1925 bool replace, bool isId, const int32_t format) 1926{ 1927 // Check for adding entries in other packages... for now we do 1928 // nothing. We need to do the right thing here to support skinning. 1929 uint32_t rid = mAssets->getIncludedResources() 1930 .identifierForName(name.string(), name.size(), 1931 type.string(), type.size(), 1932 package.string(), package.size()); 1933 if (rid != 0) { 1934 return NO_ERROR; 1935 } 1936 1937#if 0 1938 if (name == String16("left")) { 1939 printf("Adding bag left: file=%s, line=%d, type=%s\n", 1940 sourcePos.file.striing(), sourcePos.line, String8(type).string()); 1941 } 1942#endif 1943 sp<Entry> e = getEntry(package, type, name, sourcePos, replace, params); 1944 if (e == NULL) { 1945 return UNKNOWN_ERROR; 1946 } 1947 1948 // If a parent is explicitly specified, set it. 1949 if (bagParent.size() > 0) { 1950 e->setParent(bagParent); 1951 } 1952 1953 const bool first = e->getBag().indexOfKey(bagKey) < 0; 1954 status_t err = e->addToBag(sourcePos, bagKey, value, style, replace, isId, format); 1955 if (err == NO_ERROR && first) { 1956 mNumLocal++; 1957 } 1958 return err; 1959} 1960 1961bool ResourceTable::hasBagOrEntry(const String16& package, 1962 const String16& type, 1963 const String16& name) const 1964{ 1965 // First look for this in the included resources... 1966 uint32_t rid = mAssets->getIncludedResources() 1967 .identifierForName(name.string(), name.size(), 1968 type.string(), type.size(), 1969 package.string(), package.size()); 1970 if (rid != 0) { 1971 return true; 1972 } 1973 1974 sp<Package> p = mPackages.valueFor(package); 1975 if (p != NULL) { 1976 sp<Type> t = p->getTypes().valueFor(type); 1977 if (t != NULL) { 1978 sp<ConfigList> c = t->getConfigs().valueFor(name); 1979 if (c != NULL) return true; 1980 } 1981 } 1982 1983 return false; 1984} 1985 1986bool ResourceTable::hasBagOrEntry(const String16& package, 1987 const String16& type, 1988 const String16& name, 1989 const ResTable_config& config) const 1990{ 1991 // First look for this in the included resources... 1992 uint32_t rid = mAssets->getIncludedResources() 1993 .identifierForName(name.string(), name.size(), 1994 type.string(), type.size(), 1995 package.string(), package.size()); 1996 if (rid != 0) { 1997 return true; 1998 } 1999 2000 sp<Package> p = mPackages.valueFor(package); 2001 if (p != NULL) { 2002 sp<Type> t = p->getTypes().valueFor(type); 2003 if (t != NULL) { 2004 sp<ConfigList> c = t->getConfigs().valueFor(name); 2005 if (c != NULL) { 2006 sp<Entry> e = c->getEntries().valueFor(config); 2007 if (e != NULL) { 2008 return true; 2009 } 2010 } 2011 } 2012 } 2013 2014 return false; 2015} 2016 2017bool ResourceTable::hasBagOrEntry(const String16& ref, 2018 const String16* defType, 2019 const String16* defPackage) 2020{ 2021 String16 package, type, name; 2022 if (!ResTable::expandResourceRef(ref.string(), ref.size(), &package, &type, &name, 2023 defType, defPackage ? defPackage:&mAssetsPackage, NULL)) { 2024 return false; 2025 } 2026 return hasBagOrEntry(package, type, name); 2027} 2028 2029bool ResourceTable::appendComment(const String16& package, 2030 const String16& type, 2031 const String16& name, 2032 const String16& comment, 2033 bool onlyIfEmpty) 2034{ 2035 if (comment.size() <= 0) { 2036 return true; 2037 } 2038 2039 sp<Package> p = mPackages.valueFor(package); 2040 if (p != NULL) { 2041 sp<Type> t = p->getTypes().valueFor(type); 2042 if (t != NULL) { 2043 sp<ConfigList> c = t->getConfigs().valueFor(name); 2044 if (c != NULL) { 2045 c->appendComment(comment, onlyIfEmpty); 2046 return true; 2047 } 2048 } 2049 } 2050 return false; 2051} 2052 2053bool ResourceTable::appendTypeComment(const String16& package, 2054 const String16& type, 2055 const String16& name, 2056 const String16& comment) 2057{ 2058 if (comment.size() <= 0) { 2059 return true; 2060 } 2061 2062 sp<Package> p = mPackages.valueFor(package); 2063 if (p != NULL) { 2064 sp<Type> t = p->getTypes().valueFor(type); 2065 if (t != NULL) { 2066 sp<ConfigList> c = t->getConfigs().valueFor(name); 2067 if (c != NULL) { 2068 c->appendTypeComment(comment); 2069 return true; 2070 } 2071 } 2072 } 2073 return false; 2074} 2075 2076void ResourceTable::canAddEntry(const SourcePos& pos, 2077 const String16& package, const String16& type, const String16& name) 2078{ 2079 sp<Type> t = getType(package, type, pos); 2080 if (t != NULL) { 2081 t->canAddEntry(name); 2082 } 2083} 2084 2085size_t ResourceTable::size() const { 2086 return mPackages.size(); 2087} 2088 2089size_t ResourceTable::numLocalResources() const { 2090 return mNumLocal; 2091} 2092 2093bool ResourceTable::hasResources() const { 2094 return mNumLocal > 0; 2095} 2096 2097sp<AaptFile> ResourceTable::flatten(Bundle* bundle) 2098{ 2099 sp<AaptFile> data = new AaptFile(String8(), AaptGroupEntry(), String8()); 2100 status_t err = flatten(bundle, data); 2101 return err == NO_ERROR ? data : NULL; 2102} 2103 2104inline uint32_t ResourceTable::getResId(const sp<Package>& p, 2105 const sp<Type>& t, 2106 uint32_t nameId) 2107{ 2108 return makeResId(p->getAssignedId(), t->getIndex(), nameId); 2109} 2110 2111uint32_t ResourceTable::getResId(const String16& package, 2112 const String16& type, 2113 const String16& name, 2114 bool onlyPublic) const 2115{ 2116 uint32_t id = ResourceIdCache::lookup(package, type, name, onlyPublic); 2117 if (id != 0) return id; // cache hit 2118 2119 sp<Package> p = mPackages.valueFor(package); 2120 if (p == NULL) return 0; 2121 2122 // First look for this in the included resources... 2123 uint32_t specFlags = 0; 2124 uint32_t rid = mAssets->getIncludedResources() 2125 .identifierForName(name.string(), name.size(), 2126 type.string(), type.size(), 2127 package.string(), package.size(), 2128 &specFlags); 2129 if (rid != 0) { 2130 if (onlyPublic) { 2131 if ((specFlags & ResTable_typeSpec::SPEC_PUBLIC) == 0) { 2132 return 0; 2133 } 2134 } 2135 2136 if (Res_INTERNALID(rid)) { 2137 return ResourceIdCache::store(package, type, name, onlyPublic, rid); 2138 } 2139 return ResourceIdCache::store(package, type, name, onlyPublic, 2140 Res_MAKEID(p->getAssignedId()-1, Res_GETTYPE(rid), Res_GETENTRY(rid))); 2141 } 2142 2143 sp<Type> t = p->getTypes().valueFor(type); 2144 if (t == NULL) return 0; 2145 sp<ConfigList> c = t->getConfigs().valueFor(name); 2146 if (c == NULL) return 0; 2147 int32_t ei = c->getEntryIndex(); 2148 if (ei < 0) return 0; 2149 2150 return ResourceIdCache::store(package, type, name, onlyPublic, 2151 getResId(p, t, ei)); 2152} 2153 2154uint32_t ResourceTable::getResId(const String16& ref, 2155 const String16* defType, 2156 const String16* defPackage, 2157 const char** outErrorMsg, 2158 bool onlyPublic) const 2159{ 2160 String16 package, type, name; 2161 bool refOnlyPublic = true; 2162 if (!ResTable::expandResourceRef( 2163 ref.string(), ref.size(), &package, &type, &name, 2164 defType, defPackage ? defPackage:&mAssetsPackage, 2165 outErrorMsg, &refOnlyPublic)) { 2166 NOISY(printf("Expanding resource: ref=%s\n", 2167 String8(ref).string())); 2168 NOISY(printf("Expanding resource: defType=%s\n", 2169 defType ? String8(*defType).string() : "NULL")); 2170 NOISY(printf("Expanding resource: defPackage=%s\n", 2171 defPackage ? String8(*defPackage).string() : "NULL")); 2172 NOISY(printf("Expanding resource: ref=%s\n", String8(ref).string())); 2173 NOISY(printf("Expanded resource: p=%s, t=%s, n=%s, res=0\n", 2174 String8(package).string(), String8(type).string(), 2175 String8(name).string())); 2176 return 0; 2177 } 2178 uint32_t res = getResId(package, type, name, onlyPublic && refOnlyPublic); 2179 NOISY(printf("Expanded resource: p=%s, t=%s, n=%s, res=%d\n", 2180 String8(package).string(), String8(type).string(), 2181 String8(name).string(), res)); 2182 if (res == 0) { 2183 if (outErrorMsg) 2184 *outErrorMsg = "No resource found that matches the given name"; 2185 } 2186 return res; 2187} 2188 2189bool ResourceTable::isValidResourceName(const String16& s) 2190{ 2191 const char16_t* p = s.string(); 2192 bool first = true; 2193 while (*p) { 2194 if ((*p >= 'a' && *p <= 'z') 2195 || (*p >= 'A' && *p <= 'Z') 2196 || *p == '_' 2197 || (!first && *p >= '0' && *p <= '9')) { 2198 first = false; 2199 p++; 2200 continue; 2201 } 2202 return false; 2203 } 2204 return true; 2205} 2206 2207bool ResourceTable::stringToValue(Res_value* outValue, StringPool* pool, 2208 const String16& str, 2209 bool preserveSpaces, bool coerceType, 2210 uint32_t attrID, 2211 const Vector<StringPool::entry_style_span>* style, 2212 String16* outStr, void* accessorCookie, 2213 uint32_t attrType, const String8* configTypeName, 2214 const ConfigDescription* config) 2215{ 2216 String16 finalStr; 2217 2218 bool res = true; 2219 if (style == NULL || style->size() == 0) { 2220 // Text is not styled so it can be any type... let's figure it out. 2221 res = mAssets->getIncludedResources() 2222 .stringToValue(outValue, &finalStr, str.string(), str.size(), preserveSpaces, 2223 coerceType, attrID, NULL, &mAssetsPackage, this, 2224 accessorCookie, attrType); 2225 } else { 2226 // Styled text can only be a string, and while collecting the style 2227 // information we have already processed that string! 2228 outValue->size = sizeof(Res_value); 2229 outValue->res0 = 0; 2230 outValue->dataType = outValue->TYPE_STRING; 2231 outValue->data = 0; 2232 finalStr = str; 2233 } 2234 2235 if (!res) { 2236 return false; 2237 } 2238 2239 if (outValue->dataType == outValue->TYPE_STRING) { 2240 // Should do better merging styles. 2241 if (pool) { 2242 String8 configStr; 2243 if (config != NULL) { 2244 configStr = config->toString(); 2245 } else { 2246 configStr = "(null)"; 2247 } 2248 NOISY(printf("Adding to pool string style #%d config %s: %s\n", 2249 style != NULL ? style->size() : 0, 2250 configStr.string(), String8(finalStr).string())); 2251 if (style != NULL && style->size() > 0) { 2252 outValue->data = pool->add(finalStr, *style, configTypeName, config); 2253 } else { 2254 outValue->data = pool->add(finalStr, true, configTypeName, config); 2255 } 2256 } else { 2257 // Caller will fill this in later. 2258 outValue->data = 0; 2259 } 2260 2261 if (outStr) { 2262 *outStr = finalStr; 2263 } 2264 2265 } 2266 2267 return true; 2268} 2269 2270uint32_t ResourceTable::getCustomResource( 2271 const String16& package, const String16& type, const String16& name) const 2272{ 2273 //printf("getCustomResource: %s %s %s\n", String8(package).string(), 2274 // String8(type).string(), String8(name).string()); 2275 sp<Package> p = mPackages.valueFor(package); 2276 if (p == NULL) return 0; 2277 sp<Type> t = p->getTypes().valueFor(type); 2278 if (t == NULL) return 0; 2279 sp<ConfigList> c = t->getConfigs().valueFor(name); 2280 if (c == NULL) return 0; 2281 int32_t ei = c->getEntryIndex(); 2282 if (ei < 0) return 0; 2283 return getResId(p, t, ei); 2284} 2285 2286uint32_t ResourceTable::getCustomResourceWithCreation( 2287 const String16& package, const String16& type, const String16& name, 2288 const bool createIfNotFound) 2289{ 2290 uint32_t resId = getCustomResource(package, type, name); 2291 if (resId != 0 || !createIfNotFound) { 2292 return resId; 2293 } 2294 String16 value("false"); 2295 2296 status_t status = addEntry(mCurrentXmlPos, package, type, name, value, NULL, NULL, true); 2297 if (status == NO_ERROR) { 2298 resId = getResId(package, type, name); 2299 return resId; 2300 } 2301 return 0; 2302} 2303 2304uint32_t ResourceTable::getRemappedPackage(uint32_t origPackage) const 2305{ 2306 return origPackage; 2307} 2308 2309bool ResourceTable::getAttributeType(uint32_t attrID, uint32_t* outType) 2310{ 2311 //printf("getAttributeType #%08x\n", attrID); 2312 Res_value value; 2313 if (getItemValue(attrID, ResTable_map::ATTR_TYPE, &value)) { 2314 //printf("getAttributeType #%08x (%s): #%08x\n", attrID, 2315 // String8(getEntry(attrID)->getName()).string(), value.data); 2316 *outType = value.data; 2317 return true; 2318 } 2319 return false; 2320} 2321 2322bool ResourceTable::getAttributeMin(uint32_t attrID, uint32_t* outMin) 2323{ 2324 //printf("getAttributeMin #%08x\n", attrID); 2325 Res_value value; 2326 if (getItemValue(attrID, ResTable_map::ATTR_MIN, &value)) { 2327 *outMin = value.data; 2328 return true; 2329 } 2330 return false; 2331} 2332 2333bool ResourceTable::getAttributeMax(uint32_t attrID, uint32_t* outMax) 2334{ 2335 //printf("getAttributeMax #%08x\n", attrID); 2336 Res_value value; 2337 if (getItemValue(attrID, ResTable_map::ATTR_MAX, &value)) { 2338 *outMax = value.data; 2339 return true; 2340 } 2341 return false; 2342} 2343 2344uint32_t ResourceTable::getAttributeL10N(uint32_t attrID) 2345{ 2346 //printf("getAttributeL10N #%08x\n", attrID); 2347 Res_value value; 2348 if (getItemValue(attrID, ResTable_map::ATTR_L10N, &value)) { 2349 return value.data; 2350 } 2351 return ResTable_map::L10N_NOT_REQUIRED; 2352} 2353 2354bool ResourceTable::getLocalizationSetting() 2355{ 2356 return mBundle->getRequireLocalization(); 2357} 2358 2359void ResourceTable::reportError(void* accessorCookie, const char* fmt, ...) 2360{ 2361 if (accessorCookie != NULL && fmt != NULL) { 2362 AccessorCookie* ac = (AccessorCookie*)accessorCookie; 2363 int retval=0; 2364 char buf[1024]; 2365 va_list ap; 2366 va_start(ap, fmt); 2367 retval = vsnprintf(buf, sizeof(buf), fmt, ap); 2368 va_end(ap); 2369 ac->sourcePos.error("Error: %s (at '%s' with value '%s').\n", 2370 buf, ac->attr.string(), ac->value.string()); 2371 } 2372} 2373 2374bool ResourceTable::getAttributeKeys( 2375 uint32_t attrID, Vector<String16>* outKeys) 2376{ 2377 sp<const Entry> e = getEntry(attrID); 2378 if (e != NULL) { 2379 const size_t N = e->getBag().size(); 2380 for (size_t i=0; i<N; i++) { 2381 const String16& key = e->getBag().keyAt(i); 2382 if (key.size() > 0 && key.string()[0] != '^') { 2383 outKeys->add(key); 2384 } 2385 } 2386 return true; 2387 } 2388 return false; 2389} 2390 2391bool ResourceTable::getAttributeEnum( 2392 uint32_t attrID, const char16_t* name, size_t nameLen, 2393 Res_value* outValue) 2394{ 2395 //printf("getAttributeEnum #%08x %s\n", attrID, String8(name, nameLen).string()); 2396 String16 nameStr(name, nameLen); 2397 sp<const Entry> e = getEntry(attrID); 2398 if (e != NULL) { 2399 const size_t N = e->getBag().size(); 2400 for (size_t i=0; i<N; i++) { 2401 //printf("Comparing %s to %s\n", String8(name, nameLen).string(), 2402 // String8(e->getBag().keyAt(i)).string()); 2403 if (e->getBag().keyAt(i) == nameStr) { 2404 return getItemValue(attrID, e->getBag().valueAt(i).bagKeyId, outValue); 2405 } 2406 } 2407 } 2408 return false; 2409} 2410 2411bool ResourceTable::getAttributeFlags( 2412 uint32_t attrID, const char16_t* name, size_t nameLen, 2413 Res_value* outValue) 2414{ 2415 outValue->dataType = Res_value::TYPE_INT_HEX; 2416 outValue->data = 0; 2417 2418 //printf("getAttributeFlags #%08x %s\n", attrID, String8(name, nameLen).string()); 2419 String16 nameStr(name, nameLen); 2420 sp<const Entry> e = getEntry(attrID); 2421 if (e != NULL) { 2422 const size_t N = e->getBag().size(); 2423 2424 const char16_t* end = name + nameLen; 2425 const char16_t* pos = name; 2426 while (pos < end) { 2427 const char16_t* start = pos; 2428 while (pos < end && *pos != '|') { 2429 pos++; 2430 } 2431 2432 String16 nameStr(start, pos-start); 2433 size_t i; 2434 for (i=0; i<N; i++) { 2435 //printf("Comparing \"%s\" to \"%s\"\n", String8(nameStr).string(), 2436 // String8(e->getBag().keyAt(i)).string()); 2437 if (e->getBag().keyAt(i) == nameStr) { 2438 Res_value val; 2439 bool got = getItemValue(attrID, e->getBag().valueAt(i).bagKeyId, &val); 2440 if (!got) { 2441 return false; 2442 } 2443 //printf("Got value: 0x%08x\n", val.data); 2444 outValue->data |= val.data; 2445 break; 2446 } 2447 } 2448 2449 if (i >= N) { 2450 // Didn't find this flag identifier. 2451 return false; 2452 } 2453 pos++; 2454 } 2455 2456 return true; 2457 } 2458 return false; 2459} 2460 2461status_t ResourceTable::assignResourceIds() 2462{ 2463 const size_t N = mOrderedPackages.size(); 2464 size_t pi; 2465 status_t firstError = NO_ERROR; 2466 2467 // First generate all bag attributes and assign indices. 2468 for (pi=0; pi<N; pi++) { 2469 sp<Package> p = mOrderedPackages.itemAt(pi); 2470 if (p == NULL || p->getTypes().size() == 0) { 2471 // Empty, skip! 2472 continue; 2473 } 2474 2475 status_t err = p->applyPublicTypeOrder(); 2476 if (err != NO_ERROR && firstError == NO_ERROR) { 2477 firstError = err; 2478 } 2479 2480 // Generate attributes... 2481 const size_t N = p->getOrderedTypes().size(); 2482 size_t ti; 2483 for (ti=0; ti<N; ti++) { 2484 sp<Type> t = p->getOrderedTypes().itemAt(ti); 2485 if (t == NULL) { 2486 continue; 2487 } 2488 const size_t N = t->getOrderedConfigs().size(); 2489 for (size_t ci=0; ci<N; ci++) { 2490 sp<ConfigList> c = t->getOrderedConfigs().itemAt(ci); 2491 if (c == NULL) { 2492 continue; 2493 } 2494 const size_t N = c->getEntries().size(); 2495 for (size_t ei=0; ei<N; ei++) { 2496 sp<Entry> e = c->getEntries().valueAt(ei); 2497 if (e == NULL) { 2498 continue; 2499 } 2500 status_t err = e->generateAttributes(this, p->getName()); 2501 if (err != NO_ERROR && firstError == NO_ERROR) { 2502 firstError = err; 2503 } 2504 } 2505 } 2506 } 2507 2508 const SourcePos unknown(String8("????"), 0); 2509 sp<Type> attr = p->getType(String16("attr"), unknown); 2510 2511 // Assign indices... 2512 for (ti=0; ti<N; ti++) { 2513 sp<Type> t = p->getOrderedTypes().itemAt(ti); 2514 if (t == NULL) { 2515 continue; 2516 } 2517 err = t->applyPublicEntryOrder(); 2518 if (err != NO_ERROR && firstError == NO_ERROR) { 2519 firstError = err; 2520 } 2521 2522 const size_t N = t->getOrderedConfigs().size(); 2523 t->setIndex(ti+1); 2524 2525 LOG_ALWAYS_FATAL_IF(ti == 0 && attr != t, 2526 "First type is not attr!"); 2527 2528 for (size_t ei=0; ei<N; ei++) { 2529 sp<ConfigList> c = t->getOrderedConfigs().itemAt(ei); 2530 if (c == NULL) { 2531 continue; 2532 } 2533 c->setEntryIndex(ei); 2534 } 2535 } 2536 2537 // Assign resource IDs to keys in bags... 2538 for (ti=0; ti<N; ti++) { 2539 sp<Type> t = p->getOrderedTypes().itemAt(ti); 2540 if (t == NULL) { 2541 continue; 2542 } 2543 const size_t N = t->getOrderedConfigs().size(); 2544 for (size_t ci=0; ci<N; ci++) { 2545 sp<ConfigList> c = t->getOrderedConfigs().itemAt(ci); 2546 //printf("Ordered config #%d: %p\n", ci, c.get()); 2547 const size_t N = c->getEntries().size(); 2548 for (size_t ei=0; ei<N; ei++) { 2549 sp<Entry> e = c->getEntries().valueAt(ei); 2550 if (e == NULL) { 2551 continue; 2552 } 2553 status_t err = e->assignResourceIds(this, p->getName()); 2554 if (err != NO_ERROR && firstError == NO_ERROR) { 2555 firstError = err; 2556 } 2557 } 2558 } 2559 } 2560 } 2561 return firstError; 2562} 2563 2564status_t ResourceTable::addSymbols(const sp<AaptSymbols>& outSymbols) { 2565 const size_t N = mOrderedPackages.size(); 2566 size_t pi; 2567 2568 for (pi=0; pi<N; pi++) { 2569 sp<Package> p = mOrderedPackages.itemAt(pi); 2570 if (p->getTypes().size() == 0) { 2571 // Empty, skip! 2572 continue; 2573 } 2574 2575 const size_t N = p->getOrderedTypes().size(); 2576 size_t ti; 2577 2578 for (ti=0; ti<N; ti++) { 2579 sp<Type> t = p->getOrderedTypes().itemAt(ti); 2580 if (t == NULL) { 2581 continue; 2582 } 2583 const size_t N = t->getOrderedConfigs().size(); 2584 sp<AaptSymbols> typeSymbols; 2585 typeSymbols = outSymbols->addNestedSymbol(String8(t->getName()), t->getPos()); 2586 for (size_t ci=0; ci<N; ci++) { 2587 sp<ConfigList> c = t->getOrderedConfigs().itemAt(ci); 2588 if (c == NULL) { 2589 continue; 2590 } 2591 uint32_t rid = getResId(p, t, ci); 2592 if (rid == 0) { 2593 return UNKNOWN_ERROR; 2594 } 2595 if (Res_GETPACKAGE(rid) == (size_t)(p->getAssignedId()-1)) { 2596 typeSymbols->addSymbol(String8(c->getName()), rid, c->getPos()); 2597 2598 String16 comment(c->getComment()); 2599 typeSymbols->appendComment(String8(c->getName()), comment, c->getPos()); 2600 //printf("Type symbol [%08x] %s comment: %s\n", rid, 2601 // String8(c->getName()).string(), String8(comment).string()); 2602 comment = c->getTypeComment(); 2603 typeSymbols->appendTypeComment(String8(c->getName()), comment); 2604 } else { 2605#if 0 2606 printf("**** NO MATCH: 0x%08x vs 0x%08x\n", 2607 Res_GETPACKAGE(rid), p->getAssignedId()); 2608#endif 2609 } 2610 } 2611 } 2612 } 2613 return NO_ERROR; 2614} 2615 2616 2617void 2618ResourceTable::addLocalization(const String16& name, const String8& locale, const SourcePos& src) 2619{ 2620 mLocalizations[name][locale] = src; 2621} 2622 2623 2624/*! 2625 * Flag various sorts of localization problems. '+' indicates checks already implemented; 2626 * '-' indicates checks that will be implemented in the future. 2627 * 2628 * + A localized string for which no default-locale version exists => warning 2629 * + A string for which no version in an explicitly-requested locale exists => warning 2630 * + A localized translation of an translateable="false" string => warning 2631 * - A localized string not provided in every locale used by the table 2632 */ 2633status_t 2634ResourceTable::validateLocalizations(void) 2635{ 2636 status_t err = NO_ERROR; 2637 const String8 defaultLocale; 2638 2639 // For all strings... 2640 for (map<String16, map<String8, SourcePos> >::iterator nameIter = mLocalizations.begin(); 2641 nameIter != mLocalizations.end(); 2642 nameIter++) { 2643 const map<String8, SourcePos>& configSrcMap = nameIter->second; 2644 2645 // Look for strings with no default localization 2646 if (configSrcMap.count(defaultLocale) == 0) { 2647 SourcePos().warning("string '%s' has no default translation.", 2648 String8(nameIter->first).string()); 2649 if (mBundle->getVerbose()) { 2650 for (map<String8, SourcePos>::const_iterator locales = configSrcMap.begin(); 2651 locales != configSrcMap.end(); 2652 locales++) { 2653 locales->second.printf("locale %s found", locales->first.string()); 2654 } 2655 } 2656 // !!! TODO: throw an error here in some circumstances 2657 } 2658 2659 // Check that all requested localizations are present for this string 2660 if (mBundle->getConfigurations() != NULL && mBundle->getRequireLocalization()) { 2661 const char* allConfigs = mBundle->getConfigurations(); 2662 const char* start = allConfigs; 2663 const char* comma; 2664 2665 set<String8> missingConfigs; 2666 AaptLocaleValue locale; 2667 do { 2668 String8 config; 2669 comma = strchr(start, ','); 2670 if (comma != NULL) { 2671 config.setTo(start, comma - start); 2672 start = comma + 1; 2673 } else { 2674 config.setTo(start); 2675 } 2676 2677 if (!locale.initFromFilterString(config)) { 2678 continue; 2679 } 2680 2681 // don't bother with the pseudolocale "en_XA" or "ar_XB" 2682 if (config != "en_XA" && config != "ar_XB") { 2683 if (configSrcMap.find(config) == configSrcMap.end()) { 2684 // okay, no specific localization found. it's possible that we are 2685 // requiring a specific regional localization [e.g. de_DE] but there is an 2686 // available string in the generic language localization [e.g. de]; 2687 // consider that string to have fulfilled the localization requirement. 2688 String8 region(config.string(), 2); 2689 if (configSrcMap.find(region) == configSrcMap.end() && 2690 configSrcMap.count(defaultLocale) == 0) { 2691 missingConfigs.insert(config); 2692 } 2693 } 2694 } 2695 } while (comma != NULL); 2696 2697 if (!missingConfigs.empty()) { 2698 String8 configStr; 2699 for (set<String8>::iterator iter = missingConfigs.begin(); 2700 iter != missingConfigs.end(); 2701 iter++) { 2702 configStr.appendFormat(" %s", iter->string()); 2703 } 2704 SourcePos().warning("string '%s' is missing %u required localizations:%s", 2705 String8(nameIter->first).string(), 2706 (unsigned int)missingConfigs.size(), 2707 configStr.string()); 2708 } 2709 } 2710 } 2711 2712 return err; 2713} 2714 2715status_t ResourceTable::flatten(Bundle* bundle, const sp<AaptFile>& dest) 2716{ 2717 ResourceFilter filter; 2718 status_t err = filter.parse(bundle->getConfigurations()); 2719 if (err != NO_ERROR) { 2720 return err; 2721 } 2722 2723 const ConfigDescription nullConfig; 2724 2725 const size_t N = mOrderedPackages.size(); 2726 size_t pi; 2727 2728 const static String16 mipmap16("mipmap"); 2729 2730 bool useUTF8 = !bundle->getUTF16StringsOption(); 2731 2732 // Iterate through all data, collecting all values (strings, 2733 // references, etc). 2734 StringPool valueStrings(useUTF8); 2735 Vector<sp<Entry> > allEntries; 2736 for (pi=0; pi<N; pi++) { 2737 sp<Package> p = mOrderedPackages.itemAt(pi); 2738 if (p->getTypes().size() == 0) { 2739 // Empty, skip! 2740 continue; 2741 } 2742 2743 StringPool typeStrings(useUTF8); 2744 StringPool keyStrings(useUTF8); 2745 2746 const size_t N = p->getOrderedTypes().size(); 2747 for (size_t ti=0; ti<N; ti++) { 2748 sp<Type> t = p->getOrderedTypes().itemAt(ti); 2749 if (t == NULL) { 2750 typeStrings.add(String16("<empty>"), false); 2751 continue; 2752 } 2753 const String16 typeName(t->getName()); 2754 typeStrings.add(typeName, false); 2755 2756 // This is a hack to tweak the sorting order of the final strings, 2757 // to put stuff that is generally not language-specific first. 2758 String8 configTypeName(typeName); 2759 if (configTypeName == "drawable" || configTypeName == "layout" 2760 || configTypeName == "color" || configTypeName == "anim" 2761 || configTypeName == "interpolator" || configTypeName == "animator" 2762 || configTypeName == "xml" || configTypeName == "menu" 2763 || configTypeName == "mipmap" || configTypeName == "raw") { 2764 configTypeName = "1complex"; 2765 } else { 2766 configTypeName = "2value"; 2767 } 2768 2769 const bool filterable = (typeName != mipmap16); 2770 2771 const size_t N = t->getOrderedConfigs().size(); 2772 for (size_t ci=0; ci<N; ci++) { 2773 sp<ConfigList> c = t->getOrderedConfigs().itemAt(ci); 2774 if (c == NULL) { 2775 continue; 2776 } 2777 const size_t N = c->getEntries().size(); 2778 for (size_t ei=0; ei<N; ei++) { 2779 ConfigDescription config = c->getEntries().keyAt(ei); 2780 if (filterable && !filter.match(config)) { 2781 continue; 2782 } 2783 sp<Entry> e = c->getEntries().valueAt(ei); 2784 if (e == NULL) { 2785 continue; 2786 } 2787 e->setNameIndex(keyStrings.add(e->getName(), true)); 2788 2789 // If this entry has no values for other configs, 2790 // and is the default config, then it is special. Otherwise 2791 // we want to add it with the config info. 2792 ConfigDescription* valueConfig = NULL; 2793 if (N != 1 || config == nullConfig) { 2794 valueConfig = &config; 2795 } 2796 2797 status_t err = e->prepareFlatten(&valueStrings, this, 2798 &configTypeName, &config); 2799 if (err != NO_ERROR) { 2800 return err; 2801 } 2802 allEntries.add(e); 2803 } 2804 } 2805 } 2806 2807 p->setTypeStrings(typeStrings.createStringBlock()); 2808 p->setKeyStrings(keyStrings.createStringBlock()); 2809 } 2810 2811 if (bundle->getOutputAPKFile() != NULL) { 2812 // Now we want to sort the value strings for better locality. This will 2813 // cause the positions of the strings to change, so we need to go back 2814 // through out resource entries and update them accordingly. Only need 2815 // to do this if actually writing the output file. 2816 valueStrings.sortByConfig(); 2817 for (pi=0; pi<allEntries.size(); pi++) { 2818 allEntries[pi]->remapStringValue(&valueStrings); 2819 } 2820 } 2821 2822 ssize_t strAmt = 0; 2823 2824 // Now build the array of package chunks. 2825 Vector<sp<AaptFile> > flatPackages; 2826 for (pi=0; pi<N; pi++) { 2827 sp<Package> p = mOrderedPackages.itemAt(pi); 2828 if (p->getTypes().size() == 0) { 2829 // Empty, skip! 2830 continue; 2831 } 2832 2833 const size_t N = p->getTypeStrings().size(); 2834 2835 const size_t baseSize = sizeof(ResTable_package); 2836 2837 // Start the package data. 2838 sp<AaptFile> data = new AaptFile(String8(), AaptGroupEntry(), String8()); 2839 ResTable_package* header = (ResTable_package*)data->editData(baseSize); 2840 if (header == NULL) { 2841 fprintf(stderr, "ERROR: out of memory creating ResTable_package\n"); 2842 return NO_MEMORY; 2843 } 2844 memset(header, 0, sizeof(*header)); 2845 header->header.type = htods(RES_TABLE_PACKAGE_TYPE); 2846 header->header.headerSize = htods(sizeof(*header)); 2847 header->id = htodl(p->getAssignedId()); 2848 strcpy16_htod(header->name, p->getName().string()); 2849 2850 // Write the string blocks. 2851 const size_t typeStringsStart = data->getSize(); 2852 sp<AaptFile> strFile = p->getTypeStringsData(); 2853 ssize_t amt = data->writeData(strFile->getData(), strFile->getSize()); 2854 #if PRINT_STRING_METRICS 2855 fprintf(stderr, "**** type strings: %d\n", amt); 2856 #endif 2857 strAmt += amt; 2858 if (amt < 0) { 2859 return amt; 2860 } 2861 const size_t keyStringsStart = data->getSize(); 2862 strFile = p->getKeyStringsData(); 2863 amt = data->writeData(strFile->getData(), strFile->getSize()); 2864 #if PRINT_STRING_METRICS 2865 fprintf(stderr, "**** key strings: %d\n", amt); 2866 #endif 2867 strAmt += amt; 2868 if (amt < 0) { 2869 return amt; 2870 } 2871 2872 // Build the type chunks inside of this package. 2873 for (size_t ti=0; ti<N; ti++) { 2874 // Retrieve them in the same order as the type string block. 2875 size_t len; 2876 String16 typeName(p->getTypeStrings().stringAt(ti, &len)); 2877 sp<Type> t = p->getTypes().valueFor(typeName); 2878 LOG_ALWAYS_FATAL_IF(t == NULL && typeName != String16("<empty>"), 2879 "Type name %s not found", 2880 String8(typeName).string()); 2881 2882 const bool filterable = (typeName != mipmap16); 2883 2884 const size_t N = t != NULL ? t->getOrderedConfigs().size() : 0; 2885 2886 // Until a non-NO_ENTRY value has been written for a resource, 2887 // that resource is invalid; validResources[i] represents 2888 // the item at t->getOrderedConfigs().itemAt(i). 2889 Vector<bool> validResources; 2890 validResources.insertAt(false, 0, N); 2891 2892 // First write the typeSpec chunk, containing information about 2893 // each resource entry in this type. 2894 { 2895 const size_t typeSpecSize = sizeof(ResTable_typeSpec) + sizeof(uint32_t)*N; 2896 const size_t typeSpecStart = data->getSize(); 2897 ResTable_typeSpec* tsHeader = (ResTable_typeSpec*) 2898 (((uint8_t*)data->editData(typeSpecStart+typeSpecSize)) + typeSpecStart); 2899 if (tsHeader == NULL) { 2900 fprintf(stderr, "ERROR: out of memory creating ResTable_typeSpec\n"); 2901 return NO_MEMORY; 2902 } 2903 memset(tsHeader, 0, sizeof(*tsHeader)); 2904 tsHeader->header.type = htods(RES_TABLE_TYPE_SPEC_TYPE); 2905 tsHeader->header.headerSize = htods(sizeof(*tsHeader)); 2906 tsHeader->header.size = htodl(typeSpecSize); 2907 tsHeader->id = ti+1; 2908 tsHeader->entryCount = htodl(N); 2909 2910 uint32_t* typeSpecFlags = (uint32_t*) 2911 (((uint8_t*)data->editData()) 2912 + typeSpecStart + sizeof(ResTable_typeSpec)); 2913 memset(typeSpecFlags, 0, sizeof(uint32_t)*N); 2914 2915 for (size_t ei=0; ei<N; ei++) { 2916 sp<ConfigList> cl = t->getOrderedConfigs().itemAt(ei); 2917 if (cl->getPublic()) { 2918 typeSpecFlags[ei] |= htodl(ResTable_typeSpec::SPEC_PUBLIC); 2919 } 2920 const size_t CN = cl->getEntries().size(); 2921 for (size_t ci=0; ci<CN; ci++) { 2922 if (filterable && !filter.match(cl->getEntries().keyAt(ci))) { 2923 continue; 2924 } 2925 for (size_t cj=ci+1; cj<CN; cj++) { 2926 if (filterable && !filter.match(cl->getEntries().keyAt(cj))) { 2927 continue; 2928 } 2929 typeSpecFlags[ei] |= htodl( 2930 cl->getEntries().keyAt(ci).diff(cl->getEntries().keyAt(cj))); 2931 } 2932 } 2933 } 2934 } 2935 2936 // We need to write one type chunk for each configuration for 2937 // which we have entries in this type. 2938 const size_t NC = t->getUniqueConfigs().size(); 2939 2940 const size_t typeSize = sizeof(ResTable_type) + sizeof(uint32_t)*N; 2941 2942 for (size_t ci=0; ci<NC; ci++) { 2943 ConfigDescription config = t->getUniqueConfigs().itemAt(ci); 2944 2945 NOISY(printf("Writing config %d config: imsi:%d/%d lang:%c%c cnt:%c%c " 2946 "orien:%d ui:%d touch:%d density:%d key:%d inp:%d nav:%d sz:%dx%d " 2947 "sw%ddp w%ddp h%ddp dir:%d\n", 2948 ti+1, 2949 config.mcc, config.mnc, 2950 config.language[0] ? config.language[0] : '-', 2951 config.language[1] ? config.language[1] : '-', 2952 config.country[0] ? config.country[0] : '-', 2953 config.country[1] ? config.country[1] : '-', 2954 config.orientation, 2955 config.uiMode, 2956 config.touchscreen, 2957 config.density, 2958 config.keyboard, 2959 config.inputFlags, 2960 config.navigation, 2961 config.screenWidth, 2962 config.screenHeight, 2963 config.smallestScreenWidthDp, 2964 config.screenWidthDp, 2965 config.screenHeightDp, 2966 config.layoutDirection)); 2967 2968 if (filterable && !filter.match(config)) { 2969 continue; 2970 } 2971 2972 const size_t typeStart = data->getSize(); 2973 2974 ResTable_type* tHeader = (ResTable_type*) 2975 (((uint8_t*)data->editData(typeStart+typeSize)) + typeStart); 2976 if (tHeader == NULL) { 2977 fprintf(stderr, "ERROR: out of memory creating ResTable_type\n"); 2978 return NO_MEMORY; 2979 } 2980 2981 memset(tHeader, 0, sizeof(*tHeader)); 2982 tHeader->header.type = htods(RES_TABLE_TYPE_TYPE); 2983 tHeader->header.headerSize = htods(sizeof(*tHeader)); 2984 tHeader->id = ti+1; 2985 tHeader->entryCount = htodl(N); 2986 tHeader->entriesStart = htodl(typeSize); 2987 tHeader->config = config; 2988 NOISY(printf("Writing type %d config: imsi:%d/%d lang:%c%c cnt:%c%c " 2989 "orien:%d ui:%d touch:%d density:%d key:%d inp:%d nav:%d sz:%dx%d " 2990 "sw%ddp w%ddp h%ddp dir:%d\n", 2991 ti+1, 2992 tHeader->config.mcc, tHeader->config.mnc, 2993 tHeader->config.language[0] ? tHeader->config.language[0] : '-', 2994 tHeader->config.language[1] ? tHeader->config.language[1] : '-', 2995 tHeader->config.country[0] ? tHeader->config.country[0] : '-', 2996 tHeader->config.country[1] ? tHeader->config.country[1] : '-', 2997 tHeader->config.orientation, 2998 tHeader->config.uiMode, 2999 tHeader->config.touchscreen, 3000 tHeader->config.density, 3001 tHeader->config.keyboard, 3002 tHeader->config.inputFlags, 3003 tHeader->config.navigation, 3004 tHeader->config.screenWidth, 3005 tHeader->config.screenHeight, 3006 tHeader->config.smallestScreenWidthDp, 3007 tHeader->config.screenWidthDp, 3008 tHeader->config.screenHeightDp, 3009 tHeader->config.layoutDirection)); 3010 tHeader->config.swapHtoD(); 3011 3012 // Build the entries inside of this type. 3013 for (size_t ei=0; ei<N; ei++) { 3014 sp<ConfigList> cl = t->getOrderedConfigs().itemAt(ei); 3015 sp<Entry> e = cl->getEntries().valueFor(config); 3016 3017 // Set the offset for this entry in its type. 3018 uint32_t* index = (uint32_t*) 3019 (((uint8_t*)data->editData()) 3020 + typeStart + sizeof(ResTable_type)); 3021 if (e != NULL) { 3022 index[ei] = htodl(data->getSize()-typeStart-typeSize); 3023 3024 // Create the entry. 3025 ssize_t amt = e->flatten(bundle, data, cl->getPublic()); 3026 if (amt < 0) { 3027 return amt; 3028 } 3029 validResources.editItemAt(ei) = true; 3030 } else { 3031 index[ei] = htodl(ResTable_type::NO_ENTRY); 3032 } 3033 } 3034 3035 // Fill in the rest of the type information. 3036 tHeader = (ResTable_type*) 3037 (((uint8_t*)data->editData()) + typeStart); 3038 tHeader->header.size = htodl(data->getSize()-typeStart); 3039 } 3040 3041 for (size_t i = 0; i < N; ++i) { 3042 if (!validResources[i]) { 3043 sp<ConfigList> c = t->getOrderedConfigs().itemAt(i); 3044 fprintf(stderr, "warning: no entries written for %s/%s\n", 3045 String8(typeName).string(), String8(c->getName()).string()); 3046 } 3047 } 3048 } 3049 3050 // Fill in the rest of the package information. 3051 header = (ResTable_package*)data->editData(); 3052 header->header.size = htodl(data->getSize()); 3053 header->typeStrings = htodl(typeStringsStart); 3054 header->lastPublicType = htodl(p->getTypeStrings().size()); 3055 header->keyStrings = htodl(keyStringsStart); 3056 header->lastPublicKey = htodl(p->getKeyStrings().size()); 3057 3058 flatPackages.add(data); 3059 } 3060 3061 // And now write out the final chunks. 3062 const size_t dataStart = dest->getSize(); 3063 3064 { 3065 // blah 3066 ResTable_header header; 3067 memset(&header, 0, sizeof(header)); 3068 header.header.type = htods(RES_TABLE_TYPE); 3069 header.header.headerSize = htods(sizeof(header)); 3070 header.packageCount = htodl(flatPackages.size()); 3071 status_t err = dest->writeData(&header, sizeof(header)); 3072 if (err != NO_ERROR) { 3073 fprintf(stderr, "ERROR: out of memory creating ResTable_header\n"); 3074 return err; 3075 } 3076 } 3077 3078 ssize_t strStart = dest->getSize(); 3079 err = valueStrings.writeStringBlock(dest); 3080 if (err != NO_ERROR) { 3081 return err; 3082 } 3083 3084 ssize_t amt = (dest->getSize()-strStart); 3085 strAmt += amt; 3086 #if PRINT_STRING_METRICS 3087 fprintf(stderr, "**** value strings: %d\n", amt); 3088 fprintf(stderr, "**** total strings: %d\n", strAmt); 3089 #endif 3090 3091 for (pi=0; pi<flatPackages.size(); pi++) { 3092 err = dest->writeData(flatPackages[pi]->getData(), 3093 flatPackages[pi]->getSize()); 3094 if (err != NO_ERROR) { 3095 fprintf(stderr, "ERROR: out of memory creating package chunk for ResTable_header\n"); 3096 return err; 3097 } 3098 } 3099 3100 ResTable_header* header = (ResTable_header*) 3101 (((uint8_t*)dest->getData()) + dataStart); 3102 header->header.size = htodl(dest->getSize() - dataStart); 3103 3104 NOISY(aout << "Resource table:" 3105 << HexDump(dest->getData(), dest->getSize()) << endl); 3106 3107 #if PRINT_STRING_METRICS 3108 fprintf(stderr, "**** total resource table size: %d / %d%% strings\n", 3109 dest->getSize(), (strAmt*100)/dest->getSize()); 3110 #endif 3111 3112 return NO_ERROR; 3113} 3114 3115void ResourceTable::writePublicDefinitions(const String16& package, FILE* fp) 3116{ 3117 fprintf(fp, 3118 "<!-- This file contains <public> resource definitions for all\n" 3119 " resources that were generated from the source data. -->\n" 3120 "\n" 3121 "<resources>\n"); 3122 3123 writePublicDefinitions(package, fp, true); 3124 writePublicDefinitions(package, fp, false); 3125 3126 fprintf(fp, 3127 "\n" 3128 "</resources>\n"); 3129} 3130 3131void ResourceTable::writePublicDefinitions(const String16& package, FILE* fp, bool pub) 3132{ 3133 bool didHeader = false; 3134 3135 sp<Package> pkg = mPackages.valueFor(package); 3136 if (pkg != NULL) { 3137 const size_t NT = pkg->getOrderedTypes().size(); 3138 for (size_t i=0; i<NT; i++) { 3139 sp<Type> t = pkg->getOrderedTypes().itemAt(i); 3140 if (t == NULL) { 3141 continue; 3142 } 3143 3144 bool didType = false; 3145 3146 const size_t NC = t->getOrderedConfigs().size(); 3147 for (size_t j=0; j<NC; j++) { 3148 sp<ConfigList> c = t->getOrderedConfigs().itemAt(j); 3149 if (c == NULL) { 3150 continue; 3151 } 3152 3153 if (c->getPublic() != pub) { 3154 continue; 3155 } 3156 3157 if (!didType) { 3158 fprintf(fp, "\n"); 3159 didType = true; 3160 } 3161 if (!didHeader) { 3162 if (pub) { 3163 fprintf(fp," <!-- PUBLIC SECTION. These resources have been declared public.\n"); 3164 fprintf(fp," Changes to these definitions will break binary compatibility. -->\n\n"); 3165 } else { 3166 fprintf(fp," <!-- PRIVATE SECTION. These resources have not been declared public.\n"); 3167 fprintf(fp," You can make them public my moving these lines into a file in res/values. -->\n\n"); 3168 } 3169 didHeader = true; 3170 } 3171 if (!pub) { 3172 const size_t NE = c->getEntries().size(); 3173 for (size_t k=0; k<NE; k++) { 3174 const SourcePos& pos = c->getEntries().valueAt(k)->getPos(); 3175 if (pos.file != "") { 3176 fprintf(fp," <!-- Declared at %s:%d -->\n", 3177 pos.file.string(), pos.line); 3178 } 3179 } 3180 } 3181 fprintf(fp, " <public type=\"%s\" name=\"%s\" id=\"0x%08x\" />\n", 3182 String8(t->getName()).string(), 3183 String8(c->getName()).string(), 3184 getResId(pkg, t, c->getEntryIndex())); 3185 } 3186 } 3187 } 3188} 3189 3190ResourceTable::Item::Item(const SourcePos& _sourcePos, 3191 bool _isId, 3192 const String16& _value, 3193 const Vector<StringPool::entry_style_span>* _style, 3194 int32_t _format) 3195 : sourcePos(_sourcePos) 3196 , isId(_isId) 3197 , value(_value) 3198 , format(_format) 3199 , bagKeyId(0) 3200 , evaluating(false) 3201{ 3202 if (_style) { 3203 style = *_style; 3204 } 3205} 3206 3207status_t ResourceTable::Entry::makeItABag(const SourcePos& sourcePos) 3208{ 3209 if (mType == TYPE_BAG) { 3210 return NO_ERROR; 3211 } 3212 if (mType == TYPE_UNKNOWN) { 3213 mType = TYPE_BAG; 3214 return NO_ERROR; 3215 } 3216 sourcePos.error("Resource entry %s is already defined as a single item.\n" 3217 "%s:%d: Originally defined here.\n", 3218 String8(mName).string(), 3219 mItem.sourcePos.file.string(), mItem.sourcePos.line); 3220 return UNKNOWN_ERROR; 3221} 3222 3223status_t ResourceTable::Entry::setItem(const SourcePos& sourcePos, 3224 const String16& value, 3225 const Vector<StringPool::entry_style_span>* style, 3226 int32_t format, 3227 const bool overwrite) 3228{ 3229 Item item(sourcePos, false, value, style); 3230 3231 if (mType == TYPE_BAG) { 3232 const Item& item(mBag.valueAt(0)); 3233 sourcePos.error("Resource entry %s is already defined as a bag.\n" 3234 "%s:%d: Originally defined here.\n", 3235 String8(mName).string(), 3236 item.sourcePos.file.string(), item.sourcePos.line); 3237 return UNKNOWN_ERROR; 3238 } 3239 if ( (mType != TYPE_UNKNOWN) && (overwrite == false) ) { 3240 sourcePos.error("Resource entry %s is already defined.\n" 3241 "%s:%d: Originally defined here.\n", 3242 String8(mName).string(), 3243 mItem.sourcePos.file.string(), mItem.sourcePos.line); 3244 return UNKNOWN_ERROR; 3245 } 3246 3247 mType = TYPE_ITEM; 3248 mItem = item; 3249 mItemFormat = format; 3250 return NO_ERROR; 3251} 3252 3253status_t ResourceTable::Entry::addToBag(const SourcePos& sourcePos, 3254 const String16& key, const String16& value, 3255 const Vector<StringPool::entry_style_span>* style, 3256 bool replace, bool isId, int32_t format) 3257{ 3258 status_t err = makeItABag(sourcePos); 3259 if (err != NO_ERROR) { 3260 return err; 3261 } 3262 3263 Item item(sourcePos, isId, value, style, format); 3264 3265 // XXX NOTE: there is an error if you try to have a bag with two keys, 3266 // one an attr and one an id, with the same name. Not something we 3267 // currently ever have to worry about. 3268 ssize_t origKey = mBag.indexOfKey(key); 3269 if (origKey >= 0) { 3270 if (!replace) { 3271 const Item& item(mBag.valueAt(origKey)); 3272 sourcePos.error("Resource entry %s already has bag item %s.\n" 3273 "%s:%d: Originally defined here.\n", 3274 String8(mName).string(), String8(key).string(), 3275 item.sourcePos.file.string(), item.sourcePos.line); 3276 return UNKNOWN_ERROR; 3277 } 3278 //printf("Replacing %s with %s\n", 3279 // String8(mBag.valueFor(key).value).string(), String8(value).string()); 3280 mBag.replaceValueFor(key, item); 3281 } 3282 3283 mBag.add(key, item); 3284 return NO_ERROR; 3285} 3286 3287status_t ResourceTable::Entry::emptyBag(const SourcePos& sourcePos) 3288{ 3289 status_t err = makeItABag(sourcePos); 3290 if (err != NO_ERROR) { 3291 return err; 3292 } 3293 3294 mBag.clear(); 3295 return NO_ERROR; 3296} 3297 3298status_t ResourceTable::Entry::generateAttributes(ResourceTable* table, 3299 const String16& package) 3300{ 3301 const String16 attr16("attr"); 3302 const String16 id16("id"); 3303 const size_t N = mBag.size(); 3304 for (size_t i=0; i<N; i++) { 3305 const String16& key = mBag.keyAt(i); 3306 const Item& it = mBag.valueAt(i); 3307 if (it.isId) { 3308 if (!table->hasBagOrEntry(key, &id16, &package)) { 3309 String16 value("false"); 3310 status_t err = table->addEntry(SourcePos(String8("<generated>"), 0), package, 3311 id16, key, value); 3312 if (err != NO_ERROR) { 3313 return err; 3314 } 3315 } 3316 } else if (!table->hasBagOrEntry(key, &attr16, &package)) { 3317 3318#if 1 3319// fprintf(stderr, "ERROR: Bag attribute '%s' has not been defined.\n", 3320// String8(key).string()); 3321// const Item& item(mBag.valueAt(i)); 3322// fprintf(stderr, "Referenced from file %s line %d\n", 3323// item.sourcePos.file.string(), item.sourcePos.line); 3324// return UNKNOWN_ERROR; 3325#else 3326 char numberStr[16]; 3327 sprintf(numberStr, "%d", ResTable_map::TYPE_ANY); 3328 status_t err = table->addBag(SourcePos("<generated>", 0), package, 3329 attr16, key, String16(""), 3330 String16("^type"), 3331 String16(numberStr), NULL, NULL); 3332 if (err != NO_ERROR) { 3333 return err; 3334 } 3335#endif 3336 } 3337 } 3338 return NO_ERROR; 3339} 3340 3341status_t ResourceTable::Entry::assignResourceIds(ResourceTable* table, 3342 const String16& package) 3343{ 3344 bool hasErrors = false; 3345 3346 if (mType == TYPE_BAG) { 3347 const char* errorMsg; 3348 const String16 style16("style"); 3349 const String16 attr16("attr"); 3350 const String16 id16("id"); 3351 mParentId = 0; 3352 if (mParent.size() > 0) { 3353 mParentId = table->getResId(mParent, &style16, NULL, &errorMsg); 3354 if (mParentId == 0) { 3355 mPos.error("Error retrieving parent for item: %s '%s'.\n", 3356 errorMsg, String8(mParent).string()); 3357 hasErrors = true; 3358 } 3359 } 3360 const size_t N = mBag.size(); 3361 for (size_t i=0; i<N; i++) { 3362 const String16& key = mBag.keyAt(i); 3363 Item& it = mBag.editValueAt(i); 3364 it.bagKeyId = table->getResId(key, 3365 it.isId ? &id16 : &attr16, NULL, &errorMsg); 3366 //printf("Bag key of %s: #%08x\n", String8(key).string(), it.bagKeyId); 3367 if (it.bagKeyId == 0) { 3368 it.sourcePos.error("Error: %s: %s '%s'.\n", errorMsg, 3369 String8(it.isId ? id16 : attr16).string(), 3370 String8(key).string()); 3371 hasErrors = true; 3372 } 3373 } 3374 } 3375 return hasErrors ? UNKNOWN_ERROR : NO_ERROR; 3376} 3377 3378status_t ResourceTable::Entry::prepareFlatten(StringPool* strings, ResourceTable* table, 3379 const String8* configTypeName, const ConfigDescription* config) 3380{ 3381 if (mType == TYPE_ITEM) { 3382 Item& it = mItem; 3383 AccessorCookie ac(it.sourcePos, String8(mName), String8(it.value)); 3384 if (!table->stringToValue(&it.parsedValue, strings, 3385 it.value, false, true, 0, 3386 &it.style, NULL, &ac, mItemFormat, 3387 configTypeName, config)) { 3388 return UNKNOWN_ERROR; 3389 } 3390 } else if (mType == TYPE_BAG) { 3391 const size_t N = mBag.size(); 3392 for (size_t i=0; i<N; i++) { 3393 const String16& key = mBag.keyAt(i); 3394 Item& it = mBag.editValueAt(i); 3395 AccessorCookie ac(it.sourcePos, String8(key), String8(it.value)); 3396 if (!table->stringToValue(&it.parsedValue, strings, 3397 it.value, false, true, it.bagKeyId, 3398 &it.style, NULL, &ac, it.format, 3399 configTypeName, config)) { 3400 return UNKNOWN_ERROR; 3401 } 3402 } 3403 } else { 3404 mPos.error("Error: entry %s is not a single item or a bag.\n", 3405 String8(mName).string()); 3406 return UNKNOWN_ERROR; 3407 } 3408 return NO_ERROR; 3409} 3410 3411status_t ResourceTable::Entry::remapStringValue(StringPool* strings) 3412{ 3413 if (mType == TYPE_ITEM) { 3414 Item& it = mItem; 3415 if (it.parsedValue.dataType == Res_value::TYPE_STRING) { 3416 it.parsedValue.data = strings->mapOriginalPosToNewPos(it.parsedValue.data); 3417 } 3418 } else if (mType == TYPE_BAG) { 3419 const size_t N = mBag.size(); 3420 for (size_t i=0; i<N; i++) { 3421 Item& it = mBag.editValueAt(i); 3422 if (it.parsedValue.dataType == Res_value::TYPE_STRING) { 3423 it.parsedValue.data = strings->mapOriginalPosToNewPos(it.parsedValue.data); 3424 } 3425 } 3426 } else { 3427 mPos.error("Error: entry %s is not a single item or a bag.\n", 3428 String8(mName).string()); 3429 return UNKNOWN_ERROR; 3430 } 3431 return NO_ERROR; 3432} 3433 3434ssize_t ResourceTable::Entry::flatten(Bundle* bundle, const sp<AaptFile>& data, bool isPublic) 3435{ 3436 size_t amt = 0; 3437 ResTable_entry header; 3438 memset(&header, 0, sizeof(header)); 3439 header.size = htods(sizeof(header)); 3440 const type ty = this != NULL ? mType : TYPE_ITEM; 3441 if (this != NULL) { 3442 if (ty == TYPE_BAG) { 3443 header.flags |= htods(header.FLAG_COMPLEX); 3444 } 3445 if (isPublic) { 3446 header.flags |= htods(header.FLAG_PUBLIC); 3447 } 3448 header.key.index = htodl(mNameIndex); 3449 } 3450 if (ty != TYPE_BAG) { 3451 status_t err = data->writeData(&header, sizeof(header)); 3452 if (err != NO_ERROR) { 3453 fprintf(stderr, "ERROR: out of memory creating ResTable_entry\n"); 3454 return err; 3455 } 3456 3457 const Item& it = mItem; 3458 Res_value par; 3459 memset(&par, 0, sizeof(par)); 3460 par.size = htods(it.parsedValue.size); 3461 par.dataType = it.parsedValue.dataType; 3462 par.res0 = it.parsedValue.res0; 3463 par.data = htodl(it.parsedValue.data); 3464 #if 0 3465 printf("Writing item (%s): type=%d, data=0x%x, res0=0x%x\n", 3466 String8(mName).string(), it.parsedValue.dataType, 3467 it.parsedValue.data, par.res0); 3468 #endif 3469 err = data->writeData(&par, it.parsedValue.size); 3470 if (err != NO_ERROR) { 3471 fprintf(stderr, "ERROR: out of memory creating Res_value\n"); 3472 return err; 3473 } 3474 amt += it.parsedValue.size; 3475 } else { 3476 size_t N = mBag.size(); 3477 size_t i; 3478 // Create correct ordering of items. 3479 KeyedVector<uint32_t, const Item*> items; 3480 for (i=0; i<N; i++) { 3481 const Item& it = mBag.valueAt(i); 3482 items.add(it.bagKeyId, &it); 3483 } 3484 N = items.size(); 3485 3486 ResTable_map_entry mapHeader; 3487 memcpy(&mapHeader, &header, sizeof(header)); 3488 mapHeader.size = htods(sizeof(mapHeader)); 3489 mapHeader.parent.ident = htodl(mParentId); 3490 mapHeader.count = htodl(N); 3491 status_t err = data->writeData(&mapHeader, sizeof(mapHeader)); 3492 if (err != NO_ERROR) { 3493 fprintf(stderr, "ERROR: out of memory creating ResTable_entry\n"); 3494 return err; 3495 } 3496 3497 for (i=0; i<N; i++) { 3498 const Item& it = *items.valueAt(i); 3499 ResTable_map map; 3500 map.name.ident = htodl(it.bagKeyId); 3501 map.value.size = htods(it.parsedValue.size); 3502 map.value.dataType = it.parsedValue.dataType; 3503 map.value.res0 = it.parsedValue.res0; 3504 map.value.data = htodl(it.parsedValue.data); 3505 err = data->writeData(&map, sizeof(map)); 3506 if (err != NO_ERROR) { 3507 fprintf(stderr, "ERROR: out of memory creating Res_value\n"); 3508 return err; 3509 } 3510 amt += sizeof(map); 3511 } 3512 } 3513 return amt; 3514} 3515 3516void ResourceTable::ConfigList::appendComment(const String16& comment, 3517 bool onlyIfEmpty) 3518{ 3519 if (comment.size() <= 0) { 3520 return; 3521 } 3522 if (onlyIfEmpty && mComment.size() > 0) { 3523 return; 3524 } 3525 if (mComment.size() > 0) { 3526 mComment.append(String16("\n")); 3527 } 3528 mComment.append(comment); 3529} 3530 3531void ResourceTable::ConfigList::appendTypeComment(const String16& comment) 3532{ 3533 if (comment.size() <= 0) { 3534 return; 3535 } 3536 if (mTypeComment.size() > 0) { 3537 mTypeComment.append(String16("\n")); 3538 } 3539 mTypeComment.append(comment); 3540} 3541 3542status_t ResourceTable::Type::addPublic(const SourcePos& sourcePos, 3543 const String16& name, 3544 const uint32_t ident) 3545{ 3546 #if 0 3547 int32_t entryIdx = Res_GETENTRY(ident); 3548 if (entryIdx < 0) { 3549 sourcePos.error("Public resource %s/%s has an invalid 0 identifier (0x%08x).\n", 3550 String8(mName).string(), String8(name).string(), ident); 3551 return UNKNOWN_ERROR; 3552 } 3553 #endif 3554 3555 int32_t typeIdx = Res_GETTYPE(ident); 3556 if (typeIdx >= 0) { 3557 typeIdx++; 3558 if (mPublicIndex > 0 && mPublicIndex != typeIdx) { 3559 sourcePos.error("Public resource %s/%s has conflicting type codes for its" 3560 " public identifiers (0x%x vs 0x%x).\n", 3561 String8(mName).string(), String8(name).string(), 3562 mPublicIndex, typeIdx); 3563 return UNKNOWN_ERROR; 3564 } 3565 mPublicIndex = typeIdx; 3566 } 3567 3568 if (mFirstPublicSourcePos == NULL) { 3569 mFirstPublicSourcePos = new SourcePos(sourcePos); 3570 } 3571 3572 if (mPublic.indexOfKey(name) < 0) { 3573 mPublic.add(name, Public(sourcePos, String16(), ident)); 3574 } else { 3575 Public& p = mPublic.editValueFor(name); 3576 if (p.ident != ident) { 3577 sourcePos.error("Public resource %s/%s has conflicting public identifiers" 3578 " (0x%08x vs 0x%08x).\n" 3579 "%s:%d: Originally defined here.\n", 3580 String8(mName).string(), String8(name).string(), p.ident, ident, 3581 p.sourcePos.file.string(), p.sourcePos.line); 3582 return UNKNOWN_ERROR; 3583 } 3584 } 3585 3586 return NO_ERROR; 3587} 3588 3589void ResourceTable::Type::canAddEntry(const String16& name) 3590{ 3591 mCanAddEntries.add(name); 3592} 3593 3594sp<ResourceTable::Entry> ResourceTable::Type::getEntry(const String16& entry, 3595 const SourcePos& sourcePos, 3596 const ResTable_config* config, 3597 bool doSetIndex, 3598 bool overlay, 3599 bool autoAddOverlay) 3600{ 3601 int pos = -1; 3602 sp<ConfigList> c = mConfigs.valueFor(entry); 3603 if (c == NULL) { 3604 if (overlay && !autoAddOverlay && mCanAddEntries.indexOf(entry) < 0) { 3605 sourcePos.error("Resource at %s appears in overlay but not" 3606 " in the base package; use <add-resource> to add.\n", 3607 String8(entry).string()); 3608 return NULL; 3609 } 3610 c = new ConfigList(entry, sourcePos); 3611 mConfigs.add(entry, c); 3612 pos = (int)mOrderedConfigs.size(); 3613 mOrderedConfigs.add(c); 3614 if (doSetIndex) { 3615 c->setEntryIndex(pos); 3616 } 3617 } 3618 3619 ConfigDescription cdesc; 3620 if (config) cdesc = *config; 3621 3622 sp<Entry> e = c->getEntries().valueFor(cdesc); 3623 if (e == NULL) { 3624 if (config != NULL) { 3625 NOISY(printf("New entry at %s:%d: imsi:%d/%d lang:%c%c cnt:%c%c " 3626 "orien:%d touch:%d density:%d key:%d inp:%d nav:%d sz:%dx%d " 3627 "sw%ddp w%ddp h%ddp dir:%d\n", 3628 sourcePos.file.string(), sourcePos.line, 3629 config->mcc, config->mnc, 3630 config->language[0] ? config->language[0] : '-', 3631 config->language[1] ? config->language[1] : '-', 3632 config->country[0] ? config->country[0] : '-', 3633 config->country[1] ? config->country[1] : '-', 3634 config->orientation, 3635 config->touchscreen, 3636 config->density, 3637 config->keyboard, 3638 config->inputFlags, 3639 config->navigation, 3640 config->screenWidth, 3641 config->screenHeight, 3642 config->smallestScreenWidthDp, 3643 config->screenWidthDp, 3644 config->screenHeightDp, 3645 config->layoutDirection)); 3646 } else { 3647 NOISY(printf("New entry at %s:%d: NULL config\n", 3648 sourcePos.file.string(), sourcePos.line)); 3649 } 3650 e = new Entry(entry, sourcePos); 3651 c->addEntry(cdesc, e); 3652 /* 3653 if (doSetIndex) { 3654 if (pos < 0) { 3655 for (pos=0; pos<(int)mOrderedConfigs.size(); pos++) { 3656 if (mOrderedConfigs[pos] == c) { 3657 break; 3658 } 3659 } 3660 if (pos >= (int)mOrderedConfigs.size()) { 3661 sourcePos.error("Internal error: config not found in mOrderedConfigs when adding entry"); 3662 return NULL; 3663 } 3664 } 3665 e->setEntryIndex(pos); 3666 } 3667 */ 3668 } 3669 3670 mUniqueConfigs.add(cdesc); 3671 3672 return e; 3673} 3674 3675status_t ResourceTable::Type::applyPublicEntryOrder() 3676{ 3677 size_t N = mOrderedConfigs.size(); 3678 Vector<sp<ConfigList> > origOrder(mOrderedConfigs); 3679 bool hasError = false; 3680 3681 size_t i; 3682 for (i=0; i<N; i++) { 3683 mOrderedConfigs.replaceAt(NULL, i); 3684 } 3685 3686 const size_t NP = mPublic.size(); 3687 //printf("Ordering %d configs from %d public defs\n", N, NP); 3688 size_t j; 3689 for (j=0; j<NP; j++) { 3690 const String16& name = mPublic.keyAt(j); 3691 const Public& p = mPublic.valueAt(j); 3692 int32_t idx = Res_GETENTRY(p.ident); 3693 //printf("Looking for entry \"%s\"/\"%s\" (0x%08x) in %d...\n", 3694 // String8(mName).string(), String8(name).string(), p.ident, N); 3695 bool found = false; 3696 for (i=0; i<N; i++) { 3697 sp<ConfigList> e = origOrder.itemAt(i); 3698 //printf("#%d: \"%s\"\n", i, String8(e->getName()).string()); 3699 if (e->getName() == name) { 3700 if (idx >= (int32_t)mOrderedConfigs.size()) { 3701 p.sourcePos.error("Public entry identifier 0x%x entry index " 3702 "is larger than available symbols (index %d, total symbols %d).\n", 3703 p.ident, idx, mOrderedConfigs.size()); 3704 hasError = true; 3705 } else if (mOrderedConfigs.itemAt(idx) == NULL) { 3706 e->setPublic(true); 3707 e->setPublicSourcePos(p.sourcePos); 3708 mOrderedConfigs.replaceAt(e, idx); 3709 origOrder.removeAt(i); 3710 N--; 3711 found = true; 3712 break; 3713 } else { 3714 sp<ConfigList> oe = mOrderedConfigs.itemAt(idx); 3715 3716 p.sourcePos.error("Multiple entry names declared for public entry" 3717 " identifier 0x%x in type %s (%s vs %s).\n" 3718 "%s:%d: Originally defined here.", 3719 idx+1, String8(mName).string(), 3720 String8(oe->getName()).string(), 3721 String8(name).string(), 3722 oe->getPublicSourcePos().file.string(), 3723 oe->getPublicSourcePos().line); 3724 hasError = true; 3725 } 3726 } 3727 } 3728 3729 if (!found) { 3730 p.sourcePos.error("Public symbol %s/%s declared here is not defined.", 3731 String8(mName).string(), String8(name).string()); 3732 hasError = true; 3733 } 3734 } 3735 3736 //printf("Copying back in %d non-public configs, have %d\n", N, origOrder.size()); 3737 3738 if (N != origOrder.size()) { 3739 printf("Internal error: remaining private symbol count mismatch\n"); 3740 N = origOrder.size(); 3741 } 3742 3743 j = 0; 3744 for (i=0; i<N; i++) { 3745 sp<ConfigList> e = origOrder.itemAt(i); 3746 // There will always be enough room for the remaining entries. 3747 while (mOrderedConfigs.itemAt(j) != NULL) { 3748 j++; 3749 } 3750 mOrderedConfigs.replaceAt(e, j); 3751 j++; 3752 } 3753 3754 return hasError ? UNKNOWN_ERROR : NO_ERROR; 3755} 3756 3757ResourceTable::Package::Package(const String16& name, ssize_t includedId) 3758 : mName(name), mIncludedId(includedId), 3759 mTypeStringsMapping(0xffffffff), 3760 mKeyStringsMapping(0xffffffff) 3761{ 3762} 3763 3764sp<ResourceTable::Type> ResourceTable::Package::getType(const String16& type, 3765 const SourcePos& sourcePos, 3766 bool doSetIndex) 3767{ 3768 sp<Type> t = mTypes.valueFor(type); 3769 if (t == NULL) { 3770 t = new Type(type, sourcePos); 3771 mTypes.add(type, t); 3772 mOrderedTypes.add(t); 3773 if (doSetIndex) { 3774 // For some reason the type's index is set to one plus the index 3775 // in the mOrderedTypes list, rather than just the index. 3776 t->setIndex(mOrderedTypes.size()); 3777 } 3778 } 3779 return t; 3780} 3781 3782status_t ResourceTable::Package::setTypeStrings(const sp<AaptFile>& data) 3783{ 3784 mTypeStringsData = data; 3785 status_t err = setStrings(data, &mTypeStrings, &mTypeStringsMapping); 3786 if (err != NO_ERROR) { 3787 fprintf(stderr, "ERROR: Type string data is corrupt!\n"); 3788 } 3789 return err; 3790} 3791 3792status_t ResourceTable::Package::setKeyStrings(const sp<AaptFile>& data) 3793{ 3794 mKeyStringsData = data; 3795 status_t err = setStrings(data, &mKeyStrings, &mKeyStringsMapping); 3796 if (err != NO_ERROR) { 3797 fprintf(stderr, "ERROR: Key string data is corrupt!\n"); 3798 } 3799 return err; 3800} 3801 3802status_t ResourceTable::Package::setStrings(const sp<AaptFile>& data, 3803 ResStringPool* strings, 3804 DefaultKeyedVector<String16, uint32_t>* mappings) 3805{ 3806 if (data->getData() == NULL) { 3807 return UNKNOWN_ERROR; 3808 } 3809 3810 NOISY(aout << "Setting restable string pool: " 3811 << HexDump(data->getData(), data->getSize()) << endl); 3812 3813 status_t err = strings->setTo(data->getData(), data->getSize()); 3814 if (err == NO_ERROR) { 3815 const size_t N = strings->size(); 3816 for (size_t i=0; i<N; i++) { 3817 size_t len; 3818 mappings->add(String16(strings->stringAt(i, &len)), i); 3819 } 3820 } 3821 return err; 3822} 3823 3824status_t ResourceTable::Package::applyPublicTypeOrder() 3825{ 3826 size_t N = mOrderedTypes.size(); 3827 Vector<sp<Type> > origOrder(mOrderedTypes); 3828 3829 size_t i; 3830 for (i=0; i<N; i++) { 3831 mOrderedTypes.replaceAt(NULL, i); 3832 } 3833 3834 for (i=0; i<N; i++) { 3835 sp<Type> t = origOrder.itemAt(i); 3836 int32_t idx = t->getPublicIndex(); 3837 if (idx > 0) { 3838 idx--; 3839 while (idx >= (int32_t)mOrderedTypes.size()) { 3840 mOrderedTypes.add(); 3841 } 3842 if (mOrderedTypes.itemAt(idx) != NULL) { 3843 sp<Type> ot = mOrderedTypes.itemAt(idx); 3844 t->getFirstPublicSourcePos().error("Multiple type names declared for public type" 3845 " identifier 0x%x (%s vs %s).\n" 3846 "%s:%d: Originally defined here.", 3847 idx, String8(ot->getName()).string(), 3848 String8(t->getName()).string(), 3849 ot->getFirstPublicSourcePos().file.string(), 3850 ot->getFirstPublicSourcePos().line); 3851 return UNKNOWN_ERROR; 3852 } 3853 mOrderedTypes.replaceAt(t, idx); 3854 origOrder.removeAt(i); 3855 i--; 3856 N--; 3857 } 3858 } 3859 3860 size_t j=0; 3861 for (i=0; i<N; i++) { 3862 sp<Type> t = origOrder.itemAt(i); 3863 // There will always be enough room for the remaining types. 3864 while (mOrderedTypes.itemAt(j) != NULL) { 3865 j++; 3866 } 3867 mOrderedTypes.replaceAt(t, j); 3868 } 3869 3870 return NO_ERROR; 3871} 3872 3873sp<ResourceTable::Package> ResourceTable::getPackage(const String16& package) 3874{ 3875 sp<Package> p = mPackages.valueFor(package); 3876 if (p == NULL) { 3877 if (mIsAppPackage) { 3878 if (mHaveAppPackage) { 3879 fprintf(stderr, "Adding multiple application package resources; only one is allowed.\n" 3880 "Use -x to create extended resources.\n"); 3881 return NULL; 3882 } 3883 mHaveAppPackage = true; 3884 p = new Package(package, 127); 3885 } else { 3886 p = new Package(package, mNextPackageId); 3887 } 3888 //printf("*** NEW PACKAGE: \"%s\" id=%d\n", 3889 // String8(package).string(), p->getAssignedId()); 3890 mPackages.add(package, p); 3891 mOrderedPackages.add(p); 3892 mNextPackageId++; 3893 } 3894 return p; 3895} 3896 3897sp<ResourceTable::Type> ResourceTable::getType(const String16& package, 3898 const String16& type, 3899 const SourcePos& sourcePos, 3900 bool doSetIndex) 3901{ 3902 sp<Package> p = getPackage(package); 3903 if (p == NULL) { 3904 return NULL; 3905 } 3906 return p->getType(type, sourcePos, doSetIndex); 3907} 3908 3909sp<ResourceTable::Entry> ResourceTable::getEntry(const String16& package, 3910 const String16& type, 3911 const String16& name, 3912 const SourcePos& sourcePos, 3913 bool overlay, 3914 const ResTable_config* config, 3915 bool doSetIndex) 3916{ 3917 sp<Type> t = getType(package, type, sourcePos, doSetIndex); 3918 if (t == NULL) { 3919 return NULL; 3920 } 3921 return t->getEntry(name, sourcePos, config, doSetIndex, overlay, mBundle->getAutoAddOverlay()); 3922} 3923 3924sp<const ResourceTable::Entry> ResourceTable::getEntry(uint32_t resID, 3925 const ResTable_config* config) const 3926{ 3927 int pid = Res_GETPACKAGE(resID)+1; 3928 const size_t N = mOrderedPackages.size(); 3929 size_t i; 3930 sp<Package> p; 3931 for (i=0; i<N; i++) { 3932 sp<Package> check = mOrderedPackages[i]; 3933 if (check->getAssignedId() == pid) { 3934 p = check; 3935 break; 3936 } 3937 3938 } 3939 if (p == NULL) { 3940 fprintf(stderr, "warning: Package not found for resource #%08x\n", resID); 3941 return NULL; 3942 } 3943 3944 int tid = Res_GETTYPE(resID); 3945 if (tid < 0 || tid >= (int)p->getOrderedTypes().size()) { 3946 fprintf(stderr, "warning: Type not found for resource #%08x\n", resID); 3947 return NULL; 3948 } 3949 sp<Type> t = p->getOrderedTypes()[tid]; 3950 3951 int eid = Res_GETENTRY(resID); 3952 if (eid < 0 || eid >= (int)t->getOrderedConfigs().size()) { 3953 fprintf(stderr, "warning: Entry not found for resource #%08x\n", resID); 3954 return NULL; 3955 } 3956 3957 sp<ConfigList> c = t->getOrderedConfigs()[eid]; 3958 if (c == NULL) { 3959 fprintf(stderr, "warning: Entry not found for resource #%08x\n", resID); 3960 return NULL; 3961 } 3962 3963 ConfigDescription cdesc; 3964 if (config) cdesc = *config; 3965 sp<Entry> e = c->getEntries().valueFor(cdesc); 3966 if (c == NULL) { 3967 fprintf(stderr, "warning: Entry configuration not found for resource #%08x\n", resID); 3968 return NULL; 3969 } 3970 3971 return e; 3972} 3973 3974const ResourceTable::Item* ResourceTable::getItem(uint32_t resID, uint32_t attrID) const 3975{ 3976 sp<const Entry> e = getEntry(resID); 3977 if (e == NULL) { 3978 return NULL; 3979 } 3980 3981 const size_t N = e->getBag().size(); 3982 for (size_t i=0; i<N; i++) { 3983 const Item& it = e->getBag().valueAt(i); 3984 if (it.bagKeyId == 0) { 3985 fprintf(stderr, "warning: ID not yet assigned to '%s' in bag '%s'\n", 3986 String8(e->getName()).string(), 3987 String8(e->getBag().keyAt(i)).string()); 3988 } 3989 if (it.bagKeyId == attrID) { 3990 return ⁢ 3991 } 3992 } 3993 3994 return NULL; 3995} 3996 3997bool ResourceTable::getItemValue( 3998 uint32_t resID, uint32_t attrID, Res_value* outValue) 3999{ 4000 const Item* item = getItem(resID, attrID); 4001 4002 bool res = false; 4003 if (item != NULL) { 4004 if (item->evaluating) { 4005 sp<const Entry> e = getEntry(resID); 4006 const size_t N = e->getBag().size(); 4007 size_t i; 4008 for (i=0; i<N; i++) { 4009 if (&e->getBag().valueAt(i) == item) { 4010 break; 4011 } 4012 } 4013 fprintf(stderr, "warning: Circular reference detected in key '%s' of bag '%s'\n", 4014 String8(e->getName()).string(), 4015 String8(e->getBag().keyAt(i)).string()); 4016 return false; 4017 } 4018 item->evaluating = true; 4019 res = stringToValue(outValue, NULL, item->value, false, false, item->bagKeyId); 4020 NOISY( 4021 if (res) { 4022 printf("getItemValue of #%08x[#%08x] (%s): type=#%08x, data=#%08x\n", 4023 resID, attrID, String8(getEntry(resID)->getName()).string(), 4024 outValue->dataType, outValue->data); 4025 } else { 4026 printf("getItemValue of #%08x[#%08x]: failed\n", 4027 resID, attrID); 4028 } 4029 ); 4030 item->evaluating = false; 4031 } 4032 return res; 4033} 4034