]>
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 | ||
a534180c TAOSP |
48 | ResourceTypeSet::ResourceTypeSet() |
49 | :RefBase(), | |
50 | KeyedVector<String8,sp<AaptGroup> >() | |
51 | { | |
52 | } | |
53 | ||
54 | class ResourceDirIterator | |
55 | { | |
56 | public: | |
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 | ||
129 | private: | |
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 | ||
149 | bool 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 | ||
156 | static 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 | ||
7dc0cd41 KR |
174 | static status_t parsePackage(Bundle* bundle, const sp<AaptAssets>& assets, |
175 | const sp<AaptGroup>& grp) | |
a534180c TAOSP |
176 | { |
177 | if (grp->getFiles().size() != 1) { | |
406a85e3 | 178 | fprintf(stderr, "warning: Multiple AndroidManifest.xml files found, using %s\n", |
a534180c TAOSP |
179 | grp->getFiles().valueAt(0)->getPrintableSource().string()); |
180 | } | |
181 | ||
182 | sp<AaptFile> file = grp->getFiles().valueAt(0); | |
183 | ||
184 | ResXMLTree block; | |
185 | status_t err = parseXMLResource(file, &block); | |
186 | if (err != NO_ERROR) { | |
187 | return err; | |
188 | } | |
189 | //printXMLBlock(&block); | |
190 | ||
191 | ResXMLTree::event_code_t code; | |
192 | while ((code=block.next()) != ResXMLTree::START_TAG | |
193 | && code != ResXMLTree::END_DOCUMENT | |
194 | && code != ResXMLTree::BAD_DOCUMENT) { | |
195 | } | |
196 | ||
197 | size_t len; | |
198 | if (code != ResXMLTree::START_TAG) { | |
199 | fprintf(stderr, "%s:%d: No start tag found\n", | |
200 | file->getPrintableSource().string(), block.getLineNumber()); | |
201 | return UNKNOWN_ERROR; | |
202 | } | |
203 | if (strcmp16(block.getElementName(&len), String16("manifest").string()) != 0) { | |
204 | fprintf(stderr, "%s:%d: Invalid start tag %s, expected <manifest>\n", | |
205 | file->getPrintableSource().string(), block.getLineNumber(), | |
206 | String8(block.getElementName(&len)).string()); | |
207 | return UNKNOWN_ERROR; | |
208 | } | |
209 | ||
210 | ssize_t nameIndex = block.indexOfAttribute(NULL, "package"); | |
211 | if (nameIndex < 0) { | |
212 | fprintf(stderr, "%s:%d: <manifest> does not have package attribute.\n", | |
213 | file->getPrintableSource().string(), block.getLineNumber()); | |
214 | return UNKNOWN_ERROR; | |
215 | } | |
216 | ||
217 | assets->setPackage(String8(block.getAttributeStringValue(nameIndex, &len))); | |
218 | ||
7dc0cd41 KR |
219 | String16 uses_sdk16("uses-sdk"); |
220 | while ((code=block.next()) != ResXMLTree::END_DOCUMENT | |
221 | && code != ResXMLTree::BAD_DOCUMENT) { | |
222 | if (code == ResXMLTree::START_TAG) { | |
223 | if (strcmp16(block.getElementName(&len), uses_sdk16.string()) == 0) { | |
224 | ssize_t minSdkIndex = block.indexOfAttribute("android", | |
225 | "minSdkVersion"); | |
226 | if (minSdkIndex >= 0) { | |
227 | String8 minSdkString = String8( | |
228 | block.getAttributeStringValue(minSdkIndex, &len)); | |
229 | bundle->setMinSdkVersion(minSdkString.string()); | |
230 | } | |
231 | } | |
232 | } | |
233 | } | |
234 | ||
a534180c TAOSP |
235 | return NO_ERROR; |
236 | } | |
237 | ||
238 | // ========================================================================== | |
239 | // ========================================================================== | |
240 | // ========================================================================== | |
241 | ||
242 | static status_t makeFileResources(Bundle* bundle, const sp<AaptAssets>& assets, | |
243 | ResourceTable* table, | |
244 | const sp<ResourceTypeSet>& set, | |
245 | const char* resType) | |
246 | { | |
247 | String8 type8(resType); | |
248 | String16 type16(resType); | |
249 | ||
250 | bool hasErrors = false; | |
251 | ||
252 | ResourceDirIterator it(set, String8(resType)); | |
253 | ssize_t res; | |
254 | while ((res=it.next()) == NO_ERROR) { | |
255 | if (bundle->getVerbose()) { | |
256 | printf(" (new resource id %s from %s)\n", | |
257 | it.getBaseName().string(), it.getFile()->getPrintableSource().string()); | |
258 | } | |
259 | String16 baseName(it.getBaseName()); | |
260 | const char16_t* str = baseName.string(); | |
261 | const char16_t* const end = str + baseName.size(); | |
262 | while (str < end) { | |
263 | if (!((*str >= 'a' && *str <= 'z') | |
264 | || (*str >= '0' && *str <= '9') | |
265 | || *str == '_' || *str == '.')) { | |
266 | fprintf(stderr, "%s: Invalid file name: must contain only [a-z0-9_.]\n", | |
267 | it.getPath().string()); | |
268 | hasErrors = true; | |
269 | } | |
270 | str++; | |
271 | } | |
272 | String8 resPath = it.getPath(); | |
273 | resPath.convertToResPath(); | |
274 | table->addEntry(SourcePos(it.getPath(), 0), String16(assets->getPackage()), | |
275 | type16, | |
276 | baseName, | |
277 | String16(resPath), | |
278 | NULL, | |
279 | &it.getParams()); | |
280 | assets->addResource(it.getLeafName(), resPath, it.getFile(), type8); | |
281 | } | |
282 | ||
283 | return hasErrors ? UNKNOWN_ERROR : NO_ERROR; | |
284 | } | |
285 | ||
286 | static status_t preProcessImages(Bundle* bundle, const sp<AaptAssets>& assets, | |
287 | const sp<ResourceTypeSet>& set) | |
288 | { | |
289 | ResourceDirIterator it(set, String8("drawable")); | |
290 | Vector<sp<AaptFile> > newNameFiles; | |
291 | Vector<String8> newNamePaths; | |
8f940cc4 | 292 | bool hasErrors = false; |
a534180c TAOSP |
293 | ssize_t res; |
294 | while ((res=it.next()) == NO_ERROR) { | |
295 | res = preProcessImage(bundle, assets, it.getFile(), NULL); | |
8f940cc4 DS |
296 | if (res < NO_ERROR) { |
297 | hasErrors = true; | |
a534180c TAOSP |
298 | } |
299 | } | |
300 | ||
8f940cc4 | 301 | return (hasErrors || (res < NO_ERROR)) ? UNKNOWN_ERROR : NO_ERROR; |
a534180c TAOSP |
302 | } |
303 | ||
304 | status_t postProcessImages(const sp<AaptAssets>& assets, | |
305 | ResourceTable* table, | |
306 | const sp<ResourceTypeSet>& set) | |
307 | { | |
308 | ResourceDirIterator it(set, String8("drawable")); | |
8f940cc4 | 309 | bool hasErrors = false; |
a534180c TAOSP |
310 | ssize_t res; |
311 | while ((res=it.next()) == NO_ERROR) { | |
312 | res = postProcessImage(assets, table, it.getFile()); | |
8f940cc4 DS |
313 | if (res < NO_ERROR) { |
314 | hasErrors = true; | |
a534180c TAOSP |
315 | } |
316 | } | |
317 | ||
8f940cc4 | 318 | return (hasErrors || (res < NO_ERROR)) ? UNKNOWN_ERROR : NO_ERROR; |
a534180c TAOSP |
319 | } |
320 | ||
321 | static void collect_files(const sp<AaptDir>& dir, | |
322 | KeyedVector<String8, sp<ResourceTypeSet> >* resources) | |
323 | { | |
324 | const DefaultKeyedVector<String8, sp<AaptGroup> >& groups = dir->getFiles(); | |
325 | int N = groups.size(); | |
326 | for (int i=0; i<N; i++) { | |
327 | String8 leafName = groups.keyAt(i); | |
328 | const sp<AaptGroup>& group = groups.valueAt(i); | |
329 | ||
330 | const DefaultKeyedVector<AaptGroupEntry, sp<AaptFile> >& files | |
331 | = group->getFiles(); | |
332 | ||
333 | if (files.size() == 0) { | |
334 | continue; | |
335 | } | |
336 | ||
337 | String8 resType = files.valueAt(0)->getResourceType(); | |
338 | ||
339 | ssize_t index = resources->indexOfKey(resType); | |
340 | ||
341 | if (index < 0) { | |
342 | sp<ResourceTypeSet> set = new ResourceTypeSet(); | |
343 | set->add(leafName, group); | |
344 | resources->add(resType, set); | |
345 | } else { | |
346 | sp<ResourceTypeSet> set = resources->valueAt(index); | |
347 | index = set->indexOfKey(leafName); | |
348 | if (index < 0) { | |
349 | set->add(leafName, group); | |
350 | } else { | |
351 | sp<AaptGroup> existingGroup = set->valueAt(index); | |
352 | int M = files.size(); | |
353 | for (int j=0; j<M; j++) { | |
354 | existingGroup->addFile(files.valueAt(j)); | |
355 | } | |
356 | } | |
357 | } | |
358 | } | |
359 | } | |
360 | ||
361 | static void collect_files(const sp<AaptAssets>& ass, | |
362 | KeyedVector<String8, sp<ResourceTypeSet> >* resources) | |
363 | { | |
364 | const Vector<sp<AaptDir> >& dirs = ass->resDirs(); | |
365 | int N = dirs.size(); | |
366 | ||
367 | for (int i=0; i<N; i++) { | |
368 | sp<AaptDir> d = dirs.itemAt(i); | |
369 | collect_files(d, resources); | |
370 | ||
371 | // don't try to include the res dir | |
372 | ass->removeDir(d->getLeaf()); | |
373 | } | |
374 | } | |
375 | ||
376 | enum { | |
377 | ATTR_OKAY = -1, | |
378 | ATTR_NOT_FOUND = -2, | |
379 | ATTR_LEADING_SPACES = -3, | |
380 | ATTR_TRAILING_SPACES = -4 | |
381 | }; | |
382 | static int validateAttr(const String8& path, const ResXMLParser& parser, | |
383 | const char* ns, const char* attr, const char* validChars, bool required) | |
384 | { | |
385 | size_t len; | |
386 | ||
387 | ssize_t index = parser.indexOfAttribute(ns, attr); | |
388 | const uint16_t* str; | |
389 | if (index >= 0 && (str=parser.getAttributeStringValue(index, &len)) != NULL) { | |
390 | if (validChars) { | |
391 | for (size_t i=0; i<len; i++) { | |
392 | uint16_t c = str[i]; | |
393 | const char* p = validChars; | |
394 | bool okay = false; | |
395 | while (*p) { | |
396 | if (c == *p) { | |
397 | okay = true; | |
398 | break; | |
399 | } | |
400 | p++; | |
401 | } | |
402 | if (!okay) { | |
403 | fprintf(stderr, "%s:%d: Tag <%s> attribute %s has invalid character '%c'.\n", | |
404 | path.string(), parser.getLineNumber(), | |
405 | String8(parser.getElementName(&len)).string(), attr, (char)str[i]); | |
406 | return (int)i; | |
407 | } | |
408 | } | |
409 | } | |
410 | if (*str == ' ') { | |
411 | fprintf(stderr, "%s:%d: Tag <%s> attribute %s can not start with a space.\n", | |
412 | path.string(), parser.getLineNumber(), | |
413 | String8(parser.getElementName(&len)).string(), attr); | |
414 | return ATTR_LEADING_SPACES; | |
415 | } | |
416 | if (str[len-1] == ' ') { | |
417 | fprintf(stderr, "%s:%d: Tag <%s> attribute %s can not end with a space.\n", | |
418 | path.string(), parser.getLineNumber(), | |
419 | String8(parser.getElementName(&len)).string(), attr); | |
420 | return ATTR_TRAILING_SPACES; | |
421 | } | |
422 | return ATTR_OKAY; | |
423 | } | |
424 | if (required) { | |
425 | fprintf(stderr, "%s:%d: Tag <%s> missing required attribute %s.\n", | |
426 | path.string(), parser.getLineNumber(), | |
427 | String8(parser.getElementName(&len)).string(), attr); | |
428 | return ATTR_NOT_FOUND; | |
429 | } | |
430 | return ATTR_OKAY; | |
431 | } | |
432 | ||
433 | static void checkForIds(const String8& path, ResXMLParser& parser) | |
434 | { | |
435 | ResXMLTree::event_code_t code; | |
436 | while ((code=parser.next()) != ResXMLTree::END_DOCUMENT | |
437 | && code > ResXMLTree::BAD_DOCUMENT) { | |
438 | if (code == ResXMLTree::START_TAG) { | |
439 | ssize_t index = parser.indexOfAttribute(NULL, "id"); | |
440 | if (index >= 0) { | |
406a85e3 | 441 | fprintf(stderr, "%s:%d: warning: found plain 'id' attribute; did you mean the new 'android:id' name?\n", |
a534180c TAOSP |
442 | path.string(), parser.getLineNumber()); |
443 | } | |
444 | } | |
445 | } | |
446 | } | |
447 | ||
21c5c61a RG |
448 | static bool applyFileOverlay(Bundle *bundle, |
449 | const sp<AaptAssets>& assets, | |
a534180c TAOSP |
450 | const sp<ResourceTypeSet>& baseSet, |
451 | const char *resType) | |
452 | { | |
21c5c61a RG |
453 | if (bundle->getVerbose()) { |
454 | printf("applyFileOverlay for %s\n", resType); | |
455 | } | |
456 | ||
a534180c TAOSP |
457 | // Replace any base level files in this category with any found from the overlay |
458 | // Also add any found only in the overlay. | |
459 | sp<AaptAssets> overlay = assets->getOverlay(); | |
460 | String8 resTypeString(resType); | |
dd854d4c | 461 | |
a534180c TAOSP |
462 | // work through the linked list of overlays |
463 | while (overlay.get()) { | |
464 | KeyedVector<String8, sp<ResourceTypeSet> >* overlayRes = overlay->getResources(); | |
465 | ||
466 | // get the overlay resources of the requested type | |
467 | ssize_t index = overlayRes->indexOfKey(resTypeString); | |
468 | if (index >= 0) { | |
469 | sp<ResourceTypeSet> overlaySet = overlayRes->valueAt(index); | |
470 | ||
471 | // for each of the resources, check for a match in the previously built | |
472 | // non-overlay "baseset". | |
473 | size_t overlayCount = overlaySet->size(); | |
474 | for (size_t overlayIndex=0; overlayIndex<overlayCount; overlayIndex++) { | |
21c5c61a RG |
475 | if (bundle->getVerbose()) { |
476 | printf("trying overlaySet Key=%s\n",overlaySet->keyAt(overlayIndex).string()); | |
477 | } | |
a534180c | 478 | size_t baseIndex = baseSet->indexOfKey(overlaySet->keyAt(overlayIndex)); |
dd854d4c | 479 | if (baseIndex < UNKNOWN_ERROR) { |
a534180c TAOSP |
480 | // look for same flavor. For a given file (strings.xml, for example) |
481 | // there may be a locale specific or other flavors - we want to match | |
482 | // the same flavor. | |
483 | sp<AaptGroup> overlayGroup = overlaySet->valueAt(overlayIndex); | |
484 | sp<AaptGroup> baseGroup = baseSet->valueAt(baseIndex); | |
21c5c61a RG |
485 | |
486 | DefaultKeyedVector<AaptGroupEntry, sp<AaptFile> > overlayFiles = | |
a534180c | 487 | overlayGroup->getFiles(); |
21c5c61a RG |
488 | if (bundle->getVerbose()) { |
489 | DefaultKeyedVector<AaptGroupEntry, sp<AaptFile> > baseFiles = | |
490 | baseGroup->getFiles(); | |
491 | for (size_t i=0; i < baseFiles.size(); i++) { | |
492 | printf("baseFile %d has flavor %s\n", i, | |
493 | baseFiles.keyAt(i).toString().string()); | |
494 | } | |
495 | for (size_t i=0; i < overlayFiles.size(); i++) { | |
496 | printf("overlayFile %d has flavor %s\n", i, | |
497 | overlayFiles.keyAt(i).toString().string()); | |
498 | } | |
499 | } | |
500 | ||
a534180c | 501 | size_t overlayGroupSize = overlayFiles.size(); |
21c5c61a RG |
502 | for (size_t overlayGroupIndex = 0; |
503 | overlayGroupIndex<overlayGroupSize; | |
a534180c | 504 | overlayGroupIndex++) { |
21c5c61a RG |
505 | size_t baseFileIndex = |
506 | baseGroup->getFiles().indexOfKey(overlayFiles. | |
507 | keyAt(overlayGroupIndex)); | |
a534180c | 508 | if(baseFileIndex < UNKNOWN_ERROR) { |
21c5c61a RG |
509 | if (bundle->getVerbose()) { |
510 | printf("found a match (%d) for overlay file %s, for flavor %s\n", | |
511 | baseFileIndex, | |
512 | overlayGroup->getLeaf().string(), | |
513 | overlayFiles.keyAt(overlayGroupIndex).toString().string()); | |
514 | } | |
a534180c TAOSP |
515 | baseGroup->removeFile(baseFileIndex); |
516 | } else { | |
517 | // didn't find a match fall through and add it.. | |
518 | } | |
519 | baseGroup->addFile(overlayFiles.valueAt(overlayGroupIndex)); | |
a76631e8 | 520 | assets->addGroupEntry(overlayFiles.keyAt(overlayGroupIndex)); |
a534180c TAOSP |
521 | } |
522 | } else { | |
523 | // this group doesn't exist (a file that's only in the overlay) | |
a8c9cd56 DH |
524 | baseSet->add(overlaySet->keyAt(overlayIndex), |
525 | overlaySet->valueAt(overlayIndex)); | |
a76631e8 DH |
526 | // make sure all flavors are defined in the resources. |
527 | sp<AaptGroup> overlayGroup = overlaySet->valueAt(overlayIndex); | |
21c5c61a | 528 | DefaultKeyedVector<AaptGroupEntry, sp<AaptFile> > overlayFiles = |
a76631e8 DH |
529 | overlayGroup->getFiles(); |
530 | size_t overlayGroupSize = overlayFiles.size(); | |
21c5c61a RG |
531 | for (size_t overlayGroupIndex = 0; |
532 | overlayGroupIndex<overlayGroupSize; | |
a76631e8 DH |
533 | overlayGroupIndex++) { |
534 | assets->addGroupEntry(overlayFiles.keyAt(overlayGroupIndex)); | |
535 | } | |
a534180c TAOSP |
536 | } |
537 | } | |
538 | // this overlay didn't have resources for this type | |
539 | } | |
540 | // try next overlay | |
541 | overlay = overlay->getOverlay(); | |
542 | } | |
dd854d4c | 543 | return true; |
a534180c TAOSP |
544 | } |
545 | ||
e942a5c2 DH |
546 | void addTagAttribute(const sp<XMLNode>& node, const char* ns8, |
547 | const char* attr8, const char* value) | |
548 | { | |
549 | if (value == NULL) { | |
550 | return; | |
551 | } | |
552 | ||
553 | const String16 ns(ns8); | |
554 | const String16 attr(attr8); | |
555 | ||
556 | if (node->getAttribute(ns, attr) != NULL) { | |
557 | fprintf(stderr, "Warning: AndroidManifest.xml already defines %s (in %s)\n", | |
558 | String8(attr).string(), String8(ns).string()); | |
559 | return; | |
560 | } | |
561 | ||
562 | node->addAttribute(ns, attr, String16(value)); | |
563 | } | |
564 | ||
565 | status_t massageManifest(Bundle* bundle, sp<XMLNode> root) | |
566 | { | |
567 | root = root->searchElement(String16(), String16("manifest")); | |
568 | if (root == NULL) { | |
569 | fprintf(stderr, "No <manifest> tag.\n"); | |
570 | return UNKNOWN_ERROR; | |
571 | } | |
572 | ||
573 | addTagAttribute(root, RESOURCES_ANDROID_NAMESPACE, "versionCode", | |
574 | bundle->getVersionCode()); | |
575 | addTagAttribute(root, RESOURCES_ANDROID_NAMESPACE, "versionName", | |
576 | bundle->getVersionName()); | |
577 | ||
578 | if (bundle->getMinSdkVersion() != NULL | |
579 | || bundle->getTargetSdkVersion() != NULL | |
580 | || bundle->getMaxSdkVersion() != NULL) { | |
581 | sp<XMLNode> vers = root->getChildElement(String16(), String16("uses-sdk")); | |
582 | if (vers == NULL) { | |
583 | vers = XMLNode::newElement(root->getFilename(), String16(), String16("uses-sdk")); | |
584 | root->insertChildAt(vers, 0); | |
585 | } | |
586 | ||
587 | addTagAttribute(vers, RESOURCES_ANDROID_NAMESPACE, "minSdkVersion", | |
588 | bundle->getMinSdkVersion()); | |
589 | addTagAttribute(vers, RESOURCES_ANDROID_NAMESPACE, "targetSdkVersion", | |
590 | bundle->getTargetSdkVersion()); | |
591 | addTagAttribute(vers, RESOURCES_ANDROID_NAMESPACE, "maxSdkVersion", | |
592 | bundle->getMaxSdkVersion()); | |
593 | } | |
594 | ||
595 | return NO_ERROR; | |
596 | } | |
597 | ||
a534180c TAOSP |
598 | #define ASSIGN_IT(n) \ |
599 | do { \ | |
600 | ssize_t index = resources->indexOfKey(String8(#n)); \ | |
601 | if (index >= 0) { \ | |
602 | n ## s = resources->valueAt(index); \ | |
603 | } \ | |
604 | } while (0) | |
605 | ||
606 | status_t buildResources(Bundle* bundle, const sp<AaptAssets>& assets) | |
607 | { | |
608 | // First, look for a package file to parse. This is required to | |
609 | // be able to generate the resource information. | |
610 | sp<AaptGroup> androidManifestFile = | |
611 | assets->getFiles().valueFor(String8("AndroidManifest.xml")); | |
612 | if (androidManifestFile == NULL) { | |
613 | fprintf(stderr, "ERROR: No AndroidManifest.xml file found.\n"); | |
614 | return UNKNOWN_ERROR; | |
615 | } | |
616 | ||
7dc0cd41 | 617 | status_t err = parsePackage(bundle, assets, androidManifestFile); |
a534180c TAOSP |
618 | if (err != NO_ERROR) { |
619 | return err; | |
620 | } | |
621 | ||
622 | NOISY(printf("Creating resources for package %s\n", | |
623 | assets->getPackage().string())); | |
624 | ||
625 | ResourceTable table(bundle, String16(assets->getPackage())); | |
626 | err = table.addIncludedResources(bundle, assets); | |
627 | if (err != NO_ERROR) { | |
628 | return err; | |
629 | } | |
630 | ||
631 | NOISY(printf("Found %d included resource packages\n", (int)table.size())); | |
632 | ||
15c62a5b KR |
633 | // Standard flags for compiled XML and optional UTF-8 encoding |
634 | int xmlFlags = XML_COMPILE_STANDARD_RESOURCE; | |
635 | if (bundle->getUTF8()) { | |
636 | xmlFlags |= XML_COMPILE_UTF8; | |
637 | } | |
638 | ||
a534180c TAOSP |
639 | // -------------------------------------------------------------- |
640 | // First, gather all resource information. | |
641 | // -------------------------------------------------------------- | |
642 | ||
643 | // resType -> leafName -> group | |
644 | KeyedVector<String8, sp<ResourceTypeSet> > *resources = | |
645 | new KeyedVector<String8, sp<ResourceTypeSet> >; | |
646 | collect_files(assets, resources); | |
647 | ||
648 | sp<ResourceTypeSet> drawables; | |
649 | sp<ResourceTypeSet> layouts; | |
650 | sp<ResourceTypeSet> anims; | |
651 | sp<ResourceTypeSet> xmls; | |
652 | sp<ResourceTypeSet> raws; | |
653 | sp<ResourceTypeSet> colors; | |
654 | sp<ResourceTypeSet> menus; | |
655 | ||
656 | ASSIGN_IT(drawable); | |
657 | ASSIGN_IT(layout); | |
658 | ASSIGN_IT(anim); | |
659 | ASSIGN_IT(xml); | |
660 | ASSIGN_IT(raw); | |
661 | ASSIGN_IT(color); | |
662 | ASSIGN_IT(menu); | |
663 | ||
664 | assets->setResources(resources); | |
665 | // now go through any resource overlays and collect their files | |
666 | sp<AaptAssets> current = assets->getOverlay(); | |
667 | while(current.get()) { | |
668 | KeyedVector<String8, sp<ResourceTypeSet> > *resources = | |
669 | new KeyedVector<String8, sp<ResourceTypeSet> >; | |
670 | current->setResources(resources); | |
671 | collect_files(current, resources); | |
672 | current = current->getOverlay(); | |
673 | } | |
674 | // apply the overlay files to the base set | |
21c5c61a RG |
675 | if (!applyFileOverlay(bundle, assets, drawables, "drawable") || |
676 | !applyFileOverlay(bundle, assets, layouts, "layout") || | |
677 | !applyFileOverlay(bundle, assets, anims, "anim") || | |
678 | !applyFileOverlay(bundle, assets, xmls, "xml") || | |
679 | !applyFileOverlay(bundle, assets, raws, "raw") || | |
680 | !applyFileOverlay(bundle, assets, colors, "color") || | |
681 | !applyFileOverlay(bundle, assets, menus, "menu")) { | |
dd854d4c RG |
682 | return UNKNOWN_ERROR; |
683 | } | |
a534180c TAOSP |
684 | |
685 | bool hasErrors = false; | |
686 | ||
687 | if (drawables != NULL) { | |
688 | err = preProcessImages(bundle, assets, drawables); | |
689 | if (err == NO_ERROR) { | |
690 | err = makeFileResources(bundle, assets, &table, drawables, "drawable"); | |
691 | if (err != NO_ERROR) { | |
692 | hasErrors = true; | |
693 | } | |
694 | } else { | |
695 | hasErrors = true; | |
696 | } | |
697 | } | |
698 | ||
699 | if (layouts != NULL) { | |
700 | err = makeFileResources(bundle, assets, &table, layouts, "layout"); | |
701 | if (err != NO_ERROR) { | |
702 | hasErrors = true; | |
703 | } | |
704 | } | |
705 | ||
706 | if (anims != NULL) { | |
707 | err = makeFileResources(bundle, assets, &table, anims, "anim"); | |
708 | if (err != NO_ERROR) { | |
709 | hasErrors = true; | |
710 | } | |
711 | } | |
712 | ||
713 | if (xmls != NULL) { | |
714 | err = makeFileResources(bundle, assets, &table, xmls, "xml"); | |
715 | if (err != NO_ERROR) { | |
716 | hasErrors = true; | |
717 | } | |
718 | } | |
719 | ||
720 | if (raws != NULL) { | |
721 | err = makeFileResources(bundle, assets, &table, raws, "raw"); | |
722 | if (err != NO_ERROR) { | |
723 | hasErrors = true; | |
724 | } | |
725 | } | |
726 | ||
727 | // compile resources | |
728 | current = assets; | |
729 | while(current.get()) { | |
730 | KeyedVector<String8, sp<ResourceTypeSet> > *resources = | |
731 | current->getResources(); | |
732 | ||
733 | ssize_t index = resources->indexOfKey(String8("values")); | |
734 | if (index >= 0) { | |
735 | ResourceDirIterator it(resources->valueAt(index), String8("values")); | |
736 | ssize_t res; | |
737 | while ((res=it.next()) == NO_ERROR) { | |
738 | sp<AaptFile> file = it.getFile(); | |
739 | res = compileResourceFile(bundle, assets, file, it.getParams(), | |
740 | (current!=assets), &table); | |
741 | if (res != NO_ERROR) { | |
742 | hasErrors = true; | |
743 | } | |
744 | } | |
745 | } | |
746 | current = current->getOverlay(); | |
747 | } | |
748 | ||
749 | if (colors != NULL) { | |
750 | err = makeFileResources(bundle, assets, &table, colors, "color"); | |
751 | if (err != NO_ERROR) { | |
752 | hasErrors = true; | |
753 | } | |
754 | } | |
755 | ||
756 | if (menus != NULL) { | |
757 | err = makeFileResources(bundle, assets, &table, menus, "menu"); | |
758 | if (err != NO_ERROR) { | |
759 | hasErrors = true; | |
760 | } | |
761 | } | |
762 | ||
763 | // -------------------------------------------------------------------- | |
764 | // Assignment of resource IDs and initial generation of resource table. | |
765 | // -------------------------------------------------------------------- | |
766 | ||
767 | if (table.hasResources()) { | |
768 | sp<AaptFile> resFile(getResourceFile(assets)); | |
769 | if (resFile == NULL) { | |
770 | fprintf(stderr, "Error: unable to generate entry for resource data\n"); | |
771 | return UNKNOWN_ERROR; | |
772 | } | |
773 | ||
774 | err = table.assignResourceIds(); | |
775 | if (err < NO_ERROR) { | |
776 | return err; | |
777 | } | |
778 | } | |
779 | ||
780 | // -------------------------------------------------------------- | |
781 | // Finally, we can now we can compile XML files, which may reference | |
782 | // resources. | |
783 | // -------------------------------------------------------------- | |
784 | ||
785 | if (layouts != NULL) { | |
786 | ResourceDirIterator it(layouts, String8("layout")); | |
787 | while ((err=it.next()) == NO_ERROR) { | |
788 | String8 src = it.getFile()->getPrintableSource(); | |
15c62a5b | 789 | err = compileXmlFile(assets, it.getFile(), &table, xmlFlags); |
a534180c TAOSP |
790 | if (err == NO_ERROR) { |
791 | ResXMLTree block; | |
792 | block.setTo(it.getFile()->getData(), it.getFile()->getSize(), true); | |
793 | checkForIds(src, block); | |
794 | } else { | |
795 | hasErrors = true; | |
796 | } | |
797 | } | |
798 | ||
799 | if (err < NO_ERROR) { | |
800 | hasErrors = true; | |
801 | } | |
802 | err = NO_ERROR; | |
803 | } | |
804 | ||
805 | if (anims != NULL) { | |
806 | ResourceDirIterator it(anims, String8("anim")); | |
807 | while ((err=it.next()) == NO_ERROR) { | |
15c62a5b | 808 | err = compileXmlFile(assets, it.getFile(), &table, xmlFlags); |
a534180c TAOSP |
809 | if (err != NO_ERROR) { |
810 | hasErrors = true; | |
811 | } | |
812 | } | |
813 | ||
814 | if (err < NO_ERROR) { | |
815 | hasErrors = true; | |
816 | } | |
817 | err = NO_ERROR; | |
818 | } | |
819 | ||
820 | if (xmls != NULL) { | |
821 | ResourceDirIterator it(xmls, String8("xml")); | |
822 | while ((err=it.next()) == NO_ERROR) { | |
15c62a5b | 823 | err = compileXmlFile(assets, it.getFile(), &table, xmlFlags); |
a534180c TAOSP |
824 | if (err != NO_ERROR) { |
825 | hasErrors = true; | |
826 | } | |
827 | } | |
828 | ||
829 | if (err < NO_ERROR) { | |
830 | hasErrors = true; | |
831 | } | |
832 | err = NO_ERROR; | |
833 | } | |
834 | ||
835 | if (drawables != NULL) { | |
836 | err = postProcessImages(assets, &table, drawables); | |
837 | if (err != NO_ERROR) { | |
838 | hasErrors = true; | |
839 | } | |
840 | } | |
841 | ||
842 | if (colors != NULL) { | |
843 | ResourceDirIterator it(colors, String8("color")); | |
844 | while ((err=it.next()) == NO_ERROR) { | |
15c62a5b | 845 | err = compileXmlFile(assets, it.getFile(), &table, xmlFlags); |
a534180c TAOSP |
846 | if (err != NO_ERROR) { |
847 | hasErrors = true; | |
848 | } | |
849 | } | |
850 | ||
851 | if (err < NO_ERROR) { | |
852 | hasErrors = true; | |
853 | } | |
854 | err = NO_ERROR; | |
855 | } | |
856 | ||
857 | if (menus != NULL) { | |
858 | ResourceDirIterator it(menus, String8("menu")); | |
859 | while ((err=it.next()) == NO_ERROR) { | |
860 | String8 src = it.getFile()->getPrintableSource(); | |
15c62a5b | 861 | err = compileXmlFile(assets, it.getFile(), &table, xmlFlags); |
a534180c TAOSP |
862 | if (err != NO_ERROR) { |
863 | hasErrors = true; | |
864 | } | |
865 | ResXMLTree block; | |
866 | block.setTo(it.getFile()->getData(), it.getFile()->getSize(), true); | |
867 | checkForIds(src, block); | |
868 | } | |
869 | ||
870 | if (err < NO_ERROR) { | |
871 | hasErrors = true; | |
872 | } | |
873 | err = NO_ERROR; | |
874 | } | |
875 | ||
876 | const sp<AaptFile> manifestFile(androidManifestFile->getFiles().valueAt(0)); | |
877 | String8 manifestPath(manifestFile->getPrintableSource()); | |
878 | ||
879 | // Perform a basic validation of the manifest file. This time we | |
880 | // parse it with the comments intact, so that we can use them to | |
881 | // generate java docs... so we are not going to write this one | |
882 | // back out to the final manifest data. | |
883 | err = compileXmlFile(assets, manifestFile, &table, | |
884 | XML_COMPILE_ASSIGN_ATTRIBUTE_IDS | |
885 | | XML_COMPILE_STRIP_WHITESPACE | XML_COMPILE_STRIP_RAW_VALUES); | |
886 | if (err < NO_ERROR) { | |
887 | return err; | |
888 | } | |
889 | ResXMLTree block; | |
890 | block.setTo(manifestFile->getData(), manifestFile->getSize(), true); | |
891 | String16 manifest16("manifest"); | |
892 | String16 permission16("permission"); | |
893 | String16 permission_group16("permission-group"); | |
894 | String16 uses_permission16("uses-permission"); | |
895 | String16 instrumentation16("instrumentation"); | |
896 | String16 application16("application"); | |
897 | String16 provider16("provider"); | |
898 | String16 service16("service"); | |
899 | String16 receiver16("receiver"); | |
900 | String16 activity16("activity"); | |
901 | String16 action16("action"); | |
902 | String16 category16("category"); | |
903 | String16 data16("scheme"); | |
904 | const char* packageIdentChars = "abcdefghijklmnopqrstuvwxyz" | |
905 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789"; | |
906 | const char* packageIdentCharsWithTheStupid = "abcdefghijklmnopqrstuvwxyz" | |
907 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789-"; | |
908 | const char* classIdentChars = "abcdefghijklmnopqrstuvwxyz" | |
909 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789$"; | |
910 | const char* processIdentChars = "abcdefghijklmnopqrstuvwxyz" | |
911 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789:"; | |
912 | const char* authoritiesIdentChars = "abcdefghijklmnopqrstuvwxyz" | |
913 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789-:;"; | |
914 | const char* typeIdentChars = "abcdefghijklmnopqrstuvwxyz" | |
915 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789:-/*+"; | |
916 | const char* schemeIdentChars = "abcdefghijklmnopqrstuvwxyz" | |
917 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789-"; | |
918 | ResXMLTree::event_code_t code; | |
919 | sp<AaptSymbols> permissionSymbols; | |
920 | sp<AaptSymbols> permissionGroupSymbols; | |
921 | while ((code=block.next()) != ResXMLTree::END_DOCUMENT | |
922 | && code > ResXMLTree::BAD_DOCUMENT) { | |
923 | if (code == ResXMLTree::START_TAG) { | |
924 | size_t len; | |
925 | if (block.getElementNamespace(&len) != NULL) { | |
926 | continue; | |
927 | } | |
928 | if (strcmp16(block.getElementName(&len), manifest16.string()) == 0) { | |
929 | if (validateAttr(manifestPath, block, NULL, "package", | |
930 | packageIdentChars, true) != ATTR_OKAY) { | |
931 | hasErrors = true; | |
932 | } | |
933 | } else if (strcmp16(block.getElementName(&len), permission16.string()) == 0 | |
934 | || strcmp16(block.getElementName(&len), permission_group16.string()) == 0) { | |
935 | const bool isGroup = strcmp16(block.getElementName(&len), | |
936 | permission_group16.string()) == 0; | |
937 | if (validateAttr(manifestPath, block, RESOURCES_ANDROID_NAMESPACE, "name", | |
938 | isGroup ? packageIdentCharsWithTheStupid | |
939 | : packageIdentChars, true) != ATTR_OKAY) { | |
940 | hasErrors = true; | |
941 | } | |
942 | SourcePos srcPos(manifestPath, block.getLineNumber()); | |
943 | sp<AaptSymbols> syms; | |
944 | if (!isGroup) { | |
945 | syms = permissionSymbols; | |
946 | if (syms == NULL) { | |
947 | sp<AaptSymbols> symbols = | |
948 | assets->getSymbolsFor(String8("Manifest")); | |
949 | syms = permissionSymbols = symbols->addNestedSymbol( | |
950 | String8("permission"), srcPos); | |
951 | } | |
952 | } else { | |
953 | syms = permissionGroupSymbols; | |
954 | if (syms == NULL) { | |
955 | sp<AaptSymbols> symbols = | |
956 | assets->getSymbolsFor(String8("Manifest")); | |
957 | syms = permissionGroupSymbols = symbols->addNestedSymbol( | |
958 | String8("permission_group"), srcPos); | |
959 | } | |
960 | } | |
961 | size_t len; | |
962 | ssize_t index = block.indexOfAttribute(RESOURCES_ANDROID_NAMESPACE, "name"); | |
963 | const uint16_t* id = block.getAttributeStringValue(index, &len); | |
964 | if (id == NULL) { | |
965 | fprintf(stderr, "%s:%d: missing name attribute in element <%s>.\n", | |
966 | manifestPath.string(), block.getLineNumber(), | |
967 | String8(block.getElementName(&len)).string()); | |
968 | hasErrors = true; | |
969 | break; | |
970 | } | |
971 | String8 idStr(id); | |
972 | char* p = idStr.lockBuffer(idStr.size()); | |
973 | char* e = p + idStr.size(); | |
974 | bool begins_with_digit = true; // init to true so an empty string fails | |
975 | while (e > p) { | |
976 | e--; | |
977 | if (*e >= '0' && *e <= '9') { | |
978 | begins_with_digit = true; | |
979 | continue; | |
980 | } | |
981 | if ((*e >= 'a' && *e <= 'z') || | |
982 | (*e >= 'A' && *e <= 'Z') || | |
983 | (*e == '_')) { | |
984 | begins_with_digit = false; | |
985 | continue; | |
986 | } | |
987 | if (isGroup && (*e == '-')) { | |
988 | *e = '_'; | |
989 | begins_with_digit = false; | |
990 | continue; | |
991 | } | |
992 | e++; | |
993 | break; | |
994 | } | |
995 | idStr.unlockBuffer(); | |
996 | // verify that we stopped because we hit a period or | |
997 | // the beginning of the string, and that the | |
998 | // identifier didn't begin with a digit. | |
999 | if (begins_with_digit || (e != p && *(e-1) != '.')) { | |
1000 | fprintf(stderr, | |
1001 | "%s:%d: Permission name <%s> is not a valid Java symbol\n", | |
1002 | manifestPath.string(), block.getLineNumber(), idStr.string()); | |
1003 | hasErrors = true; | |
1004 | } | |
1005 | syms->addStringSymbol(String8(e), idStr, srcPos); | |
1006 | const uint16_t* cmt = block.getComment(&len); | |
1007 | if (cmt != NULL && *cmt != 0) { | |
1008 | //printf("Comment of %s: %s\n", String8(e).string(), | |
1009 | // String8(cmt).string()); | |
1010 | syms->appendComment(String8(e), String16(cmt), srcPos); | |
1011 | } else { | |
1012 | //printf("No comment for %s\n", String8(e).string()); | |
1013 | } | |
1014 | syms->makeSymbolPublic(String8(e), srcPos); | |
1015 | } else if (strcmp16(block.getElementName(&len), uses_permission16.string()) == 0) { | |
1016 | if (validateAttr(manifestPath, block, RESOURCES_ANDROID_NAMESPACE, "name", | |
1017 | packageIdentChars, true) != ATTR_OKAY) { | |
1018 | hasErrors = true; | |
1019 | } | |
1020 | } else if (strcmp16(block.getElementName(&len), instrumentation16.string()) == 0) { | |
1021 | if (validateAttr(manifestPath, block, RESOURCES_ANDROID_NAMESPACE, "name", | |
1022 | classIdentChars, true) != ATTR_OKAY) { | |
1023 | hasErrors = true; | |
1024 | } | |
1025 | if (validateAttr(manifestPath, block, | |
1026 | RESOURCES_ANDROID_NAMESPACE, "targetPackage", | |
1027 | packageIdentChars, true) != ATTR_OKAY) { | |
1028 | hasErrors = true; | |
1029 | } | |
1030 | } else if (strcmp16(block.getElementName(&len), application16.string()) == 0) { | |
1031 | if (validateAttr(manifestPath, block, RESOURCES_ANDROID_NAMESPACE, "name", | |
1032 | classIdentChars, false) != ATTR_OKAY) { | |
1033 | hasErrors = true; | |
1034 | } | |
1035 | if (validateAttr(manifestPath, block, | |
1036 | RESOURCES_ANDROID_NAMESPACE, "permission", | |
1037 | packageIdentChars, false) != ATTR_OKAY) { | |
1038 | hasErrors = true; | |
1039 | } | |
1040 | if (validateAttr(manifestPath, block, | |
1041 | RESOURCES_ANDROID_NAMESPACE, "process", | |
1042 | processIdentChars, false) != ATTR_OKAY) { | |
1043 | hasErrors = true; | |
1044 | } | |
1045 | if (validateAttr(manifestPath, block, | |
1046 | RESOURCES_ANDROID_NAMESPACE, "taskAffinity", | |
1047 | processIdentChars, false) != ATTR_OKAY) { | |
1048 | hasErrors = true; | |
1049 | } | |
1050 | } else if (strcmp16(block.getElementName(&len), provider16.string()) == 0) { | |
1051 | if (validateAttr(manifestPath, block, RESOURCES_ANDROID_NAMESPACE, "name", | |
1052 | classIdentChars, true) != ATTR_OKAY) { | |
1053 | hasErrors = true; | |
1054 | } | |
1055 | if (validateAttr(manifestPath, block, | |
1056 | RESOURCES_ANDROID_NAMESPACE, "authorities", | |
1057 | authoritiesIdentChars, true) != ATTR_OKAY) { | |
1058 | hasErrors = true; | |
1059 | } | |
1060 | if (validateAttr(manifestPath, block, | |
1061 | RESOURCES_ANDROID_NAMESPACE, "permission", | |
1062 | packageIdentChars, false) != ATTR_OKAY) { | |
1063 | hasErrors = true; | |
1064 | } | |
1065 | if (validateAttr(manifestPath, block, | |
1066 | RESOURCES_ANDROID_NAMESPACE, "process", | |
1067 | processIdentChars, false) != ATTR_OKAY) { | |
1068 | hasErrors = true; | |
1069 | } | |
1070 | } else if (strcmp16(block.getElementName(&len), service16.string()) == 0 | |
1071 | || strcmp16(block.getElementName(&len), receiver16.string()) == 0 | |
1072 | || strcmp16(block.getElementName(&len), activity16.string()) == 0) { | |
1073 | if (validateAttr(manifestPath, block, RESOURCES_ANDROID_NAMESPACE, "name", | |
1074 | classIdentChars, true) != ATTR_OKAY) { | |
1075 | hasErrors = true; | |
1076 | } | |
1077 | if (validateAttr(manifestPath, block, | |
1078 | RESOURCES_ANDROID_NAMESPACE, "permission", | |
1079 | packageIdentChars, false) != ATTR_OKAY) { | |
1080 | hasErrors = true; | |
1081 | } | |
1082 | if (validateAttr(manifestPath, block, | |
1083 | RESOURCES_ANDROID_NAMESPACE, "process", | |
1084 | processIdentChars, false) != ATTR_OKAY) { | |
1085 | hasErrors = true; | |
1086 | } | |
1087 | if (validateAttr(manifestPath, block, | |
1088 | RESOURCES_ANDROID_NAMESPACE, "taskAffinity", | |
1089 | processIdentChars, false) != ATTR_OKAY) { | |
1090 | hasErrors = true; | |
1091 | } | |
1092 | } else if (strcmp16(block.getElementName(&len), action16.string()) == 0 | |
1093 | || strcmp16(block.getElementName(&len), category16.string()) == 0) { | |
1094 | if (validateAttr(manifestPath, block, | |
1095 | RESOURCES_ANDROID_NAMESPACE, "name", | |
1096 | packageIdentChars, true) != ATTR_OKAY) { | |
1097 | hasErrors = true; | |
1098 | } | |
1099 | } else if (strcmp16(block.getElementName(&len), data16.string()) == 0) { | |
1100 | if (validateAttr(manifestPath, block, | |
1101 | RESOURCES_ANDROID_NAMESPACE, "mimeType", | |
1102 | typeIdentChars, true) != ATTR_OKAY) { | |
1103 | hasErrors = true; | |
1104 | } | |
1105 | if (validateAttr(manifestPath, block, | |
1106 | RESOURCES_ANDROID_NAMESPACE, "scheme", | |
1107 | schemeIdentChars, true) != ATTR_OKAY) { | |
1108 | hasErrors = true; | |
1109 | } | |
1110 | } | |
1111 | } | |
1112 | } | |
1113 | ||
1114 | if (table.validateLocalizations()) { | |
1115 | hasErrors = true; | |
1116 | } | |
1117 | ||
1118 | if (hasErrors) { | |
1119 | return UNKNOWN_ERROR; | |
1120 | } | |
1121 | ||
1122 | // Generate final compiled manifest file. | |
1123 | manifestFile->clearData(); | |
e942a5c2 DH |
1124 | sp<XMLNode> manifestTree = XMLNode::parse(manifestFile); |
1125 | if (manifestTree == NULL) { | |
1126 | return UNKNOWN_ERROR; | |
1127 | } | |
1128 | err = massageManifest(bundle, manifestTree); | |
1129 | if (err < NO_ERROR) { | |
1130 | return err; | |
1131 | } | |
1132 | err = compileXmlFile(assets, manifestTree, manifestFile, &table); | |
a534180c TAOSP |
1133 | if (err < NO_ERROR) { |
1134 | return err; | |
1135 | } | |
1136 | ||
1137 | //block.restart(); | |
1138 | //printXMLBlock(&block); | |
1139 | ||
1140 | // -------------------------------------------------------------- | |
1141 | // Generate the final resource table. | |
1142 | // Re-flatten because we may have added new resource IDs | |
1143 | // -------------------------------------------------------------- | |
1144 | ||
1145 | if (table.hasResources()) { | |
1146 | sp<AaptSymbols> symbols = assets->getSymbolsFor(String8("R")); | |
1147 | err = table.addSymbols(symbols); | |
1148 | if (err < NO_ERROR) { | |
1149 | return err; | |
1150 | } | |
1151 | ||
1152 | sp<AaptFile> resFile(getResourceFile(assets)); | |
1153 | if (resFile == NULL) { | |
1154 | fprintf(stderr, "Error: unable to generate entry for resource data\n"); | |
1155 | return UNKNOWN_ERROR; | |
1156 | } | |
1157 | ||
1158 | err = table.flatten(bundle, resFile); | |
1159 | if (err < NO_ERROR) { | |
1160 | return err; | |
1161 | } | |
1162 | ||
1163 | if (bundle->getPublicOutputFile()) { | |
1164 | FILE* fp = fopen(bundle->getPublicOutputFile(), "w+"); | |
1165 | if (fp == NULL) { | |
1166 | fprintf(stderr, "ERROR: Unable to open public definitions output file %s: %s\n", | |
1167 | (const char*)bundle->getPublicOutputFile(), strerror(errno)); | |
1168 | return UNKNOWN_ERROR; | |
1169 | } | |
1170 | if (bundle->getVerbose()) { | |
1171 | printf(" Writing public definitions to %s.\n", bundle->getPublicOutputFile()); | |
1172 | } | |
1173 | table.writePublicDefinitions(String16(assets->getPackage()), fp); | |
981d1579 | 1174 | fclose(fp); |
a534180c TAOSP |
1175 | } |
1176 | ||
1177 | NOISY( | |
1178 | ResTable rt; | |
1179 | rt.add(resFile->getData(), resFile->getSize(), NULL); | |
1180 | printf("Generated resources:\n"); | |
1181 | rt.print(); | |
1182 | ) | |
1183 | ||
1184 | // These resources are now considered to be a part of the included | |
1185 | // resources, for others to reference. | |
1186 | err = assets->addIncludedResources(resFile); | |
1187 | if (err < NO_ERROR) { | |
1188 | fprintf(stderr, "ERROR: Unable to parse generated resources, aborting.\n"); | |
1189 | return err; | |
1190 | } | |
1191 | } | |
a534180c TAOSP |
1192 | return err; |
1193 | } | |
1194 | ||
1195 | static const char* getIndentSpace(int indent) | |
1196 | { | |
1197 | static const char whitespace[] = | |
1198 | " "; | |
1199 | ||
1200 | return whitespace + sizeof(whitespace) - 1 - indent*4; | |
1201 | } | |
1202 | ||
1203 | static status_t fixupSymbol(String16* inoutSymbol) | |
1204 | { | |
1205 | inoutSymbol->replaceAll('.', '_'); | |
1206 | inoutSymbol->replaceAll(':', '_'); | |
1207 | return NO_ERROR; | |
1208 | } | |
1209 | ||
1210 | static String16 getAttributeComment(const sp<AaptAssets>& assets, | |
1211 | const String8& name, | |
1212 | String16* outTypeComment = NULL) | |
1213 | { | |
1214 | sp<AaptSymbols> asym = assets->getSymbolsFor(String8("R")); | |
1215 | if (asym != NULL) { | |
1216 | //printf("Got R symbols!\n"); | |
1217 | asym = asym->getNestedSymbols().valueFor(String8("attr")); | |
1218 | if (asym != NULL) { | |
1219 | //printf("Got attrs symbols! comment %s=%s\n", | |
1220 | // name.string(), String8(asym->getComment(name)).string()); | |
1221 | if (outTypeComment != NULL) { | |
1222 | *outTypeComment = asym->getTypeComment(name); | |
1223 | } | |
1224 | return asym->getComment(name); | |
1225 | } | |
1226 | } | |
1227 | return String16(); | |
1228 | } | |
1229 | ||
1230 | static status_t writeLayoutClasses( | |
1231 | FILE* fp, const sp<AaptAssets>& assets, | |
1232 | const sp<AaptSymbols>& symbols, int indent, bool includePrivate) | |
1233 | { | |
1234 | const char* indentStr = getIndentSpace(indent); | |
1235 | if (!includePrivate) { | |
1236 | fprintf(fp, "%s/** @doconly */\n", indentStr); | |
1237 | } | |
1238 | fprintf(fp, "%spublic static final class styleable {\n", indentStr); | |
1239 | indent++; | |
1240 | ||
1241 | String16 attr16("attr"); | |
1242 | String16 package16(assets->getPackage()); | |
1243 | ||
1244 | indentStr = getIndentSpace(indent); | |
1245 | bool hasErrors = false; | |
1246 | ||
1247 | size_t i; | |
1248 | size_t N = symbols->getNestedSymbols().size(); | |
1249 | for (i=0; i<N; i++) { | |
1250 | sp<AaptSymbols> nsymbols = symbols->getNestedSymbols().valueAt(i); | |
1251 | String16 nclassName16(symbols->getNestedSymbols().keyAt(i)); | |
1252 | String8 realClassName(nclassName16); | |
1253 | if (fixupSymbol(&nclassName16) != NO_ERROR) { | |
1254 | hasErrors = true; | |
1255 | } | |
1256 | String8 nclassName(nclassName16); | |
1257 | ||
1258 | SortedVector<uint32_t> idents; | |
1259 | Vector<uint32_t> origOrder; | |
1260 | Vector<bool> publicFlags; | |
1261 | ||
1262 | size_t a; | |
1263 | size_t NA = nsymbols->getSymbols().size(); | |
1264 | for (a=0; a<NA; a++) { | |
1265 | const AaptSymbolEntry& sym(nsymbols->getSymbols().valueAt(a)); | |
1266 | int32_t code = sym.typeCode == AaptSymbolEntry::TYPE_INT32 | |
1267 | ? sym.int32Val : 0; | |
1268 | bool isPublic = true; | |
1269 | if (code == 0) { | |
1270 | String16 name16(sym.name); | |
1271 | uint32_t typeSpecFlags; | |
1272 | code = assets->getIncludedResources().identifierForName( | |
1273 | name16.string(), name16.size(), | |
1274 | attr16.string(), attr16.size(), | |
1275 | package16.string(), package16.size(), &typeSpecFlags); | |
1276 | if (code == 0) { | |
1277 | fprintf(stderr, "ERROR: In <declare-styleable> %s, unable to find attribute %s\n", | |
1278 | nclassName.string(), sym.name.string()); | |
1279 | hasErrors = true; | |
1280 | } | |
1281 | isPublic = (typeSpecFlags&ResTable_typeSpec::SPEC_PUBLIC) != 0; | |
1282 | } | |
1283 | idents.add(code); | |
1284 | origOrder.add(code); | |
1285 | publicFlags.add(isPublic); | |
1286 | } | |
1287 | ||
1288 | NA = idents.size(); | |
1289 | ||
f3cd0b05 DH |
1290 | bool deprecated = false; |
1291 | ||
a534180c TAOSP |
1292 | String16 comment = symbols->getComment(realClassName); |
1293 | fprintf(fp, "%s/** ", indentStr); | |
1294 | if (comment.size() > 0) { | |
f3cd0b05 DH |
1295 | String8 cmt(comment); |
1296 | fprintf(fp, "%s\n", cmt.string()); | |
1297 | if (strstr(cmt.string(), "@deprecated") != NULL) { | |
1298 | deprecated = true; | |
1299 | } | |
a534180c TAOSP |
1300 | } else { |
1301 | fprintf(fp, "Attributes that can be used with a %s.\n", nclassName.string()); | |
1302 | } | |
1303 | bool hasTable = false; | |
1304 | for (a=0; a<NA; a++) { | |
1305 | ssize_t pos = idents.indexOf(origOrder.itemAt(a)); | |
1306 | if (pos >= 0) { | |
1307 | if (!hasTable) { | |
1308 | hasTable = true; | |
1309 | fprintf(fp, | |
1310 | "%s <p>Includes the following attributes:</p>\n" | |
3ff93dd6 | 1311 | "%s <table>\n" |
a534180c TAOSP |
1312 | "%s <colgroup align=\"left\" />\n" |
1313 | "%s <colgroup align=\"left\" />\n" | |
3ff93dd6 | 1314 | "%s <tr><th>Attribute</th><th>Description</th></tr>\n", |
a534180c TAOSP |
1315 | indentStr, |
1316 | indentStr, | |
1317 | indentStr, | |
1318 | indentStr, | |
1319 | indentStr); | |
1320 | } | |
1321 | const AaptSymbolEntry& sym = nsymbols->getSymbols().valueAt(a); | |
1322 | if (!publicFlags.itemAt(a) && !includePrivate) { | |
1323 | continue; | |
1324 | } | |
1325 | String8 name8(sym.name); | |
1326 | String16 comment(sym.comment); | |
1327 | if (comment.size() <= 0) { | |
1328 | comment = getAttributeComment(assets, name8); | |
1329 | } | |
1330 | if (comment.size() > 0) { | |
1331 | const char16_t* p = comment.string(); | |
1332 | while (*p != 0 && *p != '.') { | |
1333 | if (*p == '{') { | |
1334 | while (*p != 0 && *p != '}') { | |
1335 | p++; | |
1336 | } | |
1337 | } else { | |
1338 | p++; | |
1339 | } | |
1340 | } | |
1341 | if (*p == '.') { | |
1342 | p++; | |
1343 | } | |
1344 | comment = String16(comment.string(), p-comment.string()); | |
1345 | } | |
1346 | String16 name(name8); | |
1347 | fixupSymbol(&name); | |
3ff93dd6 | 1348 | fprintf(fp, "%s <tr><td><code>{@link #%s_%s %s:%s}</code></td><td>%s</td></tr>\n", |
a534180c TAOSP |
1349 | indentStr, nclassName.string(), |
1350 | String8(name).string(), | |
1351 | assets->getPackage().string(), | |
1352 | String8(name).string(), | |
1353 | String8(comment).string()); | |
1354 | } | |
1355 | } | |
1356 | if (hasTable) { | |
1357 | fprintf(fp, "%s </table>\n", indentStr); | |
1358 | } | |
1359 | for (a=0; a<NA; a++) { | |
1360 | ssize_t pos = idents.indexOf(origOrder.itemAt(a)); | |
1361 | if (pos >= 0) { | |
1362 | const AaptSymbolEntry& sym = nsymbols->getSymbols().valueAt(a); | |
1363 | if (!publicFlags.itemAt(a) && !includePrivate) { | |
1364 | continue; | |
1365 | } | |
1366 | String16 name(sym.name); | |
1367 | fixupSymbol(&name); | |
1368 | fprintf(fp, "%s @see #%s_%s\n", | |
1369 | indentStr, nclassName.string(), | |
1370 | String8(name).string()); | |
1371 | } | |
1372 | } | |
1373 | fprintf(fp, "%s */\n", getIndentSpace(indent)); | |
1374 | ||
f3cd0b05 DH |
1375 | if (deprecated) { |
1376 | fprintf(fp, "%s@Deprecated\n", indentStr); | |
1377 | } | |
1378 | ||
a534180c TAOSP |
1379 | fprintf(fp, |
1380 | "%spublic static final int[] %s = {\n" | |
1381 | "%s", | |
1382 | indentStr, nclassName.string(), | |
1383 | getIndentSpace(indent+1)); | |
1384 | ||
1385 | for (a=0; a<NA; a++) { | |
1386 | if (a != 0) { | |
1387 | if ((a&3) == 0) { | |
1388 | fprintf(fp, ",\n%s", getIndentSpace(indent+1)); | |
1389 | } else { | |
1390 | fprintf(fp, ", "); | |
1391 | } | |
1392 | } | |
1393 | fprintf(fp, "0x%08x", idents[a]); | |
1394 | } | |
1395 | ||
1396 | fprintf(fp, "\n%s};\n", indentStr); | |
1397 | ||
1398 | for (a=0; a<NA; a++) { | |
1399 | ssize_t pos = idents.indexOf(origOrder.itemAt(a)); | |
1400 | if (pos >= 0) { | |
1401 | const AaptSymbolEntry& sym = nsymbols->getSymbols().valueAt(a); | |
1402 | if (!publicFlags.itemAt(a) && !includePrivate) { | |
1403 | continue; | |
1404 | } | |
1405 | String8 name8(sym.name); | |
1406 | String16 comment(sym.comment); | |
1407 | String16 typeComment; | |
1408 | if (comment.size() <= 0) { | |
1409 | comment = getAttributeComment(assets, name8, &typeComment); | |
1410 | } else { | |
1411 | getAttributeComment(assets, name8, &typeComment); | |
1412 | } | |
1413 | String16 name(name8); | |
1414 | if (fixupSymbol(&name) != NO_ERROR) { | |
1415 | hasErrors = true; | |
1416 | } | |
1417 | ||
1418 | uint32_t typeSpecFlags = 0; | |
1419 | String16 name16(sym.name); | |
1420 | assets->getIncludedResources().identifierForName( | |
1421 | name16.string(), name16.size(), | |
1422 | attr16.string(), attr16.size(), | |
1423 | package16.string(), package16.size(), &typeSpecFlags); | |
1424 | //printf("%s:%s/%s: 0x%08x\n", String8(package16).string(), | |
1425 | // String8(attr16).string(), String8(name16).string(), typeSpecFlags); | |
1426 | const bool pub = (typeSpecFlags&ResTable_typeSpec::SPEC_PUBLIC) != 0; | |
f3cd0b05 DH |
1427 | |
1428 | bool deprecated = false; | |
1429 | ||
a534180c TAOSP |
1430 | fprintf(fp, "%s/**\n", indentStr); |
1431 | if (comment.size() > 0) { | |
f3cd0b05 | 1432 | String8 cmt(comment); |
a534180c | 1433 | fprintf(fp, "%s <p>\n%s @attr description\n", indentStr, indentStr); |
f3cd0b05 DH |
1434 | fprintf(fp, "%s %s\n", indentStr, cmt.string()); |
1435 | if (strstr(cmt.string(), "@deprecated") != NULL) { | |
1436 | deprecated = true; | |
1437 | } | |
a534180c TAOSP |
1438 | } else { |
1439 | fprintf(fp, | |
1440 | "%s <p>This symbol is the offset where the {@link %s.R.attr#%s}\n" | |
1441 | "%s attribute's value can be found in the {@link #%s} array.\n", | |
1442 | indentStr, | |
1443 | pub ? assets->getPackage().string() | |
1444 | : assets->getSymbolsPrivatePackage().string(), | |
1445 | String8(name).string(), | |
1446 | indentStr, nclassName.string()); | |
1447 | } | |
1448 | if (typeComment.size() > 0) { | |
f3cd0b05 DH |
1449 | String8 cmt(typeComment); |
1450 | fprintf(fp, "\n\n%s %s\n", indentStr, cmt.string()); | |
1451 | if (strstr(cmt.string(), "@deprecated") != NULL) { | |
1452 | deprecated = true; | |
1453 | } | |
a534180c TAOSP |
1454 | } |
1455 | if (comment.size() > 0) { | |
1456 | if (pub) { | |
1457 | fprintf(fp, | |
1458 | "%s <p>This corresponds to the global attribute" | |
1459 | "%s resource symbol {@link %s.R.attr#%s}.\n", | |
1460 | indentStr, indentStr, | |
1461 | assets->getPackage().string(), | |
1462 | String8(name).string()); | |
1463 | } else { | |
1464 | fprintf(fp, | |
1465 | "%s <p>This is a private symbol.\n", indentStr); | |
1466 | } | |
1467 | } | |
1468 | fprintf(fp, "%s @attr name %s:%s\n", indentStr, | |
1469 | "android", String8(name).string()); | |
1470 | fprintf(fp, "%s*/\n", indentStr); | |
f3cd0b05 DH |
1471 | if (deprecated) { |
1472 | fprintf(fp, "%s@Deprecated\n", indentStr); | |
1473 | } | |
a534180c TAOSP |
1474 | fprintf(fp, |
1475 | "%spublic static final int %s_%s = %d;\n", | |
1476 | indentStr, nclassName.string(), | |
1477 | String8(name).string(), (int)pos); | |
1478 | } | |
1479 | } | |
1480 | } | |
1481 | ||
1482 | indent--; | |
1483 | fprintf(fp, "%s};\n", getIndentSpace(indent)); | |
1484 | return hasErrors ? UNKNOWN_ERROR : NO_ERROR; | |
1485 | } | |
1486 | ||
1487 | static status_t writeSymbolClass( | |
1488 | FILE* fp, const sp<AaptAssets>& assets, bool includePrivate, | |
1489 | const sp<AaptSymbols>& symbols, const String8& className, int indent) | |
1490 | { | |
1491 | fprintf(fp, "%spublic %sfinal class %s {\n", | |
1492 | getIndentSpace(indent), | |
1493 | indent != 0 ? "static " : "", className.string()); | |
1494 | indent++; | |
1495 | ||
1496 | size_t i; | |
1497 | status_t err = NO_ERROR; | |
1498 | ||
1499 | size_t N = symbols->getSymbols().size(); | |
1500 | for (i=0; i<N; i++) { | |
1501 | const AaptSymbolEntry& sym = symbols->getSymbols().valueAt(i); | |
1502 | if (sym.typeCode != AaptSymbolEntry::TYPE_INT32) { | |
1503 | continue; | |
1504 | } | |
1505 | if (!includePrivate && !sym.isPublic) { | |
1506 | continue; | |
1507 | } | |
1508 | String16 name(sym.name); | |
1509 | String8 realName(name); | |
1510 | if (fixupSymbol(&name) != NO_ERROR) { | |
1511 | return UNKNOWN_ERROR; | |
1512 | } | |
1513 | String16 comment(sym.comment); | |
1514 | bool haveComment = false; | |
f3cd0b05 | 1515 | bool deprecated = false; |
a534180c TAOSP |
1516 | if (comment.size() > 0) { |
1517 | haveComment = true; | |
f3cd0b05 | 1518 | String8 cmt(comment); |
a534180c TAOSP |
1519 | fprintf(fp, |
1520 | "%s/** %s\n", | |
f3cd0b05 DH |
1521 | getIndentSpace(indent), cmt.string()); |
1522 | if (strstr(cmt.string(), "@deprecated") != NULL) { | |
1523 | deprecated = true; | |
1524 | } | |
a534180c TAOSP |
1525 | } else if (sym.isPublic && !includePrivate) { |
1526 | sym.sourcePos.warning("No comment for public symbol %s:%s/%s", | |
1527 | assets->getPackage().string(), className.string(), | |
1528 | String8(sym.name).string()); | |
1529 | } | |
1530 | String16 typeComment(sym.typeComment); | |
1531 | if (typeComment.size() > 0) { | |
f3cd0b05 | 1532 | String8 cmt(typeComment); |
a534180c TAOSP |
1533 | if (!haveComment) { |
1534 | haveComment = true; | |
1535 | fprintf(fp, | |
f3cd0b05 | 1536 | "%s/** %s\n", getIndentSpace(indent), cmt.string()); |
a534180c TAOSP |
1537 | } else { |
1538 | fprintf(fp, | |
f3cd0b05 DH |
1539 | "%s %s\n", getIndentSpace(indent), cmt.string()); |
1540 | } | |
1541 | if (strstr(cmt.string(), "@deprecated") != NULL) { | |
1542 | deprecated = true; | |
a534180c TAOSP |
1543 | } |
1544 | } | |
1545 | if (haveComment) { | |
1546 | fprintf(fp,"%s */\n", getIndentSpace(indent)); | |
1547 | } | |
f3cd0b05 DH |
1548 | if (deprecated) { |
1549 | fprintf(fp, "%s@Deprecated\n", getIndentSpace(indent)); | |
1550 | } | |
a534180c TAOSP |
1551 | fprintf(fp, "%spublic static final int %s=0x%08x;\n", |
1552 | getIndentSpace(indent), | |
1553 | String8(name).string(), (int)sym.int32Val); | |
1554 | } | |
1555 | ||
1556 | for (i=0; i<N; i++) { | |
1557 | const AaptSymbolEntry& sym = symbols->getSymbols().valueAt(i); | |
1558 | if (sym.typeCode != AaptSymbolEntry::TYPE_STRING) { | |
1559 | continue; | |
1560 | } | |
1561 | if (!includePrivate && !sym.isPublic) { | |
1562 | continue; | |
1563 | } | |
1564 | String16 name(sym.name); | |
1565 | if (fixupSymbol(&name) != NO_ERROR) { | |
1566 | return UNKNOWN_ERROR; | |
1567 | } | |
1568 | String16 comment(sym.comment); | |
f3cd0b05 | 1569 | bool deprecated = false; |
a534180c | 1570 | if (comment.size() > 0) { |
f3cd0b05 | 1571 | String8 cmt(comment); |
a534180c TAOSP |
1572 | fprintf(fp, |
1573 | "%s/** %s\n" | |
1574 | "%s */\n", | |
f3cd0b05 | 1575 | getIndentSpace(indent), cmt.string(), |
a534180c | 1576 | getIndentSpace(indent)); |
f3cd0b05 DH |
1577 | if (strstr(cmt.string(), "@deprecated") != NULL) { |
1578 | deprecated = true; | |
1579 | } | |
a534180c TAOSP |
1580 | } else if (sym.isPublic && !includePrivate) { |
1581 | sym.sourcePos.warning("No comment for public symbol %s:%s/%s", | |
1582 | assets->getPackage().string(), className.string(), | |
1583 | String8(sym.name).string()); | |
1584 | } | |
f3cd0b05 DH |
1585 | if (deprecated) { |
1586 | fprintf(fp, "%s@Deprecated\n", getIndentSpace(indent)); | |
1587 | } | |
a534180c TAOSP |
1588 | fprintf(fp, "%spublic static final String %s=\"%s\";\n", |
1589 | getIndentSpace(indent), | |
1590 | String8(name).string(), sym.stringVal.string()); | |
1591 | } | |
1592 | ||
1593 | sp<AaptSymbols> styleableSymbols; | |
1594 | ||
1595 | N = symbols->getNestedSymbols().size(); | |
1596 | for (i=0; i<N; i++) { | |
1597 | sp<AaptSymbols> nsymbols = symbols->getNestedSymbols().valueAt(i); | |
1598 | String8 nclassName(symbols->getNestedSymbols().keyAt(i)); | |
1599 | if (nclassName == "styleable") { | |
1600 | styleableSymbols = nsymbols; | |
1601 | } else { | |
1602 | err = writeSymbolClass(fp, assets, includePrivate, nsymbols, nclassName, indent); | |
1603 | } | |
1604 | if (err != NO_ERROR) { | |
1605 | return err; | |
1606 | } | |
1607 | } | |
1608 | ||
1609 | if (styleableSymbols != NULL) { | |
1610 | err = writeLayoutClasses(fp, assets, styleableSymbols, indent, includePrivate); | |
1611 | if (err != NO_ERROR) { | |
1612 | return err; | |
1613 | } | |
1614 | } | |
1615 | ||
1616 | indent--; | |
1617 | fprintf(fp, "%s}\n", getIndentSpace(indent)); | |
1618 | return NO_ERROR; | |
1619 | } | |
1620 | ||
1621 | status_t writeResourceSymbols(Bundle* bundle, const sp<AaptAssets>& assets, | |
1622 | const String8& package, bool includePrivate) | |
1623 | { | |
1624 | if (!bundle->getRClassDir()) { | |
1625 | return NO_ERROR; | |
1626 | } | |
1627 | ||
1628 | const size_t N = assets->getSymbols().size(); | |
1629 | for (size_t i=0; i<N; i++) { | |
1630 | sp<AaptSymbols> symbols = assets->getSymbols().valueAt(i); | |
1631 | String8 className(assets->getSymbols().keyAt(i)); | |
1632 | String8 dest(bundle->getRClassDir()); | |
1633 | if (bundle->getMakePackageDirs()) { | |
1634 | String8 pkg(package); | |
1635 | const char* last = pkg.string(); | |
1636 | const char* s = last-1; | |
1637 | do { | |
1638 | s++; | |
1639 | if (s > last && (*s == '.' || *s == 0)) { | |
1640 | String8 part(last, s-last); | |
1641 | dest.appendPath(part); | |
1642 | #ifdef HAVE_MS_C_RUNTIME | |
1643 | _mkdir(dest.string()); | |
1644 | #else | |
1645 | mkdir(dest.string(), S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP); | |
1646 | #endif | |
1647 | last = s+1; | |
1648 | } | |
1649 | } while (*s); | |
1650 | } | |
1651 | dest.appendPath(className); | |
1652 | dest.append(".java"); | |
1653 | FILE* fp = fopen(dest.string(), "w+"); | |
1654 | if (fp == NULL) { | |
1655 | fprintf(stderr, "ERROR: Unable to open class file %s: %s\n", | |
1656 | dest.string(), strerror(errno)); | |
1657 | return UNKNOWN_ERROR; | |
1658 | } | |
1659 | if (bundle->getVerbose()) { | |
1660 | printf(" Writing symbols for class %s.\n", className.string()); | |
1661 | } | |
1662 | ||
1663 | fprintf(fp, | |
1664 | "/* AUTO-GENERATED FILE. DO NOT MODIFY.\n" | |
1665 | " *\n" | |
1666 | " * This class was automatically generated by the\n" | |
1667 | " * aapt tool from the resource data it found. It\n" | |
1668 | " * should not be modified by hand.\n" | |
1669 | " */\n" | |
1670 | "\n" | |
1671 | "package %s;\n\n", package.string()); | |
1672 | ||
1673 | status_t err = writeSymbolClass(fp, assets, includePrivate, symbols, className, 0); | |
1674 | if (err != NO_ERROR) { | |
1675 | return err; | |
1676 | } | |
1677 | fclose(fp); | |
1678 | } | |
1679 | ||
1680 | return NO_ERROR; | |
1681 | } | |
6648ff78 JO |
1682 | |
1683 | ||
1684 | ||
1685 | class ProguardKeepSet | |
1686 | { | |
1687 | public: | |
1688 | // { rule --> { file locations } } | |
1689 | KeyedVector<String8, SortedVector<String8> > rules; | |
1690 | ||
1691 | void add(const String8& rule, const String8& where); | |
1692 | }; | |
1693 | ||
1694 | void ProguardKeepSet::add(const String8& rule, const String8& where) | |
1695 | { | |
1696 | ssize_t index = rules.indexOfKey(rule); | |
1697 | if (index < 0) { | |
1698 | index = rules.add(rule, SortedVector<String8>()); | |
1699 | } | |
1700 | rules.editValueAt(index).add(where); | |
1701 | } | |
1702 | ||
1703 | status_t | |
1704 | writeProguardForAndroidManifest(ProguardKeepSet* keep, const sp<AaptAssets>& assets) | |
1705 | { | |
1706 | status_t err; | |
1707 | ResXMLTree tree; | |
1708 | size_t len; | |
1709 | ResXMLTree::event_code_t code; | |
1710 | int depth = 0; | |
1711 | bool inApplication = false; | |
1712 | String8 error; | |
1713 | sp<AaptGroup> assGroup; | |
1714 | sp<AaptFile> assFile; | |
1715 | String8 pkg; | |
1716 | ||
1717 | // First, look for a package file to parse. This is required to | |
1718 | // be able to generate the resource information. | |
1719 | assGroup = assets->getFiles().valueFor(String8("AndroidManifest.xml")); | |
1720 | if (assGroup == NULL) { | |
1721 | fprintf(stderr, "ERROR: No AndroidManifest.xml file found.\n"); | |
1722 | return -1; | |
1723 | } | |
1724 | ||
1725 | if (assGroup->getFiles().size() != 1) { | |
1726 | fprintf(stderr, "warning: Multiple AndroidManifest.xml files found, using %s\n", | |
1727 | assGroup->getFiles().valueAt(0)->getPrintableSource().string()); | |
1728 | } | |
1729 | ||
1730 | assFile = assGroup->getFiles().valueAt(0); | |
1731 | ||
1732 | err = parseXMLResource(assFile, &tree); | |
1733 | if (err != NO_ERROR) { | |
1734 | return err; | |
1735 | } | |
1736 | ||
1737 | tree.restart(); | |
1738 | ||
1739 | while ((code=tree.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) { | |
1740 | if (code == ResXMLTree::END_TAG) { | |
1741 | if (/* name == "Application" && */ depth == 2) { | |
1742 | inApplication = false; | |
1743 | } | |
1744 | depth--; | |
1745 | continue; | |
1746 | } | |
1747 | if (code != ResXMLTree::START_TAG) { | |
1748 | continue; | |
1749 | } | |
1750 | depth++; | |
1751 | String8 tag(tree.getElementName(&len)); | |
1752 | // printf("Depth %d tag %s\n", depth, tag.string()); | |
1753 | if (depth == 1) { | |
1754 | if (tag != "manifest") { | |
1755 | fprintf(stderr, "ERROR: manifest does not start with <manifest> tag\n"); | |
1756 | return -1; | |
1757 | } | |
1758 | pkg = getAttribute(tree, NULL, "package", NULL); | |
1759 | } else if (depth == 2 && tag == "application") { | |
1760 | inApplication = true; | |
1761 | } | |
1d78615e | 1762 | if (inApplication) { |
6648ff78 JO |
1763 | if (tag == "application" || tag == "activity" || tag == "service" || tag == "receiver" |
1764 | || tag == "provider") { | |
1765 | String8 name = getAttribute(tree, "http://schemas.android.com/apk/res/android", | |
1766 | "name", &error); | |
1767 | if (error != "") { | |
1768 | fprintf(stderr, "ERROR: %s\n", error.string()); | |
1769 | return -1; | |
1770 | } | |
1771 | // asdf --> package.asdf | |
1772 | // .asdf .a.b --> package.asdf package.a.b | |
1773 | // asdf.adsf --> asdf.asdf | |
1774 | String8 rule("-keep class "); | |
1775 | const char* p = name.string(); | |
1776 | const char* q = strchr(p, '.'); | |
1777 | if (p == q) { | |
1778 | rule += pkg; | |
1779 | rule += name; | |
1780 | } else if (q == NULL) { | |
1781 | rule += pkg; | |
1782 | rule += "."; | |
1783 | rule += name; | |
1784 | } else { | |
1785 | rule += name; | |
1786 | } | |
1787 | ||
1788 | String8 location = tag; | |
1789 | location += " "; | |
1790 | location += assFile->getSourceFile(); | |
1791 | char lineno[20]; | |
1792 | sprintf(lineno, ":%d", tree.getLineNumber()); | |
1793 | location += lineno; | |
1794 | ||
1795 | keep->add(rule, location); | |
1796 | } | |
1797 | } | |
1798 | } | |
1799 | ||
1800 | return NO_ERROR; | |
1801 | } | |
1802 | ||
1803 | status_t | |
1804 | writeProguardForLayout(ProguardKeepSet* keep, const sp<AaptFile>& layoutFile) | |
1805 | { | |
1806 | status_t err; | |
1807 | ResXMLTree tree; | |
1808 | size_t len; | |
1809 | ResXMLTree::event_code_t code; | |
1810 | ||
1811 | err = parseXMLResource(layoutFile, &tree); | |
1812 | if (err != NO_ERROR) { | |
1813 | return err; | |
1814 | } | |
1815 | ||
1816 | tree.restart(); | |
1817 | ||
1818 | while ((code=tree.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) { | |
1819 | if (code != ResXMLTree::START_TAG) { | |
1820 | continue; | |
1821 | } | |
1822 | String8 tag(tree.getElementName(&len)); | |
1823 | ||
1824 | // If there is no '.', we'll assume that it's one of the built in names. | |
1825 | if (strchr(tag.string(), '.')) { | |
1826 | String8 rule("-keep class "); | |
1827 | rule += tag; | |
1828 | rule += " { <init>(...); }"; | |
1829 | ||
1830 | String8 location("view "); | |
1831 | location += layoutFile->getSourceFile(); | |
1832 | char lineno[20]; | |
1833 | sprintf(lineno, ":%d", tree.getLineNumber()); | |
1834 | location += lineno; | |
1835 | ||
1836 | keep->add(rule, location); | |
1837 | } | |
1838 | } | |
1839 | ||
1840 | return NO_ERROR; | |
1841 | } | |
1842 | ||
1843 | status_t | |
1844 | writeProguardForLayouts(ProguardKeepSet* keep, const sp<AaptAssets>& assets) | |
1845 | { | |
1846 | status_t err; | |
1847 | sp<AaptDir> layout = assets->resDir(String8("layout")); | |
1848 | ||
1849 | if (layout != NULL) { | |
1850 | const KeyedVector<String8,sp<AaptGroup> > groups = layout->getFiles(); | |
1851 | const size_t N = groups.size(); | |
1852 | for (size_t i=0; i<N; i++) { | |
1853 | const sp<AaptGroup>& group = groups.valueAt(i); | |
1854 | const DefaultKeyedVector<AaptGroupEntry, sp<AaptFile> >& files = group->getFiles(); | |
1855 | const size_t M = files.size(); | |
1856 | for (size_t j=0; j<M; j++) { | |
1857 | err = writeProguardForLayout(keep, files.valueAt(j)); | |
1858 | if (err < 0) { | |
1859 | return err; | |
1860 | } | |
1861 | } | |
1862 | } | |
1863 | } | |
1864 | return NO_ERROR; | |
1865 | } | |
1866 | ||
1867 | status_t | |
1868 | writeProguardFile(Bundle* bundle, const sp<AaptAssets>& assets) | |
1869 | { | |
1870 | status_t err = -1; | |
1871 | ||
1872 | if (!bundle->getProguardFile()) { | |
1873 | return NO_ERROR; | |
1874 | } | |
1875 | ||
1876 | ProguardKeepSet keep; | |
1877 | ||
1878 | err = writeProguardForAndroidManifest(&keep, assets); | |
1879 | if (err < 0) { | |
1880 | return err; | |
1881 | } | |
1882 | ||
1883 | err = writeProguardForLayouts(&keep, assets); | |
1884 | if (err < 0) { | |
1885 | return err; | |
1886 | } | |
1887 | ||
1888 | FILE* fp = fopen(bundle->getProguardFile(), "w+"); | |
1889 | if (fp == NULL) { | |
1890 | fprintf(stderr, "ERROR: Unable to open class file %s: %s\n", | |
1891 | bundle->getProguardFile(), strerror(errno)); | |
1892 | return UNKNOWN_ERROR; | |
1893 | } | |
1894 | ||
1895 | const KeyedVector<String8, SortedVector<String8> >& rules = keep.rules; | |
1896 | const size_t N = rules.size(); | |
1897 | for (size_t i=0; i<N; i++) { | |
1898 | const SortedVector<String8>& locations = rules.valueAt(i); | |
1899 | const size_t M = locations.size(); | |
1900 | for (size_t j=0; j<M; j++) { | |
1901 | fprintf(fp, "# %s\n", locations.itemAt(j).string()); | |
1902 | } | |
1903 | fprintf(fp, "%s\n\n", rules.keyAt(i).string()); | |
1904 | } | |
1905 | fclose(fp); | |
1906 | ||
1907 | return err; | |
1908 | } |