]> git.saurik.com Git - android/aapt.git/blob - ResourceTable.cpp
85665e0c17b371ec318267368a1230fe40b9143d
[android/aapt.git] / ResourceTable.cpp
1 //
2 // Copyright 2006 The Android Open Source Project
3 //
4 // Build resource files from raw assets.
5 //
6
7 #include "ResourceTable.h"
8
9 #include "XMLNode.h"
10
11 #include <utils/ByteOrder.h>
12 #include <utils/ResourceTypes.h>
13 #include <stdarg.h>
14
15 #define NOISY(x) //x
16 #define NOISY_REF(x) //x
17
18 status_t compileXmlFile(const sp<AaptAssets>& assets,
19 const sp<AaptFile>& target,
20 ResourceTable* table,
21 int options)
22 {
23 sp<XMLNode> root = XMLNode::parse(target);
24 if (root == NULL) {
25 return UNKNOWN_ERROR;
26 }
27
28 return compileXmlFile(assets, root, target, table, options);
29 }
30
31 status_t compileXmlFile(const sp<AaptAssets>& assets,
32 const sp<AaptFile>& target,
33 const sp<AaptFile>& outTarget,
34 ResourceTable* table,
35 int options)
36 {
37 sp<XMLNode> root = XMLNode::parse(target);
38 if (root == NULL) {
39 return UNKNOWN_ERROR;
40 }
41
42 return compileXmlFile(assets, root, outTarget, table, options);
43 }
44
45 status_t compileXmlFile(const sp<AaptAssets>& assets,
46 const sp<XMLNode>& root,
47 const sp<AaptFile>& target,
48 ResourceTable* table,
49 int options)
50 {
51 if ((options&XML_COMPILE_STRIP_WHITESPACE) != 0) {
52 root->removeWhitespace(true, NULL);
53 } else if ((options&XML_COMPILE_COMPACT_WHITESPACE) != 0) {
54 root->removeWhitespace(false, NULL);
55 }
56
57 if ((options&XML_COMPILE_UTF8) != 0) {
58 root->setUTF8(true);
59 }
60
61 bool hasErrors = false;
62
63 if ((options&XML_COMPILE_ASSIGN_ATTRIBUTE_IDS) != 0) {
64 status_t err = root->assignResourceIds(assets, table);
65 if (err != NO_ERROR) {
66 hasErrors = true;
67 }
68 }
69
70 status_t err = root->parseValues(assets, table);
71 if (err != NO_ERROR) {
72 hasErrors = true;
73 }
74
75 if (hasErrors) {
76 return UNKNOWN_ERROR;
77 }
78
79 NOISY(printf("Input XML Resource:\n"));
80 NOISY(root->print());
81 err = root->flatten(target,
82 (options&XML_COMPILE_STRIP_COMMENTS) != 0,
83 (options&XML_COMPILE_STRIP_RAW_VALUES) != 0);
84 if (err != NO_ERROR) {
85 return err;
86 }
87
88 NOISY(printf("Output XML Resource:\n"));
89 NOISY(ResXMLTree tree;
90 tree.setTo(target->getData(), target->getSize());
91 printXMLBlock(&tree));
92
93 target->setCompressionMethod(ZipEntry::kCompressDeflated);
94
95 return err;
96 }
97
98 #undef NOISY
99 #define NOISY(x) //x
100
101 struct flag_entry
102 {
103 const char16_t* name;
104 size_t nameLen;
105 uint32_t value;
106 const char* description;
107 };
108
109 static const char16_t referenceArray[] =
110 { 'r', 'e', 'f', 'e', 'r', 'e', 'n', 'c', 'e' };
111 static const char16_t stringArray[] =
112 { 's', 't', 'r', 'i', 'n', 'g' };
113 static const char16_t integerArray[] =
114 { 'i', 'n', 't', 'e', 'g', 'e', 'r' };
115 static const char16_t booleanArray[] =
116 { 'b', 'o', 'o', 'l', 'e', 'a', 'n' };
117 static const char16_t colorArray[] =
118 { 'c', 'o', 'l', 'o', 'r' };
119 static const char16_t floatArray[] =
120 { 'f', 'l', 'o', 'a', 't' };
121 static const char16_t dimensionArray[] =
122 { 'd', 'i', 'm', 'e', 'n', 's', 'i', 'o', 'n' };
123 static const char16_t fractionArray[] =
124 { 'f', 'r', 'a', 'c', 't', 'i', 'o', 'n' };
125 static const char16_t enumArray[] =
126 { 'e', 'n', 'u', 'm' };
127 static const char16_t flagsArray[] =
128 { 'f', 'l', 'a', 'g', 's' };
129
130 static const flag_entry gFormatFlags[] = {
131 { referenceArray, sizeof(referenceArray)/2, ResTable_map::TYPE_REFERENCE,
132 "a reference to another resource, in the form \"<code>@[+][<i>package</i>:]<i>type</i>:<i>name</i></code>\"\n"
133 "or to a theme attribute in the form \"<code>?[<i>package</i>:][<i>type</i>:]<i>name</i></code>\"."},
134 { stringArray, sizeof(stringArray)/2, ResTable_map::TYPE_STRING,
135 "a string value, using '\\\\;' to escape characters such as '\\\\n' or '\\\\uxxxx' for a unicode character." },
136 { integerArray, sizeof(integerArray)/2, ResTable_map::TYPE_INTEGER,
137 "an integer value, such as \"<code>100</code>\"." },
138 { booleanArray, sizeof(booleanArray)/2, ResTable_map::TYPE_BOOLEAN,
139 "a boolean value, either \"<code>true</code>\" or \"<code>false</code>\"." },
140 { colorArray, sizeof(colorArray)/2, ResTable_map::TYPE_COLOR,
141 "a color value, in the form of \"<code>#<i>rgb</i></code>\", \"<code>#<i>argb</i></code>\",\n"
142 "\"<code>#<i>rrggbb</i></code>\", or \"<code>#<i>aarrggbb</i></code>\"." },
143 { floatArray, sizeof(floatArray)/2, ResTable_map::TYPE_FLOAT,
144 "a floating point value, such as \"<code>1.2</code>\"."},
145 { dimensionArray, sizeof(dimensionArray)/2, ResTable_map::TYPE_DIMENSION,
146 "a dimension value, which is a floating point number appended with a unit such as \"<code>14.5sp</code>\".\n"
147 "Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size),\n"
148 "in (inches), mm (millimeters)." },
149 { fractionArray, sizeof(fractionArray)/2, ResTable_map::TYPE_FRACTION,
150 "a fractional value, which is a floating point number appended with either % or %p, such as \"<code>14.5%</code>\".\n"
151 "The % suffix always means a percentage of the base size; the optional %p suffix provides a size relative to\n"
152 "some parent container." },
153 { enumArray, sizeof(enumArray)/2, ResTable_map::TYPE_ENUM, NULL },
154 { flagsArray, sizeof(flagsArray)/2, ResTable_map::TYPE_FLAGS, NULL },
155 { NULL, 0, 0, NULL }
156 };
157
158 static const char16_t suggestedArray[] = { 's', 'u', 'g', 'g', 'e', 's', 't', 'e', 'd' };
159
160 static const flag_entry l10nRequiredFlags[] = {
161 { suggestedArray, sizeof(suggestedArray)/2, ResTable_map::L10N_SUGGESTED, NULL },
162 { NULL, 0, 0, NULL }
163 };
164
165 static const char16_t nulStr[] = { 0 };
166
167 static uint32_t parse_flags(const char16_t* str, size_t len,
168 const flag_entry* flags, bool* outError = NULL)
169 {
170 while (len > 0 && isspace(*str)) {
171 str++;
172 len--;
173 }
174 while (len > 0 && isspace(str[len-1])) {
175 len--;
176 }
177
178 const char16_t* const end = str + len;
179 uint32_t value = 0;
180
181 while (str < end) {
182 const char16_t* div = str;
183 while (div < end && *div != '|') {
184 div++;
185 }
186
187 const flag_entry* cur = flags;
188 while (cur->name) {
189 if (strzcmp16(cur->name, cur->nameLen, str, div-str) == 0) {
190 value |= cur->value;
191 break;
192 }
193 cur++;
194 }
195
196 if (!cur->name) {
197 if (outError) *outError = true;
198 return 0;
199 }
200
201 str = div < end ? div+1 : div;
202 }
203
204 if (outError) *outError = false;
205 return value;
206 }
207
208 static String16 mayOrMust(int type, int flags)
209 {
210 if ((type&(~flags)) == 0) {
211 return String16("<p>Must");
212 }
213
214 return String16("<p>May");
215 }
216
217 static void appendTypeInfo(ResourceTable* outTable, const String16& pkg,
218 const String16& typeName, const String16& ident, int type,
219 const flag_entry* flags)
220 {
221 bool hadType = false;
222 while (flags->name) {
223 if ((type&flags->value) != 0 && flags->description != NULL) {
224 String16 fullMsg(mayOrMust(type, flags->value));
225 fullMsg.append(String16(" be "));
226 fullMsg.append(String16(flags->description));
227 outTable->appendTypeComment(pkg, typeName, ident, fullMsg);
228 hadType = true;
229 }
230 flags++;
231 }
232 if (hadType && (type&ResTable_map::TYPE_REFERENCE) == 0) {
233 outTable->appendTypeComment(pkg, typeName, ident,
234 String16("<p>This may also be a reference to a resource (in the form\n"
235 "\"<code>@[<i>package</i>:]<i>type</i>:<i>name</i></code>\") or\n"
236 "theme attribute (in the form\n"
237 "\"<code>?[<i>package</i>:][<i>type</i>:]<i>name</i></code>\")\n"
238 "containing a value of this type."));
239 }
240 }
241
242 struct PendingAttribute
243 {
244 const String16 myPackage;
245 const SourcePos sourcePos;
246 const bool appendComment;
247 int32_t type;
248 String16 ident;
249 String16 comment;
250 bool hasErrors;
251 bool added;
252
253 PendingAttribute(String16 _package, const sp<AaptFile>& in,
254 ResXMLTree& block, bool _appendComment)
255 : myPackage(_package)
256 , sourcePos(in->getPrintableSource(), block.getLineNumber())
257 , appendComment(_appendComment)
258 , type(ResTable_map::TYPE_ANY)
259 , hasErrors(false)
260 , added(false)
261 {
262 }
263
264 status_t createIfNeeded(ResourceTable* outTable)
265 {
266 if (added || hasErrors) {
267 return NO_ERROR;
268 }
269 added = true;
270
271 String16 attr16("attr");
272
273 if (outTable->hasBagOrEntry(myPackage, attr16, ident)) {
274 sourcePos.error("Attribute \"%s\" has already been defined\n",
275 String8(ident).string());
276 hasErrors = true;
277 return UNKNOWN_ERROR;
278 }
279
280 char numberStr[16];
281 sprintf(numberStr, "%d", type);
282 status_t err = outTable->addBag(sourcePos, myPackage,
283 attr16, ident, String16(""),
284 String16("^type"),
285 String16(numberStr), NULL, NULL);
286 if (err != NO_ERROR) {
287 hasErrors = true;
288 return err;
289 }
290 outTable->appendComment(myPackage, attr16, ident, comment, appendComment);
291 //printf("Attribute %s comment: %s\n", String8(ident).string(),
292 // String8(comment).string());
293 return err;
294 }
295 };
296
297 static status_t compileAttribute(const sp<AaptFile>& in,
298 ResXMLTree& block,
299 const String16& myPackage,
300 ResourceTable* outTable,
301 String16* outIdent = NULL,
302 bool inStyleable = false)
303 {
304 PendingAttribute attr(myPackage, in, block, inStyleable);
305
306 const String16 attr16("attr");
307 const String16 id16("id");
308
309 // Attribute type constants.
310 const String16 enum16("enum");
311 const String16 flag16("flag");
312
313 ResXMLTree::event_code_t code;
314 size_t len;
315 status_t err;
316
317 ssize_t identIdx = block.indexOfAttribute(NULL, "name");
318 if (identIdx >= 0) {
319 attr.ident = String16(block.getAttributeStringValue(identIdx, &len));
320 if (outIdent) {
321 *outIdent = attr.ident;
322 }
323 } else {
324 attr.sourcePos.error("A 'name' attribute is required for <attr>\n");
325 attr.hasErrors = true;
326 }
327
328 attr.comment = String16(
329 block.getComment(&len) ? block.getComment(&len) : nulStr);
330
331 ssize_t typeIdx = block.indexOfAttribute(NULL, "format");
332 if (typeIdx >= 0) {
333 String16 typeStr = String16(block.getAttributeStringValue(typeIdx, &len));
334 attr.type = parse_flags(typeStr.string(), typeStr.size(), gFormatFlags);
335 if (attr.type == 0) {
336 attr.sourcePos.error("Tag <attr> 'format' attribute value \"%s\" not valid\n",
337 String8(typeStr).string());
338 attr.hasErrors = true;
339 }
340 attr.createIfNeeded(outTable);
341 } else if (!inStyleable) {
342 // Attribute definitions outside of styleables always define the
343 // attribute as a generic value.
344 attr.createIfNeeded(outTable);
345 }
346
347 //printf("Attribute %s: type=0x%08x\n", String8(attr.ident).string(), attr.type);
348
349 ssize_t minIdx = block.indexOfAttribute(NULL, "min");
350 if (minIdx >= 0) {
351 String16 val = String16(block.getAttributeStringValue(minIdx, &len));
352 if (!ResTable::stringToInt(val.string(), val.size(), NULL)) {
353 attr.sourcePos.error("Tag <attr> 'min' attribute must be a number, not \"%s\"\n",
354 String8(val).string());
355 attr.hasErrors = true;
356 }
357 attr.createIfNeeded(outTable);
358 if (!attr.hasErrors) {
359 err = outTable->addBag(attr.sourcePos, myPackage, attr16, attr.ident,
360 String16(""), String16("^min"), String16(val), NULL, NULL);
361 if (err != NO_ERROR) {
362 attr.hasErrors = true;
363 }
364 }
365 }
366
367 ssize_t maxIdx = block.indexOfAttribute(NULL, "max");
368 if (maxIdx >= 0) {
369 String16 val = String16(block.getAttributeStringValue(maxIdx, &len));
370 if (!ResTable::stringToInt(val.string(), val.size(), NULL)) {
371 attr.sourcePos.error("Tag <attr> 'max' attribute must be a number, not \"%s\"\n",
372 String8(val).string());
373 attr.hasErrors = true;
374 }
375 attr.createIfNeeded(outTable);
376 if (!attr.hasErrors) {
377 err = outTable->addBag(attr.sourcePos, myPackage, attr16, attr.ident,
378 String16(""), String16("^max"), String16(val), NULL, NULL);
379 attr.hasErrors = true;
380 }
381 }
382
383 if ((minIdx >= 0 || maxIdx >= 0) && (attr.type&ResTable_map::TYPE_INTEGER) == 0) {
384 attr.sourcePos.error("Tag <attr> must have format=integer attribute if using max or min\n");
385 attr.hasErrors = true;
386 }
387
388 ssize_t l10nIdx = block.indexOfAttribute(NULL, "localization");
389 if (l10nIdx >= 0) {
390 const uint16_t* str = block.getAttributeStringValue(l10nIdx, &len);
391 bool error;
392 uint32_t l10n_required = parse_flags(str, len, l10nRequiredFlags, &error);
393 if (error) {
394 attr.sourcePos.error("Tag <attr> 'localization' attribute value \"%s\" not valid\n",
395 String8(str).string());
396 attr.hasErrors = true;
397 }
398 attr.createIfNeeded(outTable);
399 if (!attr.hasErrors) {
400 char buf[11];
401 sprintf(buf, "%d", l10n_required);
402 err = outTable->addBag(attr.sourcePos, myPackage, attr16, attr.ident,
403 String16(""), String16("^l10n"), String16(buf), NULL, NULL);
404 if (err != NO_ERROR) {
405 attr.hasErrors = true;
406 }
407 }
408 }
409
410 String16 enumOrFlagsComment;
411
412 while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) {
413 if (code == ResXMLTree::START_TAG) {
414 uint32_t localType = 0;
415 if (strcmp16(block.getElementName(&len), enum16.string()) == 0) {
416 localType = ResTable_map::TYPE_ENUM;
417 } else if (strcmp16(block.getElementName(&len), flag16.string()) == 0) {
418 localType = ResTable_map::TYPE_FLAGS;
419 } else {
420 SourcePos(in->getPrintableSource(), block.getLineNumber())
421 .error("Tag <%s> can not appear inside <attr>, only <enum> or <flag>\n",
422 String8(block.getElementName(&len)).string());
423 return UNKNOWN_ERROR;
424 }
425
426 attr.createIfNeeded(outTable);
427
428 if (attr.type == ResTable_map::TYPE_ANY) {
429 // No type was explicitly stated, so supplying enum tags
430 // implicitly creates an enum or flag.
431 attr.type = 0;
432 }
433
434 if ((attr.type&(ResTable_map::TYPE_ENUM|ResTable_map::TYPE_FLAGS)) == 0) {
435 // Wasn't originally specified as an enum, so update its type.
436 attr.type |= localType;
437 if (!attr.hasErrors) {
438 char numberStr[16];
439 sprintf(numberStr, "%d", attr.type);
440 err = outTable->addBag(SourcePos(in->getPrintableSource(), block.getLineNumber()),
441 myPackage, attr16, attr.ident, String16(""),
442 String16("^type"), String16(numberStr), NULL, NULL, true);
443 if (err != NO_ERROR) {
444 attr.hasErrors = true;
445 }
446 }
447 } else if ((uint32_t)(attr.type&(ResTable_map::TYPE_ENUM|ResTable_map::TYPE_FLAGS)) != localType) {
448 if (localType == ResTable_map::TYPE_ENUM) {
449 SourcePos(in->getPrintableSource(), block.getLineNumber())
450 .error("<enum> attribute can not be used inside a flags format\n");
451 attr.hasErrors = true;
452 } else {
453 SourcePos(in->getPrintableSource(), block.getLineNumber())
454 .error("<flag> attribute can not be used inside a enum format\n");
455 attr.hasErrors = true;
456 }
457 }
458
459 String16 itemIdent;
460 ssize_t itemIdentIdx = block.indexOfAttribute(NULL, "name");
461 if (itemIdentIdx >= 0) {
462 itemIdent = String16(block.getAttributeStringValue(itemIdentIdx, &len));
463 } else {
464 SourcePos(in->getPrintableSource(), block.getLineNumber())
465 .error("A 'name' attribute is required for <enum> or <flag>\n");
466 attr.hasErrors = true;
467 }
468
469 String16 value;
470 ssize_t valueIdx = block.indexOfAttribute(NULL, "value");
471 if (valueIdx >= 0) {
472 value = String16(block.getAttributeStringValue(valueIdx, &len));
473 } else {
474 SourcePos(in->getPrintableSource(), block.getLineNumber())
475 .error("A 'value' attribute is required for <enum> or <flag>\n");
476 attr.hasErrors = true;
477 }
478 if (!attr.hasErrors && !ResTable::stringToInt(value.string(), value.size(), NULL)) {
479 SourcePos(in->getPrintableSource(), block.getLineNumber())
480 .error("Tag <enum> or <flag> 'value' attribute must be a number,"
481 " not \"%s\"\n",
482 String8(value).string());
483 attr.hasErrors = true;
484 }
485
486 // Make sure an id is defined for this enum/flag identifier...
487 if (!attr.hasErrors && !outTable->hasBagOrEntry(itemIdent, &id16, &myPackage)) {
488 err = outTable->startBag(SourcePos(in->getPrintableSource(), block.getLineNumber()),
489 myPackage, id16, itemIdent, String16(), NULL);
490 if (err != NO_ERROR) {
491 attr.hasErrors = true;
492 }
493 }
494
495 if (!attr.hasErrors) {
496 if (enumOrFlagsComment.size() == 0) {
497 enumOrFlagsComment.append(mayOrMust(attr.type,
498 ResTable_map::TYPE_ENUM|ResTable_map::TYPE_FLAGS));
499 enumOrFlagsComment.append((attr.type&ResTable_map::TYPE_ENUM)
500 ? String16(" be one of the following constant values.")
501 : String16(" be one or more (separated by '|') of the following constant values."));
502 enumOrFlagsComment.append(String16("</p>\n<table>\n"
503 "<colgroup align=\"left\" />\n"
504 "<colgroup align=\"left\" />\n"
505 "<colgroup align=\"left\" />\n"
506 "<tr><th>Constant</th><th>Value</th><th>Description</th></tr>"));
507 }
508
509 enumOrFlagsComment.append(String16("\n<tr><td><code>"));
510 enumOrFlagsComment.append(itemIdent);
511 enumOrFlagsComment.append(String16("</code></td><td>"));
512 enumOrFlagsComment.append(value);
513 enumOrFlagsComment.append(String16("</td><td>"));
514 if (block.getComment(&len)) {
515 enumOrFlagsComment.append(String16(block.getComment(&len)));
516 }
517 enumOrFlagsComment.append(String16("</td></tr>"));
518
519 err = outTable->addBag(SourcePos(in->getPrintableSource(), block.getLineNumber()),
520 myPackage,
521 attr16, attr.ident, String16(""),
522 itemIdent, value, NULL, NULL, false, true);
523 if (err != NO_ERROR) {
524 attr.hasErrors = true;
525 }
526 }
527 } else if (code == ResXMLTree::END_TAG) {
528 if (strcmp16(block.getElementName(&len), attr16.string()) == 0) {
529 break;
530 }
531 if ((attr.type&ResTable_map::TYPE_ENUM) != 0) {
532 if (strcmp16(block.getElementName(&len), enum16.string()) != 0) {
533 SourcePos(in->getPrintableSource(), block.getLineNumber())
534 .error("Found tag </%s> where </enum> is expected\n",
535 String8(block.getElementName(&len)).string());
536 return UNKNOWN_ERROR;
537 }
538 } else {
539 if (strcmp16(block.getElementName(&len), flag16.string()) != 0) {
540 SourcePos(in->getPrintableSource(), block.getLineNumber())
541 .error("Found tag </%s> where </flag> is expected\n",
542 String8(block.getElementName(&len)).string());
543 return UNKNOWN_ERROR;
544 }
545 }
546 }
547 }
548
549 if (!attr.hasErrors && attr.added) {
550 appendTypeInfo(outTable, myPackage, attr16, attr.ident, attr.type, gFormatFlags);
551 }
552
553 if (!attr.hasErrors && enumOrFlagsComment.size() > 0) {
554 enumOrFlagsComment.append(String16("\n</table>"));
555 outTable->appendTypeComment(myPackage, attr16, attr.ident, enumOrFlagsComment);
556 }
557
558
559 return NO_ERROR;
560 }
561
562 bool localeIsDefined(const ResTable_config& config)
563 {
564 return config.locale == 0;
565 }
566
567 status_t parseAndAddBag(Bundle* bundle,
568 const sp<AaptFile>& in,
569 ResXMLTree* block,
570 const ResTable_config& config,
571 const String16& myPackage,
572 const String16& curType,
573 const String16& ident,
574 const String16& parentIdent,
575 const String16& itemIdent,
576 int32_t curFormat,
577 bool pseudolocalize,
578 const bool overwrite,
579 ResourceTable* outTable)
580 {
581 status_t err;
582 const String16 item16("item");
583
584 String16 str;
585 Vector<StringPool::entry_style_span> spans;
586 err = parseStyledString(bundle, in->getPrintableSource().string(),
587 block, item16, &str, &spans,
588 pseudolocalize);
589 if (err != NO_ERROR) {
590 return err;
591 }
592
593 NOISY(printf("Adding resource bag entry l=%c%c c=%c%c orien=%d d=%d "
594 " pid=%s, bag=%s, id=%s: %s\n",
595 config.language[0], config.language[1],
596 config.country[0], config.country[1],
597 config.orientation, config.density,
598 String8(parentIdent).string(),
599 String8(ident).string(),
600 String8(itemIdent).string(),
601 String8(str).string()));
602
603 err = outTable->addBag(SourcePos(in->getPrintableSource(), block->getLineNumber()),
604 myPackage, curType, ident, parentIdent, itemIdent, str,
605 &spans, &config, overwrite, false, curFormat);
606 return err;
607 }
608
609
610 status_t parseAndAddEntry(Bundle* bundle,
611 const sp<AaptFile>& in,
612 ResXMLTree* block,
613 const ResTable_config& config,
614 const String16& myPackage,
615 const String16& curType,
616 const String16& ident,
617 const String16& curTag,
618 bool curIsStyled,
619 int32_t curFormat,
620 bool pseudolocalize,
621 const bool overwrite,
622 ResourceTable* outTable)
623 {
624 status_t err;
625
626 String16 str;
627 Vector<StringPool::entry_style_span> spans;
628 err = parseStyledString(bundle, in->getPrintableSource().string(), block,
629 curTag, &str, curIsStyled ? &spans : NULL,
630 pseudolocalize);
631
632 if (err < NO_ERROR) {
633 return err;
634 }
635
636 NOISY(printf("Adding resource entry l=%c%c c=%c%c orien=%d d=%d id=%s: %s\n",
637 config.language[0], config.language[1],
638 config.country[0], config.country[1],
639 config.orientation, config.density,
640 String8(ident).string(), String8(str).string()));
641
642 err = outTable->addEntry(SourcePos(in->getPrintableSource(), block->getLineNumber()),
643 myPackage, curType, ident, str, &spans, &config,
644 false, curFormat, overwrite);
645
646 return err;
647 }
648
649 status_t compileResourceFile(Bundle* bundle,
650 const sp<AaptAssets>& assets,
651 const sp<AaptFile>& in,
652 const ResTable_config& defParams,
653 const bool overwrite,
654 ResourceTable* outTable)
655 {
656 ResXMLTree block;
657 status_t err = parseXMLResource(in, &block, false, true);
658 if (err != NO_ERROR) {
659 return err;
660 }
661
662 // Top-level tag.
663 const String16 resources16("resources");
664
665 // Identifier declaration tags.
666 const String16 declare_styleable16("declare-styleable");
667 const String16 attr16("attr");
668
669 // Data creation organizational tags.
670 const String16 string16("string");
671 const String16 drawable16("drawable");
672 const String16 color16("color");
673 const String16 bool16("bool");
674 const String16 integer16("integer");
675 const String16 dimen16("dimen");
676 const String16 fraction16("fraction");
677 const String16 style16("style");
678 const String16 plurals16("plurals");
679 const String16 array16("array");
680 const String16 string_array16("string-array");
681 const String16 integer_array16("integer-array");
682 const String16 public16("public");
683 const String16 public_padding16("public-padding");
684 const String16 private_symbols16("private-symbols");
685 const String16 add_resource16("add-resource");
686 const String16 skip16("skip");
687 const String16 eat_comment16("eat-comment");
688
689 // Data creation tags.
690 const String16 bag16("bag");
691 const String16 item16("item");
692
693 // Attribute type constants.
694 const String16 enum16("enum");
695
696 // plural values
697 const String16 other16("other");
698 const String16 quantityOther16("^other");
699 const String16 zero16("zero");
700 const String16 quantityZero16("^zero");
701 const String16 one16("one");
702 const String16 quantityOne16("^one");
703 const String16 two16("two");
704 const String16 quantityTwo16("^two");
705 const String16 few16("few");
706 const String16 quantityFew16("^few");
707 const String16 many16("many");
708 const String16 quantityMany16("^many");
709
710 // useful attribute names and special values
711 const String16 name16("name");
712 const String16 translatable16("translatable");
713 const String16 false16("false");
714
715 const String16 myPackage(assets->getPackage());
716
717 bool hasErrors = false;
718
719 DefaultKeyedVector<String16, uint32_t> nextPublicId(0);
720
721 ResXMLTree::event_code_t code;
722 do {
723 code = block.next();
724 } while (code == ResXMLTree::START_NAMESPACE);
725
726 size_t len;
727 if (code != ResXMLTree::START_TAG) {
728 SourcePos(in->getPrintableSource(), block.getLineNumber()).error(
729 "No start tag found\n");
730 return UNKNOWN_ERROR;
731 }
732 if (strcmp16(block.getElementName(&len), resources16.string()) != 0) {
733 SourcePos(in->getPrintableSource(), block.getLineNumber()).error(
734 "Invalid start tag %s\n", String8(block.getElementName(&len)).string());
735 return UNKNOWN_ERROR;
736 }
737
738 ResTable_config curParams(defParams);
739
740 ResTable_config pseudoParams(curParams);
741 pseudoParams.language[0] = 'z';
742 pseudoParams.language[1] = 'z';
743 pseudoParams.country[0] = 'Z';
744 pseudoParams.country[1] = 'Z';
745
746 while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) {
747 if (code == ResXMLTree::START_TAG) {
748 const String16* curTag = NULL;
749 String16 curType;
750 int32_t curFormat = ResTable_map::TYPE_ANY;
751 bool curIsBag = false;
752 bool curIsBagReplaceOnOverwrite = false;
753 bool curIsStyled = false;
754 bool curIsPseudolocalizable = false;
755 bool localHasErrors = false;
756
757 if (strcmp16(block.getElementName(&len), skip16.string()) == 0) {
758 while ((code=block.next()) != ResXMLTree::END_DOCUMENT
759 && code != ResXMLTree::BAD_DOCUMENT) {
760 if (code == ResXMLTree::END_TAG) {
761 if (strcmp16(block.getElementName(&len), skip16.string()) == 0) {
762 break;
763 }
764 }
765 }
766 continue;
767
768 } else if (strcmp16(block.getElementName(&len), eat_comment16.string()) == 0) {
769 while ((code=block.next()) != ResXMLTree::END_DOCUMENT
770 && code != ResXMLTree::BAD_DOCUMENT) {
771 if (code == ResXMLTree::END_TAG) {
772 if (strcmp16(block.getElementName(&len), eat_comment16.string()) == 0) {
773 break;
774 }
775 }
776 }
777 continue;
778
779 } else if (strcmp16(block.getElementName(&len), public16.string()) == 0) {
780 SourcePos srcPos(in->getPrintableSource(), block.getLineNumber());
781
782 String16 type;
783 ssize_t typeIdx = block.indexOfAttribute(NULL, "type");
784 if (typeIdx < 0) {
785 srcPos.error("A 'type' attribute is required for <public>\n");
786 hasErrors = localHasErrors = true;
787 }
788 type = String16(block.getAttributeStringValue(typeIdx, &len));
789
790 String16 name;
791 ssize_t nameIdx = block.indexOfAttribute(NULL, "name");
792 if (nameIdx < 0) {
793 srcPos.error("A 'name' attribute is required for <public>\n");
794 hasErrors = localHasErrors = true;
795 }
796 name = String16(block.getAttributeStringValue(nameIdx, &len));
797
798 uint32_t ident = 0;
799 ssize_t identIdx = block.indexOfAttribute(NULL, "id");
800 if (identIdx >= 0) {
801 const char16_t* identStr = block.getAttributeStringValue(identIdx, &len);
802 Res_value identValue;
803 if (!ResTable::stringToInt(identStr, len, &identValue)) {
804 srcPos.error("Given 'id' attribute is not an integer: %s\n",
805 String8(block.getAttributeStringValue(identIdx, &len)).string());
806 hasErrors = localHasErrors = true;
807 } else {
808 ident = identValue.data;
809 nextPublicId.replaceValueFor(type, ident+1);
810 }
811 } else if (nextPublicId.indexOfKey(type) < 0) {
812 srcPos.error("No 'id' attribute supplied <public>,"
813 " and no previous id defined in this file.\n");
814 hasErrors = localHasErrors = true;
815 } else if (!localHasErrors) {
816 ident = nextPublicId.valueFor(type);
817 nextPublicId.replaceValueFor(type, ident+1);
818 }
819
820 if (!localHasErrors) {
821 err = outTable->addPublic(srcPos, myPackage, type, name, ident);
822 if (err < NO_ERROR) {
823 hasErrors = localHasErrors = true;
824 }
825 }
826 if (!localHasErrors) {
827 sp<AaptSymbols> symbols = assets->getSymbolsFor(String8("R"));
828 if (symbols != NULL) {
829 symbols = symbols->addNestedSymbol(String8(type), srcPos);
830 }
831 if (symbols != NULL) {
832 symbols->makeSymbolPublic(String8(name), srcPos);
833 String16 comment(
834 block.getComment(&len) ? block.getComment(&len) : nulStr);
835 symbols->appendComment(String8(name), comment, srcPos);
836 } else {
837 srcPos.error("Unable to create symbols!\n");
838 hasErrors = localHasErrors = true;
839 }
840 }
841
842 while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) {
843 if (code == ResXMLTree::END_TAG) {
844 if (strcmp16(block.getElementName(&len), public16.string()) == 0) {
845 break;
846 }
847 }
848 }
849 continue;
850
851 } else if (strcmp16(block.getElementName(&len), public_padding16.string()) == 0) {
852 SourcePos srcPos(in->getPrintableSource(), block.getLineNumber());
853
854 String16 type;
855 ssize_t typeIdx = block.indexOfAttribute(NULL, "type");
856 if (typeIdx < 0) {
857 srcPos.error("A 'type' attribute is required for <public-padding>\n");
858 hasErrors = localHasErrors = true;
859 }
860 type = String16(block.getAttributeStringValue(typeIdx, &len));
861
862 String16 name;
863 ssize_t nameIdx = block.indexOfAttribute(NULL, "name");
864 if (nameIdx < 0) {
865 srcPos.error("A 'name' attribute is required for <public-padding>\n");
866 hasErrors = localHasErrors = true;
867 }
868 name = String16(block.getAttributeStringValue(nameIdx, &len));
869
870 uint32_t start = 0;
871 ssize_t startIdx = block.indexOfAttribute(NULL, "start");
872 if (startIdx >= 0) {
873 const char16_t* startStr = block.getAttributeStringValue(startIdx, &len);
874 Res_value startValue;
875 if (!ResTable::stringToInt(startStr, len, &startValue)) {
876 srcPos.error("Given 'start' attribute is not an integer: %s\n",
877 String8(block.getAttributeStringValue(startIdx, &len)).string());
878 hasErrors = localHasErrors = true;
879 } else {
880 start = startValue.data;
881 }
882 } else if (nextPublicId.indexOfKey(type) < 0) {
883 srcPos.error("No 'start' attribute supplied <public-padding>,"
884 " and no previous id defined in this file.\n");
885 hasErrors = localHasErrors = true;
886 } else if (!localHasErrors) {
887 start = nextPublicId.valueFor(type);
888 }
889
890 uint32_t end = 0;
891 ssize_t endIdx = block.indexOfAttribute(NULL, "end");
892 if (endIdx >= 0) {
893 const char16_t* endStr = block.getAttributeStringValue(endIdx, &len);
894 Res_value endValue;
895 if (!ResTable::stringToInt(endStr, len, &endValue)) {
896 srcPos.error("Given 'end' attribute is not an integer: %s\n",
897 String8(block.getAttributeStringValue(endIdx, &len)).string());
898 hasErrors = localHasErrors = true;
899 } else {
900 end = endValue.data;
901 }
902 } else {
903 srcPos.error("No 'end' attribute supplied <public-padding>\n");
904 hasErrors = localHasErrors = true;
905 }
906
907 if (end >= start) {
908 nextPublicId.replaceValueFor(type, end+1);
909 } else {
910 srcPos.error("Padding start '%ul' is after end '%ul'\n",
911 start, end);
912 hasErrors = localHasErrors = true;
913 }
914
915 String16 comment(
916 block.getComment(&len) ? block.getComment(&len) : nulStr);
917 for (uint32_t curIdent=start; curIdent<=end; curIdent++) {
918 if (localHasErrors) {
919 break;
920 }
921 String16 curName(name);
922 char buf[64];
923 sprintf(buf, "%d", (int)(end-curIdent+1));
924 curName.append(String16(buf));
925
926 err = outTable->addEntry(srcPos, myPackage, type, curName,
927 String16("padding"), NULL, &curParams, false,
928 ResTable_map::TYPE_STRING, overwrite);
929 if (err < NO_ERROR) {
930 hasErrors = localHasErrors = true;
931 break;
932 }
933 err = outTable->addPublic(srcPos, myPackage, type,
934 curName, curIdent);
935 if (err < NO_ERROR) {
936 hasErrors = localHasErrors = true;
937 break;
938 }
939 sp<AaptSymbols> symbols = assets->getSymbolsFor(String8("R"));
940 if (symbols != NULL) {
941 symbols = symbols->addNestedSymbol(String8(type), srcPos);
942 }
943 if (symbols != NULL) {
944 symbols->makeSymbolPublic(String8(curName), srcPos);
945 symbols->appendComment(String8(curName), comment, srcPos);
946 } else {
947 srcPos.error("Unable to create symbols!\n");
948 hasErrors = localHasErrors = true;
949 }
950 }
951
952 while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) {
953 if (code == ResXMLTree::END_TAG) {
954 if (strcmp16(block.getElementName(&len), public_padding16.string()) == 0) {
955 break;
956 }
957 }
958 }
959 continue;
960
961 } else if (strcmp16(block.getElementName(&len), private_symbols16.string()) == 0) {
962 String16 pkg;
963 ssize_t pkgIdx = block.indexOfAttribute(NULL, "package");
964 if (pkgIdx < 0) {
965 SourcePos(in->getPrintableSource(), block.getLineNumber()).error(
966 "A 'package' attribute is required for <private-symbols>\n");
967 hasErrors = localHasErrors = true;
968 }
969 pkg = String16(block.getAttributeStringValue(pkgIdx, &len));
970 if (!localHasErrors) {
971 assets->setSymbolsPrivatePackage(String8(pkg));
972 }
973
974 while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) {
975 if (code == ResXMLTree::END_TAG) {
976 if (strcmp16(block.getElementName(&len), private_symbols16.string()) == 0) {
977 break;
978 }
979 }
980 }
981 continue;
982
983 } else if (strcmp16(block.getElementName(&len), add_resource16.string()) == 0) {
984 SourcePos srcPos(in->getPrintableSource(), block.getLineNumber());
985
986 String16 typeName;
987 ssize_t typeIdx = block.indexOfAttribute(NULL, "type");
988 if (typeIdx < 0) {
989 srcPos.error("A 'type' attribute is required for <add-resource>\n");
990 hasErrors = localHasErrors = true;
991 }
992 typeName = String16(block.getAttributeStringValue(typeIdx, &len));
993
994 String16 name;
995 ssize_t nameIdx = block.indexOfAttribute(NULL, "name");
996 if (nameIdx < 0) {
997 srcPos.error("A 'name' attribute is required for <add-resource>\n");
998 hasErrors = localHasErrors = true;
999 }
1000 name = String16(block.getAttributeStringValue(nameIdx, &len));
1001
1002 outTable->canAddEntry(srcPos, myPackage, typeName, name);
1003
1004 while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) {
1005 if (code == ResXMLTree::END_TAG) {
1006 if (strcmp16(block.getElementName(&len), add_resource16.string()) == 0) {
1007 break;
1008 }
1009 }
1010 }
1011 continue;
1012
1013 } else if (strcmp16(block.getElementName(&len), declare_styleable16.string()) == 0) {
1014 SourcePos srcPos(in->getPrintableSource(), block.getLineNumber());
1015
1016 String16 ident;
1017 ssize_t identIdx = block.indexOfAttribute(NULL, "name");
1018 if (identIdx < 0) {
1019 srcPos.error("A 'name' attribute is required for <declare-styleable>\n");
1020 hasErrors = localHasErrors = true;
1021 }
1022 ident = String16(block.getAttributeStringValue(identIdx, &len));
1023
1024 sp<AaptSymbols> symbols = assets->getSymbolsFor(String8("R"));
1025 if (!localHasErrors) {
1026 if (symbols != NULL) {
1027 symbols = symbols->addNestedSymbol(String8("styleable"), srcPos);
1028 }
1029 sp<AaptSymbols> styleSymbols = symbols;
1030 if (symbols != NULL) {
1031 symbols = symbols->addNestedSymbol(String8(ident), srcPos);
1032 }
1033 if (symbols == NULL) {
1034 srcPos.error("Unable to create symbols!\n");
1035 return UNKNOWN_ERROR;
1036 }
1037
1038 String16 comment(
1039 block.getComment(&len) ? block.getComment(&len) : nulStr);
1040 styleSymbols->appendComment(String8(ident), comment, srcPos);
1041 } else {
1042 symbols = NULL;
1043 }
1044
1045 while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) {
1046 if (code == ResXMLTree::START_TAG) {
1047 if (strcmp16(block.getElementName(&len), skip16.string()) == 0) {
1048 while ((code=block.next()) != ResXMLTree::END_DOCUMENT
1049 && code != ResXMLTree::BAD_DOCUMENT) {
1050 if (code == ResXMLTree::END_TAG) {
1051 if (strcmp16(block.getElementName(&len), skip16.string()) == 0) {
1052 break;
1053 }
1054 }
1055 }
1056 continue;
1057 } else if (strcmp16(block.getElementName(&len), eat_comment16.string()) == 0) {
1058 while ((code=block.next()) != ResXMLTree::END_DOCUMENT
1059 && code != ResXMLTree::BAD_DOCUMENT) {
1060 if (code == ResXMLTree::END_TAG) {
1061 if (strcmp16(block.getElementName(&len), eat_comment16.string()) == 0) {
1062 break;
1063 }
1064 }
1065 }
1066 continue;
1067 } else if (strcmp16(block.getElementName(&len), attr16.string()) != 0) {
1068 SourcePos(in->getPrintableSource(), block.getLineNumber()).error(
1069 "Tag <%s> can not appear inside <declare-styleable>, only <attr>\n",
1070 String8(block.getElementName(&len)).string());
1071 return UNKNOWN_ERROR;
1072 }
1073
1074 String16 comment(
1075 block.getComment(&len) ? block.getComment(&len) : nulStr);
1076 String16 itemIdent;
1077 err = compileAttribute(in, block, myPackage, outTable, &itemIdent, true);
1078 if (err != NO_ERROR) {
1079 hasErrors = localHasErrors = true;
1080 }
1081
1082 if (symbols != NULL) {
1083 SourcePos srcPos(String8(in->getPrintableSource()), block.getLineNumber());
1084 symbols->addSymbol(String8(itemIdent), 0, srcPos);
1085 symbols->appendComment(String8(itemIdent), comment, srcPos);
1086 //printf("Attribute %s comment: %s\n", String8(itemIdent).string(),
1087 // String8(comment).string());
1088 }
1089 } else if (code == ResXMLTree::END_TAG) {
1090 if (strcmp16(block.getElementName(&len), declare_styleable16.string()) == 0) {
1091 break;
1092 }
1093
1094 SourcePos(in->getPrintableSource(), block.getLineNumber()).error(
1095 "Found tag </%s> where </attr> is expected\n",
1096 String8(block.getElementName(&len)).string());
1097 return UNKNOWN_ERROR;
1098 }
1099 }
1100 continue;
1101
1102 } else if (strcmp16(block.getElementName(&len), attr16.string()) == 0) {
1103 err = compileAttribute(in, block, myPackage, outTable, NULL);
1104 if (err != NO_ERROR) {
1105 hasErrors = true;
1106 }
1107 continue;
1108
1109 } else if (strcmp16(block.getElementName(&len), item16.string()) == 0) {
1110 curTag = &item16;
1111 ssize_t attri = block.indexOfAttribute(NULL, "type");
1112 if (attri >= 0) {
1113 curType = String16(block.getAttributeStringValue(attri, &len));
1114 ssize_t formatIdx = block.indexOfAttribute(NULL, "format");
1115 if (formatIdx >= 0) {
1116 String16 formatStr = String16(block.getAttributeStringValue(
1117 formatIdx, &len));
1118 curFormat = parse_flags(formatStr.string(), formatStr.size(),
1119 gFormatFlags);
1120 if (curFormat == 0) {
1121 SourcePos(in->getPrintableSource(), block.getLineNumber()).error(
1122 "Tag <item> 'format' attribute value \"%s\" not valid\n",
1123 String8(formatStr).string());
1124 hasErrors = localHasErrors = true;
1125 }
1126 }
1127 } else {
1128 SourcePos(in->getPrintableSource(), block.getLineNumber()).error(
1129 "A 'type' attribute is required for <item>\n");
1130 hasErrors = localHasErrors = true;
1131 }
1132 curIsStyled = true;
1133 } else if (strcmp16(block.getElementName(&len), string16.string()) == 0) {
1134 // Note the existence and locale of every string we process
1135 char rawLocale[16];
1136 curParams.getLocale(rawLocale);
1137 String8 locale(rawLocale);
1138 String16 name;
1139 String16 translatable;
1140
1141 size_t n = block.getAttributeCount();
1142 for (size_t i = 0; i < n; i++) {
1143 size_t length;
1144 const uint16_t* attr = block.getAttributeName(i, &length);
1145 if (strcmp16(attr, name16.string()) == 0) {
1146 name.setTo(block.getAttributeStringValue(i, &length));
1147 } else if (strcmp16(attr, translatable16.string()) == 0) {
1148 translatable.setTo(block.getAttributeStringValue(i, &length));
1149 }
1150 }
1151
1152 if (name.size() > 0) {
1153 if (translatable == false16) {
1154 // Untranslatable strings must only exist in the default [empty] locale
1155 if (locale.size() > 0) {
1156 fprintf(stderr, "aapt: warning: string '%s' in %s marked untranslatable but exists"
1157 " in locale '%s'\n", String8(name).string(),
1158 bundle->getResourceSourceDirs()[0],
1159 locale.string());
1160 // hasErrors = localHasErrors = true;
1161 } else {
1162 // Intentionally empty block:
1163 //
1164 // Don't add untranslatable strings to the localization table; that
1165 // way if we later see localizations of them, they'll be flagged as
1166 // having no default translation.
1167 }
1168 } else {
1169 outTable->addLocalization(name, locale);
1170 }
1171 }
1172
1173 curTag = &string16;
1174 curType = string16;
1175 curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_STRING;
1176 curIsStyled = true;
1177 curIsPseudolocalizable = true;
1178 } else if (strcmp16(block.getElementName(&len), drawable16.string()) == 0) {
1179 curTag = &drawable16;
1180 curType = drawable16;
1181 curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_COLOR;
1182 } else if (strcmp16(block.getElementName(&len), color16.string()) == 0) {
1183 curTag = &color16;
1184 curType = color16;
1185 curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_COLOR;
1186 } else if (strcmp16(block.getElementName(&len), bool16.string()) == 0) {
1187 curTag = &bool16;
1188 curType = bool16;
1189 curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_BOOLEAN;
1190 } else if (strcmp16(block.getElementName(&len), integer16.string()) == 0) {
1191 curTag = &integer16;
1192 curType = integer16;
1193 curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_INTEGER;
1194 } else if (strcmp16(block.getElementName(&len), dimen16.string()) == 0) {
1195 curTag = &dimen16;
1196 curType = dimen16;
1197 curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_DIMENSION;
1198 } else if (strcmp16(block.getElementName(&len), fraction16.string()) == 0) {
1199 curTag = &fraction16;
1200 curType = fraction16;
1201 curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_FRACTION;
1202 } else if (strcmp16(block.getElementName(&len), bag16.string()) == 0) {
1203 curTag = &bag16;
1204 curIsBag = true;
1205 ssize_t attri = block.indexOfAttribute(NULL, "type");
1206 if (attri >= 0) {
1207 curType = String16(block.getAttributeStringValue(attri, &len));
1208 } else {
1209 SourcePos(in->getPrintableSource(), block.getLineNumber()).error(
1210 "A 'type' attribute is required for <bag>\n");
1211 hasErrors = localHasErrors = true;
1212 }
1213 } else if (strcmp16(block.getElementName(&len), style16.string()) == 0) {
1214 curTag = &style16;
1215 curType = style16;
1216 curIsBag = true;
1217 } else if (strcmp16(block.getElementName(&len), plurals16.string()) == 0) {
1218 curTag = &plurals16;
1219 curType = plurals16;
1220 curIsBag = true;
1221 } else if (strcmp16(block.getElementName(&len), array16.string()) == 0) {
1222 curTag = &array16;
1223 curType = array16;
1224 curIsBag = true;
1225 curIsBagReplaceOnOverwrite = true;
1226 ssize_t formatIdx = block.indexOfAttribute(NULL, "format");
1227 if (formatIdx >= 0) {
1228 String16 formatStr = String16(block.getAttributeStringValue(
1229 formatIdx, &len));
1230 curFormat = parse_flags(formatStr.string(), formatStr.size(),
1231 gFormatFlags);
1232 if (curFormat == 0) {
1233 SourcePos(in->getPrintableSource(), block.getLineNumber()).error(
1234 "Tag <array> 'format' attribute value \"%s\" not valid\n",
1235 String8(formatStr).string());
1236 hasErrors = localHasErrors = true;
1237 }
1238 }
1239 } else if (strcmp16(block.getElementName(&len), string_array16.string()) == 0) {
1240 curTag = &string_array16;
1241 curType = array16;
1242 curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_STRING;
1243 curIsBag = true;
1244 curIsBagReplaceOnOverwrite = true;
1245 curIsPseudolocalizable = true;
1246 } else if (strcmp16(block.getElementName(&len), integer_array16.string()) == 0) {
1247 curTag = &integer_array16;
1248 curType = array16;
1249 curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_INTEGER;
1250 curIsBag = true;
1251 curIsBagReplaceOnOverwrite = true;
1252 } else {
1253 SourcePos(in->getPrintableSource(), block.getLineNumber()).error(
1254 "Found tag %s where item is expected\n",
1255 String8(block.getElementName(&len)).string());
1256 return UNKNOWN_ERROR;
1257 }
1258
1259 String16 ident;
1260 ssize_t identIdx = block.indexOfAttribute(NULL, "name");
1261 if (identIdx >= 0) {
1262 ident = String16(block.getAttributeStringValue(identIdx, &len));
1263 } else {
1264 SourcePos(in->getPrintableSource(), block.getLineNumber()).error(
1265 "A 'name' attribute is required for <%s>\n",
1266 String8(*curTag).string());
1267 hasErrors = localHasErrors = true;
1268 }
1269
1270 String16 comment(block.getComment(&len) ? block.getComment(&len) : nulStr);
1271
1272 if (curIsBag) {
1273 // Figure out the parent of this bag...
1274 String16 parentIdent;
1275 ssize_t parentIdentIdx = block.indexOfAttribute(NULL, "parent");
1276 if (parentIdentIdx >= 0) {
1277 parentIdent = String16(block.getAttributeStringValue(parentIdentIdx, &len));
1278 } else {
1279 ssize_t sep = ident.findLast('.');
1280 if (sep >= 0) {
1281 parentIdent.setTo(ident, sep);
1282 }
1283 }
1284
1285 if (!localHasErrors) {
1286 err = outTable->startBag(SourcePos(in->getPrintableSource(),
1287 block.getLineNumber()), myPackage, curType, ident,
1288 parentIdent, &curParams,
1289 overwrite, curIsBagReplaceOnOverwrite);
1290 if (err != NO_ERROR) {
1291 hasErrors = localHasErrors = true;
1292 }
1293 }
1294
1295 ssize_t elmIndex = 0;
1296 char elmIndexStr[14];
1297 while ((code=block.next()) != ResXMLTree::END_DOCUMENT
1298 && code != ResXMLTree::BAD_DOCUMENT) {
1299
1300 if (code == ResXMLTree::START_TAG) {
1301 if (strcmp16(block.getElementName(&len), item16.string()) != 0) {
1302 SourcePos(in->getPrintableSource(), block.getLineNumber()).error(
1303 "Tag <%s> can not appear inside <%s>, only <item>\n",
1304 String8(block.getElementName(&len)).string(),
1305 String8(*curTag).string());
1306 return UNKNOWN_ERROR;
1307 }
1308
1309 String16 itemIdent;
1310 if (curType == array16) {
1311 sprintf(elmIndexStr, "^index_%d", (int)elmIndex++);
1312 itemIdent = String16(elmIndexStr);
1313 } else if (curType == plurals16) {
1314 ssize_t itemIdentIdx = block.indexOfAttribute(NULL, "quantity");
1315 if (itemIdentIdx >= 0) {
1316 String16 quantity16(block.getAttributeStringValue(itemIdentIdx, &len));
1317 if (quantity16 == other16) {
1318 itemIdent = quantityOther16;
1319 }
1320 else if (quantity16 == zero16) {
1321 itemIdent = quantityZero16;
1322 }
1323 else if (quantity16 == one16) {
1324 itemIdent = quantityOne16;
1325 }
1326 else if (quantity16 == two16) {
1327 itemIdent = quantityTwo16;
1328 }
1329 else if (quantity16 == few16) {
1330 itemIdent = quantityFew16;
1331 }
1332 else if (quantity16 == many16) {
1333 itemIdent = quantityMany16;
1334 }
1335 else {
1336 SourcePos(in->getPrintableSource(), block.getLineNumber()).error(
1337 "Illegal 'quantity' attribute is <item> inside <plurals>\n");
1338 hasErrors = localHasErrors = true;
1339 }
1340 } else {
1341 SourcePos(in->getPrintableSource(), block.getLineNumber()).error(
1342 "A 'quantity' attribute is required for <item> inside <plurals>\n");
1343 hasErrors = localHasErrors = true;
1344 }
1345 } else {
1346 ssize_t itemIdentIdx = block.indexOfAttribute(NULL, "name");
1347 if (itemIdentIdx >= 0) {
1348 itemIdent = String16(block.getAttributeStringValue(itemIdentIdx, &len));
1349 } else {
1350 SourcePos(in->getPrintableSource(), block.getLineNumber()).error(
1351 "A 'name' attribute is required for <item>\n");
1352 hasErrors = localHasErrors = true;
1353 }
1354 }
1355
1356 ResXMLParser::ResXMLPosition parserPosition;
1357 block.getPosition(&parserPosition);
1358
1359 err = parseAndAddBag(bundle, in, &block, curParams, myPackage, curType,
1360 ident, parentIdent, itemIdent, curFormat,
1361 false, overwrite, outTable);
1362 if (err == NO_ERROR) {
1363 if (curIsPseudolocalizable && localeIsDefined(curParams)
1364 && bundle->getPseudolocalize()) {
1365 // pseudolocalize here
1366 #if 1
1367 block.setPosition(parserPosition);
1368 err = parseAndAddBag(bundle, in, &block, pseudoParams, myPackage,
1369 curType, ident, parentIdent, itemIdent, curFormat, true,
1370 overwrite, outTable);
1371 #endif
1372 }
1373 }
1374 if (err != NO_ERROR) {
1375 hasErrors = localHasErrors = true;
1376 }
1377 } else if (code == ResXMLTree::END_TAG) {
1378 if (strcmp16(block.getElementName(&len), curTag->string()) != 0) {
1379 SourcePos(in->getPrintableSource(), block.getLineNumber()).error(
1380 "Found tag </%s> where </%s> is expected\n",
1381 String8(block.getElementName(&len)).string(),
1382 String8(*curTag).string());
1383 return UNKNOWN_ERROR;
1384 }
1385 break;
1386 }
1387 }
1388 } else {
1389 ResXMLParser::ResXMLPosition parserPosition;
1390 block.getPosition(&parserPosition);
1391
1392 err = parseAndAddEntry(bundle, in, &block, curParams, myPackage, curType, ident,
1393 *curTag, curIsStyled, curFormat, false, overwrite, outTable);
1394
1395 if (err < NO_ERROR) { // Why err < NO_ERROR instead of err != NO_ERROR?
1396 hasErrors = localHasErrors = true;
1397 }
1398 else if (err == NO_ERROR) {
1399 if (curIsPseudolocalizable && localeIsDefined(curParams)
1400 && bundle->getPseudolocalize()) {
1401 // pseudolocalize here
1402 block.setPosition(parserPosition);
1403 err = parseAndAddEntry(bundle, in, &block, pseudoParams, myPackage, curType,
1404 ident, *curTag, curIsStyled, curFormat, true, overwrite, outTable);
1405 if (err != NO_ERROR) {
1406 hasErrors = localHasErrors = true;
1407 }
1408 }
1409 }
1410 }
1411
1412 #if 0
1413 if (comment.size() > 0) {
1414 printf("Comment for @%s:%s/%s: %s\n", String8(myPackage).string(),
1415 String8(curType).string(), String8(ident).string(),
1416 String8(comment).string());
1417 }
1418 #endif
1419 if (!localHasErrors) {
1420 outTable->appendComment(myPackage, curType, ident, comment, false);
1421 }
1422 }
1423 else if (code == ResXMLTree::END_TAG) {
1424 if (strcmp16(block.getElementName(&len), resources16.string()) != 0) {
1425 SourcePos(in->getPrintableSource(), block.getLineNumber()).error(
1426 "Unexpected end tag %s\n", String8(block.getElementName(&len)).string());
1427 return UNKNOWN_ERROR;
1428 }
1429 }
1430 else if (code == ResXMLTree::START_NAMESPACE || code == ResXMLTree::END_NAMESPACE) {
1431 }
1432 else if (code == ResXMLTree::TEXT) {
1433 if (isWhitespace(block.getText(&len))) {
1434 continue;
1435 }
1436 SourcePos(in->getPrintableSource(), block.getLineNumber()).error(
1437 "Found text \"%s\" where item tag is expected\n",
1438 String8(block.getText(&len)).string());
1439 return UNKNOWN_ERROR;
1440 }
1441 }
1442
1443 return hasErrors ? UNKNOWN_ERROR : NO_ERROR;
1444 }
1445
1446 ResourceTable::ResourceTable(Bundle* bundle, const String16& assetsPackage)
1447 : mAssetsPackage(assetsPackage), mNextPackageId(1), mHaveAppPackage(false),
1448 mIsAppPackage(!bundle->getExtending()),
1449 mNumLocal(0),
1450 mBundle(bundle)
1451 {
1452 }
1453
1454 status_t ResourceTable::addIncludedResources(Bundle* bundle, const sp<AaptAssets>& assets)
1455 {
1456 status_t err = assets->buildIncludedResources(bundle);
1457 if (err != NO_ERROR) {
1458 return err;
1459 }
1460
1461 // For future reference to included resources.
1462 mAssets = assets;
1463
1464 const ResTable& incl = assets->getIncludedResources();
1465
1466 // Retrieve all the packages.
1467 const size_t N = incl.getBasePackageCount();
1468 for (size_t phase=0; phase<2; phase++) {
1469 for (size_t i=0; i<N; i++) {
1470 String16 name(incl.getBasePackageName(i));
1471 uint32_t id = incl.getBasePackageId(i);
1472 // First time through: only add base packages (id
1473 // is not 0); second time through add the other
1474 // packages.
1475 if (phase != 0) {
1476 if (id != 0) {
1477 // Skip base packages -- already one.
1478 id = 0;
1479 } else {
1480 // Assign a dynamic id.
1481 id = mNextPackageId;
1482 }
1483 } else if (id != 0) {
1484 if (id == 127) {
1485 if (mHaveAppPackage) {
1486 fprintf(stderr, "Included resources have two application packages!\n");
1487 return UNKNOWN_ERROR;
1488 }
1489 mHaveAppPackage = true;
1490 }
1491 if (mNextPackageId > id) {
1492 fprintf(stderr, "Included base package ID %d already in use!\n", id);
1493 return UNKNOWN_ERROR;
1494 }
1495 }
1496 if (id != 0) {
1497 NOISY(printf("Including package %s with ID=%d\n",
1498 String8(name).string(), id));
1499 sp<Package> p = new Package(name, id);
1500 mPackages.add(name, p);
1501 mOrderedPackages.add(p);
1502
1503 if (id >= mNextPackageId) {
1504 mNextPackageId = id+1;
1505 }
1506 }
1507 }
1508 }
1509
1510 // Every resource table always has one first entry, the bag attributes.
1511 const SourcePos unknown(String8("????"), 0);
1512 sp<Type> attr = getType(mAssetsPackage, String16("attr"), unknown);
1513
1514 return NO_ERROR;
1515 }
1516
1517 status_t ResourceTable::addPublic(const SourcePos& sourcePos,
1518 const String16& package,
1519 const String16& type,
1520 const String16& name,
1521 const uint32_t ident)
1522 {
1523 uint32_t rid = mAssets->getIncludedResources()
1524 .identifierForName(name.string(), name.size(),
1525 type.string(), type.size(),
1526 package.string(), package.size());
1527 if (rid != 0) {
1528 sourcePos.error("Error declaring public resource %s/%s for included package %s\n",
1529 String8(type).string(), String8(name).string(),
1530 String8(package).string());
1531 return UNKNOWN_ERROR;
1532 }
1533
1534 sp<Type> t = getType(package, type, sourcePos);
1535 if (t == NULL) {
1536 return UNKNOWN_ERROR;
1537 }
1538 return t->addPublic(sourcePos, name, ident);
1539 }
1540
1541 status_t ResourceTable::addEntry(const SourcePos& sourcePos,
1542 const String16& package,
1543 const String16& type,
1544 const String16& name,
1545 const String16& value,
1546 const Vector<StringPool::entry_style_span>* style,
1547 const ResTable_config* params,
1548 const bool doSetIndex,
1549 const int32_t format,
1550 const bool overwrite)
1551 {
1552 // Check for adding entries in other packages... for now we do
1553 // nothing. We need to do the right thing here to support skinning.
1554 uint32_t rid = mAssets->getIncludedResources()
1555 .identifierForName(name.string(), name.size(),
1556 type.string(), type.size(),
1557 package.string(), package.size());
1558 if (rid != 0) {
1559 return NO_ERROR;
1560 }
1561
1562 #if 0
1563 if (name == String16("left")) {
1564 printf("Adding entry left: file=%s, line=%d, type=%s, value=%s\n",
1565 sourcePos.file.string(), sourcePos.line, String8(type).string(),
1566 String8(value).string());
1567 }
1568 #endif
1569
1570 sp<Entry> e = getEntry(package, type, name, sourcePos, overwrite,
1571 params, doSetIndex);
1572 if (e == NULL) {
1573 return UNKNOWN_ERROR;
1574 }
1575 status_t err = e->setItem(sourcePos, value, style, format, overwrite);
1576 if (err == NO_ERROR) {
1577 mNumLocal++;
1578 }
1579 return err;
1580 }
1581
1582 status_t ResourceTable::startBag(const SourcePos& sourcePos,
1583 const String16& package,
1584 const String16& type,
1585 const String16& name,
1586 const String16& bagParent,
1587 const ResTable_config* params,
1588 bool overlay,
1589 bool replace, bool isId)
1590 {
1591 status_t result = NO_ERROR;
1592
1593 // Check for adding entries in other packages... for now we do
1594 // nothing. We need to do the right thing here to support skinning.
1595 uint32_t rid = mAssets->getIncludedResources()
1596 .identifierForName(name.string(), name.size(),
1597 type.string(), type.size(),
1598 package.string(), package.size());
1599 if (rid != 0) {
1600 return NO_ERROR;
1601 }
1602
1603 #if 0
1604 if (name == String16("left")) {
1605 printf("Adding bag left: file=%s, line=%d, type=%s\n",
1606 sourcePos.file.striing(), sourcePos.line, String8(type).string());
1607 }
1608 #endif
1609 if (overlay && !mBundle->getAutoAddOverlay() && !hasBagOrEntry(package, type, name)) {
1610 bool canAdd = false;
1611 sp<Package> p = mPackages.valueFor(package);
1612 if (p != NULL) {
1613 sp<Type> t = p->getTypes().valueFor(type);
1614 if (t != NULL) {
1615 if (t->getCanAddEntries().indexOf(name) >= 0) {
1616 canAdd = true;
1617 }
1618 }
1619 }
1620 if (!canAdd) {
1621 sourcePos.error("Resource does not already exist in overlay at '%s'; use <add-resource> to add.\n",
1622 String8(name).string());
1623 return UNKNOWN_ERROR;
1624 }
1625 }
1626 sp<Entry> e = getEntry(package, type, name, sourcePos, overlay, params);
1627 if (e == NULL) {
1628 return UNKNOWN_ERROR;
1629 }
1630
1631 // If a parent is explicitly specified, set it.
1632 if (bagParent.size() > 0) {
1633 String16 curPar = e->getParent();
1634 if (curPar.size() > 0 && curPar != bagParent) {
1635 sourcePos.error("Conflicting parents specified, was '%s', now '%s'\n",
1636 String8(e->getParent()).string(),
1637 String8(bagParent).string());
1638 return UNKNOWN_ERROR;
1639 }
1640 e->setParent(bagParent);
1641 }
1642
1643 if ((result = e->makeItABag(sourcePos)) != NO_ERROR) {
1644 return result;
1645 }
1646
1647 if (overlay && replace) {
1648 return e->emptyBag(sourcePos);
1649 }
1650 return result;
1651 }
1652
1653 status_t ResourceTable::addBag(const SourcePos& sourcePos,
1654 const String16& package,
1655 const String16& type,
1656 const String16& name,
1657 const String16& bagParent,
1658 const String16& bagKey,
1659 const String16& value,
1660 const Vector<StringPool::entry_style_span>* style,
1661 const ResTable_config* params,
1662 bool replace, bool isId, const int32_t format)
1663 {
1664 // Check for adding entries in other packages... for now we do
1665 // nothing. We need to do the right thing here to support skinning.
1666 uint32_t rid = mAssets->getIncludedResources()
1667 .identifierForName(name.string(), name.size(),
1668 type.string(), type.size(),
1669 package.string(), package.size());
1670 if (rid != 0) {
1671 return NO_ERROR;
1672 }
1673
1674 #if 0
1675 if (name == String16("left")) {
1676 printf("Adding bag left: file=%s, line=%d, type=%s\n",
1677 sourcePos.file.striing(), sourcePos.line, String8(type).string());
1678 }
1679 #endif
1680 sp<Entry> e = getEntry(package, type, name, sourcePos, replace, params);
1681 if (e == NULL) {
1682 return UNKNOWN_ERROR;
1683 }
1684
1685 // If a parent is explicitly specified, set it.
1686 if (bagParent.size() > 0) {
1687 String16 curPar = e->getParent();
1688 if (curPar.size() > 0 && curPar != bagParent) {
1689 sourcePos.error("Conflicting parents specified, was '%s', now '%s'\n",
1690 String8(e->getParent()).string(),
1691 String8(bagParent).string());
1692 return UNKNOWN_ERROR;
1693 }
1694 e->setParent(bagParent);
1695 }
1696
1697 const bool first = e->getBag().indexOfKey(bagKey) < 0;
1698 status_t err = e->addToBag(sourcePos, bagKey, value, style, replace, isId, format);
1699 if (err == NO_ERROR && first) {
1700 mNumLocal++;
1701 }
1702 return err;
1703 }
1704
1705 bool ResourceTable::hasBagOrEntry(const String16& package,
1706 const String16& type,
1707 const String16& name) const
1708 {
1709 // First look for this in the included resources...
1710 uint32_t rid = mAssets->getIncludedResources()
1711 .identifierForName(name.string(), name.size(),
1712 type.string(), type.size(),
1713 package.string(), package.size());
1714 if (rid != 0) {
1715 return true;
1716 }
1717
1718 sp<Package> p = mPackages.valueFor(package);
1719 if (p != NULL) {
1720 sp<Type> t = p->getTypes().valueFor(type);
1721 if (t != NULL) {
1722 sp<ConfigList> c = t->getConfigs().valueFor(name);
1723 if (c != NULL) return true;
1724 }
1725 }
1726
1727 return false;
1728 }
1729
1730 bool ResourceTable::hasBagOrEntry(const String16& ref,
1731 const String16* defType,
1732 const String16* defPackage)
1733 {
1734 String16 package, type, name;
1735 if (!ResTable::expandResourceRef(ref.string(), ref.size(), &package, &type, &name,
1736 defType, defPackage ? defPackage:&mAssetsPackage, NULL)) {
1737 return false;
1738 }
1739 return hasBagOrEntry(package, type, name);
1740 }
1741
1742 bool ResourceTable::appendComment(const String16& package,
1743 const String16& type,
1744 const String16& name,
1745 const String16& comment,
1746 bool onlyIfEmpty)
1747 {
1748 if (comment.size() <= 0) {
1749 return true;
1750 }
1751
1752 sp<Package> p = mPackages.valueFor(package);
1753 if (p != NULL) {
1754 sp<Type> t = p->getTypes().valueFor(type);
1755 if (t != NULL) {
1756 sp<ConfigList> c = t->getConfigs().valueFor(name);
1757 if (c != NULL) {
1758 c->appendComment(comment, onlyIfEmpty);
1759 return true;
1760 }
1761 }
1762 }
1763 return false;
1764 }
1765
1766 bool ResourceTable::appendTypeComment(const String16& package,
1767 const String16& type,
1768 const String16& name,
1769 const String16& comment)
1770 {
1771 if (comment.size() <= 0) {
1772 return true;
1773 }
1774
1775 sp<Package> p = mPackages.valueFor(package);
1776 if (p != NULL) {
1777 sp<Type> t = p->getTypes().valueFor(type);
1778 if (t != NULL) {
1779 sp<ConfigList> c = t->getConfigs().valueFor(name);
1780 if (c != NULL) {
1781 c->appendTypeComment(comment);
1782 return true;
1783 }
1784 }
1785 }
1786 return false;
1787 }
1788
1789 void ResourceTable::canAddEntry(const SourcePos& pos,
1790 const String16& package, const String16& type, const String16& name)
1791 {
1792 sp<Type> t = getType(package, type, pos);
1793 if (t != NULL) {
1794 t->canAddEntry(name);
1795 }
1796 }
1797
1798 size_t ResourceTable::size() const {
1799 return mPackages.size();
1800 }
1801
1802 size_t ResourceTable::numLocalResources() const {
1803 return mNumLocal;
1804 }
1805
1806 bool ResourceTable::hasResources() const {
1807 return mNumLocal > 0;
1808 }
1809
1810 sp<AaptFile> ResourceTable::flatten(Bundle* bundle)
1811 {
1812 sp<AaptFile> data = new AaptFile(String8(), AaptGroupEntry(), String8());
1813 status_t err = flatten(bundle, data);
1814 return err == NO_ERROR ? data : NULL;
1815 }
1816
1817 inline uint32_t ResourceTable::getResId(const sp<Package>& p,
1818 const sp<Type>& t,
1819 uint32_t nameId)
1820 {
1821 return makeResId(p->getAssignedId(), t->getIndex(), nameId);
1822 }
1823
1824 uint32_t ResourceTable::getResId(const String16& package,
1825 const String16& type,
1826 const String16& name,
1827 bool onlyPublic) const
1828 {
1829 sp<Package> p = mPackages.valueFor(package);
1830 if (p == NULL) return 0;
1831
1832 // First look for this in the included resources...
1833 uint32_t specFlags = 0;
1834 uint32_t rid = mAssets->getIncludedResources()
1835 .identifierForName(name.string(), name.size(),
1836 type.string(), type.size(),
1837 package.string(), package.size(),
1838 &specFlags);
1839 if (rid != 0) {
1840 if (onlyPublic) {
1841 if ((specFlags & ResTable_typeSpec::SPEC_PUBLIC) == 0) {
1842 return 0;
1843 }
1844 }
1845
1846 if (Res_INTERNALID(rid)) {
1847 return rid;
1848 }
1849 return Res_MAKEID(p->getAssignedId()-1,
1850 Res_GETTYPE(rid),
1851 Res_GETENTRY(rid));
1852 }
1853
1854 sp<Type> t = p->getTypes().valueFor(type);
1855 if (t == NULL) return 0;
1856 sp<ConfigList> c = t->getConfigs().valueFor(name);
1857 if (c == NULL) return 0;
1858 int32_t ei = c->getEntryIndex();
1859 if (ei < 0) return 0;
1860 return getResId(p, t, ei);
1861 }
1862
1863 uint32_t ResourceTable::getResId(const String16& ref,
1864 const String16* defType,
1865 const String16* defPackage,
1866 const char** outErrorMsg,
1867 bool onlyPublic) const
1868 {
1869 String16 package, type, name;
1870 if (!ResTable::expandResourceRef(
1871 ref.string(), ref.size(), &package, &type, &name,
1872 defType, defPackage ? defPackage:&mAssetsPackage,
1873 outErrorMsg)) {
1874 NOISY(printf("Expanding resource: ref=%s\n",
1875 String8(ref).string()));
1876 NOISY(printf("Expanding resource: defType=%s\n",
1877 defType ? String8(*defType).string() : "NULL"));
1878 NOISY(printf("Expanding resource: defPackage=%s\n",
1879 defPackage ? String8(*defPackage).string() : "NULL"));
1880 NOISY(printf("Expanding resource: ref=%s\n", String8(ref).string()));
1881 NOISY(printf("Expanded resource: p=%s, t=%s, n=%s, res=0\n",
1882 String8(package).string(), String8(type).string(),
1883 String8(name).string()));
1884 return 0;
1885 }
1886 uint32_t res = getResId(package, type, name, onlyPublic);
1887 NOISY(printf("Expanded resource: p=%s, t=%s, n=%s, res=%d\n",
1888 String8(package).string(), String8(type).string(),
1889 String8(name).string(), res));
1890 if (res == 0) {
1891 if (outErrorMsg)
1892 *outErrorMsg = "No resource found that matches the given name";
1893 }
1894 return res;
1895 }
1896
1897 bool ResourceTable::isValidResourceName(const String16& s)
1898 {
1899 const char16_t* p = s.string();
1900 bool first = true;
1901 while (*p) {
1902 if ((*p >= 'a' && *p <= 'z')
1903 || (*p >= 'A' && *p <= 'Z')
1904 || *p == '_'
1905 || (!first && *p >= '0' && *p <= '9')) {
1906 first = false;
1907 p++;
1908 continue;
1909 }
1910 return false;
1911 }
1912 return true;
1913 }
1914
1915 bool ResourceTable::stringToValue(Res_value* outValue, StringPool* pool,
1916 const String16& str,
1917 bool preserveSpaces, bool coerceType,
1918 uint32_t attrID,
1919 const Vector<StringPool::entry_style_span>* style,
1920 String16* outStr, void* accessorCookie,
1921 uint32_t attrType)
1922 {
1923 String16 finalStr;
1924
1925 bool res = true;
1926 if (style == NULL || style->size() == 0) {
1927 // Text is not styled so it can be any type... let's figure it out.
1928 res = mAssets->getIncludedResources()
1929 .stringToValue(outValue, &finalStr, str.string(), str.size(), preserveSpaces,
1930 coerceType, attrID, NULL, &mAssetsPackage, this,
1931 accessorCookie, attrType);
1932 } else {
1933 // Styled text can only be a string, and while collecting the style
1934 // information we have already processed that string!
1935 outValue->size = sizeof(Res_value);
1936 outValue->res0 = 0;
1937 outValue->dataType = Res_value::TYPE_STRING;
1938 outValue->data = 0;
1939 finalStr = str;
1940 }
1941
1942 if (!res) {
1943 return false;
1944 }
1945
1946 if (outValue->dataType == Res_value::TYPE_STRING) {
1947 // Should do better merging styles.
1948 if (pool) {
1949 if (style != NULL && style->size() > 0) {
1950 outValue->data = pool->add(finalStr, *style);
1951 } else {
1952 outValue->data = pool->add(finalStr, true);
1953 }
1954 } else {
1955 // Caller will fill this in later.
1956 outValue->data = 0;
1957 }
1958
1959 if (outStr) {
1960 *outStr = finalStr;
1961 }
1962
1963 }
1964
1965 return true;
1966 }
1967
1968 uint32_t ResourceTable::getCustomResource(
1969 const String16& package, const String16& type, const String16& name) const
1970 {
1971 //printf("getCustomResource: %s %s %s\n", String8(package).string(),
1972 // String8(type).string(), String8(name).string());
1973 sp<Package> p = mPackages.valueFor(package);
1974 if (p == NULL) return 0;
1975 sp<Type> t = p->getTypes().valueFor(type);
1976 if (t == NULL) return 0;
1977 sp<ConfigList> c = t->getConfigs().valueFor(name);
1978 if (c == NULL) return 0;
1979 int32_t ei = c->getEntryIndex();
1980 if (ei < 0) return 0;
1981 return getResId(p, t, ei);
1982 }
1983
1984 uint32_t ResourceTable::getCustomResourceWithCreation(
1985 const String16& package, const String16& type, const String16& name,
1986 const bool createIfNotFound)
1987 {
1988 uint32_t resId = getCustomResource(package, type, name);
1989 if (resId != 0 || !createIfNotFound) {
1990 return resId;
1991 }
1992 String16 value("false");
1993
1994 status_t status = addEntry(mCurrentXmlPos, package, type, name, value, NULL, NULL, true);
1995 if (status == NO_ERROR) {
1996 resId = getResId(package, type, name);
1997 return resId;
1998 }
1999 return 0;
2000 }
2001
2002 uint32_t ResourceTable::getRemappedPackage(uint32_t origPackage) const
2003 {
2004 return origPackage;
2005 }
2006
2007 bool ResourceTable::getAttributeType(uint32_t attrID, uint32_t* outType)
2008 {
2009 //printf("getAttributeType #%08x\n", attrID);
2010 Res_value value;
2011 if (getItemValue(attrID, ResTable_map::ATTR_TYPE, &value)) {
2012 //printf("getAttributeType #%08x (%s): #%08x\n", attrID,
2013 // String8(getEntry(attrID)->getName()).string(), value.data);
2014 *outType = value.data;
2015 return true;
2016 }
2017 return false;
2018 }
2019
2020 bool ResourceTable::getAttributeMin(uint32_t attrID, uint32_t* outMin)
2021 {
2022 //printf("getAttributeMin #%08x\n", attrID);
2023 Res_value value;
2024 if (getItemValue(attrID, ResTable_map::ATTR_MIN, &value)) {
2025 *outMin = value.data;
2026 return true;
2027 }
2028 return false;
2029 }
2030
2031 bool ResourceTable::getAttributeMax(uint32_t attrID, uint32_t* outMax)
2032 {
2033 //printf("getAttributeMax #%08x\n", attrID);
2034 Res_value value;
2035 if (getItemValue(attrID, ResTable_map::ATTR_MAX, &value)) {
2036 *outMax = value.data;
2037 return true;
2038 }
2039 return false;
2040 }
2041
2042 uint32_t ResourceTable::getAttributeL10N(uint32_t attrID)
2043 {
2044 //printf("getAttributeL10N #%08x\n", attrID);
2045 Res_value value;
2046 if (getItemValue(attrID, ResTable_map::ATTR_L10N, &value)) {
2047 return value.data;
2048 }
2049 return ResTable_map::L10N_NOT_REQUIRED;
2050 }
2051
2052 bool ResourceTable::getLocalizationSetting()
2053 {
2054 return mBundle->getRequireLocalization();
2055 }
2056
2057 void ResourceTable::reportError(void* accessorCookie, const char* fmt, ...)
2058 {
2059 if (accessorCookie != NULL && fmt != NULL) {
2060 AccessorCookie* ac = (AccessorCookie*)accessorCookie;
2061 int retval=0;
2062 char buf[1024];
2063 va_list ap;
2064 va_start(ap, fmt);
2065 retval = vsnprintf(buf, sizeof(buf), fmt, ap);
2066 va_end(ap);
2067 ac->sourcePos.error("Error: %s (at '%s' with value '%s').\n",
2068 buf, ac->attr.string(), ac->value.string());
2069 }
2070 }
2071
2072 bool ResourceTable::getAttributeKeys(
2073 uint32_t attrID, Vector<String16>* outKeys)
2074 {
2075 sp<const Entry> e = getEntry(attrID);
2076 if (e != NULL) {
2077 const size_t N = e->getBag().size();
2078 for (size_t i=0; i<N; i++) {
2079 const String16& key = e->getBag().keyAt(i);
2080 if (key.size() > 0 && key.string()[0] != '^') {
2081 outKeys->add(key);
2082 }
2083 }
2084 return true;
2085 }
2086 return false;
2087 }
2088
2089 bool ResourceTable::getAttributeEnum(
2090 uint32_t attrID, const char16_t* name, size_t nameLen,
2091 Res_value* outValue)
2092 {
2093 //printf("getAttributeEnum #%08x %s\n", attrID, String8(name, nameLen).string());
2094 String16 nameStr(name, nameLen);
2095 sp<const Entry> e = getEntry(attrID);
2096 if (e != NULL) {
2097 const size_t N = e->getBag().size();
2098 for (size_t i=0; i<N; i++) {
2099 //printf("Comparing %s to %s\n", String8(name, nameLen).string(),
2100 // String8(e->getBag().keyAt(i)).string());
2101 if (e->getBag().keyAt(i) == nameStr) {
2102 return getItemValue(attrID, e->getBag().valueAt(i).bagKeyId, outValue);
2103 }
2104 }
2105 }
2106 return false;
2107 }
2108
2109 bool ResourceTable::getAttributeFlags(
2110 uint32_t attrID, const char16_t* name, size_t nameLen,
2111 Res_value* outValue)
2112 {
2113 outValue->dataType = Res_value::TYPE_INT_HEX;
2114 outValue->data = 0;
2115
2116 //printf("getAttributeFlags #%08x %s\n", attrID, String8(name, nameLen).string());
2117 String16 nameStr(name, nameLen);
2118 sp<const Entry> e = getEntry(attrID);
2119 if (e != NULL) {
2120 const size_t N = e->getBag().size();
2121
2122 const char16_t* end = name + nameLen;
2123 const char16_t* pos = name;
2124 bool failed = false;
2125 while (pos < end && !failed) {
2126 const char16_t* start = pos;
2127 end++;
2128 while (pos < end && *pos != '|') {
2129 pos++;
2130 }
2131
2132 String16 nameStr(start, pos-start);
2133 size_t i;
2134 for (i=0; i<N; i++) {
2135 //printf("Comparing \"%s\" to \"%s\"\n", String8(nameStr).string(),
2136 // String8(e->getBag().keyAt(i)).string());
2137 if (e->getBag().keyAt(i) == nameStr) {
2138 Res_value val;
2139 bool got = getItemValue(attrID, e->getBag().valueAt(i).bagKeyId, &val);
2140 if (!got) {
2141 return false;
2142 }
2143 //printf("Got value: 0x%08x\n", val.data);
2144 outValue->data |= val.data;
2145 break;
2146 }
2147 }
2148
2149 if (i >= N) {
2150 // Didn't find this flag identifier.
2151 return false;
2152 }
2153 if (pos < end) {
2154 pos++;
2155 }
2156 }
2157
2158 return true;
2159 }
2160 return false;
2161 }
2162
2163 status_t ResourceTable::assignResourceIds()
2164 {
2165 const size_t N = mOrderedPackages.size();
2166 size_t pi;
2167 status_t firstError = NO_ERROR;
2168
2169 // First generate all bag attributes and assign indices.
2170 for (pi=0; pi<N; pi++) {
2171 sp<Package> p = mOrderedPackages.itemAt(pi);
2172 if (p == NULL || p->getTypes().size() == 0) {
2173 // Empty, skip!
2174 continue;
2175 }
2176
2177 status_t err = p->applyPublicTypeOrder();
2178 if (err != NO_ERROR && firstError == NO_ERROR) {
2179 firstError = err;
2180 }
2181
2182 // Generate attributes...
2183 const size_t N = p->getOrderedTypes().size();
2184 size_t ti;
2185 for (ti=0; ti<N; ti++) {
2186 sp<Type> t = p->getOrderedTypes().itemAt(ti);
2187 if (t == NULL) {
2188 continue;
2189 }
2190 const size_t N = t->getOrderedConfigs().size();
2191 for (size_t ci=0; ci<N; ci++) {
2192 sp<ConfigList> c = t->getOrderedConfigs().itemAt(ci);
2193 if (c == NULL) {
2194 continue;
2195 }
2196 const size_t N = c->getEntries().size();
2197 for (size_t ei=0; ei<N; ei++) {
2198 sp<Entry> e = c->getEntries().valueAt(ei);
2199 if (e == NULL) {
2200 continue;
2201 }
2202 status_t err = e->generateAttributes(this, p->getName());
2203 if (err != NO_ERROR && firstError == NO_ERROR) {
2204 firstError = err;
2205 }
2206 }
2207 }
2208 }
2209
2210 const SourcePos unknown(String8("????"), 0);
2211 sp<Type> attr = p->getType(String16("attr"), unknown);
2212
2213 // Assign indices...
2214 for (ti=0; ti<N; ti++) {
2215 sp<Type> t = p->getOrderedTypes().itemAt(ti);
2216 if (t == NULL) {
2217 continue;
2218 }
2219 err = t->applyPublicEntryOrder();
2220 if (err != NO_ERROR && firstError == NO_ERROR) {
2221 firstError = err;
2222 }
2223
2224 const size_t N = t->getOrderedConfigs().size();
2225 t->setIndex(ti+1);
2226
2227 LOG_ALWAYS_FATAL_IF(ti == 0 && attr != t,
2228 "First type is not attr!");
2229
2230 for (size_t ei=0; ei<N; ei++) {
2231 sp<ConfigList> c = t->getOrderedConfigs().itemAt(ei);
2232 if (c == NULL) {
2233 continue;
2234 }
2235 c->setEntryIndex(ei);
2236 }
2237 }
2238
2239 // Assign resource IDs to keys in bags...
2240 for (ti=0; ti<N; ti++) {
2241 sp<Type> t = p->getOrderedTypes().itemAt(ti);
2242 if (t == NULL) {
2243 continue;
2244 }
2245 const size_t N = t->getOrderedConfigs().size();
2246 for (size_t ci=0; ci<N; ci++) {
2247 sp<ConfigList> c = t->getOrderedConfigs().itemAt(ci);
2248 //printf("Ordered config #%d: %p\n", ci, c.get());
2249 const size_t N = c->getEntries().size();
2250 for (size_t ei=0; ei<N; ei++) {
2251 sp<Entry> e = c->getEntries().valueAt(ei);
2252 if (e == NULL) {
2253 continue;
2254 }
2255 status_t err = e->assignResourceIds(this, p->getName());
2256 if (err != NO_ERROR && firstError == NO_ERROR) {
2257 firstError = err;
2258 }
2259 }
2260 }
2261 }
2262 }
2263 return firstError;
2264 }
2265
2266 status_t ResourceTable::addSymbols(const sp<AaptSymbols>& outSymbols) {
2267 const size_t N = mOrderedPackages.size();
2268 size_t pi;
2269
2270 for (pi=0; pi<N; pi++) {
2271 sp<Package> p = mOrderedPackages.itemAt(pi);
2272 if (p->getTypes().size() == 0) {
2273 // Empty, skip!
2274 continue;
2275 }
2276
2277 const size_t N = p->getOrderedTypes().size();
2278 size_t ti;
2279
2280 for (ti=0; ti<N; ti++) {
2281 sp<Type> t = p->getOrderedTypes().itemAt(ti);
2282 if (t == NULL) {
2283 continue;
2284 }
2285 const size_t N = t->getOrderedConfigs().size();
2286 sp<AaptSymbols> typeSymbols;
2287 typeSymbols = outSymbols->addNestedSymbol(String8(t->getName()), t->getPos());
2288 for (size_t ci=0; ci<N; ci++) {
2289 sp<ConfigList> c = t->getOrderedConfigs().itemAt(ci);
2290 if (c == NULL) {
2291 continue;
2292 }
2293 uint32_t rid = getResId(p, t, ci);
2294 if (rid == 0) {
2295 return UNKNOWN_ERROR;
2296 }
2297 if (Res_GETPACKAGE(rid) == (size_t)(p->getAssignedId()-1)) {
2298 typeSymbols->addSymbol(String8(c->getName()), rid, c->getPos());
2299
2300 String16 comment(c->getComment());
2301 typeSymbols->appendComment(String8(c->getName()), comment, c->getPos());
2302 //printf("Type symbol %s comment: %s\n", String8(e->getName()).string(),
2303 // String8(comment).string());
2304 comment = c->getTypeComment();
2305 typeSymbols->appendTypeComment(String8(c->getName()), comment);
2306 } else {
2307 #if 0
2308 printf("**** NO MATCH: 0x%08x vs 0x%08x\n",
2309 Res_GETPACKAGE(rid), p->getAssignedId());
2310 #endif
2311 }
2312 }
2313 }
2314 }
2315 return NO_ERROR;
2316 }
2317
2318
2319 void
2320 ResourceTable::addLocalization(const String16& name, const String8& locale)
2321 {
2322 mLocalizations[name].insert(locale);
2323 }
2324
2325
2326 /*!
2327 * Flag various sorts of localization problems. '+' indicates checks already implemented;
2328 * '-' indicates checks that will be implemented in the future.
2329 *
2330 * + A localized string for which no default-locale version exists => warning
2331 * + A string for which no version in an explicitly-requested locale exists => warning
2332 * + A localized translation of an translateable="false" string => warning
2333 * - A localized string not provided in every locale used by the table
2334 */
2335 status_t
2336 ResourceTable::validateLocalizations(void)
2337 {
2338 status_t err = NO_ERROR;
2339 const String8 defaultLocale;
2340
2341 // For all strings...
2342 for (map<String16, set<String8> >::iterator nameIter = mLocalizations.begin();
2343 nameIter != mLocalizations.end();
2344 nameIter++) {
2345 const set<String8>& configSet = nameIter->second; // naming convenience
2346
2347 // Look for strings with no default localization
2348 if (configSet.count(defaultLocale) == 0) {
2349 fprintf(stdout, "aapt: warning: string '%s' has no default translation in %s; found:",
2350 String8(nameIter->first).string(), mBundle->getResourceSourceDirs()[0]);
2351 for (set<String8>::iterator locales = configSet.begin();
2352 locales != configSet.end();
2353 locales++) {
2354 fprintf(stdout, " %s", (*locales).string());
2355 }
2356 fprintf(stdout, "\n");
2357 // !!! TODO: throw an error here in some circumstances
2358 }
2359
2360 // Check that all requested localizations are present for this string
2361 if (mBundle->getConfigurations() != NULL && mBundle->getRequireLocalization()) {
2362 const char* allConfigs = mBundle->getConfigurations();
2363 const char* start = allConfigs;
2364 const char* comma;
2365
2366 do {
2367 String8 config;
2368 comma = strchr(start, ',');
2369 if (comma != NULL) {
2370 config.setTo(start, comma - start);
2371 start = comma + 1;
2372 } else {
2373 config.setTo(start);
2374 }
2375
2376 // don't bother with the pseudolocale "zz_ZZ"
2377 if (config != "zz_ZZ") {
2378 if (configSet.find(config) == configSet.end()) {
2379 // okay, no specific localization found. it's possible that we are
2380 // requiring a specific regional localization [e.g. de_DE] but there is an
2381 // available string in the generic language localization [e.g. de];
2382 // consider that string to have fulfilled the localization requirement.
2383 String8 region(config.string(), 2);
2384 if (configSet.find(region) == configSet.end()) {
2385 if (configSet.count(defaultLocale) == 0) {
2386 fprintf(stdout, "aapt: warning: "
2387 "**** string '%s' has no default or required localization "
2388 "for '%s' in %s\n",
2389 String8(nameIter->first).string(),
2390 config.string(),
2391 mBundle->getResourceSourceDirs()[0]);
2392 }
2393 }
2394 }
2395 }
2396 } while (comma != NULL);
2397 }
2398 }
2399
2400 return err;
2401 }
2402
2403
2404 status_t
2405 ResourceFilter::parse(const char* arg)
2406 {
2407 if (arg == NULL) {
2408 return 0;
2409 }
2410
2411 const char* p = arg;
2412 const char* q;
2413
2414 while (true) {
2415 q = strchr(p, ',');
2416 if (q == NULL) {
2417 q = p + strlen(p);
2418 }
2419
2420 String8 part(p, q-p);
2421
2422 if (part == "zz_ZZ") {
2423 mContainsPseudo = true;
2424 }
2425 int axis;
2426 uint32_t value;
2427 if (AaptGroupEntry::parseNamePart(part, &axis, &value)) {
2428 fprintf(stderr, "Invalid configuration: %s\n", arg);
2429 fprintf(stderr, " ");
2430 for (int i=0; i<p-arg; i++) {
2431 fprintf(stderr, " ");
2432 }
2433 for (int i=0; i<q-p; i++) {
2434 fprintf(stderr, "^");
2435 }
2436 fprintf(stderr, "\n");
2437 return 1;
2438 }
2439
2440 ssize_t index = mData.indexOfKey(axis);
2441 if (index < 0) {
2442 mData.add(axis, SortedVector<uint32_t>());
2443 }
2444 SortedVector<uint32_t>& sv = mData.editValueFor(axis);
2445 sv.add(value);
2446 // if it's a locale with a region, also match an unmodified locale of the
2447 // same language
2448 if (axis == AXIS_LANGUAGE) {
2449 if (value & 0xffff0000) {
2450 sv.add(value & 0x0000ffff);
2451 }
2452 }
2453 p = q;
2454 if (!*p) break;
2455 p++;
2456 }
2457
2458 return NO_ERROR;
2459 }
2460
2461 bool
2462 ResourceFilter::match(int axis, uint32_t value)
2463 {
2464 if (value == 0) {
2465 // they didn't specify anything so take everything
2466 return true;
2467 }
2468 ssize_t index = mData.indexOfKey(axis);
2469 if (index < 0) {
2470 // we didn't request anything on this axis so take everything
2471 return true;
2472 }
2473 const SortedVector<uint32_t>& sv = mData.valueAt(index);
2474 return sv.indexOf(value) >= 0;
2475 }
2476
2477 bool
2478 ResourceFilter::match(const ResTable_config& config)
2479 {
2480 if (config.locale) {
2481 uint32_t locale = (config.country[1] << 24) | (config.country[0] << 16)
2482 | (config.language[1] << 8) | (config.language[0]);
2483 if (!match(AXIS_LANGUAGE, locale)) {
2484 return false;
2485 }
2486 }
2487 if (!match(AXIS_ORIENTATION, config.orientation)) {
2488 return false;
2489 }
2490 if (!match(AXIS_UIMODETYPE, (config.uiMode&ResTable_config::MASK_UI_MODE_TYPE))) {
2491 return false;
2492 }
2493 if (!match(AXIS_UIMODENIGHT, (config.uiMode&ResTable_config::MASK_UI_MODE_NIGHT))) {
2494 return false;
2495 }
2496 if (!match(AXIS_DENSITY, config.density)) {
2497 return false;
2498 }
2499 if (!match(AXIS_TOUCHSCREEN, config.touchscreen)) {
2500 return false;
2501 }
2502 if (!match(AXIS_KEYSHIDDEN, config.inputFlags)) {
2503 return false;
2504 }
2505 if (!match(AXIS_KEYBOARD, config.keyboard)) {
2506 return false;
2507 }
2508 if (!match(AXIS_NAVIGATION, config.navigation)) {
2509 return false;
2510 }
2511 if (!match(AXIS_SCREENSIZE, config.screenSize)) {
2512 return false;
2513 }
2514 if (!match(AXIS_VERSION, config.version)) {
2515 return false;
2516 }
2517 return true;
2518 }
2519
2520 status_t ResourceTable::flatten(Bundle* bundle, const sp<AaptFile>& dest)
2521 {
2522 ResourceFilter filter;
2523 status_t err = filter.parse(bundle->getConfigurations());
2524 if (err != NO_ERROR) {
2525 return err;
2526 }
2527
2528 const size_t N = mOrderedPackages.size();
2529 size_t pi;
2530
2531 // Iterate through all data, collecting all values (strings,
2532 // references, etc).
2533 StringPool valueStrings = StringPool(false, bundle->getUTF8());
2534 ResourceConfigReferences configRefs;
2535 for (pi=0; pi<N; pi++) {
2536 sp<Package> p = mOrderedPackages.itemAt(pi);
2537 if (p->getTypes().size() == 0) {
2538 // Empty, skip!
2539 continue;
2540 }
2541
2542 StringPool typeStrings = StringPool(false, bundle->getUTF8());
2543 StringPool keyStrings = StringPool(false, bundle->getUTF8());
2544
2545 const size_t N = p->getOrderedTypes().size();
2546 for (size_t ti=0; ti<N; ti++) {
2547 sp<Type> t = p->getOrderedTypes().itemAt(ti);
2548 if (t == NULL) {
2549 typeStrings.add(String16("<empty>"), false);
2550 continue;
2551 }
2552 typeStrings.add(t->getName(), false);
2553
2554 const size_t N = t->getOrderedConfigs().size();
2555 for (size_t ci=0; ci<N; ci++) {
2556 sp<ConfigList> c = t->getOrderedConfigs().itemAt(ci);
2557 if (c == NULL) {
2558 continue;
2559 }
2560 const size_t N = c->getEntries().size();
2561 for (size_t ei=0; ei<N; ei++) {
2562 ConfigDescription config = c->getEntries().keyAt(ei);
2563 if (!filter.match(config)) {
2564 continue;
2565 }
2566 sp<Entry> e = c->getEntries().valueAt(ei);
2567 if (e == NULL) {
2568 continue;
2569 }
2570 e->setNameIndex(keyStrings.add(e->getName(), true));
2571 status_t err = e->prepareFlatten(&valueStrings, this);
2572 if (err != NO_ERROR) {
2573 return err;
2574 }
2575 if (e->getType() == Entry::TYPE_ITEM) {
2576 const Item* item = e->getItem();
2577 if (item != NULL) {
2578 uint32_t poolIndex = item->parsedValue.data;
2579 configRefs.add(poolIndex, config);
2580 }
2581 }
2582 }
2583 }
2584 }
2585
2586 p->setTypeStrings(typeStrings.createStringBlock());
2587 p->setKeyStrings(keyStrings.createStringBlock());
2588 }
2589
2590 NOISY_REF(configRefs.dump();)
2591
2592 // Trim all entries in config tables that are not roots for that string.
2593 // i.e., rely on the resource system to grab the string from the more
2594 // generic pool during runtime to save space.
2595 for (pi=0; pi<N; pi++) {
2596 sp<Package> p = mOrderedPackages.itemAt(pi);
2597 if (p->getTypes().size() == 0) {
2598 // Empty, skip!
2599 continue;
2600 }
2601 const size_t TN = p->getOrderedTypes().size();
2602 for (size_t ti=0; ti<TN; ti++) {
2603 sp<Type> t = p->getOrderedTypes().itemAt(ti);
2604 if (t == NULL) {
2605 continue;
2606 }
2607 size_t CN = t->getOrderedConfigs().size();
2608 for (size_t ci=0; ci<CN; ci++) {
2609 sp<ConfigList> c = t->getOrderedConfigs().itemAt(ci);
2610 if (c == NULL) {
2611 continue;
2612 }
2613 DefaultKeyedVector<ConfigDescription, sp<Entry> > newEntries;
2614 size_t EN = c->getEntries().size();
2615 for (size_t ei=0; ei<EN; ++ei) {
2616 ConfigDescription config = c->getEntries().keyAt(ei);
2617 if (!filter.match(config)) {
2618 continue;
2619 }
2620 sp<Entry> e = c->getEntries().valueAt(ei);
2621 if (e == NULL) {
2622 continue;
2623 }
2624 if (e->getType() == Entry::TYPE_ITEM) {
2625 const Item* item = e->getItem();
2626 if (item != NULL) {
2627 uint32_t poolIndex = item->parsedValue.data;
2628 if (!configRefs.isRoot(poolIndex, config)) {
2629 NOISY_REF(
2630 printf(" ConfigRef %d: removing ", poolIndex);
2631 DEBUG_LANG(config)
2632 printf("\n");
2633 )
2634 c->removeEntryAt(ei);
2635 t->removeUniqueConfig(config);
2636 --ei; --EN;
2637 }
2638 }
2639 }
2640 }
2641 if (EN == 0) {
2642 // We removed all the entries from a config, so remove the
2643 // config itself.
2644 NOISY_REF(
2645 printf(" ConfigRef REMOVED ENTIRE CONFIG\n");
2646 )
2647 t->removeOrderedConfigAt(ci);
2648 --ci; --CN;
2649 }
2650 }
2651 }
2652 }
2653
2654 ssize_t strAmt = 0;
2655
2656 // Now build the array of package chunks.
2657 Vector<sp<AaptFile> > flatPackages;
2658 for (pi=0; pi<N; pi++) {
2659 sp<Package> p = mOrderedPackages.itemAt(pi);
2660 if (p->getTypes().size() == 0) {
2661 // Empty, skip!
2662 continue;
2663 }
2664
2665 const size_t N = p->getTypeStrings().size();
2666
2667 const size_t baseSize = sizeof(ResTable_package);
2668
2669 // Start the package data.
2670 sp<AaptFile> data = new AaptFile(String8(), AaptGroupEntry(), String8());
2671 ResTable_package* header = (ResTable_package*)data->editData(baseSize);
2672 if (header == NULL) {
2673 fprintf(stderr, "ERROR: out of memory creating ResTable_package\n");
2674 return NO_MEMORY;
2675 }
2676 memset(header, 0, sizeof(*header));
2677 header->header.type = htods(RES_TABLE_PACKAGE_TYPE);
2678 header->header.headerSize = htods(sizeof(*header));
2679 header->id = htodl(p->getAssignedId());
2680 strcpy16_htod(header->name, p->getName().string());
2681
2682 // Write the string blocks.
2683 const size_t typeStringsStart = data->getSize();
2684 sp<AaptFile> strFile = p->getTypeStringsData();
2685 ssize_t amt = data->writeData(strFile->getData(), strFile->getSize());
2686 #if PRINT_STRING_METRICS
2687 fprintf(stderr, "**** type strings: %d\n", amt);
2688 #endif
2689 strAmt += amt;
2690 if (amt < 0) {
2691 return amt;
2692 }
2693 const size_t keyStringsStart = data->getSize();
2694 strFile = p->getKeyStringsData();
2695 amt = data->writeData(strFile->getData(), strFile->getSize());
2696 #if PRINT_STRING_METRICS
2697 fprintf(stderr, "**** key strings: %d\n", amt);
2698 #endif
2699 strAmt += amt;
2700 if (amt < 0) {
2701 return amt;
2702 }
2703
2704 // Build the type chunks inside of this package.
2705 for (size_t ti=0; ti<N; ti++) {
2706 // Retrieve them in the same order as the type string block.
2707 size_t len;
2708 String16 typeName(p->getTypeStrings().stringAt(ti, &len));
2709 sp<Type> t = p->getTypes().valueFor(typeName);
2710 LOG_ALWAYS_FATAL_IF(t == NULL && typeName != String16("<empty>"),
2711 "Type name %s not found",
2712 String8(typeName).string());
2713
2714 const size_t N = t != NULL ? t->getOrderedConfigs().size() : 0;
2715
2716 // First write the typeSpec chunk, containing information about
2717 // each resource entry in this type.
2718 {
2719 const size_t typeSpecSize = sizeof(ResTable_typeSpec) + sizeof(uint32_t)*N;
2720 const size_t typeSpecStart = data->getSize();
2721 ResTable_typeSpec* tsHeader = (ResTable_typeSpec*)
2722 (((uint8_t*)data->editData(typeSpecStart+typeSpecSize)) + typeSpecStart);
2723 if (tsHeader == NULL) {
2724 fprintf(stderr, "ERROR: out of memory creating ResTable_typeSpec\n");
2725 return NO_MEMORY;
2726 }
2727 memset(tsHeader, 0, sizeof(*tsHeader));
2728 tsHeader->header.type = htods(RES_TABLE_TYPE_SPEC_TYPE);
2729 tsHeader->header.headerSize = htods(sizeof(*tsHeader));
2730 tsHeader->header.size = htodl(typeSpecSize);
2731 tsHeader->id = ti+1;
2732 tsHeader->entryCount = htodl(N);
2733
2734 uint32_t* typeSpecFlags = (uint32_t*)
2735 (((uint8_t*)data->editData())
2736 + typeSpecStart + sizeof(ResTable_typeSpec));
2737 memset(typeSpecFlags, 0, sizeof(uint32_t)*N);
2738
2739 for (size_t ei=0; ei<N; ei++) {
2740 sp<ConfigList> cl = t->getOrderedConfigs().itemAt(ei);
2741 if (cl->getPublic()) {
2742 typeSpecFlags[ei] |= htodl(ResTable_typeSpec::SPEC_PUBLIC);
2743 }
2744 const size_t CN = cl->getEntries().size();
2745 for (size_t ci=0; ci<CN; ci++) {
2746 if (!filter.match(cl->getEntries().keyAt(ci))) {
2747 continue;
2748 }
2749 for (size_t cj=ci+1; cj<CN; cj++) {
2750 if (!filter.match(cl->getEntries().keyAt(cj))) {
2751 continue;
2752 }
2753 typeSpecFlags[ei] |= htodl(
2754 cl->getEntries().keyAt(ci).diff(cl->getEntries().keyAt(cj)));
2755 }
2756 }
2757 }
2758 }
2759
2760 // We need to write one type chunk for each configuration for
2761 // which we have entries in this type.
2762 const size_t NC = t->getUniqueConfigs().size();
2763
2764 const size_t typeSize = sizeof(ResTable_type) + sizeof(uint32_t)*N;
2765
2766 for (size_t ci=0; ci<NC; ci++) {
2767 ConfigDescription config = t->getUniqueConfigs().itemAt(ci);
2768
2769 NOISY(printf("Writing config %d config: imsi:%d/%d lang:%c%c cnt:%c%c "
2770 "orien:%d ui:%d touch:%d density:%d key:%d inp:%d nav:%d w:%d h:%d\n",
2771 ti+1,
2772 config.mcc, config.mnc,
2773 config.language[0] ? config.language[0] : '-',
2774 config.language[1] ? config.language[1] : '-',
2775 config.country[0] ? config.country[0] : '-',
2776 config.country[1] ? config.country[1] : '-',
2777 config.orientation,
2778 config.uiMode,
2779 config.touchscreen,
2780 config.density,
2781 config.keyboard,
2782 config.inputFlags,
2783 config.navigation,
2784 config.screenWidth,
2785 config.screenHeight));
2786
2787 if (!filter.match(config)) {
2788 continue;
2789 }
2790
2791 const size_t typeStart = data->getSize();
2792
2793 ResTable_type* tHeader = (ResTable_type*)
2794 (((uint8_t*)data->editData(typeStart+typeSize)) + typeStart);
2795 if (tHeader == NULL) {
2796 fprintf(stderr, "ERROR: out of memory creating ResTable_type\n");
2797 return NO_MEMORY;
2798 }
2799
2800 memset(tHeader, 0, sizeof(*tHeader));
2801 tHeader->header.type = htods(RES_TABLE_TYPE_TYPE);
2802 tHeader->header.headerSize = htods(sizeof(*tHeader));
2803 tHeader->id = ti+1;
2804 tHeader->entryCount = htodl(N);
2805 tHeader->entriesStart = htodl(typeSize);
2806 tHeader->config = config;
2807 NOISY(printf("Writing type %d config: imsi:%d/%d lang:%c%c cnt:%c%c "
2808 "orien:%d ui:%d touch:%d density:%d key:%d inp:%d nav:%d w:%d h:%d\n",
2809 ti+1,
2810 tHeader->config.mcc, tHeader->config.mnc,
2811 tHeader->config.language[0] ? tHeader->config.language[0] : '-',
2812 tHeader->config.language[1] ? tHeader->config.language[1] : '-',
2813 tHeader->config.country[0] ? tHeader->config.country[0] : '-',
2814 tHeader->config.country[1] ? tHeader->config.country[1] : '-',
2815 tHeader->config.orientation,
2816 tHeader->config.uiMode,
2817 tHeader->config.touchscreen,
2818 tHeader->config.density,
2819 tHeader->config.keyboard,
2820 tHeader->config.inputFlags,
2821 tHeader->config.navigation,
2822 tHeader->config.screenWidth,
2823 tHeader->config.screenHeight));
2824 tHeader->config.swapHtoD();
2825
2826 // Build the entries inside of this type.
2827 for (size_t ei=0; ei<N; ei++) {
2828 sp<ConfigList> cl = t->getOrderedConfigs().itemAt(ei);
2829 sp<Entry> e = cl->getEntries().valueFor(config);
2830
2831 // Set the offset for this entry in its type.
2832 uint32_t* index = (uint32_t*)
2833 (((uint8_t*)data->editData())
2834 + typeStart + sizeof(ResTable_type));
2835 if (e != NULL) {
2836 index[ei] = htodl(data->getSize()-typeStart-typeSize);
2837
2838 // Create the entry.
2839 ssize_t amt = e->flatten(bundle, data, cl->getPublic());
2840 if (amt < 0) {
2841 return amt;
2842 }
2843 } else {
2844 index[ei] = htodl(ResTable_type::NO_ENTRY);
2845 }
2846 }
2847
2848 // Fill in the rest of the type information.
2849 tHeader = (ResTable_type*)
2850 (((uint8_t*)data->editData()) + typeStart);
2851 tHeader->header.size = htodl(data->getSize()-typeStart);
2852 }
2853 }
2854
2855 // Fill in the rest of the package information.
2856 header = (ResTable_package*)data->editData();
2857 header->header.size = htodl(data->getSize());
2858 header->typeStrings = htodl(typeStringsStart);
2859 header->lastPublicType = htodl(p->getTypeStrings().size());
2860 header->keyStrings = htodl(keyStringsStart);
2861 header->lastPublicKey = htodl(p->getKeyStrings().size());
2862
2863 flatPackages.add(data);
2864 }
2865
2866 // And now write out the final chunks.
2867 const size_t dataStart = dest->getSize();
2868
2869 {
2870 // blah
2871 ResTable_header header;
2872 memset(&header, 0, sizeof(header));
2873 header.header.type = htods(RES_TABLE_TYPE);
2874 header.header.headerSize = htods(sizeof(header));
2875 header.packageCount = htodl(flatPackages.size());
2876 status_t err = dest->writeData(&header, sizeof(header));
2877 if (err != NO_ERROR) {
2878 fprintf(stderr, "ERROR: out of memory creating ResTable_header\n");
2879 return err;
2880 }
2881 }
2882
2883 ssize_t strStart = dest->getSize();
2884 err = valueStrings.writeStringBlock(dest);
2885 if (err != NO_ERROR) {
2886 return err;
2887 }
2888
2889 ssize_t amt = (dest->getSize()-strStart);
2890 strAmt += amt;
2891 #if PRINT_STRING_METRICS
2892 fprintf(stderr, "**** value strings: %d\n", amt);
2893 fprintf(stderr, "**** total strings: %d\n", strAmt);
2894 #endif
2895
2896 for (pi=0; pi<flatPackages.size(); pi++) {
2897 err = dest->writeData(flatPackages[pi]->getData(),
2898 flatPackages[pi]->getSize());
2899 if (err != NO_ERROR) {
2900 fprintf(stderr, "ERROR: out of memory creating package chunk for ResTable_header\n");
2901 return err;
2902 }
2903 }
2904
2905 ResTable_header* header = (ResTable_header*)
2906 (((uint8_t*)dest->getData()) + dataStart);
2907 header->header.size = htodl(dest->getSize() - dataStart);
2908
2909 NOISY(aout << "Resource table:"
2910 << HexDump(dest->getData(), dest->getSize()) << endl);
2911
2912 #if PRINT_STRING_METRICS
2913 fprintf(stderr, "**** total resource table size: %d / %d%% strings\n",
2914 dest->getSize(), (strAmt*100)/dest->getSize());
2915 #endif
2916
2917 return NO_ERROR;
2918 }
2919
2920 void ResourceTable::writePublicDefinitions(const String16& package, FILE* fp)
2921 {
2922 fprintf(fp,
2923 "<!-- This file contains <public> resource definitions for all\n"
2924 " resources that were generated from the source data. -->\n"
2925 "\n"
2926 "<resources>\n");
2927
2928 writePublicDefinitions(package, fp, true);
2929 writePublicDefinitions(package, fp, false);
2930
2931 fprintf(fp,
2932 "\n"
2933 "</resources>\n");
2934 }
2935
2936 void ResourceTable::writePublicDefinitions(const String16& package, FILE* fp, bool pub)
2937 {
2938 bool didHeader = false;
2939
2940 sp<Package> pkg = mPackages.valueFor(package);
2941 if (pkg != NULL) {
2942 const size_t NT = pkg->getOrderedTypes().size();
2943 for (size_t i=0; i<NT; i++) {
2944 sp<Type> t = pkg->getOrderedTypes().itemAt(i);
2945 if (t == NULL) {
2946 continue;
2947 }
2948
2949 bool didType = false;
2950
2951 const size_t NC = t->getOrderedConfigs().size();
2952 for (size_t j=0; j<NC; j++) {
2953 sp<ConfigList> c = t->getOrderedConfigs().itemAt(j);
2954 if (c == NULL) {
2955 continue;
2956 }
2957
2958 if (c->getPublic() != pub) {
2959 continue;
2960 }
2961
2962 if (!didType) {
2963 fprintf(fp, "\n");
2964 didType = true;
2965 }
2966 if (!didHeader) {
2967 if (pub) {
2968 fprintf(fp," <!-- PUBLIC SECTION. These resources have been declared public.\n");
2969 fprintf(fp," Changes to these definitions will break binary compatibility. -->\n\n");
2970 } else {
2971 fprintf(fp," <!-- PRIVATE SECTION. These resources have not been declared public.\n");
2972 fprintf(fp," You can make them public my moving these lines into a file in res/values. -->\n\n");
2973 }
2974 didHeader = true;
2975 }
2976 if (!pub) {
2977 const size_t NE = c->getEntries().size();
2978 for (size_t k=0; k<NE; k++) {
2979 const SourcePos& pos = c->getEntries().valueAt(k)->getPos();
2980 if (pos.file != "") {
2981 fprintf(fp," <!-- Declared at %s:%d -->\n",
2982 pos.file.string(), pos.line);
2983 }
2984 }
2985 }
2986 fprintf(fp, " <public type=\"%s\" name=\"%s\" id=\"0x%08x\" />\n",
2987 String8(t->getName()).string(),
2988 String8(c->getName()).string(),
2989 getResId(pkg, t, c->getEntryIndex()));
2990 }
2991 }
2992 }
2993 }
2994
2995 ResourceTable::Item::Item(const SourcePos& _sourcePos,
2996 bool _isId,
2997 const String16& _value,
2998 const Vector<StringPool::entry_style_span>* _style,
2999 int32_t _format)
3000 : sourcePos(_sourcePos)
3001 , isId(_isId)
3002 , value(_value)
3003 , format(_format)
3004 , bagKeyId(0)
3005 , evaluating(false)
3006 {
3007 if (_style) {
3008 style = *_style;
3009 }
3010 }
3011
3012 status_t ResourceTable::Entry::makeItABag(const SourcePos& sourcePos)
3013 {
3014 if (mType == TYPE_BAG) {
3015 return NO_ERROR;
3016 }
3017 if (mType == TYPE_UNKNOWN) {
3018 mType = TYPE_BAG;
3019 return NO_ERROR;
3020 }
3021 sourcePos.error("Resource entry %s is already defined as a single item.\n"
3022 "%s:%d: Originally defined here.\n",
3023 String8(mName).string(),
3024 mItem.sourcePos.file.string(), mItem.sourcePos.line);
3025 return UNKNOWN_ERROR;
3026 }
3027
3028 status_t ResourceTable::Entry::setItem(const SourcePos& sourcePos,
3029 const String16& value,
3030 const Vector<StringPool::entry_style_span>* style,
3031 int32_t format,
3032 const bool overwrite)
3033 {
3034 Item item(sourcePos, false, value, style);
3035
3036 if (mType == TYPE_BAG) {
3037 const Item& item(mBag.valueAt(0));
3038 sourcePos.error("Resource entry %s is already defined as a bag.\n"
3039 "%s:%d: Originally defined here.\n",
3040 String8(mName).string(),
3041 item.sourcePos.file.string(), item.sourcePos.line);
3042 return UNKNOWN_ERROR;
3043 }
3044 if ( (mType != TYPE_UNKNOWN) && (overwrite == false) ) {
3045 sourcePos.error("Resource entry %s is already defined.\n"
3046 "%s:%d: Originally defined here.\n",
3047 String8(mName).string(),
3048 mItem.sourcePos.file.string(), mItem.sourcePos.line);
3049 return UNKNOWN_ERROR;
3050 }
3051
3052 mType = TYPE_ITEM;
3053 mItem = item;
3054 mItemFormat = format;
3055 return NO_ERROR;
3056 }
3057
3058 status_t ResourceTable::Entry::addToBag(const SourcePos& sourcePos,
3059 const String16& key, const String16& value,
3060 const Vector<StringPool::entry_style_span>* style,
3061 bool replace, bool isId, int32_t format)
3062 {
3063 status_t err = makeItABag(sourcePos);
3064 if (err != NO_ERROR) {
3065 return err;
3066 }
3067
3068 Item item(sourcePos, isId, value, style, format);
3069
3070 // XXX NOTE: there is an error if you try to have a bag with two keys,
3071 // one an attr and one an id, with the same name. Not something we
3072 // currently ever have to worry about.
3073 ssize_t origKey = mBag.indexOfKey(key);
3074 if (origKey >= 0) {
3075 if (!replace) {
3076 const Item& item(mBag.valueAt(origKey));
3077 sourcePos.error("Resource entry %s already has bag item %s.\n"
3078 "%s:%d: Originally defined here.\n",
3079 String8(mName).string(), String8(key).string(),
3080 item.sourcePos.file.string(), item.sourcePos.line);
3081 return UNKNOWN_ERROR;
3082 }
3083 //printf("Replacing %s with %s\n",
3084 // String8(mBag.valueFor(key).value).string(), String8(value).string());
3085 mBag.replaceValueFor(key, item);
3086 }
3087
3088 mBag.add(key, item);
3089 return NO_ERROR;
3090 }
3091
3092 status_t ResourceTable::Entry::emptyBag(const SourcePos& sourcePos)
3093 {
3094 status_t err = makeItABag(sourcePos);
3095 if (err != NO_ERROR) {
3096 return err;
3097 }
3098
3099 mBag.clear();
3100 return NO_ERROR;
3101 }
3102
3103 status_t ResourceTable::Entry::generateAttributes(ResourceTable* table,
3104 const String16& package)
3105 {
3106 const String16 attr16("attr");
3107 const String16 id16("id");
3108 const size_t N = mBag.size();
3109 for (size_t i=0; i<N; i++) {
3110 const String16& key = mBag.keyAt(i);
3111 const Item& it = mBag.valueAt(i);
3112 if (it.isId) {
3113 if (!table->hasBagOrEntry(key, &id16, &package)) {
3114 String16 value("false");
3115 status_t err = table->addEntry(SourcePos(String8("<generated>"), 0), package,
3116 id16, key, value);
3117 if (err != NO_ERROR) {
3118 return err;
3119 }
3120 }
3121 } else if (!table->hasBagOrEntry(key, &attr16, &package)) {
3122
3123 #if 1
3124 // fprintf(stderr, "ERROR: Bag attribute '%s' has not been defined.\n",
3125 // String8(key).string());
3126 // const Item& item(mBag.valueAt(i));
3127 // fprintf(stderr, "Referenced from file %s line %d\n",
3128 // item.sourcePos.file.string(), item.sourcePos.line);
3129 // return UNKNOWN_ERROR;
3130 #else
3131 char numberStr[16];
3132 sprintf(numberStr, "%d", ResTable_map::TYPE_ANY);
3133 status_t err = table->addBag(SourcePos("<generated>", 0), package,
3134 attr16, key, String16(""),
3135 String16("^type"),
3136 String16(numberStr), NULL, NULL);
3137 if (err != NO_ERROR) {
3138 return err;
3139 }
3140 #endif
3141 }
3142 }
3143 return NO_ERROR;
3144 }
3145
3146 status_t ResourceTable::Entry::assignResourceIds(ResourceTable* table,
3147 const String16& package)
3148 {
3149 bool hasErrors = false;
3150
3151 if (mType == TYPE_BAG) {
3152 const char* errorMsg;
3153 const String16 style16("style");
3154 const String16 attr16("attr");
3155 const String16 id16("id");
3156 mParentId = 0;
3157 if (mParent.size() > 0) {
3158 mParentId = table->getResId(mParent, &style16, NULL, &errorMsg);
3159 if (mParentId == 0) {
3160 mPos.error("Error retrieving parent for item: %s '%s'.\n",
3161 errorMsg, String8(mParent).string());
3162 hasErrors = true;
3163 }
3164 }
3165 const size_t N = mBag.size();
3166 for (size_t i=0; i<N; i++) {
3167 const String16& key = mBag.keyAt(i);
3168 Item& it = mBag.editValueAt(i);
3169 it.bagKeyId = table->getResId(key,
3170 it.isId ? &id16 : &attr16, NULL, &errorMsg);
3171 //printf("Bag key of %s: #%08x\n", String8(key).string(), it.bagKeyId);
3172 if (it.bagKeyId == 0) {
3173 it.sourcePos.error("Error: %s: %s '%s'.\n", errorMsg,
3174 String8(it.isId ? id16 : attr16).string(),
3175 String8(key).string());
3176 hasErrors = true;
3177 }
3178 }
3179 }
3180 return hasErrors ? UNKNOWN_ERROR : NO_ERROR;
3181 }
3182
3183 status_t ResourceTable::Entry::prepareFlatten(StringPool* strings, ResourceTable* table)
3184 {
3185 if (mType == TYPE_ITEM) {
3186 Item& it = mItem;
3187 AccessorCookie ac(it.sourcePos, String8(mName), String8(it.value));
3188 if (!table->stringToValue(&it.parsedValue, strings,
3189 it.value, false, true, 0,
3190 &it.style, NULL, &ac, mItemFormat)) {
3191 return UNKNOWN_ERROR;
3192 }
3193 } else if (mType == TYPE_BAG) {
3194 const size_t N = mBag.size();
3195 for (size_t i=0; i<N; i++) {
3196 const String16& key = mBag.keyAt(i);
3197 Item& it = mBag.editValueAt(i);
3198 AccessorCookie ac(it.sourcePos, String8(key), String8(it.value));
3199 if (!table->stringToValue(&it.parsedValue, strings,
3200 it.value, false, true, it.bagKeyId,
3201 &it.style, NULL, &ac, it.format)) {
3202 return UNKNOWN_ERROR;
3203 }
3204 }
3205 } else {
3206 mPos.error("Error: entry %s is not a single item or a bag.\n",
3207 String8(mName).string());
3208 return UNKNOWN_ERROR;
3209 }
3210 return NO_ERROR;
3211 }
3212
3213 ssize_t ResourceTable::Entry::flatten(Bundle* bundle, const sp<AaptFile>& data, bool isPublic)
3214 {
3215 size_t amt = 0;
3216 ResTable_entry header;
3217 memset(&header, 0, sizeof(header));
3218 header.size = htods(sizeof(header));
3219 const type ty = this != NULL ? mType : TYPE_ITEM;
3220 if (this != NULL) {
3221 if (ty == TYPE_BAG) {
3222 header.flags |= htods(header.FLAG_COMPLEX);
3223 }
3224 if (isPublic) {
3225 header.flags |= htods(header.FLAG_PUBLIC);
3226 }
3227 header.key.index = htodl(mNameIndex);
3228 }
3229 if (ty != TYPE_BAG) {
3230 status_t err = data->writeData(&header, sizeof(header));
3231 if (err != NO_ERROR) {
3232 fprintf(stderr, "ERROR: out of memory creating ResTable_entry\n");
3233 return err;
3234 }
3235
3236 const Item& it = mItem;
3237 Res_value par;
3238 memset(&par, 0, sizeof(par));
3239 par.size = htods(it.parsedValue.size);
3240 par.dataType = it.parsedValue.dataType;
3241 par.res0 = it.parsedValue.res0;
3242 par.data = htodl(it.parsedValue.data);
3243 #if 0
3244 printf("Writing item (%s): type=%d, data=0x%x, res0=0x%x\n",
3245 String8(mName).string(), it.parsedValue.dataType,
3246 it.parsedValue.data, par.res0);
3247 #endif
3248 err = data->writeData(&par, it.parsedValue.size);
3249 if (err != NO_ERROR) {
3250 fprintf(stderr, "ERROR: out of memory creating Res_value\n");
3251 return err;
3252 }
3253 amt += it.parsedValue.size;
3254 } else {
3255 size_t N = mBag.size();
3256 size_t i;
3257 // Create correct ordering of items.
3258 KeyedVector<uint32_t, const Item*> items;
3259 for (i=0; i<N; i++) {
3260 const Item& it = mBag.valueAt(i);
3261 items.add(it.bagKeyId, &it);
3262 }
3263 N = items.size();
3264
3265 ResTable_map_entry mapHeader;
3266 memcpy(&mapHeader, &header, sizeof(header));
3267 mapHeader.size = htods(sizeof(mapHeader));
3268 mapHeader.parent.ident = htodl(mParentId);
3269 mapHeader.count = htodl(N);
3270 status_t err = data->writeData(&mapHeader, sizeof(mapHeader));
3271 if (err != NO_ERROR) {
3272 fprintf(stderr, "ERROR: out of memory creating ResTable_entry\n");
3273 return err;
3274 }
3275
3276 for (i=0; i<N; i++) {
3277 const Item& it = *items.valueAt(i);
3278 ResTable_map map;
3279 map.name.ident = htodl(it.bagKeyId);
3280 map.value.size = htods(it.parsedValue.size);
3281 map.value.dataType = it.parsedValue.dataType;
3282 map.value.res0 = it.parsedValue.res0;
3283 map.value.data = htodl(it.parsedValue.data);
3284 err = data->writeData(&map, sizeof(map));
3285 if (err != NO_ERROR) {
3286 fprintf(stderr, "ERROR: out of memory creating Res_value\n");
3287 return err;
3288 }
3289 amt += sizeof(map);
3290 }
3291 }
3292 return amt;
3293 }
3294
3295 void ResourceTable::ConfigList::appendComment(const String16& comment,
3296 bool onlyIfEmpty)
3297 {
3298 if (comment.size() <= 0) {
3299 return;
3300 }
3301 if (onlyIfEmpty && mComment.size() > 0) {
3302 return;
3303 }
3304 if (mComment.size() > 0) {
3305 mComment.append(String16("\n"));
3306 }
3307 mComment.append(comment);
3308 }
3309
3310 void ResourceTable::ConfigList::appendTypeComment(const String16& comment)
3311 {
3312 if (comment.size() <= 0) {
3313 return;
3314 }
3315 if (mTypeComment.size() > 0) {
3316 mTypeComment.append(String16("\n"));
3317 }
3318 mTypeComment.append(comment);
3319 }
3320
3321 status_t ResourceTable::Type::addPublic(const SourcePos& sourcePos,
3322 const String16& name,
3323 const uint32_t ident)
3324 {
3325 #if 0
3326 int32_t entryIdx = Res_GETENTRY(ident);
3327 if (entryIdx < 0) {
3328 sourcePos.error("Public resource %s/%s has an invalid 0 identifier (0x%08x).\n",
3329 String8(mName).string(), String8(name).string(), ident);
3330 return UNKNOWN_ERROR;
3331 }
3332 #endif
3333
3334 int32_t typeIdx = Res_GETTYPE(ident);
3335 if (typeIdx >= 0) {
3336 typeIdx++;
3337 if (mPublicIndex > 0 && mPublicIndex != typeIdx) {
3338 sourcePos.error("Public resource %s/%s has conflicting type codes for its"
3339 " public identifiers (0x%x vs 0x%x).\n",
3340 String8(mName).string(), String8(name).string(),
3341 mPublicIndex, typeIdx);
3342 return UNKNOWN_ERROR;
3343 }
3344 mPublicIndex = typeIdx;
3345 }
3346
3347 if (mFirstPublicSourcePos == NULL) {
3348 mFirstPublicSourcePos = new SourcePos(sourcePos);
3349 }
3350
3351 if (mPublic.indexOfKey(name) < 0) {
3352 mPublic.add(name, Public(sourcePos, String16(), ident));
3353 } else {
3354 Public& p = mPublic.editValueFor(name);
3355 if (p.ident != ident) {
3356 sourcePos.error("Public resource %s/%s has conflicting public identifiers"
3357 " (0x%08x vs 0x%08x).\n"
3358 "%s:%d: Originally defined here.\n",
3359 String8(mName).string(), String8(name).string(), p.ident, ident,
3360 p.sourcePos.file.string(), p.sourcePos.line);
3361 return UNKNOWN_ERROR;
3362 }
3363 }
3364
3365 return NO_ERROR;
3366 }
3367
3368 void ResourceTable::Type::canAddEntry(const String16& name)
3369 {
3370 mCanAddEntries.add(name);
3371 }
3372
3373 sp<ResourceTable::Entry> ResourceTable::Type::getEntry(const String16& entry,
3374 const SourcePos& sourcePos,
3375 const ResTable_config* config,
3376 bool doSetIndex,
3377 bool overlay,
3378 bool autoAddOverlay)
3379 {
3380 int pos = -1;
3381 sp<ConfigList> c = mConfigs.valueFor(entry);
3382 if (c == NULL) {
3383 if (overlay && !autoAddOverlay && mCanAddEntries.indexOf(entry) < 0) {
3384 sourcePos.error("Resource at %s appears in overlay but not"
3385 " in the base package; use <add-resource> to add.\n",
3386 String8(entry).string());
3387 return NULL;
3388 }
3389 c = new ConfigList(entry, sourcePos);
3390 mConfigs.add(entry, c);
3391 pos = (int)mOrderedConfigs.size();
3392 mOrderedConfigs.add(c);
3393 if (doSetIndex) {
3394 c->setEntryIndex(pos);
3395 }
3396 }
3397
3398 ConfigDescription cdesc;
3399 if (config) cdesc = *config;
3400
3401 sp<Entry> e = c->getEntries().valueFor(cdesc);
3402 if (e == NULL) {
3403 if (config != NULL) {
3404 NOISY(printf("New entry at %s:%d: imsi:%d/%d lang:%c%c cnt:%c%c "
3405 "orien:%d touch:%d density:%d key:%d inp:%d nav:%d w:%d h:%d\n",
3406 sourcePos.file.string(), sourcePos.line,
3407 config->mcc, config->mnc,
3408 config->language[0] ? config->language[0] : '-',
3409 config->language[1] ? config->language[1] : '-',
3410 config->country[0] ? config->country[0] : '-',
3411 config->country[1] ? config->country[1] : '-',
3412 config->orientation,
3413 config->touchscreen,
3414 config->density,
3415 config->keyboard,
3416 config->inputFlags,
3417 config->navigation,
3418 config->screenWidth,
3419 config->screenHeight));
3420 } else {
3421 NOISY(printf("New entry at %s:%d: NULL config\n",
3422 sourcePos.file.string(), sourcePos.line));
3423 }
3424 e = new Entry(entry, sourcePos);
3425 c->addEntry(cdesc, e);
3426 /*
3427 if (doSetIndex) {
3428 if (pos < 0) {
3429 for (pos=0; pos<(int)mOrderedConfigs.size(); pos++) {
3430 if (mOrderedConfigs[pos] == c) {
3431 break;
3432 }
3433 }
3434 if (pos >= (int)mOrderedConfigs.size()) {
3435 sourcePos.error("Internal error: config not found in mOrderedConfigs when adding entry");
3436 return NULL;
3437 }
3438 }
3439 e->setEntryIndex(pos);
3440 }
3441 */
3442 }
3443
3444 mUniqueConfigs.add(cdesc);
3445
3446 return e;
3447 }
3448
3449 status_t ResourceTable::Type::applyPublicEntryOrder()
3450 {
3451 size_t N = mOrderedConfigs.size();
3452 Vector<sp<ConfigList> > origOrder(mOrderedConfigs);
3453 bool hasError = false;
3454
3455 size_t i;
3456 for (i=0; i<N; i++) {
3457 mOrderedConfigs.replaceAt(NULL, i);
3458 }
3459
3460 const size_t NP = mPublic.size();
3461 //printf("Ordering %d configs from %d public defs\n", N, NP);
3462 size_t j;
3463 for (j=0; j<NP; j++) {
3464 const String16& name = mPublic.keyAt(j);
3465 const Public& p = mPublic.valueAt(j);
3466 int32_t idx = Res_GETENTRY(p.ident);
3467 //printf("Looking for entry \"%s\"/\"%s\" (0x%08x) in %d...\n",
3468 // String8(mName).string(), String8(name).string(), p.ident, N);
3469 bool found = false;
3470 for (i=0; i<N; i++) {
3471 sp<ConfigList> e = origOrder.itemAt(i);
3472 //printf("#%d: \"%s\"\n", i, String8(e->getName()).string());
3473 if (e->getName() == name) {
3474 if (idx >= (int32_t)mOrderedConfigs.size()) {
3475 p.sourcePos.error("Public entry identifier 0x%x entry index "
3476 "is larger than available symbols (index %d, total symbols %d).\n",
3477 p.ident, idx, mOrderedConfigs.size());
3478 hasError = true;
3479 } else if (mOrderedConfigs.itemAt(idx) == NULL) {
3480 e->setPublic(true);
3481 e->setPublicSourcePos(p.sourcePos);
3482 mOrderedConfigs.replaceAt(e, idx);
3483 origOrder.removeAt(i);
3484 N--;
3485 found = true;
3486 break;
3487 } else {
3488 sp<ConfigList> oe = mOrderedConfigs.itemAt(idx);
3489
3490 p.sourcePos.error("Multiple entry names declared for public entry"
3491 " identifier 0x%x in type %s (%s vs %s).\n"
3492 "%s:%d: Originally defined here.",
3493 idx+1, String8(mName).string(),
3494 String8(oe->getName()).string(),
3495 String8(name).string(),
3496 oe->getPublicSourcePos().file.string(),
3497 oe->getPublicSourcePos().line);
3498 hasError = true;
3499 }
3500 }
3501 }
3502
3503 if (!found) {
3504 p.sourcePos.error("Public symbol %s/%s declared here is not defined.",
3505 String8(mName).string(), String8(name).string());
3506 hasError = true;
3507 }
3508 }
3509
3510 //printf("Copying back in %d non-public configs, have %d\n", N, origOrder.size());
3511
3512 if (N != origOrder.size()) {
3513 printf("Internal error: remaining private symbol count mismatch\n");
3514 N = origOrder.size();
3515 }
3516
3517 j = 0;
3518 for (i=0; i<N; i++) {
3519 sp<ConfigList> e = origOrder.itemAt(i);
3520 // There will always be enough room for the remaining entries.
3521 while (mOrderedConfigs.itemAt(j) != NULL) {
3522 j++;
3523 }
3524 mOrderedConfigs.replaceAt(e, j);
3525 j++;
3526 }
3527
3528 return hasError ? UNKNOWN_ERROR : NO_ERROR;
3529 }
3530
3531 ResourceTable::Package::Package(const String16& name, ssize_t includedId)
3532 : mName(name), mIncludedId(includedId),
3533 mTypeStringsMapping(0xffffffff),
3534 mKeyStringsMapping(0xffffffff)
3535 {
3536 }
3537
3538 sp<ResourceTable::Type> ResourceTable::Package::getType(const String16& type,
3539 const SourcePos& sourcePos,
3540 bool doSetIndex)
3541 {
3542 sp<Type> t = mTypes.valueFor(type);
3543 if (t == NULL) {
3544 t = new Type(type, sourcePos);
3545 mTypes.add(type, t);
3546 mOrderedTypes.add(t);
3547 if (doSetIndex) {
3548 // For some reason the type's index is set to one plus the index
3549 // in the mOrderedTypes list, rather than just the index.
3550 t->setIndex(mOrderedTypes.size());
3551 }
3552 }
3553 return t;
3554 }
3555
3556 status_t ResourceTable::Package::setTypeStrings(const sp<AaptFile>& data)
3557 {
3558 mTypeStringsData = data;
3559 status_t err = setStrings(data, &mTypeStrings, &mTypeStringsMapping);
3560 if (err != NO_ERROR) {
3561 fprintf(stderr, "ERROR: Type string data is corrupt!\n");
3562 }
3563 return err;
3564 }
3565
3566 status_t ResourceTable::Package::setKeyStrings(const sp<AaptFile>& data)
3567 {
3568 mKeyStringsData = data;
3569 status_t err = setStrings(data, &mKeyStrings, &mKeyStringsMapping);
3570 if (err != NO_ERROR) {
3571 fprintf(stderr, "ERROR: Key string data is corrupt!\n");
3572 }
3573 return err;
3574 }
3575
3576 status_t ResourceTable::Package::setStrings(const sp<AaptFile>& data,
3577 ResStringPool* strings,
3578 DefaultKeyedVector<String16, uint32_t>* mappings)
3579 {
3580 if (data->getData() == NULL) {
3581 return UNKNOWN_ERROR;
3582 }
3583
3584 NOISY(aout << "Setting restable string pool: "
3585 << HexDump(data->getData(), data->getSize()) << endl);
3586
3587 status_t err = strings->setTo(data->getData(), data->getSize());
3588 if (err == NO_ERROR) {
3589 const size_t N = strings->size();
3590 for (size_t i=0; i<N; i++) {
3591 size_t len;
3592 mappings->add(String16(strings->stringAt(i, &len)), i);
3593 }
3594 }
3595 return err;
3596 }
3597
3598 status_t ResourceTable::Package::applyPublicTypeOrder()
3599 {
3600 size_t N = mOrderedTypes.size();
3601 Vector<sp<Type> > origOrder(mOrderedTypes);
3602
3603 size_t i;
3604 for (i=0; i<N; i++) {
3605 mOrderedTypes.replaceAt(NULL, i);
3606 }
3607
3608 for (i=0; i<N; i++) {
3609 sp<Type> t = origOrder.itemAt(i);
3610 int32_t idx = t->getPublicIndex();
3611 if (idx > 0) {
3612 idx--;
3613 while (idx >= (int32_t)mOrderedTypes.size()) {
3614 mOrderedTypes.add();
3615 }
3616 if (mOrderedTypes.itemAt(idx) != NULL) {
3617 sp<Type> ot = mOrderedTypes.itemAt(idx);
3618 t->getFirstPublicSourcePos().error("Multiple type names declared for public type"
3619 " identifier 0x%x (%s vs %s).\n"
3620 "%s:%d: Originally defined here.",
3621 idx, String8(ot->getName()).string(),
3622 String8(t->getName()).string(),
3623 ot->getFirstPublicSourcePos().file.string(),
3624 ot->getFirstPublicSourcePos().line);
3625 return UNKNOWN_ERROR;
3626 }
3627 mOrderedTypes.replaceAt(t, idx);
3628 origOrder.removeAt(i);
3629 i--;
3630 N--;
3631 }
3632 }
3633
3634 size_t j=0;
3635 for (i=0; i<N; i++) {
3636 sp<Type> t = origOrder.itemAt(i);
3637 // There will always be enough room for the remaining types.
3638 while (mOrderedTypes.itemAt(j) != NULL) {
3639 j++;
3640 }
3641 mOrderedTypes.replaceAt(t, j);
3642 }
3643
3644 return NO_ERROR;
3645 }
3646
3647 sp<ResourceTable::Package> ResourceTable::getPackage(const String16& package)
3648 {
3649 sp<Package> p = mPackages.valueFor(package);
3650 if (p == NULL) {
3651 if (mIsAppPackage) {
3652 if (mHaveAppPackage) {
3653 fprintf(stderr, "Adding multiple application package resources; only one is allowed.\n"
3654 "Use -x to create extended resources.\n");
3655 return NULL;
3656 }
3657 mHaveAppPackage = true;
3658 p = new Package(package, 127);
3659 } else {
3660 p = new Package(package, mNextPackageId);
3661 }
3662 //printf("*** NEW PACKAGE: \"%s\" id=%d\n",
3663 // String8(package).string(), p->getAssignedId());
3664 mPackages.add(package, p);
3665 mOrderedPackages.add(p);
3666 mNextPackageId++;
3667 }
3668 return p;
3669 }
3670
3671 sp<ResourceTable::Type> ResourceTable::getType(const String16& package,
3672 const String16& type,
3673 const SourcePos& sourcePos,
3674 bool doSetIndex)
3675 {
3676 sp<Package> p = getPackage(package);
3677 if (p == NULL) {
3678 return NULL;
3679 }
3680 return p->getType(type, sourcePos, doSetIndex);
3681 }
3682
3683 sp<ResourceTable::Entry> ResourceTable::getEntry(const String16& package,
3684 const String16& type,
3685 const String16& name,
3686 const SourcePos& sourcePos,
3687 bool overlay,
3688 const ResTable_config* config,
3689 bool doSetIndex)
3690 {
3691 sp<Type> t = getType(package, type, sourcePos, doSetIndex);
3692 if (t == NULL) {
3693 return NULL;
3694 }
3695 return t->getEntry(name, sourcePos, config, doSetIndex, overlay, mBundle->getAutoAddOverlay());
3696 }
3697
3698 sp<const ResourceTable::Entry> ResourceTable::getEntry(uint32_t resID,
3699 const ResTable_config* config) const
3700 {
3701 int pid = Res_GETPACKAGE(resID)+1;
3702 const size_t N = mOrderedPackages.size();
3703 size_t i;
3704 sp<Package> p;
3705 for (i=0; i<N; i++) {
3706 sp<Package> check = mOrderedPackages[i];
3707 if (check->getAssignedId() == pid) {
3708 p = check;
3709 break;
3710 }
3711
3712 }
3713 if (p == NULL) {
3714 fprintf(stderr, "warning: Package not found for resource #%08x\n", resID);
3715 return NULL;
3716 }
3717
3718 int tid = Res_GETTYPE(resID);
3719 if (tid < 0 || tid >= (int)p->getOrderedTypes().size()) {
3720 fprintf(stderr, "warning: Type not found for resource #%08x\n", resID);
3721 return NULL;
3722 }
3723 sp<Type> t = p->getOrderedTypes()[tid];
3724
3725 int eid = Res_GETENTRY(resID);
3726 if (eid < 0 || eid >= (int)t->getOrderedConfigs().size()) {
3727 fprintf(stderr, "warning: Entry not found for resource #%08x\n", resID);
3728 return NULL;
3729 }
3730
3731 sp<ConfigList> c = t->getOrderedConfigs()[eid];
3732 if (c == NULL) {
3733 fprintf(stderr, "warning: Entry not found for resource #%08x\n", resID);
3734 return NULL;
3735 }
3736
3737 ConfigDescription cdesc;
3738 if (config) cdesc = *config;
3739 sp<Entry> e = c->getEntries().valueFor(cdesc);
3740 if (c == NULL) {
3741 fprintf(stderr, "warning: Entry configuration not found for resource #%08x\n", resID);
3742 return NULL;
3743 }
3744
3745 return e;
3746 }
3747
3748 const ResourceTable::Item* ResourceTable::getItem(uint32_t resID, uint32_t attrID) const
3749 {
3750 sp<const Entry> e = getEntry(resID);
3751 if (e == NULL) {
3752 return NULL;
3753 }
3754
3755 const size_t N = e->getBag().size();
3756 for (size_t i=0; i<N; i++) {
3757 const Item& it = e->getBag().valueAt(i);
3758 if (it.bagKeyId == 0) {
3759 fprintf(stderr, "warning: ID not yet assigned to '%s' in bag '%s'\n",
3760 String8(e->getName()).string(),
3761 String8(e->getBag().keyAt(i)).string());
3762 }
3763 if (it.bagKeyId == attrID) {
3764 return &it;
3765 }
3766 }
3767
3768 return NULL;
3769 }
3770
3771 bool ResourceTable::getItemValue(
3772 uint32_t resID, uint32_t attrID, Res_value* outValue)
3773 {
3774 const Item* item = getItem(resID, attrID);
3775
3776 bool res = false;
3777 if (item != NULL) {
3778 if (item->evaluating) {
3779 sp<const Entry> e = getEntry(resID);
3780 const size_t N = e->getBag().size();
3781 size_t i;
3782 for (i=0; i<N; i++) {
3783 if (&e->getBag().valueAt(i) == item) {
3784 break;
3785 }
3786 }
3787 fprintf(stderr, "warning: Circular reference detected in key '%s' of bag '%s'\n",
3788 String8(e->getName()).string(),
3789 String8(e->getBag().keyAt(i)).string());
3790 return false;
3791 }
3792 item->evaluating = true;
3793 res = stringToValue(outValue, NULL, item->value, false, false, item->bagKeyId);
3794 NOISY(
3795 if (res) {
3796 printf("getItemValue of #%08x[#%08x] (%s): type=#%08x, data=#%08x\n",
3797 resID, attrID, String8(getEntry(resID)->getName()).string(),
3798 outValue->dataType, outValue->data);
3799 } else {
3800 printf("getItemValue of #%08x[#%08x]: failed\n",
3801 resID, attrID);
3802 }
3803 );
3804 item->evaluating = false;
3805 }
3806 return res;
3807 }
3808
3809 #define DEBUG_LANG(x) printf("lang=%c%c cnt=%c%c ", \
3810 (x).language[0] ? (x).language[0] : '-', \
3811 (x).language[1] ? (x).language[1] : '-', \
3812 (x).country[0] ? (x).country[0] : '-', \
3813 (x).country[1] ? (x).country[1] : '-');
3814
3815 status_t ResourceConfigReferences::add(uint32_t id, const ResTable_config& config)
3816 {
3817 ssize_t index = mRoots.indexOfKey(id);
3818 if (index < 0) {
3819 index = mRoots.add(id, Vector<const ResTable_config*>());
3820 }
3821 Vector<const ResTable_config*>& configRoots = mRoots.editValueFor(id);
3822
3823 if (!configRoots.isEmpty()) {
3824 ssize_t NR = configRoots.size();
3825 for (int ri=0; ri<NR; ++ri) {
3826 const ResTable_config* current = configRoots[ri];
3827
3828 if (config.match(*current)) {
3829 // We already have something more generic than our incoming string.
3830 NOISY_REF(
3831 printf(" ConfigRef %d: ignoring ", id);
3832 DEBUG_LANG(config)
3833 printf("\n");
3834 )
3835 return NO_ERROR;
3836 } else if (current->match(config)) {
3837 // more generic
3838 NOISY_REF(
3839 printf(" ConfigRef %d: remove ", id);
3840 DEBUG_LANG(current)
3841 printf("\n");
3842 )
3843 configRoots.removeItemsAt(ri);
3844 --ri; --NR;
3845 }
3846 }
3847 }
3848 NOISY_REF(
3849 printf(" ConfigRef %d: add ", id);
3850 DEBUG_LANG(config)
3851 printf("\n");
3852 )
3853 ResTable_config *configCopy = (ResTable_config*)malloc(sizeof(ResTable_config));
3854 memcpy(configCopy, &config, sizeof(ResTable_config));
3855 configRoots.add(configCopy);
3856
3857 return NO_ERROR;
3858 }
3859
3860 void ResourceConfigReferences::dump()
3861 {
3862 printf("ResourceConfigReferences\n");
3863 const ssize_t NR = mRoots.size();
3864 for (int ri=0; ri<NR; ++ri) {
3865 const Vector<const ResTable_config*>& configRoots = mRoots.valueAt(ri);
3866 printf(" String %d\n", mRoots.keyAt(ri));
3867 const ssize_t NC = configRoots.size();
3868 for (int ci=0; ci<NC; ++ci) {
3869 printf(" ");
3870 DEBUG_LANG(*configRoots[ci])
3871 printf("\n");
3872 }
3873 }
3874 }
3875
3876 bool ResourceConfigReferences::isRoot(uint32_t id, const ResTable_config& config)
3877 {
3878 const Vector<const ResTable_config*>& configRoots = mRoots.editValueFor(id);
3879 const ssize_t NR = configRoots.size();
3880 for (int ri = 0; ri<NR; ++ri) {
3881 if (configRoots[ri]->match(config)) {
3882 return true;
3883 }
3884 }
3885 return false;
3886 }
3887
3888 ResourceConfigReferences::~ResourceConfigReferences()
3889 {
3890 const ssize_t NR = mRoots.size();
3891 for (int ri=0; ri<NR; ++ri) {
3892 Vector<const ResTable_config*> configRoots = mRoots.editValueAt(ri);
3893 const ssize_t NC = configRoots.size();
3894 for (int ci=0; ci<NC; ++ci) {
3895 ResTable_config* config = const_cast<ResTable_config*>(configRoots[ci]);
3896 free(config);
3897 }
3898 }
3899 }