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