2 // Copyright 2006 The Android Open Source Project
4 // Build resource files from raw assets.
7 #include "ResourceTable.h"
11 #include <utils/ByteOrder.h>
12 #include <utils/ResourceTypes.h>
17 status_t
compileXmlFile(const sp
<AaptAssets
>& assets
,
18 const sp
<AaptFile
>& target
,
22 sp
<XMLNode
> root
= XMLNode::parse(target
);
27 return compileXmlFile(assets
, root
, target
, table
, options
);
30 status_t
compileXmlFile(const sp
<AaptAssets
>& assets
,
31 const sp
<XMLNode
>& root
,
32 const sp
<AaptFile
>& target
,
36 if ((options
&XML_COMPILE_STRIP_WHITESPACE
) != 0) {
37 root
->removeWhitespace(true, NULL
);
38 } else if ((options
&XML_COMPILE_COMPACT_WHITESPACE
) != 0) {
39 root
->removeWhitespace(false, NULL
);
42 if ((options
&XML_COMPILE_UTF8
) != 0) {
46 bool hasErrors
= false;
48 if ((options
&XML_COMPILE_ASSIGN_ATTRIBUTE_IDS
) != 0) {
49 status_t err
= root
->assignResourceIds(assets
, table
);
50 if (err
!= NO_ERROR
) {
55 status_t err
= root
->parseValues(assets
, table
);
56 if (err
!= NO_ERROR
) {
64 NOISY(printf("Input XML Resource:\n"));
66 err
= root
->flatten(target
,
67 (options
&XML_COMPILE_STRIP_COMMENTS
) != 0,
68 (options
&XML_COMPILE_STRIP_RAW_VALUES
) != 0);
69 if (err
!= NO_ERROR
) {
73 NOISY(printf("Output XML Resource:\n"));
74 NOISY(ResXMLTree tree
;
75 tree
.setTo(target
->getData(), target
->getSize());
76 printXMLBlock(&tree
));
78 target
->setCompressionMethod(ZipEntry::kCompressDeflated
);
91 const char* description
;
94 static const char16_t referenceArray
[] =
95 { 'r', 'e', 'f', 'e', 'r', 'e', 'n', 'c', 'e' };
96 static const char16_t stringArray
[] =
97 { 's', 't', 'r', 'i', 'n', 'g' };
98 static const char16_t integerArray
[] =
99 { 'i', 'n', 't', 'e', 'g', 'e', 'r' };
100 static const char16_t booleanArray
[] =
101 { 'b', 'o', 'o', 'l', 'e', 'a', 'n' };
102 static const char16_t colorArray
[] =
103 { 'c', 'o', 'l', 'o', 'r' };
104 static const char16_t floatArray
[] =
105 { 'f', 'l', 'o', 'a', 't' };
106 static const char16_t dimensionArray
[] =
107 { 'd', 'i', 'm', 'e', 'n', 's', 'i', 'o', 'n' };
108 static const char16_t fractionArray
[] =
109 { 'f', 'r', 'a', 'c', 't', 'i', 'o', 'n' };
110 static const char16_t enumArray
[] =
111 { 'e', 'n', 'u', 'm' };
112 static const char16_t flagsArray
[] =
113 { 'f', 'l', 'a', 'g', 's' };
115 static const flag_entry gFormatFlags
[] = {
116 { referenceArray
, sizeof(referenceArray
)/2, ResTable_map::TYPE_REFERENCE
,
117 "a reference to another resource, in the form \"<code>@[+][<i>package</i>:]<i>type</i>:<i>name</i></code>\"\n"
118 "or to a theme attribute in the form \"<code>?[<i>package</i>:][<i>type</i>:]<i>name</i></code>\"."},
119 { stringArray
, sizeof(stringArray
)/2, ResTable_map::TYPE_STRING
,
120 "a string value, using '\\\\;' to escape characters such as '\\\\n' or '\\\\uxxxx' for a unicode character." },
121 { integerArray
, sizeof(integerArray
)/2, ResTable_map::TYPE_INTEGER
,
122 "an integer value, such as \"<code>100</code>\"." },
123 { booleanArray
, sizeof(booleanArray
)/2, ResTable_map::TYPE_BOOLEAN
,
124 "a boolean value, either \"<code>true</code>\" or \"<code>false</code>\"." },
125 { colorArray
, sizeof(colorArray
)/2, ResTable_map::TYPE_COLOR
,
126 "a color value, in the form of \"<code>#<i>rgb</i></code>\", \"<code>#<i>argb</i></code>\",\n"
127 "\"<code>#<i>rrggbb</i></code>\", or \"<code>#<i>aarrggbb</i></code>\"." },
128 { floatArray
, sizeof(floatArray
)/2, ResTable_map::TYPE_FLOAT
,
129 "a floating point value, such as \"<code>1.2</code>\"."},
130 { dimensionArray
, sizeof(dimensionArray
)/2, ResTable_map::TYPE_DIMENSION
,
131 "a dimension value, which is a floating point number appended with a unit such as \"<code>14.5sp</code>\".\n"
132 "Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size),\n"
133 "in (inches), mm (millimeters)." },
134 { fractionArray
, sizeof(fractionArray
)/2, ResTable_map::TYPE_FRACTION
,
135 "a fractional value, which is a floating point number appended with either % or %p, such as \"<code>14.5%</code>\".\n"
136 "The % suffix always means a percentage of the base size; the optional %p suffix provides a size relative to\n"
137 "some parent container." },
138 { enumArray
, sizeof(enumArray
)/2, ResTable_map::TYPE_ENUM
, NULL
},
139 { flagsArray
, sizeof(flagsArray
)/2, ResTable_map::TYPE_FLAGS
, NULL
},
143 static const char16_t suggestedArray
[] = { 's', 'u', 'g', 'g', 'e', 's', 't', 'e', 'd' };
145 static const flag_entry l10nRequiredFlags
[] = {
146 { suggestedArray
, sizeof(suggestedArray
)/2, ResTable_map::L10N_SUGGESTED
, NULL
},
150 static const char16_t nulStr
[] = { 0 };
152 static uint32_t parse_flags(const char16_t* str
, size_t len
,
153 const flag_entry
* flags
, bool* outError
= NULL
)
155 while (len
> 0 && isspace(*str
)) {
159 while (len
> 0 && isspace(str
[len
-1])) {
163 const char16_t* const end
= str
+ len
;
167 const char16_t* div
= str
;
168 while (div
< end
&& *div
!= '|') {
172 const flag_entry
* cur
= flags
;
174 if (strzcmp16(cur
->name
, cur
->nameLen
, str
, div
-str
) == 0) {
182 if (outError
) *outError
= true;
186 str
= div
< end
? div
+1 : div
;
189 if (outError
) *outError
= false;
193 static String16
mayOrMust(int type
, int flags
)
195 if ((type
&(~flags
)) == 0) {
196 return String16("<p>Must");
199 return String16("<p>May");
202 static void appendTypeInfo(ResourceTable
* outTable
, const String16
& pkg
,
203 const String16
& typeName
, const String16
& ident
, int type
,
204 const flag_entry
* flags
)
206 bool hadType
= false;
207 while (flags
->name
) {
208 if ((type
&flags
->value
) != 0 && flags
->description
!= NULL
) {
209 String16
fullMsg(mayOrMust(type
, flags
->value
));
210 fullMsg
.append(String16(" be "));
211 fullMsg
.append(String16(flags
->description
));
212 outTable
->appendTypeComment(pkg
, typeName
, ident
, fullMsg
);
217 if (hadType
&& (type
&ResTable_map::TYPE_REFERENCE
) == 0) {
218 outTable
->appendTypeComment(pkg
, typeName
, ident
,
219 String16("<p>This may also be a reference to a resource (in the form\n"
220 "\"<code>@[<i>package</i>:]<i>type</i>:<i>name</i></code>\") or\n"
221 "theme attribute (in the form\n"
222 "\"<code>?[<i>package</i>:][<i>type</i>:]<i>name</i></code>\")\n"
223 "containing a value of this type."));
227 struct PendingAttribute
229 const String16 myPackage
;
230 const SourcePos sourcePos
;
231 const bool appendComment
;
238 PendingAttribute(String16 _package
, const sp
<AaptFile
>& in
,
239 ResXMLTree
& block
, bool _appendComment
)
240 : myPackage(_package
)
241 , sourcePos(in
->getPrintableSource(), block
.getLineNumber())
242 , appendComment(_appendComment
)
243 , type(ResTable_map::TYPE_ANY
)
249 status_t
createIfNeeded(ResourceTable
* outTable
)
251 if (added
|| hasErrors
) {
256 String16
attr16("attr");
258 if (outTable
->hasBagOrEntry(myPackage
, attr16
, ident
)) {
259 sourcePos
.error("Attribute \"%s\" has already been defined\n",
260 String8(ident
).string());
262 return UNKNOWN_ERROR
;
266 sprintf(numberStr
, "%d", type
);
267 status_t err
= outTable
->addBag(sourcePos
, myPackage
,
268 attr16
, ident
, String16(""),
270 String16(numberStr
), NULL
, NULL
);
271 if (err
!= NO_ERROR
) {
275 outTable
->appendComment(myPackage
, attr16
, ident
, comment
, appendComment
);
276 //printf("Attribute %s comment: %s\n", String8(ident).string(),
277 // String8(comment).string());
282 static status_t
compileAttribute(const sp
<AaptFile
>& in
,
284 const String16
& myPackage
,
285 ResourceTable
* outTable
,
286 String16
* outIdent
= NULL
,
287 bool inStyleable
= false)
289 PendingAttribute
attr(myPackage
, in
, block
, inStyleable
);
291 const String16
attr16("attr");
292 const String16
id16("id");
294 // Attribute type constants.
295 const String16
enum16("enum");
296 const String16
flag16("flag");
298 ResXMLTree::event_code_t code
;
302 ssize_t identIdx
= block
.indexOfAttribute(NULL
, "name");
304 attr
.ident
= String16(block
.getAttributeStringValue(identIdx
, &len
));
306 *outIdent
= attr
.ident
;
309 attr
.sourcePos
.error("A 'name' attribute is required for <attr>\n");
310 attr
.hasErrors
= true;
313 attr
.comment
= String16(
314 block
.getComment(&len
) ? block
.getComment(&len
) : nulStr
);
316 ssize_t typeIdx
= block
.indexOfAttribute(NULL
, "format");
318 String16 typeStr
= String16(block
.getAttributeStringValue(typeIdx
, &len
));
319 attr
.type
= parse_flags(typeStr
.string(), typeStr
.size(), gFormatFlags
);
320 if (attr
.type
== 0) {
321 attr
.sourcePos
.error("Tag <attr> 'format' attribute value \"%s\" not valid\n",
322 String8(typeStr
).string());
323 attr
.hasErrors
= true;
325 attr
.createIfNeeded(outTable
);
326 } else if (!inStyleable
) {
327 // Attribute definitions outside of styleables always define the
328 // attribute as a generic value.
329 attr
.createIfNeeded(outTable
);
332 //printf("Attribute %s: type=0x%08x\n", String8(attr.ident).string(), attr.type);
334 ssize_t minIdx
= block
.indexOfAttribute(NULL
, "min");
336 String16 val
= String16(block
.getAttributeStringValue(minIdx
, &len
));
337 if (!ResTable::stringToInt(val
.string(), val
.size(), NULL
)) {
338 attr
.sourcePos
.error("Tag <attr> 'min' attribute must be a number, not \"%s\"\n",
339 String8(val
).string());
340 attr
.hasErrors
= true;
342 attr
.createIfNeeded(outTable
);
343 if (!attr
.hasErrors
) {
344 err
= outTable
->addBag(attr
.sourcePos
, myPackage
, attr16
, attr
.ident
,
345 String16(""), String16("^min"), String16(val
), NULL
, NULL
);
346 if (err
!= NO_ERROR
) {
347 attr
.hasErrors
= true;
352 ssize_t maxIdx
= block
.indexOfAttribute(NULL
, "max");
354 String16 val
= String16(block
.getAttributeStringValue(maxIdx
, &len
));
355 if (!ResTable::stringToInt(val
.string(), val
.size(), NULL
)) {
356 attr
.sourcePos
.error("Tag <attr> 'max' attribute must be a number, not \"%s\"\n",
357 String8(val
).string());
358 attr
.hasErrors
= true;
360 attr
.createIfNeeded(outTable
);
361 if (!attr
.hasErrors
) {
362 err
= outTable
->addBag(attr
.sourcePos
, myPackage
, attr16
, attr
.ident
,
363 String16(""), String16("^max"), String16(val
), NULL
, NULL
);
364 attr
.hasErrors
= true;
368 if ((minIdx
>= 0 || maxIdx
>= 0) && (attr
.type
&ResTable_map::TYPE_INTEGER
) == 0) {
369 attr
.sourcePos
.error("Tag <attr> must have format=integer attribute if using max or min\n");
370 attr
.hasErrors
= true;
373 ssize_t l10nIdx
= block
.indexOfAttribute(NULL
, "localization");
375 const uint16_t* str
= block
.getAttributeStringValue(l10nIdx
, &len
);
377 uint32_t l10n_required
= parse_flags(str
, len
, l10nRequiredFlags
, &error
);
379 attr
.sourcePos
.error("Tag <attr> 'localization' attribute value \"%s\" not valid\n",
380 String8(str
).string());
381 attr
.hasErrors
= true;
383 attr
.createIfNeeded(outTable
);
384 if (!attr
.hasErrors
) {
386 sprintf(buf
, "%d", l10n_required
);
387 err
= outTable
->addBag(attr
.sourcePos
, myPackage
, attr16
, attr
.ident
,
388 String16(""), String16("^l10n"), String16(buf
), NULL
, NULL
);
389 if (err
!= NO_ERROR
) {
390 attr
.hasErrors
= true;
395 String16 enumOrFlagsComment
;
397 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
398 if (code
== ResXMLTree::START_TAG
) {
399 uint32_t localType
= 0;
400 if (strcmp16(block
.getElementName(&len
), enum16
.string()) == 0) {
401 localType
= ResTable_map::TYPE_ENUM
;
402 } else if (strcmp16(block
.getElementName(&len
), flag16
.string()) == 0) {
403 localType
= ResTable_map::TYPE_FLAGS
;
405 SourcePos(in
->getPrintableSource(), block
.getLineNumber())
406 .error("Tag <%s> can not appear inside <attr>, only <enum> or <flag>\n",
407 String8(block
.getElementName(&len
)).string());
408 return UNKNOWN_ERROR
;
411 attr
.createIfNeeded(outTable
);
413 if (attr
.type
== ResTable_map::TYPE_ANY
) {
414 // No type was explicitly stated, so supplying enum tags
415 // implicitly creates an enum or flag.
419 if ((attr
.type
&(ResTable_map::TYPE_ENUM
|ResTable_map::TYPE_FLAGS
)) == 0) {
420 // Wasn't originally specified as an enum, so update its type.
421 attr
.type
|= localType
;
422 if (!attr
.hasErrors
) {
424 sprintf(numberStr
, "%d", attr
.type
);
425 err
= outTable
->addBag(SourcePos(in
->getPrintableSource(), block
.getLineNumber()),
426 myPackage
, attr16
, attr
.ident
, String16(""),
427 String16("^type"), String16(numberStr
), NULL
, NULL
, true);
428 if (err
!= NO_ERROR
) {
429 attr
.hasErrors
= true;
432 } else if ((uint32_t)(attr
.type
&(ResTable_map::TYPE_ENUM
|ResTable_map::TYPE_FLAGS
)) != localType
) {
433 if (localType
== ResTable_map::TYPE_ENUM
) {
434 SourcePos(in
->getPrintableSource(), block
.getLineNumber())
435 .error("<enum> attribute can not be used inside a flags format\n");
436 attr
.hasErrors
= true;
438 SourcePos(in
->getPrintableSource(), block
.getLineNumber())
439 .error("<flag> attribute can not be used inside a enum format\n");
440 attr
.hasErrors
= true;
445 ssize_t itemIdentIdx
= block
.indexOfAttribute(NULL
, "name");
446 if (itemIdentIdx
>= 0) {
447 itemIdent
= String16(block
.getAttributeStringValue(itemIdentIdx
, &len
));
449 SourcePos(in
->getPrintableSource(), block
.getLineNumber())
450 .error("A 'name' attribute is required for <enum> or <flag>\n");
451 attr
.hasErrors
= true;
455 ssize_t valueIdx
= block
.indexOfAttribute(NULL
, "value");
457 value
= String16(block
.getAttributeStringValue(valueIdx
, &len
));
459 SourcePos(in
->getPrintableSource(), block
.getLineNumber())
460 .error("A 'value' attribute is required for <enum> or <flag>\n");
461 attr
.hasErrors
= true;
463 if (!attr
.hasErrors
&& !ResTable::stringToInt(value
.string(), value
.size(), NULL
)) {
464 SourcePos(in
->getPrintableSource(), block
.getLineNumber())
465 .error("Tag <enum> or <flag> 'value' attribute must be a number,"
467 String8(value
).string());
468 attr
.hasErrors
= true;
471 // Make sure an id is defined for this enum/flag identifier...
472 if (!attr
.hasErrors
&& !outTable
->hasBagOrEntry(itemIdent
, &id16
, &myPackage
)) {
473 err
= outTable
->startBag(SourcePos(in
->getPrintableSource(), block
.getLineNumber()),
474 myPackage
, id16
, itemIdent
, String16(), NULL
);
475 if (err
!= NO_ERROR
) {
476 attr
.hasErrors
= true;
480 if (!attr
.hasErrors
) {
481 if (enumOrFlagsComment
.size() == 0) {
482 enumOrFlagsComment
.append(mayOrMust(attr
.type
,
483 ResTable_map::TYPE_ENUM
|ResTable_map::TYPE_FLAGS
));
484 enumOrFlagsComment
.append((attr
.type
&ResTable_map::TYPE_ENUM
)
485 ? String16(" be one of the following constant values.")
486 : String16(" be one or more (separated by '|') of the following constant values."));
487 enumOrFlagsComment
.append(String16("</p>\n<table>\n"
488 "<colgroup align=\"left\" />\n"
489 "<colgroup align=\"left\" />\n"
490 "<colgroup align=\"left\" />\n"
491 "<tr><th>Constant</th><th>Value</th><th>Description</th></tr>"));
494 enumOrFlagsComment
.append(String16("\n<tr><td><code>"));
495 enumOrFlagsComment
.append(itemIdent
);
496 enumOrFlagsComment
.append(String16("</code></td><td>"));
497 enumOrFlagsComment
.append(value
);
498 enumOrFlagsComment
.append(String16("</td><td>"));
499 if (block
.getComment(&len
)) {
500 enumOrFlagsComment
.append(String16(block
.getComment(&len
)));
502 enumOrFlagsComment
.append(String16("</td></tr>"));
504 err
= outTable
->addBag(SourcePos(in
->getPrintableSource(), block
.getLineNumber()),
506 attr16
, attr
.ident
, String16(""),
507 itemIdent
, value
, NULL
, NULL
, false, true);
508 if (err
!= NO_ERROR
) {
509 attr
.hasErrors
= true;
512 } else if (code
== ResXMLTree::END_TAG
) {
513 if (strcmp16(block
.getElementName(&len
), attr16
.string()) == 0) {
516 if ((attr
.type
&ResTable_map::TYPE_ENUM
) != 0) {
517 if (strcmp16(block
.getElementName(&len
), enum16
.string()) != 0) {
518 SourcePos(in
->getPrintableSource(), block
.getLineNumber())
519 .error("Found tag </%s> where </enum> is expected\n",
520 String8(block
.getElementName(&len
)).string());
521 return UNKNOWN_ERROR
;
524 if (strcmp16(block
.getElementName(&len
), flag16
.string()) != 0) {
525 SourcePos(in
->getPrintableSource(), block
.getLineNumber())
526 .error("Found tag </%s> where </flag> is expected\n",
527 String8(block
.getElementName(&len
)).string());
528 return UNKNOWN_ERROR
;
534 if (!attr
.hasErrors
&& attr
.added
) {
535 appendTypeInfo(outTable
, myPackage
, attr16
, attr
.ident
, attr
.type
, gFormatFlags
);
538 if (!attr
.hasErrors
&& enumOrFlagsComment
.size() > 0) {
539 enumOrFlagsComment
.append(String16("\n</table>"));
540 outTable
->appendTypeComment(myPackage
, attr16
, attr
.ident
, enumOrFlagsComment
);
547 bool localeIsDefined(const ResTable_config
& config
)
549 return config
.locale
== 0;
552 status_t
parseAndAddBag(Bundle
* bundle
,
553 const sp
<AaptFile
>& in
,
555 const ResTable_config
& config
,
556 const String16
& myPackage
,
557 const String16
& curType
,
558 const String16
& ident
,
559 const String16
& parentIdent
,
560 const String16
& itemIdent
,
563 const bool overwrite
,
564 ResourceTable
* outTable
)
567 const String16
item16("item");
570 Vector
<StringPool::entry_style_span
> spans
;
571 err
= parseStyledString(bundle
, in
->getPrintableSource().string(),
572 block
, item16
, &str
, &spans
,
574 if (err
!= NO_ERROR
) {
578 NOISY(printf("Adding resource bag entry l=%c%c c=%c%c orien=%d d=%d "
579 " pid=%s, bag=%s, id=%s: %s\n",
580 config
.language
[0], config
.language
[1],
581 config
.country
[0], config
.country
[1],
582 config
.orientation
, config
.density
,
583 String8(parentIdent
).string(),
584 String8(ident
).string(),
585 String8(itemIdent
).string(),
586 String8(str
).string()));
588 err
= outTable
->addBag(SourcePos(in
->getPrintableSource(), block
->getLineNumber()),
589 myPackage
, curType
, ident
, parentIdent
, itemIdent
, str
,
590 &spans
, &config
, overwrite
, false, curFormat
);
595 status_t
parseAndAddEntry(Bundle
* bundle
,
596 const sp
<AaptFile
>& in
,
598 const ResTable_config
& config
,
599 const String16
& myPackage
,
600 const String16
& curType
,
601 const String16
& ident
,
602 const String16
& curTag
,
606 const bool overwrite
,
607 ResourceTable
* outTable
)
612 Vector
<StringPool::entry_style_span
> spans
;
613 err
= parseStyledString(bundle
, in
->getPrintableSource().string(), block
,
614 curTag
, &str
, curIsStyled
? &spans
: NULL
,
617 if (err
< NO_ERROR
) {
621 NOISY(printf("Adding resource entry l=%c%c c=%c%c orien=%d d=%d id=%s: %s\n",
622 config
.language
[0], config
.language
[1],
623 config
.country
[0], config
.country
[1],
624 config
.orientation
, config
.density
,
625 String8(ident
).string(), String8(str
).string()));
627 err
= outTable
->addEntry(SourcePos(in
->getPrintableSource(), block
->getLineNumber()),
628 myPackage
, curType
, ident
, str
, &spans
, &config
,
629 false, curFormat
, overwrite
);
634 status_t
compileResourceFile(Bundle
* bundle
,
635 const sp
<AaptAssets
>& assets
,
636 const sp
<AaptFile
>& in
,
637 const ResTable_config
& defParams
,
638 const bool overwrite
,
639 ResourceTable
* outTable
)
642 status_t err
= parseXMLResource(in
, &block
, false, true);
643 if (err
!= NO_ERROR
) {
648 const String16
resources16("resources");
650 // Identifier declaration tags.
651 const String16
declare_styleable16("declare-styleable");
652 const String16
attr16("attr");
654 // Data creation organizational tags.
655 const String16
string16("string");
656 const String16
drawable16("drawable");
657 const String16
color16("color");
658 const String16
bool16("bool");
659 const String16
integer16("integer");
660 const String16
dimen16("dimen");
661 const String16
fraction16("fraction");
662 const String16
style16("style");
663 const String16
plurals16("plurals");
664 const String16
array16("array");
665 const String16
string_array16("string-array");
666 const String16
integer_array16("integer-array");
667 const String16
public16("public");
668 const String16
public_padding16("public-padding");
669 const String16
private_symbols16("private-symbols");
670 const String16
add_resource16("add-resource");
671 const String16
skip16("skip");
672 const String16
eat_comment16("eat-comment");
674 // Data creation tags.
675 const String16
bag16("bag");
676 const String16
item16("item");
678 // Attribute type constants.
679 const String16
enum16("enum");
682 const String16
other16("other");
683 const String16
quantityOther16("^other");
684 const String16
zero16("zero");
685 const String16
quantityZero16("^zero");
686 const String16
one16("one");
687 const String16
quantityOne16("^one");
688 const String16
two16("two");
689 const String16
quantityTwo16("^two");
690 const String16
few16("few");
691 const String16
quantityFew16("^few");
692 const String16
many16("many");
693 const String16
quantityMany16("^many");
695 // useful attribute names and special values
696 const String16
name16("name");
697 const String16
translatable16("translatable");
698 const String16
false16("false");
700 const String16
myPackage(assets
->getPackage());
702 bool hasErrors
= false;
704 DefaultKeyedVector
<String16
, uint32_t> nextPublicId(0);
706 ResXMLTree::event_code_t code
;
709 } while (code
== ResXMLTree::START_NAMESPACE
);
712 if (code
!= ResXMLTree::START_TAG
) {
713 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
714 "No start tag found\n");
715 return UNKNOWN_ERROR
;
717 if (strcmp16(block
.getElementName(&len
), resources16
.string()) != 0) {
718 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
719 "Invalid start tag %s\n", String8(block
.getElementName(&len
)).string());
720 return UNKNOWN_ERROR
;
723 ResTable_config
curParams(defParams
);
725 ResTable_config
pseudoParams(curParams
);
726 pseudoParams
.language
[0] = 'z';
727 pseudoParams
.language
[1] = 'z';
728 pseudoParams
.country
[0] = 'Z';
729 pseudoParams
.country
[1] = 'Z';
731 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
732 if (code
== ResXMLTree::START_TAG
) {
733 const String16
* curTag
= NULL
;
735 int32_t curFormat
= ResTable_map::TYPE_ANY
;
736 bool curIsBag
= false;
737 bool curIsBagReplaceOnOverwrite
= false;
738 bool curIsStyled
= false;
739 bool curIsPseudolocalizable
= false;
740 bool localHasErrors
= false;
742 if (strcmp16(block
.getElementName(&len
), skip16
.string()) == 0) {
743 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
744 && code
!= ResXMLTree::BAD_DOCUMENT
) {
745 if (code
== ResXMLTree::END_TAG
) {
746 if (strcmp16(block
.getElementName(&len
), skip16
.string()) == 0) {
753 } else if (strcmp16(block
.getElementName(&len
), eat_comment16
.string()) == 0) {
754 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
755 && code
!= ResXMLTree::BAD_DOCUMENT
) {
756 if (code
== ResXMLTree::END_TAG
) {
757 if (strcmp16(block
.getElementName(&len
), eat_comment16
.string()) == 0) {
764 } else if (strcmp16(block
.getElementName(&len
), public16
.string()) == 0) {
765 SourcePos
srcPos(in
->getPrintableSource(), block
.getLineNumber());
768 ssize_t typeIdx
= block
.indexOfAttribute(NULL
, "type");
770 srcPos
.error("A 'type' attribute is required for <public>\n");
771 hasErrors
= localHasErrors
= true;
773 type
= String16(block
.getAttributeStringValue(typeIdx
, &len
));
776 ssize_t nameIdx
= block
.indexOfAttribute(NULL
, "name");
778 srcPos
.error("A 'name' attribute is required for <public>\n");
779 hasErrors
= localHasErrors
= true;
781 name
= String16(block
.getAttributeStringValue(nameIdx
, &len
));
784 ssize_t identIdx
= block
.indexOfAttribute(NULL
, "id");
786 const char16_t* identStr
= block
.getAttributeStringValue(identIdx
, &len
);
787 Res_value identValue
;
788 if (!ResTable::stringToInt(identStr
, len
, &identValue
)) {
789 srcPos
.error("Given 'id' attribute is not an integer: %s\n",
790 String8(block
.getAttributeStringValue(identIdx
, &len
)).string());
791 hasErrors
= localHasErrors
= true;
793 ident
= identValue
.data
;
794 nextPublicId
.replaceValueFor(type
, ident
+1);
796 } else if (nextPublicId
.indexOfKey(type
) < 0) {
797 srcPos
.error("No 'id' attribute supplied <public>,"
798 " and no previous id defined in this file.\n");
799 hasErrors
= localHasErrors
= true;
800 } else if (!localHasErrors
) {
801 ident
= nextPublicId
.valueFor(type
);
802 nextPublicId
.replaceValueFor(type
, ident
+1);
805 if (!localHasErrors
) {
806 err
= outTable
->addPublic(srcPos
, myPackage
, type
, name
, ident
);
807 if (err
< NO_ERROR
) {
808 hasErrors
= localHasErrors
= true;
811 if (!localHasErrors
) {
812 sp
<AaptSymbols
> symbols
= assets
->getSymbolsFor(String8("R"));
813 if (symbols
!= NULL
) {
814 symbols
= symbols
->addNestedSymbol(String8(type
), srcPos
);
816 if (symbols
!= NULL
) {
817 symbols
->makeSymbolPublic(String8(name
), srcPos
);
819 block
.getComment(&len
) ? block
.getComment(&len
) : nulStr
);
820 symbols
->appendComment(String8(name
), comment
, srcPos
);
822 srcPos
.error("Unable to create symbols!\n");
823 hasErrors
= localHasErrors
= true;
827 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
828 if (code
== ResXMLTree::END_TAG
) {
829 if (strcmp16(block
.getElementName(&len
), public16
.string()) == 0) {
836 } else if (strcmp16(block
.getElementName(&len
), public_padding16
.string()) == 0) {
837 SourcePos
srcPos(in
->getPrintableSource(), block
.getLineNumber());
840 ssize_t typeIdx
= block
.indexOfAttribute(NULL
, "type");
842 srcPos
.error("A 'type' attribute is required for <public-padding>\n");
843 hasErrors
= localHasErrors
= true;
845 type
= String16(block
.getAttributeStringValue(typeIdx
, &len
));
848 ssize_t nameIdx
= block
.indexOfAttribute(NULL
, "name");
850 srcPos
.error("A 'name' attribute is required for <public-padding>\n");
851 hasErrors
= localHasErrors
= true;
853 name
= String16(block
.getAttributeStringValue(nameIdx
, &len
));
856 ssize_t startIdx
= block
.indexOfAttribute(NULL
, "start");
858 const char16_t* startStr
= block
.getAttributeStringValue(startIdx
, &len
);
859 Res_value startValue
;
860 if (!ResTable::stringToInt(startStr
, len
, &startValue
)) {
861 srcPos
.error("Given 'start' attribute is not an integer: %s\n",
862 String8(block
.getAttributeStringValue(startIdx
, &len
)).string());
863 hasErrors
= localHasErrors
= true;
865 start
= startValue
.data
;
867 } else if (nextPublicId
.indexOfKey(type
) < 0) {
868 srcPos
.error("No 'start' attribute supplied <public-padding>,"
869 " and no previous id defined in this file.\n");
870 hasErrors
= localHasErrors
= true;
871 } else if (!localHasErrors
) {
872 start
= nextPublicId
.valueFor(type
);
876 ssize_t endIdx
= block
.indexOfAttribute(NULL
, "end");
878 const char16_t* endStr
= block
.getAttributeStringValue(endIdx
, &len
);
880 if (!ResTable::stringToInt(endStr
, len
, &endValue
)) {
881 srcPos
.error("Given 'end' attribute is not an integer: %s\n",
882 String8(block
.getAttributeStringValue(endIdx
, &len
)).string());
883 hasErrors
= localHasErrors
= true;
888 srcPos
.error("No 'end' attribute supplied <public-padding>\n");
889 hasErrors
= localHasErrors
= true;
893 nextPublicId
.replaceValueFor(type
, end
+1);
895 srcPos
.error("Padding start '%ul' is after end '%ul'\n",
897 hasErrors
= localHasErrors
= true;
901 block
.getComment(&len
) ? block
.getComment(&len
) : nulStr
);
902 for (uint32_t curIdent
=start
; curIdent
<=end
; curIdent
++) {
903 if (localHasErrors
) {
906 String16
curName(name
);
908 sprintf(buf
, "%d", (int)(end
-curIdent
+1));
909 curName
.append(String16(buf
));
911 err
= outTable
->addEntry(srcPos
, myPackage
, type
, curName
,
912 String16("padding"), NULL
, &curParams
, false,
913 ResTable_map::TYPE_STRING
, overwrite
);
914 if (err
< NO_ERROR
) {
915 hasErrors
= localHasErrors
= true;
918 err
= outTable
->addPublic(srcPos
, myPackage
, type
,
920 if (err
< NO_ERROR
) {
921 hasErrors
= localHasErrors
= true;
924 sp
<AaptSymbols
> symbols
= assets
->getSymbolsFor(String8("R"));
925 if (symbols
!= NULL
) {
926 symbols
= symbols
->addNestedSymbol(String8(type
), srcPos
);
928 if (symbols
!= NULL
) {
929 symbols
->makeSymbolPublic(String8(curName
), srcPos
);
930 symbols
->appendComment(String8(curName
), comment
, srcPos
);
932 srcPos
.error("Unable to create symbols!\n");
933 hasErrors
= localHasErrors
= true;
937 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
938 if (code
== ResXMLTree::END_TAG
) {
939 if (strcmp16(block
.getElementName(&len
), public_padding16
.string()) == 0) {
946 } else if (strcmp16(block
.getElementName(&len
), private_symbols16
.string()) == 0) {
948 ssize_t pkgIdx
= block
.indexOfAttribute(NULL
, "package");
950 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
951 "A 'package' attribute is required for <private-symbols>\n");
952 hasErrors
= localHasErrors
= true;
954 pkg
= String16(block
.getAttributeStringValue(pkgIdx
, &len
));
955 if (!localHasErrors
) {
956 assets
->setSymbolsPrivatePackage(String8(pkg
));
959 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
960 if (code
== ResXMLTree::END_TAG
) {
961 if (strcmp16(block
.getElementName(&len
), private_symbols16
.string()) == 0) {
968 } else if (strcmp16(block
.getElementName(&len
), add_resource16
.string()) == 0) {
969 SourcePos
srcPos(in
->getPrintableSource(), block
.getLineNumber());
972 ssize_t typeIdx
= block
.indexOfAttribute(NULL
, "type");
974 srcPos
.error("A 'type' attribute is required for <add-resource>\n");
975 hasErrors
= localHasErrors
= true;
977 typeName
= String16(block
.getAttributeStringValue(typeIdx
, &len
));
980 ssize_t nameIdx
= block
.indexOfAttribute(NULL
, "name");
982 srcPos
.error("A 'name' attribute is required for <add-resource>\n");
983 hasErrors
= localHasErrors
= true;
985 name
= String16(block
.getAttributeStringValue(nameIdx
, &len
));
987 outTable
->canAddEntry(srcPos
, myPackage
, typeName
, name
);
989 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
990 if (code
== ResXMLTree::END_TAG
) {
991 if (strcmp16(block
.getElementName(&len
), add_resource16
.string()) == 0) {
998 } else if (strcmp16(block
.getElementName(&len
), declare_styleable16
.string()) == 0) {
999 SourcePos
srcPos(in
->getPrintableSource(), block
.getLineNumber());
1002 ssize_t identIdx
= block
.indexOfAttribute(NULL
, "name");
1004 srcPos
.error("A 'name' attribute is required for <declare-styleable>\n");
1005 hasErrors
= localHasErrors
= true;
1007 ident
= String16(block
.getAttributeStringValue(identIdx
, &len
));
1009 sp
<AaptSymbols
> symbols
= assets
->getSymbolsFor(String8("R"));
1010 if (!localHasErrors
) {
1011 if (symbols
!= NULL
) {
1012 symbols
= symbols
->addNestedSymbol(String8("styleable"), srcPos
);
1014 sp
<AaptSymbols
> styleSymbols
= symbols
;
1015 if (symbols
!= NULL
) {
1016 symbols
= symbols
->addNestedSymbol(String8(ident
), srcPos
);
1018 if (symbols
== NULL
) {
1019 srcPos
.error("Unable to create symbols!\n");
1020 return UNKNOWN_ERROR
;
1024 block
.getComment(&len
) ? block
.getComment(&len
) : nulStr
);
1025 styleSymbols
->appendComment(String8(ident
), comment
, srcPos
);
1030 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
1031 if (code
== ResXMLTree::START_TAG
) {
1032 if (strcmp16(block
.getElementName(&len
), skip16
.string()) == 0) {
1033 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
1034 && code
!= ResXMLTree::BAD_DOCUMENT
) {
1035 if (code
== ResXMLTree::END_TAG
) {
1036 if (strcmp16(block
.getElementName(&len
), skip16
.string()) == 0) {
1042 } else if (strcmp16(block
.getElementName(&len
), eat_comment16
.string()) == 0) {
1043 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
1044 && code
!= ResXMLTree::BAD_DOCUMENT
) {
1045 if (code
== ResXMLTree::END_TAG
) {
1046 if (strcmp16(block
.getElementName(&len
), eat_comment16
.string()) == 0) {
1052 } else if (strcmp16(block
.getElementName(&len
), attr16
.string()) != 0) {
1053 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1054 "Tag <%s> can not appear inside <declare-styleable>, only <attr>\n",
1055 String8(block
.getElementName(&len
)).string());
1056 return UNKNOWN_ERROR
;
1060 block
.getComment(&len
) ? block
.getComment(&len
) : nulStr
);
1062 err
= compileAttribute(in
, block
, myPackage
, outTable
, &itemIdent
, true);
1063 if (err
!= NO_ERROR
) {
1064 hasErrors
= localHasErrors
= true;
1067 if (symbols
!= NULL
) {
1068 SourcePos
srcPos(String8(in
->getPrintableSource()), block
.getLineNumber());
1069 symbols
->addSymbol(String8(itemIdent
), 0, srcPos
);
1070 symbols
->appendComment(String8(itemIdent
), comment
, srcPos
);
1071 //printf("Attribute %s comment: %s\n", String8(itemIdent).string(),
1072 // String8(comment).string());
1074 } else if (code
== ResXMLTree::END_TAG
) {
1075 if (strcmp16(block
.getElementName(&len
), declare_styleable16
.string()) == 0) {
1079 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1080 "Found tag </%s> where </attr> is expected\n",
1081 String8(block
.getElementName(&len
)).string());
1082 return UNKNOWN_ERROR
;
1087 } else if (strcmp16(block
.getElementName(&len
), attr16
.string()) == 0) {
1088 err
= compileAttribute(in
, block
, myPackage
, outTable
, NULL
);
1089 if (err
!= NO_ERROR
) {
1094 } else if (strcmp16(block
.getElementName(&len
), item16
.string()) == 0) {
1096 ssize_t attri
= block
.indexOfAttribute(NULL
, "type");
1098 curType
= String16(block
.getAttributeStringValue(attri
, &len
));
1099 ssize_t formatIdx
= block
.indexOfAttribute(NULL
, "format");
1100 if (formatIdx
>= 0) {
1101 String16 formatStr
= String16(block
.getAttributeStringValue(
1103 curFormat
= parse_flags(formatStr
.string(), formatStr
.size(),
1105 if (curFormat
== 0) {
1106 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1107 "Tag <item> 'format' attribute value \"%s\" not valid\n",
1108 String8(formatStr
).string());
1109 hasErrors
= localHasErrors
= true;
1113 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1114 "A 'type' attribute is required for <item>\n");
1115 hasErrors
= localHasErrors
= true;
1118 } else if (strcmp16(block
.getElementName(&len
), string16
.string()) == 0) {
1119 // Note the existence and locale of every string we process
1121 curParams
.getLocale(rawLocale
);
1122 String8
locale(rawLocale
);
1124 String16 translatable
;
1126 size_t n
= block
.getAttributeCount();
1127 for (size_t i
= 0; i
< n
; i
++) {
1129 const uint16_t* attr
= block
.getAttributeName(i
, &length
);
1130 if (strcmp16(attr
, name16
.string()) == 0) {
1131 name
.setTo(block
.getAttributeStringValue(i
, &length
));
1132 } else if (strcmp16(attr
, translatable16
.string()) == 0) {
1133 translatable
.setTo(block
.getAttributeStringValue(i
, &length
));
1137 if (name
.size() > 0) {
1138 if (translatable
== false16
) {
1139 // Untranslatable strings must only exist in the default [empty] locale
1140 if (locale
.size() > 0) {
1141 fprintf(stderr
, "aapt: warning: string '%s' in %s marked untranslatable but exists"
1142 " in locale '%s'\n", String8(name
).string(),
1143 bundle
->getResourceSourceDirs()[0],
1145 // hasErrors = localHasErrors = true;
1147 // Intentionally empty block:
1149 // Don't add untranslatable strings to the localization table; that
1150 // way if we later see localizations of them, they'll be flagged as
1151 // having no default translation.
1154 outTable
->addLocalization(name
, locale
);
1160 curFormat
= ResTable_map::TYPE_REFERENCE
|ResTable_map::TYPE_STRING
;
1162 curIsPseudolocalizable
= true;
1163 } else if (strcmp16(block
.getElementName(&len
), drawable16
.string()) == 0) {
1164 curTag
= &drawable16
;
1165 curType
= drawable16
;
1166 curFormat
= ResTable_map::TYPE_REFERENCE
|ResTable_map::TYPE_COLOR
;
1167 } else if (strcmp16(block
.getElementName(&len
), color16
.string()) == 0) {
1170 curFormat
= ResTable_map::TYPE_REFERENCE
|ResTable_map::TYPE_COLOR
;
1171 } else if (strcmp16(block
.getElementName(&len
), bool16
.string()) == 0) {
1174 curFormat
= ResTable_map::TYPE_REFERENCE
|ResTable_map::TYPE_BOOLEAN
;
1175 } else if (strcmp16(block
.getElementName(&len
), integer16
.string()) == 0) {
1176 curTag
= &integer16
;
1177 curType
= integer16
;
1178 curFormat
= ResTable_map::TYPE_REFERENCE
|ResTable_map::TYPE_INTEGER
;
1179 } else if (strcmp16(block
.getElementName(&len
), dimen16
.string()) == 0) {
1182 curFormat
= ResTable_map::TYPE_REFERENCE
|ResTable_map::TYPE_DIMENSION
;
1183 } else if (strcmp16(block
.getElementName(&len
), fraction16
.string()) == 0) {
1184 curTag
= &fraction16
;
1185 curType
= fraction16
;
1186 curFormat
= ResTable_map::TYPE_REFERENCE
|ResTable_map::TYPE_FRACTION
;
1187 } else if (strcmp16(block
.getElementName(&len
), bag16
.string()) == 0) {
1190 ssize_t attri
= block
.indexOfAttribute(NULL
, "type");
1192 curType
= String16(block
.getAttributeStringValue(attri
, &len
));
1194 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1195 "A 'type' attribute is required for <bag>\n");
1196 hasErrors
= localHasErrors
= true;
1198 } else if (strcmp16(block
.getElementName(&len
), style16
.string()) == 0) {
1202 } else if (strcmp16(block
.getElementName(&len
), plurals16
.string()) == 0) {
1203 curTag
= &plurals16
;
1204 curType
= plurals16
;
1206 } else if (strcmp16(block
.getElementName(&len
), array16
.string()) == 0) {
1210 curIsBagReplaceOnOverwrite
= true;
1211 ssize_t formatIdx
= block
.indexOfAttribute(NULL
, "format");
1212 if (formatIdx
>= 0) {
1213 String16 formatStr
= String16(block
.getAttributeStringValue(
1215 curFormat
= parse_flags(formatStr
.string(), formatStr
.size(),
1217 if (curFormat
== 0) {
1218 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1219 "Tag <array> 'format' attribute value \"%s\" not valid\n",
1220 String8(formatStr
).string());
1221 hasErrors
= localHasErrors
= true;
1224 } else if (strcmp16(block
.getElementName(&len
), string_array16
.string()) == 0) {
1225 curTag
= &string_array16
;
1227 curFormat
= ResTable_map::TYPE_REFERENCE
|ResTable_map::TYPE_STRING
;
1229 curIsBagReplaceOnOverwrite
= true;
1230 curIsPseudolocalizable
= true;
1231 } else if (strcmp16(block
.getElementName(&len
), integer_array16
.string()) == 0) {
1232 curTag
= &integer_array16
;
1234 curFormat
= ResTable_map::TYPE_REFERENCE
|ResTable_map::TYPE_INTEGER
;
1236 curIsBagReplaceOnOverwrite
= true;
1238 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1239 "Found tag %s where item is expected\n",
1240 String8(block
.getElementName(&len
)).string());
1241 return UNKNOWN_ERROR
;
1245 ssize_t identIdx
= block
.indexOfAttribute(NULL
, "name");
1246 if (identIdx
>= 0) {
1247 ident
= String16(block
.getAttributeStringValue(identIdx
, &len
));
1249 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1250 "A 'name' attribute is required for <%s>\n",
1251 String8(*curTag
).string());
1252 hasErrors
= localHasErrors
= true;
1255 String16
comment(block
.getComment(&len
) ? block
.getComment(&len
) : nulStr
);
1258 // Figure out the parent of this bag...
1259 String16 parentIdent
;
1260 ssize_t parentIdentIdx
= block
.indexOfAttribute(NULL
, "parent");
1261 if (parentIdentIdx
>= 0) {
1262 parentIdent
= String16(block
.getAttributeStringValue(parentIdentIdx
, &len
));
1264 ssize_t sep
= ident
.findLast('.');
1266 parentIdent
.setTo(ident
, sep
);
1270 if (!localHasErrors
) {
1271 err
= outTable
->startBag(SourcePos(in
->getPrintableSource(),
1272 block
.getLineNumber()), myPackage
, curType
, ident
,
1273 parentIdent
, &curParams
,
1274 overwrite
, curIsBagReplaceOnOverwrite
);
1275 if (err
!= NO_ERROR
) {
1276 hasErrors
= localHasErrors
= true;
1280 ssize_t elmIndex
= 0;
1281 char elmIndexStr
[14];
1282 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
1283 && code
!= ResXMLTree::BAD_DOCUMENT
) {
1285 if (code
== ResXMLTree::START_TAG
) {
1286 if (strcmp16(block
.getElementName(&len
), item16
.string()) != 0) {
1287 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1288 "Tag <%s> can not appear inside <%s>, only <item>\n",
1289 String8(block
.getElementName(&len
)).string(),
1290 String8(*curTag
).string());
1291 return UNKNOWN_ERROR
;
1295 if (curType
== array16
) {
1296 sprintf(elmIndexStr
, "^index_%d", (int)elmIndex
++);
1297 itemIdent
= String16(elmIndexStr
);
1298 } else if (curType
== plurals16
) {
1299 ssize_t itemIdentIdx
= block
.indexOfAttribute(NULL
, "quantity");
1300 if (itemIdentIdx
>= 0) {
1301 String16
quantity16(block
.getAttributeStringValue(itemIdentIdx
, &len
));
1302 if (quantity16
== other16
) {
1303 itemIdent
= quantityOther16
;
1305 else if (quantity16
== zero16
) {
1306 itemIdent
= quantityZero16
;
1308 else if (quantity16
== one16
) {
1309 itemIdent
= quantityOne16
;
1311 else if (quantity16
== two16
) {
1312 itemIdent
= quantityTwo16
;
1314 else if (quantity16
== few16
) {
1315 itemIdent
= quantityFew16
;
1317 else if (quantity16
== many16
) {
1318 itemIdent
= quantityMany16
;
1321 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1322 "Illegal 'quantity' attribute is <item> inside <plurals>\n");
1323 hasErrors
= localHasErrors
= true;
1326 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1327 "A 'quantity' attribute is required for <item> inside <plurals>\n");
1328 hasErrors
= localHasErrors
= true;
1331 ssize_t itemIdentIdx
= block
.indexOfAttribute(NULL
, "name");
1332 if (itemIdentIdx
>= 0) {
1333 itemIdent
= String16(block
.getAttributeStringValue(itemIdentIdx
, &len
));
1335 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1336 "A 'name' attribute is required for <item>\n");
1337 hasErrors
= localHasErrors
= true;
1341 ResXMLParser::ResXMLPosition parserPosition
;
1342 block
.getPosition(&parserPosition
);
1344 err
= parseAndAddBag(bundle
, in
, &block
, curParams
, myPackage
, curType
,
1345 ident
, parentIdent
, itemIdent
, curFormat
,
1346 false, overwrite
, outTable
);
1347 if (err
== NO_ERROR
) {
1348 if (curIsPseudolocalizable
&& localeIsDefined(curParams
)
1349 && bundle
->getPseudolocalize()) {
1350 // pseudolocalize here
1352 block
.setPosition(parserPosition
);
1353 err
= parseAndAddBag(bundle
, in
, &block
, pseudoParams
, myPackage
,
1354 curType
, ident
, parentIdent
, itemIdent
, curFormat
, true,
1355 overwrite
, outTable
);
1359 if (err
!= NO_ERROR
) {
1360 hasErrors
= localHasErrors
= true;
1362 } else if (code
== ResXMLTree::END_TAG
) {
1363 if (strcmp16(block
.getElementName(&len
), curTag
->string()) != 0) {
1364 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1365 "Found tag </%s> where </%s> is expected\n",
1366 String8(block
.getElementName(&len
)).string(),
1367 String8(*curTag
).string());
1368 return UNKNOWN_ERROR
;
1374 ResXMLParser::ResXMLPosition parserPosition
;
1375 block
.getPosition(&parserPosition
);
1377 err
= parseAndAddEntry(bundle
, in
, &block
, curParams
, myPackage
, curType
, ident
,
1378 *curTag
, curIsStyled
, curFormat
, false, overwrite
, outTable
);
1380 if (err
< NO_ERROR
) { // Why err < NO_ERROR instead of err != NO_ERROR?
1381 hasErrors
= localHasErrors
= true;
1383 else if (err
== NO_ERROR
) {
1384 if (curIsPseudolocalizable
&& localeIsDefined(curParams
)
1385 && bundle
->getPseudolocalize()) {
1386 // pseudolocalize here
1387 block
.setPosition(parserPosition
);
1388 err
= parseAndAddEntry(bundle
, in
, &block
, pseudoParams
, myPackage
, curType
,
1389 ident
, *curTag
, curIsStyled
, curFormat
, true, overwrite
, outTable
);
1390 if (err
!= NO_ERROR
) {
1391 hasErrors
= localHasErrors
= true;
1398 if (comment
.size() > 0) {
1399 printf("Comment for @%s:%s/%s: %s\n", String8(myPackage
).string(),
1400 String8(curType
).string(), String8(ident
).string(),
1401 String8(comment
).string());
1404 if (!localHasErrors
) {
1405 outTable
->appendComment(myPackage
, curType
, ident
, comment
, false);
1408 else if (code
== ResXMLTree::END_TAG
) {
1409 if (strcmp16(block
.getElementName(&len
), resources16
.string()) != 0) {
1410 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1411 "Unexpected end tag %s\n", String8(block
.getElementName(&len
)).string());
1412 return UNKNOWN_ERROR
;
1415 else if (code
== ResXMLTree::START_NAMESPACE
|| code
== ResXMLTree::END_NAMESPACE
) {
1417 else if (code
== ResXMLTree::TEXT
) {
1418 if (isWhitespace(block
.getText(&len
))) {
1421 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1422 "Found text \"%s\" where item tag is expected\n",
1423 String8(block
.getText(&len
)).string());
1424 return UNKNOWN_ERROR
;
1428 return hasErrors
? UNKNOWN_ERROR
: NO_ERROR
;
1431 ResourceTable::ResourceTable(Bundle
* bundle
, const String16
& assetsPackage
)
1432 : mAssetsPackage(assetsPackage
), mNextPackageId(1), mHaveAppPackage(false),
1433 mIsAppPackage(!bundle
->getExtending()),
1439 status_t
ResourceTable::addIncludedResources(Bundle
* bundle
, const sp
<AaptAssets
>& assets
)
1441 status_t err
= assets
->buildIncludedResources(bundle
);
1442 if (err
!= NO_ERROR
) {
1446 // For future reference to included resources.
1449 const ResTable
& incl
= assets
->getIncludedResources();
1451 // Retrieve all the packages.
1452 const size_t N
= incl
.getBasePackageCount();
1453 for (size_t phase
=0; phase
<2; phase
++) {
1454 for (size_t i
=0; i
<N
; i
++) {
1455 String16
name(incl
.getBasePackageName(i
));
1456 uint32_t id
= incl
.getBasePackageId(i
);
1457 // First time through: only add base packages (id
1458 // is not 0); second time through add the other
1462 // Skip base packages -- already one.
1465 // Assign a dynamic id.
1466 id
= mNextPackageId
;
1468 } else if (id
!= 0) {
1470 if (mHaveAppPackage
) {
1471 fprintf(stderr
, "Included resources have two application packages!\n");
1472 return UNKNOWN_ERROR
;
1474 mHaveAppPackage
= true;
1476 if (mNextPackageId
> id
) {
1477 fprintf(stderr
, "Included base package ID %d already in use!\n", id
);
1478 return UNKNOWN_ERROR
;
1482 NOISY(printf("Including package %s with ID=%d\n",
1483 String8(name
).string(), id
));
1484 sp
<Package
> p
= new Package(name
, id
);
1485 mPackages
.add(name
, p
);
1486 mOrderedPackages
.add(p
);
1488 if (id
>= mNextPackageId
) {
1489 mNextPackageId
= id
+1;
1495 // Every resource table always has one first entry, the bag attributes.
1496 const SourcePos
unknown(String8("????"), 0);
1497 sp
<Type
> attr
= getType(mAssetsPackage
, String16("attr"), unknown
);
1502 status_t
ResourceTable::addPublic(const SourcePos
& sourcePos
,
1503 const String16
& package
,
1504 const String16
& type
,
1505 const String16
& name
,
1506 const uint32_t ident
)
1508 uint32_t rid
= mAssets
->getIncludedResources()
1509 .identifierForName(name
.string(), name
.size(),
1510 type
.string(), type
.size(),
1511 package
.string(), package
.size());
1513 sourcePos
.error("Error declaring public resource %s/%s for included package %s\n",
1514 String8(type
).string(), String8(name
).string(),
1515 String8(package
).string());
1516 return UNKNOWN_ERROR
;
1519 sp
<Type
> t
= getType(package
, type
, sourcePos
);
1521 return UNKNOWN_ERROR
;
1523 return t
->addPublic(sourcePos
, name
, ident
);
1526 status_t
ResourceTable::addEntry(const SourcePos
& sourcePos
,
1527 const String16
& package
,
1528 const String16
& type
,
1529 const String16
& name
,
1530 const String16
& value
,
1531 const Vector
<StringPool::entry_style_span
>* style
,
1532 const ResTable_config
* params
,
1533 const bool doSetIndex
,
1534 const int32_t format
,
1535 const bool overwrite
)
1537 // Check for adding entries in other packages... for now we do
1538 // nothing. We need to do the right thing here to support skinning.
1539 uint32_t rid
= mAssets
->getIncludedResources()
1540 .identifierForName(name
.string(), name
.size(),
1541 type
.string(), type
.size(),
1542 package
.string(), package
.size());
1548 if (name
== String16("left")) {
1549 printf("Adding entry left: file=%s, line=%d, type=%s, value=%s\n",
1550 sourcePos
.file
.string(), sourcePos
.line
, String8(type
).string(),
1551 String8(value
).string());
1555 sp
<Entry
> e
= getEntry(package
, type
, name
, sourcePos
, overwrite
,
1556 params
, doSetIndex
);
1558 return UNKNOWN_ERROR
;
1560 status_t err
= e
->setItem(sourcePos
, value
, style
, format
, overwrite
);
1561 if (err
== NO_ERROR
) {
1567 status_t
ResourceTable::startBag(const SourcePos
& sourcePos
,
1568 const String16
& package
,
1569 const String16
& type
,
1570 const String16
& name
,
1571 const String16
& bagParent
,
1572 const ResTable_config
* params
,
1574 bool replace
, bool isId
)
1576 status_t result
= NO_ERROR
;
1578 // Check for adding entries in other packages... for now we do
1579 // nothing. We need to do the right thing here to support skinning.
1580 uint32_t rid
= mAssets
->getIncludedResources()
1581 .identifierForName(name
.string(), name
.size(),
1582 type
.string(), type
.size(),
1583 package
.string(), package
.size());
1589 if (name
== String16("left")) {
1590 printf("Adding bag left: file=%s, line=%d, type=%s\n",
1591 sourcePos
.file
.striing(), sourcePos
.line
, String8(type
).string());
1594 if (overlay
&& !mBundle
->getAutoAddOverlay() && !hasBagOrEntry(package
, type
, name
)) {
1595 bool canAdd
= false;
1596 sp
<Package
> p
= mPackages
.valueFor(package
);
1598 sp
<Type
> t
= p
->getTypes().valueFor(type
);
1600 if (t
->getCanAddEntries().indexOf(name
) >= 0) {
1606 sourcePos
.error("Resource does not already exist in overlay at '%s'; use <add-resource> to add.\n",
1607 String8(name
).string());
1608 return UNKNOWN_ERROR
;
1611 sp
<Entry
> e
= getEntry(package
, type
, name
, sourcePos
, overlay
, params
);
1613 return UNKNOWN_ERROR
;
1616 // If a parent is explicitly specified, set it.
1617 if (bagParent
.size() > 0) {
1618 String16 curPar
= e
->getParent();
1619 if (curPar
.size() > 0 && curPar
!= bagParent
) {
1620 sourcePos
.error("Conflicting parents specified, was '%s', now '%s'\n",
1621 String8(e
->getParent()).string(),
1622 String8(bagParent
).string());
1623 return UNKNOWN_ERROR
;
1625 e
->setParent(bagParent
);
1628 if ((result
= e
->makeItABag(sourcePos
)) != NO_ERROR
) {
1632 if (overlay
&& replace
) {
1633 return e
->emptyBag(sourcePos
);
1638 status_t
ResourceTable::addBag(const SourcePos
& sourcePos
,
1639 const String16
& package
,
1640 const String16
& type
,
1641 const String16
& name
,
1642 const String16
& bagParent
,
1643 const String16
& bagKey
,
1644 const String16
& value
,
1645 const Vector
<StringPool::entry_style_span
>* style
,
1646 const ResTable_config
* params
,
1647 bool replace
, bool isId
, const int32_t format
)
1649 // Check for adding entries in other packages... for now we do
1650 // nothing. We need to do the right thing here to support skinning.
1651 uint32_t rid
= mAssets
->getIncludedResources()
1652 .identifierForName(name
.string(), name
.size(),
1653 type
.string(), type
.size(),
1654 package
.string(), package
.size());
1660 if (name
== String16("left")) {
1661 printf("Adding bag left: file=%s, line=%d, type=%s\n",
1662 sourcePos
.file
.striing(), sourcePos
.line
, String8(type
).string());
1665 sp
<Entry
> e
= getEntry(package
, type
, name
, sourcePos
, replace
, params
);
1667 return UNKNOWN_ERROR
;
1670 // If a parent is explicitly specified, set it.
1671 if (bagParent
.size() > 0) {
1672 String16 curPar
= e
->getParent();
1673 if (curPar
.size() > 0 && curPar
!= bagParent
) {
1674 sourcePos
.error("Conflicting parents specified, was '%s', now '%s'\n",
1675 String8(e
->getParent()).string(),
1676 String8(bagParent
).string());
1677 return UNKNOWN_ERROR
;
1679 e
->setParent(bagParent
);
1682 const bool first
= e
->getBag().indexOfKey(bagKey
) < 0;
1683 status_t err
= e
->addToBag(sourcePos
, bagKey
, value
, style
, replace
, isId
, format
);
1684 if (err
== NO_ERROR
&& first
) {
1690 bool ResourceTable::hasBagOrEntry(const String16
& package
,
1691 const String16
& type
,
1692 const String16
& name
) const
1694 // First look for this in the included resources...
1695 uint32_t rid
= mAssets
->getIncludedResources()
1696 .identifierForName(name
.string(), name
.size(),
1697 type
.string(), type
.size(),
1698 package
.string(), package
.size());
1703 sp
<Package
> p
= mPackages
.valueFor(package
);
1705 sp
<Type
> t
= p
->getTypes().valueFor(type
);
1707 sp
<ConfigList
> c
= t
->getConfigs().valueFor(name
);
1708 if (c
!= NULL
) return true;
1715 bool ResourceTable::hasBagOrEntry(const String16
& ref
,
1716 const String16
* defType
,
1717 const String16
* defPackage
)
1719 String16 package
, type
, name
;
1720 if (!ResTable::expandResourceRef(ref
.string(), ref
.size(), &package
, &type
, &name
,
1721 defType
, defPackage
? defPackage
:&mAssetsPackage
, NULL
)) {
1724 return hasBagOrEntry(package
, type
, name
);
1727 bool ResourceTable::appendComment(const String16
& package
,
1728 const String16
& type
,
1729 const String16
& name
,
1730 const String16
& comment
,
1733 if (comment
.size() <= 0) {
1737 sp
<Package
> p
= mPackages
.valueFor(package
);
1739 sp
<Type
> t
= p
->getTypes().valueFor(type
);
1741 sp
<ConfigList
> c
= t
->getConfigs().valueFor(name
);
1743 c
->appendComment(comment
, onlyIfEmpty
);
1751 bool ResourceTable::appendTypeComment(const String16
& package
,
1752 const String16
& type
,
1753 const String16
& name
,
1754 const String16
& comment
)
1756 if (comment
.size() <= 0) {
1760 sp
<Package
> p
= mPackages
.valueFor(package
);
1762 sp
<Type
> t
= p
->getTypes().valueFor(type
);
1764 sp
<ConfigList
> c
= t
->getConfigs().valueFor(name
);
1766 c
->appendTypeComment(comment
);
1774 void ResourceTable::canAddEntry(const SourcePos
& pos
,
1775 const String16
& package
, const String16
& type
, const String16
& name
)
1777 sp
<Type
> t
= getType(package
, type
, pos
);
1779 t
->canAddEntry(name
);
1783 size_t ResourceTable::size() const {
1784 return mPackages
.size();
1787 size_t ResourceTable::numLocalResources() const {
1791 bool ResourceTable::hasResources() const {
1792 return mNumLocal
> 0;
1795 sp
<AaptFile
> ResourceTable::flatten(Bundle
* bundle
)
1797 sp
<AaptFile
> data
= new AaptFile(String8(), AaptGroupEntry(), String8());
1798 status_t err
= flatten(bundle
, data
);
1799 return err
== NO_ERROR
? data
: NULL
;
1802 inline uint32_t ResourceTable::getResId(const sp
<Package
>& p
,
1806 return makeResId(p
->getAssignedId(), t
->getIndex(), nameId
);
1809 uint32_t ResourceTable::getResId(const String16
& package
,
1810 const String16
& type
,
1811 const String16
& name
,
1812 bool onlyPublic
) const
1814 sp
<Package
> p
= mPackages
.valueFor(package
);
1815 if (p
== NULL
) return 0;
1817 // First look for this in the included resources...
1818 uint32_t specFlags
= 0;
1819 uint32_t rid
= mAssets
->getIncludedResources()
1820 .identifierForName(name
.string(), name
.size(),
1821 type
.string(), type
.size(),
1822 package
.string(), package
.size(),
1826 if ((specFlags
& ResTable_typeSpec::SPEC_PUBLIC
) == 0) {
1831 if (Res_INTERNALID(rid
)) {
1834 return Res_MAKEID(p
->getAssignedId()-1,
1839 sp
<Type
> t
= p
->getTypes().valueFor(type
);
1840 if (t
== NULL
) return 0;
1841 sp
<ConfigList
> c
= t
->getConfigs().valueFor(name
);
1842 if (c
== NULL
) return 0;
1843 int32_t ei
= c
->getEntryIndex();
1844 if (ei
< 0) return 0;
1845 return getResId(p
, t
, ei
);
1848 uint32_t ResourceTable::getResId(const String16
& ref
,
1849 const String16
* defType
,
1850 const String16
* defPackage
,
1851 const char** outErrorMsg
,
1852 bool onlyPublic
) const
1854 String16 package
, type
, name
;
1855 if (!ResTable::expandResourceRef(
1856 ref
.string(), ref
.size(), &package
, &type
, &name
,
1857 defType
, defPackage
? defPackage
:&mAssetsPackage
,
1859 NOISY(printf("Expanding resource: ref=%s\n",
1860 String8(ref
).string()));
1861 NOISY(printf("Expanding resource: defType=%s\n",
1862 defType
? String8(*defType
).string() : "NULL"));
1863 NOISY(printf("Expanding resource: defPackage=%s\n",
1864 defPackage
? String8(*defPackage
).string() : "NULL"));
1865 NOISY(printf("Expanding resource: ref=%s\n", String8(ref
).string()));
1866 NOISY(printf("Expanded resource: p=%s, t=%s, n=%s, res=0\n",
1867 String8(package
).string(), String8(type
).string(),
1868 String8(name
).string()));
1871 uint32_t res
= getResId(package
, type
, name
, onlyPublic
);
1872 NOISY(printf("Expanded resource: p=%s, t=%s, n=%s, res=%d\n",
1873 String8(package
).string(), String8(type
).string(),
1874 String8(name
).string(), res
));
1877 *outErrorMsg
= "No resource found that matches the given name";
1882 bool ResourceTable::isValidResourceName(const String16
& s
)
1884 const char16_t* p
= s
.string();
1887 if ((*p
>= 'a' && *p
<= 'z')
1888 || (*p
>= 'A' && *p
<= 'Z')
1890 || (!first
&& *p
>= '0' && *p
<= '9')) {
1900 bool ResourceTable::stringToValue(Res_value
* outValue
, StringPool
* pool
,
1901 const String16
& str
,
1902 bool preserveSpaces
, bool coerceType
,
1904 const Vector
<StringPool::entry_style_span
>* style
,
1905 String16
* outStr
, void* accessorCookie
,
1911 if (style
== NULL
|| style
->size() == 0) {
1912 // Text is not styled so it can be any type... let's figure it out.
1913 res
= mAssets
->getIncludedResources()
1914 .stringToValue(outValue
, &finalStr
, str
.string(), str
.size(), preserveSpaces
,
1915 coerceType
, attrID
, NULL
, &mAssetsPackage
, this,
1916 accessorCookie
, attrType
);
1918 // Styled text can only be a string, and while collecting the style
1919 // information we have already processed that string!
1920 outValue
->size
= sizeof(Res_value
);
1922 outValue
->dataType
= outValue
->TYPE_STRING
;
1931 if (outValue
->dataType
== outValue
->TYPE_STRING
) {
1932 // Should do better merging styles.
1934 if (style
!= NULL
&& style
->size() > 0) {
1935 outValue
->data
= pool
->add(finalStr
, *style
);
1937 outValue
->data
= pool
->add(finalStr
, true);
1940 // Caller will fill this in later.
1953 uint32_t ResourceTable::getCustomResource(
1954 const String16
& package
, const String16
& type
, const String16
& name
) const
1956 //printf("getCustomResource: %s %s %s\n", String8(package).string(),
1957 // String8(type).string(), String8(name).string());
1958 sp
<Package
> p
= mPackages
.valueFor(package
);
1959 if (p
== NULL
) return 0;
1960 sp
<Type
> t
= p
->getTypes().valueFor(type
);
1961 if (t
== NULL
) return 0;
1962 sp
<ConfigList
> c
= t
->getConfigs().valueFor(name
);
1963 if (c
== NULL
) return 0;
1964 int32_t ei
= c
->getEntryIndex();
1965 if (ei
< 0) return 0;
1966 return getResId(p
, t
, ei
);
1969 uint32_t ResourceTable::getCustomResourceWithCreation(
1970 const String16
& package
, const String16
& type
, const String16
& name
,
1971 const bool createIfNotFound
)
1973 uint32_t resId
= getCustomResource(package
, type
, name
);
1974 if (resId
!= 0 || !createIfNotFound
) {
1977 String16
value("false");
1979 status_t status
= addEntry(mCurrentXmlPos
, package
, type
, name
, value
, NULL
, NULL
, true);
1980 if (status
== NO_ERROR
) {
1981 resId
= getResId(package
, type
, name
);
1987 uint32_t ResourceTable::getRemappedPackage(uint32_t origPackage
) const
1992 bool ResourceTable::getAttributeType(uint32_t attrID
, uint32_t* outType
)
1994 //printf("getAttributeType #%08x\n", attrID);
1996 if (getItemValue(attrID
, ResTable_map::ATTR_TYPE
, &value
)) {
1997 //printf("getAttributeType #%08x (%s): #%08x\n", attrID,
1998 // String8(getEntry(attrID)->getName()).string(), value.data);
1999 *outType
= value
.data
;
2005 bool ResourceTable::getAttributeMin(uint32_t attrID
, uint32_t* outMin
)
2007 //printf("getAttributeMin #%08x\n", attrID);
2009 if (getItemValue(attrID
, ResTable_map::ATTR_MIN
, &value
)) {
2010 *outMin
= value
.data
;
2016 bool ResourceTable::getAttributeMax(uint32_t attrID
, uint32_t* outMax
)
2018 //printf("getAttributeMax #%08x\n", attrID);
2020 if (getItemValue(attrID
, ResTable_map::ATTR_MAX
, &value
)) {
2021 *outMax
= value
.data
;
2027 uint32_t ResourceTable::getAttributeL10N(uint32_t attrID
)
2029 //printf("getAttributeL10N #%08x\n", attrID);
2031 if (getItemValue(attrID
, ResTable_map::ATTR_L10N
, &value
)) {
2034 return ResTable_map::L10N_NOT_REQUIRED
;
2037 bool ResourceTable::getLocalizationSetting()
2039 return mBundle
->getRequireLocalization();
2042 void ResourceTable::reportError(void* accessorCookie
, const char* fmt
, ...)
2044 if (accessorCookie
!= NULL
&& fmt
!= NULL
) {
2045 AccessorCookie
* ac
= (AccessorCookie
*)accessorCookie
;
2050 retval
= vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
2052 ac
->sourcePos
.error("Error: %s (at '%s' with value '%s').\n",
2053 buf
, ac
->attr
.string(), ac
->value
.string());
2057 bool ResourceTable::getAttributeKeys(
2058 uint32_t attrID
, Vector
<String16
>* outKeys
)
2060 sp
<const Entry
> e
= getEntry(attrID
);
2062 const size_t N
= e
->getBag().size();
2063 for (size_t i
=0; i
<N
; i
++) {
2064 const String16
& key
= e
->getBag().keyAt(i
);
2065 if (key
.size() > 0 && key
.string()[0] != '^') {
2074 bool ResourceTable::getAttributeEnum(
2075 uint32_t attrID
, const char16_t* name
, size_t nameLen
,
2076 Res_value
* outValue
)
2078 //printf("getAttributeEnum #%08x %s\n", attrID, String8(name, nameLen).string());
2079 String16
nameStr(name
, nameLen
);
2080 sp
<const Entry
> e
= getEntry(attrID
);
2082 const size_t N
= e
->getBag().size();
2083 for (size_t i
=0; i
<N
; i
++) {
2084 //printf("Comparing %s to %s\n", String8(name, nameLen).string(),
2085 // String8(e->getBag().keyAt(i)).string());
2086 if (e
->getBag().keyAt(i
) == nameStr
) {
2087 return getItemValue(attrID
, e
->getBag().valueAt(i
).bagKeyId
, outValue
);
2094 bool ResourceTable::getAttributeFlags(
2095 uint32_t attrID
, const char16_t* name
, size_t nameLen
,
2096 Res_value
* outValue
)
2098 outValue
->dataType
= Res_value::TYPE_INT_HEX
;
2101 //printf("getAttributeFlags #%08x %s\n", attrID, String8(name, nameLen).string());
2102 String16
nameStr(name
, nameLen
);
2103 sp
<const Entry
> e
= getEntry(attrID
);
2105 const size_t N
= e
->getBag().size();
2107 const char16_t* end
= name
+ nameLen
;
2108 const char16_t* pos
= name
;
2109 bool failed
= false;
2110 while (pos
< end
&& !failed
) {
2111 const char16_t* start
= pos
;
2113 while (pos
< end
&& *pos
!= '|') {
2117 String16
nameStr(start
, pos
-start
);
2119 for (i
=0; i
<N
; i
++) {
2120 //printf("Comparing \"%s\" to \"%s\"\n", String8(nameStr).string(),
2121 // String8(e->getBag().keyAt(i)).string());
2122 if (e
->getBag().keyAt(i
) == nameStr
) {
2124 bool got
= getItemValue(attrID
, e
->getBag().valueAt(i
).bagKeyId
, &val
);
2128 //printf("Got value: 0x%08x\n", val.data);
2129 outValue
->data
|= val
.data
;
2135 // Didn't find this flag identifier.
2148 status_t
ResourceTable::assignResourceIds()
2150 const size_t N
= mOrderedPackages
.size();
2152 status_t firstError
= NO_ERROR
;
2154 // First generate all bag attributes and assign indices.
2155 for (pi
=0; pi
<N
; pi
++) {
2156 sp
<Package
> p
= mOrderedPackages
.itemAt(pi
);
2157 if (p
== NULL
|| p
->getTypes().size() == 0) {
2162 status_t err
= p
->applyPublicTypeOrder();
2163 if (err
!= NO_ERROR
&& firstError
== NO_ERROR
) {
2167 // Generate attributes...
2168 const size_t N
= p
->getOrderedTypes().size();
2170 for (ti
=0; ti
<N
; ti
++) {
2171 sp
<Type
> t
= p
->getOrderedTypes().itemAt(ti
);
2175 const size_t N
= t
->getOrderedConfigs().size();
2176 for (size_t ci
=0; ci
<N
; ci
++) {
2177 sp
<ConfigList
> c
= t
->getOrderedConfigs().itemAt(ci
);
2181 const size_t N
= c
->getEntries().size();
2182 for (size_t ei
=0; ei
<N
; ei
++) {
2183 sp
<Entry
> e
= c
->getEntries().valueAt(ei
);
2187 status_t err
= e
->generateAttributes(this, p
->getName());
2188 if (err
!= NO_ERROR
&& firstError
== NO_ERROR
) {
2195 const SourcePos
unknown(String8("????"), 0);
2196 sp
<Type
> attr
= p
->getType(String16("attr"), unknown
);
2198 // Assign indices...
2199 for (ti
=0; ti
<N
; ti
++) {
2200 sp
<Type
> t
= p
->getOrderedTypes().itemAt(ti
);
2204 err
= t
->applyPublicEntryOrder();
2205 if (err
!= NO_ERROR
&& firstError
== NO_ERROR
) {
2209 const size_t N
= t
->getOrderedConfigs().size();
2212 LOG_ALWAYS_FATAL_IF(ti
== 0 && attr
!= t
,
2213 "First type is not attr!");
2215 for (size_t ei
=0; ei
<N
; ei
++) {
2216 sp
<ConfigList
> c
= t
->getOrderedConfigs().itemAt(ei
);
2220 c
->setEntryIndex(ei
);
2224 // Assign resource IDs to keys in bags...
2225 for (ti
=0; ti
<N
; ti
++) {
2226 sp
<Type
> t
= p
->getOrderedTypes().itemAt(ti
);
2230 const size_t N
= t
->getOrderedConfigs().size();
2231 for (size_t ci
=0; ci
<N
; ci
++) {
2232 sp
<ConfigList
> c
= t
->getOrderedConfigs().itemAt(ci
);
2233 //printf("Ordered config #%d: %p\n", ci, c.get());
2234 const size_t N
= c
->getEntries().size();
2235 for (size_t ei
=0; ei
<N
; ei
++) {
2236 sp
<Entry
> e
= c
->getEntries().valueAt(ei
);
2240 status_t err
= e
->assignResourceIds(this, p
->getName());
2241 if (err
!= NO_ERROR
&& firstError
== NO_ERROR
) {
2251 status_t
ResourceTable::addSymbols(const sp
<AaptSymbols
>& outSymbols
) {
2252 const size_t N
= mOrderedPackages
.size();
2255 for (pi
=0; pi
<N
; pi
++) {
2256 sp
<Package
> p
= mOrderedPackages
.itemAt(pi
);
2257 if (p
->getTypes().size() == 0) {
2262 const size_t N
= p
->getOrderedTypes().size();
2265 for (ti
=0; ti
<N
; ti
++) {
2266 sp
<Type
> t
= p
->getOrderedTypes().itemAt(ti
);
2270 const size_t N
= t
->getOrderedConfigs().size();
2271 sp
<AaptSymbols
> typeSymbols
;
2272 typeSymbols
= outSymbols
->addNestedSymbol(String8(t
->getName()), t
->getPos());
2273 for (size_t ci
=0; ci
<N
; ci
++) {
2274 sp
<ConfigList
> c
= t
->getOrderedConfigs().itemAt(ci
);
2278 uint32_t rid
= getResId(p
, t
, ci
);
2280 return UNKNOWN_ERROR
;
2282 if (Res_GETPACKAGE(rid
) == (size_t)(p
->getAssignedId()-1)) {
2283 typeSymbols
->addSymbol(String8(c
->getName()), rid
, c
->getPos());
2285 String16
comment(c
->getComment());
2286 typeSymbols
->appendComment(String8(c
->getName()), comment
, c
->getPos());
2287 //printf("Type symbol %s comment: %s\n", String8(e->getName()).string(),
2288 // String8(comment).string());
2289 comment
= c
->getTypeComment();
2290 typeSymbols
->appendTypeComment(String8(c
->getName()), comment
);
2293 printf("**** NO MATCH: 0x%08x vs 0x%08x\n",
2294 Res_GETPACKAGE(rid
), p
->getAssignedId());
2305 ResourceTable::addLocalization(const String16
& name
, const String8
& locale
)
2307 mLocalizations
[name
].insert(locale
);
2312 * Flag various sorts of localization problems. '+' indicates checks already implemented;
2313 * '-' indicates checks that will be implemented in the future.
2315 * + A localized string for which no default-locale version exists => warning
2316 * + A string for which no version in an explicitly-requested locale exists => warning
2317 * + A localized translation of an translateable="false" string => warning
2318 * - A localized string not provided in every locale used by the table
2321 ResourceTable::validateLocalizations(void)
2323 status_t err
= NO_ERROR
;
2324 const String8 defaultLocale
;
2326 // For all strings...
2327 for (map
<String16
, set
<String8
> >::iterator nameIter
= mLocalizations
.begin();
2328 nameIter
!= mLocalizations
.end();
2330 const set
<String8
>& configSet
= nameIter
->second
; // naming convenience
2332 // Look for strings with no default localization
2333 if (configSet
.count(defaultLocale
) == 0) {
2334 fprintf(stdout
, "aapt: warning: string '%s' has no default translation in %s; found:",
2335 String8(nameIter
->first
).string(), mBundle
->getResourceSourceDirs()[0]);
2336 for (set
<String8
>::iterator locales
= configSet
.begin();
2337 locales
!= configSet
.end();
2339 fprintf(stdout
, " %s", (*locales
).string());
2341 fprintf(stdout
, "\n");
2342 // !!! TODO: throw an error here in some circumstances
2345 // Check that all requested localizations are present for this string
2346 if (mBundle
->getConfigurations() != NULL
&& mBundle
->getRequireLocalization()) {
2347 const char* allConfigs
= mBundle
->getConfigurations();
2348 const char* start
= allConfigs
;
2353 comma
= strchr(start
, ',');
2354 if (comma
!= NULL
) {
2355 config
.setTo(start
, comma
- start
);
2358 config
.setTo(start
);
2361 // don't bother with the pseudolocale "zz_ZZ"
2362 if (config
!= "zz_ZZ") {
2363 if (configSet
.find(config
) == configSet
.end()) {
2364 // okay, no specific localization found. it's possible that we are
2365 // requiring a specific regional localization [e.g. de_DE] but there is an
2366 // available string in the generic language localization [e.g. de];
2367 // consider that string to have fulfilled the localization requirement.
2368 String8
region(config
.string(), 2);
2369 if (configSet
.find(region
) == configSet
.end()) {
2370 if (configSet
.count(defaultLocale
) == 0) {
2371 fprintf(stdout
, "aapt: warning: "
2372 "**** string '%s' has no default or required localization "
2374 String8(nameIter
->first
).string(),
2376 mBundle
->getResourceSourceDirs()[0]);
2381 } while (comma
!= NULL
);
2390 ResourceFilter::parse(const char* arg
)
2396 const char* p
= arg
;
2405 String8
part(p
, q
-p
);
2407 if (part
== "zz_ZZ") {
2408 mContainsPseudo
= true;
2412 if (AaptGroupEntry::parseNamePart(part
, &axis
, &value
)) {
2413 fprintf(stderr
, "Invalid configuration: %s\n", arg
);
2414 fprintf(stderr
, " ");
2415 for (int i
=0; i
<p
-arg
; i
++) {
2416 fprintf(stderr
, " ");
2418 for (int i
=0; i
<q
-p
; i
++) {
2419 fprintf(stderr
, "^");
2421 fprintf(stderr
, "\n");
2425 ssize_t index
= mData
.indexOfKey(axis
);
2427 mData
.add(axis
, SortedVector
<uint32_t>());
2429 SortedVector
<uint32_t>& sv
= mData
.editValueFor(axis
);
2431 // if it's a locale with a region, also match an unmodified locale of the
2433 if (axis
== AXIS_LANGUAGE
) {
2434 if (value
& 0xffff0000) {
2435 sv
.add(value
& 0x0000ffff);
2447 ResourceFilter::match(int axis
, uint32_t value
)
2450 // they didn't specify anything so take everything
2453 ssize_t index
= mData
.indexOfKey(axis
);
2455 // we didn't request anything on this axis so take everything
2458 const SortedVector
<uint32_t>& sv
= mData
.valueAt(index
);
2459 return sv
.indexOf(value
) >= 0;
2463 ResourceFilter::match(const ResTable_config
& config
)
2465 if (config
.locale
) {
2466 uint32_t locale
= (config
.country
[1] << 24) | (config
.country
[0] << 16)
2467 | (config
.language
[1] << 8) | (config
.language
[0]);
2468 if (!match(AXIS_LANGUAGE
, locale
)) {
2472 if (!match(AXIS_ORIENTATION
, config
.orientation
)) {
2475 if (!match(AXIS_UIMODETYPE
, (config
.uiMode
&ResTable_config::MASK_UI_MODE_TYPE
))) {
2478 if (!match(AXIS_UIMODENIGHT
, (config
.uiMode
&ResTable_config::MASK_UI_MODE_NIGHT
))) {
2481 if (!match(AXIS_DENSITY
, config
.density
)) {
2484 if (!match(AXIS_TOUCHSCREEN
, config
.touchscreen
)) {
2487 if (!match(AXIS_KEYSHIDDEN
, config
.inputFlags
)) {
2490 if (!match(AXIS_KEYBOARD
, config
.keyboard
)) {
2493 if (!match(AXIS_NAVIGATION
, config
.navigation
)) {
2496 if (!match(AXIS_SCREENSIZE
, config
.screenSize
)) {
2499 if (!match(AXIS_VERSION
, config
.version
)) {
2505 status_t
ResourceTable::flatten(Bundle
* bundle
, const sp
<AaptFile
>& dest
)
2507 ResourceFilter filter
;
2508 status_t err
= filter
.parse(bundle
->getConfigurations());
2509 if (err
!= NO_ERROR
) {
2513 const size_t N
= mOrderedPackages
.size();
2516 // Iterate through all data, collecting all values (strings,
2517 // references, etc).
2518 StringPool valueStrings
= StringPool(false, bundle
->getUTF8());
2519 for (pi
=0; pi
<N
; pi
++) {
2520 sp
<Package
> p
= mOrderedPackages
.itemAt(pi
);
2521 if (p
->getTypes().size() == 0) {
2526 StringPool typeStrings
= StringPool(false, bundle
->getUTF8());
2527 StringPool keyStrings
= StringPool(false, bundle
->getUTF8());
2529 const size_t N
= p
->getOrderedTypes().size();
2530 for (size_t ti
=0; ti
<N
; ti
++) {
2531 sp
<Type
> t
= p
->getOrderedTypes().itemAt(ti
);
2533 typeStrings
.add(String16("<empty>"), false);
2536 typeStrings
.add(t
->getName(), false);
2538 const size_t N
= t
->getOrderedConfigs().size();
2539 for (size_t ci
=0; ci
<N
; ci
++) {
2540 sp
<ConfigList
> c
= t
->getOrderedConfigs().itemAt(ci
);
2544 const size_t N
= c
->getEntries().size();
2545 for (size_t ei
=0; ei
<N
; ei
++) {
2546 ConfigDescription config
= c
->getEntries().keyAt(ei
);
2547 if (!filter
.match(config
)) {
2550 sp
<Entry
> e
= c
->getEntries().valueAt(ei
);
2554 e
->setNameIndex(keyStrings
.add(e
->getName(), true));
2555 status_t err
= e
->prepareFlatten(&valueStrings
, this);
2556 if (err
!= NO_ERROR
) {
2563 p
->setTypeStrings(typeStrings
.createStringBlock());
2564 p
->setKeyStrings(keyStrings
.createStringBlock());
2569 // Now build the array of package chunks.
2570 Vector
<sp
<AaptFile
> > flatPackages
;
2571 for (pi
=0; pi
<N
; pi
++) {
2572 sp
<Package
> p
= mOrderedPackages
.itemAt(pi
);
2573 if (p
->getTypes().size() == 0) {
2578 const size_t N
= p
->getTypeStrings().size();
2580 const size_t baseSize
= sizeof(ResTable_package
);
2582 // Start the package data.
2583 sp
<AaptFile
> data
= new AaptFile(String8(), AaptGroupEntry(), String8());
2584 ResTable_package
* header
= (ResTable_package
*)data
->editData(baseSize
);
2585 if (header
== NULL
) {
2586 fprintf(stderr
, "ERROR: out of memory creating ResTable_package\n");
2589 memset(header
, 0, sizeof(*header
));
2590 header
->header
.type
= htods(RES_TABLE_PACKAGE_TYPE
);
2591 header
->header
.headerSize
= htods(sizeof(*header
));
2592 header
->id
= htodl(p
->getAssignedId());
2593 strcpy16_htod(header
->name
, p
->getName().string());
2595 // Write the string blocks.
2596 const size_t typeStringsStart
= data
->getSize();
2597 sp
<AaptFile
> strFile
= p
->getTypeStringsData();
2598 ssize_t amt
= data
->writeData(strFile
->getData(), strFile
->getSize());
2599 #if PRINT_STRING_METRICS
2600 fprintf(stderr
, "**** type strings: %d\n", amt
);
2606 const size_t keyStringsStart
= data
->getSize();
2607 strFile
= p
->getKeyStringsData();
2608 amt
= data
->writeData(strFile
->getData(), strFile
->getSize());
2609 #if PRINT_STRING_METRICS
2610 fprintf(stderr
, "**** key strings: %d\n", amt
);
2617 // Build the type chunks inside of this package.
2618 for (size_t ti
=0; ti
<N
; ti
++) {
2619 // Retrieve them in the same order as the type string block.
2621 String16
typeName(p
->getTypeStrings().stringAt(ti
, &len
));
2622 sp
<Type
> t
= p
->getTypes().valueFor(typeName
);
2623 LOG_ALWAYS_FATAL_IF(t
== NULL
&& typeName
!= String16("<empty>"),
2624 "Type name %s not found",
2625 String8(typeName
).string());
2627 const size_t N
= t
!= NULL
? t
->getOrderedConfigs().size() : 0;
2629 // First write the typeSpec chunk, containing information about
2630 // each resource entry in this type.
2632 const size_t typeSpecSize
= sizeof(ResTable_typeSpec
) + sizeof(uint32_t)*N
;
2633 const size_t typeSpecStart
= data
->getSize();
2634 ResTable_typeSpec
* tsHeader
= (ResTable_typeSpec
*)
2635 (((uint8_t*)data
->editData(typeSpecStart
+typeSpecSize
)) + typeSpecStart
);
2636 if (tsHeader
== NULL
) {
2637 fprintf(stderr
, "ERROR: out of memory creating ResTable_typeSpec\n");
2640 memset(tsHeader
, 0, sizeof(*tsHeader
));
2641 tsHeader
->header
.type
= htods(RES_TABLE_TYPE_SPEC_TYPE
);
2642 tsHeader
->header
.headerSize
= htods(sizeof(*tsHeader
));
2643 tsHeader
->header
.size
= htodl(typeSpecSize
);
2644 tsHeader
->id
= ti
+1;
2645 tsHeader
->entryCount
= htodl(N
);
2647 uint32_t* typeSpecFlags
= (uint32_t*)
2648 (((uint8_t*)data
->editData())
2649 + typeSpecStart
+ sizeof(ResTable_typeSpec
));
2650 memset(typeSpecFlags
, 0, sizeof(uint32_t)*N
);
2652 for (size_t ei
=0; ei
<N
; ei
++) {
2653 sp
<ConfigList
> cl
= t
->getOrderedConfigs().itemAt(ei
);
2654 if (cl
->getPublic()) {
2655 typeSpecFlags
[ei
] |= htodl(ResTable_typeSpec::SPEC_PUBLIC
);
2657 const size_t CN
= cl
->getEntries().size();
2658 for (size_t ci
=0; ci
<CN
; ci
++) {
2659 if (!filter
.match(cl
->getEntries().keyAt(ci
))) {
2662 for (size_t cj
=ci
+1; cj
<CN
; cj
++) {
2663 if (!filter
.match(cl
->getEntries().keyAt(cj
))) {
2666 typeSpecFlags
[ei
] |= htodl(
2667 cl
->getEntries().keyAt(ci
).diff(cl
->getEntries().keyAt(cj
)));
2673 // We need to write one type chunk for each configuration for
2674 // which we have entries in this type.
2675 const size_t NC
= t
->getUniqueConfigs().size();
2677 const size_t typeSize
= sizeof(ResTable_type
) + sizeof(uint32_t)*N
;
2679 for (size_t ci
=0; ci
<NC
; ci
++) {
2680 ConfigDescription config
= t
->getUniqueConfigs().itemAt(ci
);
2682 NOISY(printf("Writing config %d config: imsi:%d/%d lang:%c%c cnt:%c%c "
2683 "orien:%d ui:%d touch:%d density:%d key:%d inp:%d nav:%d w:%d h:%d\n",
2685 config
.mcc
, config
.mnc
,
2686 config
.language
[0] ? config
.language
[0] : '-',
2687 config
.language
[1] ? config
.language
[1] : '-',
2688 config
.country
[0] ? config
.country
[0] : '-',
2689 config
.country
[1] ? config
.country
[1] : '-',
2698 config
.screenHeight
));
2700 if (!filter
.match(config
)) {
2704 const size_t typeStart
= data
->getSize();
2706 ResTable_type
* tHeader
= (ResTable_type
*)
2707 (((uint8_t*)data
->editData(typeStart
+typeSize
)) + typeStart
);
2708 if (tHeader
== NULL
) {
2709 fprintf(stderr
, "ERROR: out of memory creating ResTable_type\n");
2713 memset(tHeader
, 0, sizeof(*tHeader
));
2714 tHeader
->header
.type
= htods(RES_TABLE_TYPE_TYPE
);
2715 tHeader
->header
.headerSize
= htods(sizeof(*tHeader
));
2717 tHeader
->entryCount
= htodl(N
);
2718 tHeader
->entriesStart
= htodl(typeSize
);
2719 tHeader
->config
= config
;
2720 NOISY(printf("Writing type %d config: imsi:%d/%d lang:%c%c cnt:%c%c "
2721 "orien:%d ui:%d touch:%d density:%d key:%d inp:%d nav:%d w:%d h:%d\n",
2723 tHeader
->config
.mcc
, tHeader
->config
.mnc
,
2724 tHeader
->config
.language
[0] ? tHeader
->config
.language
[0] : '-',
2725 tHeader
->config
.language
[1] ? tHeader
->config
.language
[1] : '-',
2726 tHeader
->config
.country
[0] ? tHeader
->config
.country
[0] : '-',
2727 tHeader
->config
.country
[1] ? tHeader
->config
.country
[1] : '-',
2728 tHeader
->config
.orientation
,
2729 tHeader
->config
.uiMode
,
2730 tHeader
->config
.touchscreen
,
2731 tHeader
->config
.density
,
2732 tHeader
->config
.keyboard
,
2733 tHeader
->config
.inputFlags
,
2734 tHeader
->config
.navigation
,
2735 tHeader
->config
.screenWidth
,
2736 tHeader
->config
.screenHeight
));
2737 tHeader
->config
.swapHtoD();
2739 // Build the entries inside of this type.
2740 for (size_t ei
=0; ei
<N
; ei
++) {
2741 sp
<ConfigList
> cl
= t
->getOrderedConfigs().itemAt(ei
);
2742 sp
<Entry
> e
= cl
->getEntries().valueFor(config
);
2744 // Set the offset for this entry in its type.
2745 uint32_t* index
= (uint32_t*)
2746 (((uint8_t*)data
->editData())
2747 + typeStart
+ sizeof(ResTable_type
));
2749 index
[ei
] = htodl(data
->getSize()-typeStart
-typeSize
);
2751 // Create the entry.
2752 ssize_t amt
= e
->flatten(bundle
, data
, cl
->getPublic());
2757 index
[ei
] = htodl(ResTable_type::NO_ENTRY
);
2761 // Fill in the rest of the type information.
2762 tHeader
= (ResTable_type
*)
2763 (((uint8_t*)data
->editData()) + typeStart
);
2764 tHeader
->header
.size
= htodl(data
->getSize()-typeStart
);
2768 // Fill in the rest of the package information.
2769 header
= (ResTable_package
*)data
->editData();
2770 header
->header
.size
= htodl(data
->getSize());
2771 header
->typeStrings
= htodl(typeStringsStart
);
2772 header
->lastPublicType
= htodl(p
->getTypeStrings().size());
2773 header
->keyStrings
= htodl(keyStringsStart
);
2774 header
->lastPublicKey
= htodl(p
->getKeyStrings().size());
2776 flatPackages
.add(data
);
2779 // And now write out the final chunks.
2780 const size_t dataStart
= dest
->getSize();
2784 ResTable_header header
;
2785 memset(&header
, 0, sizeof(header
));
2786 header
.header
.type
= htods(RES_TABLE_TYPE
);
2787 header
.header
.headerSize
= htods(sizeof(header
));
2788 header
.packageCount
= htodl(flatPackages
.size());
2789 status_t err
= dest
->writeData(&header
, sizeof(header
));
2790 if (err
!= NO_ERROR
) {
2791 fprintf(stderr
, "ERROR: out of memory creating ResTable_header\n");
2796 ssize_t strStart
= dest
->getSize();
2797 err
= valueStrings
.writeStringBlock(dest
);
2798 if (err
!= NO_ERROR
) {
2802 ssize_t amt
= (dest
->getSize()-strStart
);
2804 #if PRINT_STRING_METRICS
2805 fprintf(stderr
, "**** value strings: %d\n", amt
);
2806 fprintf(stderr
, "**** total strings: %d\n", strAmt
);
2809 for (pi
=0; pi
<flatPackages
.size(); pi
++) {
2810 err
= dest
->writeData(flatPackages
[pi
]->getData(),
2811 flatPackages
[pi
]->getSize());
2812 if (err
!= NO_ERROR
) {
2813 fprintf(stderr
, "ERROR: out of memory creating package chunk for ResTable_header\n");
2818 ResTable_header
* header
= (ResTable_header
*)
2819 (((uint8_t*)dest
->getData()) + dataStart
);
2820 header
->header
.size
= htodl(dest
->getSize() - dataStart
);
2822 NOISY(aout
<< "Resource table:"
2823 << HexDump(dest
->getData(), dest
->getSize()) << endl
);
2825 #if PRINT_STRING_METRICS
2826 fprintf(stderr
, "**** total resource table size: %d / %d%% strings\n",
2827 dest
->getSize(), (strAmt
*100)/dest
->getSize());
2833 void ResourceTable::writePublicDefinitions(const String16
& package
, FILE* fp
)
2836 "<!-- This file contains <public> resource definitions for all\n"
2837 " resources that were generated from the source data. -->\n"
2841 writePublicDefinitions(package
, fp
, true);
2842 writePublicDefinitions(package
, fp
, false);
2849 void ResourceTable::writePublicDefinitions(const String16
& package
, FILE* fp
, bool pub
)
2851 bool didHeader
= false;
2853 sp
<Package
> pkg
= mPackages
.valueFor(package
);
2855 const size_t NT
= pkg
->getOrderedTypes().size();
2856 for (size_t i
=0; i
<NT
; i
++) {
2857 sp
<Type
> t
= pkg
->getOrderedTypes().itemAt(i
);
2862 bool didType
= false;
2864 const size_t NC
= t
->getOrderedConfigs().size();
2865 for (size_t j
=0; j
<NC
; j
++) {
2866 sp
<ConfigList
> c
= t
->getOrderedConfigs().itemAt(j
);
2871 if (c
->getPublic() != pub
) {
2881 fprintf(fp
," <!-- PUBLIC SECTION. These resources have been declared public.\n");
2882 fprintf(fp
," Changes to these definitions will break binary compatibility. -->\n\n");
2884 fprintf(fp
," <!-- PRIVATE SECTION. These resources have not been declared public.\n");
2885 fprintf(fp
," You can make them public my moving these lines into a file in res/values. -->\n\n");
2890 const size_t NE
= c
->getEntries().size();
2891 for (size_t k
=0; k
<NE
; k
++) {
2892 const SourcePos
& pos
= c
->getEntries().valueAt(k
)->getPos();
2893 if (pos
.file
!= "") {
2894 fprintf(fp
," <!-- Declared at %s:%d -->\n",
2895 pos
.file
.string(), pos
.line
);
2899 fprintf(fp
, " <public type=\"%s\" name=\"%s\" id=\"0x%08x\" />\n",
2900 String8(t
->getName()).string(),
2901 String8(c
->getName()).string(),
2902 getResId(pkg
, t
, c
->getEntryIndex()));
2908 ResourceTable::Item::Item(const SourcePos
& _sourcePos
,
2910 const String16
& _value
,
2911 const Vector
<StringPool::entry_style_span
>* _style
,
2913 : sourcePos(_sourcePos
)
2925 status_t
ResourceTable::Entry::makeItABag(const SourcePos
& sourcePos
)
2927 if (mType
== TYPE_BAG
) {
2930 if (mType
== TYPE_UNKNOWN
) {
2934 sourcePos
.error("Resource entry %s is already defined as a single item.\n"
2935 "%s:%d: Originally defined here.\n",
2936 String8(mName
).string(),
2937 mItem
.sourcePos
.file
.string(), mItem
.sourcePos
.line
);
2938 return UNKNOWN_ERROR
;
2941 status_t
ResourceTable::Entry::setItem(const SourcePos
& sourcePos
,
2942 const String16
& value
,
2943 const Vector
<StringPool::entry_style_span
>* style
,
2945 const bool overwrite
)
2947 Item
item(sourcePos
, false, value
, style
);
2949 if (mType
== TYPE_BAG
) {
2950 const Item
& item(mBag
.valueAt(0));
2951 sourcePos
.error("Resource entry %s is already defined as a bag.\n"
2952 "%s:%d: Originally defined here.\n",
2953 String8(mName
).string(),
2954 item
.sourcePos
.file
.string(), item
.sourcePos
.line
);
2955 return UNKNOWN_ERROR
;
2957 if ( (mType
!= TYPE_UNKNOWN
) && (overwrite
== false) ) {
2958 sourcePos
.error("Resource entry %s is already defined.\n"
2959 "%s:%d: Originally defined here.\n",
2960 String8(mName
).string(),
2961 mItem
.sourcePos
.file
.string(), mItem
.sourcePos
.line
);
2962 return UNKNOWN_ERROR
;
2967 mItemFormat
= format
;
2971 status_t
ResourceTable::Entry::addToBag(const SourcePos
& sourcePos
,
2972 const String16
& key
, const String16
& value
,
2973 const Vector
<StringPool::entry_style_span
>* style
,
2974 bool replace
, bool isId
, int32_t format
)
2976 status_t err
= makeItABag(sourcePos
);
2977 if (err
!= NO_ERROR
) {
2981 Item
item(sourcePos
, isId
, value
, style
, format
);
2983 // XXX NOTE: there is an error if you try to have a bag with two keys,
2984 // one an attr and one an id, with the same name. Not something we
2985 // currently ever have to worry about.
2986 ssize_t origKey
= mBag
.indexOfKey(key
);
2989 const Item
& item(mBag
.valueAt(origKey
));
2990 sourcePos
.error("Resource entry %s already has bag item %s.\n"
2991 "%s:%d: Originally defined here.\n",
2992 String8(mName
).string(), String8(key
).string(),
2993 item
.sourcePos
.file
.string(), item
.sourcePos
.line
);
2994 return UNKNOWN_ERROR
;
2996 //printf("Replacing %s with %s\n",
2997 // String8(mBag.valueFor(key).value).string(), String8(value).string());
2998 mBag
.replaceValueFor(key
, item
);
3001 mBag
.add(key
, item
);
3005 status_t
ResourceTable::Entry::emptyBag(const SourcePos
& sourcePos
)
3007 status_t err
= makeItABag(sourcePos
);
3008 if (err
!= NO_ERROR
) {
3016 status_t
ResourceTable::Entry::generateAttributes(ResourceTable
* table
,
3017 const String16
& package
)
3019 const String16
attr16("attr");
3020 const String16
id16("id");
3021 const size_t N
= mBag
.size();
3022 for (size_t i
=0; i
<N
; i
++) {
3023 const String16
& key
= mBag
.keyAt(i
);
3024 const Item
& it
= mBag
.valueAt(i
);
3026 if (!table
->hasBagOrEntry(key
, &id16
, &package
)) {
3027 String16
value("false");
3028 status_t err
= table
->addEntry(SourcePos(String8("<generated>"), 0), package
,
3030 if (err
!= NO_ERROR
) {
3034 } else if (!table
->hasBagOrEntry(key
, &attr16
, &package
)) {
3037 // fprintf(stderr, "ERROR: Bag attribute '%s' has not been defined.\n",
3038 // String8(key).string());
3039 // const Item& item(mBag.valueAt(i));
3040 // fprintf(stderr, "Referenced from file %s line %d\n",
3041 // item.sourcePos.file.string(), item.sourcePos.line);
3042 // return UNKNOWN_ERROR;
3045 sprintf(numberStr
, "%d", ResTable_map::TYPE_ANY
);
3046 status_t err
= table
->addBag(SourcePos("<generated>", 0), package
,
3047 attr16
, key
, String16(""),
3049 String16(numberStr
), NULL
, NULL
);
3050 if (err
!= NO_ERROR
) {
3059 status_t
ResourceTable::Entry::assignResourceIds(ResourceTable
* table
,
3060 const String16
& package
)
3062 bool hasErrors
= false;
3064 if (mType
== TYPE_BAG
) {
3065 const char* errorMsg
;
3066 const String16
style16("style");
3067 const String16
attr16("attr");
3068 const String16
id16("id");
3070 if (mParent
.size() > 0) {
3071 mParentId
= table
->getResId(mParent
, &style16
, NULL
, &errorMsg
);
3072 if (mParentId
== 0) {
3073 mPos
.error("Error retrieving parent for item: %s '%s'.\n",
3074 errorMsg
, String8(mParent
).string());
3078 const size_t N
= mBag
.size();
3079 for (size_t i
=0; i
<N
; i
++) {
3080 const String16
& key
= mBag
.keyAt(i
);
3081 Item
& it
= mBag
.editValueAt(i
);
3082 it
.bagKeyId
= table
->getResId(key
,
3083 it
.isId
? &id16
: &attr16
, NULL
, &errorMsg
);
3084 //printf("Bag key of %s: #%08x\n", String8(key).string(), it.bagKeyId);
3085 if (it
.bagKeyId
== 0) {
3086 it
.sourcePos
.error("Error: %s: %s '%s'.\n", errorMsg
,
3087 String8(it
.isId
? id16
: attr16
).string(),
3088 String8(key
).string());
3093 return hasErrors
? UNKNOWN_ERROR
: NO_ERROR
;
3096 status_t
ResourceTable::Entry::prepareFlatten(StringPool
* strings
, ResourceTable
* table
)
3098 if (mType
== TYPE_ITEM
) {
3100 AccessorCookie
ac(it
.sourcePos
, String8(mName
), String8(it
.value
));
3101 if (!table
->stringToValue(&it
.parsedValue
, strings
,
3102 it
.value
, false, true, 0,
3103 &it
.style
, NULL
, &ac
, mItemFormat
)) {
3104 return UNKNOWN_ERROR
;
3106 } else if (mType
== TYPE_BAG
) {
3107 const size_t N
= mBag
.size();
3108 for (size_t i
=0; i
<N
; i
++) {
3109 const String16
& key
= mBag
.keyAt(i
);
3110 Item
& it
= mBag
.editValueAt(i
);
3111 AccessorCookie
ac(it
.sourcePos
, String8(key
), String8(it
.value
));
3112 if (!table
->stringToValue(&it
.parsedValue
, strings
,
3113 it
.value
, false, true, it
.bagKeyId
,
3114 &it
.style
, NULL
, &ac
, it
.format
)) {
3115 return UNKNOWN_ERROR
;
3119 mPos
.error("Error: entry %s is not a single item or a bag.\n",
3120 String8(mName
).string());
3121 return UNKNOWN_ERROR
;
3126 ssize_t
ResourceTable::Entry::flatten(Bundle
* bundle
, const sp
<AaptFile
>& data
, bool isPublic
)
3129 ResTable_entry header
;
3130 memset(&header
, 0, sizeof(header
));
3131 header
.size
= htods(sizeof(header
));
3132 const type ty
= this != NULL
? mType
: TYPE_ITEM
;
3134 if (ty
== TYPE_BAG
) {
3135 header
.flags
|= htods(header
.FLAG_COMPLEX
);
3138 header
.flags
|= htods(header
.FLAG_PUBLIC
);
3140 header
.key
.index
= htodl(mNameIndex
);
3142 if (ty
!= TYPE_BAG
) {
3143 status_t err
= data
->writeData(&header
, sizeof(header
));
3144 if (err
!= NO_ERROR
) {
3145 fprintf(stderr
, "ERROR: out of memory creating ResTable_entry\n");
3149 const Item
& it
= mItem
;
3151 memset(&par
, 0, sizeof(par
));
3152 par
.size
= htods(it
.parsedValue
.size
);
3153 par
.dataType
= it
.parsedValue
.dataType
;
3154 par
.res0
= it
.parsedValue
.res0
;
3155 par
.data
= htodl(it
.parsedValue
.data
);
3157 printf("Writing item (%s): type=%d, data=0x%x, res0=0x%x\n",
3158 String8(mName
).string(), it
.parsedValue
.dataType
,
3159 it
.parsedValue
.data
, par
.res0
);
3161 err
= data
->writeData(&par
, it
.parsedValue
.size
);
3162 if (err
!= NO_ERROR
) {
3163 fprintf(stderr
, "ERROR: out of memory creating Res_value\n");
3166 amt
+= it
.parsedValue
.size
;
3168 size_t N
= mBag
.size();
3170 // Create correct ordering of items.
3171 KeyedVector
<uint32_t, const Item
*> items
;
3172 for (i
=0; i
<N
; i
++) {
3173 const Item
& it
= mBag
.valueAt(i
);
3174 items
.add(it
.bagKeyId
, &it
);
3178 ResTable_map_entry mapHeader
;
3179 memcpy(&mapHeader
, &header
, sizeof(header
));
3180 mapHeader
.size
= htods(sizeof(mapHeader
));
3181 mapHeader
.parent
.ident
= htodl(mParentId
);
3182 mapHeader
.count
= htodl(N
);
3183 status_t err
= data
->writeData(&mapHeader
, sizeof(mapHeader
));
3184 if (err
!= NO_ERROR
) {
3185 fprintf(stderr
, "ERROR: out of memory creating ResTable_entry\n");
3189 for (i
=0; i
<N
; i
++) {
3190 const Item
& it
= *items
.valueAt(i
);
3192 map
.name
.ident
= htodl(it
.bagKeyId
);
3193 map
.value
.size
= htods(it
.parsedValue
.size
);
3194 map
.value
.dataType
= it
.parsedValue
.dataType
;
3195 map
.value
.res0
= it
.parsedValue
.res0
;
3196 map
.value
.data
= htodl(it
.parsedValue
.data
);
3197 err
= data
->writeData(&map
, sizeof(map
));
3198 if (err
!= NO_ERROR
) {
3199 fprintf(stderr
, "ERROR: out of memory creating Res_value\n");
3208 void ResourceTable::ConfigList::appendComment(const String16
& comment
,
3211 if (comment
.size() <= 0) {
3214 if (onlyIfEmpty
&& mComment
.size() > 0) {
3217 if (mComment
.size() > 0) {
3218 mComment
.append(String16("\n"));
3220 mComment
.append(comment
);
3223 void ResourceTable::ConfigList::appendTypeComment(const String16
& comment
)
3225 if (comment
.size() <= 0) {
3228 if (mTypeComment
.size() > 0) {
3229 mTypeComment
.append(String16("\n"));
3231 mTypeComment
.append(comment
);
3234 status_t
ResourceTable::Type::addPublic(const SourcePos
& sourcePos
,
3235 const String16
& name
,
3236 const uint32_t ident
)
3239 int32_t entryIdx
= Res_GETENTRY(ident
);
3241 sourcePos
.error("Public resource %s/%s has an invalid 0 identifier (0x%08x).\n",
3242 String8(mName
).string(), String8(name
).string(), ident
);
3243 return UNKNOWN_ERROR
;
3247 int32_t typeIdx
= Res_GETTYPE(ident
);
3250 if (mPublicIndex
> 0 && mPublicIndex
!= typeIdx
) {
3251 sourcePos
.error("Public resource %s/%s has conflicting type codes for its"
3252 " public identifiers (0x%x vs 0x%x).\n",
3253 String8(mName
).string(), String8(name
).string(),
3254 mPublicIndex
, typeIdx
);
3255 return UNKNOWN_ERROR
;
3257 mPublicIndex
= typeIdx
;
3260 if (mFirstPublicSourcePos
== NULL
) {
3261 mFirstPublicSourcePos
= new SourcePos(sourcePos
);
3264 if (mPublic
.indexOfKey(name
) < 0) {
3265 mPublic
.add(name
, Public(sourcePos
, String16(), ident
));
3267 Public
& p
= mPublic
.editValueFor(name
);
3268 if (p
.ident
!= ident
) {
3269 sourcePos
.error("Public resource %s/%s has conflicting public identifiers"
3270 " (0x%08x vs 0x%08x).\n"
3271 "%s:%d: Originally defined here.\n",
3272 String8(mName
).string(), String8(name
).string(), p
.ident
, ident
,
3273 p
.sourcePos
.file
.string(), p
.sourcePos
.line
);
3274 return UNKNOWN_ERROR
;
3281 void ResourceTable::Type::canAddEntry(const String16
& name
)
3283 mCanAddEntries
.add(name
);
3286 sp
<ResourceTable::Entry
> ResourceTable::Type::getEntry(const String16
& entry
,
3287 const SourcePos
& sourcePos
,
3288 const ResTable_config
* config
,
3291 bool autoAddOverlay
)
3294 sp
<ConfigList
> c
= mConfigs
.valueFor(entry
);
3296 if (overlay
&& !autoAddOverlay
&& mCanAddEntries
.indexOf(entry
) < 0) {
3297 sourcePos
.error("Resource at %s appears in overlay but not"
3298 " in the base package; use <add-resource> to add.\n",
3299 String8(entry
).string());
3302 c
= new ConfigList(entry
, sourcePos
);
3303 mConfigs
.add(entry
, c
);
3304 pos
= (int)mOrderedConfigs
.size();
3305 mOrderedConfigs
.add(c
);
3307 c
->setEntryIndex(pos
);
3311 ConfigDescription cdesc
;
3312 if (config
) cdesc
= *config
;
3314 sp
<Entry
> e
= c
->getEntries().valueFor(cdesc
);
3316 if (config
!= NULL
) {
3317 NOISY(printf("New entry at %s:%d: imsi:%d/%d lang:%c%c cnt:%c%c "
3318 "orien:%d touch:%d density:%d key:%d inp:%d nav:%d w:%d h:%d\n",
3319 sourcePos
.file
.string(), sourcePos
.line
,
3320 config
->mcc
, config
->mnc
,
3321 config
->language
[0] ? config
->language
[0] : '-',
3322 config
->language
[1] ? config
->language
[1] : '-',
3323 config
->country
[0] ? config
->country
[0] : '-',
3324 config
->country
[1] ? config
->country
[1] : '-',
3325 config
->orientation
,
3326 config
->touchscreen
,
3331 config
->screenWidth
,
3332 config
->screenHeight
));
3334 NOISY(printf("New entry at %s:%d: NULL config\n",
3335 sourcePos
.file
.string(), sourcePos
.line
));
3337 e
= new Entry(entry
, sourcePos
);
3338 c
->addEntry(cdesc
, e
);
3342 for (pos=0; pos<(int)mOrderedConfigs.size(); pos++) {
3343 if (mOrderedConfigs[pos] == c) {
3347 if (pos >= (int)mOrderedConfigs.size()) {
3348 sourcePos.error("Internal error: config not found in mOrderedConfigs when adding entry");
3352 e->setEntryIndex(pos);
3357 mUniqueConfigs
.add(cdesc
);
3362 status_t
ResourceTable::Type::applyPublicEntryOrder()
3364 size_t N
= mOrderedConfigs
.size();
3365 Vector
<sp
<ConfigList
> > origOrder(mOrderedConfigs
);
3366 bool hasError
= false;
3369 for (i
=0; i
<N
; i
++) {
3370 mOrderedConfigs
.replaceAt(NULL
, i
);
3373 const size_t NP
= mPublic
.size();
3374 //printf("Ordering %d configs from %d public defs\n", N, NP);
3376 for (j
=0; j
<NP
; j
++) {
3377 const String16
& name
= mPublic
.keyAt(j
);
3378 const Public
& p
= mPublic
.valueAt(j
);
3379 int32_t idx
= Res_GETENTRY(p
.ident
);
3380 //printf("Looking for entry \"%s\"/\"%s\" (0x%08x) in %d...\n",
3381 // String8(mName).string(), String8(name).string(), p.ident, N);
3383 for (i
=0; i
<N
; i
++) {
3384 sp
<ConfigList
> e
= origOrder
.itemAt(i
);
3385 //printf("#%d: \"%s\"\n", i, String8(e->getName()).string());
3386 if (e
->getName() == name
) {
3387 if (idx
>= (int32_t)mOrderedConfigs
.size()) {
3388 p
.sourcePos
.error("Public entry identifier 0x%x entry index "
3389 "is larger than available symbols (index %d, total symbols %d).\n",
3390 p
.ident
, idx
, mOrderedConfigs
.size());
3392 } else if (mOrderedConfigs
.itemAt(idx
) == NULL
) {
3394 e
->setPublicSourcePos(p
.sourcePos
);
3395 mOrderedConfigs
.replaceAt(e
, idx
);
3396 origOrder
.removeAt(i
);
3401 sp
<ConfigList
> oe
= mOrderedConfigs
.itemAt(idx
);
3403 p
.sourcePos
.error("Multiple entry names declared for public entry"
3404 " identifier 0x%x in type %s (%s vs %s).\n"
3405 "%s:%d: Originally defined here.",
3406 idx
+1, String8(mName
).string(),
3407 String8(oe
->getName()).string(),
3408 String8(name
).string(),
3409 oe
->getPublicSourcePos().file
.string(),
3410 oe
->getPublicSourcePos().line
);
3417 p
.sourcePos
.error("Public symbol %s/%s declared here is not defined.",
3418 String8(mName
).string(), String8(name
).string());
3423 //printf("Copying back in %d non-public configs, have %d\n", N, origOrder.size());
3425 if (N
!= origOrder
.size()) {
3426 printf("Internal error: remaining private symbol count mismatch\n");
3427 N
= origOrder
.size();
3431 for (i
=0; i
<N
; i
++) {
3432 sp
<ConfigList
> e
= origOrder
.itemAt(i
);
3433 // There will always be enough room for the remaining entries.
3434 while (mOrderedConfigs
.itemAt(j
) != NULL
) {
3437 mOrderedConfigs
.replaceAt(e
, j
);
3441 return hasError
? UNKNOWN_ERROR
: NO_ERROR
;
3444 ResourceTable::Package::Package(const String16
& name
, ssize_t includedId
)
3445 : mName(name
), mIncludedId(includedId
),
3446 mTypeStringsMapping(0xffffffff),
3447 mKeyStringsMapping(0xffffffff)
3451 sp
<ResourceTable::Type
> ResourceTable::Package::getType(const String16
& type
,
3452 const SourcePos
& sourcePos
,
3455 sp
<Type
> t
= mTypes
.valueFor(type
);
3457 t
= new Type(type
, sourcePos
);
3458 mTypes
.add(type
, t
);
3459 mOrderedTypes
.add(t
);
3461 // For some reason the type's index is set to one plus the index
3462 // in the mOrderedTypes list, rather than just the index.
3463 t
->setIndex(mOrderedTypes
.size());
3469 status_t
ResourceTable::Package::setTypeStrings(const sp
<AaptFile
>& data
)
3471 mTypeStringsData
= data
;
3472 status_t err
= setStrings(data
, &mTypeStrings
, &mTypeStringsMapping
);
3473 if (err
!= NO_ERROR
) {
3474 fprintf(stderr
, "ERROR: Type string data is corrupt!\n");
3479 status_t
ResourceTable::Package::setKeyStrings(const sp
<AaptFile
>& data
)
3481 mKeyStringsData
= data
;
3482 status_t err
= setStrings(data
, &mKeyStrings
, &mKeyStringsMapping
);
3483 if (err
!= NO_ERROR
) {
3484 fprintf(stderr
, "ERROR: Key string data is corrupt!\n");
3489 status_t
ResourceTable::Package::setStrings(const sp
<AaptFile
>& data
,
3490 ResStringPool
* strings
,
3491 DefaultKeyedVector
<String16
, uint32_t>* mappings
)
3493 if (data
->getData() == NULL
) {
3494 return UNKNOWN_ERROR
;
3497 NOISY(aout
<< "Setting restable string pool: "
3498 << HexDump(data
->getData(), data
->getSize()) << endl
);
3500 status_t err
= strings
->setTo(data
->getData(), data
->getSize());
3501 if (err
== NO_ERROR
) {
3502 const size_t N
= strings
->size();
3503 for (size_t i
=0; i
<N
; i
++) {
3505 mappings
->add(String16(strings
->stringAt(i
, &len
)), i
);
3511 status_t
ResourceTable::Package::applyPublicTypeOrder()
3513 size_t N
= mOrderedTypes
.size();
3514 Vector
<sp
<Type
> > origOrder(mOrderedTypes
);
3517 for (i
=0; i
<N
; i
++) {
3518 mOrderedTypes
.replaceAt(NULL
, i
);
3521 for (i
=0; i
<N
; i
++) {
3522 sp
<Type
> t
= origOrder
.itemAt(i
);
3523 int32_t idx
= t
->getPublicIndex();
3526 while (idx
>= (int32_t)mOrderedTypes
.size()) {
3527 mOrderedTypes
.add();
3529 if (mOrderedTypes
.itemAt(idx
) != NULL
) {
3530 sp
<Type
> ot
= mOrderedTypes
.itemAt(idx
);
3531 t
->getFirstPublicSourcePos().error("Multiple type names declared for public type"
3532 " identifier 0x%x (%s vs %s).\n"
3533 "%s:%d: Originally defined here.",
3534 idx
, String8(ot
->getName()).string(),
3535 String8(t
->getName()).string(),
3536 ot
->getFirstPublicSourcePos().file
.string(),
3537 ot
->getFirstPublicSourcePos().line
);
3538 return UNKNOWN_ERROR
;
3540 mOrderedTypes
.replaceAt(t
, idx
);
3541 origOrder
.removeAt(i
);
3548 for (i
=0; i
<N
; i
++) {
3549 sp
<Type
> t
= origOrder
.itemAt(i
);
3550 // There will always be enough room for the remaining types.
3551 while (mOrderedTypes
.itemAt(j
) != NULL
) {
3554 mOrderedTypes
.replaceAt(t
, j
);
3560 sp
<ResourceTable::Package
> ResourceTable::getPackage(const String16
& package
)
3562 sp
<Package
> p
= mPackages
.valueFor(package
);
3564 if (mIsAppPackage
) {
3565 if (mHaveAppPackage
) {
3566 fprintf(stderr
, "Adding multiple application package resources; only one is allowed.\n"
3567 "Use -x to create extended resources.\n");
3570 mHaveAppPackage
= true;
3571 p
= new Package(package
, 127);
3573 p
= new Package(package
, mNextPackageId
);
3575 //printf("*** NEW PACKAGE: \"%s\" id=%d\n",
3576 // String8(package).string(), p->getAssignedId());
3577 mPackages
.add(package
, p
);
3578 mOrderedPackages
.add(p
);
3584 sp
<ResourceTable::Type
> ResourceTable::getType(const String16
& package
,
3585 const String16
& type
,
3586 const SourcePos
& sourcePos
,
3589 sp
<Package
> p
= getPackage(package
);
3593 return p
->getType(type
, sourcePos
, doSetIndex
);
3596 sp
<ResourceTable::Entry
> ResourceTable::getEntry(const String16
& package
,
3597 const String16
& type
,
3598 const String16
& name
,
3599 const SourcePos
& sourcePos
,
3601 const ResTable_config
* config
,
3604 sp
<Type
> t
= getType(package
, type
, sourcePos
, doSetIndex
);
3608 return t
->getEntry(name
, sourcePos
, config
, doSetIndex
, overlay
, mBundle
->getAutoAddOverlay());
3611 sp
<const ResourceTable::Entry
> ResourceTable::getEntry(uint32_t resID
,
3612 const ResTable_config
* config
) const
3614 int pid
= Res_GETPACKAGE(resID
)+1;
3615 const size_t N
= mOrderedPackages
.size();
3618 for (i
=0; i
<N
; i
++) {
3619 sp
<Package
> check
= mOrderedPackages
[i
];
3620 if (check
->getAssignedId() == pid
) {
3627 fprintf(stderr
, "warning: Package not found for resource #%08x\n", resID
);
3631 int tid
= Res_GETTYPE(resID
);
3632 if (tid
< 0 || tid
>= (int)p
->getOrderedTypes().size()) {
3633 fprintf(stderr
, "warning: Type not found for resource #%08x\n", resID
);
3636 sp
<Type
> t
= p
->getOrderedTypes()[tid
];
3638 int eid
= Res_GETENTRY(resID
);
3639 if (eid
< 0 || eid
>= (int)t
->getOrderedConfigs().size()) {
3640 fprintf(stderr
, "warning: Entry not found for resource #%08x\n", resID
);
3644 sp
<ConfigList
> c
= t
->getOrderedConfigs()[eid
];
3646 fprintf(stderr
, "warning: Entry not found for resource #%08x\n", resID
);
3650 ConfigDescription cdesc
;
3651 if (config
) cdesc
= *config
;
3652 sp
<Entry
> e
= c
->getEntries().valueFor(cdesc
);
3654 fprintf(stderr
, "warning: Entry configuration not found for resource #%08x\n", resID
);
3661 const ResourceTable::Item
* ResourceTable::getItem(uint32_t resID
, uint32_t attrID
) const
3663 sp
<const Entry
> e
= getEntry(resID
);
3668 const size_t N
= e
->getBag().size();
3669 for (size_t i
=0; i
<N
; i
++) {
3670 const Item
& it
= e
->getBag().valueAt(i
);
3671 if (it
.bagKeyId
== 0) {
3672 fprintf(stderr
, "warning: ID not yet assigned to '%s' in bag '%s'\n",
3673 String8(e
->getName()).string(),
3674 String8(e
->getBag().keyAt(i
)).string());
3676 if (it
.bagKeyId
== attrID
) {
3684 bool ResourceTable::getItemValue(
3685 uint32_t resID
, uint32_t attrID
, Res_value
* outValue
)
3687 const Item
* item
= getItem(resID
, attrID
);
3691 if (item
->evaluating
) {
3692 sp
<const Entry
> e
= getEntry(resID
);
3693 const size_t N
= e
->getBag().size();
3695 for (i
=0; i
<N
; i
++) {
3696 if (&e
->getBag().valueAt(i
) == item
) {
3700 fprintf(stderr
, "warning: Circular reference detected in key '%s' of bag '%s'\n",
3701 String8(e
->getName()).string(),
3702 String8(e
->getBag().keyAt(i
)).string());
3705 item
->evaluating
= true;
3706 res
= stringToValue(outValue
, NULL
, item
->value
, false, false, item
->bagKeyId
);
3709 printf("getItemValue of #%08x[#%08x] (%s): type=#%08x, data=#%08x\n",
3710 resID
, attrID
, String8(getEntry(resID
)->getName()).string(),
3711 outValue
->dataType
, outValue
->data
);
3713 printf("getItemValue of #%08x[#%08x]: failed\n",
3717 item
->evaluating
= false;