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_DENSITY
, config
.density
)) {
2478 if (!match(AXIS_TOUCHSCREEN
, config
.touchscreen
)) {
2481 if (!match(AXIS_KEYSHIDDEN
, config
.inputFlags
)) {
2484 if (!match(AXIS_KEYBOARD
, config
.keyboard
)) {
2487 if (!match(AXIS_NAVIGATION
, config
.navigation
)) {
2490 if (!match(AXIS_SCREENSIZE
, config
.screenSize
)) {
2493 if (!match(AXIS_VERSION
, config
.version
)) {
2499 status_t
ResourceTable::flatten(Bundle
* bundle
, const sp
<AaptFile
>& dest
)
2501 ResourceFilter filter
;
2502 status_t err
= filter
.parse(bundle
->getConfigurations());
2503 if (err
!= NO_ERROR
) {
2507 const size_t N
= mOrderedPackages
.size();
2510 // Iterate through all data, collecting all values (strings,
2511 // references, etc).
2512 StringPool valueStrings
= StringPool(false, bundle
->getUTF8());
2513 for (pi
=0; pi
<N
; pi
++) {
2514 sp
<Package
> p
= mOrderedPackages
.itemAt(pi
);
2515 if (p
->getTypes().size() == 0) {
2520 StringPool typeStrings
= StringPool(false, bundle
->getUTF8());
2521 StringPool keyStrings
= StringPool(false, bundle
->getUTF8());
2523 const size_t N
= p
->getOrderedTypes().size();
2524 for (size_t ti
=0; ti
<N
; ti
++) {
2525 sp
<Type
> t
= p
->getOrderedTypes().itemAt(ti
);
2527 typeStrings
.add(String16("<empty>"), false);
2530 typeStrings
.add(t
->getName(), false);
2532 const size_t N
= t
->getOrderedConfigs().size();
2533 for (size_t ci
=0; ci
<N
; ci
++) {
2534 sp
<ConfigList
> c
= t
->getOrderedConfigs().itemAt(ci
);
2538 const size_t N
= c
->getEntries().size();
2539 for (size_t ei
=0; ei
<N
; ei
++) {
2540 ConfigDescription config
= c
->getEntries().keyAt(ei
);
2541 if (!filter
.match(config
)) {
2544 sp
<Entry
> e
= c
->getEntries().valueAt(ei
);
2548 e
->setNameIndex(keyStrings
.add(e
->getName(), true));
2549 status_t err
= e
->prepareFlatten(&valueStrings
, this);
2550 if (err
!= NO_ERROR
) {
2557 p
->setTypeStrings(typeStrings
.createStringBlock());
2558 p
->setKeyStrings(keyStrings
.createStringBlock());
2563 // Now build the array of package chunks.
2564 Vector
<sp
<AaptFile
> > flatPackages
;
2565 for (pi
=0; pi
<N
; pi
++) {
2566 sp
<Package
> p
= mOrderedPackages
.itemAt(pi
);
2567 if (p
->getTypes().size() == 0) {
2572 const size_t N
= p
->getTypeStrings().size();
2574 const size_t baseSize
= sizeof(ResTable_package
);
2576 // Start the package data.
2577 sp
<AaptFile
> data
= new AaptFile(String8(), AaptGroupEntry(), String8());
2578 ResTable_package
* header
= (ResTable_package
*)data
->editData(baseSize
);
2579 if (header
== NULL
) {
2580 fprintf(stderr
, "ERROR: out of memory creating ResTable_package\n");
2583 memset(header
, 0, sizeof(*header
));
2584 header
->header
.type
= htods(RES_TABLE_PACKAGE_TYPE
);
2585 header
->header
.headerSize
= htods(sizeof(*header
));
2586 header
->id
= htodl(p
->getAssignedId());
2587 strcpy16_htod(header
->name
, p
->getName().string());
2589 // Write the string blocks.
2590 const size_t typeStringsStart
= data
->getSize();
2591 sp
<AaptFile
> strFile
= p
->getTypeStringsData();
2592 ssize_t amt
= data
->writeData(strFile
->getData(), strFile
->getSize());
2593 #if PRINT_STRING_METRICS
2594 fprintf(stderr
, "**** type strings: %d\n", amt
);
2600 const size_t keyStringsStart
= data
->getSize();
2601 strFile
= p
->getKeyStringsData();
2602 amt
= data
->writeData(strFile
->getData(), strFile
->getSize());
2603 #if PRINT_STRING_METRICS
2604 fprintf(stderr
, "**** key strings: %d\n", amt
);
2611 // Build the type chunks inside of this package.
2612 for (size_t ti
=0; ti
<N
; ti
++) {
2613 // Retrieve them in the same order as the type string block.
2615 String16
typeName(p
->getTypeStrings().stringAt(ti
, &len
));
2616 sp
<Type
> t
= p
->getTypes().valueFor(typeName
);
2617 LOG_ALWAYS_FATAL_IF(t
== NULL
&& typeName
!= String16("<empty>"),
2618 "Type name %s not found",
2619 String8(typeName
).string());
2621 const size_t N
= t
!= NULL
? t
->getOrderedConfigs().size() : 0;
2623 // First write the typeSpec chunk, containing information about
2624 // each resource entry in this type.
2626 const size_t typeSpecSize
= sizeof(ResTable_typeSpec
) + sizeof(uint32_t)*N
;
2627 const size_t typeSpecStart
= data
->getSize();
2628 ResTable_typeSpec
* tsHeader
= (ResTable_typeSpec
*)
2629 (((uint8_t*)data
->editData(typeSpecStart
+typeSpecSize
)) + typeSpecStart
);
2630 if (tsHeader
== NULL
) {
2631 fprintf(stderr
, "ERROR: out of memory creating ResTable_typeSpec\n");
2634 memset(tsHeader
, 0, sizeof(*tsHeader
));
2635 tsHeader
->header
.type
= htods(RES_TABLE_TYPE_SPEC_TYPE
);
2636 tsHeader
->header
.headerSize
= htods(sizeof(*tsHeader
));
2637 tsHeader
->header
.size
= htodl(typeSpecSize
);
2638 tsHeader
->id
= ti
+1;
2639 tsHeader
->entryCount
= htodl(N
);
2641 uint32_t* typeSpecFlags
= (uint32_t*)
2642 (((uint8_t*)data
->editData())
2643 + typeSpecStart
+ sizeof(ResTable_typeSpec
));
2644 memset(typeSpecFlags
, 0, sizeof(uint32_t)*N
);
2646 for (size_t ei
=0; ei
<N
; ei
++) {
2647 sp
<ConfigList
> cl
= t
->getOrderedConfigs().itemAt(ei
);
2648 if (cl
->getPublic()) {
2649 typeSpecFlags
[ei
] |= htodl(ResTable_typeSpec::SPEC_PUBLIC
);
2651 const size_t CN
= cl
->getEntries().size();
2652 for (size_t ci
=0; ci
<CN
; ci
++) {
2653 if (!filter
.match(cl
->getEntries().keyAt(ci
))) {
2656 for (size_t cj
=ci
+1; cj
<CN
; cj
++) {
2657 if (!filter
.match(cl
->getEntries().keyAt(cj
))) {
2660 typeSpecFlags
[ei
] |= htodl(
2661 cl
->getEntries().keyAt(ci
).diff(cl
->getEntries().keyAt(cj
)));
2667 // We need to write one type chunk for each configuration for
2668 // which we have entries in this type.
2669 const size_t NC
= t
->getUniqueConfigs().size();
2671 const size_t typeSize
= sizeof(ResTable_type
) + sizeof(uint32_t)*N
;
2673 for (size_t ci
=0; ci
<NC
; ci
++) {
2674 ConfigDescription config
= t
->getUniqueConfigs().itemAt(ci
);
2676 NOISY(printf("Writing config %d config: imsi:%d/%d lang:%c%c cnt:%c%c "
2677 "orien:%d touch:%d density:%d key:%d inp:%d nav:%d w:%d h:%d\n",
2679 config
.mcc
, config
.mnc
,
2680 config
.language
[0] ? config
.language
[0] : '-',
2681 config
.language
[1] ? config
.language
[1] : '-',
2682 config
.country
[0] ? config
.country
[0] : '-',
2683 config
.country
[1] ? config
.country
[1] : '-',
2691 config
.screenHeight
));
2693 if (!filter
.match(config
)) {
2697 const size_t typeStart
= data
->getSize();
2699 ResTable_type
* tHeader
= (ResTable_type
*)
2700 (((uint8_t*)data
->editData(typeStart
+typeSize
)) + typeStart
);
2701 if (tHeader
== NULL
) {
2702 fprintf(stderr
, "ERROR: out of memory creating ResTable_type\n");
2706 memset(tHeader
, 0, sizeof(*tHeader
));
2707 tHeader
->header
.type
= htods(RES_TABLE_TYPE_TYPE
);
2708 tHeader
->header
.headerSize
= htods(sizeof(*tHeader
));
2710 tHeader
->entryCount
= htodl(N
);
2711 tHeader
->entriesStart
= htodl(typeSize
);
2712 tHeader
->config
= config
;
2713 NOISY(printf("Writing type %d config: imsi:%d/%d lang:%c%c cnt:%c%c "
2714 "orien:%d touch:%d density:%d key:%d inp:%d nav:%d w:%d h:%d\n",
2716 tHeader
->config
.mcc
, tHeader
->config
.mnc
,
2717 tHeader
->config
.language
[0] ? tHeader
->config
.language
[0] : '-',
2718 tHeader
->config
.language
[1] ? tHeader
->config
.language
[1] : '-',
2719 tHeader
->config
.country
[0] ? tHeader
->config
.country
[0] : '-',
2720 tHeader
->config
.country
[1] ? tHeader
->config
.country
[1] : '-',
2721 tHeader
->config
.orientation
,
2722 tHeader
->config
.touchscreen
,
2723 tHeader
->config
.density
,
2724 tHeader
->config
.keyboard
,
2725 tHeader
->config
.inputFlags
,
2726 tHeader
->config
.navigation
,
2727 tHeader
->config
.screenWidth
,
2728 tHeader
->config
.screenHeight
));
2729 tHeader
->config
.swapHtoD();
2731 // Build the entries inside of this type.
2732 for (size_t ei
=0; ei
<N
; ei
++) {
2733 sp
<ConfigList
> cl
= t
->getOrderedConfigs().itemAt(ei
);
2734 sp
<Entry
> e
= cl
->getEntries().valueFor(config
);
2736 // Set the offset for this entry in its type.
2737 uint32_t* index
= (uint32_t*)
2738 (((uint8_t*)data
->editData())
2739 + typeStart
+ sizeof(ResTable_type
));
2741 index
[ei
] = htodl(data
->getSize()-typeStart
-typeSize
);
2743 // Create the entry.
2744 ssize_t amt
= e
->flatten(bundle
, data
, cl
->getPublic());
2749 index
[ei
] = htodl(ResTable_type::NO_ENTRY
);
2753 // Fill in the rest of the type information.
2754 tHeader
= (ResTable_type
*)
2755 (((uint8_t*)data
->editData()) + typeStart
);
2756 tHeader
->header
.size
= htodl(data
->getSize()-typeStart
);
2760 // Fill in the rest of the package information.
2761 header
= (ResTable_package
*)data
->editData();
2762 header
->header
.size
= htodl(data
->getSize());
2763 header
->typeStrings
= htodl(typeStringsStart
);
2764 header
->lastPublicType
= htodl(p
->getTypeStrings().size());
2765 header
->keyStrings
= htodl(keyStringsStart
);
2766 header
->lastPublicKey
= htodl(p
->getKeyStrings().size());
2768 flatPackages
.add(data
);
2771 // And now write out the final chunks.
2772 const size_t dataStart
= dest
->getSize();
2776 ResTable_header header
;
2777 memset(&header
, 0, sizeof(header
));
2778 header
.header
.type
= htods(RES_TABLE_TYPE
);
2779 header
.header
.headerSize
= htods(sizeof(header
));
2780 header
.packageCount
= htodl(flatPackages
.size());
2781 status_t err
= dest
->writeData(&header
, sizeof(header
));
2782 if (err
!= NO_ERROR
) {
2783 fprintf(stderr
, "ERROR: out of memory creating ResTable_header\n");
2788 ssize_t strStart
= dest
->getSize();
2789 err
= valueStrings
.writeStringBlock(dest
);
2790 if (err
!= NO_ERROR
) {
2794 ssize_t amt
= (dest
->getSize()-strStart
);
2796 #if PRINT_STRING_METRICS
2797 fprintf(stderr
, "**** value strings: %d\n", amt
);
2798 fprintf(stderr
, "**** total strings: %d\n", strAmt
);
2801 for (pi
=0; pi
<flatPackages
.size(); pi
++) {
2802 err
= dest
->writeData(flatPackages
[pi
]->getData(),
2803 flatPackages
[pi
]->getSize());
2804 if (err
!= NO_ERROR
) {
2805 fprintf(stderr
, "ERROR: out of memory creating package chunk for ResTable_header\n");
2810 ResTable_header
* header
= (ResTable_header
*)
2811 (((uint8_t*)dest
->getData()) + dataStart
);
2812 header
->header
.size
= htodl(dest
->getSize() - dataStart
);
2814 NOISY(aout
<< "Resource table:"
2815 << HexDump(dest
->getData(), dest
->getSize()) << endl
);
2817 #if PRINT_STRING_METRICS
2818 fprintf(stderr
, "**** total resource table size: %d / %d%% strings\n",
2819 dest
->getSize(), (strAmt
*100)/dest
->getSize());
2825 void ResourceTable::writePublicDefinitions(const String16
& package
, FILE* fp
)
2828 "<!-- This file contains <public> resource definitions for all\n"
2829 " resources that were generated from the source data. -->\n"
2833 writePublicDefinitions(package
, fp
, true);
2834 writePublicDefinitions(package
, fp
, false);
2841 void ResourceTable::writePublicDefinitions(const String16
& package
, FILE* fp
, bool pub
)
2843 bool didHeader
= false;
2845 sp
<Package
> pkg
= mPackages
.valueFor(package
);
2847 const size_t NT
= pkg
->getOrderedTypes().size();
2848 for (size_t i
=0; i
<NT
; i
++) {
2849 sp
<Type
> t
= pkg
->getOrderedTypes().itemAt(i
);
2854 bool didType
= false;
2856 const size_t NC
= t
->getOrderedConfigs().size();
2857 for (size_t j
=0; j
<NC
; j
++) {
2858 sp
<ConfigList
> c
= t
->getOrderedConfigs().itemAt(j
);
2863 if (c
->getPublic() != pub
) {
2873 fprintf(fp
," <!-- PUBLIC SECTION. These resources have been declared public.\n");
2874 fprintf(fp
," Changes to these definitions will break binary compatibility. -->\n\n");
2876 fprintf(fp
," <!-- PRIVATE SECTION. These resources have not been declared public.\n");
2877 fprintf(fp
," You can make them public my moving these lines into a file in res/values. -->\n\n");
2882 const size_t NE
= c
->getEntries().size();
2883 for (size_t k
=0; k
<NE
; k
++) {
2884 const SourcePos
& pos
= c
->getEntries().valueAt(k
)->getPos();
2885 if (pos
.file
!= "") {
2886 fprintf(fp
," <!-- Declared at %s:%d -->\n",
2887 pos
.file
.string(), pos
.line
);
2891 fprintf(fp
, " <public type=\"%s\" name=\"%s\" id=\"0x%08x\" />\n",
2892 String8(t
->getName()).string(),
2893 String8(c
->getName()).string(),
2894 getResId(pkg
, t
, c
->getEntryIndex()));
2900 ResourceTable::Item::Item(const SourcePos
& _sourcePos
,
2902 const String16
& _value
,
2903 const Vector
<StringPool::entry_style_span
>* _style
,
2905 : sourcePos(_sourcePos
)
2917 status_t
ResourceTable::Entry::makeItABag(const SourcePos
& sourcePos
)
2919 if (mType
== TYPE_BAG
) {
2922 if (mType
== TYPE_UNKNOWN
) {
2926 sourcePos
.error("Resource entry %s is already defined as a single item.\n"
2927 "%s:%d: Originally defined here.\n",
2928 String8(mName
).string(),
2929 mItem
.sourcePos
.file
.string(), mItem
.sourcePos
.line
);
2930 return UNKNOWN_ERROR
;
2933 status_t
ResourceTable::Entry::setItem(const SourcePos
& sourcePos
,
2934 const String16
& value
,
2935 const Vector
<StringPool::entry_style_span
>* style
,
2937 const bool overwrite
)
2939 Item
item(sourcePos
, false, value
, style
);
2941 if (mType
== TYPE_BAG
) {
2942 const Item
& item(mBag
.valueAt(0));
2943 sourcePos
.error("Resource entry %s is already defined as a bag.\n"
2944 "%s:%d: Originally defined here.\n",
2945 String8(mName
).string(),
2946 item
.sourcePos
.file
.string(), item
.sourcePos
.line
);
2947 return UNKNOWN_ERROR
;
2949 if ( (mType
!= TYPE_UNKNOWN
) && (overwrite
== false) ) {
2950 sourcePos
.error("Resource entry %s is already defined.\n"
2951 "%s:%d: Originally defined here.\n",
2952 String8(mName
).string(),
2953 mItem
.sourcePos
.file
.string(), mItem
.sourcePos
.line
);
2954 return UNKNOWN_ERROR
;
2959 mItemFormat
= format
;
2963 status_t
ResourceTable::Entry::addToBag(const SourcePos
& sourcePos
,
2964 const String16
& key
, const String16
& value
,
2965 const Vector
<StringPool::entry_style_span
>* style
,
2966 bool replace
, bool isId
, int32_t format
)
2968 status_t err
= makeItABag(sourcePos
);
2969 if (err
!= NO_ERROR
) {
2973 Item
item(sourcePos
, isId
, value
, style
, format
);
2975 // XXX NOTE: there is an error if you try to have a bag with two keys,
2976 // one an attr and one an id, with the same name. Not something we
2977 // currently ever have to worry about.
2978 ssize_t origKey
= mBag
.indexOfKey(key
);
2981 const Item
& item(mBag
.valueAt(origKey
));
2982 sourcePos
.error("Resource entry %s already has bag item %s.\n"
2983 "%s:%d: Originally defined here.\n",
2984 String8(mName
).string(), String8(key
).string(),
2985 item
.sourcePos
.file
.string(), item
.sourcePos
.line
);
2986 return UNKNOWN_ERROR
;
2988 //printf("Replacing %s with %s\n",
2989 // String8(mBag.valueFor(key).value).string(), String8(value).string());
2990 mBag
.replaceValueFor(key
, item
);
2993 mBag
.add(key
, item
);
2997 status_t
ResourceTable::Entry::emptyBag(const SourcePos
& sourcePos
)
2999 status_t err
= makeItABag(sourcePos
);
3000 if (err
!= NO_ERROR
) {
3008 status_t
ResourceTable::Entry::generateAttributes(ResourceTable
* table
,
3009 const String16
& package
)
3011 const String16
attr16("attr");
3012 const String16
id16("id");
3013 const size_t N
= mBag
.size();
3014 for (size_t i
=0; i
<N
; i
++) {
3015 const String16
& key
= mBag
.keyAt(i
);
3016 const Item
& it
= mBag
.valueAt(i
);
3018 if (!table
->hasBagOrEntry(key
, &id16
, &package
)) {
3019 String16
value("false");
3020 status_t err
= table
->addEntry(SourcePos(String8("<generated>"), 0), package
,
3022 if (err
!= NO_ERROR
) {
3026 } else if (!table
->hasBagOrEntry(key
, &attr16
, &package
)) {
3029 // fprintf(stderr, "ERROR: Bag attribute '%s' has not been defined.\n",
3030 // String8(key).string());
3031 // const Item& item(mBag.valueAt(i));
3032 // fprintf(stderr, "Referenced from file %s line %d\n",
3033 // item.sourcePos.file.string(), item.sourcePos.line);
3034 // return UNKNOWN_ERROR;
3037 sprintf(numberStr
, "%d", ResTable_map::TYPE_ANY
);
3038 status_t err
= table
->addBag(SourcePos("<generated>", 0), package
,
3039 attr16
, key
, String16(""),
3041 String16(numberStr
), NULL
, NULL
);
3042 if (err
!= NO_ERROR
) {
3051 status_t
ResourceTable::Entry::assignResourceIds(ResourceTable
* table
,
3052 const String16
& package
)
3054 bool hasErrors
= false;
3056 if (mType
== TYPE_BAG
) {
3057 const char* errorMsg
;
3058 const String16
style16("style");
3059 const String16
attr16("attr");
3060 const String16
id16("id");
3062 if (mParent
.size() > 0) {
3063 mParentId
= table
->getResId(mParent
, &style16
, NULL
, &errorMsg
);
3064 if (mParentId
== 0) {
3065 mPos
.error("Error retrieving parent for item: %s '%s'.\n",
3066 errorMsg
, String8(mParent
).string());
3070 const size_t N
= mBag
.size();
3071 for (size_t i
=0; i
<N
; i
++) {
3072 const String16
& key
= mBag
.keyAt(i
);
3073 Item
& it
= mBag
.editValueAt(i
);
3074 it
.bagKeyId
= table
->getResId(key
,
3075 it
.isId
? &id16
: &attr16
, NULL
, &errorMsg
);
3076 //printf("Bag key of %s: #%08x\n", String8(key).string(), it.bagKeyId);
3077 if (it
.bagKeyId
== 0) {
3078 it
.sourcePos
.error("Error: %s: %s '%s'.\n", errorMsg
,
3079 String8(it
.isId
? id16
: attr16
).string(),
3080 String8(key
).string());
3085 return hasErrors
? UNKNOWN_ERROR
: NO_ERROR
;
3088 status_t
ResourceTable::Entry::prepareFlatten(StringPool
* strings
, ResourceTable
* table
)
3090 if (mType
== TYPE_ITEM
) {
3092 AccessorCookie
ac(it
.sourcePos
, String8(mName
), String8(it
.value
));
3093 if (!table
->stringToValue(&it
.parsedValue
, strings
,
3094 it
.value
, false, true, 0,
3095 &it
.style
, NULL
, &ac
, mItemFormat
)) {
3096 return UNKNOWN_ERROR
;
3098 } else if (mType
== TYPE_BAG
) {
3099 const size_t N
= mBag
.size();
3100 for (size_t i
=0; i
<N
; i
++) {
3101 const String16
& key
= mBag
.keyAt(i
);
3102 Item
& it
= mBag
.editValueAt(i
);
3103 AccessorCookie
ac(it
.sourcePos
, String8(key
), String8(it
.value
));
3104 if (!table
->stringToValue(&it
.parsedValue
, strings
,
3105 it
.value
, false, true, it
.bagKeyId
,
3106 &it
.style
, NULL
, &ac
, it
.format
)) {
3107 return UNKNOWN_ERROR
;
3111 mPos
.error("Error: entry %s is not a single item or a bag.\n",
3112 String8(mName
).string());
3113 return UNKNOWN_ERROR
;
3118 ssize_t
ResourceTable::Entry::flatten(Bundle
* bundle
, const sp
<AaptFile
>& data
, bool isPublic
)
3121 ResTable_entry header
;
3122 memset(&header
, 0, sizeof(header
));
3123 header
.size
= htods(sizeof(header
));
3124 const type ty
= this != NULL
? mType
: TYPE_ITEM
;
3126 if (ty
== TYPE_BAG
) {
3127 header
.flags
|= htods(header
.FLAG_COMPLEX
);
3130 header
.flags
|= htods(header
.FLAG_PUBLIC
);
3132 header
.key
.index
= htodl(mNameIndex
);
3134 if (ty
!= TYPE_BAG
) {
3135 status_t err
= data
->writeData(&header
, sizeof(header
));
3136 if (err
!= NO_ERROR
) {
3137 fprintf(stderr
, "ERROR: out of memory creating ResTable_entry\n");
3141 const Item
& it
= mItem
;
3143 memset(&par
, 0, sizeof(par
));
3144 par
.size
= htods(it
.parsedValue
.size
);
3145 par
.dataType
= it
.parsedValue
.dataType
;
3146 par
.res0
= it
.parsedValue
.res0
;
3147 par
.data
= htodl(it
.parsedValue
.data
);
3149 printf("Writing item (%s): type=%d, data=0x%x, res0=0x%x\n",
3150 String8(mName
).string(), it
.parsedValue
.dataType
,
3151 it
.parsedValue
.data
, par
.res0
);
3153 err
= data
->writeData(&par
, it
.parsedValue
.size
);
3154 if (err
!= NO_ERROR
) {
3155 fprintf(stderr
, "ERROR: out of memory creating Res_value\n");
3158 amt
+= it
.parsedValue
.size
;
3160 size_t N
= mBag
.size();
3162 // Create correct ordering of items.
3163 KeyedVector
<uint32_t, const Item
*> items
;
3164 for (i
=0; i
<N
; i
++) {
3165 const Item
& it
= mBag
.valueAt(i
);
3166 items
.add(it
.bagKeyId
, &it
);
3170 ResTable_map_entry mapHeader
;
3171 memcpy(&mapHeader
, &header
, sizeof(header
));
3172 mapHeader
.size
= htods(sizeof(mapHeader
));
3173 mapHeader
.parent
.ident
= htodl(mParentId
);
3174 mapHeader
.count
= htodl(N
);
3175 status_t err
= data
->writeData(&mapHeader
, sizeof(mapHeader
));
3176 if (err
!= NO_ERROR
) {
3177 fprintf(stderr
, "ERROR: out of memory creating ResTable_entry\n");
3181 for (i
=0; i
<N
; i
++) {
3182 const Item
& it
= *items
.valueAt(i
);
3184 map
.name
.ident
= htodl(it
.bagKeyId
);
3185 map
.value
.size
= htods(it
.parsedValue
.size
);
3186 map
.value
.dataType
= it
.parsedValue
.dataType
;
3187 map
.value
.res0
= it
.parsedValue
.res0
;
3188 map
.value
.data
= htodl(it
.parsedValue
.data
);
3189 err
= data
->writeData(&map
, sizeof(map
));
3190 if (err
!= NO_ERROR
) {
3191 fprintf(stderr
, "ERROR: out of memory creating Res_value\n");
3200 void ResourceTable::ConfigList::appendComment(const String16
& comment
,
3203 if (comment
.size() <= 0) {
3206 if (onlyIfEmpty
&& mComment
.size() > 0) {
3209 if (mComment
.size() > 0) {
3210 mComment
.append(String16("\n"));
3212 mComment
.append(comment
);
3215 void ResourceTable::ConfigList::appendTypeComment(const String16
& comment
)
3217 if (comment
.size() <= 0) {
3220 if (mTypeComment
.size() > 0) {
3221 mTypeComment
.append(String16("\n"));
3223 mTypeComment
.append(comment
);
3226 status_t
ResourceTable::Type::addPublic(const SourcePos
& sourcePos
,
3227 const String16
& name
,
3228 const uint32_t ident
)
3231 int32_t entryIdx
= Res_GETENTRY(ident
);
3233 sourcePos
.error("Public resource %s/%s has an invalid 0 identifier (0x%08x).\n",
3234 String8(mName
).string(), String8(name
).string(), ident
);
3235 return UNKNOWN_ERROR
;
3239 int32_t typeIdx
= Res_GETTYPE(ident
);
3242 if (mPublicIndex
> 0 && mPublicIndex
!= typeIdx
) {
3243 sourcePos
.error("Public resource %s/%s has conflicting type codes for its"
3244 " public identifiers (0x%x vs 0x%x).\n",
3245 String8(mName
).string(), String8(name
).string(),
3246 mPublicIndex
, typeIdx
);
3247 return UNKNOWN_ERROR
;
3249 mPublicIndex
= typeIdx
;
3252 if (mFirstPublicSourcePos
== NULL
) {
3253 mFirstPublicSourcePos
= new SourcePos(sourcePos
);
3256 if (mPublic
.indexOfKey(name
) < 0) {
3257 mPublic
.add(name
, Public(sourcePos
, String16(), ident
));
3259 Public
& p
= mPublic
.editValueFor(name
);
3260 if (p
.ident
!= ident
) {
3261 sourcePos
.error("Public resource %s/%s has conflicting public identifiers"
3262 " (0x%08x vs 0x%08x).\n"
3263 "%s:%d: Originally defined here.\n",
3264 String8(mName
).string(), String8(name
).string(), p
.ident
, ident
,
3265 p
.sourcePos
.file
.string(), p
.sourcePos
.line
);
3266 return UNKNOWN_ERROR
;
3273 void ResourceTable::Type::canAddEntry(const String16
& name
)
3275 mCanAddEntries
.add(name
);
3278 sp
<ResourceTable::Entry
> ResourceTable::Type::getEntry(const String16
& entry
,
3279 const SourcePos
& sourcePos
,
3280 const ResTable_config
* config
,
3283 bool autoAddOverlay
)
3286 sp
<ConfigList
> c
= mConfigs
.valueFor(entry
);
3288 if (overlay
&& !autoAddOverlay
&& mCanAddEntries
.indexOf(entry
) < 0) {
3289 sourcePos
.error("Resource at %s appears in overlay but not"
3290 " in the base package; use <add-resource> to add.\n",
3291 String8(entry
).string());
3294 c
= new ConfigList(entry
, sourcePos
);
3295 mConfigs
.add(entry
, c
);
3296 pos
= (int)mOrderedConfigs
.size();
3297 mOrderedConfigs
.add(c
);
3299 c
->setEntryIndex(pos
);
3303 ConfigDescription cdesc
;
3304 if (config
) cdesc
= *config
;
3306 sp
<Entry
> e
= c
->getEntries().valueFor(cdesc
);
3308 if (config
!= NULL
) {
3309 NOISY(printf("New entry at %s:%d: imsi:%d/%d lang:%c%c cnt:%c%c "
3310 "orien:%d touch:%d density:%d key:%d inp:%d nav:%d w:%d h:%d\n",
3311 sourcePos
.file
.string(), sourcePos
.line
,
3312 config
->mcc
, config
->mnc
,
3313 config
->language
[0] ? config
->language
[0] : '-',
3314 config
->language
[1] ? config
->language
[1] : '-',
3315 config
->country
[0] ? config
->country
[0] : '-',
3316 config
->country
[1] ? config
->country
[1] : '-',
3317 config
->orientation
,
3318 config
->touchscreen
,
3323 config
->screenWidth
,
3324 config
->screenHeight
));
3326 NOISY(printf("New entry at %s:%d: NULL config\n",
3327 sourcePos
.file
.string(), sourcePos
.line
));
3329 e
= new Entry(entry
, sourcePos
);
3330 c
->addEntry(cdesc
, e
);
3334 for (pos=0; pos<(int)mOrderedConfigs.size(); pos++) {
3335 if (mOrderedConfigs[pos] == c) {
3339 if (pos >= (int)mOrderedConfigs.size()) {
3340 sourcePos.error("Internal error: config not found in mOrderedConfigs when adding entry");
3344 e->setEntryIndex(pos);
3349 mUniqueConfigs
.add(cdesc
);
3354 status_t
ResourceTable::Type::applyPublicEntryOrder()
3356 size_t N
= mOrderedConfigs
.size();
3357 Vector
<sp
<ConfigList
> > origOrder(mOrderedConfigs
);
3358 bool hasError
= false;
3361 for (i
=0; i
<N
; i
++) {
3362 mOrderedConfigs
.replaceAt(NULL
, i
);
3365 const size_t NP
= mPublic
.size();
3366 //printf("Ordering %d configs from %d public defs\n", N, NP);
3368 for (j
=0; j
<NP
; j
++) {
3369 const String16
& name
= mPublic
.keyAt(j
);
3370 const Public
& p
= mPublic
.valueAt(j
);
3371 int32_t idx
= Res_GETENTRY(p
.ident
);
3372 //printf("Looking for entry \"%s\"/\"%s\" (0x%08x) in %d...\n",
3373 // String8(mName).string(), String8(name).string(), p.ident, N);
3375 for (i
=0; i
<N
; i
++) {
3376 sp
<ConfigList
> e
= origOrder
.itemAt(i
);
3377 //printf("#%d: \"%s\"\n", i, String8(e->getName()).string());
3378 if (e
->getName() == name
) {
3379 if (idx
>= (int32_t)mOrderedConfigs
.size()) {
3380 p
.sourcePos
.error("Public entry identifier 0x%x entry index "
3381 "is larger than available symbols (index %d, total symbols %d).\n",
3382 p
.ident
, idx
, mOrderedConfigs
.size());
3384 } else if (mOrderedConfigs
.itemAt(idx
) == NULL
) {
3386 e
->setPublicSourcePos(p
.sourcePos
);
3387 mOrderedConfigs
.replaceAt(e
, idx
);
3388 origOrder
.removeAt(i
);
3393 sp
<ConfigList
> oe
= mOrderedConfigs
.itemAt(idx
);
3395 p
.sourcePos
.error("Multiple entry names declared for public entry"
3396 " identifier 0x%x in type %s (%s vs %s).\n"
3397 "%s:%d: Originally defined here.",
3398 idx
+1, String8(mName
).string(),
3399 String8(oe
->getName()).string(),
3400 String8(name
).string(),
3401 oe
->getPublicSourcePos().file
.string(),
3402 oe
->getPublicSourcePos().line
);
3409 p
.sourcePos
.error("Public symbol %s/%s declared here is not defined.",
3410 String8(mName
).string(), String8(name
).string());
3415 //printf("Copying back in %d non-public configs, have %d\n", N, origOrder.size());
3417 if (N
!= origOrder
.size()) {
3418 printf("Internal error: remaining private symbol count mismatch\n");
3419 N
= origOrder
.size();
3423 for (i
=0; i
<N
; i
++) {
3424 sp
<ConfigList
> e
= origOrder
.itemAt(i
);
3425 // There will always be enough room for the remaining entries.
3426 while (mOrderedConfigs
.itemAt(j
) != NULL
) {
3429 mOrderedConfigs
.replaceAt(e
, j
);
3433 return hasError
? UNKNOWN_ERROR
: NO_ERROR
;
3436 ResourceTable::Package::Package(const String16
& name
, ssize_t includedId
)
3437 : mName(name
), mIncludedId(includedId
),
3438 mTypeStringsMapping(0xffffffff),
3439 mKeyStringsMapping(0xffffffff)
3443 sp
<ResourceTable::Type
> ResourceTable::Package::getType(const String16
& type
,
3444 const SourcePos
& sourcePos
,
3447 sp
<Type
> t
= mTypes
.valueFor(type
);
3449 t
= new Type(type
, sourcePos
);
3450 mTypes
.add(type
, t
);
3451 mOrderedTypes
.add(t
);
3453 // For some reason the type's index is set to one plus the index
3454 // in the mOrderedTypes list, rather than just the index.
3455 t
->setIndex(mOrderedTypes
.size());
3461 status_t
ResourceTable::Package::setTypeStrings(const sp
<AaptFile
>& data
)
3463 mTypeStringsData
= data
;
3464 status_t err
= setStrings(data
, &mTypeStrings
, &mTypeStringsMapping
);
3465 if (err
!= NO_ERROR
) {
3466 fprintf(stderr
, "ERROR: Type string data is corrupt!\n");
3471 status_t
ResourceTable::Package::setKeyStrings(const sp
<AaptFile
>& data
)
3473 mKeyStringsData
= data
;
3474 status_t err
= setStrings(data
, &mKeyStrings
, &mKeyStringsMapping
);
3475 if (err
!= NO_ERROR
) {
3476 fprintf(stderr
, "ERROR: Key string data is corrupt!\n");
3481 status_t
ResourceTable::Package::setStrings(const sp
<AaptFile
>& data
,
3482 ResStringPool
* strings
,
3483 DefaultKeyedVector
<String16
, uint32_t>* mappings
)
3485 if (data
->getData() == NULL
) {
3486 return UNKNOWN_ERROR
;
3489 NOISY(aout
<< "Setting restable string pool: "
3490 << HexDump(data
->getData(), data
->getSize()) << endl
);
3492 status_t err
= strings
->setTo(data
->getData(), data
->getSize());
3493 if (err
== NO_ERROR
) {
3494 const size_t N
= strings
->size();
3495 for (size_t i
=0; i
<N
; i
++) {
3497 mappings
->add(String16(strings
->stringAt(i
, &len
)), i
);
3503 status_t
ResourceTable::Package::applyPublicTypeOrder()
3505 size_t N
= mOrderedTypes
.size();
3506 Vector
<sp
<Type
> > origOrder(mOrderedTypes
);
3509 for (i
=0; i
<N
; i
++) {
3510 mOrderedTypes
.replaceAt(NULL
, i
);
3513 for (i
=0; i
<N
; i
++) {
3514 sp
<Type
> t
= origOrder
.itemAt(i
);
3515 int32_t idx
= t
->getPublicIndex();
3518 while (idx
>= (int32_t)mOrderedTypes
.size()) {
3519 mOrderedTypes
.add();
3521 if (mOrderedTypes
.itemAt(idx
) != NULL
) {
3522 sp
<Type
> ot
= mOrderedTypes
.itemAt(idx
);
3523 t
->getFirstPublicSourcePos().error("Multiple type names declared for public type"
3524 " identifier 0x%x (%s vs %s).\n"
3525 "%s:%d: Originally defined here.",
3526 idx
, String8(ot
->getName()).string(),
3527 String8(t
->getName()).string(),
3528 ot
->getFirstPublicSourcePos().file
.string(),
3529 ot
->getFirstPublicSourcePos().line
);
3530 return UNKNOWN_ERROR
;
3532 mOrderedTypes
.replaceAt(t
, idx
);
3533 origOrder
.removeAt(i
);
3540 for (i
=0; i
<N
; i
++) {
3541 sp
<Type
> t
= origOrder
.itemAt(i
);
3542 // There will always be enough room for the remaining types.
3543 while (mOrderedTypes
.itemAt(j
) != NULL
) {
3546 mOrderedTypes
.replaceAt(t
, j
);
3552 sp
<ResourceTable::Package
> ResourceTable::getPackage(const String16
& package
)
3554 sp
<Package
> p
= mPackages
.valueFor(package
);
3556 if (mIsAppPackage
) {
3557 if (mHaveAppPackage
) {
3558 fprintf(stderr
, "Adding multiple application package resources; only one is allowed.\n"
3559 "Use -x to create extended resources.\n");
3562 mHaveAppPackage
= true;
3563 p
= new Package(package
, 127);
3565 p
= new Package(package
, mNextPackageId
);
3567 //printf("*** NEW PACKAGE: \"%s\" id=%d\n",
3568 // String8(package).string(), p->getAssignedId());
3569 mPackages
.add(package
, p
);
3570 mOrderedPackages
.add(p
);
3576 sp
<ResourceTable::Type
> ResourceTable::getType(const String16
& package
,
3577 const String16
& type
,
3578 const SourcePos
& sourcePos
,
3581 sp
<Package
> p
= getPackage(package
);
3585 return p
->getType(type
, sourcePos
, doSetIndex
);
3588 sp
<ResourceTable::Entry
> ResourceTable::getEntry(const String16
& package
,
3589 const String16
& type
,
3590 const String16
& name
,
3591 const SourcePos
& sourcePos
,
3593 const ResTable_config
* config
,
3596 sp
<Type
> t
= getType(package
, type
, sourcePos
, doSetIndex
);
3600 return t
->getEntry(name
, sourcePos
, config
, doSetIndex
, overlay
, mBundle
->getAutoAddOverlay());
3603 sp
<const ResourceTable::Entry
> ResourceTable::getEntry(uint32_t resID
,
3604 const ResTable_config
* config
) const
3606 int pid
= Res_GETPACKAGE(resID
)+1;
3607 const size_t N
= mOrderedPackages
.size();
3610 for (i
=0; i
<N
; i
++) {
3611 sp
<Package
> check
= mOrderedPackages
[i
];
3612 if (check
->getAssignedId() == pid
) {
3619 fprintf(stderr
, "warning: Package not found for resource #%08x\n", resID
);
3623 int tid
= Res_GETTYPE(resID
);
3624 if (tid
< 0 || tid
>= (int)p
->getOrderedTypes().size()) {
3625 fprintf(stderr
, "warning: Type not found for resource #%08x\n", resID
);
3628 sp
<Type
> t
= p
->getOrderedTypes()[tid
];
3630 int eid
= Res_GETENTRY(resID
);
3631 if (eid
< 0 || eid
>= (int)t
->getOrderedConfigs().size()) {
3632 fprintf(stderr
, "warning: Entry not found for resource #%08x\n", resID
);
3636 sp
<ConfigList
> c
= t
->getOrderedConfigs()[eid
];
3638 fprintf(stderr
, "warning: Entry not found for resource #%08x\n", resID
);
3642 ConfigDescription cdesc
;
3643 if (config
) cdesc
= *config
;
3644 sp
<Entry
> e
= c
->getEntries().valueFor(cdesc
);
3646 fprintf(stderr
, "warning: Entry configuration not found for resource #%08x\n", resID
);
3653 const ResourceTable::Item
* ResourceTable::getItem(uint32_t resID
, uint32_t attrID
) const
3655 sp
<const Entry
> e
= getEntry(resID
);
3660 const size_t N
= e
->getBag().size();
3661 for (size_t i
=0; i
<N
; i
++) {
3662 const Item
& it
= e
->getBag().valueAt(i
);
3663 if (it
.bagKeyId
== 0) {
3664 fprintf(stderr
, "warning: ID not yet assigned to '%s' in bag '%s'\n",
3665 String8(e
->getName()).string(),
3666 String8(e
->getBag().keyAt(i
)).string());
3668 if (it
.bagKeyId
== attrID
) {
3676 bool ResourceTable::getItemValue(
3677 uint32_t resID
, uint32_t attrID
, Res_value
* outValue
)
3679 const Item
* item
= getItem(resID
, attrID
);
3683 if (item
->evaluating
) {
3684 sp
<const Entry
> e
= getEntry(resID
);
3685 const size_t N
= e
->getBag().size();
3687 for (i
=0; i
<N
; i
++) {
3688 if (&e
->getBag().valueAt(i
) == item
) {
3692 fprintf(stderr
, "warning: Circular reference detected in key '%s' of bag '%s'\n",
3693 String8(e
->getName()).string(),
3694 String8(e
->getBag().keyAt(i
)).string());
3697 item
->evaluating
= true;
3698 res
= stringToValue(outValue
, NULL
, item
->value
, false, false, item
->bagKeyId
);
3701 printf("getItemValue of #%08x[#%08x] (%s): type=#%08x, data=#%08x\n",
3702 resID
, attrID
, String8(getEntry(resID
)->getName()).string(),
3703 outValue
->dataType
, outValue
->data
);
3705 printf("getItemValue of #%08x[#%08x]: failed\n",
3709 item
->evaluating
= false;