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