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>
16 #define NOISY_REF(x) //x
18 status_t
compileXmlFile(const sp
<AaptAssets
>& assets
,
19 const sp
<AaptFile
>& target
,
23 sp
<XMLNode
> root
= XMLNode::parse(target
);
28 return compileXmlFile(assets
, root
, target
, table
, options
);
31 status_t
compileXmlFile(const sp
<AaptAssets
>& assets
,
32 const sp
<AaptFile
>& target
,
33 const sp
<AaptFile
>& outTarget
,
37 sp
<XMLNode
> root
= XMLNode::parse(target
);
42 return compileXmlFile(assets
, root
, outTarget
, table
, options
);
45 status_t
compileXmlFile(const sp
<AaptAssets
>& assets
,
46 const sp
<XMLNode
>& root
,
47 const sp
<AaptFile
>& target
,
51 if ((options
&XML_COMPILE_STRIP_WHITESPACE
) != 0) {
52 root
->removeWhitespace(true, NULL
);
53 } else if ((options
&XML_COMPILE_COMPACT_WHITESPACE
) != 0) {
54 root
->removeWhitespace(false, NULL
);
57 if ((options
&XML_COMPILE_UTF8
) != 0) {
61 bool hasErrors
= false;
63 if ((options
&XML_COMPILE_ASSIGN_ATTRIBUTE_IDS
) != 0) {
64 status_t err
= root
->assignResourceIds(assets
, table
);
65 if (err
!= NO_ERROR
) {
70 status_t err
= root
->parseValues(assets
, table
);
71 if (err
!= NO_ERROR
) {
79 NOISY(printf("Input XML Resource:\n"));
81 err
= root
->flatten(target
,
82 (options
&XML_COMPILE_STRIP_COMMENTS
) != 0,
83 (options
&XML_COMPILE_STRIP_RAW_VALUES
) != 0);
84 if (err
!= NO_ERROR
) {
88 NOISY(printf("Output XML Resource:\n"));
89 NOISY(ResXMLTree tree
;
90 tree
.setTo(target
->getData(), target
->getSize());
91 printXMLBlock(&tree
));
93 target
->setCompressionMethod(ZipEntry::kCompressDeflated
);
103 const char16_t* name
;
106 const char* description
;
109 static const char16_t referenceArray
[] =
110 { 'r', 'e', 'f', 'e', 'r', 'e', 'n', 'c', 'e' };
111 static const char16_t stringArray
[] =
112 { 's', 't', 'r', 'i', 'n', 'g' };
113 static const char16_t integerArray
[] =
114 { 'i', 'n', 't', 'e', 'g', 'e', 'r' };
115 static const char16_t booleanArray
[] =
116 { 'b', 'o', 'o', 'l', 'e', 'a', 'n' };
117 static const char16_t colorArray
[] =
118 { 'c', 'o', 'l', 'o', 'r' };
119 static const char16_t floatArray
[] =
120 { 'f', 'l', 'o', 'a', 't' };
121 static const char16_t dimensionArray
[] =
122 { 'd', 'i', 'm', 'e', 'n', 's', 'i', 'o', 'n' };
123 static const char16_t fractionArray
[] =
124 { 'f', 'r', 'a', 'c', 't', 'i', 'o', 'n' };
125 static const char16_t enumArray
[] =
126 { 'e', 'n', 'u', 'm' };
127 static const char16_t flagsArray
[] =
128 { 'f', 'l', 'a', 'g', 's' };
130 static const flag_entry gFormatFlags
[] = {
131 { referenceArray
, sizeof(referenceArray
)/2, ResTable_map::TYPE_REFERENCE
,
132 "a reference to another resource, in the form \"<code>@[+][<i>package</i>:]<i>type</i>:<i>name</i></code>\"\n"
133 "or to a theme attribute in the form \"<code>?[<i>package</i>:][<i>type</i>:]<i>name</i></code>\"."},
134 { stringArray
, sizeof(stringArray
)/2, ResTable_map::TYPE_STRING
,
135 "a string value, using '\\\\;' to escape characters such as '\\\\n' or '\\\\uxxxx' for a unicode character." },
136 { integerArray
, sizeof(integerArray
)/2, ResTable_map::TYPE_INTEGER
,
137 "an integer value, such as \"<code>100</code>\"." },
138 { booleanArray
, sizeof(booleanArray
)/2, ResTable_map::TYPE_BOOLEAN
,
139 "a boolean value, either \"<code>true</code>\" or \"<code>false</code>\"." },
140 { colorArray
, sizeof(colorArray
)/2, ResTable_map::TYPE_COLOR
,
141 "a color value, in the form of \"<code>#<i>rgb</i></code>\", \"<code>#<i>argb</i></code>\",\n"
142 "\"<code>#<i>rrggbb</i></code>\", or \"<code>#<i>aarrggbb</i></code>\"." },
143 { floatArray
, sizeof(floatArray
)/2, ResTable_map::TYPE_FLOAT
,
144 "a floating point value, such as \"<code>1.2</code>\"."},
145 { dimensionArray
, sizeof(dimensionArray
)/2, ResTable_map::TYPE_DIMENSION
,
146 "a dimension value, which is a floating point number appended with a unit such as \"<code>14.5sp</code>\".\n"
147 "Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size),\n"
148 "in (inches), mm (millimeters)." },
149 { fractionArray
, sizeof(fractionArray
)/2, ResTable_map::TYPE_FRACTION
,
150 "a fractional value, which is a floating point number appended with either % or %p, such as \"<code>14.5%</code>\".\n"
151 "The % suffix always means a percentage of the base size; the optional %p suffix provides a size relative to\n"
152 "some parent container." },
153 { enumArray
, sizeof(enumArray
)/2, ResTable_map::TYPE_ENUM
, NULL
},
154 { flagsArray
, sizeof(flagsArray
)/2, ResTable_map::TYPE_FLAGS
, NULL
},
158 static const char16_t suggestedArray
[] = { 's', 'u', 'g', 'g', 'e', 's', 't', 'e', 'd' };
160 static const flag_entry l10nRequiredFlags
[] = {
161 { suggestedArray
, sizeof(suggestedArray
)/2, ResTable_map::L10N_SUGGESTED
, NULL
},
165 static const char16_t nulStr
[] = { 0 };
167 static uint32_t parse_flags(const char16_t* str
, size_t len
,
168 const flag_entry
* flags
, bool* outError
= NULL
)
170 while (len
> 0 && isspace(*str
)) {
174 while (len
> 0 && isspace(str
[len
-1])) {
178 const char16_t* const end
= str
+ len
;
182 const char16_t* div
= str
;
183 while (div
< end
&& *div
!= '|') {
187 const flag_entry
* cur
= flags
;
189 if (strzcmp16(cur
->name
, cur
->nameLen
, str
, div
-str
) == 0) {
197 if (outError
) *outError
= true;
201 str
= div
< end
? div
+1 : div
;
204 if (outError
) *outError
= false;
208 static String16
mayOrMust(int type
, int flags
)
210 if ((type
&(~flags
)) == 0) {
211 return String16("<p>Must");
214 return String16("<p>May");
217 static void appendTypeInfo(ResourceTable
* outTable
, const String16
& pkg
,
218 const String16
& typeName
, const String16
& ident
, int type
,
219 const flag_entry
* flags
)
221 bool hadType
= false;
222 while (flags
->name
) {
223 if ((type
&flags
->value
) != 0 && flags
->description
!= NULL
) {
224 String16
fullMsg(mayOrMust(type
, flags
->value
));
225 fullMsg
.append(String16(" be "));
226 fullMsg
.append(String16(flags
->description
));
227 outTable
->appendTypeComment(pkg
, typeName
, ident
, fullMsg
);
232 if (hadType
&& (type
&ResTable_map::TYPE_REFERENCE
) == 0) {
233 outTable
->appendTypeComment(pkg
, typeName
, ident
,
234 String16("<p>This may also be a reference to a resource (in the form\n"
235 "\"<code>@[<i>package</i>:]<i>type</i>:<i>name</i></code>\") or\n"
236 "theme attribute (in the form\n"
237 "\"<code>?[<i>package</i>:][<i>type</i>:]<i>name</i></code>\")\n"
238 "containing a value of this type."));
242 struct PendingAttribute
244 const String16 myPackage
;
245 const SourcePos sourcePos
;
246 const bool appendComment
;
253 PendingAttribute(String16 _package
, const sp
<AaptFile
>& in
,
254 ResXMLTree
& block
, bool _appendComment
)
255 : myPackage(_package
)
256 , sourcePos(in
->getPrintableSource(), block
.getLineNumber())
257 , appendComment(_appendComment
)
258 , type(ResTable_map::TYPE_ANY
)
264 status_t
createIfNeeded(ResourceTable
* outTable
)
266 if (added
|| hasErrors
) {
271 String16
attr16("attr");
273 if (outTable
->hasBagOrEntry(myPackage
, attr16
, ident
)) {
274 sourcePos
.error("Attribute \"%s\" has already been defined\n",
275 String8(ident
).string());
277 return UNKNOWN_ERROR
;
281 sprintf(numberStr
, "%d", type
);
282 status_t err
= outTable
->addBag(sourcePos
, myPackage
,
283 attr16
, ident
, String16(""),
285 String16(numberStr
), NULL
, NULL
);
286 if (err
!= NO_ERROR
) {
290 outTable
->appendComment(myPackage
, attr16
, ident
, comment
, appendComment
);
291 //printf("Attribute %s comment: %s\n", String8(ident).string(),
292 // String8(comment).string());
297 static status_t
compileAttribute(const sp
<AaptFile
>& in
,
299 const String16
& myPackage
,
300 ResourceTable
* outTable
,
301 String16
* outIdent
= NULL
,
302 bool inStyleable
= false)
304 PendingAttribute
attr(myPackage
, in
, block
, inStyleable
);
306 const String16
attr16("attr");
307 const String16
id16("id");
309 // Attribute type constants.
310 const String16
enum16("enum");
311 const String16
flag16("flag");
313 ResXMLTree::event_code_t code
;
317 ssize_t identIdx
= block
.indexOfAttribute(NULL
, "name");
319 attr
.ident
= String16(block
.getAttributeStringValue(identIdx
, &len
));
321 *outIdent
= attr
.ident
;
324 attr
.sourcePos
.error("A 'name' attribute is required for <attr>\n");
325 attr
.hasErrors
= true;
328 attr
.comment
= String16(
329 block
.getComment(&len
) ? block
.getComment(&len
) : nulStr
);
331 ssize_t typeIdx
= block
.indexOfAttribute(NULL
, "format");
333 String16 typeStr
= String16(block
.getAttributeStringValue(typeIdx
, &len
));
334 attr
.type
= parse_flags(typeStr
.string(), typeStr
.size(), gFormatFlags
);
335 if (attr
.type
== 0) {
336 attr
.sourcePos
.error("Tag <attr> 'format' attribute value \"%s\" not valid\n",
337 String8(typeStr
).string());
338 attr
.hasErrors
= true;
340 attr
.createIfNeeded(outTable
);
341 } else if (!inStyleable
) {
342 // Attribute definitions outside of styleables always define the
343 // attribute as a generic value.
344 attr
.createIfNeeded(outTable
);
347 //printf("Attribute %s: type=0x%08x\n", String8(attr.ident).string(), attr.type);
349 ssize_t minIdx
= block
.indexOfAttribute(NULL
, "min");
351 String16 val
= String16(block
.getAttributeStringValue(minIdx
, &len
));
352 if (!ResTable::stringToInt(val
.string(), val
.size(), NULL
)) {
353 attr
.sourcePos
.error("Tag <attr> 'min' attribute must be a number, not \"%s\"\n",
354 String8(val
).string());
355 attr
.hasErrors
= true;
357 attr
.createIfNeeded(outTable
);
358 if (!attr
.hasErrors
) {
359 err
= outTable
->addBag(attr
.sourcePos
, myPackage
, attr16
, attr
.ident
,
360 String16(""), String16("^min"), String16(val
), NULL
, NULL
);
361 if (err
!= NO_ERROR
) {
362 attr
.hasErrors
= true;
367 ssize_t maxIdx
= block
.indexOfAttribute(NULL
, "max");
369 String16 val
= String16(block
.getAttributeStringValue(maxIdx
, &len
));
370 if (!ResTable::stringToInt(val
.string(), val
.size(), NULL
)) {
371 attr
.sourcePos
.error("Tag <attr> 'max' attribute must be a number, not \"%s\"\n",
372 String8(val
).string());
373 attr
.hasErrors
= true;
375 attr
.createIfNeeded(outTable
);
376 if (!attr
.hasErrors
) {
377 err
= outTable
->addBag(attr
.sourcePos
, myPackage
, attr16
, attr
.ident
,
378 String16(""), String16("^max"), String16(val
), NULL
, NULL
);
379 attr
.hasErrors
= true;
383 if ((minIdx
>= 0 || maxIdx
>= 0) && (attr
.type
&ResTable_map::TYPE_INTEGER
) == 0) {
384 attr
.sourcePos
.error("Tag <attr> must have format=integer attribute if using max or min\n");
385 attr
.hasErrors
= true;
388 ssize_t l10nIdx
= block
.indexOfAttribute(NULL
, "localization");
390 const uint16_t* str
= block
.getAttributeStringValue(l10nIdx
, &len
);
392 uint32_t l10n_required
= parse_flags(str
, len
, l10nRequiredFlags
, &error
);
394 attr
.sourcePos
.error("Tag <attr> 'localization' attribute value \"%s\" not valid\n",
395 String8(str
).string());
396 attr
.hasErrors
= true;
398 attr
.createIfNeeded(outTable
);
399 if (!attr
.hasErrors
) {
401 sprintf(buf
, "%d", l10n_required
);
402 err
= outTable
->addBag(attr
.sourcePos
, myPackage
, attr16
, attr
.ident
,
403 String16(""), String16("^l10n"), String16(buf
), NULL
, NULL
);
404 if (err
!= NO_ERROR
) {
405 attr
.hasErrors
= true;
410 String16 enumOrFlagsComment
;
412 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
413 if (code
== ResXMLTree::START_TAG
) {
414 uint32_t localType
= 0;
415 if (strcmp16(block
.getElementName(&len
), enum16
.string()) == 0) {
416 localType
= ResTable_map::TYPE_ENUM
;
417 } else if (strcmp16(block
.getElementName(&len
), flag16
.string()) == 0) {
418 localType
= ResTable_map::TYPE_FLAGS
;
420 SourcePos(in
->getPrintableSource(), block
.getLineNumber())
421 .error("Tag <%s> can not appear inside <attr>, only <enum> or <flag>\n",
422 String8(block
.getElementName(&len
)).string());
423 return UNKNOWN_ERROR
;
426 attr
.createIfNeeded(outTable
);
428 if (attr
.type
== ResTable_map::TYPE_ANY
) {
429 // No type was explicitly stated, so supplying enum tags
430 // implicitly creates an enum or flag.
434 if ((attr
.type
&(ResTable_map::TYPE_ENUM
|ResTable_map::TYPE_FLAGS
)) == 0) {
435 // Wasn't originally specified as an enum, so update its type.
436 attr
.type
|= localType
;
437 if (!attr
.hasErrors
) {
439 sprintf(numberStr
, "%d", attr
.type
);
440 err
= outTable
->addBag(SourcePos(in
->getPrintableSource(), block
.getLineNumber()),
441 myPackage
, attr16
, attr
.ident
, String16(""),
442 String16("^type"), String16(numberStr
), NULL
, NULL
, true);
443 if (err
!= NO_ERROR
) {
444 attr
.hasErrors
= true;
447 } else if ((uint32_t)(attr
.type
&(ResTable_map::TYPE_ENUM
|ResTable_map::TYPE_FLAGS
)) != localType
) {
448 if (localType
== ResTable_map::TYPE_ENUM
) {
449 SourcePos(in
->getPrintableSource(), block
.getLineNumber())
450 .error("<enum> attribute can not be used inside a flags format\n");
451 attr
.hasErrors
= true;
453 SourcePos(in
->getPrintableSource(), block
.getLineNumber())
454 .error("<flag> attribute can not be used inside a enum format\n");
455 attr
.hasErrors
= true;
460 ssize_t itemIdentIdx
= block
.indexOfAttribute(NULL
, "name");
461 if (itemIdentIdx
>= 0) {
462 itemIdent
= String16(block
.getAttributeStringValue(itemIdentIdx
, &len
));
464 SourcePos(in
->getPrintableSource(), block
.getLineNumber())
465 .error("A 'name' attribute is required for <enum> or <flag>\n");
466 attr
.hasErrors
= true;
470 ssize_t valueIdx
= block
.indexOfAttribute(NULL
, "value");
472 value
= String16(block
.getAttributeStringValue(valueIdx
, &len
));
474 SourcePos(in
->getPrintableSource(), block
.getLineNumber())
475 .error("A 'value' attribute is required for <enum> or <flag>\n");
476 attr
.hasErrors
= true;
478 if (!attr
.hasErrors
&& !ResTable::stringToInt(value
.string(), value
.size(), NULL
)) {
479 SourcePos(in
->getPrintableSource(), block
.getLineNumber())
480 .error("Tag <enum> or <flag> 'value' attribute must be a number,"
482 String8(value
).string());
483 attr
.hasErrors
= true;
486 // Make sure an id is defined for this enum/flag identifier...
487 if (!attr
.hasErrors
&& !outTable
->hasBagOrEntry(itemIdent
, &id16
, &myPackage
)) {
488 err
= outTable
->startBag(SourcePos(in
->getPrintableSource(), block
.getLineNumber()),
489 myPackage
, id16
, itemIdent
, String16(), NULL
);
490 if (err
!= NO_ERROR
) {
491 attr
.hasErrors
= true;
495 if (!attr
.hasErrors
) {
496 if (enumOrFlagsComment
.size() == 0) {
497 enumOrFlagsComment
.append(mayOrMust(attr
.type
,
498 ResTable_map::TYPE_ENUM
|ResTable_map::TYPE_FLAGS
));
499 enumOrFlagsComment
.append((attr
.type
&ResTable_map::TYPE_ENUM
)
500 ? String16(" be one of the following constant values.")
501 : String16(" be one or more (separated by '|') of the following constant values."));
502 enumOrFlagsComment
.append(String16("</p>\n<table>\n"
503 "<colgroup align=\"left\" />\n"
504 "<colgroup align=\"left\" />\n"
505 "<colgroup align=\"left\" />\n"
506 "<tr><th>Constant</th><th>Value</th><th>Description</th></tr>"));
509 enumOrFlagsComment
.append(String16("\n<tr><td><code>"));
510 enumOrFlagsComment
.append(itemIdent
);
511 enumOrFlagsComment
.append(String16("</code></td><td>"));
512 enumOrFlagsComment
.append(value
);
513 enumOrFlagsComment
.append(String16("</td><td>"));
514 if (block
.getComment(&len
)) {
515 enumOrFlagsComment
.append(String16(block
.getComment(&len
)));
517 enumOrFlagsComment
.append(String16("</td></tr>"));
519 err
= outTable
->addBag(SourcePos(in
->getPrintableSource(), block
.getLineNumber()),
521 attr16
, attr
.ident
, String16(""),
522 itemIdent
, value
, NULL
, NULL
, false, true);
523 if (err
!= NO_ERROR
) {
524 attr
.hasErrors
= true;
527 } else if (code
== ResXMLTree::END_TAG
) {
528 if (strcmp16(block
.getElementName(&len
), attr16
.string()) == 0) {
531 if ((attr
.type
&ResTable_map::TYPE_ENUM
) != 0) {
532 if (strcmp16(block
.getElementName(&len
), enum16
.string()) != 0) {
533 SourcePos(in
->getPrintableSource(), block
.getLineNumber())
534 .error("Found tag </%s> where </enum> is expected\n",
535 String8(block
.getElementName(&len
)).string());
536 return UNKNOWN_ERROR
;
539 if (strcmp16(block
.getElementName(&len
), flag16
.string()) != 0) {
540 SourcePos(in
->getPrintableSource(), block
.getLineNumber())
541 .error("Found tag </%s> where </flag> is expected\n",
542 String8(block
.getElementName(&len
)).string());
543 return UNKNOWN_ERROR
;
549 if (!attr
.hasErrors
&& attr
.added
) {
550 appendTypeInfo(outTable
, myPackage
, attr16
, attr
.ident
, attr
.type
, gFormatFlags
);
553 if (!attr
.hasErrors
&& enumOrFlagsComment
.size() > 0) {
554 enumOrFlagsComment
.append(String16("\n</table>"));
555 outTable
->appendTypeComment(myPackage
, attr16
, attr
.ident
, enumOrFlagsComment
);
562 bool localeIsDefined(const ResTable_config
& config
)
564 return config
.locale
== 0;
567 status_t
parseAndAddBag(Bundle
* bundle
,
568 const sp
<AaptFile
>& in
,
570 const ResTable_config
& config
,
571 const String16
& myPackage
,
572 const String16
& curType
,
573 const String16
& ident
,
574 const String16
& parentIdent
,
575 const String16
& itemIdent
,
578 const bool overwrite
,
579 ResourceTable
* outTable
)
582 const String16
item16("item");
585 Vector
<StringPool::entry_style_span
> spans
;
586 err
= parseStyledString(bundle
, in
->getPrintableSource().string(),
587 block
, item16
, &str
, &spans
,
589 if (err
!= NO_ERROR
) {
593 NOISY(printf("Adding resource bag entry l=%c%c c=%c%c orien=%d d=%d "
594 " pid=%s, bag=%s, id=%s: %s\n",
595 config
.language
[0], config
.language
[1],
596 config
.country
[0], config
.country
[1],
597 config
.orientation
, config
.density
,
598 String8(parentIdent
).string(),
599 String8(ident
).string(),
600 String8(itemIdent
).string(),
601 String8(str
).string()));
603 err
= outTable
->addBag(SourcePos(in
->getPrintableSource(), block
->getLineNumber()),
604 myPackage
, curType
, ident
, parentIdent
, itemIdent
, str
,
605 &spans
, &config
, overwrite
, false, curFormat
);
610 status_t
parseAndAddEntry(Bundle
* bundle
,
611 const sp
<AaptFile
>& in
,
613 const ResTable_config
& config
,
614 const String16
& myPackage
,
615 const String16
& curType
,
616 const String16
& ident
,
617 const String16
& curTag
,
621 const bool overwrite
,
622 ResourceTable
* outTable
)
627 Vector
<StringPool::entry_style_span
> spans
;
628 err
= parseStyledString(bundle
, in
->getPrintableSource().string(), block
,
629 curTag
, &str
, curIsStyled
? &spans
: NULL
,
632 if (err
< NO_ERROR
) {
636 NOISY(printf("Adding resource entry l=%c%c c=%c%c orien=%d d=%d id=%s: %s\n",
637 config
.language
[0], config
.language
[1],
638 config
.country
[0], config
.country
[1],
639 config
.orientation
, config
.density
,
640 String8(ident
).string(), String8(str
).string()));
642 err
= outTable
->addEntry(SourcePos(in
->getPrintableSource(), block
->getLineNumber()),
643 myPackage
, curType
, ident
, str
, &spans
, &config
,
644 false, curFormat
, overwrite
);
649 status_t
compileResourceFile(Bundle
* bundle
,
650 const sp
<AaptAssets
>& assets
,
651 const sp
<AaptFile
>& in
,
652 const ResTable_config
& defParams
,
653 const bool overwrite
,
654 ResourceTable
* outTable
)
657 status_t err
= parseXMLResource(in
, &block
, false, true);
658 if (err
!= NO_ERROR
) {
663 const String16
resources16("resources");
665 // Identifier declaration tags.
666 const String16
declare_styleable16("declare-styleable");
667 const String16
attr16("attr");
669 // Data creation organizational tags.
670 const String16
string16("string");
671 const String16
drawable16("drawable");
672 const String16
color16("color");
673 const String16
bool16("bool");
674 const String16
integer16("integer");
675 const String16
dimen16("dimen");
676 const String16
fraction16("fraction");
677 const String16
style16("style");
678 const String16
plurals16("plurals");
679 const String16
array16("array");
680 const String16
string_array16("string-array");
681 const String16
integer_array16("integer-array");
682 const String16
public16("public");
683 const String16
public_padding16("public-padding");
684 const String16
private_symbols16("private-symbols");
685 const String16
add_resource16("add-resource");
686 const String16
skip16("skip");
687 const String16
eat_comment16("eat-comment");
689 // Data creation tags.
690 const String16
bag16("bag");
691 const String16
item16("item");
693 // Attribute type constants.
694 const String16
enum16("enum");
697 const String16
other16("other");
698 const String16
quantityOther16("^other");
699 const String16
zero16("zero");
700 const String16
quantityZero16("^zero");
701 const String16
one16("one");
702 const String16
quantityOne16("^one");
703 const String16
two16("two");
704 const String16
quantityTwo16("^two");
705 const String16
few16("few");
706 const String16
quantityFew16("^few");
707 const String16
many16("many");
708 const String16
quantityMany16("^many");
710 // useful attribute names and special values
711 const String16
name16("name");
712 const String16
translatable16("translatable");
713 const String16
false16("false");
715 const String16
myPackage(assets
->getPackage());
717 bool hasErrors
= false;
719 DefaultKeyedVector
<String16
, uint32_t> nextPublicId(0);
721 ResXMLTree::event_code_t code
;
724 } while (code
== ResXMLTree::START_NAMESPACE
);
727 if (code
!= ResXMLTree::START_TAG
) {
728 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
729 "No start tag found\n");
730 return UNKNOWN_ERROR
;
732 if (strcmp16(block
.getElementName(&len
), resources16
.string()) != 0) {
733 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
734 "Invalid start tag %s\n", String8(block
.getElementName(&len
)).string());
735 return UNKNOWN_ERROR
;
738 ResTable_config
curParams(defParams
);
740 ResTable_config
pseudoParams(curParams
);
741 pseudoParams
.language
[0] = 'z';
742 pseudoParams
.language
[1] = 'z';
743 pseudoParams
.country
[0] = 'Z';
744 pseudoParams
.country
[1] = 'Z';
746 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
747 if (code
== ResXMLTree::START_TAG
) {
748 const String16
* curTag
= NULL
;
750 int32_t curFormat
= ResTable_map::TYPE_ANY
;
751 bool curIsBag
= false;
752 bool curIsBagReplaceOnOverwrite
= false;
753 bool curIsStyled
= false;
754 bool curIsPseudolocalizable
= false;
755 bool localHasErrors
= false;
757 if (strcmp16(block
.getElementName(&len
), skip16
.string()) == 0) {
758 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
759 && code
!= ResXMLTree::BAD_DOCUMENT
) {
760 if (code
== ResXMLTree::END_TAG
) {
761 if (strcmp16(block
.getElementName(&len
), skip16
.string()) == 0) {
768 } else if (strcmp16(block
.getElementName(&len
), eat_comment16
.string()) == 0) {
769 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
770 && code
!= ResXMLTree::BAD_DOCUMENT
) {
771 if (code
== ResXMLTree::END_TAG
) {
772 if (strcmp16(block
.getElementName(&len
), eat_comment16
.string()) == 0) {
779 } else if (strcmp16(block
.getElementName(&len
), public16
.string()) == 0) {
780 SourcePos
srcPos(in
->getPrintableSource(), block
.getLineNumber());
783 ssize_t typeIdx
= block
.indexOfAttribute(NULL
, "type");
785 srcPos
.error("A 'type' attribute is required for <public>\n");
786 hasErrors
= localHasErrors
= true;
788 type
= String16(block
.getAttributeStringValue(typeIdx
, &len
));
791 ssize_t nameIdx
= block
.indexOfAttribute(NULL
, "name");
793 srcPos
.error("A 'name' attribute is required for <public>\n");
794 hasErrors
= localHasErrors
= true;
796 name
= String16(block
.getAttributeStringValue(nameIdx
, &len
));
799 ssize_t identIdx
= block
.indexOfAttribute(NULL
, "id");
801 const char16_t* identStr
= block
.getAttributeStringValue(identIdx
, &len
);
802 Res_value identValue
;
803 if (!ResTable::stringToInt(identStr
, len
, &identValue
)) {
804 srcPos
.error("Given 'id' attribute is not an integer: %s\n",
805 String8(block
.getAttributeStringValue(identIdx
, &len
)).string());
806 hasErrors
= localHasErrors
= true;
808 ident
= identValue
.data
;
809 nextPublicId
.replaceValueFor(type
, ident
+1);
811 } else if (nextPublicId
.indexOfKey(type
) < 0) {
812 srcPos
.error("No 'id' attribute supplied <public>,"
813 " and no previous id defined in this file.\n");
814 hasErrors
= localHasErrors
= true;
815 } else if (!localHasErrors
) {
816 ident
= nextPublicId
.valueFor(type
);
817 nextPublicId
.replaceValueFor(type
, ident
+1);
820 if (!localHasErrors
) {
821 err
= outTable
->addPublic(srcPos
, myPackage
, type
, name
, ident
);
822 if (err
< NO_ERROR
) {
823 hasErrors
= localHasErrors
= true;
826 if (!localHasErrors
) {
827 sp
<AaptSymbols
> symbols
= assets
->getSymbolsFor(String8("R"));
828 if (symbols
!= NULL
) {
829 symbols
= symbols
->addNestedSymbol(String8(type
), srcPos
);
831 if (symbols
!= NULL
) {
832 symbols
->makeSymbolPublic(String8(name
), srcPos
);
834 block
.getComment(&len
) ? block
.getComment(&len
) : nulStr
);
835 symbols
->appendComment(String8(name
), comment
, srcPos
);
837 srcPos
.error("Unable to create symbols!\n");
838 hasErrors
= localHasErrors
= true;
842 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
843 if (code
== ResXMLTree::END_TAG
) {
844 if (strcmp16(block
.getElementName(&len
), public16
.string()) == 0) {
851 } else if (strcmp16(block
.getElementName(&len
), public_padding16
.string()) == 0) {
852 SourcePos
srcPos(in
->getPrintableSource(), block
.getLineNumber());
855 ssize_t typeIdx
= block
.indexOfAttribute(NULL
, "type");
857 srcPos
.error("A 'type' attribute is required for <public-padding>\n");
858 hasErrors
= localHasErrors
= true;
860 type
= String16(block
.getAttributeStringValue(typeIdx
, &len
));
863 ssize_t nameIdx
= block
.indexOfAttribute(NULL
, "name");
865 srcPos
.error("A 'name' attribute is required for <public-padding>\n");
866 hasErrors
= localHasErrors
= true;
868 name
= String16(block
.getAttributeStringValue(nameIdx
, &len
));
871 ssize_t startIdx
= block
.indexOfAttribute(NULL
, "start");
873 const char16_t* startStr
= block
.getAttributeStringValue(startIdx
, &len
);
874 Res_value startValue
;
875 if (!ResTable::stringToInt(startStr
, len
, &startValue
)) {
876 srcPos
.error("Given 'start' attribute is not an integer: %s\n",
877 String8(block
.getAttributeStringValue(startIdx
, &len
)).string());
878 hasErrors
= localHasErrors
= true;
880 start
= startValue
.data
;
882 } else if (nextPublicId
.indexOfKey(type
) < 0) {
883 srcPos
.error("No 'start' attribute supplied <public-padding>,"
884 " and no previous id defined in this file.\n");
885 hasErrors
= localHasErrors
= true;
886 } else if (!localHasErrors
) {
887 start
= nextPublicId
.valueFor(type
);
891 ssize_t endIdx
= block
.indexOfAttribute(NULL
, "end");
893 const char16_t* endStr
= block
.getAttributeStringValue(endIdx
, &len
);
895 if (!ResTable::stringToInt(endStr
, len
, &endValue
)) {
896 srcPos
.error("Given 'end' attribute is not an integer: %s\n",
897 String8(block
.getAttributeStringValue(endIdx
, &len
)).string());
898 hasErrors
= localHasErrors
= true;
903 srcPos
.error("No 'end' attribute supplied <public-padding>\n");
904 hasErrors
= localHasErrors
= true;
908 nextPublicId
.replaceValueFor(type
, end
+1);
910 srcPos
.error("Padding start '%ul' is after end '%ul'\n",
912 hasErrors
= localHasErrors
= true;
916 block
.getComment(&len
) ? block
.getComment(&len
) : nulStr
);
917 for (uint32_t curIdent
=start
; curIdent
<=end
; curIdent
++) {
918 if (localHasErrors
) {
921 String16
curName(name
);
923 sprintf(buf
, "%d", (int)(end
-curIdent
+1));
924 curName
.append(String16(buf
));
926 err
= outTable
->addEntry(srcPos
, myPackage
, type
, curName
,
927 String16("padding"), NULL
, &curParams
, false,
928 ResTable_map::TYPE_STRING
, overwrite
);
929 if (err
< NO_ERROR
) {
930 hasErrors
= localHasErrors
= true;
933 err
= outTable
->addPublic(srcPos
, myPackage
, type
,
935 if (err
< NO_ERROR
) {
936 hasErrors
= localHasErrors
= true;
939 sp
<AaptSymbols
> symbols
= assets
->getSymbolsFor(String8("R"));
940 if (symbols
!= NULL
) {
941 symbols
= symbols
->addNestedSymbol(String8(type
), srcPos
);
943 if (symbols
!= NULL
) {
944 symbols
->makeSymbolPublic(String8(curName
), srcPos
);
945 symbols
->appendComment(String8(curName
), comment
, srcPos
);
947 srcPos
.error("Unable to create symbols!\n");
948 hasErrors
= localHasErrors
= true;
952 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
953 if (code
== ResXMLTree::END_TAG
) {
954 if (strcmp16(block
.getElementName(&len
), public_padding16
.string()) == 0) {
961 } else if (strcmp16(block
.getElementName(&len
), private_symbols16
.string()) == 0) {
963 ssize_t pkgIdx
= block
.indexOfAttribute(NULL
, "package");
965 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
966 "A 'package' attribute is required for <private-symbols>\n");
967 hasErrors
= localHasErrors
= true;
969 pkg
= String16(block
.getAttributeStringValue(pkgIdx
, &len
));
970 if (!localHasErrors
) {
971 assets
->setSymbolsPrivatePackage(String8(pkg
));
974 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
975 if (code
== ResXMLTree::END_TAG
) {
976 if (strcmp16(block
.getElementName(&len
), private_symbols16
.string()) == 0) {
983 } else if (strcmp16(block
.getElementName(&len
), add_resource16
.string()) == 0) {
984 SourcePos
srcPos(in
->getPrintableSource(), block
.getLineNumber());
987 ssize_t typeIdx
= block
.indexOfAttribute(NULL
, "type");
989 srcPos
.error("A 'type' attribute is required for <add-resource>\n");
990 hasErrors
= localHasErrors
= true;
992 typeName
= String16(block
.getAttributeStringValue(typeIdx
, &len
));
995 ssize_t nameIdx
= block
.indexOfAttribute(NULL
, "name");
997 srcPos
.error("A 'name' attribute is required for <add-resource>\n");
998 hasErrors
= localHasErrors
= true;
1000 name
= String16(block
.getAttributeStringValue(nameIdx
, &len
));
1002 outTable
->canAddEntry(srcPos
, myPackage
, typeName
, name
);
1004 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
1005 if (code
== ResXMLTree::END_TAG
) {
1006 if (strcmp16(block
.getElementName(&len
), add_resource16
.string()) == 0) {
1013 } else if (strcmp16(block
.getElementName(&len
), declare_styleable16
.string()) == 0) {
1014 SourcePos
srcPos(in
->getPrintableSource(), block
.getLineNumber());
1017 ssize_t identIdx
= block
.indexOfAttribute(NULL
, "name");
1019 srcPos
.error("A 'name' attribute is required for <declare-styleable>\n");
1020 hasErrors
= localHasErrors
= true;
1022 ident
= String16(block
.getAttributeStringValue(identIdx
, &len
));
1024 sp
<AaptSymbols
> symbols
= assets
->getSymbolsFor(String8("R"));
1025 if (!localHasErrors
) {
1026 if (symbols
!= NULL
) {
1027 symbols
= symbols
->addNestedSymbol(String8("styleable"), srcPos
);
1029 sp
<AaptSymbols
> styleSymbols
= symbols
;
1030 if (symbols
!= NULL
) {
1031 symbols
= symbols
->addNestedSymbol(String8(ident
), srcPos
);
1033 if (symbols
== NULL
) {
1034 srcPos
.error("Unable to create symbols!\n");
1035 return UNKNOWN_ERROR
;
1039 block
.getComment(&len
) ? block
.getComment(&len
) : nulStr
);
1040 styleSymbols
->appendComment(String8(ident
), comment
, srcPos
);
1045 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
1046 if (code
== ResXMLTree::START_TAG
) {
1047 if (strcmp16(block
.getElementName(&len
), skip16
.string()) == 0) {
1048 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
1049 && code
!= ResXMLTree::BAD_DOCUMENT
) {
1050 if (code
== ResXMLTree::END_TAG
) {
1051 if (strcmp16(block
.getElementName(&len
), skip16
.string()) == 0) {
1057 } else if (strcmp16(block
.getElementName(&len
), eat_comment16
.string()) == 0) {
1058 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
1059 && code
!= ResXMLTree::BAD_DOCUMENT
) {
1060 if (code
== ResXMLTree::END_TAG
) {
1061 if (strcmp16(block
.getElementName(&len
), eat_comment16
.string()) == 0) {
1067 } else if (strcmp16(block
.getElementName(&len
), attr16
.string()) != 0) {
1068 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1069 "Tag <%s> can not appear inside <declare-styleable>, only <attr>\n",
1070 String8(block
.getElementName(&len
)).string());
1071 return UNKNOWN_ERROR
;
1075 block
.getComment(&len
) ? block
.getComment(&len
) : nulStr
);
1077 err
= compileAttribute(in
, block
, myPackage
, outTable
, &itemIdent
, true);
1078 if (err
!= NO_ERROR
) {
1079 hasErrors
= localHasErrors
= true;
1082 if (symbols
!= NULL
) {
1083 SourcePos
srcPos(String8(in
->getPrintableSource()), block
.getLineNumber());
1084 symbols
->addSymbol(String8(itemIdent
), 0, srcPos
);
1085 symbols
->appendComment(String8(itemIdent
), comment
, srcPos
);
1086 //printf("Attribute %s comment: %s\n", String8(itemIdent).string(),
1087 // String8(comment).string());
1089 } else if (code
== ResXMLTree::END_TAG
) {
1090 if (strcmp16(block
.getElementName(&len
), declare_styleable16
.string()) == 0) {
1094 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1095 "Found tag </%s> where </attr> is expected\n",
1096 String8(block
.getElementName(&len
)).string());
1097 return UNKNOWN_ERROR
;
1102 } else if (strcmp16(block
.getElementName(&len
), attr16
.string()) == 0) {
1103 err
= compileAttribute(in
, block
, myPackage
, outTable
, NULL
);
1104 if (err
!= NO_ERROR
) {
1109 } else if (strcmp16(block
.getElementName(&len
), item16
.string()) == 0) {
1111 ssize_t attri
= block
.indexOfAttribute(NULL
, "type");
1113 curType
= String16(block
.getAttributeStringValue(attri
, &len
));
1114 ssize_t formatIdx
= block
.indexOfAttribute(NULL
, "format");
1115 if (formatIdx
>= 0) {
1116 String16 formatStr
= String16(block
.getAttributeStringValue(
1118 curFormat
= parse_flags(formatStr
.string(), formatStr
.size(),
1120 if (curFormat
== 0) {
1121 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1122 "Tag <item> 'format' attribute value \"%s\" not valid\n",
1123 String8(formatStr
).string());
1124 hasErrors
= localHasErrors
= true;
1128 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1129 "A 'type' attribute is required for <item>\n");
1130 hasErrors
= localHasErrors
= true;
1133 } else if (strcmp16(block
.getElementName(&len
), string16
.string()) == 0) {
1134 // Note the existence and locale of every string we process
1136 curParams
.getLocale(rawLocale
);
1137 String8
locale(rawLocale
);
1139 String16 translatable
;
1141 size_t n
= block
.getAttributeCount();
1142 for (size_t i
= 0; i
< n
; i
++) {
1144 const uint16_t* attr
= block
.getAttributeName(i
, &length
);
1145 if (strcmp16(attr
, name16
.string()) == 0) {
1146 name
.setTo(block
.getAttributeStringValue(i
, &length
));
1147 } else if (strcmp16(attr
, translatable16
.string()) == 0) {
1148 translatable
.setTo(block
.getAttributeStringValue(i
, &length
));
1152 if (name
.size() > 0) {
1153 if (translatable
== false16
) {
1154 // Untranslatable strings must only exist in the default [empty] locale
1155 if (locale
.size() > 0) {
1156 fprintf(stderr
, "aapt: warning: string '%s' in %s marked untranslatable but exists"
1157 " in locale '%s'\n", String8(name
).string(),
1158 bundle
->getResourceSourceDirs()[0],
1160 // hasErrors = localHasErrors = true;
1162 // Intentionally empty block:
1164 // Don't add untranslatable strings to the localization table; that
1165 // way if we later see localizations of them, they'll be flagged as
1166 // having no default translation.
1169 outTable
->addLocalization(name
, locale
);
1175 curFormat
= ResTable_map::TYPE_REFERENCE
|ResTable_map::TYPE_STRING
;
1177 curIsPseudolocalizable
= true;
1178 } else if (strcmp16(block
.getElementName(&len
), drawable16
.string()) == 0) {
1179 curTag
= &drawable16
;
1180 curType
= drawable16
;
1181 curFormat
= ResTable_map::TYPE_REFERENCE
|ResTable_map::TYPE_COLOR
;
1182 } else if (strcmp16(block
.getElementName(&len
), color16
.string()) == 0) {
1185 curFormat
= ResTable_map::TYPE_REFERENCE
|ResTable_map::TYPE_COLOR
;
1186 } else if (strcmp16(block
.getElementName(&len
), bool16
.string()) == 0) {
1189 curFormat
= ResTable_map::TYPE_REFERENCE
|ResTable_map::TYPE_BOOLEAN
;
1190 } else if (strcmp16(block
.getElementName(&len
), integer16
.string()) == 0) {
1191 curTag
= &integer16
;
1192 curType
= integer16
;
1193 curFormat
= ResTable_map::TYPE_REFERENCE
|ResTable_map::TYPE_INTEGER
;
1194 } else if (strcmp16(block
.getElementName(&len
), dimen16
.string()) == 0) {
1197 curFormat
= ResTable_map::TYPE_REFERENCE
|ResTable_map::TYPE_DIMENSION
;
1198 } else if (strcmp16(block
.getElementName(&len
), fraction16
.string()) == 0) {
1199 curTag
= &fraction16
;
1200 curType
= fraction16
;
1201 curFormat
= ResTable_map::TYPE_REFERENCE
|ResTable_map::TYPE_FRACTION
;
1202 } else if (strcmp16(block
.getElementName(&len
), bag16
.string()) == 0) {
1205 ssize_t attri
= block
.indexOfAttribute(NULL
, "type");
1207 curType
= String16(block
.getAttributeStringValue(attri
, &len
));
1209 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1210 "A 'type' attribute is required for <bag>\n");
1211 hasErrors
= localHasErrors
= true;
1213 } else if (strcmp16(block
.getElementName(&len
), style16
.string()) == 0) {
1217 } else if (strcmp16(block
.getElementName(&len
), plurals16
.string()) == 0) {
1218 curTag
= &plurals16
;
1219 curType
= plurals16
;
1221 } else if (strcmp16(block
.getElementName(&len
), array16
.string()) == 0) {
1225 curIsBagReplaceOnOverwrite
= true;
1226 ssize_t formatIdx
= block
.indexOfAttribute(NULL
, "format");
1227 if (formatIdx
>= 0) {
1228 String16 formatStr
= String16(block
.getAttributeStringValue(
1230 curFormat
= parse_flags(formatStr
.string(), formatStr
.size(),
1232 if (curFormat
== 0) {
1233 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1234 "Tag <array> 'format' attribute value \"%s\" not valid\n",
1235 String8(formatStr
).string());
1236 hasErrors
= localHasErrors
= true;
1239 } else if (strcmp16(block
.getElementName(&len
), string_array16
.string()) == 0) {
1240 curTag
= &string_array16
;
1242 curFormat
= ResTable_map::TYPE_REFERENCE
|ResTable_map::TYPE_STRING
;
1244 curIsBagReplaceOnOverwrite
= true;
1245 curIsPseudolocalizable
= true;
1246 } else if (strcmp16(block
.getElementName(&len
), integer_array16
.string()) == 0) {
1247 curTag
= &integer_array16
;
1249 curFormat
= ResTable_map::TYPE_REFERENCE
|ResTable_map::TYPE_INTEGER
;
1251 curIsBagReplaceOnOverwrite
= true;
1253 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1254 "Found tag %s where item is expected\n",
1255 String8(block
.getElementName(&len
)).string());
1256 return UNKNOWN_ERROR
;
1260 ssize_t identIdx
= block
.indexOfAttribute(NULL
, "name");
1261 if (identIdx
>= 0) {
1262 ident
= String16(block
.getAttributeStringValue(identIdx
, &len
));
1264 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1265 "A 'name' attribute is required for <%s>\n",
1266 String8(*curTag
).string());
1267 hasErrors
= localHasErrors
= true;
1270 String16
comment(block
.getComment(&len
) ? block
.getComment(&len
) : nulStr
);
1273 // Figure out the parent of this bag...
1274 String16 parentIdent
;
1275 ssize_t parentIdentIdx
= block
.indexOfAttribute(NULL
, "parent");
1276 if (parentIdentIdx
>= 0) {
1277 parentIdent
= String16(block
.getAttributeStringValue(parentIdentIdx
, &len
));
1279 ssize_t sep
= ident
.findLast('.');
1281 parentIdent
.setTo(ident
, sep
);
1285 if (!localHasErrors
) {
1286 err
= outTable
->startBag(SourcePos(in
->getPrintableSource(),
1287 block
.getLineNumber()), myPackage
, curType
, ident
,
1288 parentIdent
, &curParams
,
1289 overwrite
, curIsBagReplaceOnOverwrite
);
1290 if (err
!= NO_ERROR
) {
1291 hasErrors
= localHasErrors
= true;
1295 ssize_t elmIndex
= 0;
1296 char elmIndexStr
[14];
1297 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
1298 && code
!= ResXMLTree::BAD_DOCUMENT
) {
1300 if (code
== ResXMLTree::START_TAG
) {
1301 if (strcmp16(block
.getElementName(&len
), item16
.string()) != 0) {
1302 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1303 "Tag <%s> can not appear inside <%s>, only <item>\n",
1304 String8(block
.getElementName(&len
)).string(),
1305 String8(*curTag
).string());
1306 return UNKNOWN_ERROR
;
1310 if (curType
== array16
) {
1311 sprintf(elmIndexStr
, "^index_%d", (int)elmIndex
++);
1312 itemIdent
= String16(elmIndexStr
);
1313 } else if (curType
== plurals16
) {
1314 ssize_t itemIdentIdx
= block
.indexOfAttribute(NULL
, "quantity");
1315 if (itemIdentIdx
>= 0) {
1316 String16
quantity16(block
.getAttributeStringValue(itemIdentIdx
, &len
));
1317 if (quantity16
== other16
) {
1318 itemIdent
= quantityOther16
;
1320 else if (quantity16
== zero16
) {
1321 itemIdent
= quantityZero16
;
1323 else if (quantity16
== one16
) {
1324 itemIdent
= quantityOne16
;
1326 else if (quantity16
== two16
) {
1327 itemIdent
= quantityTwo16
;
1329 else if (quantity16
== few16
) {
1330 itemIdent
= quantityFew16
;
1332 else if (quantity16
== many16
) {
1333 itemIdent
= quantityMany16
;
1336 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1337 "Illegal 'quantity' attribute is <item> inside <plurals>\n");
1338 hasErrors
= localHasErrors
= true;
1341 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1342 "A 'quantity' attribute is required for <item> inside <plurals>\n");
1343 hasErrors
= localHasErrors
= true;
1346 ssize_t itemIdentIdx
= block
.indexOfAttribute(NULL
, "name");
1347 if (itemIdentIdx
>= 0) {
1348 itemIdent
= String16(block
.getAttributeStringValue(itemIdentIdx
, &len
));
1350 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1351 "A 'name' attribute is required for <item>\n");
1352 hasErrors
= localHasErrors
= true;
1356 ResXMLParser::ResXMLPosition parserPosition
;
1357 block
.getPosition(&parserPosition
);
1359 err
= parseAndAddBag(bundle
, in
, &block
, curParams
, myPackage
, curType
,
1360 ident
, parentIdent
, itemIdent
, curFormat
,
1361 false, overwrite
, outTable
);
1362 if (err
== NO_ERROR
) {
1363 if (curIsPseudolocalizable
&& localeIsDefined(curParams
)
1364 && bundle
->getPseudolocalize()) {
1365 // pseudolocalize here
1367 block
.setPosition(parserPosition
);
1368 err
= parseAndAddBag(bundle
, in
, &block
, pseudoParams
, myPackage
,
1369 curType
, ident
, parentIdent
, itemIdent
, curFormat
, true,
1370 overwrite
, outTable
);
1374 if (err
!= NO_ERROR
) {
1375 hasErrors
= localHasErrors
= true;
1377 } else if (code
== ResXMLTree::END_TAG
) {
1378 if (strcmp16(block
.getElementName(&len
), curTag
->string()) != 0) {
1379 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1380 "Found tag </%s> where </%s> is expected\n",
1381 String8(block
.getElementName(&len
)).string(),
1382 String8(*curTag
).string());
1383 return UNKNOWN_ERROR
;
1389 ResXMLParser::ResXMLPosition parserPosition
;
1390 block
.getPosition(&parserPosition
);
1392 err
= parseAndAddEntry(bundle
, in
, &block
, curParams
, myPackage
, curType
, ident
,
1393 *curTag
, curIsStyled
, curFormat
, false, overwrite
, outTable
);
1395 if (err
< NO_ERROR
) { // Why err < NO_ERROR instead of err != NO_ERROR?
1396 hasErrors
= localHasErrors
= true;
1398 else if (err
== NO_ERROR
) {
1399 if (curIsPseudolocalizable
&& localeIsDefined(curParams
)
1400 && bundle
->getPseudolocalize()) {
1401 // pseudolocalize here
1402 block
.setPosition(parserPosition
);
1403 err
= parseAndAddEntry(bundle
, in
, &block
, pseudoParams
, myPackage
, curType
,
1404 ident
, *curTag
, curIsStyled
, curFormat
, true, overwrite
, outTable
);
1405 if (err
!= NO_ERROR
) {
1406 hasErrors
= localHasErrors
= true;
1413 if (comment
.size() > 0) {
1414 printf("Comment for @%s:%s/%s: %s\n", String8(myPackage
).string(),
1415 String8(curType
).string(), String8(ident
).string(),
1416 String8(comment
).string());
1419 if (!localHasErrors
) {
1420 outTable
->appendComment(myPackage
, curType
, ident
, comment
, false);
1423 else if (code
== ResXMLTree::END_TAG
) {
1424 if (strcmp16(block
.getElementName(&len
), resources16
.string()) != 0) {
1425 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1426 "Unexpected end tag %s\n", String8(block
.getElementName(&len
)).string());
1427 return UNKNOWN_ERROR
;
1430 else if (code
== ResXMLTree::START_NAMESPACE
|| code
== ResXMLTree::END_NAMESPACE
) {
1432 else if (code
== ResXMLTree::TEXT
) {
1433 if (isWhitespace(block
.getText(&len
))) {
1436 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1437 "Found text \"%s\" where item tag is expected\n",
1438 String8(block
.getText(&len
)).string());
1439 return UNKNOWN_ERROR
;
1443 return hasErrors
? UNKNOWN_ERROR
: NO_ERROR
;
1446 ResourceTable::ResourceTable(Bundle
* bundle
, const String16
& assetsPackage
)
1447 : mAssetsPackage(assetsPackage
), mNextPackageId(1), mHaveAppPackage(false),
1448 mIsAppPackage(!bundle
->getExtending()),
1454 status_t
ResourceTable::addIncludedResources(Bundle
* bundle
, const sp
<AaptAssets
>& assets
)
1456 status_t err
= assets
->buildIncludedResources(bundle
);
1457 if (err
!= NO_ERROR
) {
1461 // For future reference to included resources.
1464 const ResTable
& incl
= assets
->getIncludedResources();
1466 // Retrieve all the packages.
1467 const size_t N
= incl
.getBasePackageCount();
1468 for (size_t phase
=0; phase
<2; phase
++) {
1469 for (size_t i
=0; i
<N
; i
++) {
1470 String16
name(incl
.getBasePackageName(i
));
1471 uint32_t id
= incl
.getBasePackageId(i
);
1472 // First time through: only add base packages (id
1473 // is not 0); second time through add the other
1477 // Skip base packages -- already one.
1480 // Assign a dynamic id.
1481 id
= mNextPackageId
;
1483 } else if (id
!= 0) {
1485 if (mHaveAppPackage
) {
1486 fprintf(stderr
, "Included resources have two application packages!\n");
1487 return UNKNOWN_ERROR
;
1489 mHaveAppPackage
= true;
1491 if (mNextPackageId
> id
) {
1492 fprintf(stderr
, "Included base package ID %d already in use!\n", id
);
1493 return UNKNOWN_ERROR
;
1497 NOISY(printf("Including package %s with ID=%d\n",
1498 String8(name
).string(), id
));
1499 sp
<Package
> p
= new Package(name
, id
);
1500 mPackages
.add(name
, p
);
1501 mOrderedPackages
.add(p
);
1503 if (id
>= mNextPackageId
) {
1504 mNextPackageId
= id
+1;
1510 // Every resource table always has one first entry, the bag attributes.
1511 const SourcePos
unknown(String8("????"), 0);
1512 sp
<Type
> attr
= getType(mAssetsPackage
, String16("attr"), unknown
);
1517 status_t
ResourceTable::addPublic(const SourcePos
& sourcePos
,
1518 const String16
& package
,
1519 const String16
& type
,
1520 const String16
& name
,
1521 const uint32_t ident
)
1523 uint32_t rid
= mAssets
->getIncludedResources()
1524 .identifierForName(name
.string(), name
.size(),
1525 type
.string(), type
.size(),
1526 package
.string(), package
.size());
1528 sourcePos
.error("Error declaring public resource %s/%s for included package %s\n",
1529 String8(type
).string(), String8(name
).string(),
1530 String8(package
).string());
1531 return UNKNOWN_ERROR
;
1534 sp
<Type
> t
= getType(package
, type
, sourcePos
);
1536 return UNKNOWN_ERROR
;
1538 return t
->addPublic(sourcePos
, name
, ident
);
1541 status_t
ResourceTable::addEntry(const SourcePos
& sourcePos
,
1542 const String16
& package
,
1543 const String16
& type
,
1544 const String16
& name
,
1545 const String16
& value
,
1546 const Vector
<StringPool::entry_style_span
>* style
,
1547 const ResTable_config
* params
,
1548 const bool doSetIndex
,
1549 const int32_t format
,
1550 const bool overwrite
)
1552 // Check for adding entries in other packages... for now we do
1553 // nothing. We need to do the right thing here to support skinning.
1554 uint32_t rid
= mAssets
->getIncludedResources()
1555 .identifierForName(name
.string(), name
.size(),
1556 type
.string(), type
.size(),
1557 package
.string(), package
.size());
1563 if (name
== String16("left")) {
1564 printf("Adding entry left: file=%s, line=%d, type=%s, value=%s\n",
1565 sourcePos
.file
.string(), sourcePos
.line
, String8(type
).string(),
1566 String8(value
).string());
1570 sp
<Entry
> e
= getEntry(package
, type
, name
, sourcePos
, overwrite
,
1571 params
, doSetIndex
);
1573 return UNKNOWN_ERROR
;
1575 status_t err
= e
->setItem(sourcePos
, value
, style
, format
, overwrite
);
1576 if (err
== NO_ERROR
) {
1582 status_t
ResourceTable::startBag(const SourcePos
& sourcePos
,
1583 const String16
& package
,
1584 const String16
& type
,
1585 const String16
& name
,
1586 const String16
& bagParent
,
1587 const ResTable_config
* params
,
1589 bool replace
, bool isId
)
1591 status_t result
= NO_ERROR
;
1593 // Check for adding entries in other packages... for now we do
1594 // nothing. We need to do the right thing here to support skinning.
1595 uint32_t rid
= mAssets
->getIncludedResources()
1596 .identifierForName(name
.string(), name
.size(),
1597 type
.string(), type
.size(),
1598 package
.string(), package
.size());
1604 if (name
== String16("left")) {
1605 printf("Adding bag left: file=%s, line=%d, type=%s\n",
1606 sourcePos
.file
.striing(), sourcePos
.line
, String8(type
).string());
1609 if (overlay
&& !mBundle
->getAutoAddOverlay() && !hasBagOrEntry(package
, type
, name
)) {
1610 bool canAdd
= false;
1611 sp
<Package
> p
= mPackages
.valueFor(package
);
1613 sp
<Type
> t
= p
->getTypes().valueFor(type
);
1615 if (t
->getCanAddEntries().indexOf(name
) >= 0) {
1621 sourcePos
.error("Resource does not already exist in overlay at '%s'; use <add-resource> to add.\n",
1622 String8(name
).string());
1623 return UNKNOWN_ERROR
;
1626 sp
<Entry
> e
= getEntry(package
, type
, name
, sourcePos
, overlay
, params
);
1628 return UNKNOWN_ERROR
;
1631 // If a parent is explicitly specified, set it.
1632 if (bagParent
.size() > 0) {
1633 String16 curPar
= e
->getParent();
1634 if (curPar
.size() > 0 && curPar
!= bagParent
) {
1635 sourcePos
.error("Conflicting parents specified, was '%s', now '%s'\n",
1636 String8(e
->getParent()).string(),
1637 String8(bagParent
).string());
1638 return UNKNOWN_ERROR
;
1640 e
->setParent(bagParent
);
1643 if ((result
= e
->makeItABag(sourcePos
)) != NO_ERROR
) {
1647 if (overlay
&& replace
) {
1648 return e
->emptyBag(sourcePos
);
1653 status_t
ResourceTable::addBag(const SourcePos
& sourcePos
,
1654 const String16
& package
,
1655 const String16
& type
,
1656 const String16
& name
,
1657 const String16
& bagParent
,
1658 const String16
& bagKey
,
1659 const String16
& value
,
1660 const Vector
<StringPool::entry_style_span
>* style
,
1661 const ResTable_config
* params
,
1662 bool replace
, bool isId
, const int32_t format
)
1664 // Check for adding entries in other packages... for now we do
1665 // nothing. We need to do the right thing here to support skinning.
1666 uint32_t rid
= mAssets
->getIncludedResources()
1667 .identifierForName(name
.string(), name
.size(),
1668 type
.string(), type
.size(),
1669 package
.string(), package
.size());
1675 if (name
== String16("left")) {
1676 printf("Adding bag left: file=%s, line=%d, type=%s\n",
1677 sourcePos
.file
.striing(), sourcePos
.line
, String8(type
).string());
1680 sp
<Entry
> e
= getEntry(package
, type
, name
, sourcePos
, replace
, params
);
1682 return UNKNOWN_ERROR
;
1685 // If a parent is explicitly specified, set it.
1686 if (bagParent
.size() > 0) {
1687 String16 curPar
= e
->getParent();
1688 if (curPar
.size() > 0 && curPar
!= bagParent
) {
1689 sourcePos
.error("Conflicting parents specified, was '%s', now '%s'\n",
1690 String8(e
->getParent()).string(),
1691 String8(bagParent
).string());
1692 return UNKNOWN_ERROR
;
1694 e
->setParent(bagParent
);
1697 const bool first
= e
->getBag().indexOfKey(bagKey
) < 0;
1698 status_t err
= e
->addToBag(sourcePos
, bagKey
, value
, style
, replace
, isId
, format
);
1699 if (err
== NO_ERROR
&& first
) {
1705 bool ResourceTable::hasBagOrEntry(const String16
& package
,
1706 const String16
& type
,
1707 const String16
& name
) const
1709 // First look for this in the included resources...
1710 uint32_t rid
= mAssets
->getIncludedResources()
1711 .identifierForName(name
.string(), name
.size(),
1712 type
.string(), type
.size(),
1713 package
.string(), package
.size());
1718 sp
<Package
> p
= mPackages
.valueFor(package
);
1720 sp
<Type
> t
= p
->getTypes().valueFor(type
);
1722 sp
<ConfigList
> c
= t
->getConfigs().valueFor(name
);
1723 if (c
!= NULL
) return true;
1730 bool ResourceTable::hasBagOrEntry(const String16
& ref
,
1731 const String16
* defType
,
1732 const String16
* defPackage
)
1734 String16 package
, type
, name
;
1735 if (!ResTable::expandResourceRef(ref
.string(), ref
.size(), &package
, &type
, &name
,
1736 defType
, defPackage
? defPackage
:&mAssetsPackage
, NULL
)) {
1739 return hasBagOrEntry(package
, type
, name
);
1742 bool ResourceTable::appendComment(const String16
& package
,
1743 const String16
& type
,
1744 const String16
& name
,
1745 const String16
& comment
,
1748 if (comment
.size() <= 0) {
1752 sp
<Package
> p
= mPackages
.valueFor(package
);
1754 sp
<Type
> t
= p
->getTypes().valueFor(type
);
1756 sp
<ConfigList
> c
= t
->getConfigs().valueFor(name
);
1758 c
->appendComment(comment
, onlyIfEmpty
);
1766 bool ResourceTable::appendTypeComment(const String16
& package
,
1767 const String16
& type
,
1768 const String16
& name
,
1769 const String16
& comment
)
1771 if (comment
.size() <= 0) {
1775 sp
<Package
> p
= mPackages
.valueFor(package
);
1777 sp
<Type
> t
= p
->getTypes().valueFor(type
);
1779 sp
<ConfigList
> c
= t
->getConfigs().valueFor(name
);
1781 c
->appendTypeComment(comment
);
1789 void ResourceTable::canAddEntry(const SourcePos
& pos
,
1790 const String16
& package
, const String16
& type
, const String16
& name
)
1792 sp
<Type
> t
= getType(package
, type
, pos
);
1794 t
->canAddEntry(name
);
1798 size_t ResourceTable::size() const {
1799 return mPackages
.size();
1802 size_t ResourceTable::numLocalResources() const {
1806 bool ResourceTable::hasResources() const {
1807 return mNumLocal
> 0;
1810 sp
<AaptFile
> ResourceTable::flatten(Bundle
* bundle
)
1812 sp
<AaptFile
> data
= new AaptFile(String8(), AaptGroupEntry(), String8());
1813 status_t err
= flatten(bundle
, data
);
1814 return err
== NO_ERROR
? data
: NULL
;
1817 inline uint32_t ResourceTable::getResId(const sp
<Package
>& p
,
1821 return makeResId(p
->getAssignedId(), t
->getIndex(), nameId
);
1824 uint32_t ResourceTable::getResId(const String16
& package
,
1825 const String16
& type
,
1826 const String16
& name
,
1827 bool onlyPublic
) const
1829 sp
<Package
> p
= mPackages
.valueFor(package
);
1830 if (p
== NULL
) return 0;
1832 // First look for this in the included resources...
1833 uint32_t specFlags
= 0;
1834 uint32_t rid
= mAssets
->getIncludedResources()
1835 .identifierForName(name
.string(), name
.size(),
1836 type
.string(), type
.size(),
1837 package
.string(), package
.size(),
1841 if ((specFlags
& ResTable_typeSpec::SPEC_PUBLIC
) == 0) {
1846 if (Res_INTERNALID(rid
)) {
1849 return Res_MAKEID(p
->getAssignedId()-1,
1854 sp
<Type
> t
= p
->getTypes().valueFor(type
);
1855 if (t
== NULL
) return 0;
1856 sp
<ConfigList
> c
= t
->getConfigs().valueFor(name
);
1857 if (c
== NULL
) return 0;
1858 int32_t ei
= c
->getEntryIndex();
1859 if (ei
< 0) return 0;
1860 return getResId(p
, t
, ei
);
1863 uint32_t ResourceTable::getResId(const String16
& ref
,
1864 const String16
* defType
,
1865 const String16
* defPackage
,
1866 const char** outErrorMsg
,
1867 bool onlyPublic
) const
1869 String16 package
, type
, name
;
1870 if (!ResTable::expandResourceRef(
1871 ref
.string(), ref
.size(), &package
, &type
, &name
,
1872 defType
, defPackage
? defPackage
:&mAssetsPackage
,
1874 NOISY(printf("Expanding resource: ref=%s\n",
1875 String8(ref
).string()));
1876 NOISY(printf("Expanding resource: defType=%s\n",
1877 defType
? String8(*defType
).string() : "NULL"));
1878 NOISY(printf("Expanding resource: defPackage=%s\n",
1879 defPackage
? String8(*defPackage
).string() : "NULL"));
1880 NOISY(printf("Expanding resource: ref=%s\n", String8(ref
).string()));
1881 NOISY(printf("Expanded resource: p=%s, t=%s, n=%s, res=0\n",
1882 String8(package
).string(), String8(type
).string(),
1883 String8(name
).string()));
1886 uint32_t res
= getResId(package
, type
, name
, onlyPublic
);
1887 NOISY(printf("Expanded resource: p=%s, t=%s, n=%s, res=%d\n",
1888 String8(package
).string(), String8(type
).string(),
1889 String8(name
).string(), res
));
1892 *outErrorMsg
= "No resource found that matches the given name";
1897 bool ResourceTable::isValidResourceName(const String16
& s
)
1899 const char16_t* p
= s
.string();
1902 if ((*p
>= 'a' && *p
<= 'z')
1903 || (*p
>= 'A' && *p
<= 'Z')
1905 || (!first
&& *p
>= '0' && *p
<= '9')) {
1915 bool ResourceTable::stringToValue(Res_value
* outValue
, StringPool
* pool
,
1916 const String16
& str
,
1917 bool preserveSpaces
, bool coerceType
,
1919 const Vector
<StringPool::entry_style_span
>* style
,
1920 String16
* outStr
, void* accessorCookie
,
1926 if (style
== NULL
|| style
->size() == 0) {
1927 // Text is not styled so it can be any type... let's figure it out.
1928 res
= mAssets
->getIncludedResources()
1929 .stringToValue(outValue
, &finalStr
, str
.string(), str
.size(), preserveSpaces
,
1930 coerceType
, attrID
, NULL
, &mAssetsPackage
, this,
1931 accessorCookie
, attrType
);
1933 // Styled text can only be a string, and while collecting the style
1934 // information we have already processed that string!
1935 outValue
->size
= sizeof(Res_value
);
1937 outValue
->dataType
= Res_value::TYPE_STRING
;
1946 if (outValue
->dataType
== Res_value::TYPE_STRING
) {
1947 // Should do better merging styles.
1949 if (style
!= NULL
&& style
->size() > 0) {
1950 outValue
->data
= pool
->add(finalStr
, *style
);
1952 outValue
->data
= pool
->add(finalStr
, true);
1955 // Caller will fill this in later.
1968 uint32_t ResourceTable::getCustomResource(
1969 const String16
& package
, const String16
& type
, const String16
& name
) const
1971 //printf("getCustomResource: %s %s %s\n", String8(package).string(),
1972 // String8(type).string(), String8(name).string());
1973 sp
<Package
> p
= mPackages
.valueFor(package
);
1974 if (p
== NULL
) return 0;
1975 sp
<Type
> t
= p
->getTypes().valueFor(type
);
1976 if (t
== NULL
) return 0;
1977 sp
<ConfigList
> c
= t
->getConfigs().valueFor(name
);
1978 if (c
== NULL
) return 0;
1979 int32_t ei
= c
->getEntryIndex();
1980 if (ei
< 0) return 0;
1981 return getResId(p
, t
, ei
);
1984 uint32_t ResourceTable::getCustomResourceWithCreation(
1985 const String16
& package
, const String16
& type
, const String16
& name
,
1986 const bool createIfNotFound
)
1988 uint32_t resId
= getCustomResource(package
, type
, name
);
1989 if (resId
!= 0 || !createIfNotFound
) {
1992 String16
value("false");
1994 status_t status
= addEntry(mCurrentXmlPos
, package
, type
, name
, value
, NULL
, NULL
, true);
1995 if (status
== NO_ERROR
) {
1996 resId
= getResId(package
, type
, name
);
2002 uint32_t ResourceTable::getRemappedPackage(uint32_t origPackage
) const
2007 bool ResourceTable::getAttributeType(uint32_t attrID
, uint32_t* outType
)
2009 //printf("getAttributeType #%08x\n", attrID);
2011 if (getItemValue(attrID
, ResTable_map::ATTR_TYPE
, &value
)) {
2012 //printf("getAttributeType #%08x (%s): #%08x\n", attrID,
2013 // String8(getEntry(attrID)->getName()).string(), value.data);
2014 *outType
= value
.data
;
2020 bool ResourceTable::getAttributeMin(uint32_t attrID
, uint32_t* outMin
)
2022 //printf("getAttributeMin #%08x\n", attrID);
2024 if (getItemValue(attrID
, ResTable_map::ATTR_MIN
, &value
)) {
2025 *outMin
= value
.data
;
2031 bool ResourceTable::getAttributeMax(uint32_t attrID
, uint32_t* outMax
)
2033 //printf("getAttributeMax #%08x\n", attrID);
2035 if (getItemValue(attrID
, ResTable_map::ATTR_MAX
, &value
)) {
2036 *outMax
= value
.data
;
2042 uint32_t ResourceTable::getAttributeL10N(uint32_t attrID
)
2044 //printf("getAttributeL10N #%08x\n", attrID);
2046 if (getItemValue(attrID
, ResTable_map::ATTR_L10N
, &value
)) {
2049 return ResTable_map::L10N_NOT_REQUIRED
;
2052 bool ResourceTable::getLocalizationSetting()
2054 return mBundle
->getRequireLocalization();
2057 void ResourceTable::reportError(void* accessorCookie
, const char* fmt
, ...)
2059 if (accessorCookie
!= NULL
&& fmt
!= NULL
) {
2060 AccessorCookie
* ac
= (AccessorCookie
*)accessorCookie
;
2065 retval
= vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
2067 ac
->sourcePos
.error("Error: %s (at '%s' with value '%s').\n",
2068 buf
, ac
->attr
.string(), ac
->value
.string());
2072 bool ResourceTable::getAttributeKeys(
2073 uint32_t attrID
, Vector
<String16
>* outKeys
)
2075 sp
<const Entry
> e
= getEntry(attrID
);
2077 const size_t N
= e
->getBag().size();
2078 for (size_t i
=0; i
<N
; i
++) {
2079 const String16
& key
= e
->getBag().keyAt(i
);
2080 if (key
.size() > 0 && key
.string()[0] != '^') {
2089 bool ResourceTable::getAttributeEnum(
2090 uint32_t attrID
, const char16_t* name
, size_t nameLen
,
2091 Res_value
* outValue
)
2093 //printf("getAttributeEnum #%08x %s\n", attrID, String8(name, nameLen).string());
2094 String16
nameStr(name
, nameLen
);
2095 sp
<const Entry
> e
= getEntry(attrID
);
2097 const size_t N
= e
->getBag().size();
2098 for (size_t i
=0; i
<N
; i
++) {
2099 //printf("Comparing %s to %s\n", String8(name, nameLen).string(),
2100 // String8(e->getBag().keyAt(i)).string());
2101 if (e
->getBag().keyAt(i
) == nameStr
) {
2102 return getItemValue(attrID
, e
->getBag().valueAt(i
).bagKeyId
, outValue
);
2109 bool ResourceTable::getAttributeFlags(
2110 uint32_t attrID
, const char16_t* name
, size_t nameLen
,
2111 Res_value
* outValue
)
2113 outValue
->dataType
= Res_value::TYPE_INT_HEX
;
2116 //printf("getAttributeFlags #%08x %s\n", attrID, String8(name, nameLen).string());
2117 String16
nameStr(name
, nameLen
);
2118 sp
<const Entry
> e
= getEntry(attrID
);
2120 const size_t N
= e
->getBag().size();
2122 const char16_t* end
= name
+ nameLen
;
2123 const char16_t* pos
= name
;
2124 bool failed
= false;
2125 while (pos
< end
&& !failed
) {
2126 const char16_t* start
= pos
;
2128 while (pos
< end
&& *pos
!= '|') {
2132 String16
nameStr(start
, pos
-start
);
2134 for (i
=0; i
<N
; i
++) {
2135 //printf("Comparing \"%s\" to \"%s\"\n", String8(nameStr).string(),
2136 // String8(e->getBag().keyAt(i)).string());
2137 if (e
->getBag().keyAt(i
) == nameStr
) {
2139 bool got
= getItemValue(attrID
, e
->getBag().valueAt(i
).bagKeyId
, &val
);
2143 //printf("Got value: 0x%08x\n", val.data);
2144 outValue
->data
|= val
.data
;
2150 // Didn't find this flag identifier.
2163 status_t
ResourceTable::assignResourceIds()
2165 const size_t N
= mOrderedPackages
.size();
2167 status_t firstError
= NO_ERROR
;
2169 // First generate all bag attributes and assign indices.
2170 for (pi
=0; pi
<N
; pi
++) {
2171 sp
<Package
> p
= mOrderedPackages
.itemAt(pi
);
2172 if (p
== NULL
|| p
->getTypes().size() == 0) {
2177 status_t err
= p
->applyPublicTypeOrder();
2178 if (err
!= NO_ERROR
&& firstError
== NO_ERROR
) {
2182 // Generate attributes...
2183 const size_t N
= p
->getOrderedTypes().size();
2185 for (ti
=0; ti
<N
; ti
++) {
2186 sp
<Type
> t
= p
->getOrderedTypes().itemAt(ti
);
2190 const size_t N
= t
->getOrderedConfigs().size();
2191 for (size_t ci
=0; ci
<N
; ci
++) {
2192 sp
<ConfigList
> c
= t
->getOrderedConfigs().itemAt(ci
);
2196 const size_t N
= c
->getEntries().size();
2197 for (size_t ei
=0; ei
<N
; ei
++) {
2198 sp
<Entry
> e
= c
->getEntries().valueAt(ei
);
2202 status_t err
= e
->generateAttributes(this, p
->getName());
2203 if (err
!= NO_ERROR
&& firstError
== NO_ERROR
) {
2210 const SourcePos
unknown(String8("????"), 0);
2211 sp
<Type
> attr
= p
->getType(String16("attr"), unknown
);
2213 // Assign indices...
2214 for (ti
=0; ti
<N
; ti
++) {
2215 sp
<Type
> t
= p
->getOrderedTypes().itemAt(ti
);
2219 err
= t
->applyPublicEntryOrder();
2220 if (err
!= NO_ERROR
&& firstError
== NO_ERROR
) {
2224 const size_t N
= t
->getOrderedConfigs().size();
2227 LOG_ALWAYS_FATAL_IF(ti
== 0 && attr
!= t
,
2228 "First type is not attr!");
2230 for (size_t ei
=0; ei
<N
; ei
++) {
2231 sp
<ConfigList
> c
= t
->getOrderedConfigs().itemAt(ei
);
2235 c
->setEntryIndex(ei
);
2239 // Assign resource IDs to keys in bags...
2240 for (ti
=0; ti
<N
; ti
++) {
2241 sp
<Type
> t
= p
->getOrderedTypes().itemAt(ti
);
2245 const size_t N
= t
->getOrderedConfigs().size();
2246 for (size_t ci
=0; ci
<N
; ci
++) {
2247 sp
<ConfigList
> c
= t
->getOrderedConfigs().itemAt(ci
);
2248 //printf("Ordered config #%d: %p\n", ci, c.get());
2249 const size_t N
= c
->getEntries().size();
2250 for (size_t ei
=0; ei
<N
; ei
++) {
2251 sp
<Entry
> e
= c
->getEntries().valueAt(ei
);
2255 status_t err
= e
->assignResourceIds(this, p
->getName());
2256 if (err
!= NO_ERROR
&& firstError
== NO_ERROR
) {
2266 status_t
ResourceTable::addSymbols(const sp
<AaptSymbols
>& outSymbols
) {
2267 const size_t N
= mOrderedPackages
.size();
2270 for (pi
=0; pi
<N
; pi
++) {
2271 sp
<Package
> p
= mOrderedPackages
.itemAt(pi
);
2272 if (p
->getTypes().size() == 0) {
2277 const size_t N
= p
->getOrderedTypes().size();
2280 for (ti
=0; ti
<N
; ti
++) {
2281 sp
<Type
> t
= p
->getOrderedTypes().itemAt(ti
);
2285 const size_t N
= t
->getOrderedConfigs().size();
2286 sp
<AaptSymbols
> typeSymbols
;
2287 typeSymbols
= outSymbols
->addNestedSymbol(String8(t
->getName()), t
->getPos());
2288 for (size_t ci
=0; ci
<N
; ci
++) {
2289 sp
<ConfigList
> c
= t
->getOrderedConfigs().itemAt(ci
);
2293 uint32_t rid
= getResId(p
, t
, ci
);
2295 return UNKNOWN_ERROR
;
2297 if (Res_GETPACKAGE(rid
) == (size_t)(p
->getAssignedId()-1)) {
2298 typeSymbols
->addSymbol(String8(c
->getName()), rid
, c
->getPos());
2300 String16
comment(c
->getComment());
2301 typeSymbols
->appendComment(String8(c
->getName()), comment
, c
->getPos());
2302 //printf("Type symbol %s comment: %s\n", String8(e->getName()).string(),
2303 // String8(comment).string());
2304 comment
= c
->getTypeComment();
2305 typeSymbols
->appendTypeComment(String8(c
->getName()), comment
);
2308 printf("**** NO MATCH: 0x%08x vs 0x%08x\n",
2309 Res_GETPACKAGE(rid
), p
->getAssignedId());
2320 ResourceTable::addLocalization(const String16
& name
, const String8
& locale
)
2322 mLocalizations
[name
].insert(locale
);
2327 * Flag various sorts of localization problems. '+' indicates checks already implemented;
2328 * '-' indicates checks that will be implemented in the future.
2330 * + A localized string for which no default-locale version exists => warning
2331 * + A string for which no version in an explicitly-requested locale exists => warning
2332 * + A localized translation of an translateable="false" string => warning
2333 * - A localized string not provided in every locale used by the table
2336 ResourceTable::validateLocalizations(void)
2338 status_t err
= NO_ERROR
;
2339 const String8 defaultLocale
;
2341 // For all strings...
2342 for (map
<String16
, set
<String8
> >::iterator nameIter
= mLocalizations
.begin();
2343 nameIter
!= mLocalizations
.end();
2345 const set
<String8
>& configSet
= nameIter
->second
; // naming convenience
2347 // Look for strings with no default localization
2348 if (configSet
.count(defaultLocale
) == 0) {
2349 fprintf(stdout
, "aapt: warning: string '%s' has no default translation in %s; found:",
2350 String8(nameIter
->first
).string(), mBundle
->getResourceSourceDirs()[0]);
2351 for (set
<String8
>::iterator locales
= configSet
.begin();
2352 locales
!= configSet
.end();
2354 fprintf(stdout
, " %s", (*locales
).string());
2356 fprintf(stdout
, "\n");
2357 // !!! TODO: throw an error here in some circumstances
2360 // Check that all requested localizations are present for this string
2361 if (mBundle
->getConfigurations() != NULL
&& mBundle
->getRequireLocalization()) {
2362 const char* allConfigs
= mBundle
->getConfigurations();
2363 const char* start
= allConfigs
;
2368 comma
= strchr(start
, ',');
2369 if (comma
!= NULL
) {
2370 config
.setTo(start
, comma
- start
);
2373 config
.setTo(start
);
2376 // don't bother with the pseudolocale "zz_ZZ"
2377 if (config
!= "zz_ZZ") {
2378 if (configSet
.find(config
) == configSet
.end()) {
2379 // okay, no specific localization found. it's possible that we are
2380 // requiring a specific regional localization [e.g. de_DE] but there is an
2381 // available string in the generic language localization [e.g. de];
2382 // consider that string to have fulfilled the localization requirement.
2383 String8
region(config
.string(), 2);
2384 if (configSet
.find(region
) == configSet
.end()) {
2385 if (configSet
.count(defaultLocale
) == 0) {
2386 fprintf(stdout
, "aapt: warning: "
2387 "**** string '%s' has no default or required localization "
2389 String8(nameIter
->first
).string(),
2391 mBundle
->getResourceSourceDirs()[0]);
2396 } while (comma
!= NULL
);
2405 ResourceFilter::parse(const char* arg
)
2411 const char* p
= arg
;
2420 String8
part(p
, q
-p
);
2422 if (part
== "zz_ZZ") {
2423 mContainsPseudo
= true;
2427 if (AaptGroupEntry::parseNamePart(part
, &axis
, &value
)) {
2428 fprintf(stderr
, "Invalid configuration: %s\n", arg
);
2429 fprintf(stderr
, " ");
2430 for (int i
=0; i
<p
-arg
; i
++) {
2431 fprintf(stderr
, " ");
2433 for (int i
=0; i
<q
-p
; i
++) {
2434 fprintf(stderr
, "^");
2436 fprintf(stderr
, "\n");
2440 ssize_t index
= mData
.indexOfKey(axis
);
2442 mData
.add(axis
, SortedVector
<uint32_t>());
2444 SortedVector
<uint32_t>& sv
= mData
.editValueFor(axis
);
2446 // if it's a locale with a region, also match an unmodified locale of the
2448 if (axis
== AXIS_LANGUAGE
) {
2449 if (value
& 0xffff0000) {
2450 sv
.add(value
& 0x0000ffff);
2462 ResourceFilter::match(int axis
, uint32_t value
)
2465 // they didn't specify anything so take everything
2468 ssize_t index
= mData
.indexOfKey(axis
);
2470 // we didn't request anything on this axis so take everything
2473 const SortedVector
<uint32_t>& sv
= mData
.valueAt(index
);
2474 return sv
.indexOf(value
) >= 0;
2478 ResourceFilter::match(const ResTable_config
& config
)
2480 if (config
.locale
) {
2481 uint32_t locale
= (config
.country
[1] << 24) | (config
.country
[0] << 16)
2482 | (config
.language
[1] << 8) | (config
.language
[0]);
2483 if (!match(AXIS_LANGUAGE
, locale
)) {
2487 if (!match(AXIS_ORIENTATION
, config
.orientation
)) {
2490 if (!match(AXIS_UIMODETYPE
, (config
.uiMode
&ResTable_config::MASK_UI_MODE_TYPE
))) {
2493 if (!match(AXIS_UIMODENIGHT
, (config
.uiMode
&ResTable_config::MASK_UI_MODE_NIGHT
))) {
2496 if (!match(AXIS_DENSITY
, config
.density
)) {
2499 if (!match(AXIS_TOUCHSCREEN
, config
.touchscreen
)) {
2502 if (!match(AXIS_KEYSHIDDEN
, config
.inputFlags
)) {
2505 if (!match(AXIS_KEYBOARD
, config
.keyboard
)) {
2508 if (!match(AXIS_NAVIGATION
, config
.navigation
)) {
2511 if (!match(AXIS_SCREENSIZE
, config
.screenSize
)) {
2514 if (!match(AXIS_VERSION
, config
.version
)) {
2520 status_t
ResourceTable::flatten(Bundle
* bundle
, const sp
<AaptFile
>& dest
)
2522 ResourceFilter filter
;
2523 status_t err
= filter
.parse(bundle
->getConfigurations());
2524 if (err
!= NO_ERROR
) {
2528 const size_t N
= mOrderedPackages
.size();
2531 // Iterate through all data, collecting all values (strings,
2532 // references, etc).
2533 StringPool valueStrings
= StringPool(false, bundle
->getUTF8());
2534 ResourceConfigReferences configRefs
;
2535 for (pi
=0; pi
<N
; pi
++) {
2536 sp
<Package
> p
= mOrderedPackages
.itemAt(pi
);
2537 if (p
->getTypes().size() == 0) {
2542 StringPool typeStrings
= StringPool(false, bundle
->getUTF8());
2543 StringPool keyStrings
= StringPool(false, bundle
->getUTF8());
2545 const size_t N
= p
->getOrderedTypes().size();
2546 for (size_t ti
=0; ti
<N
; ti
++) {
2547 sp
<Type
> t
= p
->getOrderedTypes().itemAt(ti
);
2549 typeStrings
.add(String16("<empty>"), false);
2552 typeStrings
.add(t
->getName(), false);
2554 const size_t N
= t
->getOrderedConfigs().size();
2555 for (size_t ci
=0; ci
<N
; ci
++) {
2556 sp
<ConfigList
> c
= t
->getOrderedConfigs().itemAt(ci
);
2560 const size_t N
= c
->getEntries().size();
2561 for (size_t ei
=0; ei
<N
; ei
++) {
2562 ConfigDescription config
= c
->getEntries().keyAt(ei
);
2563 if (!filter
.match(config
)) {
2566 sp
<Entry
> e
= c
->getEntries().valueAt(ei
);
2570 e
->setNameIndex(keyStrings
.add(e
->getName(), true));
2571 status_t err
= e
->prepareFlatten(&valueStrings
, this);
2572 if (err
!= NO_ERROR
) {
2575 if (e
->getType() == Entry::TYPE_ITEM
) {
2576 const Item
* item
= e
->getItem();
2578 uint32_t poolIndex
= item
->parsedValue
.data
;
2579 configRefs
.add(poolIndex
, config
);
2586 p
->setTypeStrings(typeStrings
.createStringBlock());
2587 p
->setKeyStrings(keyStrings
.createStringBlock());
2590 NOISY_REF(configRefs
.dump();)
2592 // Trim all entries in config tables that are not roots for that string.
2593 // i.e., rely on the resource system to grab the string from the more
2594 // generic pool during runtime to save space.
2595 for (pi
=0; pi
<N
; pi
++) {
2596 sp
<Package
> p
= mOrderedPackages
.itemAt(pi
);
2597 if (p
->getTypes().size() == 0) {
2601 const size_t TN
= p
->getOrderedTypes().size();
2602 for (size_t ti
=0; ti
<TN
; ti
++) {
2603 sp
<Type
> t
= p
->getOrderedTypes().itemAt(ti
);
2607 size_t CN
= t
->getOrderedConfigs().size();
2608 for (size_t ci
=0; ci
<CN
; ci
++) {
2609 sp
<ConfigList
> c
= t
->getOrderedConfigs().itemAt(ci
);
2613 DefaultKeyedVector
<ConfigDescription
, sp
<Entry
> > newEntries
;
2614 size_t EN
= c
->getEntries().size();
2615 for (size_t ei
=0; ei
<EN
; ++ei
) {
2616 ConfigDescription config
= c
->getEntries().keyAt(ei
);
2617 if (!filter
.match(config
)) {
2620 sp
<Entry
> e
= c
->getEntries().valueAt(ei
);
2624 if (e
->getType() == Entry::TYPE_ITEM
) {
2625 const Item
* item
= e
->getItem();
2627 uint32_t poolIndex
= item
->parsedValue
.data
;
2628 if (!configRefs
.isRoot(poolIndex
, config
)) {
2630 printf(" ConfigRef %d: removing ", poolIndex
);
2634 c
->removeEntryAt(ei
);
2635 t
->removeUniqueConfig(config
);
2642 // We removed all the entries from a config, so remove the
2645 printf(" ConfigRef REMOVED ENTIRE CONFIG\n");
2647 t
->removeOrderedConfigAt(ci
);
2656 // Now build the array of package chunks.
2657 Vector
<sp
<AaptFile
> > flatPackages
;
2658 for (pi
=0; pi
<N
; pi
++) {
2659 sp
<Package
> p
= mOrderedPackages
.itemAt(pi
);
2660 if (p
->getTypes().size() == 0) {
2665 const size_t N
= p
->getTypeStrings().size();
2667 const size_t baseSize
= sizeof(ResTable_package
);
2669 // Start the package data.
2670 sp
<AaptFile
> data
= new AaptFile(String8(), AaptGroupEntry(), String8());
2671 ResTable_package
* header
= (ResTable_package
*)data
->editData(baseSize
);
2672 if (header
== NULL
) {
2673 fprintf(stderr
, "ERROR: out of memory creating ResTable_package\n");
2676 memset(header
, 0, sizeof(*header
));
2677 header
->header
.type
= htods(RES_TABLE_PACKAGE_TYPE
);
2678 header
->header
.headerSize
= htods(sizeof(*header
));
2679 header
->id
= htodl(p
->getAssignedId());
2680 strcpy16_htod(header
->name
, p
->getName().string());
2682 // Write the string blocks.
2683 const size_t typeStringsStart
= data
->getSize();
2684 sp
<AaptFile
> strFile
= p
->getTypeStringsData();
2685 ssize_t amt
= data
->writeData(strFile
->getData(), strFile
->getSize());
2686 #if PRINT_STRING_METRICS
2687 fprintf(stderr
, "**** type strings: %d\n", amt
);
2693 const size_t keyStringsStart
= data
->getSize();
2694 strFile
= p
->getKeyStringsData();
2695 amt
= data
->writeData(strFile
->getData(), strFile
->getSize());
2696 #if PRINT_STRING_METRICS
2697 fprintf(stderr
, "**** key strings: %d\n", amt
);
2704 // Build the type chunks inside of this package.
2705 for (size_t ti
=0; ti
<N
; ti
++) {
2706 // Retrieve them in the same order as the type string block.
2708 String16
typeName(p
->getTypeStrings().stringAt(ti
, &len
));
2709 sp
<Type
> t
= p
->getTypes().valueFor(typeName
);
2710 LOG_ALWAYS_FATAL_IF(t
== NULL
&& typeName
!= String16("<empty>"),
2711 "Type name %s not found",
2712 String8(typeName
).string());
2714 const size_t N
= t
!= NULL
? t
->getOrderedConfigs().size() : 0;
2716 // First write the typeSpec chunk, containing information about
2717 // each resource entry in this type.
2719 const size_t typeSpecSize
= sizeof(ResTable_typeSpec
) + sizeof(uint32_t)*N
;
2720 const size_t typeSpecStart
= data
->getSize();
2721 ResTable_typeSpec
* tsHeader
= (ResTable_typeSpec
*)
2722 (((uint8_t*)data
->editData(typeSpecStart
+typeSpecSize
)) + typeSpecStart
);
2723 if (tsHeader
== NULL
) {
2724 fprintf(stderr
, "ERROR: out of memory creating ResTable_typeSpec\n");
2727 memset(tsHeader
, 0, sizeof(*tsHeader
));
2728 tsHeader
->header
.type
= htods(RES_TABLE_TYPE_SPEC_TYPE
);
2729 tsHeader
->header
.headerSize
= htods(sizeof(*tsHeader
));
2730 tsHeader
->header
.size
= htodl(typeSpecSize
);
2731 tsHeader
->id
= ti
+1;
2732 tsHeader
->entryCount
= htodl(N
);
2734 uint32_t* typeSpecFlags
= (uint32_t*)
2735 (((uint8_t*)data
->editData())
2736 + typeSpecStart
+ sizeof(ResTable_typeSpec
));
2737 memset(typeSpecFlags
, 0, sizeof(uint32_t)*N
);
2739 for (size_t ei
=0; ei
<N
; ei
++) {
2740 sp
<ConfigList
> cl
= t
->getOrderedConfigs().itemAt(ei
);
2741 if (cl
->getPublic()) {
2742 typeSpecFlags
[ei
] |= htodl(ResTable_typeSpec::SPEC_PUBLIC
);
2744 const size_t CN
= cl
->getEntries().size();
2745 for (size_t ci
=0; ci
<CN
; ci
++) {
2746 if (!filter
.match(cl
->getEntries().keyAt(ci
))) {
2749 for (size_t cj
=ci
+1; cj
<CN
; cj
++) {
2750 if (!filter
.match(cl
->getEntries().keyAt(cj
))) {
2753 typeSpecFlags
[ei
] |= htodl(
2754 cl
->getEntries().keyAt(ci
).diff(cl
->getEntries().keyAt(cj
)));
2760 // We need to write one type chunk for each configuration for
2761 // which we have entries in this type.
2762 const size_t NC
= t
->getUniqueConfigs().size();
2764 const size_t typeSize
= sizeof(ResTable_type
) + sizeof(uint32_t)*N
;
2766 for (size_t ci
=0; ci
<NC
; ci
++) {
2767 ConfigDescription config
= t
->getUniqueConfigs().itemAt(ci
);
2769 NOISY(printf("Writing config %d config: imsi:%d/%d lang:%c%c cnt:%c%c "
2770 "orien:%d ui:%d touch:%d density:%d key:%d inp:%d nav:%d w:%d h:%d\n",
2772 config
.mcc
, config
.mnc
,
2773 config
.language
[0] ? config
.language
[0] : '-',
2774 config
.language
[1] ? config
.language
[1] : '-',
2775 config
.country
[0] ? config
.country
[0] : '-',
2776 config
.country
[1] ? config
.country
[1] : '-',
2785 config
.screenHeight
));
2787 if (!filter
.match(config
)) {
2791 const size_t typeStart
= data
->getSize();
2793 ResTable_type
* tHeader
= (ResTable_type
*)
2794 (((uint8_t*)data
->editData(typeStart
+typeSize
)) + typeStart
);
2795 if (tHeader
== NULL
) {
2796 fprintf(stderr
, "ERROR: out of memory creating ResTable_type\n");
2800 memset(tHeader
, 0, sizeof(*tHeader
));
2801 tHeader
->header
.type
= htods(RES_TABLE_TYPE_TYPE
);
2802 tHeader
->header
.headerSize
= htods(sizeof(*tHeader
));
2804 tHeader
->entryCount
= htodl(N
);
2805 tHeader
->entriesStart
= htodl(typeSize
);
2806 tHeader
->config
= config
;
2807 NOISY(printf("Writing type %d config: imsi:%d/%d lang:%c%c cnt:%c%c "
2808 "orien:%d ui:%d touch:%d density:%d key:%d inp:%d nav:%d w:%d h:%d\n",
2810 tHeader
->config
.mcc
, tHeader
->config
.mnc
,
2811 tHeader
->config
.language
[0] ? tHeader
->config
.language
[0] : '-',
2812 tHeader
->config
.language
[1] ? tHeader
->config
.language
[1] : '-',
2813 tHeader
->config
.country
[0] ? tHeader
->config
.country
[0] : '-',
2814 tHeader
->config
.country
[1] ? tHeader
->config
.country
[1] : '-',
2815 tHeader
->config
.orientation
,
2816 tHeader
->config
.uiMode
,
2817 tHeader
->config
.touchscreen
,
2818 tHeader
->config
.density
,
2819 tHeader
->config
.keyboard
,
2820 tHeader
->config
.inputFlags
,
2821 tHeader
->config
.navigation
,
2822 tHeader
->config
.screenWidth
,
2823 tHeader
->config
.screenHeight
));
2824 tHeader
->config
.swapHtoD();
2826 // Build the entries inside of this type.
2827 for (size_t ei
=0; ei
<N
; ei
++) {
2828 sp
<ConfigList
> cl
= t
->getOrderedConfigs().itemAt(ei
);
2829 sp
<Entry
> e
= cl
->getEntries().valueFor(config
);
2831 // Set the offset for this entry in its type.
2832 uint32_t* index
= (uint32_t*)
2833 (((uint8_t*)data
->editData())
2834 + typeStart
+ sizeof(ResTable_type
));
2836 index
[ei
] = htodl(data
->getSize()-typeStart
-typeSize
);
2838 // Create the entry.
2839 ssize_t amt
= e
->flatten(bundle
, data
, cl
->getPublic());
2844 index
[ei
] = htodl(ResTable_type::NO_ENTRY
);
2848 // Fill in the rest of the type information.
2849 tHeader
= (ResTable_type
*)
2850 (((uint8_t*)data
->editData()) + typeStart
);
2851 tHeader
->header
.size
= htodl(data
->getSize()-typeStart
);
2855 // Fill in the rest of the package information.
2856 header
= (ResTable_package
*)data
->editData();
2857 header
->header
.size
= htodl(data
->getSize());
2858 header
->typeStrings
= htodl(typeStringsStart
);
2859 header
->lastPublicType
= htodl(p
->getTypeStrings().size());
2860 header
->keyStrings
= htodl(keyStringsStart
);
2861 header
->lastPublicKey
= htodl(p
->getKeyStrings().size());
2863 flatPackages
.add(data
);
2866 // And now write out the final chunks.
2867 const size_t dataStart
= dest
->getSize();
2871 ResTable_header header
;
2872 memset(&header
, 0, sizeof(header
));
2873 header
.header
.type
= htods(RES_TABLE_TYPE
);
2874 header
.header
.headerSize
= htods(sizeof(header
));
2875 header
.packageCount
= htodl(flatPackages
.size());
2876 status_t err
= dest
->writeData(&header
, sizeof(header
));
2877 if (err
!= NO_ERROR
) {
2878 fprintf(stderr
, "ERROR: out of memory creating ResTable_header\n");
2883 ssize_t strStart
= dest
->getSize();
2884 err
= valueStrings
.writeStringBlock(dest
);
2885 if (err
!= NO_ERROR
) {
2889 ssize_t amt
= (dest
->getSize()-strStart
);
2891 #if PRINT_STRING_METRICS
2892 fprintf(stderr
, "**** value strings: %d\n", amt
);
2893 fprintf(stderr
, "**** total strings: %d\n", strAmt
);
2896 for (pi
=0; pi
<flatPackages
.size(); pi
++) {
2897 err
= dest
->writeData(flatPackages
[pi
]->getData(),
2898 flatPackages
[pi
]->getSize());
2899 if (err
!= NO_ERROR
) {
2900 fprintf(stderr
, "ERROR: out of memory creating package chunk for ResTable_header\n");
2905 ResTable_header
* header
= (ResTable_header
*)
2906 (((uint8_t*)dest
->getData()) + dataStart
);
2907 header
->header
.size
= htodl(dest
->getSize() - dataStart
);
2909 NOISY(aout
<< "Resource table:"
2910 << HexDump(dest
->getData(), dest
->getSize()) << endl
);
2912 #if PRINT_STRING_METRICS
2913 fprintf(stderr
, "**** total resource table size: %d / %d%% strings\n",
2914 dest
->getSize(), (strAmt
*100)/dest
->getSize());
2920 void ResourceTable::writePublicDefinitions(const String16
& package
, FILE* fp
)
2923 "<!-- This file contains <public> resource definitions for all\n"
2924 " resources that were generated from the source data. -->\n"
2928 writePublicDefinitions(package
, fp
, true);
2929 writePublicDefinitions(package
, fp
, false);
2936 void ResourceTable::writePublicDefinitions(const String16
& package
, FILE* fp
, bool pub
)
2938 bool didHeader
= false;
2940 sp
<Package
> pkg
= mPackages
.valueFor(package
);
2942 const size_t NT
= pkg
->getOrderedTypes().size();
2943 for (size_t i
=0; i
<NT
; i
++) {
2944 sp
<Type
> t
= pkg
->getOrderedTypes().itemAt(i
);
2949 bool didType
= false;
2951 const size_t NC
= t
->getOrderedConfigs().size();
2952 for (size_t j
=0; j
<NC
; j
++) {
2953 sp
<ConfigList
> c
= t
->getOrderedConfigs().itemAt(j
);
2958 if (c
->getPublic() != pub
) {
2968 fprintf(fp
," <!-- PUBLIC SECTION. These resources have been declared public.\n");
2969 fprintf(fp
," Changes to these definitions will break binary compatibility. -->\n\n");
2971 fprintf(fp
," <!-- PRIVATE SECTION. These resources have not been declared public.\n");
2972 fprintf(fp
," You can make them public my moving these lines into a file in res/values. -->\n\n");
2977 const size_t NE
= c
->getEntries().size();
2978 for (size_t k
=0; k
<NE
; k
++) {
2979 const SourcePos
& pos
= c
->getEntries().valueAt(k
)->getPos();
2980 if (pos
.file
!= "") {
2981 fprintf(fp
," <!-- Declared at %s:%d -->\n",
2982 pos
.file
.string(), pos
.line
);
2986 fprintf(fp
, " <public type=\"%s\" name=\"%s\" id=\"0x%08x\" />\n",
2987 String8(t
->getName()).string(),
2988 String8(c
->getName()).string(),
2989 getResId(pkg
, t
, c
->getEntryIndex()));
2995 ResourceTable::Item::Item(const SourcePos
& _sourcePos
,
2997 const String16
& _value
,
2998 const Vector
<StringPool::entry_style_span
>* _style
,
3000 : sourcePos(_sourcePos
)
3012 status_t
ResourceTable::Entry::makeItABag(const SourcePos
& sourcePos
)
3014 if (mType
== TYPE_BAG
) {
3017 if (mType
== TYPE_UNKNOWN
) {
3021 sourcePos
.error("Resource entry %s is already defined as a single item.\n"
3022 "%s:%d: Originally defined here.\n",
3023 String8(mName
).string(),
3024 mItem
.sourcePos
.file
.string(), mItem
.sourcePos
.line
);
3025 return UNKNOWN_ERROR
;
3028 status_t
ResourceTable::Entry::setItem(const SourcePos
& sourcePos
,
3029 const String16
& value
,
3030 const Vector
<StringPool::entry_style_span
>* style
,
3032 const bool overwrite
)
3034 Item
item(sourcePos
, false, value
, style
);
3036 if (mType
== TYPE_BAG
) {
3037 const Item
& item(mBag
.valueAt(0));
3038 sourcePos
.error("Resource entry %s is already defined as a bag.\n"
3039 "%s:%d: Originally defined here.\n",
3040 String8(mName
).string(),
3041 item
.sourcePos
.file
.string(), item
.sourcePos
.line
);
3042 return UNKNOWN_ERROR
;
3044 if ( (mType
!= TYPE_UNKNOWN
) && (overwrite
== false) ) {
3045 sourcePos
.error("Resource entry %s is already defined.\n"
3046 "%s:%d: Originally defined here.\n",
3047 String8(mName
).string(),
3048 mItem
.sourcePos
.file
.string(), mItem
.sourcePos
.line
);
3049 return UNKNOWN_ERROR
;
3054 mItemFormat
= format
;
3058 status_t
ResourceTable::Entry::addToBag(const SourcePos
& sourcePos
,
3059 const String16
& key
, const String16
& value
,
3060 const Vector
<StringPool::entry_style_span
>* style
,
3061 bool replace
, bool isId
, int32_t format
)
3063 status_t err
= makeItABag(sourcePos
);
3064 if (err
!= NO_ERROR
) {
3068 Item
item(sourcePos
, isId
, value
, style
, format
);
3070 // XXX NOTE: there is an error if you try to have a bag with two keys,
3071 // one an attr and one an id, with the same name. Not something we
3072 // currently ever have to worry about.
3073 ssize_t origKey
= mBag
.indexOfKey(key
);
3076 const Item
& item(mBag
.valueAt(origKey
));
3077 sourcePos
.error("Resource entry %s already has bag item %s.\n"
3078 "%s:%d: Originally defined here.\n",
3079 String8(mName
).string(), String8(key
).string(),
3080 item
.sourcePos
.file
.string(), item
.sourcePos
.line
);
3081 return UNKNOWN_ERROR
;
3083 //printf("Replacing %s with %s\n",
3084 // String8(mBag.valueFor(key).value).string(), String8(value).string());
3085 mBag
.replaceValueFor(key
, item
);
3088 mBag
.add(key
, item
);
3092 status_t
ResourceTable::Entry::emptyBag(const SourcePos
& sourcePos
)
3094 status_t err
= makeItABag(sourcePos
);
3095 if (err
!= NO_ERROR
) {
3103 status_t
ResourceTable::Entry::generateAttributes(ResourceTable
* table
,
3104 const String16
& package
)
3106 const String16
attr16("attr");
3107 const String16
id16("id");
3108 const size_t N
= mBag
.size();
3109 for (size_t i
=0; i
<N
; i
++) {
3110 const String16
& key
= mBag
.keyAt(i
);
3111 const Item
& it
= mBag
.valueAt(i
);
3113 if (!table
->hasBagOrEntry(key
, &id16
, &package
)) {
3114 String16
value("false");
3115 status_t err
= table
->addEntry(SourcePos(String8("<generated>"), 0), package
,
3117 if (err
!= NO_ERROR
) {
3121 } else if (!table
->hasBagOrEntry(key
, &attr16
, &package
)) {
3124 // fprintf(stderr, "ERROR: Bag attribute '%s' has not been defined.\n",
3125 // String8(key).string());
3126 // const Item& item(mBag.valueAt(i));
3127 // fprintf(stderr, "Referenced from file %s line %d\n",
3128 // item.sourcePos.file.string(), item.sourcePos.line);
3129 // return UNKNOWN_ERROR;
3132 sprintf(numberStr
, "%d", ResTable_map::TYPE_ANY
);
3133 status_t err
= table
->addBag(SourcePos("<generated>", 0), package
,
3134 attr16
, key
, String16(""),
3136 String16(numberStr
), NULL
, NULL
);
3137 if (err
!= NO_ERROR
) {
3146 status_t
ResourceTable::Entry::assignResourceIds(ResourceTable
* table
,
3147 const String16
& package
)
3149 bool hasErrors
= false;
3151 if (mType
== TYPE_BAG
) {
3152 const char* errorMsg
;
3153 const String16
style16("style");
3154 const String16
attr16("attr");
3155 const String16
id16("id");
3157 if (mParent
.size() > 0) {
3158 mParentId
= table
->getResId(mParent
, &style16
, NULL
, &errorMsg
);
3159 if (mParentId
== 0) {
3160 mPos
.error("Error retrieving parent for item: %s '%s'.\n",
3161 errorMsg
, String8(mParent
).string());
3165 const size_t N
= mBag
.size();
3166 for (size_t i
=0; i
<N
; i
++) {
3167 const String16
& key
= mBag
.keyAt(i
);
3168 Item
& it
= mBag
.editValueAt(i
);
3169 it
.bagKeyId
= table
->getResId(key
,
3170 it
.isId
? &id16
: &attr16
, NULL
, &errorMsg
);
3171 //printf("Bag key of %s: #%08x\n", String8(key).string(), it.bagKeyId);
3172 if (it
.bagKeyId
== 0) {
3173 it
.sourcePos
.error("Error: %s: %s '%s'.\n", errorMsg
,
3174 String8(it
.isId
? id16
: attr16
).string(),
3175 String8(key
).string());
3180 return hasErrors
? UNKNOWN_ERROR
: NO_ERROR
;
3183 status_t
ResourceTable::Entry::prepareFlatten(StringPool
* strings
, ResourceTable
* table
)
3185 if (mType
== TYPE_ITEM
) {
3187 AccessorCookie
ac(it
.sourcePos
, String8(mName
), String8(it
.value
));
3188 if (!table
->stringToValue(&it
.parsedValue
, strings
,
3189 it
.value
, false, true, 0,
3190 &it
.style
, NULL
, &ac
, mItemFormat
)) {
3191 return UNKNOWN_ERROR
;
3193 } else if (mType
== TYPE_BAG
) {
3194 const size_t N
= mBag
.size();
3195 for (size_t i
=0; i
<N
; i
++) {
3196 const String16
& key
= mBag
.keyAt(i
);
3197 Item
& it
= mBag
.editValueAt(i
);
3198 AccessorCookie
ac(it
.sourcePos
, String8(key
), String8(it
.value
));
3199 if (!table
->stringToValue(&it
.parsedValue
, strings
,
3200 it
.value
, false, true, it
.bagKeyId
,
3201 &it
.style
, NULL
, &ac
, it
.format
)) {
3202 return UNKNOWN_ERROR
;
3206 mPos
.error("Error: entry %s is not a single item or a bag.\n",
3207 String8(mName
).string());
3208 return UNKNOWN_ERROR
;
3213 ssize_t
ResourceTable::Entry::flatten(Bundle
* bundle
, const sp
<AaptFile
>& data
, bool isPublic
)
3216 ResTable_entry header
;
3217 memset(&header
, 0, sizeof(header
));
3218 header
.size
= htods(sizeof(header
));
3219 const type ty
= this != NULL
? mType
: TYPE_ITEM
;
3221 if (ty
== TYPE_BAG
) {
3222 header
.flags
|= htods(header
.FLAG_COMPLEX
);
3225 header
.flags
|= htods(header
.FLAG_PUBLIC
);
3227 header
.key
.index
= htodl(mNameIndex
);
3229 if (ty
!= TYPE_BAG
) {
3230 status_t err
= data
->writeData(&header
, sizeof(header
));
3231 if (err
!= NO_ERROR
) {
3232 fprintf(stderr
, "ERROR: out of memory creating ResTable_entry\n");
3236 const Item
& it
= mItem
;
3238 memset(&par
, 0, sizeof(par
));
3239 par
.size
= htods(it
.parsedValue
.size
);
3240 par
.dataType
= it
.parsedValue
.dataType
;
3241 par
.res0
= it
.parsedValue
.res0
;
3242 par
.data
= htodl(it
.parsedValue
.data
);
3244 printf("Writing item (%s): type=%d, data=0x%x, res0=0x%x\n",
3245 String8(mName
).string(), it
.parsedValue
.dataType
,
3246 it
.parsedValue
.data
, par
.res0
);
3248 err
= data
->writeData(&par
, it
.parsedValue
.size
);
3249 if (err
!= NO_ERROR
) {
3250 fprintf(stderr
, "ERROR: out of memory creating Res_value\n");
3253 amt
+= it
.parsedValue
.size
;
3255 size_t N
= mBag
.size();
3257 // Create correct ordering of items.
3258 KeyedVector
<uint32_t, const Item
*> items
;
3259 for (i
=0; i
<N
; i
++) {
3260 const Item
& it
= mBag
.valueAt(i
);
3261 items
.add(it
.bagKeyId
, &it
);
3265 ResTable_map_entry mapHeader
;
3266 memcpy(&mapHeader
, &header
, sizeof(header
));
3267 mapHeader
.size
= htods(sizeof(mapHeader
));
3268 mapHeader
.parent
.ident
= htodl(mParentId
);
3269 mapHeader
.count
= htodl(N
);
3270 status_t err
= data
->writeData(&mapHeader
, sizeof(mapHeader
));
3271 if (err
!= NO_ERROR
) {
3272 fprintf(stderr
, "ERROR: out of memory creating ResTable_entry\n");
3276 for (i
=0; i
<N
; i
++) {
3277 const Item
& it
= *items
.valueAt(i
);
3279 map
.name
.ident
= htodl(it
.bagKeyId
);
3280 map
.value
.size
= htods(it
.parsedValue
.size
);
3281 map
.value
.dataType
= it
.parsedValue
.dataType
;
3282 map
.value
.res0
= it
.parsedValue
.res0
;
3283 map
.value
.data
= htodl(it
.parsedValue
.data
);
3284 err
= data
->writeData(&map
, sizeof(map
));
3285 if (err
!= NO_ERROR
) {
3286 fprintf(stderr
, "ERROR: out of memory creating Res_value\n");
3295 void ResourceTable::ConfigList::appendComment(const String16
& comment
,
3298 if (comment
.size() <= 0) {
3301 if (onlyIfEmpty
&& mComment
.size() > 0) {
3304 if (mComment
.size() > 0) {
3305 mComment
.append(String16("\n"));
3307 mComment
.append(comment
);
3310 void ResourceTable::ConfigList::appendTypeComment(const String16
& comment
)
3312 if (comment
.size() <= 0) {
3315 if (mTypeComment
.size() > 0) {
3316 mTypeComment
.append(String16("\n"));
3318 mTypeComment
.append(comment
);
3321 status_t
ResourceTable::Type::addPublic(const SourcePos
& sourcePos
,
3322 const String16
& name
,
3323 const uint32_t ident
)
3326 int32_t entryIdx
= Res_GETENTRY(ident
);
3328 sourcePos
.error("Public resource %s/%s has an invalid 0 identifier (0x%08x).\n",
3329 String8(mName
).string(), String8(name
).string(), ident
);
3330 return UNKNOWN_ERROR
;
3334 int32_t typeIdx
= Res_GETTYPE(ident
);
3337 if (mPublicIndex
> 0 && mPublicIndex
!= typeIdx
) {
3338 sourcePos
.error("Public resource %s/%s has conflicting type codes for its"
3339 " public identifiers (0x%x vs 0x%x).\n",
3340 String8(mName
).string(), String8(name
).string(),
3341 mPublicIndex
, typeIdx
);
3342 return UNKNOWN_ERROR
;
3344 mPublicIndex
= typeIdx
;
3347 if (mFirstPublicSourcePos
== NULL
) {
3348 mFirstPublicSourcePos
= new SourcePos(sourcePos
);
3351 if (mPublic
.indexOfKey(name
) < 0) {
3352 mPublic
.add(name
, Public(sourcePos
, String16(), ident
));
3354 Public
& p
= mPublic
.editValueFor(name
);
3355 if (p
.ident
!= ident
) {
3356 sourcePos
.error("Public resource %s/%s has conflicting public identifiers"
3357 " (0x%08x vs 0x%08x).\n"
3358 "%s:%d: Originally defined here.\n",
3359 String8(mName
).string(), String8(name
).string(), p
.ident
, ident
,
3360 p
.sourcePos
.file
.string(), p
.sourcePos
.line
);
3361 return UNKNOWN_ERROR
;
3368 void ResourceTable::Type::canAddEntry(const String16
& name
)
3370 mCanAddEntries
.add(name
);
3373 sp
<ResourceTable::Entry
> ResourceTable::Type::getEntry(const String16
& entry
,
3374 const SourcePos
& sourcePos
,
3375 const ResTable_config
* config
,
3378 bool autoAddOverlay
)
3381 sp
<ConfigList
> c
= mConfigs
.valueFor(entry
);
3383 if (overlay
&& !autoAddOverlay
&& mCanAddEntries
.indexOf(entry
) < 0) {
3384 sourcePos
.error("Resource at %s appears in overlay but not"
3385 " in the base package; use <add-resource> to add.\n",
3386 String8(entry
).string());
3389 c
= new ConfigList(entry
, sourcePos
);
3390 mConfigs
.add(entry
, c
);
3391 pos
= (int)mOrderedConfigs
.size();
3392 mOrderedConfigs
.add(c
);
3394 c
->setEntryIndex(pos
);
3398 ConfigDescription cdesc
;
3399 if (config
) cdesc
= *config
;
3401 sp
<Entry
> e
= c
->getEntries().valueFor(cdesc
);
3403 if (config
!= NULL
) {
3404 NOISY(printf("New entry at %s:%d: imsi:%d/%d lang:%c%c cnt:%c%c "
3405 "orien:%d touch:%d density:%d key:%d inp:%d nav:%d w:%d h:%d\n",
3406 sourcePos
.file
.string(), sourcePos
.line
,
3407 config
->mcc
, config
->mnc
,
3408 config
->language
[0] ? config
->language
[0] : '-',
3409 config
->language
[1] ? config
->language
[1] : '-',
3410 config
->country
[0] ? config
->country
[0] : '-',
3411 config
->country
[1] ? config
->country
[1] : '-',
3412 config
->orientation
,
3413 config
->touchscreen
,
3418 config
->screenWidth
,
3419 config
->screenHeight
));
3421 NOISY(printf("New entry at %s:%d: NULL config\n",
3422 sourcePos
.file
.string(), sourcePos
.line
));
3424 e
= new Entry(entry
, sourcePos
);
3425 c
->addEntry(cdesc
, e
);
3429 for (pos=0; pos<(int)mOrderedConfigs.size(); pos++) {
3430 if (mOrderedConfigs[pos] == c) {
3434 if (pos >= (int)mOrderedConfigs.size()) {
3435 sourcePos.error("Internal error: config not found in mOrderedConfigs when adding entry");
3439 e->setEntryIndex(pos);
3444 mUniqueConfigs
.add(cdesc
);
3449 status_t
ResourceTable::Type::applyPublicEntryOrder()
3451 size_t N
= mOrderedConfigs
.size();
3452 Vector
<sp
<ConfigList
> > origOrder(mOrderedConfigs
);
3453 bool hasError
= false;
3456 for (i
=0; i
<N
; i
++) {
3457 mOrderedConfigs
.replaceAt(NULL
, i
);
3460 const size_t NP
= mPublic
.size();
3461 //printf("Ordering %d configs from %d public defs\n", N, NP);
3463 for (j
=0; j
<NP
; j
++) {
3464 const String16
& name
= mPublic
.keyAt(j
);
3465 const Public
& p
= mPublic
.valueAt(j
);
3466 int32_t idx
= Res_GETENTRY(p
.ident
);
3467 //printf("Looking for entry \"%s\"/\"%s\" (0x%08x) in %d...\n",
3468 // String8(mName).string(), String8(name).string(), p.ident, N);
3470 for (i
=0; i
<N
; i
++) {
3471 sp
<ConfigList
> e
= origOrder
.itemAt(i
);
3472 //printf("#%d: \"%s\"\n", i, String8(e->getName()).string());
3473 if (e
->getName() == name
) {
3474 if (idx
>= (int32_t)mOrderedConfigs
.size()) {
3475 p
.sourcePos
.error("Public entry identifier 0x%x entry index "
3476 "is larger than available symbols (index %d, total symbols %d).\n",
3477 p
.ident
, idx
, mOrderedConfigs
.size());
3479 } else if (mOrderedConfigs
.itemAt(idx
) == NULL
) {
3481 e
->setPublicSourcePos(p
.sourcePos
);
3482 mOrderedConfigs
.replaceAt(e
, idx
);
3483 origOrder
.removeAt(i
);
3488 sp
<ConfigList
> oe
= mOrderedConfigs
.itemAt(idx
);
3490 p
.sourcePos
.error("Multiple entry names declared for public entry"
3491 " identifier 0x%x in type %s (%s vs %s).\n"
3492 "%s:%d: Originally defined here.",
3493 idx
+1, String8(mName
).string(),
3494 String8(oe
->getName()).string(),
3495 String8(name
).string(),
3496 oe
->getPublicSourcePos().file
.string(),
3497 oe
->getPublicSourcePos().line
);
3504 p
.sourcePos
.error("Public symbol %s/%s declared here is not defined.",
3505 String8(mName
).string(), String8(name
).string());
3510 //printf("Copying back in %d non-public configs, have %d\n", N, origOrder.size());
3512 if (N
!= origOrder
.size()) {
3513 printf("Internal error: remaining private symbol count mismatch\n");
3514 N
= origOrder
.size();
3518 for (i
=0; i
<N
; i
++) {
3519 sp
<ConfigList
> e
= origOrder
.itemAt(i
);
3520 // There will always be enough room for the remaining entries.
3521 while (mOrderedConfigs
.itemAt(j
) != NULL
) {
3524 mOrderedConfigs
.replaceAt(e
, j
);
3528 return hasError
? UNKNOWN_ERROR
: NO_ERROR
;
3531 ResourceTable::Package::Package(const String16
& name
, ssize_t includedId
)
3532 : mName(name
), mIncludedId(includedId
),
3533 mTypeStringsMapping(0xffffffff),
3534 mKeyStringsMapping(0xffffffff)
3538 sp
<ResourceTable::Type
> ResourceTable::Package::getType(const String16
& type
,
3539 const SourcePos
& sourcePos
,
3542 sp
<Type
> t
= mTypes
.valueFor(type
);
3544 t
= new Type(type
, sourcePos
);
3545 mTypes
.add(type
, t
);
3546 mOrderedTypes
.add(t
);
3548 // For some reason the type's index is set to one plus the index
3549 // in the mOrderedTypes list, rather than just the index.
3550 t
->setIndex(mOrderedTypes
.size());
3556 status_t
ResourceTable::Package::setTypeStrings(const sp
<AaptFile
>& data
)
3558 mTypeStringsData
= data
;
3559 status_t err
= setStrings(data
, &mTypeStrings
, &mTypeStringsMapping
);
3560 if (err
!= NO_ERROR
) {
3561 fprintf(stderr
, "ERROR: Type string data is corrupt!\n");
3566 status_t
ResourceTable::Package::setKeyStrings(const sp
<AaptFile
>& data
)
3568 mKeyStringsData
= data
;
3569 status_t err
= setStrings(data
, &mKeyStrings
, &mKeyStringsMapping
);
3570 if (err
!= NO_ERROR
) {
3571 fprintf(stderr
, "ERROR: Key string data is corrupt!\n");
3576 status_t
ResourceTable::Package::setStrings(const sp
<AaptFile
>& data
,
3577 ResStringPool
* strings
,
3578 DefaultKeyedVector
<String16
, uint32_t>* mappings
)
3580 if (data
->getData() == NULL
) {
3581 return UNKNOWN_ERROR
;
3584 NOISY(aout
<< "Setting restable string pool: "
3585 << HexDump(data
->getData(), data
->getSize()) << endl
);
3587 status_t err
= strings
->setTo(data
->getData(), data
->getSize());
3588 if (err
== NO_ERROR
) {
3589 const size_t N
= strings
->size();
3590 for (size_t i
=0; i
<N
; i
++) {
3592 mappings
->add(String16(strings
->stringAt(i
, &len
)), i
);
3598 status_t
ResourceTable::Package::applyPublicTypeOrder()
3600 size_t N
= mOrderedTypes
.size();
3601 Vector
<sp
<Type
> > origOrder(mOrderedTypes
);
3604 for (i
=0; i
<N
; i
++) {
3605 mOrderedTypes
.replaceAt(NULL
, i
);
3608 for (i
=0; i
<N
; i
++) {
3609 sp
<Type
> t
= origOrder
.itemAt(i
);
3610 int32_t idx
= t
->getPublicIndex();
3613 while (idx
>= (int32_t)mOrderedTypes
.size()) {
3614 mOrderedTypes
.add();
3616 if (mOrderedTypes
.itemAt(idx
) != NULL
) {
3617 sp
<Type
> ot
= mOrderedTypes
.itemAt(idx
);
3618 t
->getFirstPublicSourcePos().error("Multiple type names declared for public type"
3619 " identifier 0x%x (%s vs %s).\n"
3620 "%s:%d: Originally defined here.",
3621 idx
, String8(ot
->getName()).string(),
3622 String8(t
->getName()).string(),
3623 ot
->getFirstPublicSourcePos().file
.string(),
3624 ot
->getFirstPublicSourcePos().line
);
3625 return UNKNOWN_ERROR
;
3627 mOrderedTypes
.replaceAt(t
, idx
);
3628 origOrder
.removeAt(i
);
3635 for (i
=0; i
<N
; i
++) {
3636 sp
<Type
> t
= origOrder
.itemAt(i
);
3637 // There will always be enough room for the remaining types.
3638 while (mOrderedTypes
.itemAt(j
) != NULL
) {
3641 mOrderedTypes
.replaceAt(t
, j
);
3647 sp
<ResourceTable::Package
> ResourceTable::getPackage(const String16
& package
)
3649 sp
<Package
> p
= mPackages
.valueFor(package
);
3651 if (mIsAppPackage
) {
3652 if (mHaveAppPackage
) {
3653 fprintf(stderr
, "Adding multiple application package resources; only one is allowed.\n"
3654 "Use -x to create extended resources.\n");
3657 mHaveAppPackage
= true;
3658 p
= new Package(package
, 127);
3660 p
= new Package(package
, mNextPackageId
);
3662 //printf("*** NEW PACKAGE: \"%s\" id=%d\n",
3663 // String8(package).string(), p->getAssignedId());
3664 mPackages
.add(package
, p
);
3665 mOrderedPackages
.add(p
);
3671 sp
<ResourceTable::Type
> ResourceTable::getType(const String16
& package
,
3672 const String16
& type
,
3673 const SourcePos
& sourcePos
,
3676 sp
<Package
> p
= getPackage(package
);
3680 return p
->getType(type
, sourcePos
, doSetIndex
);
3683 sp
<ResourceTable::Entry
> ResourceTable::getEntry(const String16
& package
,
3684 const String16
& type
,
3685 const String16
& name
,
3686 const SourcePos
& sourcePos
,
3688 const ResTable_config
* config
,
3691 sp
<Type
> t
= getType(package
, type
, sourcePos
, doSetIndex
);
3695 return t
->getEntry(name
, sourcePos
, config
, doSetIndex
, overlay
, mBundle
->getAutoAddOverlay());
3698 sp
<const ResourceTable::Entry
> ResourceTable::getEntry(uint32_t resID
,
3699 const ResTable_config
* config
) const
3701 int pid
= Res_GETPACKAGE(resID
)+1;
3702 const size_t N
= mOrderedPackages
.size();
3705 for (i
=0; i
<N
; i
++) {
3706 sp
<Package
> check
= mOrderedPackages
[i
];
3707 if (check
->getAssignedId() == pid
) {
3714 fprintf(stderr
, "warning: Package not found for resource #%08x\n", resID
);
3718 int tid
= Res_GETTYPE(resID
);
3719 if (tid
< 0 || tid
>= (int)p
->getOrderedTypes().size()) {
3720 fprintf(stderr
, "warning: Type not found for resource #%08x\n", resID
);
3723 sp
<Type
> t
= p
->getOrderedTypes()[tid
];
3725 int eid
= Res_GETENTRY(resID
);
3726 if (eid
< 0 || eid
>= (int)t
->getOrderedConfigs().size()) {
3727 fprintf(stderr
, "warning: Entry not found for resource #%08x\n", resID
);
3731 sp
<ConfigList
> c
= t
->getOrderedConfigs()[eid
];
3733 fprintf(stderr
, "warning: Entry not found for resource #%08x\n", resID
);
3737 ConfigDescription cdesc
;
3738 if (config
) cdesc
= *config
;
3739 sp
<Entry
> e
= c
->getEntries().valueFor(cdesc
);
3741 fprintf(stderr
, "warning: Entry configuration not found for resource #%08x\n", resID
);
3748 const ResourceTable::Item
* ResourceTable::getItem(uint32_t resID
, uint32_t attrID
) const
3750 sp
<const Entry
> e
= getEntry(resID
);
3755 const size_t N
= e
->getBag().size();
3756 for (size_t i
=0; i
<N
; i
++) {
3757 const Item
& it
= e
->getBag().valueAt(i
);
3758 if (it
.bagKeyId
== 0) {
3759 fprintf(stderr
, "warning: ID not yet assigned to '%s' in bag '%s'\n",
3760 String8(e
->getName()).string(),
3761 String8(e
->getBag().keyAt(i
)).string());
3763 if (it
.bagKeyId
== attrID
) {
3771 bool ResourceTable::getItemValue(
3772 uint32_t resID
, uint32_t attrID
, Res_value
* outValue
)
3774 const Item
* item
= getItem(resID
, attrID
);
3778 if (item
->evaluating
) {
3779 sp
<const Entry
> e
= getEntry(resID
);
3780 const size_t N
= e
->getBag().size();
3782 for (i
=0; i
<N
; i
++) {
3783 if (&e
->getBag().valueAt(i
) == item
) {
3787 fprintf(stderr
, "warning: Circular reference detected in key '%s' of bag '%s'\n",
3788 String8(e
->getName()).string(),
3789 String8(e
->getBag().keyAt(i
)).string());
3792 item
->evaluating
= true;
3793 res
= stringToValue(outValue
, NULL
, item
->value
, false, false, item
->bagKeyId
);
3796 printf("getItemValue of #%08x[#%08x] (%s): type=#%08x, data=#%08x\n",
3797 resID
, attrID
, String8(getEntry(resID
)->getName()).string(),
3798 outValue
->dataType
, outValue
->data
);
3800 printf("getItemValue of #%08x[#%08x]: failed\n",
3804 item
->evaluating
= false;
3809 #define DEBUG_LANG(x) printf("lang=%c%c cnt=%c%c ", \
3810 (x).language[0] ? (x).language[0] : '-', \
3811 (x).language[1] ? (x).language[1] : '-', \
3812 (x).country[0] ? (x).country[0] : '-', \
3813 (x).country[1] ? (x).country[1] : '-');
3815 status_t
ResourceConfigReferences::add(uint32_t id
, const ResTable_config
& config
)
3817 ssize_t index
= mRoots
.indexOfKey(id
);
3819 index
= mRoots
.add(id
, Vector
<const ResTable_config
*>());
3821 Vector
<const ResTable_config
*>& configRoots
= mRoots
.editValueFor(id
);
3823 if (!configRoots
.isEmpty()) {
3824 ssize_t NR
= configRoots
.size();
3825 for (int ri
=0; ri
<NR
; ++ri
) {
3826 const ResTable_config
* current
= configRoots
[ri
];
3828 if (config
.match(*current
)) {
3829 // We already have something more generic than our incoming string.
3831 printf(" ConfigRef %d: ignoring ", id
);
3836 } else if (current
->match(config
)) {
3839 printf(" ConfigRef %d: remove ", id
);
3843 configRoots
.removeItemsAt(ri
);
3849 printf(" ConfigRef %d: add ", id
);
3853 ResTable_config
*configCopy
= (ResTable_config
*)malloc(sizeof(ResTable_config
));
3854 memcpy(configCopy
, &config
, sizeof(ResTable_config
));
3855 configRoots
.add(configCopy
);
3860 void ResourceConfigReferences::dump()
3862 printf("ResourceConfigReferences\n");
3863 const ssize_t NR
= mRoots
.size();
3864 for (int ri
=0; ri
<NR
; ++ri
) {
3865 const Vector
<const ResTable_config
*>& configRoots
= mRoots
.valueAt(ri
);
3866 printf(" String %d\n", mRoots
.keyAt(ri
));
3867 const ssize_t NC
= configRoots
.size();
3868 for (int ci
=0; ci
<NC
; ++ci
) {
3870 DEBUG_LANG(*configRoots
[ci
])
3876 bool ResourceConfigReferences::isRoot(uint32_t id
, const ResTable_config
& config
)
3878 const Vector
<const ResTable_config
*>& configRoots
= mRoots
.editValueFor(id
);
3879 const ssize_t NR
= configRoots
.size();
3880 for (int ri
= 0; ri
<NR
; ++ri
) {
3881 if (configRoots
[ri
]->match(config
)) {
3888 ResourceConfigReferences::~ResourceConfigReferences()
3890 const ssize_t NR
= mRoots
.size();
3891 for (int ri
=0; ri
<NR
; ++ri
) {
3892 Vector
<const ResTable_config
*> configRoots
= mRoots
.editValueAt(ri
);
3893 const ssize_t NC
= configRoots
.size();
3894 for (int ci
=0; ci
<NC
; ++ci
) {
3895 ResTable_config
* config
= const_cast<ResTable_config
*>(configRoots
[ci
]);