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