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