Resource.cpp revision 6a1fadea851db236331b3dfe1f3c1e482556057f
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 491#define ASSIGN_IT(n) \ 492 do { \ 493 ssize_t index = resources->indexOfKey(String8(#n)); \ 494 if (index >= 0) { \ 495 n ## s = resources->valueAt(index); \ 496 } \ 497 } while (0) 498 499status_t buildResources(Bundle* bundle, const sp<AaptAssets>& assets) 500{ 501 // First, look for a package file to parse. This is required to 502 // be able to generate the resource information. 503 sp<AaptGroup> androidManifestFile = 504 assets->getFiles().valueFor(String8("AndroidManifest.xml")); 505 if (androidManifestFile == NULL) { 506 fprintf(stderr, "ERROR: No AndroidManifest.xml file found.\n"); 507 return UNKNOWN_ERROR; 508 } 509 510 status_t err = parsePackage(assets, androidManifestFile); 511 if (err != NO_ERROR) { 512 return err; 513 } 514 515 NOISY(printf("Creating resources for package %s\n", 516 assets->getPackage().string())); 517 518 ResourceTable table(bundle, String16(assets->getPackage())); 519 err = table.addIncludedResources(bundle, assets); 520 if (err != NO_ERROR) { 521 return err; 522 } 523 524 NOISY(printf("Found %d included resource packages\n", (int)table.size())); 525 526 // -------------------------------------------------------------- 527 // First, gather all resource information. 528 // -------------------------------------------------------------- 529 530 // resType -> leafName -> group 531 KeyedVector<String8, sp<ResourceTypeSet> > *resources = 532 new KeyedVector<String8, sp<ResourceTypeSet> >; 533 collect_files(assets, resources); 534 535 sp<ResourceTypeSet> drawables; 536 sp<ResourceTypeSet> layouts; 537 sp<ResourceTypeSet> anims; 538 sp<ResourceTypeSet> xmls; 539 sp<ResourceTypeSet> raws; 540 sp<ResourceTypeSet> colors; 541 sp<ResourceTypeSet> menus; 542 543 ASSIGN_IT(drawable); 544 ASSIGN_IT(layout); 545 ASSIGN_IT(anim); 546 ASSIGN_IT(xml); 547 ASSIGN_IT(raw); 548 ASSIGN_IT(color); 549 ASSIGN_IT(menu); 550 551 assets->setResources(resources); 552 // now go through any resource overlays and collect their files 553 sp<AaptAssets> current = assets->getOverlay(); 554 while(current.get()) { 555 KeyedVector<String8, sp<ResourceTypeSet> > *resources = 556 new KeyedVector<String8, sp<ResourceTypeSet> >; 557 current->setResources(resources); 558 collect_files(current, resources); 559 current = current->getOverlay(); 560 } 561 // apply the overlay files to the base set 562 applyFileOverlay(assets, drawables, "drawable"); 563 applyFileOverlay(assets, layouts, "layout"); 564 applyFileOverlay(assets, anims, "anim"); 565 applyFileOverlay(assets, xmls, "xml"); 566 applyFileOverlay(assets, raws, "raw"); 567 applyFileOverlay(assets, colors, "color"); 568 applyFileOverlay(assets, menus, "menu"); 569 570 bool hasErrors = false; 571 572 if (drawables != NULL) { 573 err = preProcessImages(bundle, assets, drawables); 574 if (err == NO_ERROR) { 575 err = makeFileResources(bundle, assets, &table, drawables, "drawable"); 576 if (err != NO_ERROR) { 577 hasErrors = true; 578 } 579 } else { 580 hasErrors = true; 581 } 582 } 583 584 if (layouts != NULL) { 585 err = makeFileResources(bundle, assets, &table, layouts, "layout"); 586 if (err != NO_ERROR) { 587 hasErrors = true; 588 } 589 } 590 591 if (anims != NULL) { 592 err = makeFileResources(bundle, assets, &table, anims, "anim"); 593 if (err != NO_ERROR) { 594 hasErrors = true; 595 } 596 } 597 598 if (xmls != NULL) { 599 err = makeFileResources(bundle, assets, &table, xmls, "xml"); 600 if (err != NO_ERROR) { 601 hasErrors = true; 602 } 603 } 604 605 if (raws != NULL) { 606 err = makeFileResources(bundle, assets, &table, raws, "raw"); 607 if (err != NO_ERROR) { 608 hasErrors = true; 609 } 610 } 611 612 // compile resources 613 current = assets; 614 while(current.get()) { 615 KeyedVector<String8, sp<ResourceTypeSet> > *resources = 616 current->getResources(); 617 618 ssize_t index = resources->indexOfKey(String8("values")); 619 if (index >= 0) { 620 ResourceDirIterator it(resources->valueAt(index), String8("values")); 621 ssize_t res; 622 while ((res=it.next()) == NO_ERROR) { 623 sp<AaptFile> file = it.getFile(); 624 res = compileResourceFile(bundle, assets, file, it.getParams(), 625 (current!=assets), &table); 626 if (res != NO_ERROR) { 627 hasErrors = true; 628 } 629 } 630 } 631 current = current->getOverlay(); 632 } 633 634 if (colors != NULL) { 635 err = makeFileResources(bundle, assets, &table, colors, "color"); 636 if (err != NO_ERROR) { 637 hasErrors = true; 638 } 639 } 640 641 if (menus != NULL) { 642 err = makeFileResources(bundle, assets, &table, menus, "menu"); 643 if (err != NO_ERROR) { 644 hasErrors = true; 645 } 646 } 647 648 // -------------------------------------------------------------------- 649 // Assignment of resource IDs and initial generation of resource table. 650 // -------------------------------------------------------------------- 651 652 if (table.hasResources()) { 653 sp<AaptFile> resFile(getResourceFile(assets)); 654 if (resFile == NULL) { 655 fprintf(stderr, "Error: unable to generate entry for resource data\n"); 656 return UNKNOWN_ERROR; 657 } 658 659 err = table.assignResourceIds(); 660 if (err < NO_ERROR) { 661 return err; 662 } 663 } 664 665 // -------------------------------------------------------------- 666 // Finally, we can now we can compile XML files, which may reference 667 // resources. 668 // -------------------------------------------------------------- 669 670 if (layouts != NULL) { 671 ResourceDirIterator it(layouts, String8("layout")); 672 while ((err=it.next()) == NO_ERROR) { 673 String8 src = it.getFile()->getPrintableSource(); 674 err = compileXmlFile(assets, it.getFile(), &table); 675 if (err == NO_ERROR) { 676 ResXMLTree block; 677 block.setTo(it.getFile()->getData(), it.getFile()->getSize(), true); 678 checkForIds(src, block); 679 } else { 680 hasErrors = true; 681 } 682 } 683 684 if (err < NO_ERROR) { 685 hasErrors = true; 686 } 687 err = NO_ERROR; 688 } 689 690 if (anims != NULL) { 691 ResourceDirIterator it(anims, String8("anim")); 692 while ((err=it.next()) == NO_ERROR) { 693 err = compileXmlFile(assets, it.getFile(), &table); 694 if (err != NO_ERROR) { 695 hasErrors = true; 696 } 697 } 698 699 if (err < NO_ERROR) { 700 hasErrors = true; 701 } 702 err = NO_ERROR; 703 } 704 705 if (xmls != NULL) { 706 ResourceDirIterator it(xmls, String8("xml")); 707 while ((err=it.next()) == NO_ERROR) { 708 err = compileXmlFile(assets, it.getFile(), &table); 709 if (err != NO_ERROR) { 710 hasErrors = true; 711 } 712 } 713 714 if (err < NO_ERROR) { 715 hasErrors = true; 716 } 717 err = NO_ERROR; 718 } 719 720 if (drawables != NULL) { 721 err = postProcessImages(assets, &table, drawables); 722 if (err != NO_ERROR) { 723 hasErrors = true; 724 } 725 } 726 727 if (colors != NULL) { 728 ResourceDirIterator it(colors, String8("color")); 729 while ((err=it.next()) == NO_ERROR) { 730 err = compileXmlFile(assets, it.getFile(), &table); 731 if (err != NO_ERROR) { 732 hasErrors = true; 733 } 734 } 735 736 if (err < NO_ERROR) { 737 hasErrors = true; 738 } 739 err = NO_ERROR; 740 } 741 742 if (menus != NULL) { 743 ResourceDirIterator it(menus, String8("menu")); 744 while ((err=it.next()) == NO_ERROR) { 745 String8 src = it.getFile()->getPrintableSource(); 746 err = compileXmlFile(assets, it.getFile(), &table); 747 if (err != NO_ERROR) { 748 hasErrors = true; 749 } 750 ResXMLTree block; 751 block.setTo(it.getFile()->getData(), it.getFile()->getSize(), true); 752 checkForIds(src, block); 753 } 754 755 if (err < NO_ERROR) { 756 hasErrors = true; 757 } 758 err = NO_ERROR; 759 } 760 761 const sp<AaptFile> manifestFile(androidManifestFile->getFiles().valueAt(0)); 762 String8 manifestPath(manifestFile->getPrintableSource()); 763 764 // Perform a basic validation of the manifest file. This time we 765 // parse it with the comments intact, so that we can use them to 766 // generate java docs... so we are not going to write this one 767 // back out to the final manifest data. 768 err = compileXmlFile(assets, manifestFile, &table, 769 XML_COMPILE_ASSIGN_ATTRIBUTE_IDS 770 | XML_COMPILE_STRIP_WHITESPACE | XML_COMPILE_STRIP_RAW_VALUES); 771 if (err < NO_ERROR) { 772 return err; 773 } 774 ResXMLTree block; 775 block.setTo(manifestFile->getData(), manifestFile->getSize(), true); 776 String16 manifest16("manifest"); 777 String16 permission16("permission"); 778 String16 permission_group16("permission-group"); 779 String16 uses_permission16("uses-permission"); 780 String16 instrumentation16("instrumentation"); 781 String16 application16("application"); 782 String16 provider16("provider"); 783 String16 service16("service"); 784 String16 receiver16("receiver"); 785 String16 activity16("activity"); 786 String16 action16("action"); 787 String16 category16("category"); 788 String16 data16("scheme"); 789 const char* packageIdentChars = "abcdefghijklmnopqrstuvwxyz" 790 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789"; 791 const char* packageIdentCharsWithTheStupid = "abcdefghijklmnopqrstuvwxyz" 792 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789-"; 793 const char* classIdentChars = "abcdefghijklmnopqrstuvwxyz" 794 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789$"; 795 const char* processIdentChars = "abcdefghijklmnopqrstuvwxyz" 796 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789:"; 797 const char* authoritiesIdentChars = "abcdefghijklmnopqrstuvwxyz" 798 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789-:;"; 799 const char* typeIdentChars = "abcdefghijklmnopqrstuvwxyz" 800 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789:-/*+"; 801 const char* schemeIdentChars = "abcdefghijklmnopqrstuvwxyz" 802 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789-"; 803 ResXMLTree::event_code_t code; 804 sp<AaptSymbols> permissionSymbols; 805 sp<AaptSymbols> permissionGroupSymbols; 806 while ((code=block.next()) != ResXMLTree::END_DOCUMENT 807 && code > ResXMLTree::BAD_DOCUMENT) { 808 if (code == ResXMLTree::START_TAG) { 809 size_t len; 810 if (block.getElementNamespace(&len) != NULL) { 811 continue; 812 } 813 if (strcmp16(block.getElementName(&len), manifest16.string()) == 0) { 814 if (validateAttr(manifestPath, block, NULL, "package", 815 packageIdentChars, true) != ATTR_OKAY) { 816 hasErrors = true; 817 } 818 } else if (strcmp16(block.getElementName(&len), permission16.string()) == 0 819 || strcmp16(block.getElementName(&len), permission_group16.string()) == 0) { 820 const bool isGroup = strcmp16(block.getElementName(&len), 821 permission_group16.string()) == 0; 822 if (validateAttr(manifestPath, block, RESOURCES_ANDROID_NAMESPACE, "name", 823 isGroup ? packageIdentCharsWithTheStupid 824 : packageIdentChars, true) != ATTR_OKAY) { 825 hasErrors = true; 826 } 827 SourcePos srcPos(manifestPath, block.getLineNumber()); 828 sp<AaptSymbols> syms; 829 if (!isGroup) { 830 syms = permissionSymbols; 831 if (syms == NULL) { 832 sp<AaptSymbols> symbols = 833 assets->getSymbolsFor(String8("Manifest")); 834 syms = permissionSymbols = symbols->addNestedSymbol( 835 String8("permission"), srcPos); 836 } 837 } else { 838 syms = permissionGroupSymbols; 839 if (syms == NULL) { 840 sp<AaptSymbols> symbols = 841 assets->getSymbolsFor(String8("Manifest")); 842 syms = permissionGroupSymbols = symbols->addNestedSymbol( 843 String8("permission_group"), srcPos); 844 } 845 } 846 size_t len; 847 ssize_t index = block.indexOfAttribute(RESOURCES_ANDROID_NAMESPACE, "name"); 848 const uint16_t* id = block.getAttributeStringValue(index, &len); 849 if (id == NULL) { 850 fprintf(stderr, "%s:%d: missing name attribute in element <%s>.\n", 851 manifestPath.string(), block.getLineNumber(), 852 String8(block.getElementName(&len)).string()); 853 hasErrors = true; 854 break; 855 } 856 String8 idStr(id); 857 char* p = idStr.lockBuffer(idStr.size()); 858 char* e = p + idStr.size(); 859 bool begins_with_digit = true; // init to true so an empty string fails 860 while (e > p) { 861 e--; 862 if (*e >= '0' && *e <= '9') { 863 begins_with_digit = true; 864 continue; 865 } 866 if ((*e >= 'a' && *e <= 'z') || 867 (*e >= 'A' && *e <= 'Z') || 868 (*e == '_')) { 869 begins_with_digit = false; 870 continue; 871 } 872 if (isGroup && (*e == '-')) { 873 *e = '_'; 874 begins_with_digit = false; 875 continue; 876 } 877 e++; 878 break; 879 } 880 idStr.unlockBuffer(); 881 // verify that we stopped because we hit a period or 882 // the beginning of the string, and that the 883 // identifier didn't begin with a digit. 884 if (begins_with_digit || (e != p && *(e-1) != '.')) { 885 fprintf(stderr, 886 "%s:%d: Permission name <%s> is not a valid Java symbol\n", 887 manifestPath.string(), block.getLineNumber(), idStr.string()); 888 hasErrors = true; 889 } 890 syms->addStringSymbol(String8(e), idStr, srcPos); 891 const uint16_t* cmt = block.getComment(&len); 892 if (cmt != NULL && *cmt != 0) { 893 //printf("Comment of %s: %s\n", String8(e).string(), 894 // String8(cmt).string()); 895 syms->appendComment(String8(e), String16(cmt), srcPos); 896 } else { 897 //printf("No comment for %s\n", String8(e).string()); 898 } 899 syms->makeSymbolPublic(String8(e), srcPos); 900 } else if (strcmp16(block.getElementName(&len), uses_permission16.string()) == 0) { 901 if (validateAttr(manifestPath, block, RESOURCES_ANDROID_NAMESPACE, "name", 902 packageIdentChars, true) != ATTR_OKAY) { 903 hasErrors = true; 904 } 905 } else if (strcmp16(block.getElementName(&len), instrumentation16.string()) == 0) { 906 if (validateAttr(manifestPath, block, RESOURCES_ANDROID_NAMESPACE, "name", 907 classIdentChars, true) != ATTR_OKAY) { 908 hasErrors = true; 909 } 910 if (validateAttr(manifestPath, block, 911 RESOURCES_ANDROID_NAMESPACE, "targetPackage", 912 packageIdentChars, true) != ATTR_OKAY) { 913 hasErrors = true; 914 } 915 } else if (strcmp16(block.getElementName(&len), application16.string()) == 0) { 916 if (validateAttr(manifestPath, block, RESOURCES_ANDROID_NAMESPACE, "name", 917 classIdentChars, false) != ATTR_OKAY) { 918 hasErrors = true; 919 } 920 if (validateAttr(manifestPath, block, 921 RESOURCES_ANDROID_NAMESPACE, "permission", 922 packageIdentChars, false) != ATTR_OKAY) { 923 hasErrors = true; 924 } 925 if (validateAttr(manifestPath, block, 926 RESOURCES_ANDROID_NAMESPACE, "process", 927 processIdentChars, false) != ATTR_OKAY) { 928 hasErrors = true; 929 } 930 if (validateAttr(manifestPath, block, 931 RESOURCES_ANDROID_NAMESPACE, "taskAffinity", 932 processIdentChars, false) != ATTR_OKAY) { 933 hasErrors = true; 934 } 935 } else if (strcmp16(block.getElementName(&len), provider16.string()) == 0) { 936 if (validateAttr(manifestPath, block, RESOURCES_ANDROID_NAMESPACE, "name", 937 classIdentChars, true) != ATTR_OKAY) { 938 hasErrors = true; 939 } 940 if (validateAttr(manifestPath, block, 941 RESOURCES_ANDROID_NAMESPACE, "authorities", 942 authoritiesIdentChars, true) != ATTR_OKAY) { 943 hasErrors = true; 944 } 945 if (validateAttr(manifestPath, block, 946 RESOURCES_ANDROID_NAMESPACE, "permission", 947 packageIdentChars, false) != ATTR_OKAY) { 948 hasErrors = true; 949 } 950 if (validateAttr(manifestPath, block, 951 RESOURCES_ANDROID_NAMESPACE, "process", 952 processIdentChars, false) != ATTR_OKAY) { 953 hasErrors = true; 954 } 955 } else if (strcmp16(block.getElementName(&len), service16.string()) == 0 956 || strcmp16(block.getElementName(&len), receiver16.string()) == 0 957 || strcmp16(block.getElementName(&len), activity16.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, "permission", 964 packageIdentChars, false) != ATTR_OKAY) { 965 hasErrors = true; 966 } 967 if (validateAttr(manifestPath, block, 968 RESOURCES_ANDROID_NAMESPACE, "process", 969 processIdentChars, false) != ATTR_OKAY) { 970 hasErrors = true; 971 } 972 if (validateAttr(manifestPath, block, 973 RESOURCES_ANDROID_NAMESPACE, "taskAffinity", 974 processIdentChars, false) != ATTR_OKAY) { 975 hasErrors = true; 976 } 977 } else if (strcmp16(block.getElementName(&len), action16.string()) == 0 978 || strcmp16(block.getElementName(&len), category16.string()) == 0) { 979 if (validateAttr(manifestPath, block, 980 RESOURCES_ANDROID_NAMESPACE, "name", 981 packageIdentChars, true) != ATTR_OKAY) { 982 hasErrors = true; 983 } 984 } else if (strcmp16(block.getElementName(&len), data16.string()) == 0) { 985 if (validateAttr(manifestPath, block, 986 RESOURCES_ANDROID_NAMESPACE, "mimeType", 987 typeIdentChars, true) != ATTR_OKAY) { 988 hasErrors = true; 989 } 990 if (validateAttr(manifestPath, block, 991 RESOURCES_ANDROID_NAMESPACE, "scheme", 992 schemeIdentChars, true) != ATTR_OKAY) { 993 hasErrors = true; 994 } 995 } 996 } 997 } 998 999 if (table.validateLocalizations()) { 1000 hasErrors = true; 1001 } 1002 1003 if (hasErrors) { 1004 return UNKNOWN_ERROR; 1005 } 1006 1007 // Generate final compiled manifest file. 1008 manifestFile->clearData(); 1009 err = compileXmlFile(assets, manifestFile, &table); 1010 if (err < NO_ERROR) { 1011 return err; 1012 } 1013 1014 //block.restart(); 1015 //printXMLBlock(&block); 1016 1017 // -------------------------------------------------------------- 1018 // Generate the final resource table. 1019 // Re-flatten because we may have added new resource IDs 1020 // -------------------------------------------------------------- 1021 1022 if (table.hasResources()) { 1023 sp<AaptSymbols> symbols = assets->getSymbolsFor(String8("R")); 1024 err = table.addSymbols(symbols); 1025 if (err < NO_ERROR) { 1026 return err; 1027 } 1028 1029 sp<AaptFile> resFile(getResourceFile(assets)); 1030 if (resFile == NULL) { 1031 fprintf(stderr, "Error: unable to generate entry for resource data\n"); 1032 return UNKNOWN_ERROR; 1033 } 1034 1035 err = table.flatten(bundle, resFile); 1036 if (err < NO_ERROR) { 1037 return err; 1038 } 1039 1040 if (bundle->getPublicOutputFile()) { 1041 FILE* fp = fopen(bundle->getPublicOutputFile(), "w+"); 1042 if (fp == NULL) { 1043 fprintf(stderr, "ERROR: Unable to open public definitions output file %s: %s\n", 1044 (const char*)bundle->getPublicOutputFile(), strerror(errno)); 1045 return UNKNOWN_ERROR; 1046 } 1047 if (bundle->getVerbose()) { 1048 printf(" Writing public definitions to %s.\n", bundle->getPublicOutputFile()); 1049 } 1050 table.writePublicDefinitions(String16(assets->getPackage()), fp); 1051 fclose(fp); 1052 } 1053 1054 NOISY( 1055 ResTable rt; 1056 rt.add(resFile->getData(), resFile->getSize(), NULL); 1057 printf("Generated resources:\n"); 1058 rt.print(); 1059 ) 1060 1061 // These resources are now considered to be a part of the included 1062 // resources, for others to reference. 1063 err = assets->addIncludedResources(resFile); 1064 if (err < NO_ERROR) { 1065 fprintf(stderr, "ERROR: Unable to parse generated resources, aborting.\n"); 1066 return err; 1067 } 1068 } 1069 return err; 1070} 1071 1072static const char* getIndentSpace(int indent) 1073{ 1074static const char whitespace[] = 1075" "; 1076 1077 return whitespace + sizeof(whitespace) - 1 - indent*4; 1078} 1079 1080static status_t fixupSymbol(String16* inoutSymbol) 1081{ 1082 inoutSymbol->replaceAll('.', '_'); 1083 inoutSymbol->replaceAll(':', '_'); 1084 return NO_ERROR; 1085} 1086 1087static String16 getAttributeComment(const sp<AaptAssets>& assets, 1088 const String8& name, 1089 String16* outTypeComment = NULL) 1090{ 1091 sp<AaptSymbols> asym = assets->getSymbolsFor(String8("R")); 1092 if (asym != NULL) { 1093 //printf("Got R symbols!\n"); 1094 asym = asym->getNestedSymbols().valueFor(String8("attr")); 1095 if (asym != NULL) { 1096 //printf("Got attrs symbols! comment %s=%s\n", 1097 // name.string(), String8(asym->getComment(name)).string()); 1098 if (outTypeComment != NULL) { 1099 *outTypeComment = asym->getTypeComment(name); 1100 } 1101 return asym->getComment(name); 1102 } 1103 } 1104 return String16(); 1105} 1106 1107static status_t writeLayoutClasses( 1108 FILE* fp, const sp<AaptAssets>& assets, 1109 const sp<AaptSymbols>& symbols, int indent, bool includePrivate) 1110{ 1111 const char* indentStr = getIndentSpace(indent); 1112 if (!includePrivate) { 1113 fprintf(fp, "%s/** @doconly */\n", indentStr); 1114 } 1115 fprintf(fp, "%spublic static final class styleable {\n", indentStr); 1116 indent++; 1117 1118 String16 attr16("attr"); 1119 String16 package16(assets->getPackage()); 1120 1121 indentStr = getIndentSpace(indent); 1122 bool hasErrors = false; 1123 1124 size_t i; 1125 size_t N = symbols->getNestedSymbols().size(); 1126 for (i=0; i<N; i++) { 1127 sp<AaptSymbols> nsymbols = symbols->getNestedSymbols().valueAt(i); 1128 String16 nclassName16(symbols->getNestedSymbols().keyAt(i)); 1129 String8 realClassName(nclassName16); 1130 if (fixupSymbol(&nclassName16) != NO_ERROR) { 1131 hasErrors = true; 1132 } 1133 String8 nclassName(nclassName16); 1134 1135 SortedVector<uint32_t> idents; 1136 Vector<uint32_t> origOrder; 1137 Vector<bool> publicFlags; 1138 1139 size_t a; 1140 size_t NA = nsymbols->getSymbols().size(); 1141 for (a=0; a<NA; a++) { 1142 const AaptSymbolEntry& sym(nsymbols->getSymbols().valueAt(a)); 1143 int32_t code = sym.typeCode == AaptSymbolEntry::TYPE_INT32 1144 ? sym.int32Val : 0; 1145 bool isPublic = true; 1146 if (code == 0) { 1147 String16 name16(sym.name); 1148 uint32_t typeSpecFlags; 1149 code = assets->getIncludedResources().identifierForName( 1150 name16.string(), name16.size(), 1151 attr16.string(), attr16.size(), 1152 package16.string(), package16.size(), &typeSpecFlags); 1153 if (code == 0) { 1154 fprintf(stderr, "ERROR: In <declare-styleable> %s, unable to find attribute %s\n", 1155 nclassName.string(), sym.name.string()); 1156 hasErrors = true; 1157 } 1158 isPublic = (typeSpecFlags&ResTable_typeSpec::SPEC_PUBLIC) != 0; 1159 } 1160 idents.add(code); 1161 origOrder.add(code); 1162 publicFlags.add(isPublic); 1163 } 1164 1165 NA = idents.size(); 1166 1167 String16 comment = symbols->getComment(realClassName); 1168 fprintf(fp, "%s/** ", indentStr); 1169 if (comment.size() > 0) { 1170 fprintf(fp, "%s\n", String8(comment).string()); 1171 } else { 1172 fprintf(fp, "Attributes that can be used with a %s.\n", nclassName.string()); 1173 } 1174 bool hasTable = false; 1175 for (a=0; a<NA; a++) { 1176 ssize_t pos = idents.indexOf(origOrder.itemAt(a)); 1177 if (pos >= 0) { 1178 if (!hasTable) { 1179 hasTable = true; 1180 fprintf(fp, 1181 "%s <p>Includes the following attributes:</p>\n" 1182 "%s <table border=\"2\" width=\"85%%\" align=\"center\" frame=\"hsides\" rules=\"all\" cellpadding=\"5\">\n" 1183 "%s <colgroup align=\"left\" />\n" 1184 "%s <colgroup align=\"left\" />\n" 1185 "%s <tr><th>Attribute<th>Summary</tr>\n", 1186 indentStr, 1187 indentStr, 1188 indentStr, 1189 indentStr, 1190 indentStr); 1191 } 1192 const AaptSymbolEntry& sym = nsymbols->getSymbols().valueAt(a); 1193 if (!publicFlags.itemAt(a) && !includePrivate) { 1194 continue; 1195 } 1196 String8 name8(sym.name); 1197 String16 comment(sym.comment); 1198 if (comment.size() <= 0) { 1199 comment = getAttributeComment(assets, name8); 1200 } 1201 if (comment.size() > 0) { 1202 const char16_t* p = comment.string(); 1203 while (*p != 0 && *p != '.') { 1204 if (*p == '{') { 1205 while (*p != 0 && *p != '}') { 1206 p++; 1207 } 1208 } else { 1209 p++; 1210 } 1211 } 1212 if (*p == '.') { 1213 p++; 1214 } 1215 comment = String16(comment.string(), p-comment.string()); 1216 } 1217 String16 name(name8); 1218 fixupSymbol(&name); 1219 fprintf(fp, "%s <tr><th><code>{@link #%s_%s %s:%s}</code><td>%s</tr>\n", 1220 indentStr, nclassName.string(), 1221 String8(name).string(), 1222 assets->getPackage().string(), 1223 String8(name).string(), 1224 String8(comment).string()); 1225 } 1226 } 1227 if (hasTable) { 1228 fprintf(fp, "%s </table>\n", indentStr); 1229 } 1230 for (a=0; a<NA; a++) { 1231 ssize_t pos = idents.indexOf(origOrder.itemAt(a)); 1232 if (pos >= 0) { 1233 const AaptSymbolEntry& sym = nsymbols->getSymbols().valueAt(a); 1234 if (!publicFlags.itemAt(a) && !includePrivate) { 1235 continue; 1236 } 1237 String16 name(sym.name); 1238 fixupSymbol(&name); 1239 fprintf(fp, "%s @see #%s_%s\n", 1240 indentStr, nclassName.string(), 1241 String8(name).string()); 1242 } 1243 } 1244 fprintf(fp, "%s */\n", getIndentSpace(indent)); 1245 1246 fprintf(fp, 1247 "%spublic static final int[] %s = {\n" 1248 "%s", 1249 indentStr, nclassName.string(), 1250 getIndentSpace(indent+1)); 1251 1252 for (a=0; a<NA; a++) { 1253 if (a != 0) { 1254 if ((a&3) == 0) { 1255 fprintf(fp, ",\n%s", getIndentSpace(indent+1)); 1256 } else { 1257 fprintf(fp, ", "); 1258 } 1259 } 1260 fprintf(fp, "0x%08x", idents[a]); 1261 } 1262 1263 fprintf(fp, "\n%s};\n", indentStr); 1264 1265 for (a=0; a<NA; a++) { 1266 ssize_t pos = idents.indexOf(origOrder.itemAt(a)); 1267 if (pos >= 0) { 1268 const AaptSymbolEntry& sym = nsymbols->getSymbols().valueAt(a); 1269 if (!publicFlags.itemAt(a) && !includePrivate) { 1270 continue; 1271 } 1272 String8 name8(sym.name); 1273 String16 comment(sym.comment); 1274 String16 typeComment; 1275 if (comment.size() <= 0) { 1276 comment = getAttributeComment(assets, name8, &typeComment); 1277 } else { 1278 getAttributeComment(assets, name8, &typeComment); 1279 } 1280 String16 name(name8); 1281 if (fixupSymbol(&name) != NO_ERROR) { 1282 hasErrors = true; 1283 } 1284 1285 uint32_t typeSpecFlags = 0; 1286 String16 name16(sym.name); 1287 assets->getIncludedResources().identifierForName( 1288 name16.string(), name16.size(), 1289 attr16.string(), attr16.size(), 1290 package16.string(), package16.size(), &typeSpecFlags); 1291 //printf("%s:%s/%s: 0x%08x\n", String8(package16).string(), 1292 // String8(attr16).string(), String8(name16).string(), typeSpecFlags); 1293 const bool pub = (typeSpecFlags&ResTable_typeSpec::SPEC_PUBLIC) != 0; 1294 1295 fprintf(fp, "%s/**\n", indentStr); 1296 if (comment.size() > 0) { 1297 fprintf(fp, "%s <p>\n%s @attr description\n", indentStr, indentStr); 1298 fprintf(fp, "%s %s\n", indentStr, String8(comment).string()); 1299 } else { 1300 fprintf(fp, 1301 "%s <p>This symbol is the offset where the {@link %s.R.attr#%s}\n" 1302 "%s attribute's value can be found in the {@link #%s} array.\n", 1303 indentStr, 1304 pub ? assets->getPackage().string() 1305 : assets->getSymbolsPrivatePackage().string(), 1306 String8(name).string(), 1307 indentStr, nclassName.string()); 1308 } 1309 if (typeComment.size() > 0) { 1310 fprintf(fp, "\n\n%s %s\n", indentStr, String8(typeComment).string()); 1311 } 1312 if (comment.size() > 0) { 1313 if (pub) { 1314 fprintf(fp, 1315 "%s <p>This corresponds to the global attribute" 1316 "%s resource symbol {@link %s.R.attr#%s}.\n", 1317 indentStr, indentStr, 1318 assets->getPackage().string(), 1319 String8(name).string()); 1320 } else { 1321 fprintf(fp, 1322 "%s <p>This is a private symbol.\n", indentStr); 1323 } 1324 } 1325 fprintf(fp, "%s @attr name %s:%s\n", indentStr, 1326 "android", String8(name).string()); 1327 fprintf(fp, "%s*/\n", indentStr); 1328 fprintf(fp, 1329 "%spublic static final int %s_%s = %d;\n", 1330 indentStr, nclassName.string(), 1331 String8(name).string(), (int)pos); 1332 } 1333 } 1334 } 1335 1336 indent--; 1337 fprintf(fp, "%s};\n", getIndentSpace(indent)); 1338 return hasErrors ? UNKNOWN_ERROR : NO_ERROR; 1339} 1340 1341static status_t writeSymbolClass( 1342 FILE* fp, const sp<AaptAssets>& assets, bool includePrivate, 1343 const sp<AaptSymbols>& symbols, const String8& className, int indent) 1344{ 1345 fprintf(fp, "%spublic %sfinal class %s {\n", 1346 getIndentSpace(indent), 1347 indent != 0 ? "static " : "", className.string()); 1348 indent++; 1349 1350 size_t i; 1351 status_t err = NO_ERROR; 1352 1353 size_t N = symbols->getSymbols().size(); 1354 for (i=0; i<N; i++) { 1355 const AaptSymbolEntry& sym = symbols->getSymbols().valueAt(i); 1356 if (sym.typeCode != AaptSymbolEntry::TYPE_INT32) { 1357 continue; 1358 } 1359 if (!includePrivate && !sym.isPublic) { 1360 continue; 1361 } 1362 String16 name(sym.name); 1363 String8 realName(name); 1364 if (fixupSymbol(&name) != NO_ERROR) { 1365 return UNKNOWN_ERROR; 1366 } 1367 String16 comment(sym.comment); 1368 bool haveComment = false; 1369 if (comment.size() > 0) { 1370 haveComment = true; 1371 fprintf(fp, 1372 "%s/** %s\n", 1373 getIndentSpace(indent), String8(comment).string()); 1374 } else if (sym.isPublic && !includePrivate) { 1375 sym.sourcePos.warning("No comment for public symbol %s:%s/%s", 1376 assets->getPackage().string(), className.string(), 1377 String8(sym.name).string()); 1378 } 1379 String16 typeComment(sym.typeComment); 1380 if (typeComment.size() > 0) { 1381 if (!haveComment) { 1382 haveComment = true; 1383 fprintf(fp, 1384 "%s/** %s\n", 1385 getIndentSpace(indent), String8(typeComment).string()); 1386 } else { 1387 fprintf(fp, 1388 "%s %s\n", 1389 getIndentSpace(indent), String8(typeComment).string()); 1390 } 1391 } 1392 if (haveComment) { 1393 fprintf(fp,"%s */\n", getIndentSpace(indent)); 1394 } 1395 fprintf(fp, "%spublic static final int %s=0x%08x;\n", 1396 getIndentSpace(indent), 1397 String8(name).string(), (int)sym.int32Val); 1398 } 1399 1400 for (i=0; i<N; i++) { 1401 const AaptSymbolEntry& sym = symbols->getSymbols().valueAt(i); 1402 if (sym.typeCode != AaptSymbolEntry::TYPE_STRING) { 1403 continue; 1404 } 1405 if (!includePrivate && !sym.isPublic) { 1406 continue; 1407 } 1408 String16 name(sym.name); 1409 if (fixupSymbol(&name) != NO_ERROR) { 1410 return UNKNOWN_ERROR; 1411 } 1412 String16 comment(sym.comment); 1413 if (comment.size() > 0) { 1414 fprintf(fp, 1415 "%s/** %s\n" 1416 "%s */\n", 1417 getIndentSpace(indent), String8(comment).string(), 1418 getIndentSpace(indent)); 1419 } else if (sym.isPublic && !includePrivate) { 1420 sym.sourcePos.warning("No comment for public symbol %s:%s/%s", 1421 assets->getPackage().string(), className.string(), 1422 String8(sym.name).string()); 1423 } 1424 fprintf(fp, "%spublic static final String %s=\"%s\";\n", 1425 getIndentSpace(indent), 1426 String8(name).string(), sym.stringVal.string()); 1427 } 1428 1429 sp<AaptSymbols> styleableSymbols; 1430 1431 N = symbols->getNestedSymbols().size(); 1432 for (i=0; i<N; i++) { 1433 sp<AaptSymbols> nsymbols = symbols->getNestedSymbols().valueAt(i); 1434 String8 nclassName(symbols->getNestedSymbols().keyAt(i)); 1435 if (nclassName == "styleable") { 1436 styleableSymbols = nsymbols; 1437 } else { 1438 err = writeSymbolClass(fp, assets, includePrivate, nsymbols, nclassName, indent); 1439 } 1440 if (err != NO_ERROR) { 1441 return err; 1442 } 1443 } 1444 1445 if (styleableSymbols != NULL) { 1446 err = writeLayoutClasses(fp, assets, styleableSymbols, indent, includePrivate); 1447 if (err != NO_ERROR) { 1448 return err; 1449 } 1450 } 1451 1452 indent--; 1453 fprintf(fp, "%s}\n", getIndentSpace(indent)); 1454 return NO_ERROR; 1455} 1456 1457status_t writeResourceSymbols(Bundle* bundle, const sp<AaptAssets>& assets, 1458 const String8& package, bool includePrivate) 1459{ 1460 if (!bundle->getRClassDir()) { 1461 return NO_ERROR; 1462 } 1463 1464 const size_t N = assets->getSymbols().size(); 1465 for (size_t i=0; i<N; i++) { 1466 sp<AaptSymbols> symbols = assets->getSymbols().valueAt(i); 1467 String8 className(assets->getSymbols().keyAt(i)); 1468 String8 dest(bundle->getRClassDir()); 1469 if (bundle->getMakePackageDirs()) { 1470 String8 pkg(package); 1471 const char* last = pkg.string(); 1472 const char* s = last-1; 1473 do { 1474 s++; 1475 if (s > last && (*s == '.' || *s == 0)) { 1476 String8 part(last, s-last); 1477 dest.appendPath(part); 1478#ifdef HAVE_MS_C_RUNTIME 1479 _mkdir(dest.string()); 1480#else 1481 mkdir(dest.string(), S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP); 1482#endif 1483 last = s+1; 1484 } 1485 } while (*s); 1486 } 1487 dest.appendPath(className); 1488 dest.append(".java"); 1489 FILE* fp = fopen(dest.string(), "w+"); 1490 if (fp == NULL) { 1491 fprintf(stderr, "ERROR: Unable to open class file %s: %s\n", 1492 dest.string(), strerror(errno)); 1493 return UNKNOWN_ERROR; 1494 } 1495 if (bundle->getVerbose()) { 1496 printf(" Writing symbols for class %s.\n", className.string()); 1497 } 1498 1499 fprintf(fp, 1500 "/* AUTO-GENERATED FILE. DO NOT MODIFY.\n" 1501 " *\n" 1502 " * This class was automatically generated by the\n" 1503 " * aapt tool from the resource data it found. It\n" 1504 " * should not be modified by hand.\n" 1505 " */\n" 1506 "\n" 1507 "package %s;\n\n", package.string()); 1508 1509 status_t err = writeSymbolClass(fp, assets, includePrivate, symbols, className, 0); 1510 if (err != NO_ERROR) { 1511 return err; 1512 } 1513 fclose(fp); 1514 } 1515 1516 return NO_ERROR; 1517} 1518