Resource.cpp revision a96cbb435d7b2197ab2b61fd98d14cbd6e0c5c3d
1// 2// Copyright 2006 The Android Open Source Project 3// 4// Build resource files from raw assets. 5// 6#include "Main.h" 7#include "AaptAssets.h" 8#include "StringPool.h" 9#include "XMLNode.h" 10#include "ResourceTable.h" 11#include "Images.h" 12 13#define NOISY(x) // x 14 15// ========================================================================== 16// ========================================================================== 17// ========================================================================== 18 19class PackageInfo 20{ 21public: 22 PackageInfo() 23 { 24 } 25 ~PackageInfo() 26 { 27 } 28 29 status_t parsePackage(const sp<AaptGroup>& grp); 30}; 31 32// ========================================================================== 33// ========================================================================== 34// ========================================================================== 35 36static String8 parseResourceName(const String8& leaf) 37{ 38 const char* firstDot = strchr(leaf.string(), '.'); 39 const char* str = leaf.string(); 40 41 if (firstDot) { 42 return String8(str, firstDot-str); 43 } else { 44 return String8(str); 45 } 46} 47 48class ResourceTypeSet : public RefBase, 49 public KeyedVector<String8,sp<AaptGroup> > 50{ 51public: 52 ResourceTypeSet(); 53}; 54 55ResourceTypeSet::ResourceTypeSet() 56 :RefBase(), 57 KeyedVector<String8,sp<AaptGroup> >() 58{ 59} 60 61class ResourceDirIterator 62{ 63public: 64 ResourceDirIterator(const sp<ResourceTypeSet>& set, const String8& resType) 65 : mResType(resType), mSet(set), mSetPos(0), mGroupPos(0) 66 { 67 } 68 69 inline const sp<AaptGroup>& getGroup() const { return mGroup; } 70 inline const sp<AaptFile>& getFile() const { return mFile; } 71 72 inline const String8& getBaseName() const { return mBaseName; } 73 inline const String8& getLeafName() const { return mLeafName; } 74 inline String8 getPath() const { return mPath; } 75 inline const ResTable_config& getParams() const { return mParams; } 76 77 enum { 78 EOD = 1 79 }; 80 81 ssize_t next() 82 { 83 while (true) { 84 sp<AaptGroup> group; 85 sp<AaptFile> file; 86 87 // Try to get next file in this current group. 88 if (mGroup != NULL && mGroupPos < mGroup->getFiles().size()) { 89 group = mGroup; 90 file = group->getFiles().valueAt(mGroupPos++); 91 92 // Try to get the next group/file in this directory 93 } else if (mSetPos < mSet->size()) { 94 mGroup = group = mSet->valueAt(mSetPos++); 95 if (group->getFiles().size() < 1) { 96 continue; 97 } 98 file = group->getFiles().valueAt(0); 99 mGroupPos = 1; 100 101 // All done! 102 } else { 103 return EOD; 104 } 105 106 mFile = file; 107 108 String8 leaf(group->getLeaf()); 109 mLeafName = String8(leaf); 110 mParams = file->getGroupEntry().toParams(); 111 NOISY(printf("Dir %s: mcc=%d mnc=%d lang=%c%c cnt=%c%c orient=%d density=%d touch=%d key=%d inp=%d nav=%d\n", 112 group->getPath().string(), mParams.mcc, mParams.mnc, 113 mParams.language[0] ? mParams.language[0] : '-', 114 mParams.language[1] ? mParams.language[1] : '-', 115 mParams.country[0] ? mParams.country[0] : '-', 116 mParams.country[1] ? mParams.country[1] : '-', 117 mParams.orientation, 118 mParams.density, mParams.touchscreen, mParams.keyboard, 119 mParams.inputFlags, mParams.navigation)); 120 mPath = "res"; 121 mPath.appendPath(file->getGroupEntry().toDirName(mResType)); 122 mPath.appendPath(leaf); 123 mBaseName = parseResourceName(leaf); 124 if (mBaseName == "") { 125 fprintf(stderr, "Error: malformed resource filename %s\n", 126 file->getPrintableSource().string()); 127 return UNKNOWN_ERROR; 128 } 129 130 NOISY(printf("file name=%s\n", mBaseName.string())); 131 132 return NO_ERROR; 133 } 134 } 135 136private: 137 String8 mResType; 138 139 const sp<ResourceTypeSet> mSet; 140 size_t mSetPos; 141 142 sp<AaptGroup> mGroup; 143 size_t mGroupPos; 144 145 sp<AaptFile> mFile; 146 String8 mBaseName; 147 String8 mLeafName; 148 String8 mPath; 149 ResTable_config mParams; 150}; 151 152// ========================================================================== 153// ========================================================================== 154// ========================================================================== 155 156bool isValidResourceType(const String8& type) 157{ 158 return type == "anim" || type == "drawable" || type == "layout" 159 || type == "values" || type == "xml" || type == "raw" 160 || type == "color" || type == "menu"; 161} 162 163static sp<AaptFile> getResourceFile(const sp<AaptAssets>& assets, bool makeIfNecessary=true) 164{ 165 sp<AaptGroup> group = assets->getFiles().valueFor(String8("resources.arsc")); 166 sp<AaptFile> file; 167 if (group != NULL) { 168 file = group->getFiles().valueFor(AaptGroupEntry()); 169 if (file != NULL) { 170 return file; 171 } 172 } 173 174 if (!makeIfNecessary) { 175 return NULL; 176 } 177 return assets->addFile(String8("resources.arsc"), AaptGroupEntry(), String8(), 178 NULL, String8()); 179} 180 181static status_t parsePackage(const sp<AaptAssets>& assets, const sp<AaptGroup>& grp) 182{ 183 if (grp->getFiles().size() != 1) { 184 fprintf(stderr, "WARNING: Multiple AndroidManifest.xml files found, using %s\n", 185 grp->getFiles().valueAt(0)->getPrintableSource().string()); 186 } 187 188 sp<AaptFile> file = grp->getFiles().valueAt(0); 189 190 ResXMLTree block; 191 status_t err = parseXMLResource(file, &block); 192 if (err != NO_ERROR) { 193 return err; 194 } 195 //printXMLBlock(&block); 196 197 ResXMLTree::event_code_t code; 198 while ((code=block.next()) != ResXMLTree::START_TAG 199 && code != ResXMLTree::END_DOCUMENT 200 && code != ResXMLTree::BAD_DOCUMENT) { 201 } 202 203 size_t len; 204 if (code != ResXMLTree::START_TAG) { 205 fprintf(stderr, "%s:%d: No start tag found\n", 206 file->getPrintableSource().string(), block.getLineNumber()); 207 return UNKNOWN_ERROR; 208 } 209 if (strcmp16(block.getElementName(&len), String16("manifest").string()) != 0) { 210 fprintf(stderr, "%s:%d: Invalid start tag %s, expected <manifest>\n", 211 file->getPrintableSource().string(), block.getLineNumber(), 212 String8(block.getElementName(&len)).string()); 213 return UNKNOWN_ERROR; 214 } 215 216 ssize_t nameIndex = block.indexOfAttribute(NULL, "package"); 217 if (nameIndex < 0) { 218 fprintf(stderr, "%s:%d: <manifest> does not have package attribute.\n", 219 file->getPrintableSource().string(), block.getLineNumber()); 220 return UNKNOWN_ERROR; 221 } 222 223 assets->setPackage(String8(block.getAttributeStringValue(nameIndex, &len))); 224 225 return NO_ERROR; 226} 227 228// ========================================================================== 229// ========================================================================== 230// ========================================================================== 231 232static status_t makeFileResources(Bundle* bundle, const sp<AaptAssets>& assets, 233 ResourceTable* table, 234 const sp<ResourceTypeSet>& set, 235 const char* resType) 236{ 237 String8 type8(resType); 238 String16 type16(resType); 239 240 bool hasErrors = false; 241 242 ResourceDirIterator it(set, String8(resType)); 243 ssize_t res; 244 while ((res=it.next()) == NO_ERROR) { 245 if (bundle->getVerbose()) { 246 printf(" (new resource id %s from %s)\n", 247 it.getBaseName().string(), it.getFile()->getPrintableSource().string()); 248 } 249 String16 baseName(it.getBaseName()); 250 const char16_t* str = baseName.string(); 251 const char16_t* const end = str + baseName.size(); 252 while (str < end) { 253 if (!((*str >= 'a' && *str <= 'z') 254 || (*str >= '0' && *str <= '9') 255 || *str == '_' || *str == '.')) { 256 fprintf(stderr, "%s: Invalid file name: must contain only [a-z0-9_.]\n", 257 it.getPath().string()); 258 hasErrors = true; 259 } 260 str++; 261 } 262 String8 resPath = it.getPath(); 263 resPath.convertToResPath(); 264 table->addEntry(SourcePos(it.getPath(), 0), String16(assets->getPackage()), 265 type16, 266 baseName, 267 String16(resPath), 268 NULL, 269 &it.getParams()); 270 assets->addResource(it.getLeafName(), resPath, it.getFile(), type8); 271 } 272 273 return hasErrors ? UNKNOWN_ERROR : NO_ERROR; 274} 275 276static status_t preProcessImages(Bundle* bundle, const sp<AaptAssets>& assets, 277 const sp<ResourceTypeSet>& set) 278{ 279 ResourceDirIterator it(set, String8("drawable")); 280 Vector<sp<AaptFile> > newNameFiles; 281 Vector<String8> newNamePaths; 282 ssize_t res; 283 while ((res=it.next()) == NO_ERROR) { 284 res = preProcessImage(bundle, assets, it.getFile(), NULL); 285 if (res != NO_ERROR) { 286 return res; 287 } 288 } 289 290 return NO_ERROR; 291} 292 293status_t postProcessImages(const sp<AaptAssets>& assets, 294 ResourceTable* table, 295 const sp<ResourceTypeSet>& set) 296{ 297 ResourceDirIterator it(set, String8("drawable")); 298 ssize_t res; 299 while ((res=it.next()) == NO_ERROR) { 300 res = postProcessImage(assets, table, it.getFile()); 301 if (res != NO_ERROR) { 302 return res; 303 } 304 } 305 306 return res < NO_ERROR ? res : (status_t)NO_ERROR; 307} 308 309static void collect_files(const sp<AaptDir>& dir, 310 KeyedVector<String8, sp<ResourceTypeSet> >* resources) 311{ 312 const DefaultKeyedVector<String8, sp<AaptGroup> >& groups = dir->getFiles(); 313 int N = groups.size(); 314 for (int i=0; i<N; i++) { 315 String8 leafName = groups.keyAt(i); 316 const sp<AaptGroup>& group = groups.valueAt(i); 317 318 const DefaultKeyedVector<AaptGroupEntry, sp<AaptFile> >& files 319 = group->getFiles(); 320 321 if (files.size() == 0) { 322 continue; 323 } 324 325 String8 resType = files.valueAt(0)->getResourceType(); 326 327 ssize_t index = resources->indexOfKey(resType); 328 329 if (index < 0) { 330 sp<ResourceTypeSet> set = new ResourceTypeSet(); 331 set->add(leafName, group); 332 resources->add(resType, set); 333 } else { 334 sp<ResourceTypeSet> set = resources->valueAt(index); 335 index = set->indexOfKey(leafName); 336 if (index < 0) { 337 set->add(leafName, group); 338 } else { 339 sp<AaptGroup> existingGroup = set->valueAt(index); 340 int M = files.size(); 341 for (int j=0; j<M; j++) { 342 existingGroup->addFile(files.valueAt(j)); 343 } 344 } 345 } 346 } 347} 348 349static void collect_files(const sp<AaptAssets>& ass, 350 KeyedVector<String8, sp<ResourceTypeSet> >* resources) 351{ 352 const Vector<sp<AaptDir> >& dirs = ass->resDirs(); 353 int N = dirs.size(); 354 355 for (int i=0; i<N; i++) { 356 sp<AaptDir> d = dirs.itemAt(i); 357 collect_files(d, resources); 358 359 // don't try to include the res dir 360 ass->removeDir(d->getLeaf()); 361 } 362} 363 364enum { 365 ATTR_OKAY = -1, 366 ATTR_NOT_FOUND = -2, 367 ATTR_LEADING_SPACES = -3, 368 ATTR_TRAILING_SPACES = -4 369}; 370static int validateAttr(const String8& path, const ResXMLParser& parser, 371 const char* ns, const char* attr, const char* validChars, bool required) 372{ 373 size_t len; 374 375 ssize_t index = parser.indexOfAttribute(ns, attr); 376 const uint16_t* str; 377 if (index >= 0 && (str=parser.getAttributeStringValue(index, &len)) != NULL) { 378 if (validChars) { 379 for (size_t i=0; i<len; i++) { 380 uint16_t c = str[i]; 381 const char* p = validChars; 382 bool okay = false; 383 while (*p) { 384 if (c == *p) { 385 okay = true; 386 break; 387 } 388 p++; 389 } 390 if (!okay) { 391 fprintf(stderr, "%s:%d: Tag <%s> attribute %s has invalid character '%c'.\n", 392 path.string(), parser.getLineNumber(), 393 String8(parser.getElementName(&len)).string(), attr, (char)str[i]); 394 return (int)i; 395 } 396 } 397 } 398 if (*str == ' ') { 399 fprintf(stderr, "%s:%d: Tag <%s> attribute %s can not start with a space.\n", 400 path.string(), parser.getLineNumber(), 401 String8(parser.getElementName(&len)).string(), attr); 402 return ATTR_LEADING_SPACES; 403 } 404 if (str[len-1] == ' ') { 405 fprintf(stderr, "%s:%d: Tag <%s> attribute %s can not end with a space.\n", 406 path.string(), parser.getLineNumber(), 407 String8(parser.getElementName(&len)).string(), attr); 408 return ATTR_TRAILING_SPACES; 409 } 410 return ATTR_OKAY; 411 } 412 if (required) { 413 fprintf(stderr, "%s:%d: Tag <%s> missing required attribute %s.\n", 414 path.string(), parser.getLineNumber(), 415 String8(parser.getElementName(&len)).string(), attr); 416 return ATTR_NOT_FOUND; 417 } 418 return ATTR_OKAY; 419} 420 421static void checkForIds(const String8& path, ResXMLParser& parser) 422{ 423 ResXMLTree::event_code_t code; 424 while ((code=parser.next()) != ResXMLTree::END_DOCUMENT 425 && code > ResXMLTree::BAD_DOCUMENT) { 426 if (code == ResXMLTree::START_TAG) { 427 ssize_t index = parser.indexOfAttribute(NULL, "id"); 428 if (index >= 0) { 429 fprintf(stderr, "%s:%d: WARNING: found plain 'id' attribute; did you mean the new 'android:id' name?\n", 430 path.string(), parser.getLineNumber()); 431 } 432 } 433 } 434} 435 436static void applyFileOverlay(const sp<AaptAssets>& assets, 437 const sp<ResourceTypeSet>& baseSet, 438 const char *resType) 439{ 440 // Replace any base level files in this category with any found from the overlay 441 // Also add any found only in the overlay. 442 sp<AaptAssets> overlay = assets->getOverlay(); 443 String8 resTypeString(resType); 444 445 // work through the linked list of overlays 446 while (overlay.get()) { 447 KeyedVector<String8, sp<ResourceTypeSet> >* overlayRes = overlay->getResources(); 448 449 // get the overlay resources of the requested type 450 ssize_t index = overlayRes->indexOfKey(resTypeString); 451 if (index >= 0) { 452 sp<ResourceTypeSet> overlaySet = overlayRes->valueAt(index); 453 454 // for each of the resources, check for a match in the previously built 455 // non-overlay "baseset". 456 size_t overlayCount = overlaySet->size(); 457 for (size_t overlayIndex=0; overlayIndex<overlayCount; overlayIndex++) { 458 size_t baseIndex = baseSet->indexOfKey(overlaySet->keyAt(overlayIndex)); 459 if (baseIndex != UNKNOWN_ERROR) { 460 // look for same flavor. For a given file (strings.xml, for example) 461 // there may be a locale specific or other flavors - we want to match 462 // the same flavor. 463 sp<AaptGroup> overlayGroup = overlaySet->valueAt(overlayIndex); 464 sp<AaptGroup> baseGroup = baseSet->valueAt(baseIndex); 465 466 DefaultKeyedVector<AaptGroupEntry, sp<AaptFile> > baseFiles = 467 baseGroup->getFiles(); 468 DefaultKeyedVector<AaptGroupEntry, sp<AaptFile> > overlayFiles = 469 overlayGroup->getFiles(); 470 size_t overlayGroupSize = overlayFiles.size(); 471 for (size_t overlayGroupIndex = 0; 472 overlayGroupIndex<overlayGroupSize; 473 overlayGroupIndex++) { 474 size_t baseFileIndex = 475 baseFiles.indexOfKey(overlayFiles.keyAt(overlayGroupIndex)); 476 if(baseFileIndex < UNKNOWN_ERROR) { 477 baseGroup->removeFile(baseFileIndex); 478 } else { 479 // didn't find a match fall through and add it.. 480 } 481 baseGroup->addFile(overlayFiles.valueAt(overlayGroupIndex)); 482 } 483 } else { 484 // this group doesn't exist (a file that's only in the overlay) 485 // add it 486 baseSet->add(overlaySet->keyAt(overlayIndex), 487 overlaySet->valueAt(overlayIndex)); 488 } 489 } 490 // this overlay didn't have resources for this type 491 } 492 // try next overlay 493 overlay = overlay->getOverlay(); 494 } 495 return; 496} 497 498void addTagAttribute(const sp<XMLNode>& node, const char* ns8, 499 const char* attr8, const char* value) 500{ 501 if (value == NULL) { 502 return; 503 } 504 505 const String16 ns(ns8); 506 const String16 attr(attr8); 507 508 if (node->getAttribute(ns, attr) != NULL) { 509 fprintf(stderr, "Warning: AndroidManifest.xml already defines %s (in %s)\n", 510 String8(attr).string(), String8(ns).string()); 511 return; 512 } 513 514 node->addAttribute(ns, attr, String16(value)); 515} 516 517status_t massageManifest(Bundle* bundle, sp<XMLNode> root) 518{ 519 root = root->searchElement(String16(), String16("manifest")); 520 if (root == NULL) { 521 fprintf(stderr, "No <manifest> tag.\n"); 522 return UNKNOWN_ERROR; 523 } 524 525 addTagAttribute(root, RESOURCES_ANDROID_NAMESPACE, "versionCode", 526 bundle->getVersionCode()); 527 addTagAttribute(root, RESOURCES_ANDROID_NAMESPACE, "versionName", 528 bundle->getVersionName()); 529 530 if (bundle->getMinSdkVersion() != NULL 531 || bundle->getTargetSdkVersion() != NULL 532 || bundle->getMaxSdkVersion() != NULL) { 533 sp<XMLNode> vers = root->getChildElement(String16(), String16("uses-sdk")); 534 if (vers == NULL) { 535 vers = XMLNode::newElement(root->getFilename(), String16(), String16("uses-sdk")); 536 root->insertChildAt(vers, 0); 537 } 538 539 addTagAttribute(vers, RESOURCES_ANDROID_NAMESPACE, "minSdkVersion", 540 bundle->getMinSdkVersion()); 541 addTagAttribute(vers, RESOURCES_ANDROID_NAMESPACE, "targetSdkVersion", 542 bundle->getTargetSdkVersion()); 543 addTagAttribute(vers, RESOURCES_ANDROID_NAMESPACE, "maxSdkVersion", 544 bundle->getMaxSdkVersion()); 545 } 546 547 return NO_ERROR; 548} 549 550#define ASSIGN_IT(n) \ 551 do { \ 552 ssize_t index = resources->indexOfKey(String8(#n)); \ 553 if (index >= 0) { \ 554 n ## s = resources->valueAt(index); \ 555 } \ 556 } while (0) 557 558status_t buildResources(Bundle* bundle, const sp<AaptAssets>& assets) 559{ 560 // First, look for a package file to parse. This is required to 561 // be able to generate the resource information. 562 sp<AaptGroup> androidManifestFile = 563 assets->getFiles().valueFor(String8("AndroidManifest.xml")); 564 if (androidManifestFile == NULL) { 565 fprintf(stderr, "ERROR: No AndroidManifest.xml file found.\n"); 566 return UNKNOWN_ERROR; 567 } 568 569 status_t err = parsePackage(assets, androidManifestFile); 570 if (err != NO_ERROR) { 571 return err; 572 } 573 574 NOISY(printf("Creating resources for package %s\n", 575 assets->getPackage().string())); 576 577 ResourceTable table(bundle, String16(assets->getPackage())); 578 err = table.addIncludedResources(bundle, assets); 579 if (err != NO_ERROR) { 580 return err; 581 } 582 583 NOISY(printf("Found %d included resource packages\n", (int)table.size())); 584 585 // -------------------------------------------------------------- 586 // First, gather all resource information. 587 // -------------------------------------------------------------- 588 589 // resType -> leafName -> group 590 KeyedVector<String8, sp<ResourceTypeSet> > *resources = 591 new KeyedVector<String8, sp<ResourceTypeSet> >; 592 collect_files(assets, resources); 593 594 sp<ResourceTypeSet> drawables; 595 sp<ResourceTypeSet> layouts; 596 sp<ResourceTypeSet> anims; 597 sp<ResourceTypeSet> xmls; 598 sp<ResourceTypeSet> raws; 599 sp<ResourceTypeSet> colors; 600 sp<ResourceTypeSet> menus; 601 602 ASSIGN_IT(drawable); 603 ASSIGN_IT(layout); 604 ASSIGN_IT(anim); 605 ASSIGN_IT(xml); 606 ASSIGN_IT(raw); 607 ASSIGN_IT(color); 608 ASSIGN_IT(menu); 609 610 assets->setResources(resources); 611 // now go through any resource overlays and collect their files 612 sp<AaptAssets> current = assets->getOverlay(); 613 while(current.get()) { 614 KeyedVector<String8, sp<ResourceTypeSet> > *resources = 615 new KeyedVector<String8, sp<ResourceTypeSet> >; 616 current->setResources(resources); 617 collect_files(current, resources); 618 current = current->getOverlay(); 619 } 620 // apply the overlay files to the base set 621 applyFileOverlay(assets, drawables, "drawable"); 622 applyFileOverlay(assets, layouts, "layout"); 623 applyFileOverlay(assets, anims, "anim"); 624 applyFileOverlay(assets, xmls, "xml"); 625 applyFileOverlay(assets, raws, "raw"); 626 applyFileOverlay(assets, colors, "color"); 627 applyFileOverlay(assets, menus, "menu"); 628 629 bool hasErrors = false; 630 631 if (drawables != NULL) { 632 err = preProcessImages(bundle, assets, drawables); 633 if (err == NO_ERROR) { 634 err = makeFileResources(bundle, assets, &table, drawables, "drawable"); 635 if (err != NO_ERROR) { 636 hasErrors = true; 637 } 638 } else { 639 hasErrors = true; 640 } 641 } 642 643 if (layouts != NULL) { 644 err = makeFileResources(bundle, assets, &table, layouts, "layout"); 645 if (err != NO_ERROR) { 646 hasErrors = true; 647 } 648 } 649 650 if (anims != NULL) { 651 err = makeFileResources(bundle, assets, &table, anims, "anim"); 652 if (err != NO_ERROR) { 653 hasErrors = true; 654 } 655 } 656 657 if (xmls != NULL) { 658 err = makeFileResources(bundle, assets, &table, xmls, "xml"); 659 if (err != NO_ERROR) { 660 hasErrors = true; 661 } 662 } 663 664 if (raws != NULL) { 665 err = makeFileResources(bundle, assets, &table, raws, "raw"); 666 if (err != NO_ERROR) { 667 hasErrors = true; 668 } 669 } 670 671 // compile resources 672 current = assets; 673 while(current.get()) { 674 KeyedVector<String8, sp<ResourceTypeSet> > *resources = 675 current->getResources(); 676 677 ssize_t index = resources->indexOfKey(String8("values")); 678 if (index >= 0) { 679 ResourceDirIterator it(resources->valueAt(index), String8("values")); 680 ssize_t res; 681 while ((res=it.next()) == NO_ERROR) { 682 sp<AaptFile> file = it.getFile(); 683 res = compileResourceFile(bundle, assets, file, it.getParams(), 684 (current!=assets), &table); 685 if (res != NO_ERROR) { 686 hasErrors = true; 687 } 688 } 689 } 690 current = current->getOverlay(); 691 } 692 693 if (colors != NULL) { 694 err = makeFileResources(bundle, assets, &table, colors, "color"); 695 if (err != NO_ERROR) { 696 hasErrors = true; 697 } 698 } 699 700 if (menus != NULL) { 701 err = makeFileResources(bundle, assets, &table, menus, "menu"); 702 if (err != NO_ERROR) { 703 hasErrors = true; 704 } 705 } 706 707 // -------------------------------------------------------------------- 708 // Assignment of resource IDs and initial generation of resource table. 709 // -------------------------------------------------------------------- 710 711 if (table.hasResources()) { 712 sp<AaptFile> resFile(getResourceFile(assets)); 713 if (resFile == NULL) { 714 fprintf(stderr, "Error: unable to generate entry for resource data\n"); 715 return UNKNOWN_ERROR; 716 } 717 718 err = table.assignResourceIds(); 719 if (err < NO_ERROR) { 720 return err; 721 } 722 } 723 724 // -------------------------------------------------------------- 725 // Finally, we can now we can compile XML files, which may reference 726 // resources. 727 // -------------------------------------------------------------- 728 729 if (layouts != NULL) { 730 ResourceDirIterator it(layouts, String8("layout")); 731 while ((err=it.next()) == NO_ERROR) { 732 String8 src = it.getFile()->getPrintableSource(); 733 err = compileXmlFile(assets, it.getFile(), &table); 734 if (err == NO_ERROR) { 735 ResXMLTree block; 736 block.setTo(it.getFile()->getData(), it.getFile()->getSize(), true); 737 checkForIds(src, block); 738 } else { 739 hasErrors = true; 740 } 741 } 742 743 if (err < NO_ERROR) { 744 hasErrors = true; 745 } 746 err = NO_ERROR; 747 } 748 749 if (anims != NULL) { 750 ResourceDirIterator it(anims, String8("anim")); 751 while ((err=it.next()) == NO_ERROR) { 752 err = compileXmlFile(assets, it.getFile(), &table); 753 if (err != NO_ERROR) { 754 hasErrors = true; 755 } 756 } 757 758 if (err < NO_ERROR) { 759 hasErrors = true; 760 } 761 err = NO_ERROR; 762 } 763 764 if (xmls != NULL) { 765 ResourceDirIterator it(xmls, String8("xml")); 766 while ((err=it.next()) == NO_ERROR) { 767 err = compileXmlFile(assets, it.getFile(), &table); 768 if (err != NO_ERROR) { 769 hasErrors = true; 770 } 771 } 772 773 if (err < NO_ERROR) { 774 hasErrors = true; 775 } 776 err = NO_ERROR; 777 } 778 779 if (drawables != NULL) { 780 err = postProcessImages(assets, &table, drawables); 781 if (err != NO_ERROR) { 782 hasErrors = true; 783 } 784 } 785 786 if (colors != NULL) { 787 ResourceDirIterator it(colors, String8("color")); 788 while ((err=it.next()) == NO_ERROR) { 789 err = compileXmlFile(assets, it.getFile(), &table); 790 if (err != NO_ERROR) { 791 hasErrors = true; 792 } 793 } 794 795 if (err < NO_ERROR) { 796 hasErrors = true; 797 } 798 err = NO_ERROR; 799 } 800 801 if (menus != NULL) { 802 ResourceDirIterator it(menus, String8("menu")); 803 while ((err=it.next()) == NO_ERROR) { 804 String8 src = it.getFile()->getPrintableSource(); 805 err = compileXmlFile(assets, it.getFile(), &table); 806 if (err != NO_ERROR) { 807 hasErrors = true; 808 } 809 ResXMLTree block; 810 block.setTo(it.getFile()->getData(), it.getFile()->getSize(), true); 811 checkForIds(src, block); 812 } 813 814 if (err < NO_ERROR) { 815 hasErrors = true; 816 } 817 err = NO_ERROR; 818 } 819 820 const sp<AaptFile> manifestFile(androidManifestFile->getFiles().valueAt(0)); 821 String8 manifestPath(manifestFile->getPrintableSource()); 822 823 // Perform a basic validation of the manifest file. This time we 824 // parse it with the comments intact, so that we can use them to 825 // generate java docs... so we are not going to write this one 826 // back out to the final manifest data. 827 err = compileXmlFile(assets, manifestFile, &table, 828 XML_COMPILE_ASSIGN_ATTRIBUTE_IDS 829 | XML_COMPILE_STRIP_WHITESPACE | XML_COMPILE_STRIP_RAW_VALUES); 830 if (err < NO_ERROR) { 831 return err; 832 } 833 ResXMLTree block; 834 block.setTo(manifestFile->getData(), manifestFile->getSize(), true); 835 String16 manifest16("manifest"); 836 String16 permission16("permission"); 837 String16 permission_group16("permission-group"); 838 String16 uses_permission16("uses-permission"); 839 String16 instrumentation16("instrumentation"); 840 String16 application16("application"); 841 String16 provider16("provider"); 842 String16 service16("service"); 843 String16 receiver16("receiver"); 844 String16 activity16("activity"); 845 String16 action16("action"); 846 String16 category16("category"); 847 String16 data16("scheme"); 848 const char* packageIdentChars = "abcdefghijklmnopqrstuvwxyz" 849 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789"; 850 const char* packageIdentCharsWithTheStupid = "abcdefghijklmnopqrstuvwxyz" 851 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789-"; 852 const char* classIdentChars = "abcdefghijklmnopqrstuvwxyz" 853 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789$"; 854 const char* processIdentChars = "abcdefghijklmnopqrstuvwxyz" 855 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789:"; 856 const char* authoritiesIdentChars = "abcdefghijklmnopqrstuvwxyz" 857 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789-:;"; 858 const char* typeIdentChars = "abcdefghijklmnopqrstuvwxyz" 859 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789:-/*+"; 860 const char* schemeIdentChars = "abcdefghijklmnopqrstuvwxyz" 861 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789-"; 862 ResXMLTree::event_code_t code; 863 sp<AaptSymbols> permissionSymbols; 864 sp<AaptSymbols> permissionGroupSymbols; 865 while ((code=block.next()) != ResXMLTree::END_DOCUMENT 866 && code > ResXMLTree::BAD_DOCUMENT) { 867 if (code == ResXMLTree::START_TAG) { 868 size_t len; 869 if (block.getElementNamespace(&len) != NULL) { 870 continue; 871 } 872 if (strcmp16(block.getElementName(&len), manifest16.string()) == 0) { 873 if (validateAttr(manifestPath, block, NULL, "package", 874 packageIdentChars, true) != ATTR_OKAY) { 875 hasErrors = true; 876 } 877 } else if (strcmp16(block.getElementName(&len), permission16.string()) == 0 878 || strcmp16(block.getElementName(&len), permission_group16.string()) == 0) { 879 const bool isGroup = strcmp16(block.getElementName(&len), 880 permission_group16.string()) == 0; 881 if (validateAttr(manifestPath, block, RESOURCES_ANDROID_NAMESPACE, "name", 882 isGroup ? packageIdentCharsWithTheStupid 883 : packageIdentChars, true) != ATTR_OKAY) { 884 hasErrors = true; 885 } 886 SourcePos srcPos(manifestPath, block.getLineNumber()); 887 sp<AaptSymbols> syms; 888 if (!isGroup) { 889 syms = permissionSymbols; 890 if (syms == NULL) { 891 sp<AaptSymbols> symbols = 892 assets->getSymbolsFor(String8("Manifest")); 893 syms = permissionSymbols = symbols->addNestedSymbol( 894 String8("permission"), srcPos); 895 } 896 } else { 897 syms = permissionGroupSymbols; 898 if (syms == NULL) { 899 sp<AaptSymbols> symbols = 900 assets->getSymbolsFor(String8("Manifest")); 901 syms = permissionGroupSymbols = symbols->addNestedSymbol( 902 String8("permission_group"), srcPos); 903 } 904 } 905 size_t len; 906 ssize_t index = block.indexOfAttribute(RESOURCES_ANDROID_NAMESPACE, "name"); 907 const uint16_t* id = block.getAttributeStringValue(index, &len); 908 if (id == NULL) { 909 fprintf(stderr, "%s:%d: missing name attribute in element <%s>.\n", 910 manifestPath.string(), block.getLineNumber(), 911 String8(block.getElementName(&len)).string()); 912 hasErrors = true; 913 break; 914 } 915 String8 idStr(id); 916 char* p = idStr.lockBuffer(idStr.size()); 917 char* e = p + idStr.size(); 918 bool begins_with_digit = true; // init to true so an empty string fails 919 while (e > p) { 920 e--; 921 if (*e >= '0' && *e <= '9') { 922 begins_with_digit = true; 923 continue; 924 } 925 if ((*e >= 'a' && *e <= 'z') || 926 (*e >= 'A' && *e <= 'Z') || 927 (*e == '_')) { 928 begins_with_digit = false; 929 continue; 930 } 931 if (isGroup && (*e == '-')) { 932 *e = '_'; 933 begins_with_digit = false; 934 continue; 935 } 936 e++; 937 break; 938 } 939 idStr.unlockBuffer(); 940 // verify that we stopped because we hit a period or 941 // the beginning of the string, and that the 942 // identifier didn't begin with a digit. 943 if (begins_with_digit || (e != p && *(e-1) != '.')) { 944 fprintf(stderr, 945 "%s:%d: Permission name <%s> is not a valid Java symbol\n", 946 manifestPath.string(), block.getLineNumber(), idStr.string()); 947 hasErrors = true; 948 } 949 syms->addStringSymbol(String8(e), idStr, srcPos); 950 const uint16_t* cmt = block.getComment(&len); 951 if (cmt != NULL && *cmt != 0) { 952 //printf("Comment of %s: %s\n", String8(e).string(), 953 // String8(cmt).string()); 954 syms->appendComment(String8(e), String16(cmt), srcPos); 955 } else { 956 //printf("No comment for %s\n", String8(e).string()); 957 } 958 syms->makeSymbolPublic(String8(e), srcPos); 959 } else if (strcmp16(block.getElementName(&len), uses_permission16.string()) == 0) { 960 if (validateAttr(manifestPath, block, RESOURCES_ANDROID_NAMESPACE, "name", 961 packageIdentChars, true) != ATTR_OKAY) { 962 hasErrors = true; 963 } 964 } else if (strcmp16(block.getElementName(&len), instrumentation16.string()) == 0) { 965 if (validateAttr(manifestPath, block, RESOURCES_ANDROID_NAMESPACE, "name", 966 classIdentChars, true) != ATTR_OKAY) { 967 hasErrors = true; 968 } 969 if (validateAttr(manifestPath, block, 970 RESOURCES_ANDROID_NAMESPACE, "targetPackage", 971 packageIdentChars, true) != ATTR_OKAY) { 972 hasErrors = true; 973 } 974 } else if (strcmp16(block.getElementName(&len), application16.string()) == 0) { 975 if (validateAttr(manifestPath, block, RESOURCES_ANDROID_NAMESPACE, "name", 976 classIdentChars, false) != ATTR_OKAY) { 977 hasErrors = true; 978 } 979 if (validateAttr(manifestPath, block, 980 RESOURCES_ANDROID_NAMESPACE, "permission", 981 packageIdentChars, false) != ATTR_OKAY) { 982 hasErrors = true; 983 } 984 if (validateAttr(manifestPath, block, 985 RESOURCES_ANDROID_NAMESPACE, "process", 986 processIdentChars, false) != ATTR_OKAY) { 987 hasErrors = true; 988 } 989 if (validateAttr(manifestPath, block, 990 RESOURCES_ANDROID_NAMESPACE, "taskAffinity", 991 processIdentChars, false) != ATTR_OKAY) { 992 hasErrors = true; 993 } 994 } else if (strcmp16(block.getElementName(&len), provider16.string()) == 0) { 995 if (validateAttr(manifestPath, block, RESOURCES_ANDROID_NAMESPACE, "name", 996 classIdentChars, true) != ATTR_OKAY) { 997 hasErrors = true; 998 } 999 if (validateAttr(manifestPath, block, 1000 RESOURCES_ANDROID_NAMESPACE, "authorities", 1001 authoritiesIdentChars, true) != ATTR_OKAY) { 1002 hasErrors = true; 1003 } 1004 if (validateAttr(manifestPath, block, 1005 RESOURCES_ANDROID_NAMESPACE, "permission", 1006 packageIdentChars, false) != ATTR_OKAY) { 1007 hasErrors = true; 1008 } 1009 if (validateAttr(manifestPath, block, 1010 RESOURCES_ANDROID_NAMESPACE, "process", 1011 processIdentChars, false) != ATTR_OKAY) { 1012 hasErrors = true; 1013 } 1014 } else if (strcmp16(block.getElementName(&len), service16.string()) == 0 1015 || strcmp16(block.getElementName(&len), receiver16.string()) == 0 1016 || strcmp16(block.getElementName(&len), activity16.string()) == 0) { 1017 if (validateAttr(manifestPath, block, RESOURCES_ANDROID_NAMESPACE, "name", 1018 classIdentChars, true) != ATTR_OKAY) { 1019 hasErrors = true; 1020 } 1021 if (validateAttr(manifestPath, block, 1022 RESOURCES_ANDROID_NAMESPACE, "permission", 1023 packageIdentChars, false) != ATTR_OKAY) { 1024 hasErrors = true; 1025 } 1026 if (validateAttr(manifestPath, block, 1027 RESOURCES_ANDROID_NAMESPACE, "process", 1028 processIdentChars, false) != ATTR_OKAY) { 1029 hasErrors = true; 1030 } 1031 if (validateAttr(manifestPath, block, 1032 RESOURCES_ANDROID_NAMESPACE, "taskAffinity", 1033 processIdentChars, false) != ATTR_OKAY) { 1034 hasErrors = true; 1035 } 1036 } else if (strcmp16(block.getElementName(&len), action16.string()) == 0 1037 || strcmp16(block.getElementName(&len), category16.string()) == 0) { 1038 if (validateAttr(manifestPath, block, 1039 RESOURCES_ANDROID_NAMESPACE, "name", 1040 packageIdentChars, true) != ATTR_OKAY) { 1041 hasErrors = true; 1042 } 1043 } else if (strcmp16(block.getElementName(&len), data16.string()) == 0) { 1044 if (validateAttr(manifestPath, block, 1045 RESOURCES_ANDROID_NAMESPACE, "mimeType", 1046 typeIdentChars, true) != ATTR_OKAY) { 1047 hasErrors = true; 1048 } 1049 if (validateAttr(manifestPath, block, 1050 RESOURCES_ANDROID_NAMESPACE, "scheme", 1051 schemeIdentChars, true) != ATTR_OKAY) { 1052 hasErrors = true; 1053 } 1054 } 1055 } 1056 } 1057 1058 if (table.validateLocalizations()) { 1059 hasErrors = true; 1060 } 1061 1062 if (hasErrors) { 1063 return UNKNOWN_ERROR; 1064 } 1065 1066 // Generate final compiled manifest file. 1067 manifestFile->clearData(); 1068 sp<XMLNode> manifestTree = XMLNode::parse(manifestFile); 1069 if (manifestTree == NULL) { 1070 return UNKNOWN_ERROR; 1071 } 1072 err = massageManifest(bundle, manifestTree); 1073 if (err < NO_ERROR) { 1074 return err; 1075 } 1076 err = compileXmlFile(assets, manifestTree, manifestFile, &table); 1077 if (err < NO_ERROR) { 1078 return err; 1079 } 1080 1081 //block.restart(); 1082 //printXMLBlock(&block); 1083 1084 // -------------------------------------------------------------- 1085 // Generate the final resource table. 1086 // Re-flatten because we may have added new resource IDs 1087 // -------------------------------------------------------------- 1088 1089 if (table.hasResources()) { 1090 sp<AaptSymbols> symbols = assets->getSymbolsFor(String8("R")); 1091 err = table.addSymbols(symbols); 1092 if (err < NO_ERROR) { 1093 return err; 1094 } 1095 1096 sp<AaptFile> resFile(getResourceFile(assets)); 1097 if (resFile == NULL) { 1098 fprintf(stderr, "Error: unable to generate entry for resource data\n"); 1099 return UNKNOWN_ERROR; 1100 } 1101 1102 err = table.flatten(bundle, resFile); 1103 if (err < NO_ERROR) { 1104 return err; 1105 } 1106 1107 if (bundle->getPublicOutputFile()) { 1108 FILE* fp = fopen(bundle->getPublicOutputFile(), "w+"); 1109 if (fp == NULL) { 1110 fprintf(stderr, "ERROR: Unable to open public definitions output file %s: %s\n", 1111 (const char*)bundle->getPublicOutputFile(), strerror(errno)); 1112 return UNKNOWN_ERROR; 1113 } 1114 if (bundle->getVerbose()) { 1115 printf(" Writing public definitions to %s.\n", bundle->getPublicOutputFile()); 1116 } 1117 table.writePublicDefinitions(String16(assets->getPackage()), fp); 1118 } 1119 1120 NOISY( 1121 ResTable rt; 1122 rt.add(resFile->getData(), resFile->getSize(), NULL); 1123 printf("Generated resources:\n"); 1124 rt.print(); 1125 ) 1126 1127 // These resources are now considered to be a part of the included 1128 // resources, for others to reference. 1129 err = assets->addIncludedResources(resFile); 1130 if (err < NO_ERROR) { 1131 fprintf(stderr, "ERROR: Unable to parse generated resources, aborting.\n"); 1132 return err; 1133 } 1134 } 1135 1136 return err; 1137} 1138 1139static const char* getIndentSpace(int indent) 1140{ 1141static const char whitespace[] = 1142" "; 1143 1144 return whitespace + sizeof(whitespace) - 1 - indent*4; 1145} 1146 1147static status_t fixupSymbol(String16* inoutSymbol) 1148{ 1149 inoutSymbol->replaceAll('.', '_'); 1150 inoutSymbol->replaceAll(':', '_'); 1151 return NO_ERROR; 1152} 1153 1154static String16 getAttributeComment(const sp<AaptAssets>& assets, 1155 const String8& name, 1156 String16* outTypeComment = NULL) 1157{ 1158 sp<AaptSymbols> asym = assets->getSymbolsFor(String8("R")); 1159 if (asym != NULL) { 1160 //printf("Got R symbols!\n"); 1161 asym = asym->getNestedSymbols().valueFor(String8("attr")); 1162 if (asym != NULL) { 1163 //printf("Got attrs symbols! comment %s=%s\n", 1164 // name.string(), String8(asym->getComment(name)).string()); 1165 if (outTypeComment != NULL) { 1166 *outTypeComment = asym->getTypeComment(name); 1167 } 1168 return asym->getComment(name); 1169 } 1170 } 1171 return String16(); 1172} 1173 1174static status_t writeLayoutClasses( 1175 FILE* fp, const sp<AaptAssets>& assets, 1176 const sp<AaptSymbols>& symbols, int indent, bool includePrivate) 1177{ 1178 const char* indentStr = getIndentSpace(indent); 1179 if (!includePrivate) { 1180 fprintf(fp, "%s/** @doconly */\n", indentStr); 1181 } 1182 fprintf(fp, "%spublic static final class styleable {\n", indentStr); 1183 indent++; 1184 1185 String16 attr16("attr"); 1186 String16 package16(assets->getPackage()); 1187 1188 indentStr = getIndentSpace(indent); 1189 bool hasErrors = false; 1190 1191 size_t i; 1192 size_t N = symbols->getNestedSymbols().size(); 1193 for (i=0; i<N; i++) { 1194 sp<AaptSymbols> nsymbols = symbols->getNestedSymbols().valueAt(i); 1195 String16 nclassName16(symbols->getNestedSymbols().keyAt(i)); 1196 String8 realClassName(nclassName16); 1197 if (fixupSymbol(&nclassName16) != NO_ERROR) { 1198 hasErrors = true; 1199 } 1200 String8 nclassName(nclassName16); 1201 1202 SortedVector<uint32_t> idents; 1203 Vector<uint32_t> origOrder; 1204 Vector<bool> publicFlags; 1205 1206 size_t a; 1207 size_t NA = nsymbols->getSymbols().size(); 1208 for (a=0; a<NA; a++) { 1209 const AaptSymbolEntry& sym(nsymbols->getSymbols().valueAt(a)); 1210 int32_t code = sym.typeCode == AaptSymbolEntry::TYPE_INT32 1211 ? sym.int32Val : 0; 1212 bool isPublic = true; 1213 if (code == 0) { 1214 String16 name16(sym.name); 1215 uint32_t typeSpecFlags; 1216 code = assets->getIncludedResources().identifierForName( 1217 name16.string(), name16.size(), 1218 attr16.string(), attr16.size(), 1219 package16.string(), package16.size(), &typeSpecFlags); 1220 if (code == 0) { 1221 fprintf(stderr, "ERROR: In <declare-styleable> %s, unable to find attribute %s\n", 1222 nclassName.string(), sym.name.string()); 1223 hasErrors = true; 1224 } 1225 isPublic = (typeSpecFlags&ResTable_typeSpec::SPEC_PUBLIC) != 0; 1226 } 1227 idents.add(code); 1228 origOrder.add(code); 1229 publicFlags.add(isPublic); 1230 } 1231 1232 NA = idents.size(); 1233 1234 String16 comment = symbols->getComment(realClassName); 1235 fprintf(fp, "%s/** ", indentStr); 1236 if (comment.size() > 0) { 1237 fprintf(fp, "%s\n", String8(comment).string()); 1238 } else { 1239 fprintf(fp, "Attributes that can be used with a %s.\n", nclassName.string()); 1240 } 1241 bool hasTable = false; 1242 for (a=0; a<NA; a++) { 1243 ssize_t pos = idents.indexOf(origOrder.itemAt(a)); 1244 if (pos >= 0) { 1245 if (!hasTable) { 1246 hasTable = true; 1247 fprintf(fp, 1248 "%s <p>Includes the following attributes:</p>\n" 1249 "%s <table border=\"2\" width=\"85%%\" align=\"center\" frame=\"hsides\" rules=\"all\" cellpadding=\"5\">\n" 1250 "%s <colgroup align=\"left\" />\n" 1251 "%s <colgroup align=\"left\" />\n" 1252 "%s <tr><th>Attribute<th>Summary</tr>\n", 1253 indentStr, 1254 indentStr, 1255 indentStr, 1256 indentStr, 1257 indentStr); 1258 } 1259 const AaptSymbolEntry& sym = nsymbols->getSymbols().valueAt(a); 1260 if (!publicFlags.itemAt(a) && !includePrivate) { 1261 continue; 1262 } 1263 String8 name8(sym.name); 1264 String16 comment(sym.comment); 1265 if (comment.size() <= 0) { 1266 comment = getAttributeComment(assets, name8); 1267 } 1268 if (comment.size() > 0) { 1269 const char16_t* p = comment.string(); 1270 while (*p != 0 && *p != '.') { 1271 if (*p == '{') { 1272 while (*p != 0 && *p != '}') { 1273 p++; 1274 } 1275 } else { 1276 p++; 1277 } 1278 } 1279 if (*p == '.') { 1280 p++; 1281 } 1282 comment = String16(comment.string(), p-comment.string()); 1283 } 1284 String16 name(name8); 1285 fixupSymbol(&name); 1286 fprintf(fp, "%s <tr><th><code>{@link #%s_%s %s:%s}</code><td>%s</tr>\n", 1287 indentStr, nclassName.string(), 1288 String8(name).string(), 1289 assets->getPackage().string(), 1290 String8(name).string(), 1291 String8(comment).string()); 1292 } 1293 } 1294 if (hasTable) { 1295 fprintf(fp, "%s </table>\n", indentStr); 1296 } 1297 for (a=0; a<NA; a++) { 1298 ssize_t pos = idents.indexOf(origOrder.itemAt(a)); 1299 if (pos >= 0) { 1300 const AaptSymbolEntry& sym = nsymbols->getSymbols().valueAt(a); 1301 if (!publicFlags.itemAt(a) && !includePrivate) { 1302 continue; 1303 } 1304 String16 name(sym.name); 1305 fixupSymbol(&name); 1306 fprintf(fp, "%s @see #%s_%s\n", 1307 indentStr, nclassName.string(), 1308 String8(name).string()); 1309 } 1310 } 1311 fprintf(fp, "%s */\n", getIndentSpace(indent)); 1312 1313 fprintf(fp, 1314 "%spublic static final int[] %s = {\n" 1315 "%s", 1316 indentStr, nclassName.string(), 1317 getIndentSpace(indent+1)); 1318 1319 for (a=0; a<NA; a++) { 1320 if (a != 0) { 1321 if ((a&3) == 0) { 1322 fprintf(fp, ",\n%s", getIndentSpace(indent+1)); 1323 } else { 1324 fprintf(fp, ", "); 1325 } 1326 } 1327 fprintf(fp, "0x%08x", idents[a]); 1328 } 1329 1330 fprintf(fp, "\n%s};\n", indentStr); 1331 1332 for (a=0; a<NA; a++) { 1333 ssize_t pos = idents.indexOf(origOrder.itemAt(a)); 1334 if (pos >= 0) { 1335 const AaptSymbolEntry& sym = nsymbols->getSymbols().valueAt(a); 1336 if (!publicFlags.itemAt(a) && !includePrivate) { 1337 continue; 1338 } 1339 String8 name8(sym.name); 1340 String16 comment(sym.comment); 1341 String16 typeComment; 1342 if (comment.size() <= 0) { 1343 comment = getAttributeComment(assets, name8, &typeComment); 1344 } else { 1345 getAttributeComment(assets, name8, &typeComment); 1346 } 1347 String16 name(name8); 1348 if (fixupSymbol(&name) != NO_ERROR) { 1349 hasErrors = true; 1350 } 1351 1352 uint32_t typeSpecFlags = 0; 1353 String16 name16(sym.name); 1354 assets->getIncludedResources().identifierForName( 1355 name16.string(), name16.size(), 1356 attr16.string(), attr16.size(), 1357 package16.string(), package16.size(), &typeSpecFlags); 1358 //printf("%s:%s/%s: 0x%08x\n", String8(package16).string(), 1359 // String8(attr16).string(), String8(name16).string(), typeSpecFlags); 1360 const bool pub = (typeSpecFlags&ResTable_typeSpec::SPEC_PUBLIC) != 0; 1361 1362 fprintf(fp, "%s/**\n", indentStr); 1363 if (comment.size() > 0) { 1364 fprintf(fp, "%s <p>\n%s @attr description\n", indentStr, indentStr); 1365 fprintf(fp, "%s %s\n", indentStr, String8(comment).string()); 1366 } else { 1367 fprintf(fp, 1368 "%s <p>This symbol is the offset where the {@link %s.R.attr#%s}\n" 1369 "%s attribute's value can be found in the {@link #%s} array.\n", 1370 indentStr, 1371 pub ? assets->getPackage().string() 1372 : assets->getSymbolsPrivatePackage().string(), 1373 String8(name).string(), 1374 indentStr, nclassName.string()); 1375 } 1376 if (typeComment.size() > 0) { 1377 fprintf(fp, "\n\n%s %s\n", indentStr, String8(typeComment).string()); 1378 } 1379 if (comment.size() > 0) { 1380 if (pub) { 1381 fprintf(fp, 1382 "%s <p>This corresponds to the global attribute" 1383 "%s resource symbol {@link %s.R.attr#%s}.\n", 1384 indentStr, indentStr, 1385 assets->getPackage().string(), 1386 String8(name).string()); 1387 } else { 1388 fprintf(fp, 1389 "%s <p>This is a private symbol.\n", indentStr); 1390 } 1391 } 1392 fprintf(fp, "%s @attr name %s:%s\n", indentStr, 1393 "android", String8(name).string()); 1394 fprintf(fp, "%s*/\n", indentStr); 1395 fprintf(fp, 1396 "%spublic static final int %s_%s = %d;\n", 1397 indentStr, nclassName.string(), 1398 String8(name).string(), (int)pos); 1399 } 1400 } 1401 } 1402 1403 indent--; 1404 fprintf(fp, "%s};\n", getIndentSpace(indent)); 1405 return hasErrors ? UNKNOWN_ERROR : NO_ERROR; 1406} 1407 1408static status_t writeSymbolClass( 1409 FILE* fp, const sp<AaptAssets>& assets, bool includePrivate, 1410 const sp<AaptSymbols>& symbols, const String8& className, int indent) 1411{ 1412 fprintf(fp, "%spublic %sfinal class %s {\n", 1413 getIndentSpace(indent), 1414 indent != 0 ? "static " : "", className.string()); 1415 indent++; 1416 1417 size_t i; 1418 status_t err = NO_ERROR; 1419 1420 size_t N = symbols->getSymbols().size(); 1421 for (i=0; i<N; i++) { 1422 const AaptSymbolEntry& sym = symbols->getSymbols().valueAt(i); 1423 if (sym.typeCode != AaptSymbolEntry::TYPE_INT32) { 1424 continue; 1425 } 1426 if (!includePrivate && !sym.isPublic) { 1427 continue; 1428 } 1429 String16 name(sym.name); 1430 String8 realName(name); 1431 if (fixupSymbol(&name) != NO_ERROR) { 1432 return UNKNOWN_ERROR; 1433 } 1434 String16 comment(sym.comment); 1435 bool haveComment = false; 1436 if (comment.size() > 0) { 1437 haveComment = true; 1438 fprintf(fp, 1439 "%s/** %s\n", 1440 getIndentSpace(indent), String8(comment).string()); 1441 } else if (sym.isPublic && !includePrivate) { 1442 sym.sourcePos.warning("No comment for public symbol %s:%s/%s", 1443 assets->getPackage().string(), className.string(), 1444 String8(sym.name).string()); 1445 } 1446 String16 typeComment(sym.typeComment); 1447 if (typeComment.size() > 0) { 1448 if (!haveComment) { 1449 haveComment = true; 1450 fprintf(fp, 1451 "%s/** %s\n", 1452 getIndentSpace(indent), String8(typeComment).string()); 1453 } else { 1454 fprintf(fp, 1455 "%s %s\n", 1456 getIndentSpace(indent), String8(typeComment).string()); 1457 } 1458 } 1459 if (haveComment) { 1460 fprintf(fp,"%s */\n", getIndentSpace(indent)); 1461 } 1462 fprintf(fp, "%spublic static final int %s=0x%08x;\n", 1463 getIndentSpace(indent), 1464 String8(name).string(), (int)sym.int32Val); 1465 } 1466 1467 for (i=0; i<N; i++) { 1468 const AaptSymbolEntry& sym = symbols->getSymbols().valueAt(i); 1469 if (sym.typeCode != AaptSymbolEntry::TYPE_STRING) { 1470 continue; 1471 } 1472 if (!includePrivate && !sym.isPublic) { 1473 continue; 1474 } 1475 String16 name(sym.name); 1476 if (fixupSymbol(&name) != NO_ERROR) { 1477 return UNKNOWN_ERROR; 1478 } 1479 String16 comment(sym.comment); 1480 if (comment.size() > 0) { 1481 fprintf(fp, 1482 "%s/** %s\n" 1483 "%s */\n", 1484 getIndentSpace(indent), String8(comment).string(), 1485 getIndentSpace(indent)); 1486 } else if (sym.isPublic && !includePrivate) { 1487 sym.sourcePos.warning("No comment for public symbol %s:%s/%s", 1488 assets->getPackage().string(), className.string(), 1489 String8(sym.name).string()); 1490 } 1491 fprintf(fp, "%spublic static final String %s=\"%s\";\n", 1492 getIndentSpace(indent), 1493 String8(name).string(), sym.stringVal.string()); 1494 } 1495 1496 sp<AaptSymbols> styleableSymbols; 1497 1498 N = symbols->getNestedSymbols().size(); 1499 for (i=0; i<N; i++) { 1500 sp<AaptSymbols> nsymbols = symbols->getNestedSymbols().valueAt(i); 1501 String8 nclassName(symbols->getNestedSymbols().keyAt(i)); 1502 if (nclassName == "styleable") { 1503 styleableSymbols = nsymbols; 1504 } else { 1505 err = writeSymbolClass(fp, assets, includePrivate, nsymbols, nclassName, indent); 1506 } 1507 if (err != NO_ERROR) { 1508 return err; 1509 } 1510 } 1511 1512 if (styleableSymbols != NULL) { 1513 err = writeLayoutClasses(fp, assets, styleableSymbols, indent, includePrivate); 1514 if (err != NO_ERROR) { 1515 return err; 1516 } 1517 } 1518 1519 indent--; 1520 fprintf(fp, "%s}\n", getIndentSpace(indent)); 1521 return NO_ERROR; 1522} 1523 1524status_t writeResourceSymbols(Bundle* bundle, const sp<AaptAssets>& assets, 1525 const String8& package, bool includePrivate) 1526{ 1527 if (!bundle->getRClassDir()) { 1528 return NO_ERROR; 1529 } 1530 1531 const size_t N = assets->getSymbols().size(); 1532 for (size_t i=0; i<N; i++) { 1533 sp<AaptSymbols> symbols = assets->getSymbols().valueAt(i); 1534 String8 className(assets->getSymbols().keyAt(i)); 1535 String8 dest(bundle->getRClassDir()); 1536 if (bundle->getMakePackageDirs()) { 1537 String8 pkg(package); 1538 const char* last = pkg.string(); 1539 const char* s = last-1; 1540 do { 1541 s++; 1542 if (s > last && (*s == '.' || *s == 0)) { 1543 String8 part(last, s-last); 1544 dest.appendPath(part); 1545#ifdef HAVE_MS_C_RUNTIME 1546 _mkdir(dest.string()); 1547#else 1548 mkdir(dest.string(), S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP); 1549#endif 1550 last = s+1; 1551 } 1552 } while (*s); 1553 } 1554 dest.appendPath(className); 1555 dest.append(".java"); 1556 FILE* fp = fopen(dest.string(), "w+"); 1557 if (fp == NULL) { 1558 fprintf(stderr, "ERROR: Unable to open class file %s: %s\n", 1559 dest.string(), strerror(errno)); 1560 return UNKNOWN_ERROR; 1561 } 1562 if (bundle->getVerbose()) { 1563 printf(" Writing symbols for class %s.\n", className.string()); 1564 } 1565 1566 fprintf(fp, 1567 "/* AUTO-GENERATED FILE. DO NOT MODIFY.\n" 1568 " *\n" 1569 " * This class was automatically generated by the\n" 1570 " * aapt tool from the resource data it found. It\n" 1571 " * should not be modified by hand.\n" 1572 " */\n" 1573 "\n" 1574 "package %s;\n\n", package.string()); 1575 1576 status_t err = writeSymbolClass(fp, assets, includePrivate, symbols, className, 0); 1577 if (err != NO_ERROR) { 1578 return err; 1579 } 1580 fclose(fp); 1581 } 1582 1583 return NO_ERROR; 1584} 1585