2 // Copyright 2006 The Android Open Source Project
4 // Build resource files from raw assets.
7 #include "ResourceTable.h"
11 #include <utils/ByteOrder.h>
12 #include <utils/ResourceTypes.h>
17 status_t
compileXmlFile(const sp
<AaptAssets
>& assets
,
18 const sp
<AaptFile
>& target
,
22 sp
<XMLNode
> root
= XMLNode::parse(target
);
27 return compileXmlFile(assets
, root
, target
, table
, options
);
30 status_t
compileXmlFile(const sp
<AaptAssets
>& assets
,
31 const sp
<AaptFile
>& target
,
32 const sp
<AaptFile
>& outTarget
,
36 sp
<XMLNode
> root
= XMLNode::parse(target
);
41 return compileXmlFile(assets
, root
, outTarget
, table
, options
);
44 status_t
compileXmlFile(const sp
<AaptAssets
>& assets
,
45 const sp
<XMLNode
>& root
,
46 const sp
<AaptFile
>& target
,
50 if ((options
&XML_COMPILE_STRIP_WHITESPACE
) != 0) {
51 root
->removeWhitespace(true, NULL
);
52 } else if ((options
&XML_COMPILE_COMPACT_WHITESPACE
) != 0) {
53 root
->removeWhitespace(false, NULL
);
56 if ((options
&XML_COMPILE_UTF8
) != 0) {
60 bool hasErrors
= false;
62 if ((options
&XML_COMPILE_ASSIGN_ATTRIBUTE_IDS
) != 0) {
63 status_t err
= root
->assignResourceIds(assets
, table
);
64 if (err
!= NO_ERROR
) {
69 status_t err
= root
->parseValues(assets
, table
);
70 if (err
!= NO_ERROR
) {
78 NOISY(printf("Input XML Resource:\n"));
80 err
= root
->flatten(target
,
81 (options
&XML_COMPILE_STRIP_COMMENTS
) != 0,
82 (options
&XML_COMPILE_STRIP_RAW_VALUES
) != 0);
83 if (err
!= NO_ERROR
) {
87 NOISY(printf("Output XML Resource:\n"));
88 NOISY(ResXMLTree tree
;
89 tree
.setTo(target
->getData(), target
->getSize());
90 printXMLBlock(&tree
));
92 target
->setCompressionMethod(ZipEntry::kCompressDeflated
);
102 const char16_t* name
;
105 const char* description
;
108 static const char16_t referenceArray
[] =
109 { 'r', 'e', 'f', 'e', 'r', 'e', 'n', 'c', 'e' };
110 static const char16_t stringArray
[] =
111 { 's', 't', 'r', 'i', 'n', 'g' };
112 static const char16_t integerArray
[] =
113 { 'i', 'n', 't', 'e', 'g', 'e', 'r' };
114 static const char16_t booleanArray
[] =
115 { 'b', 'o', 'o', 'l', 'e', 'a', 'n' };
116 static const char16_t colorArray
[] =
117 { 'c', 'o', 'l', 'o', 'r' };
118 static const char16_t floatArray
[] =
119 { 'f', 'l', 'o', 'a', 't' };
120 static const char16_t dimensionArray
[] =
121 { 'd', 'i', 'm', 'e', 'n', 's', 'i', 'o', 'n' };
122 static const char16_t fractionArray
[] =
123 { 'f', 'r', 'a', 'c', 't', 'i', 'o', 'n' };
124 static const char16_t enumArray
[] =
125 { 'e', 'n', 'u', 'm' };
126 static const char16_t flagsArray
[] =
127 { 'f', 'l', 'a', 'g', 's' };
129 static const flag_entry gFormatFlags
[] = {
130 { referenceArray
, sizeof(referenceArray
)/2, ResTable_map::TYPE_REFERENCE
,
131 "a reference to another resource, in the form \"<code>@[+][<i>package</i>:]<i>type</i>:<i>name</i></code>\"\n"
132 "or to a theme attribute in the form \"<code>?[<i>package</i>:][<i>type</i>:]<i>name</i></code>\"."},
133 { stringArray
, sizeof(stringArray
)/2, ResTable_map::TYPE_STRING
,
134 "a string value, using '\\\\;' to escape characters such as '\\\\n' or '\\\\uxxxx' for a unicode character." },
135 { integerArray
, sizeof(integerArray
)/2, ResTable_map::TYPE_INTEGER
,
136 "an integer value, such as \"<code>100</code>\"." },
137 { booleanArray
, sizeof(booleanArray
)/2, ResTable_map::TYPE_BOOLEAN
,
138 "a boolean value, either \"<code>true</code>\" or \"<code>false</code>\"." },
139 { colorArray
, sizeof(colorArray
)/2, ResTable_map::TYPE_COLOR
,
140 "a color value, in the form of \"<code>#<i>rgb</i></code>\", \"<code>#<i>argb</i></code>\",\n"
141 "\"<code>#<i>rrggbb</i></code>\", or \"<code>#<i>aarrggbb</i></code>\"." },
142 { floatArray
, sizeof(floatArray
)/2, ResTable_map::TYPE_FLOAT
,
143 "a floating point value, such as \"<code>1.2</code>\"."},
144 { dimensionArray
, sizeof(dimensionArray
)/2, ResTable_map::TYPE_DIMENSION
,
145 "a dimension value, which is a floating point number appended with a unit such as \"<code>14.5sp</code>\".\n"
146 "Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size),\n"
147 "in (inches), mm (millimeters)." },
148 { fractionArray
, sizeof(fractionArray
)/2, ResTable_map::TYPE_FRACTION
,
149 "a fractional value, which is a floating point number appended with either % or %p, such as \"<code>14.5%</code>\".\n"
150 "The % suffix always means a percentage of the base size; the optional %p suffix provides a size relative to\n"
151 "some parent container." },
152 { enumArray
, sizeof(enumArray
)/2, ResTable_map::TYPE_ENUM
, NULL
},
153 { flagsArray
, sizeof(flagsArray
)/2, ResTable_map::TYPE_FLAGS
, NULL
},
157 static const char16_t suggestedArray
[] = { 's', 'u', 'g', 'g', 'e', 's', 't', 'e', 'd' };
159 static const flag_entry l10nRequiredFlags
[] = {
160 { suggestedArray
, sizeof(suggestedArray
)/2, ResTable_map::L10N_SUGGESTED
, NULL
},
164 static const char16_t nulStr
[] = { 0 };
166 static uint32_t parse_flags(const char16_t* str
, size_t len
,
167 const flag_entry
* flags
, bool* outError
= NULL
)
169 while (len
> 0 && isspace(*str
)) {
173 while (len
> 0 && isspace(str
[len
-1])) {
177 const char16_t* const end
= str
+ len
;
181 const char16_t* div
= str
;
182 while (div
< end
&& *div
!= '|') {
186 const flag_entry
* cur
= flags
;
188 if (strzcmp16(cur
->name
, cur
->nameLen
, str
, div
-str
) == 0) {
196 if (outError
) *outError
= true;
200 str
= div
< end
? div
+1 : div
;
203 if (outError
) *outError
= false;
207 static String16
mayOrMust(int type
, int flags
)
209 if ((type
&(~flags
)) == 0) {
210 return String16("<p>Must");
213 return String16("<p>May");
216 static void appendTypeInfo(ResourceTable
* outTable
, const String16
& pkg
,
217 const String16
& typeName
, const String16
& ident
, int type
,
218 const flag_entry
* flags
)
220 bool hadType
= false;
221 while (flags
->name
) {
222 if ((type
&flags
->value
) != 0 && flags
->description
!= NULL
) {
223 String16
fullMsg(mayOrMust(type
, flags
->value
));
224 fullMsg
.append(String16(" be "));
225 fullMsg
.append(String16(flags
->description
));
226 outTable
->appendTypeComment(pkg
, typeName
, ident
, fullMsg
);
231 if (hadType
&& (type
&ResTable_map::TYPE_REFERENCE
) == 0) {
232 outTable
->appendTypeComment(pkg
, typeName
, ident
,
233 String16("<p>This may also be a reference to a resource (in the form\n"
234 "\"<code>@[<i>package</i>:]<i>type</i>:<i>name</i></code>\") or\n"
235 "theme attribute (in the form\n"
236 "\"<code>?[<i>package</i>:][<i>type</i>:]<i>name</i></code>\")\n"
237 "containing a value of this type."));
241 struct PendingAttribute
243 const String16 myPackage
;
244 const SourcePos sourcePos
;
245 const bool appendComment
;
252 PendingAttribute(String16 _package
, const sp
<AaptFile
>& in
,
253 ResXMLTree
& block
, bool _appendComment
)
254 : myPackage(_package
)
255 , sourcePos(in
->getPrintableSource(), block
.getLineNumber())
256 , appendComment(_appendComment
)
257 , type(ResTable_map::TYPE_ANY
)
263 status_t
createIfNeeded(ResourceTable
* outTable
)
265 if (added
|| hasErrors
) {
270 String16
attr16("attr");
272 if (outTable
->hasBagOrEntry(myPackage
, attr16
, ident
)) {
273 sourcePos
.error("Attribute \"%s\" has already been defined\n",
274 String8(ident
).string());
276 return UNKNOWN_ERROR
;
280 sprintf(numberStr
, "%d", type
);
281 status_t err
= outTable
->addBag(sourcePos
, myPackage
,
282 attr16
, ident
, String16(""),
284 String16(numberStr
), NULL
, NULL
);
285 if (err
!= NO_ERROR
) {
289 outTable
->appendComment(myPackage
, attr16
, ident
, comment
, appendComment
);
290 //printf("Attribute %s comment: %s\n", String8(ident).string(),
291 // String8(comment).string());
296 static status_t
compileAttribute(const sp
<AaptFile
>& in
,
298 const String16
& myPackage
,
299 ResourceTable
* outTable
,
300 String16
* outIdent
= NULL
,
301 bool inStyleable
= false)
303 PendingAttribute
attr(myPackage
, in
, block
, inStyleable
);
305 const String16
attr16("attr");
306 const String16
id16("id");
308 // Attribute type constants.
309 const String16
enum16("enum");
310 const String16
flag16("flag");
312 ResXMLTree::event_code_t code
;
316 ssize_t identIdx
= block
.indexOfAttribute(NULL
, "name");
318 attr
.ident
= String16(block
.getAttributeStringValue(identIdx
, &len
));
320 *outIdent
= attr
.ident
;
323 attr
.sourcePos
.error("A 'name' attribute is required for <attr>\n");
324 attr
.hasErrors
= true;
327 attr
.comment
= String16(
328 block
.getComment(&len
) ? block
.getComment(&len
) : nulStr
);
330 ssize_t typeIdx
= block
.indexOfAttribute(NULL
, "format");
332 String16 typeStr
= String16(block
.getAttributeStringValue(typeIdx
, &len
));
333 attr
.type
= parse_flags(typeStr
.string(), typeStr
.size(), gFormatFlags
);
334 if (attr
.type
== 0) {
335 attr
.sourcePos
.error("Tag <attr> 'format' attribute value \"%s\" not valid\n",
336 String8(typeStr
).string());
337 attr
.hasErrors
= true;
339 attr
.createIfNeeded(outTable
);
340 } else if (!inStyleable
) {
341 // Attribute definitions outside of styleables always define the
342 // attribute as a generic value.
343 attr
.createIfNeeded(outTable
);
346 //printf("Attribute %s: type=0x%08x\n", String8(attr.ident).string(), attr.type);
348 ssize_t minIdx
= block
.indexOfAttribute(NULL
, "min");
350 String16 val
= String16(block
.getAttributeStringValue(minIdx
, &len
));
351 if (!ResTable::stringToInt(val
.string(), val
.size(), NULL
)) {
352 attr
.sourcePos
.error("Tag <attr> 'min' attribute must be a number, not \"%s\"\n",
353 String8(val
).string());
354 attr
.hasErrors
= true;
356 attr
.createIfNeeded(outTable
);
357 if (!attr
.hasErrors
) {
358 err
= outTable
->addBag(attr
.sourcePos
, myPackage
, attr16
, attr
.ident
,
359 String16(""), String16("^min"), String16(val
), NULL
, NULL
);
360 if (err
!= NO_ERROR
) {
361 attr
.hasErrors
= true;
366 ssize_t maxIdx
= block
.indexOfAttribute(NULL
, "max");
368 String16 val
= String16(block
.getAttributeStringValue(maxIdx
, &len
));
369 if (!ResTable::stringToInt(val
.string(), val
.size(), NULL
)) {
370 attr
.sourcePos
.error("Tag <attr> 'max' attribute must be a number, not \"%s\"\n",
371 String8(val
).string());
372 attr
.hasErrors
= true;
374 attr
.createIfNeeded(outTable
);
375 if (!attr
.hasErrors
) {
376 err
= outTable
->addBag(attr
.sourcePos
, myPackage
, attr16
, attr
.ident
,
377 String16(""), String16("^max"), String16(val
), NULL
, NULL
);
378 attr
.hasErrors
= true;
382 if ((minIdx
>= 0 || maxIdx
>= 0) && (attr
.type
&ResTable_map::TYPE_INTEGER
) == 0) {
383 attr
.sourcePos
.error("Tag <attr> must have format=integer attribute if using max or min\n");
384 attr
.hasErrors
= true;
387 ssize_t l10nIdx
= block
.indexOfAttribute(NULL
, "localization");
389 const uint16_t* str
= block
.getAttributeStringValue(l10nIdx
, &len
);
391 uint32_t l10n_required
= parse_flags(str
, len
, l10nRequiredFlags
, &error
);
393 attr
.sourcePos
.error("Tag <attr> 'localization' attribute value \"%s\" not valid\n",
394 String8(str
).string());
395 attr
.hasErrors
= true;
397 attr
.createIfNeeded(outTable
);
398 if (!attr
.hasErrors
) {
400 sprintf(buf
, "%d", l10n_required
);
401 err
= outTable
->addBag(attr
.sourcePos
, myPackage
, attr16
, attr
.ident
,
402 String16(""), String16("^l10n"), String16(buf
), NULL
, NULL
);
403 if (err
!= NO_ERROR
) {
404 attr
.hasErrors
= true;
409 String16 enumOrFlagsComment
;
411 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
412 if (code
== ResXMLTree::START_TAG
) {
413 uint32_t localType
= 0;
414 if (strcmp16(block
.getElementName(&len
), enum16
.string()) == 0) {
415 localType
= ResTable_map::TYPE_ENUM
;
416 } else if (strcmp16(block
.getElementName(&len
), flag16
.string()) == 0) {
417 localType
= ResTable_map::TYPE_FLAGS
;
419 SourcePos(in
->getPrintableSource(), block
.getLineNumber())
420 .error("Tag <%s> can not appear inside <attr>, only <enum> or <flag>\n",
421 String8(block
.getElementName(&len
)).string());
422 return UNKNOWN_ERROR
;
425 attr
.createIfNeeded(outTable
);
427 if (attr
.type
== ResTable_map::TYPE_ANY
) {
428 // No type was explicitly stated, so supplying enum tags
429 // implicitly creates an enum or flag.
433 if ((attr
.type
&(ResTable_map::TYPE_ENUM
|ResTable_map::TYPE_FLAGS
)) == 0) {
434 // Wasn't originally specified as an enum, so update its type.
435 attr
.type
|= localType
;
436 if (!attr
.hasErrors
) {
438 sprintf(numberStr
, "%d", attr
.type
);
439 err
= outTable
->addBag(SourcePos(in
->getPrintableSource(), block
.getLineNumber()),
440 myPackage
, attr16
, attr
.ident
, String16(""),
441 String16("^type"), String16(numberStr
), NULL
, NULL
, true);
442 if (err
!= NO_ERROR
) {
443 attr
.hasErrors
= true;
446 } else if ((uint32_t)(attr
.type
&(ResTable_map::TYPE_ENUM
|ResTable_map::TYPE_FLAGS
)) != localType
) {
447 if (localType
== ResTable_map::TYPE_ENUM
) {
448 SourcePos(in
->getPrintableSource(), block
.getLineNumber())
449 .error("<enum> attribute can not be used inside a flags format\n");
450 attr
.hasErrors
= true;
452 SourcePos(in
->getPrintableSource(), block
.getLineNumber())
453 .error("<flag> attribute can not be used inside a enum format\n");
454 attr
.hasErrors
= true;
459 ssize_t itemIdentIdx
= block
.indexOfAttribute(NULL
, "name");
460 if (itemIdentIdx
>= 0) {
461 itemIdent
= String16(block
.getAttributeStringValue(itemIdentIdx
, &len
));
463 SourcePos(in
->getPrintableSource(), block
.getLineNumber())
464 .error("A 'name' attribute is required for <enum> or <flag>\n");
465 attr
.hasErrors
= true;
469 ssize_t valueIdx
= block
.indexOfAttribute(NULL
, "value");
471 value
= String16(block
.getAttributeStringValue(valueIdx
, &len
));
473 SourcePos(in
->getPrintableSource(), block
.getLineNumber())
474 .error("A 'value' attribute is required for <enum> or <flag>\n");
475 attr
.hasErrors
= true;
477 if (!attr
.hasErrors
&& !ResTable::stringToInt(value
.string(), value
.size(), NULL
)) {
478 SourcePos(in
->getPrintableSource(), block
.getLineNumber())
479 .error("Tag <enum> or <flag> 'value' attribute must be a number,"
481 String8(value
).string());
482 attr
.hasErrors
= true;
485 // Make sure an id is defined for this enum/flag identifier...
486 if (!attr
.hasErrors
&& !outTable
->hasBagOrEntry(itemIdent
, &id16
, &myPackage
)) {
487 err
= outTable
->startBag(SourcePos(in
->getPrintableSource(), block
.getLineNumber()),
488 myPackage
, id16
, itemIdent
, String16(), NULL
);
489 if (err
!= NO_ERROR
) {
490 attr
.hasErrors
= true;
494 if (!attr
.hasErrors
) {
495 if (enumOrFlagsComment
.size() == 0) {
496 enumOrFlagsComment
.append(mayOrMust(attr
.type
,
497 ResTable_map::TYPE_ENUM
|ResTable_map::TYPE_FLAGS
));
498 enumOrFlagsComment
.append((attr
.type
&ResTable_map::TYPE_ENUM
)
499 ? String16(" be one of the following constant values.")
500 : String16(" be one or more (separated by '|') of the following constant values."));
501 enumOrFlagsComment
.append(String16("</p>\n<table>\n"
502 "<colgroup align=\"left\" />\n"
503 "<colgroup align=\"left\" />\n"
504 "<colgroup align=\"left\" />\n"
505 "<tr><th>Constant</th><th>Value</th><th>Description</th></tr>"));
508 enumOrFlagsComment
.append(String16("\n<tr><td><code>"));
509 enumOrFlagsComment
.append(itemIdent
);
510 enumOrFlagsComment
.append(String16("</code></td><td>"));
511 enumOrFlagsComment
.append(value
);
512 enumOrFlagsComment
.append(String16("</td><td>"));
513 if (block
.getComment(&len
)) {
514 enumOrFlagsComment
.append(String16(block
.getComment(&len
)));
516 enumOrFlagsComment
.append(String16("</td></tr>"));
518 err
= outTable
->addBag(SourcePos(in
->getPrintableSource(), block
.getLineNumber()),
520 attr16
, attr
.ident
, String16(""),
521 itemIdent
, value
, NULL
, NULL
, false, true);
522 if (err
!= NO_ERROR
) {
523 attr
.hasErrors
= true;
526 } else if (code
== ResXMLTree::END_TAG
) {
527 if (strcmp16(block
.getElementName(&len
), attr16
.string()) == 0) {
530 if ((attr
.type
&ResTable_map::TYPE_ENUM
) != 0) {
531 if (strcmp16(block
.getElementName(&len
), enum16
.string()) != 0) {
532 SourcePos(in
->getPrintableSource(), block
.getLineNumber())
533 .error("Found tag </%s> where </enum> is expected\n",
534 String8(block
.getElementName(&len
)).string());
535 return UNKNOWN_ERROR
;
538 if (strcmp16(block
.getElementName(&len
), flag16
.string()) != 0) {
539 SourcePos(in
->getPrintableSource(), block
.getLineNumber())
540 .error("Found tag </%s> where </flag> is expected\n",
541 String8(block
.getElementName(&len
)).string());
542 return UNKNOWN_ERROR
;
548 if (!attr
.hasErrors
&& attr
.added
) {
549 appendTypeInfo(outTable
, myPackage
, attr16
, attr
.ident
, attr
.type
, gFormatFlags
);
552 if (!attr
.hasErrors
&& enumOrFlagsComment
.size() > 0) {
553 enumOrFlagsComment
.append(String16("\n</table>"));
554 outTable
->appendTypeComment(myPackage
, attr16
, attr
.ident
, enumOrFlagsComment
);
561 bool localeIsDefined(const ResTable_config
& config
)
563 return config
.locale
== 0;
566 status_t
parseAndAddBag(Bundle
* bundle
,
567 const sp
<AaptFile
>& in
,
569 const ResTable_config
& config
,
570 const String16
& myPackage
,
571 const String16
& curType
,
572 const String16
& ident
,
573 const String16
& parentIdent
,
574 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
, isFormatted
,
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
,
622 const bool overwrite
,
623 ResourceTable
* outTable
)
628 Vector
<StringPool::entry_style_span
> spans
;
629 err
= parseStyledString(bundle
, in
->getPrintableSource().string(), block
,
630 curTag
, &str
, curIsStyled
? &spans
: NULL
,
631 isFormatted
, pseudolocalize
);
633 if (err
< NO_ERROR
) {
637 NOISY(printf("Adding resource entry l=%c%c c=%c%c orien=%d d=%d id=%s: %s\n",
638 config
.language
[0], config
.language
[1],
639 config
.country
[0], config
.country
[1],
640 config
.orientation
, config
.density
,
641 String8(ident
).string(), String8(str
).string()));
643 err
= outTable
->addEntry(SourcePos(in
->getPrintableSource(), block
->getLineNumber()),
644 myPackage
, curType
, ident
, str
, &spans
, &config
,
645 false, curFormat
, overwrite
);
650 status_t
compileResourceFile(Bundle
* bundle
,
651 const sp
<AaptAssets
>& assets
,
652 const sp
<AaptFile
>& in
,
653 const ResTable_config
& defParams
,
654 const bool overwrite
,
655 ResourceTable
* outTable
)
658 status_t err
= parseXMLResource(in
, &block
, false, true);
659 if (err
!= NO_ERROR
) {
664 const String16
resources16("resources");
666 // Identifier declaration tags.
667 const String16
declare_styleable16("declare-styleable");
668 const String16
attr16("attr");
670 // Data creation organizational tags.
671 const String16
string16("string");
672 const String16
drawable16("drawable");
673 const String16
color16("color");
674 const String16
bool16("bool");
675 const String16
integer16("integer");
676 const String16
dimen16("dimen");
677 const String16
fraction16("fraction");
678 const String16
style16("style");
679 const String16
plurals16("plurals");
680 const String16
array16("array");
681 const String16
string_array16("string-array");
682 const String16
integer_array16("integer-array");
683 const String16
public16("public");
684 const String16
public_padding16("public-padding");
685 const String16
private_symbols16("private-symbols");
686 const String16
add_resource16("add-resource");
687 const String16
skip16("skip");
688 const String16
eat_comment16("eat-comment");
690 // Data creation tags.
691 const String16
bag16("bag");
692 const String16
item16("item");
694 // Attribute type constants.
695 const String16
enum16("enum");
698 const String16
other16("other");
699 const String16
quantityOther16("^other");
700 const String16
zero16("zero");
701 const String16
quantityZero16("^zero");
702 const String16
one16("one");
703 const String16
quantityOne16("^one");
704 const String16
two16("two");
705 const String16
quantityTwo16("^two");
706 const String16
few16("few");
707 const String16
quantityFew16("^few");
708 const String16
many16("many");
709 const String16
quantityMany16("^many");
711 // useful attribute names and special values
712 const String16
name16("name");
713 const String16
translatable16("translatable");
714 const String16
formatted16("formatted");
715 const String16
false16("false");
717 const String16
myPackage(assets
->getPackage());
719 bool hasErrors
= false;
721 bool fileIsTranslatable
= true;
722 if (strstr(in
->getPrintableSource().string(), "donottranslate") != NULL
) {
723 fileIsTranslatable
= false;
726 DefaultKeyedVector
<String16
, uint32_t> nextPublicId(0);
728 ResXMLTree::event_code_t code
;
731 } while (code
== ResXMLTree::START_NAMESPACE
);
734 if (code
!= ResXMLTree::START_TAG
) {
735 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
736 "No start tag found\n");
737 return UNKNOWN_ERROR
;
739 if (strcmp16(block
.getElementName(&len
), resources16
.string()) != 0) {
740 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
741 "Invalid start tag %s\n", String8(block
.getElementName(&len
)).string());
742 return UNKNOWN_ERROR
;
745 ResTable_config
curParams(defParams
);
747 ResTable_config
pseudoParams(curParams
);
748 pseudoParams
.language
[0] = 'z';
749 pseudoParams
.language
[1] = 'z';
750 pseudoParams
.country
[0] = 'Z';
751 pseudoParams
.country
[1] = 'Z';
753 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
754 if (code
== ResXMLTree::START_TAG
) {
755 const String16
* curTag
= NULL
;
757 int32_t curFormat
= ResTable_map::TYPE_ANY
;
758 bool curIsBag
= false;
759 bool curIsBagReplaceOnOverwrite
= false;
760 bool curIsStyled
= false;
761 bool curIsPseudolocalizable
= false;
762 bool curIsFormatted
= fileIsTranslatable
;
763 bool localHasErrors
= false;
765 if (strcmp16(block
.getElementName(&len
), skip16
.string()) == 0) {
766 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
767 && code
!= ResXMLTree::BAD_DOCUMENT
) {
768 if (code
== ResXMLTree::END_TAG
) {
769 if (strcmp16(block
.getElementName(&len
), skip16
.string()) == 0) {
776 } else if (strcmp16(block
.getElementName(&len
), eat_comment16
.string()) == 0) {
777 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
778 && code
!= ResXMLTree::BAD_DOCUMENT
) {
779 if (code
== ResXMLTree::END_TAG
) {
780 if (strcmp16(block
.getElementName(&len
), eat_comment16
.string()) == 0) {
787 } else if (strcmp16(block
.getElementName(&len
), public16
.string()) == 0) {
788 SourcePos
srcPos(in
->getPrintableSource(), block
.getLineNumber());
791 ssize_t typeIdx
= block
.indexOfAttribute(NULL
, "type");
793 srcPos
.error("A 'type' attribute is required for <public>\n");
794 hasErrors
= localHasErrors
= true;
796 type
= String16(block
.getAttributeStringValue(typeIdx
, &len
));
799 ssize_t nameIdx
= block
.indexOfAttribute(NULL
, "name");
801 srcPos
.error("A 'name' attribute is required for <public>\n");
802 hasErrors
= localHasErrors
= true;
804 name
= String16(block
.getAttributeStringValue(nameIdx
, &len
));
807 ssize_t identIdx
= block
.indexOfAttribute(NULL
, "id");
809 const char16_t* identStr
= block
.getAttributeStringValue(identIdx
, &len
);
810 Res_value identValue
;
811 if (!ResTable::stringToInt(identStr
, len
, &identValue
)) {
812 srcPos
.error("Given 'id' attribute is not an integer: %s\n",
813 String8(block
.getAttributeStringValue(identIdx
, &len
)).string());
814 hasErrors
= localHasErrors
= true;
816 ident
= identValue
.data
;
817 nextPublicId
.replaceValueFor(type
, ident
+1);
819 } else if (nextPublicId
.indexOfKey(type
) < 0) {
820 srcPos
.error("No 'id' attribute supplied <public>,"
821 " and no previous id defined in this file.\n");
822 hasErrors
= localHasErrors
= true;
823 } else if (!localHasErrors
) {
824 ident
= nextPublicId
.valueFor(type
);
825 nextPublicId
.replaceValueFor(type
, ident
+1);
828 if (!localHasErrors
) {
829 err
= outTable
->addPublic(srcPos
, myPackage
, type
, name
, ident
);
830 if (err
< NO_ERROR
) {
831 hasErrors
= localHasErrors
= true;
834 if (!localHasErrors
) {
835 sp
<AaptSymbols
> symbols
= assets
->getSymbolsFor(String8("R"));
836 if (symbols
!= NULL
) {
837 symbols
= symbols
->addNestedSymbol(String8(type
), srcPos
);
839 if (symbols
!= NULL
) {
840 symbols
->makeSymbolPublic(String8(name
), srcPos
);
842 block
.getComment(&len
) ? block
.getComment(&len
) : nulStr
);
843 symbols
->appendComment(String8(name
), comment
, srcPos
);
845 srcPos
.error("Unable to create symbols!\n");
846 hasErrors
= localHasErrors
= true;
850 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
851 if (code
== ResXMLTree::END_TAG
) {
852 if (strcmp16(block
.getElementName(&len
), public16
.string()) == 0) {
859 } else if (strcmp16(block
.getElementName(&len
), public_padding16
.string()) == 0) {
860 SourcePos
srcPos(in
->getPrintableSource(), block
.getLineNumber());
863 ssize_t typeIdx
= block
.indexOfAttribute(NULL
, "type");
865 srcPos
.error("A 'type' attribute is required for <public-padding>\n");
866 hasErrors
= localHasErrors
= true;
868 type
= String16(block
.getAttributeStringValue(typeIdx
, &len
));
871 ssize_t nameIdx
= block
.indexOfAttribute(NULL
, "name");
873 srcPos
.error("A 'name' attribute is required for <public-padding>\n");
874 hasErrors
= localHasErrors
= true;
876 name
= String16(block
.getAttributeStringValue(nameIdx
, &len
));
879 ssize_t startIdx
= block
.indexOfAttribute(NULL
, "start");
881 const char16_t* startStr
= block
.getAttributeStringValue(startIdx
, &len
);
882 Res_value startValue
;
883 if (!ResTable::stringToInt(startStr
, len
, &startValue
)) {
884 srcPos
.error("Given 'start' attribute is not an integer: %s\n",
885 String8(block
.getAttributeStringValue(startIdx
, &len
)).string());
886 hasErrors
= localHasErrors
= true;
888 start
= startValue
.data
;
890 } else if (nextPublicId
.indexOfKey(type
) < 0) {
891 srcPos
.error("No 'start' attribute supplied <public-padding>,"
892 " and no previous id defined in this file.\n");
893 hasErrors
= localHasErrors
= true;
894 } else if (!localHasErrors
) {
895 start
= nextPublicId
.valueFor(type
);
899 ssize_t endIdx
= block
.indexOfAttribute(NULL
, "end");
901 const char16_t* endStr
= block
.getAttributeStringValue(endIdx
, &len
);
903 if (!ResTable::stringToInt(endStr
, len
, &endValue
)) {
904 srcPos
.error("Given 'end' attribute is not an integer: %s\n",
905 String8(block
.getAttributeStringValue(endIdx
, &len
)).string());
906 hasErrors
= localHasErrors
= true;
911 srcPos
.error("No 'end' attribute supplied <public-padding>\n");
912 hasErrors
= localHasErrors
= true;
916 nextPublicId
.replaceValueFor(type
, end
+1);
918 srcPos
.error("Padding start '%ul' is after end '%ul'\n",
920 hasErrors
= localHasErrors
= true;
924 block
.getComment(&len
) ? block
.getComment(&len
) : nulStr
);
925 for (uint32_t curIdent
=start
; curIdent
<=end
; curIdent
++) {
926 if (localHasErrors
) {
929 String16
curName(name
);
931 sprintf(buf
, "%d", (int)(end
-curIdent
+1));
932 curName
.append(String16(buf
));
934 err
= outTable
->addEntry(srcPos
, myPackage
, type
, curName
,
935 String16("padding"), NULL
, &curParams
, false,
936 ResTable_map::TYPE_STRING
, overwrite
);
937 if (err
< NO_ERROR
) {
938 hasErrors
= localHasErrors
= true;
941 err
= outTable
->addPublic(srcPos
, myPackage
, type
,
943 if (err
< NO_ERROR
) {
944 hasErrors
= localHasErrors
= true;
947 sp
<AaptSymbols
> symbols
= assets
->getSymbolsFor(String8("R"));
948 if (symbols
!= NULL
) {
949 symbols
= symbols
->addNestedSymbol(String8(type
), srcPos
);
951 if (symbols
!= NULL
) {
952 symbols
->makeSymbolPublic(String8(curName
), srcPos
);
953 symbols
->appendComment(String8(curName
), comment
, srcPos
);
955 srcPos
.error("Unable to create symbols!\n");
956 hasErrors
= localHasErrors
= true;
960 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
961 if (code
== ResXMLTree::END_TAG
) {
962 if (strcmp16(block
.getElementName(&len
), public_padding16
.string()) == 0) {
969 } else if (strcmp16(block
.getElementName(&len
), private_symbols16
.string()) == 0) {
971 ssize_t pkgIdx
= block
.indexOfAttribute(NULL
, "package");
973 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
974 "A 'package' attribute is required for <private-symbols>\n");
975 hasErrors
= localHasErrors
= true;
977 pkg
= String16(block
.getAttributeStringValue(pkgIdx
, &len
));
978 if (!localHasErrors
) {
979 assets
->setSymbolsPrivatePackage(String8(pkg
));
982 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
983 if (code
== ResXMLTree::END_TAG
) {
984 if (strcmp16(block
.getElementName(&len
), private_symbols16
.string()) == 0) {
991 } else if (strcmp16(block
.getElementName(&len
), add_resource16
.string()) == 0) {
992 SourcePos
srcPos(in
->getPrintableSource(), block
.getLineNumber());
995 ssize_t typeIdx
= block
.indexOfAttribute(NULL
, "type");
997 srcPos
.error("A 'type' attribute is required for <add-resource>\n");
998 hasErrors
= localHasErrors
= true;
1000 typeName
= String16(block
.getAttributeStringValue(typeIdx
, &len
));
1003 ssize_t nameIdx
= block
.indexOfAttribute(NULL
, "name");
1005 srcPos
.error("A 'name' attribute is required for <add-resource>\n");
1006 hasErrors
= localHasErrors
= true;
1008 name
= String16(block
.getAttributeStringValue(nameIdx
, &len
));
1010 outTable
->canAddEntry(srcPos
, myPackage
, typeName
, name
);
1012 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
1013 if (code
== ResXMLTree::END_TAG
) {
1014 if (strcmp16(block
.getElementName(&len
), add_resource16
.string()) == 0) {
1021 } else if (strcmp16(block
.getElementName(&len
), declare_styleable16
.string()) == 0) {
1022 SourcePos
srcPos(in
->getPrintableSource(), block
.getLineNumber());
1025 ssize_t identIdx
= block
.indexOfAttribute(NULL
, "name");
1027 srcPos
.error("A 'name' attribute is required for <declare-styleable>\n");
1028 hasErrors
= localHasErrors
= true;
1030 ident
= String16(block
.getAttributeStringValue(identIdx
, &len
));
1032 sp
<AaptSymbols
> symbols
= assets
->getSymbolsFor(String8("R"));
1033 if (!localHasErrors
) {
1034 if (symbols
!= NULL
) {
1035 symbols
= symbols
->addNestedSymbol(String8("styleable"), srcPos
);
1037 sp
<AaptSymbols
> styleSymbols
= symbols
;
1038 if (symbols
!= NULL
) {
1039 symbols
= symbols
->addNestedSymbol(String8(ident
), srcPos
);
1041 if (symbols
== NULL
) {
1042 srcPos
.error("Unable to create symbols!\n");
1043 return UNKNOWN_ERROR
;
1047 block
.getComment(&len
) ? block
.getComment(&len
) : nulStr
);
1048 styleSymbols
->appendComment(String8(ident
), comment
, srcPos
);
1053 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
1054 if (code
== ResXMLTree::START_TAG
) {
1055 if (strcmp16(block
.getElementName(&len
), skip16
.string()) == 0) {
1056 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
1057 && code
!= ResXMLTree::BAD_DOCUMENT
) {
1058 if (code
== ResXMLTree::END_TAG
) {
1059 if (strcmp16(block
.getElementName(&len
), skip16
.string()) == 0) {
1065 } else if (strcmp16(block
.getElementName(&len
), eat_comment16
.string()) == 0) {
1066 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
1067 && code
!= ResXMLTree::BAD_DOCUMENT
) {
1068 if (code
== ResXMLTree::END_TAG
) {
1069 if (strcmp16(block
.getElementName(&len
), eat_comment16
.string()) == 0) {
1075 } else if (strcmp16(block
.getElementName(&len
), attr16
.string()) != 0) {
1076 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1077 "Tag <%s> can not appear inside <declare-styleable>, only <attr>\n",
1078 String8(block
.getElementName(&len
)).string());
1079 return UNKNOWN_ERROR
;
1083 block
.getComment(&len
) ? block
.getComment(&len
) : nulStr
);
1085 err
= compileAttribute(in
, block
, myPackage
, outTable
, &itemIdent
, true);
1086 if (err
!= NO_ERROR
) {
1087 hasErrors
= localHasErrors
= true;
1090 if (symbols
!= NULL
) {
1091 SourcePos
srcPos(String8(in
->getPrintableSource()), block
.getLineNumber());
1092 symbols
->addSymbol(String8(itemIdent
), 0, srcPos
);
1093 symbols
->appendComment(String8(itemIdent
), comment
, srcPos
);
1094 //printf("Attribute %s comment: %s\n", String8(itemIdent).string(),
1095 // String8(comment).string());
1097 } else if (code
== ResXMLTree::END_TAG
) {
1098 if (strcmp16(block
.getElementName(&len
), declare_styleable16
.string()) == 0) {
1102 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1103 "Found tag </%s> where </attr> is expected\n",
1104 String8(block
.getElementName(&len
)).string());
1105 return UNKNOWN_ERROR
;
1110 } else if (strcmp16(block
.getElementName(&len
), attr16
.string()) == 0) {
1111 err
= compileAttribute(in
, block
, myPackage
, outTable
, NULL
);
1112 if (err
!= NO_ERROR
) {
1117 } else if (strcmp16(block
.getElementName(&len
), item16
.string()) == 0) {
1119 ssize_t attri
= block
.indexOfAttribute(NULL
, "type");
1121 curType
= String16(block
.getAttributeStringValue(attri
, &len
));
1122 ssize_t formatIdx
= block
.indexOfAttribute(NULL
, "format");
1123 if (formatIdx
>= 0) {
1124 String16 formatStr
= String16(block
.getAttributeStringValue(
1126 curFormat
= parse_flags(formatStr
.string(), formatStr
.size(),
1128 if (curFormat
== 0) {
1129 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1130 "Tag <item> 'format' attribute value \"%s\" not valid\n",
1131 String8(formatStr
).string());
1132 hasErrors
= localHasErrors
= true;
1136 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1137 "A 'type' attribute is required for <item>\n");
1138 hasErrors
= localHasErrors
= true;
1141 } else if (strcmp16(block
.getElementName(&len
), string16
.string()) == 0) {
1142 // Note the existence and locale of every string we process
1144 curParams
.getLocale(rawLocale
);
1145 String8
locale(rawLocale
);
1147 String16 translatable
;
1150 size_t n
= block
.getAttributeCount();
1151 for (size_t i
= 0; i
< n
; i
++) {
1153 const uint16_t* attr
= block
.getAttributeName(i
, &length
);
1154 if (strcmp16(attr
, name16
.string()) == 0) {
1155 name
.setTo(block
.getAttributeStringValue(i
, &length
));
1156 } else if (strcmp16(attr
, translatable16
.string()) == 0) {
1157 translatable
.setTo(block
.getAttributeStringValue(i
, &length
));
1158 } else if (strcmp16(attr
, formatted16
.string()) == 0) {
1159 formatted
.setTo(block
.getAttributeStringValue(i
, &length
));
1163 if (name
.size() > 0) {
1164 if (translatable
== false16
) {
1165 curIsFormatted
= false;
1166 // Untranslatable strings must only exist in the default [empty] locale
1167 if (locale
.size() > 0) {
1168 fprintf(stderr
, "aapt: warning: string '%s' in %s marked untranslatable but exists"
1169 " in locale '%s'\n", String8(name
).string(),
1170 bundle
->getResourceSourceDirs()[0],
1172 // hasErrors = localHasErrors = true;
1174 // Intentionally empty block:
1176 // Don't add untranslatable strings to the localization table; that
1177 // way if we later see localizations of them, they'll be flagged as
1178 // having no default translation.
1181 outTable
->addLocalization(name
, locale
);
1184 if (formatted
== false16
) {
1185 curIsFormatted
= false;
1191 curFormat
= ResTable_map::TYPE_REFERENCE
|ResTable_map::TYPE_STRING
;
1193 curIsPseudolocalizable
= true;
1194 } else if (strcmp16(block
.getElementName(&len
), drawable16
.string()) == 0) {
1195 curTag
= &drawable16
;
1196 curType
= drawable16
;
1197 curFormat
= ResTable_map::TYPE_REFERENCE
|ResTable_map::TYPE_COLOR
;
1198 } else if (strcmp16(block
.getElementName(&len
), color16
.string()) == 0) {
1201 curFormat
= ResTable_map::TYPE_REFERENCE
|ResTable_map::TYPE_COLOR
;
1202 } else if (strcmp16(block
.getElementName(&len
), bool16
.string()) == 0) {
1205 curFormat
= ResTable_map::TYPE_REFERENCE
|ResTable_map::TYPE_BOOLEAN
;
1206 } else if (strcmp16(block
.getElementName(&len
), integer16
.string()) == 0) {
1207 curTag
= &integer16
;
1208 curType
= integer16
;
1209 curFormat
= ResTable_map::TYPE_REFERENCE
|ResTable_map::TYPE_INTEGER
;
1210 } else if (strcmp16(block
.getElementName(&len
), dimen16
.string()) == 0) {
1213 curFormat
= ResTable_map::TYPE_REFERENCE
|ResTable_map::TYPE_DIMENSION
;
1214 } else if (strcmp16(block
.getElementName(&len
), fraction16
.string()) == 0) {
1215 curTag
= &fraction16
;
1216 curType
= fraction16
;
1217 curFormat
= ResTable_map::TYPE_REFERENCE
|ResTable_map::TYPE_FRACTION
;
1218 } else if (strcmp16(block
.getElementName(&len
), bag16
.string()) == 0) {
1221 ssize_t attri
= block
.indexOfAttribute(NULL
, "type");
1223 curType
= String16(block
.getAttributeStringValue(attri
, &len
));
1225 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1226 "A 'type' attribute is required for <bag>\n");
1227 hasErrors
= localHasErrors
= true;
1229 } else if (strcmp16(block
.getElementName(&len
), style16
.string()) == 0) {
1233 } else if (strcmp16(block
.getElementName(&len
), plurals16
.string()) == 0) {
1234 curTag
= &plurals16
;
1235 curType
= plurals16
;
1237 } else if (strcmp16(block
.getElementName(&len
), array16
.string()) == 0) {
1241 curIsBagReplaceOnOverwrite
= true;
1242 ssize_t formatIdx
= block
.indexOfAttribute(NULL
, "format");
1243 if (formatIdx
>= 0) {
1244 String16 formatStr
= String16(block
.getAttributeStringValue(
1246 curFormat
= parse_flags(formatStr
.string(), formatStr
.size(),
1248 if (curFormat
== 0) {
1249 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1250 "Tag <array> 'format' attribute value \"%s\" not valid\n",
1251 String8(formatStr
).string());
1252 hasErrors
= localHasErrors
= true;
1255 } else if (strcmp16(block
.getElementName(&len
), string_array16
.string()) == 0) {
1256 curTag
= &string_array16
;
1258 curFormat
= ResTable_map::TYPE_REFERENCE
|ResTable_map::TYPE_STRING
;
1260 curIsBagReplaceOnOverwrite
= true;
1261 curIsPseudolocalizable
= true;
1262 } else if (strcmp16(block
.getElementName(&len
), integer_array16
.string()) == 0) {
1263 curTag
= &integer_array16
;
1265 curFormat
= ResTable_map::TYPE_REFERENCE
|ResTable_map::TYPE_INTEGER
;
1267 curIsBagReplaceOnOverwrite
= true;
1269 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1270 "Found tag %s where item is expected\n",
1271 String8(block
.getElementName(&len
)).string());
1272 return UNKNOWN_ERROR
;
1276 ssize_t identIdx
= block
.indexOfAttribute(NULL
, "name");
1277 if (identIdx
>= 0) {
1278 ident
= String16(block
.getAttributeStringValue(identIdx
, &len
));
1280 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1281 "A 'name' attribute is required for <%s>\n",
1282 String8(*curTag
).string());
1283 hasErrors
= localHasErrors
= true;
1286 String16
comment(block
.getComment(&len
) ? block
.getComment(&len
) : nulStr
);
1289 // Figure out the parent of this bag...
1290 String16 parentIdent
;
1291 ssize_t parentIdentIdx
= block
.indexOfAttribute(NULL
, "parent");
1292 if (parentIdentIdx
>= 0) {
1293 parentIdent
= String16(block
.getAttributeStringValue(parentIdentIdx
, &len
));
1295 ssize_t sep
= ident
.findLast('.');
1297 parentIdent
.setTo(ident
, sep
);
1301 if (!localHasErrors
) {
1302 err
= outTable
->startBag(SourcePos(in
->getPrintableSource(),
1303 block
.getLineNumber()), myPackage
, curType
, ident
,
1304 parentIdent
, &curParams
,
1305 overwrite
, curIsBagReplaceOnOverwrite
);
1306 if (err
!= NO_ERROR
) {
1307 hasErrors
= localHasErrors
= true;
1311 ssize_t elmIndex
= 0;
1312 char elmIndexStr
[14];
1313 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
1314 && code
!= ResXMLTree::BAD_DOCUMENT
) {
1316 if (code
== ResXMLTree::START_TAG
) {
1317 if (strcmp16(block
.getElementName(&len
), item16
.string()) != 0) {
1318 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1319 "Tag <%s> can not appear inside <%s>, only <item>\n",
1320 String8(block
.getElementName(&len
)).string(),
1321 String8(*curTag
).string());
1322 return UNKNOWN_ERROR
;
1326 if (curType
== array16
) {
1327 sprintf(elmIndexStr
, "^index_%d", (int)elmIndex
++);
1328 itemIdent
= String16(elmIndexStr
);
1329 } else if (curType
== plurals16
) {
1330 ssize_t itemIdentIdx
= block
.indexOfAttribute(NULL
, "quantity");
1331 if (itemIdentIdx
>= 0) {
1332 String16
quantity16(block
.getAttributeStringValue(itemIdentIdx
, &len
));
1333 if (quantity16
== other16
) {
1334 itemIdent
= quantityOther16
;
1336 else if (quantity16
== zero16
) {
1337 itemIdent
= quantityZero16
;
1339 else if (quantity16
== one16
) {
1340 itemIdent
= quantityOne16
;
1342 else if (quantity16
== two16
) {
1343 itemIdent
= quantityTwo16
;
1345 else if (quantity16
== few16
) {
1346 itemIdent
= quantityFew16
;
1348 else if (quantity16
== many16
) {
1349 itemIdent
= quantityMany16
;
1352 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1353 "Illegal 'quantity' attribute is <item> inside <plurals>\n");
1354 hasErrors
= localHasErrors
= true;
1357 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1358 "A 'quantity' attribute is required for <item> inside <plurals>\n");
1359 hasErrors
= localHasErrors
= true;
1362 ssize_t itemIdentIdx
= block
.indexOfAttribute(NULL
, "name");
1363 if (itemIdentIdx
>= 0) {
1364 itemIdent
= String16(block
.getAttributeStringValue(itemIdentIdx
, &len
));
1366 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1367 "A 'name' attribute is required for <item>\n");
1368 hasErrors
= localHasErrors
= true;
1372 ResXMLParser::ResXMLPosition parserPosition
;
1373 block
.getPosition(&parserPosition
);
1375 err
= parseAndAddBag(bundle
, in
, &block
, curParams
, myPackage
, curType
,
1376 ident
, parentIdent
, itemIdent
, curFormat
, curIsFormatted
,
1377 false, overwrite
, outTable
);
1378 if (err
== NO_ERROR
) {
1379 if (curIsPseudolocalizable
&& localeIsDefined(curParams
)
1380 && bundle
->getPseudolocalize()) {
1381 // pseudolocalize here
1383 block
.setPosition(parserPosition
);
1384 err
= parseAndAddBag(bundle
, in
, &block
, pseudoParams
, myPackage
,
1385 curType
, ident
, parentIdent
, itemIdent
, curFormat
,
1386 curIsFormatted
, true, overwrite
, outTable
);
1390 if (err
!= NO_ERROR
) {
1391 hasErrors
= localHasErrors
= true;
1393 } else if (code
== ResXMLTree::END_TAG
) {
1394 if (strcmp16(block
.getElementName(&len
), curTag
->string()) != 0) {
1395 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1396 "Found tag </%s> where </%s> is expected\n",
1397 String8(block
.getElementName(&len
)).string(),
1398 String8(*curTag
).string());
1399 return UNKNOWN_ERROR
;
1405 ResXMLParser::ResXMLPosition parserPosition
;
1406 block
.getPosition(&parserPosition
);
1408 err
= parseAndAddEntry(bundle
, in
, &block
, curParams
, myPackage
, curType
, ident
,
1409 *curTag
, curIsStyled
, curFormat
, curIsFormatted
,
1410 false, overwrite
, outTable
);
1412 if (err
< NO_ERROR
) { // Why err < NO_ERROR instead of err != NO_ERROR?
1413 hasErrors
= localHasErrors
= true;
1415 else if (err
== NO_ERROR
) {
1416 if (curIsPseudolocalizable
&& localeIsDefined(curParams
)
1417 && bundle
->getPseudolocalize()) {
1418 // pseudolocalize here
1419 block
.setPosition(parserPosition
);
1420 err
= parseAndAddEntry(bundle
, in
, &block
, pseudoParams
, myPackage
, curType
,
1421 ident
, *curTag
, curIsStyled
, curFormat
,
1422 curIsFormatted
, true, overwrite
, outTable
);
1423 if (err
!= NO_ERROR
) {
1424 hasErrors
= localHasErrors
= true;
1431 if (comment
.size() > 0) {
1432 printf("Comment for @%s:%s/%s: %s\n", String8(myPackage
).string(),
1433 String8(curType
).string(), String8(ident
).string(),
1434 String8(comment
).string());
1437 if (!localHasErrors
) {
1438 outTable
->appendComment(myPackage
, curType
, ident
, comment
, false);
1441 else if (code
== ResXMLTree::END_TAG
) {
1442 if (strcmp16(block
.getElementName(&len
), resources16
.string()) != 0) {
1443 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1444 "Unexpected end tag %s\n", String8(block
.getElementName(&len
)).string());
1445 return UNKNOWN_ERROR
;
1448 else if (code
== ResXMLTree::START_NAMESPACE
|| code
== ResXMLTree::END_NAMESPACE
) {
1450 else if (code
== ResXMLTree::TEXT
) {
1451 if (isWhitespace(block
.getText(&len
))) {
1454 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1455 "Found text \"%s\" where item tag is expected\n",
1456 String8(block
.getText(&len
)).string());
1457 return UNKNOWN_ERROR
;
1461 return hasErrors
? UNKNOWN_ERROR
: NO_ERROR
;
1464 ResourceTable::ResourceTable(Bundle
* bundle
, const String16
& assetsPackage
)
1465 : mAssetsPackage(assetsPackage
), mNextPackageId(1), mHaveAppPackage(false),
1466 mIsAppPackage(!bundle
->getExtending()),
1472 status_t
ResourceTable::addIncludedResources(Bundle
* bundle
, const sp
<AaptAssets
>& assets
)
1474 status_t err
= assets
->buildIncludedResources(bundle
);
1475 if (err
!= NO_ERROR
) {
1479 // For future reference to included resources.
1482 const ResTable
& incl
= assets
->getIncludedResources();
1484 // Retrieve all the packages.
1485 const size_t N
= incl
.getBasePackageCount();
1486 for (size_t phase
=0; phase
<2; phase
++) {
1487 for (size_t i
=0; i
<N
; i
++) {
1488 String16
name(incl
.getBasePackageName(i
));
1489 uint32_t id
= incl
.getBasePackageId(i
);
1490 // First time through: only add base packages (id
1491 // is not 0); second time through add the other
1495 // Skip base packages -- already one.
1498 // Assign a dynamic id.
1499 id
= mNextPackageId
;
1501 } else if (id
!= 0) {
1503 if (mHaveAppPackage
) {
1504 fprintf(stderr
, "Included resources have two application packages!\n");
1505 return UNKNOWN_ERROR
;
1507 mHaveAppPackage
= true;
1509 if (mNextPackageId
> id
) {
1510 fprintf(stderr
, "Included base package ID %d already in use!\n", id
);
1511 return UNKNOWN_ERROR
;
1515 NOISY(printf("Including package %s with ID=%d\n",
1516 String8(name
).string(), id
));
1517 sp
<Package
> p
= new Package(name
, id
);
1518 mPackages
.add(name
, p
);
1519 mOrderedPackages
.add(p
);
1521 if (id
>= mNextPackageId
) {
1522 mNextPackageId
= id
+1;
1528 // Every resource table always has one first entry, the bag attributes.
1529 const SourcePos
unknown(String8("????"), 0);
1530 sp
<Type
> attr
= getType(mAssetsPackage
, String16("attr"), unknown
);
1535 status_t
ResourceTable::addPublic(const SourcePos
& sourcePos
,
1536 const String16
& package
,
1537 const String16
& type
,
1538 const String16
& name
,
1539 const uint32_t ident
)
1541 uint32_t rid
= mAssets
->getIncludedResources()
1542 .identifierForName(name
.string(), name
.size(),
1543 type
.string(), type
.size(),
1544 package
.string(), package
.size());
1546 sourcePos
.error("Error declaring public resource %s/%s for included package %s\n",
1547 String8(type
).string(), String8(name
).string(),
1548 String8(package
).string());
1549 return UNKNOWN_ERROR
;
1552 sp
<Type
> t
= getType(package
, type
, sourcePos
);
1554 return UNKNOWN_ERROR
;
1556 return t
->addPublic(sourcePos
, name
, ident
);
1559 status_t
ResourceTable::addEntry(const SourcePos
& sourcePos
,
1560 const String16
& package
,
1561 const String16
& type
,
1562 const String16
& name
,
1563 const String16
& value
,
1564 const Vector
<StringPool::entry_style_span
>* style
,
1565 const ResTable_config
* params
,
1566 const bool doSetIndex
,
1567 const int32_t format
,
1568 const bool overwrite
)
1570 // Check for adding entries in other packages... for now we do
1571 // nothing. We need to do the right thing here to support skinning.
1572 uint32_t rid
= mAssets
->getIncludedResources()
1573 .identifierForName(name
.string(), name
.size(),
1574 type
.string(), type
.size(),
1575 package
.string(), package
.size());
1581 if (name
== String16("left")) {
1582 printf("Adding entry left: file=%s, line=%d, type=%s, value=%s\n",
1583 sourcePos
.file
.string(), sourcePos
.line
, String8(type
).string(),
1584 String8(value
).string());
1588 sp
<Entry
> e
= getEntry(package
, type
, name
, sourcePos
, overwrite
,
1589 params
, doSetIndex
);
1591 return UNKNOWN_ERROR
;
1593 status_t err
= e
->setItem(sourcePos
, value
, style
, format
, overwrite
);
1594 if (err
== NO_ERROR
) {
1600 status_t
ResourceTable::startBag(const SourcePos
& sourcePos
,
1601 const String16
& package
,
1602 const String16
& type
,
1603 const String16
& name
,
1604 const String16
& bagParent
,
1605 const ResTable_config
* params
,
1607 bool replace
, bool isId
)
1609 status_t result
= NO_ERROR
;
1611 // Check for adding entries in other packages... for now we do
1612 // nothing. We need to do the right thing here to support skinning.
1613 uint32_t rid
= mAssets
->getIncludedResources()
1614 .identifierForName(name
.string(), name
.size(),
1615 type
.string(), type
.size(),
1616 package
.string(), package
.size());
1622 if (name
== String16("left")) {
1623 printf("Adding bag left: file=%s, line=%d, type=%s\n",
1624 sourcePos
.file
.striing(), sourcePos
.line
, String8(type
).string());
1627 if (overlay
&& !mBundle
->getAutoAddOverlay() && !hasBagOrEntry(package
, type
, name
)) {
1628 bool canAdd
= false;
1629 sp
<Package
> p
= mPackages
.valueFor(package
);
1631 sp
<Type
> t
= p
->getTypes().valueFor(type
);
1633 if (t
->getCanAddEntries().indexOf(name
) >= 0) {
1639 sourcePos
.error("Resource does not already exist in overlay at '%s'; use <add-resource> to add.\n",
1640 String8(name
).string());
1641 return UNKNOWN_ERROR
;
1644 sp
<Entry
> e
= getEntry(package
, type
, name
, sourcePos
, overlay
, params
);
1646 return UNKNOWN_ERROR
;
1649 // If a parent is explicitly specified, set it.
1650 if (bagParent
.size() > 0) {
1651 String16 curPar
= e
->getParent();
1652 if (curPar
.size() > 0 && curPar
!= bagParent
) {
1653 sourcePos
.error("Conflicting parents specified, was '%s', now '%s'\n",
1654 String8(e
->getParent()).string(),
1655 String8(bagParent
).string());
1656 return UNKNOWN_ERROR
;
1658 e
->setParent(bagParent
);
1661 if ((result
= e
->makeItABag(sourcePos
)) != NO_ERROR
) {
1665 if (overlay
&& replace
) {
1666 return e
->emptyBag(sourcePos
);
1671 status_t
ResourceTable::addBag(const SourcePos
& sourcePos
,
1672 const String16
& package
,
1673 const String16
& type
,
1674 const String16
& name
,
1675 const String16
& bagParent
,
1676 const String16
& bagKey
,
1677 const String16
& value
,
1678 const Vector
<StringPool::entry_style_span
>* style
,
1679 const ResTable_config
* params
,
1680 bool replace
, bool isId
, const int32_t format
)
1682 // Check for adding entries in other packages... for now we do
1683 // nothing. We need to do the right thing here to support skinning.
1684 uint32_t rid
= mAssets
->getIncludedResources()
1685 .identifierForName(name
.string(), name
.size(),
1686 type
.string(), type
.size(),
1687 package
.string(), package
.size());
1693 if (name
== String16("left")) {
1694 printf("Adding bag left: file=%s, line=%d, type=%s\n",
1695 sourcePos
.file
.striing(), sourcePos
.line
, String8(type
).string());
1698 sp
<Entry
> e
= getEntry(package
, type
, name
, sourcePos
, replace
, params
);
1700 return UNKNOWN_ERROR
;
1703 // If a parent is explicitly specified, set it.
1704 if (bagParent
.size() > 0) {
1705 String16 curPar
= e
->getParent();
1706 if (curPar
.size() > 0 && curPar
!= bagParent
) {
1707 sourcePos
.error("Conflicting parents specified, was '%s', now '%s'\n",
1708 String8(e
->getParent()).string(),
1709 String8(bagParent
).string());
1710 return UNKNOWN_ERROR
;
1712 e
->setParent(bagParent
);
1715 const bool first
= e
->getBag().indexOfKey(bagKey
) < 0;
1716 status_t err
= e
->addToBag(sourcePos
, bagKey
, value
, style
, replace
, isId
, format
);
1717 if (err
== NO_ERROR
&& first
) {
1723 bool ResourceTable::hasBagOrEntry(const String16
& package
,
1724 const String16
& type
,
1725 const String16
& name
) const
1727 // First look for this in the included resources...
1728 uint32_t rid
= mAssets
->getIncludedResources()
1729 .identifierForName(name
.string(), name
.size(),
1730 type
.string(), type
.size(),
1731 package
.string(), package
.size());
1736 sp
<Package
> p
= mPackages
.valueFor(package
);
1738 sp
<Type
> t
= p
->getTypes().valueFor(type
);
1740 sp
<ConfigList
> c
= t
->getConfigs().valueFor(name
);
1741 if (c
!= NULL
) return true;
1748 bool ResourceTable::hasBagOrEntry(const String16
& ref
,
1749 const String16
* defType
,
1750 const String16
* defPackage
)
1752 String16 package
, type
, name
;
1753 if (!ResTable::expandResourceRef(ref
.string(), ref
.size(), &package
, &type
, &name
,
1754 defType
, defPackage
? defPackage
:&mAssetsPackage
, NULL
)) {
1757 return hasBagOrEntry(package
, type
, name
);
1760 bool ResourceTable::appendComment(const String16
& package
,
1761 const String16
& type
,
1762 const String16
& name
,
1763 const String16
& comment
,
1766 if (comment
.size() <= 0) {
1770 sp
<Package
> p
= mPackages
.valueFor(package
);
1772 sp
<Type
> t
= p
->getTypes().valueFor(type
);
1774 sp
<ConfigList
> c
= t
->getConfigs().valueFor(name
);
1776 c
->appendComment(comment
, onlyIfEmpty
);
1784 bool ResourceTable::appendTypeComment(const String16
& package
,
1785 const String16
& type
,
1786 const String16
& name
,
1787 const String16
& comment
)
1789 if (comment
.size() <= 0) {
1793 sp
<Package
> p
= mPackages
.valueFor(package
);
1795 sp
<Type
> t
= p
->getTypes().valueFor(type
);
1797 sp
<ConfigList
> c
= t
->getConfigs().valueFor(name
);
1799 c
->appendTypeComment(comment
);
1807 void ResourceTable::canAddEntry(const SourcePos
& pos
,
1808 const String16
& package
, const String16
& type
, const String16
& name
)
1810 sp
<Type
> t
= getType(package
, type
, pos
);
1812 t
->canAddEntry(name
);
1816 size_t ResourceTable::size() const {
1817 return mPackages
.size();
1820 size_t ResourceTable::numLocalResources() const {
1824 bool ResourceTable::hasResources() const {
1825 return mNumLocal
> 0;
1828 sp
<AaptFile
> ResourceTable::flatten(Bundle
* bundle
)
1830 sp
<AaptFile
> data
= new AaptFile(String8(), AaptGroupEntry(), String8());
1831 status_t err
= flatten(bundle
, data
);
1832 return err
== NO_ERROR
? data
: NULL
;
1835 inline uint32_t ResourceTable::getResId(const sp
<Package
>& p
,
1839 return makeResId(p
->getAssignedId(), t
->getIndex(), nameId
);
1842 uint32_t ResourceTable::getResId(const String16
& package
,
1843 const String16
& type
,
1844 const String16
& name
,
1845 bool onlyPublic
) const
1847 sp
<Package
> p
= mPackages
.valueFor(package
);
1848 if (p
== NULL
) return 0;
1850 // First look for this in the included resources...
1851 uint32_t specFlags
= 0;
1852 uint32_t rid
= mAssets
->getIncludedResources()
1853 .identifierForName(name
.string(), name
.size(),
1854 type
.string(), type
.size(),
1855 package
.string(), package
.size(),
1859 if ((specFlags
& ResTable_typeSpec::SPEC_PUBLIC
) == 0) {
1864 if (Res_INTERNALID(rid
)) {
1867 return Res_MAKEID(p
->getAssignedId()-1,
1872 sp
<Type
> t
= p
->getTypes().valueFor(type
);
1873 if (t
== NULL
) return 0;
1874 sp
<ConfigList
> c
= t
->getConfigs().valueFor(name
);
1875 if (c
== NULL
) return 0;
1876 int32_t ei
= c
->getEntryIndex();
1877 if (ei
< 0) return 0;
1878 return getResId(p
, t
, ei
);
1881 uint32_t ResourceTable::getResId(const String16
& ref
,
1882 const String16
* defType
,
1883 const String16
* defPackage
,
1884 const char** outErrorMsg
,
1885 bool onlyPublic
) const
1887 String16 package
, type
, name
;
1888 if (!ResTable::expandResourceRef(
1889 ref
.string(), ref
.size(), &package
, &type
, &name
,
1890 defType
, defPackage
? defPackage
:&mAssetsPackage
,
1892 NOISY(printf("Expanding resource: ref=%s\n",
1893 String8(ref
).string()));
1894 NOISY(printf("Expanding resource: defType=%s\n",
1895 defType
? String8(*defType
).string() : "NULL"));
1896 NOISY(printf("Expanding resource: defPackage=%s\n",
1897 defPackage
? String8(*defPackage
).string() : "NULL"));
1898 NOISY(printf("Expanding resource: ref=%s\n", String8(ref
).string()));
1899 NOISY(printf("Expanded resource: p=%s, t=%s, n=%s, res=0\n",
1900 String8(package
).string(), String8(type
).string(),
1901 String8(name
).string()));
1904 uint32_t res
= getResId(package
, type
, name
, onlyPublic
);
1905 NOISY(printf("Expanded resource: p=%s, t=%s, n=%s, res=%d\n",
1906 String8(package
).string(), String8(type
).string(),
1907 String8(name
).string(), res
));
1910 *outErrorMsg
= "No resource found that matches the given name";
1915 bool ResourceTable::isValidResourceName(const String16
& s
)
1917 const char16_t* p
= s
.string();
1920 if ((*p
>= 'a' && *p
<= 'z')
1921 || (*p
>= 'A' && *p
<= 'Z')
1923 || (!first
&& *p
>= '0' && *p
<= '9')) {
1933 bool ResourceTable::stringToValue(Res_value
* outValue
, StringPool
* pool
,
1934 const String16
& str
,
1935 bool preserveSpaces
, bool coerceType
,
1937 const Vector
<StringPool::entry_style_span
>* style
,
1938 String16
* outStr
, void* accessorCookie
,
1944 if (style
== NULL
|| style
->size() == 0) {
1945 // Text is not styled so it can be any type... let's figure it out.
1946 res
= mAssets
->getIncludedResources()
1947 .stringToValue(outValue
, &finalStr
, str
.string(), str
.size(), preserveSpaces
,
1948 coerceType
, attrID
, NULL
, &mAssetsPackage
, this,
1949 accessorCookie
, attrType
);
1951 // Styled text can only be a string, and while collecting the style
1952 // information we have already processed that string!
1953 outValue
->size
= sizeof(Res_value
);
1955 outValue
->dataType
= outValue
->TYPE_STRING
;
1964 if (outValue
->dataType
== outValue
->TYPE_STRING
) {
1965 // Should do better merging styles.
1967 if (style
!= NULL
&& style
->size() > 0) {
1968 outValue
->data
= pool
->add(finalStr
, *style
);
1970 outValue
->data
= pool
->add(finalStr
, true);
1973 // Caller will fill this in later.
1986 uint32_t ResourceTable::getCustomResource(
1987 const String16
& package
, const String16
& type
, const String16
& name
) const
1989 //printf("getCustomResource: %s %s %s\n", String8(package).string(),
1990 // String8(type).string(), String8(name).string());
1991 sp
<Package
> p
= mPackages
.valueFor(package
);
1992 if (p
== NULL
) return 0;
1993 sp
<Type
> t
= p
->getTypes().valueFor(type
);
1994 if (t
== NULL
) return 0;
1995 sp
<ConfigList
> c
= t
->getConfigs().valueFor(name
);
1996 if (c
== NULL
) return 0;
1997 int32_t ei
= c
->getEntryIndex();
1998 if (ei
< 0) return 0;
1999 return getResId(p
, t
, ei
);
2002 uint32_t ResourceTable::getCustomResourceWithCreation(
2003 const String16
& package
, const String16
& type
, const String16
& name
,
2004 const bool createIfNotFound
)
2006 uint32_t resId
= getCustomResource(package
, type
, name
);
2007 if (resId
!= 0 || !createIfNotFound
) {
2010 String16
value("false");
2012 status_t status
= addEntry(mCurrentXmlPos
, package
, type
, name
, value
, NULL
, NULL
, true);
2013 if (status
== NO_ERROR
) {
2014 resId
= getResId(package
, type
, name
);
2020 uint32_t ResourceTable::getRemappedPackage(uint32_t origPackage
) const
2025 bool ResourceTable::getAttributeType(uint32_t attrID
, uint32_t* outType
)
2027 //printf("getAttributeType #%08x\n", attrID);
2029 if (getItemValue(attrID
, ResTable_map::ATTR_TYPE
, &value
)) {
2030 //printf("getAttributeType #%08x (%s): #%08x\n", attrID,
2031 // String8(getEntry(attrID)->getName()).string(), value.data);
2032 *outType
= value
.data
;
2038 bool ResourceTable::getAttributeMin(uint32_t attrID
, uint32_t* outMin
)
2040 //printf("getAttributeMin #%08x\n", attrID);
2042 if (getItemValue(attrID
, ResTable_map::ATTR_MIN
, &value
)) {
2043 *outMin
= value
.data
;
2049 bool ResourceTable::getAttributeMax(uint32_t attrID
, uint32_t* outMax
)
2051 //printf("getAttributeMax #%08x\n", attrID);
2053 if (getItemValue(attrID
, ResTable_map::ATTR_MAX
, &value
)) {
2054 *outMax
= value
.data
;
2060 uint32_t ResourceTable::getAttributeL10N(uint32_t attrID
)
2062 //printf("getAttributeL10N #%08x\n", attrID);
2064 if (getItemValue(attrID
, ResTable_map::ATTR_L10N
, &value
)) {
2067 return ResTable_map::L10N_NOT_REQUIRED
;
2070 bool ResourceTable::getLocalizationSetting()
2072 return mBundle
->getRequireLocalization();
2075 void ResourceTable::reportError(void* accessorCookie
, const char* fmt
, ...)
2077 if (accessorCookie
!= NULL
&& fmt
!= NULL
) {
2078 AccessorCookie
* ac
= (AccessorCookie
*)accessorCookie
;
2083 retval
= vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
2085 ac
->sourcePos
.error("Error: %s (at '%s' with value '%s').\n",
2086 buf
, ac
->attr
.string(), ac
->value
.string());
2090 bool ResourceTable::getAttributeKeys(
2091 uint32_t attrID
, Vector
<String16
>* outKeys
)
2093 sp
<const Entry
> e
= getEntry(attrID
);
2095 const size_t N
= e
->getBag().size();
2096 for (size_t i
=0; i
<N
; i
++) {
2097 const String16
& key
= e
->getBag().keyAt(i
);
2098 if (key
.size() > 0 && key
.string()[0] != '^') {
2107 bool ResourceTable::getAttributeEnum(
2108 uint32_t attrID
, const char16_t* name
, size_t nameLen
,
2109 Res_value
* outValue
)
2111 //printf("getAttributeEnum #%08x %s\n", attrID, String8(name, nameLen).string());
2112 String16
nameStr(name
, nameLen
);
2113 sp
<const Entry
> e
= getEntry(attrID
);
2115 const size_t N
= e
->getBag().size();
2116 for (size_t i
=0; i
<N
; i
++) {
2117 //printf("Comparing %s to %s\n", String8(name, nameLen).string(),
2118 // String8(e->getBag().keyAt(i)).string());
2119 if (e
->getBag().keyAt(i
) == nameStr
) {
2120 return getItemValue(attrID
, e
->getBag().valueAt(i
).bagKeyId
, outValue
);
2127 bool ResourceTable::getAttributeFlags(
2128 uint32_t attrID
, const char16_t* name
, size_t nameLen
,
2129 Res_value
* outValue
)
2131 outValue
->dataType
= Res_value::TYPE_INT_HEX
;
2134 //printf("getAttributeFlags #%08x %s\n", attrID, String8(name, nameLen).string());
2135 String16
nameStr(name
, nameLen
);
2136 sp
<const Entry
> e
= getEntry(attrID
);
2138 const size_t N
= e
->getBag().size();
2140 const char16_t* end
= name
+ nameLen
;
2141 const char16_t* pos
= name
;
2142 bool failed
= false;
2143 while (pos
< end
&& !failed
) {
2144 const char16_t* start
= pos
;
2146 while (pos
< end
&& *pos
!= '|') {
2150 String16
nameStr(start
, pos
-start
);
2152 for (i
=0; i
<N
; i
++) {
2153 //printf("Comparing \"%s\" to \"%s\"\n", String8(nameStr).string(),
2154 // String8(e->getBag().keyAt(i)).string());
2155 if (e
->getBag().keyAt(i
) == nameStr
) {
2157 bool got
= getItemValue(attrID
, e
->getBag().valueAt(i
).bagKeyId
, &val
);
2161 //printf("Got value: 0x%08x\n", val.data);
2162 outValue
->data
|= val
.data
;
2168 // Didn't find this flag identifier.
2181 status_t
ResourceTable::assignResourceIds()
2183 const size_t N
= mOrderedPackages
.size();
2185 status_t firstError
= NO_ERROR
;
2187 // First generate all bag attributes and assign indices.
2188 for (pi
=0; pi
<N
; pi
++) {
2189 sp
<Package
> p
= mOrderedPackages
.itemAt(pi
);
2190 if (p
== NULL
|| p
->getTypes().size() == 0) {
2195 status_t err
= p
->applyPublicTypeOrder();
2196 if (err
!= NO_ERROR
&& firstError
== NO_ERROR
) {
2200 // Generate attributes...
2201 const size_t N
= p
->getOrderedTypes().size();
2203 for (ti
=0; ti
<N
; ti
++) {
2204 sp
<Type
> t
= p
->getOrderedTypes().itemAt(ti
);
2208 const size_t N
= t
->getOrderedConfigs().size();
2209 for (size_t ci
=0; ci
<N
; ci
++) {
2210 sp
<ConfigList
> c
= t
->getOrderedConfigs().itemAt(ci
);
2214 const size_t N
= c
->getEntries().size();
2215 for (size_t ei
=0; ei
<N
; ei
++) {
2216 sp
<Entry
> e
= c
->getEntries().valueAt(ei
);
2220 status_t err
= e
->generateAttributes(this, p
->getName());
2221 if (err
!= NO_ERROR
&& firstError
== NO_ERROR
) {
2228 const SourcePos
unknown(String8("????"), 0);
2229 sp
<Type
> attr
= p
->getType(String16("attr"), unknown
);
2231 // Assign indices...
2232 for (ti
=0; ti
<N
; ti
++) {
2233 sp
<Type
> t
= p
->getOrderedTypes().itemAt(ti
);
2237 err
= t
->applyPublicEntryOrder();
2238 if (err
!= NO_ERROR
&& firstError
== NO_ERROR
) {
2242 const size_t N
= t
->getOrderedConfigs().size();
2245 LOG_ALWAYS_FATAL_IF(ti
== 0 && attr
!= t
,
2246 "First type is not attr!");
2248 for (size_t ei
=0; ei
<N
; ei
++) {
2249 sp
<ConfigList
> c
= t
->getOrderedConfigs().itemAt(ei
);
2253 c
->setEntryIndex(ei
);
2257 // Assign resource IDs to keys in bags...
2258 for (ti
=0; ti
<N
; ti
++) {
2259 sp
<Type
> t
= p
->getOrderedTypes().itemAt(ti
);
2263 const size_t N
= t
->getOrderedConfigs().size();
2264 for (size_t ci
=0; ci
<N
; ci
++) {
2265 sp
<ConfigList
> c
= t
->getOrderedConfigs().itemAt(ci
);
2266 //printf("Ordered config #%d: %p\n", ci, c.get());
2267 const size_t N
= c
->getEntries().size();
2268 for (size_t ei
=0; ei
<N
; ei
++) {
2269 sp
<Entry
> e
= c
->getEntries().valueAt(ei
);
2273 status_t err
= e
->assignResourceIds(this, p
->getName());
2274 if (err
!= NO_ERROR
&& firstError
== NO_ERROR
) {
2284 status_t
ResourceTable::addSymbols(const sp
<AaptSymbols
>& outSymbols
) {
2285 const size_t N
= mOrderedPackages
.size();
2288 for (pi
=0; pi
<N
; pi
++) {
2289 sp
<Package
> p
= mOrderedPackages
.itemAt(pi
);
2290 if (p
->getTypes().size() == 0) {
2295 const size_t N
= p
->getOrderedTypes().size();
2298 for (ti
=0; ti
<N
; ti
++) {
2299 sp
<Type
> t
= p
->getOrderedTypes().itemAt(ti
);
2303 const size_t N
= t
->getOrderedConfigs().size();
2304 sp
<AaptSymbols
> typeSymbols
;
2305 typeSymbols
= outSymbols
->addNestedSymbol(String8(t
->getName()), t
->getPos());
2306 for (size_t ci
=0; ci
<N
; ci
++) {
2307 sp
<ConfigList
> c
= t
->getOrderedConfigs().itemAt(ci
);
2311 uint32_t rid
= getResId(p
, t
, ci
);
2313 return UNKNOWN_ERROR
;
2315 if (Res_GETPACKAGE(rid
) == (size_t)(p
->getAssignedId()-1)) {
2316 typeSymbols
->addSymbol(String8(c
->getName()), rid
, c
->getPos());
2318 String16
comment(c
->getComment());
2319 typeSymbols
->appendComment(String8(c
->getName()), comment
, c
->getPos());
2320 //printf("Type symbol %s comment: %s\n", String8(e->getName()).string(),
2321 // String8(comment).string());
2322 comment
= c
->getTypeComment();
2323 typeSymbols
->appendTypeComment(String8(c
->getName()), comment
);
2326 printf("**** NO MATCH: 0x%08x vs 0x%08x\n",
2327 Res_GETPACKAGE(rid
), p
->getAssignedId());
2338 ResourceTable::addLocalization(const String16
& name
, const String8
& locale
)
2340 mLocalizations
[name
].insert(locale
);
2345 * Flag various sorts of localization problems. '+' indicates checks already implemented;
2346 * '-' indicates checks that will be implemented in the future.
2348 * + A localized string for which no default-locale version exists => warning
2349 * + A string for which no version in an explicitly-requested locale exists => warning
2350 * + A localized translation of an translateable="false" string => warning
2351 * - A localized string not provided in every locale used by the table
2354 ResourceTable::validateLocalizations(void)
2356 status_t err
= NO_ERROR
;
2357 const String8 defaultLocale
;
2359 // For all strings...
2360 for (map
<String16
, set
<String8
> >::iterator nameIter
= mLocalizations
.begin();
2361 nameIter
!= mLocalizations
.end();
2363 const set
<String8
>& configSet
= nameIter
->second
; // naming convenience
2365 // Look for strings with no default localization
2366 if (configSet
.count(defaultLocale
) == 0) {
2367 fprintf(stdout
, "aapt: warning: string '%s' has no default translation in %s; found:",
2368 String8(nameIter
->first
).string(), mBundle
->getResourceSourceDirs()[0]);
2369 for (set
<String8
>::iterator locales
= configSet
.begin();
2370 locales
!= configSet
.end();
2372 fprintf(stdout
, " %s", (*locales
).string());
2374 fprintf(stdout
, "\n");
2375 // !!! TODO: throw an error here in some circumstances
2378 // Check that all requested localizations are present for this string
2379 if (mBundle
->getConfigurations() != NULL
&& mBundle
->getRequireLocalization()) {
2380 const char* allConfigs
= mBundle
->getConfigurations();
2381 const char* start
= allConfigs
;
2386 comma
= strchr(start
, ',');
2387 if (comma
!= NULL
) {
2388 config
.setTo(start
, comma
- start
);
2391 config
.setTo(start
);
2394 // don't bother with the pseudolocale "zz_ZZ"
2395 if (config
!= "zz_ZZ") {
2396 if (configSet
.find(config
) == configSet
.end()) {
2397 // okay, no specific localization found. it's possible that we are
2398 // requiring a specific regional localization [e.g. de_DE] but there is an
2399 // available string in the generic language localization [e.g. de];
2400 // consider that string to have fulfilled the localization requirement.
2401 String8
region(config
.string(), 2);
2402 if (configSet
.find(region
) == configSet
.end()) {
2403 if (configSet
.count(defaultLocale
) == 0) {
2404 fprintf(stdout
, "aapt: warning: "
2405 "**** string '%s' has no default or required localization "
2407 String8(nameIter
->first
).string(),
2409 mBundle
->getResourceSourceDirs()[0]);
2414 } while (comma
!= NULL
);
2423 ResourceFilter::parse(const char* arg
)
2429 const char* p
= arg
;
2438 String8
part(p
, q
-p
);
2440 if (part
== "zz_ZZ") {
2441 mContainsPseudo
= true;
2445 if (AaptGroupEntry::parseNamePart(part
, &axis
, &value
)) {
2446 fprintf(stderr
, "Invalid configuration: %s\n", arg
);
2447 fprintf(stderr
, " ");
2448 for (int i
=0; i
<p
-arg
; i
++) {
2449 fprintf(stderr
, " ");
2451 for (int i
=0; i
<q
-p
; i
++) {
2452 fprintf(stderr
, "^");
2454 fprintf(stderr
, "\n");
2458 ssize_t index
= mData
.indexOfKey(axis
);
2460 mData
.add(axis
, SortedVector
<uint32_t>());
2462 SortedVector
<uint32_t>& sv
= mData
.editValueFor(axis
);
2464 // if it's a locale with a region, also match an unmodified locale of the
2466 if (axis
== AXIS_LANGUAGE
) {
2467 if (value
& 0xffff0000) {
2468 sv
.add(value
& 0x0000ffff);
2480 ResourceFilter::match(int axis
, uint32_t value
)
2483 // they didn't specify anything so take everything
2486 ssize_t index
= mData
.indexOfKey(axis
);
2488 // we didn't request anything on this axis so take everything
2491 const SortedVector
<uint32_t>& sv
= mData
.valueAt(index
);
2492 return sv
.indexOf(value
) >= 0;
2496 ResourceFilter::match(const ResTable_config
& config
)
2498 if (config
.locale
) {
2499 uint32_t locale
= (config
.country
[1] << 24) | (config
.country
[0] << 16)
2500 | (config
.language
[1] << 8) | (config
.language
[0]);
2501 if (!match(AXIS_LANGUAGE
, locale
)) {
2505 if (!match(AXIS_ORIENTATION
, config
.orientation
)) {
2508 if (!match(AXIS_UIMODETYPE
, (config
.uiMode
&ResTable_config::MASK_UI_MODE_TYPE
))) {
2511 if (!match(AXIS_UIMODENIGHT
, (config
.uiMode
&ResTable_config::MASK_UI_MODE_NIGHT
))) {
2514 if (!match(AXIS_DENSITY
, config
.density
)) {
2517 if (!match(AXIS_TOUCHSCREEN
, config
.touchscreen
)) {
2520 if (!match(AXIS_KEYSHIDDEN
, config
.inputFlags
)) {
2523 if (!match(AXIS_KEYBOARD
, config
.keyboard
)) {
2526 if (!match(AXIS_NAVIGATION
, config
.navigation
)) {
2529 if (!match(AXIS_SCREENSIZE
, config
.screenSize
)) {
2532 if (!match(AXIS_VERSION
, config
.version
)) {
2538 status_t
ResourceTable::flatten(Bundle
* bundle
, const sp
<AaptFile
>& dest
)
2540 ResourceFilter filter
;
2541 status_t err
= filter
.parse(bundle
->getConfigurations());
2542 if (err
!= NO_ERROR
) {
2546 const size_t N
= mOrderedPackages
.size();
2549 bool useUTF8
= !bundle
->getWantUTF16() && bundle
->isMinSdkAtLeast(SDK_FROYO
);
2551 // Iterate through all data, collecting all values (strings,
2552 // references, etc).
2553 StringPool valueStrings
= StringPool(false, useUTF8
);
2554 for (pi
=0; pi
<N
; pi
++) {
2555 sp
<Package
> p
= mOrderedPackages
.itemAt(pi
);
2556 if (p
->getTypes().size() == 0) {
2561 StringPool typeStrings
= StringPool(false, useUTF8
);
2562 StringPool keyStrings
= StringPool(false, useUTF8
);
2564 const size_t N
= p
->getOrderedTypes().size();
2565 for (size_t ti
=0; ti
<N
; ti
++) {
2566 sp
<Type
> t
= p
->getOrderedTypes().itemAt(ti
);
2568 typeStrings
.add(String16("<empty>"), false);
2571 typeStrings
.add(t
->getName(), false);
2573 const size_t N
= t
->getOrderedConfigs().size();
2574 for (size_t ci
=0; ci
<N
; ci
++) {
2575 sp
<ConfigList
> c
= t
->getOrderedConfigs().itemAt(ci
);
2579 const size_t N
= c
->getEntries().size();
2580 for (size_t ei
=0; ei
<N
; ei
++) {
2581 ConfigDescription config
= c
->getEntries().keyAt(ei
);
2582 if (!filter
.match(config
)) {
2585 sp
<Entry
> e
= c
->getEntries().valueAt(ei
);
2589 e
->setNameIndex(keyStrings
.add(e
->getName(), true));
2590 status_t err
= e
->prepareFlatten(&valueStrings
, this);
2591 if (err
!= NO_ERROR
) {
2598 p
->setTypeStrings(typeStrings
.createStringBlock());
2599 p
->setKeyStrings(keyStrings
.createStringBlock());
2604 // Now build the array of package chunks.
2605 Vector
<sp
<AaptFile
> > flatPackages
;
2606 for (pi
=0; pi
<N
; pi
++) {
2607 sp
<Package
> p
= mOrderedPackages
.itemAt(pi
);
2608 if (p
->getTypes().size() == 0) {
2613 const size_t N
= p
->getTypeStrings().size();
2615 const size_t baseSize
= sizeof(ResTable_package
);
2617 // Start the package data.
2618 sp
<AaptFile
> data
= new AaptFile(String8(), AaptGroupEntry(), String8());
2619 ResTable_package
* header
= (ResTable_package
*)data
->editData(baseSize
);
2620 if (header
== NULL
) {
2621 fprintf(stderr
, "ERROR: out of memory creating ResTable_package\n");
2624 memset(header
, 0, sizeof(*header
));
2625 header
->header
.type
= htods(RES_TABLE_PACKAGE_TYPE
);
2626 header
->header
.headerSize
= htods(sizeof(*header
));
2627 header
->id
= htodl(p
->getAssignedId());
2628 strcpy16_htod(header
->name
, p
->getName().string());
2630 // Write the string blocks.
2631 const size_t typeStringsStart
= data
->getSize();
2632 sp
<AaptFile
> strFile
= p
->getTypeStringsData();
2633 ssize_t amt
= data
->writeData(strFile
->getData(), strFile
->getSize());
2634 #if PRINT_STRING_METRICS
2635 fprintf(stderr
, "**** type strings: %d\n", amt
);
2641 const size_t keyStringsStart
= data
->getSize();
2642 strFile
= p
->getKeyStringsData();
2643 amt
= data
->writeData(strFile
->getData(), strFile
->getSize());
2644 #if PRINT_STRING_METRICS
2645 fprintf(stderr
, "**** key strings: %d\n", amt
);
2652 // Build the type chunks inside of this package.
2653 for (size_t ti
=0; ti
<N
; ti
++) {
2654 // Retrieve them in the same order as the type string block.
2656 String16
typeName(p
->getTypeStrings().stringAt(ti
, &len
));
2657 sp
<Type
> t
= p
->getTypes().valueFor(typeName
);
2658 LOG_ALWAYS_FATAL_IF(t
== NULL
&& typeName
!= String16("<empty>"),
2659 "Type name %s not found",
2660 String8(typeName
).string());
2662 const size_t N
= t
!= NULL
? t
->getOrderedConfigs().size() : 0;
2664 // First write the typeSpec chunk, containing information about
2665 // each resource entry in this type.
2667 const size_t typeSpecSize
= sizeof(ResTable_typeSpec
) + sizeof(uint32_t)*N
;
2668 const size_t typeSpecStart
= data
->getSize();
2669 ResTable_typeSpec
* tsHeader
= (ResTable_typeSpec
*)
2670 (((uint8_t*)data
->editData(typeSpecStart
+typeSpecSize
)) + typeSpecStart
);
2671 if (tsHeader
== NULL
) {
2672 fprintf(stderr
, "ERROR: out of memory creating ResTable_typeSpec\n");
2675 memset(tsHeader
, 0, sizeof(*tsHeader
));
2676 tsHeader
->header
.type
= htods(RES_TABLE_TYPE_SPEC_TYPE
);
2677 tsHeader
->header
.headerSize
= htods(sizeof(*tsHeader
));
2678 tsHeader
->header
.size
= htodl(typeSpecSize
);
2679 tsHeader
->id
= ti
+1;
2680 tsHeader
->entryCount
= htodl(N
);
2682 uint32_t* typeSpecFlags
= (uint32_t*)
2683 (((uint8_t*)data
->editData())
2684 + typeSpecStart
+ sizeof(ResTable_typeSpec
));
2685 memset(typeSpecFlags
, 0, sizeof(uint32_t)*N
);
2687 for (size_t ei
=0; ei
<N
; ei
++) {
2688 sp
<ConfigList
> cl
= t
->getOrderedConfigs().itemAt(ei
);
2689 if (cl
->getPublic()) {
2690 typeSpecFlags
[ei
] |= htodl(ResTable_typeSpec::SPEC_PUBLIC
);
2692 const size_t CN
= cl
->getEntries().size();
2693 for (size_t ci
=0; ci
<CN
; ci
++) {
2694 if (!filter
.match(cl
->getEntries().keyAt(ci
))) {
2697 for (size_t cj
=ci
+1; cj
<CN
; cj
++) {
2698 if (!filter
.match(cl
->getEntries().keyAt(cj
))) {
2701 typeSpecFlags
[ei
] |= htodl(
2702 cl
->getEntries().keyAt(ci
).diff(cl
->getEntries().keyAt(cj
)));
2708 // We need to write one type chunk for each configuration for
2709 // which we have entries in this type.
2710 const size_t NC
= t
->getUniqueConfigs().size();
2712 const size_t typeSize
= sizeof(ResTable_type
) + sizeof(uint32_t)*N
;
2714 for (size_t ci
=0; ci
<NC
; ci
++) {
2715 ConfigDescription config
= t
->getUniqueConfigs().itemAt(ci
);
2717 NOISY(printf("Writing config %d config: imsi:%d/%d lang:%c%c cnt:%c%c "
2718 "orien:%d ui:%d touch:%d density:%d key:%d inp:%d nav:%d w:%d h:%d\n",
2720 config
.mcc
, config
.mnc
,
2721 config
.language
[0] ? config
.language
[0] : '-',
2722 config
.language
[1] ? config
.language
[1] : '-',
2723 config
.country
[0] ? config
.country
[0] : '-',
2724 config
.country
[1] ? config
.country
[1] : '-',
2733 config
.screenHeight
));
2735 if (!filter
.match(config
)) {
2739 const size_t typeStart
= data
->getSize();
2741 ResTable_type
* tHeader
= (ResTable_type
*)
2742 (((uint8_t*)data
->editData(typeStart
+typeSize
)) + typeStart
);
2743 if (tHeader
== NULL
) {
2744 fprintf(stderr
, "ERROR: out of memory creating ResTable_type\n");
2748 memset(tHeader
, 0, sizeof(*tHeader
));
2749 tHeader
->header
.type
= htods(RES_TABLE_TYPE_TYPE
);
2750 tHeader
->header
.headerSize
= htods(sizeof(*tHeader
));
2752 tHeader
->entryCount
= htodl(N
);
2753 tHeader
->entriesStart
= htodl(typeSize
);
2754 tHeader
->config
= config
;
2755 NOISY(printf("Writing type %d config: imsi:%d/%d lang:%c%c cnt:%c%c "
2756 "orien:%d ui:%d touch:%d density:%d key:%d inp:%d nav:%d w:%d h:%d\n",
2758 tHeader
->config
.mcc
, tHeader
->config
.mnc
,
2759 tHeader
->config
.language
[0] ? tHeader
->config
.language
[0] : '-',
2760 tHeader
->config
.language
[1] ? tHeader
->config
.language
[1] : '-',
2761 tHeader
->config
.country
[0] ? tHeader
->config
.country
[0] : '-',
2762 tHeader
->config
.country
[1] ? tHeader
->config
.country
[1] : '-',
2763 tHeader
->config
.orientation
,
2764 tHeader
->config
.uiMode
,
2765 tHeader
->config
.touchscreen
,
2766 tHeader
->config
.density
,
2767 tHeader
->config
.keyboard
,
2768 tHeader
->config
.inputFlags
,
2769 tHeader
->config
.navigation
,
2770 tHeader
->config
.screenWidth
,
2771 tHeader
->config
.screenHeight
));
2772 tHeader
->config
.swapHtoD();
2774 // Build the entries inside of this type.
2775 for (size_t ei
=0; ei
<N
; ei
++) {
2776 sp
<ConfigList
> cl
= t
->getOrderedConfigs().itemAt(ei
);
2777 sp
<Entry
> e
= cl
->getEntries().valueFor(config
);
2779 // Set the offset for this entry in its type.
2780 uint32_t* index
= (uint32_t*)
2781 (((uint8_t*)data
->editData())
2782 + typeStart
+ sizeof(ResTable_type
));
2784 index
[ei
] = htodl(data
->getSize()-typeStart
-typeSize
);
2786 // Create the entry.
2787 ssize_t amt
= e
->flatten(bundle
, data
, cl
->getPublic());
2792 index
[ei
] = htodl(ResTable_type::NO_ENTRY
);
2796 // Fill in the rest of the type information.
2797 tHeader
= (ResTable_type
*)
2798 (((uint8_t*)data
->editData()) + typeStart
);
2799 tHeader
->header
.size
= htodl(data
->getSize()-typeStart
);
2803 // Fill in the rest of the package information.
2804 header
= (ResTable_package
*)data
->editData();
2805 header
->header
.size
= htodl(data
->getSize());
2806 header
->typeStrings
= htodl(typeStringsStart
);
2807 header
->lastPublicType
= htodl(p
->getTypeStrings().size());
2808 header
->keyStrings
= htodl(keyStringsStart
);
2809 header
->lastPublicKey
= htodl(p
->getKeyStrings().size());
2811 flatPackages
.add(data
);
2814 // And now write out the final chunks.
2815 const size_t dataStart
= dest
->getSize();
2819 ResTable_header header
;
2820 memset(&header
, 0, sizeof(header
));
2821 header
.header
.type
= htods(RES_TABLE_TYPE
);
2822 header
.header
.headerSize
= htods(sizeof(header
));
2823 header
.packageCount
= htodl(flatPackages
.size());
2824 status_t err
= dest
->writeData(&header
, sizeof(header
));
2825 if (err
!= NO_ERROR
) {
2826 fprintf(stderr
, "ERROR: out of memory creating ResTable_header\n");
2831 ssize_t strStart
= dest
->getSize();
2832 err
= valueStrings
.writeStringBlock(dest
);
2833 if (err
!= NO_ERROR
) {
2837 ssize_t amt
= (dest
->getSize()-strStart
);
2839 #if PRINT_STRING_METRICS
2840 fprintf(stderr
, "**** value strings: %d\n", amt
);
2841 fprintf(stderr
, "**** total strings: %d\n", strAmt
);
2844 for (pi
=0; pi
<flatPackages
.size(); pi
++) {
2845 err
= dest
->writeData(flatPackages
[pi
]->getData(),
2846 flatPackages
[pi
]->getSize());
2847 if (err
!= NO_ERROR
) {
2848 fprintf(stderr
, "ERROR: out of memory creating package chunk for ResTable_header\n");
2853 ResTable_header
* header
= (ResTable_header
*)
2854 (((uint8_t*)dest
->getData()) + dataStart
);
2855 header
->header
.size
= htodl(dest
->getSize() - dataStart
);
2857 NOISY(aout
<< "Resource table:"
2858 << HexDump(dest
->getData(), dest
->getSize()) << endl
);
2860 #if PRINT_STRING_METRICS
2861 fprintf(stderr
, "**** total resource table size: %d / %d%% strings\n",
2862 dest
->getSize(), (strAmt
*100)/dest
->getSize());
2868 void ResourceTable::writePublicDefinitions(const String16
& package
, FILE* fp
)
2871 "<!-- This file contains <public> resource definitions for all\n"
2872 " resources that were generated from the source data. -->\n"
2876 writePublicDefinitions(package
, fp
, true);
2877 writePublicDefinitions(package
, fp
, false);
2884 void ResourceTable::writePublicDefinitions(const String16
& package
, FILE* fp
, bool pub
)
2886 bool didHeader
= false;
2888 sp
<Package
> pkg
= mPackages
.valueFor(package
);
2890 const size_t NT
= pkg
->getOrderedTypes().size();
2891 for (size_t i
=0; i
<NT
; i
++) {
2892 sp
<Type
> t
= pkg
->getOrderedTypes().itemAt(i
);
2897 bool didType
= false;
2899 const size_t NC
= t
->getOrderedConfigs().size();
2900 for (size_t j
=0; j
<NC
; j
++) {
2901 sp
<ConfigList
> c
= t
->getOrderedConfigs().itemAt(j
);
2906 if (c
->getPublic() != pub
) {
2916 fprintf(fp
," <!-- PUBLIC SECTION. These resources have been declared public.\n");
2917 fprintf(fp
," Changes to these definitions will break binary compatibility. -->\n\n");
2919 fprintf(fp
," <!-- PRIVATE SECTION. These resources have not been declared public.\n");
2920 fprintf(fp
," You can make them public my moving these lines into a file in res/values. -->\n\n");
2925 const size_t NE
= c
->getEntries().size();
2926 for (size_t k
=0; k
<NE
; k
++) {
2927 const SourcePos
& pos
= c
->getEntries().valueAt(k
)->getPos();
2928 if (pos
.file
!= "") {
2929 fprintf(fp
," <!-- Declared at %s:%d -->\n",
2930 pos
.file
.string(), pos
.line
);
2934 fprintf(fp
, " <public type=\"%s\" name=\"%s\" id=\"0x%08x\" />\n",
2935 String8(t
->getName()).string(),
2936 String8(c
->getName()).string(),
2937 getResId(pkg
, t
, c
->getEntryIndex()));
2943 ResourceTable::Item::Item(const SourcePos
& _sourcePos
,
2945 const String16
& _value
,
2946 const Vector
<StringPool::entry_style_span
>* _style
,
2948 : sourcePos(_sourcePos
)
2960 status_t
ResourceTable::Entry::makeItABag(const SourcePos
& sourcePos
)
2962 if (mType
== TYPE_BAG
) {
2965 if (mType
== TYPE_UNKNOWN
) {
2969 sourcePos
.error("Resource entry %s is already defined as a single item.\n"
2970 "%s:%d: Originally defined here.\n",
2971 String8(mName
).string(),
2972 mItem
.sourcePos
.file
.string(), mItem
.sourcePos
.line
);
2973 return UNKNOWN_ERROR
;
2976 status_t
ResourceTable::Entry::setItem(const SourcePos
& sourcePos
,
2977 const String16
& value
,
2978 const Vector
<StringPool::entry_style_span
>* style
,
2980 const bool overwrite
)
2982 Item
item(sourcePos
, false, value
, style
);
2984 if (mType
== TYPE_BAG
) {
2985 const Item
& item(mBag
.valueAt(0));
2986 sourcePos
.error("Resource entry %s is already defined as a bag.\n"
2987 "%s:%d: Originally defined here.\n",
2988 String8(mName
).string(),
2989 item
.sourcePos
.file
.string(), item
.sourcePos
.line
);
2990 return UNKNOWN_ERROR
;
2992 if ( (mType
!= TYPE_UNKNOWN
) && (overwrite
== false) ) {
2993 sourcePos
.error("Resource entry %s is already defined.\n"
2994 "%s:%d: Originally defined here.\n",
2995 String8(mName
).string(),
2996 mItem
.sourcePos
.file
.string(), mItem
.sourcePos
.line
);
2997 return UNKNOWN_ERROR
;
3002 mItemFormat
= format
;
3006 status_t
ResourceTable::Entry::addToBag(const SourcePos
& sourcePos
,
3007 const String16
& key
, const String16
& value
,
3008 const Vector
<StringPool::entry_style_span
>* style
,
3009 bool replace
, bool isId
, int32_t format
)
3011 status_t err
= makeItABag(sourcePos
);
3012 if (err
!= NO_ERROR
) {
3016 Item
item(sourcePos
, isId
, value
, style
, format
);
3018 // XXX NOTE: there is an error if you try to have a bag with two keys,
3019 // one an attr and one an id, with the same name. Not something we
3020 // currently ever have to worry about.
3021 ssize_t origKey
= mBag
.indexOfKey(key
);
3024 const Item
& item(mBag
.valueAt(origKey
));
3025 sourcePos
.error("Resource entry %s already has bag item %s.\n"
3026 "%s:%d: Originally defined here.\n",
3027 String8(mName
).string(), String8(key
).string(),
3028 item
.sourcePos
.file
.string(), item
.sourcePos
.line
);
3029 return UNKNOWN_ERROR
;
3031 //printf("Replacing %s with %s\n",
3032 // String8(mBag.valueFor(key).value).string(), String8(value).string());
3033 mBag
.replaceValueFor(key
, item
);
3036 mBag
.add(key
, item
);
3040 status_t
ResourceTable::Entry::emptyBag(const SourcePos
& sourcePos
)
3042 status_t err
= makeItABag(sourcePos
);
3043 if (err
!= NO_ERROR
) {
3051 status_t
ResourceTable::Entry::generateAttributes(ResourceTable
* table
,
3052 const String16
& package
)
3054 const String16
attr16("attr");
3055 const String16
id16("id");
3056 const size_t N
= mBag
.size();
3057 for (size_t i
=0; i
<N
; i
++) {
3058 const String16
& key
= mBag
.keyAt(i
);
3059 const Item
& it
= mBag
.valueAt(i
);
3061 if (!table
->hasBagOrEntry(key
, &id16
, &package
)) {
3062 String16
value("false");
3063 status_t err
= table
->addEntry(SourcePos(String8("<generated>"), 0), package
,
3065 if (err
!= NO_ERROR
) {
3069 } else if (!table
->hasBagOrEntry(key
, &attr16
, &package
)) {
3072 // fprintf(stderr, "ERROR: Bag attribute '%s' has not been defined.\n",
3073 // String8(key).string());
3074 // const Item& item(mBag.valueAt(i));
3075 // fprintf(stderr, "Referenced from file %s line %d\n",
3076 // item.sourcePos.file.string(), item.sourcePos.line);
3077 // return UNKNOWN_ERROR;
3080 sprintf(numberStr
, "%d", ResTable_map::TYPE_ANY
);
3081 status_t err
= table
->addBag(SourcePos("<generated>", 0), package
,
3082 attr16
, key
, String16(""),
3084 String16(numberStr
), NULL
, NULL
);
3085 if (err
!= NO_ERROR
) {
3094 status_t
ResourceTable::Entry::assignResourceIds(ResourceTable
* table
,
3095 const String16
& package
)
3097 bool hasErrors
= false;
3099 if (mType
== TYPE_BAG
) {
3100 const char* errorMsg
;
3101 const String16
style16("style");
3102 const String16
attr16("attr");
3103 const String16
id16("id");
3105 if (mParent
.size() > 0) {
3106 mParentId
= table
->getResId(mParent
, &style16
, NULL
, &errorMsg
);
3107 if (mParentId
== 0) {
3108 mPos
.error("Error retrieving parent for item: %s '%s'.\n",
3109 errorMsg
, String8(mParent
).string());
3113 const size_t N
= mBag
.size();
3114 for (size_t i
=0; i
<N
; i
++) {
3115 const String16
& key
= mBag
.keyAt(i
);
3116 Item
& it
= mBag
.editValueAt(i
);
3117 it
.bagKeyId
= table
->getResId(key
,
3118 it
.isId
? &id16
: &attr16
, NULL
, &errorMsg
);
3119 //printf("Bag key of %s: #%08x\n", String8(key).string(), it.bagKeyId);
3120 if (it
.bagKeyId
== 0) {
3121 it
.sourcePos
.error("Error: %s: %s '%s'.\n", errorMsg
,
3122 String8(it
.isId
? id16
: attr16
).string(),
3123 String8(key
).string());
3128 return hasErrors
? UNKNOWN_ERROR
: NO_ERROR
;
3131 status_t
ResourceTable::Entry::prepareFlatten(StringPool
* strings
, ResourceTable
* table
)
3133 if (mType
== TYPE_ITEM
) {
3135 AccessorCookie
ac(it
.sourcePos
, String8(mName
), String8(it
.value
));
3136 if (!table
->stringToValue(&it
.parsedValue
, strings
,
3137 it
.value
, false, true, 0,
3138 &it
.style
, NULL
, &ac
, mItemFormat
)) {
3139 return UNKNOWN_ERROR
;
3141 } else if (mType
== TYPE_BAG
) {
3142 const size_t N
= mBag
.size();
3143 for (size_t i
=0; i
<N
; i
++) {
3144 const String16
& key
= mBag
.keyAt(i
);
3145 Item
& it
= mBag
.editValueAt(i
);
3146 AccessorCookie
ac(it
.sourcePos
, String8(key
), String8(it
.value
));
3147 if (!table
->stringToValue(&it
.parsedValue
, strings
,
3148 it
.value
, false, true, it
.bagKeyId
,
3149 &it
.style
, NULL
, &ac
, it
.format
)) {
3150 return UNKNOWN_ERROR
;
3154 mPos
.error("Error: entry %s is not a single item or a bag.\n",
3155 String8(mName
).string());
3156 return UNKNOWN_ERROR
;
3161 ssize_t
ResourceTable::Entry::flatten(Bundle
* bundle
, const sp
<AaptFile
>& data
, bool isPublic
)
3164 ResTable_entry header
;
3165 memset(&header
, 0, sizeof(header
));
3166 header
.size
= htods(sizeof(header
));
3167 const type ty
= this != NULL
? mType
: TYPE_ITEM
;
3169 if (ty
== TYPE_BAG
) {
3170 header
.flags
|= htods(header
.FLAG_COMPLEX
);
3173 header
.flags
|= htods(header
.FLAG_PUBLIC
);
3175 header
.key
.index
= htodl(mNameIndex
);
3177 if (ty
!= TYPE_BAG
) {
3178 status_t err
= data
->writeData(&header
, sizeof(header
));
3179 if (err
!= NO_ERROR
) {
3180 fprintf(stderr
, "ERROR: out of memory creating ResTable_entry\n");
3184 const Item
& it
= mItem
;
3186 memset(&par
, 0, sizeof(par
));
3187 par
.size
= htods(it
.parsedValue
.size
);
3188 par
.dataType
= it
.parsedValue
.dataType
;
3189 par
.res0
= it
.parsedValue
.res0
;
3190 par
.data
= htodl(it
.parsedValue
.data
);
3192 printf("Writing item (%s): type=%d, data=0x%x, res0=0x%x\n",
3193 String8(mName
).string(), it
.parsedValue
.dataType
,
3194 it
.parsedValue
.data
, par
.res0
);
3196 err
= data
->writeData(&par
, it
.parsedValue
.size
);
3197 if (err
!= NO_ERROR
) {
3198 fprintf(stderr
, "ERROR: out of memory creating Res_value\n");
3201 amt
+= it
.parsedValue
.size
;
3203 size_t N
= mBag
.size();
3205 // Create correct ordering of items.
3206 KeyedVector
<uint32_t, const Item
*> items
;
3207 for (i
=0; i
<N
; i
++) {
3208 const Item
& it
= mBag
.valueAt(i
);
3209 items
.add(it
.bagKeyId
, &it
);
3213 ResTable_map_entry mapHeader
;
3214 memcpy(&mapHeader
, &header
, sizeof(header
));
3215 mapHeader
.size
= htods(sizeof(mapHeader
));
3216 mapHeader
.parent
.ident
= htodl(mParentId
);
3217 mapHeader
.count
= htodl(N
);
3218 status_t err
= data
->writeData(&mapHeader
, sizeof(mapHeader
));
3219 if (err
!= NO_ERROR
) {
3220 fprintf(stderr
, "ERROR: out of memory creating ResTable_entry\n");
3224 for (i
=0; i
<N
; i
++) {
3225 const Item
& it
= *items
.valueAt(i
);
3227 map
.name
.ident
= htodl(it
.bagKeyId
);
3228 map
.value
.size
= htods(it
.parsedValue
.size
);
3229 map
.value
.dataType
= it
.parsedValue
.dataType
;
3230 map
.value
.res0
= it
.parsedValue
.res0
;
3231 map
.value
.data
= htodl(it
.parsedValue
.data
);
3232 err
= data
->writeData(&map
, sizeof(map
));
3233 if (err
!= NO_ERROR
) {
3234 fprintf(stderr
, "ERROR: out of memory creating Res_value\n");
3243 void ResourceTable::ConfigList::appendComment(const String16
& comment
,
3246 if (comment
.size() <= 0) {
3249 if (onlyIfEmpty
&& mComment
.size() > 0) {
3252 if (mComment
.size() > 0) {
3253 mComment
.append(String16("\n"));
3255 mComment
.append(comment
);
3258 void ResourceTable::ConfigList::appendTypeComment(const String16
& comment
)
3260 if (comment
.size() <= 0) {
3263 if (mTypeComment
.size() > 0) {
3264 mTypeComment
.append(String16("\n"));
3266 mTypeComment
.append(comment
);
3269 status_t
ResourceTable::Type::addPublic(const SourcePos
& sourcePos
,
3270 const String16
& name
,
3271 const uint32_t ident
)
3274 int32_t entryIdx
= Res_GETENTRY(ident
);
3276 sourcePos
.error("Public resource %s/%s has an invalid 0 identifier (0x%08x).\n",
3277 String8(mName
).string(), String8(name
).string(), ident
);
3278 return UNKNOWN_ERROR
;
3282 int32_t typeIdx
= Res_GETTYPE(ident
);
3285 if (mPublicIndex
> 0 && mPublicIndex
!= typeIdx
) {
3286 sourcePos
.error("Public resource %s/%s has conflicting type codes for its"
3287 " public identifiers (0x%x vs 0x%x).\n",
3288 String8(mName
).string(), String8(name
).string(),
3289 mPublicIndex
, typeIdx
);
3290 return UNKNOWN_ERROR
;
3292 mPublicIndex
= typeIdx
;
3295 if (mFirstPublicSourcePos
== NULL
) {
3296 mFirstPublicSourcePos
= new SourcePos(sourcePos
);
3299 if (mPublic
.indexOfKey(name
) < 0) {
3300 mPublic
.add(name
, Public(sourcePos
, String16(), ident
));
3302 Public
& p
= mPublic
.editValueFor(name
);
3303 if (p
.ident
!= ident
) {
3304 sourcePos
.error("Public resource %s/%s has conflicting public identifiers"
3305 " (0x%08x vs 0x%08x).\n"
3306 "%s:%d: Originally defined here.\n",
3307 String8(mName
).string(), String8(name
).string(), p
.ident
, ident
,
3308 p
.sourcePos
.file
.string(), p
.sourcePos
.line
);
3309 return UNKNOWN_ERROR
;
3316 void ResourceTable::Type::canAddEntry(const String16
& name
)
3318 mCanAddEntries
.add(name
);
3321 sp
<ResourceTable::Entry
> ResourceTable::Type::getEntry(const String16
& entry
,
3322 const SourcePos
& sourcePos
,
3323 const ResTable_config
* config
,
3326 bool autoAddOverlay
)
3329 sp
<ConfigList
> c
= mConfigs
.valueFor(entry
);
3331 if (overlay
&& !autoAddOverlay
&& mCanAddEntries
.indexOf(entry
) < 0) {
3332 sourcePos
.error("Resource at %s appears in overlay but not"
3333 " in the base package; use <add-resource> to add.\n",
3334 String8(entry
).string());
3337 c
= new ConfigList(entry
, sourcePos
);
3338 mConfigs
.add(entry
, c
);
3339 pos
= (int)mOrderedConfigs
.size();
3340 mOrderedConfigs
.add(c
);
3342 c
->setEntryIndex(pos
);
3346 ConfigDescription cdesc
;
3347 if (config
) cdesc
= *config
;
3349 sp
<Entry
> e
= c
->getEntries().valueFor(cdesc
);
3351 if (config
!= NULL
) {
3352 NOISY(printf("New entry at %s:%d: imsi:%d/%d lang:%c%c cnt:%c%c "
3353 "orien:%d touch:%d density:%d key:%d inp:%d nav:%d w:%d h:%d\n",
3354 sourcePos
.file
.string(), sourcePos
.line
,
3355 config
->mcc
, config
->mnc
,
3356 config
->language
[0] ? config
->language
[0] : '-',
3357 config
->language
[1] ? config
->language
[1] : '-',
3358 config
->country
[0] ? config
->country
[0] : '-',
3359 config
->country
[1] ? config
->country
[1] : '-',
3360 config
->orientation
,
3361 config
->touchscreen
,
3366 config
->screenWidth
,
3367 config
->screenHeight
));
3369 NOISY(printf("New entry at %s:%d: NULL config\n",
3370 sourcePos
.file
.string(), sourcePos
.line
));
3372 e
= new Entry(entry
, sourcePos
);
3373 c
->addEntry(cdesc
, e
);
3377 for (pos=0; pos<(int)mOrderedConfigs.size(); pos++) {
3378 if (mOrderedConfigs[pos] == c) {
3382 if (pos >= (int)mOrderedConfigs.size()) {
3383 sourcePos.error("Internal error: config not found in mOrderedConfigs when adding entry");
3387 e->setEntryIndex(pos);
3392 mUniqueConfigs
.add(cdesc
);
3397 status_t
ResourceTable::Type::applyPublicEntryOrder()
3399 size_t N
= mOrderedConfigs
.size();
3400 Vector
<sp
<ConfigList
> > origOrder(mOrderedConfigs
);
3401 bool hasError
= false;
3404 for (i
=0; i
<N
; i
++) {
3405 mOrderedConfigs
.replaceAt(NULL
, i
);
3408 const size_t NP
= mPublic
.size();
3409 //printf("Ordering %d configs from %d public defs\n", N, NP);
3411 for (j
=0; j
<NP
; j
++) {
3412 const String16
& name
= mPublic
.keyAt(j
);
3413 const Public
& p
= mPublic
.valueAt(j
);
3414 int32_t idx
= Res_GETENTRY(p
.ident
);
3415 //printf("Looking for entry \"%s\"/\"%s\" (0x%08x) in %d...\n",
3416 // String8(mName).string(), String8(name).string(), p.ident, N);
3418 for (i
=0; i
<N
; i
++) {
3419 sp
<ConfigList
> e
= origOrder
.itemAt(i
);
3420 //printf("#%d: \"%s\"\n", i, String8(e->getName()).string());
3421 if (e
->getName() == name
) {
3422 if (idx
>= (int32_t)mOrderedConfigs
.size()) {
3423 p
.sourcePos
.error("Public entry identifier 0x%x entry index "
3424 "is larger than available symbols (index %d, total symbols %d).\n",
3425 p
.ident
, idx
, mOrderedConfigs
.size());
3427 } else if (mOrderedConfigs
.itemAt(idx
) == NULL
) {
3429 e
->setPublicSourcePos(p
.sourcePos
);
3430 mOrderedConfigs
.replaceAt(e
, idx
);
3431 origOrder
.removeAt(i
);
3436 sp
<ConfigList
> oe
= mOrderedConfigs
.itemAt(idx
);
3438 p
.sourcePos
.error("Multiple entry names declared for public entry"
3439 " identifier 0x%x in type %s (%s vs %s).\n"
3440 "%s:%d: Originally defined here.",
3441 idx
+1, String8(mName
).string(),
3442 String8(oe
->getName()).string(),
3443 String8(name
).string(),
3444 oe
->getPublicSourcePos().file
.string(),
3445 oe
->getPublicSourcePos().line
);
3452 p
.sourcePos
.error("Public symbol %s/%s declared here is not defined.",
3453 String8(mName
).string(), String8(name
).string());
3458 //printf("Copying back in %d non-public configs, have %d\n", N, origOrder.size());
3460 if (N
!= origOrder
.size()) {
3461 printf("Internal error: remaining private symbol count mismatch\n");
3462 N
= origOrder
.size();
3466 for (i
=0; i
<N
; i
++) {
3467 sp
<ConfigList
> e
= origOrder
.itemAt(i
);
3468 // There will always be enough room for the remaining entries.
3469 while (mOrderedConfigs
.itemAt(j
) != NULL
) {
3472 mOrderedConfigs
.replaceAt(e
, j
);
3476 return hasError
? UNKNOWN_ERROR
: NO_ERROR
;
3479 ResourceTable::Package::Package(const String16
& name
, ssize_t includedId
)
3480 : mName(name
), mIncludedId(includedId
),
3481 mTypeStringsMapping(0xffffffff),
3482 mKeyStringsMapping(0xffffffff)
3486 sp
<ResourceTable::Type
> ResourceTable::Package::getType(const String16
& type
,
3487 const SourcePos
& sourcePos
,
3490 sp
<Type
> t
= mTypes
.valueFor(type
);
3492 t
= new Type(type
, sourcePos
);
3493 mTypes
.add(type
, t
);
3494 mOrderedTypes
.add(t
);
3496 // For some reason the type's index is set to one plus the index
3497 // in the mOrderedTypes list, rather than just the index.
3498 t
->setIndex(mOrderedTypes
.size());
3504 status_t
ResourceTable::Package::setTypeStrings(const sp
<AaptFile
>& data
)
3506 mTypeStringsData
= data
;
3507 status_t err
= setStrings(data
, &mTypeStrings
, &mTypeStringsMapping
);
3508 if (err
!= NO_ERROR
) {
3509 fprintf(stderr
, "ERROR: Type string data is corrupt!\n");
3514 status_t
ResourceTable::Package::setKeyStrings(const sp
<AaptFile
>& data
)
3516 mKeyStringsData
= data
;
3517 status_t err
= setStrings(data
, &mKeyStrings
, &mKeyStringsMapping
);
3518 if (err
!= NO_ERROR
) {
3519 fprintf(stderr
, "ERROR: Key string data is corrupt!\n");
3524 status_t
ResourceTable::Package::setStrings(const sp
<AaptFile
>& data
,
3525 ResStringPool
* strings
,
3526 DefaultKeyedVector
<String16
, uint32_t>* mappings
)
3528 if (data
->getData() == NULL
) {
3529 return UNKNOWN_ERROR
;
3532 NOISY(aout
<< "Setting restable string pool: "
3533 << HexDump(data
->getData(), data
->getSize()) << endl
);
3535 status_t err
= strings
->setTo(data
->getData(), data
->getSize());
3536 if (err
== NO_ERROR
) {
3537 const size_t N
= strings
->size();
3538 for (size_t i
=0; i
<N
; i
++) {
3540 mappings
->add(String16(strings
->stringAt(i
, &len
)), i
);
3546 status_t
ResourceTable::Package::applyPublicTypeOrder()
3548 size_t N
= mOrderedTypes
.size();
3549 Vector
<sp
<Type
> > origOrder(mOrderedTypes
);
3552 for (i
=0; i
<N
; i
++) {
3553 mOrderedTypes
.replaceAt(NULL
, i
);
3556 for (i
=0; i
<N
; i
++) {
3557 sp
<Type
> t
= origOrder
.itemAt(i
);
3558 int32_t idx
= t
->getPublicIndex();
3561 while (idx
>= (int32_t)mOrderedTypes
.size()) {
3562 mOrderedTypes
.add();
3564 if (mOrderedTypes
.itemAt(idx
) != NULL
) {
3565 sp
<Type
> ot
= mOrderedTypes
.itemAt(idx
);
3566 t
->getFirstPublicSourcePos().error("Multiple type names declared for public type"
3567 " identifier 0x%x (%s vs %s).\n"
3568 "%s:%d: Originally defined here.",
3569 idx
, String8(ot
->getName()).string(),
3570 String8(t
->getName()).string(),
3571 ot
->getFirstPublicSourcePos().file
.string(),
3572 ot
->getFirstPublicSourcePos().line
);
3573 return UNKNOWN_ERROR
;
3575 mOrderedTypes
.replaceAt(t
, idx
);
3576 origOrder
.removeAt(i
);
3583 for (i
=0; i
<N
; i
++) {
3584 sp
<Type
> t
= origOrder
.itemAt(i
);
3585 // There will always be enough room for the remaining types.
3586 while (mOrderedTypes
.itemAt(j
) != NULL
) {
3589 mOrderedTypes
.replaceAt(t
, j
);
3595 sp
<ResourceTable::Package
> ResourceTable::getPackage(const String16
& package
)
3597 sp
<Package
> p
= mPackages
.valueFor(package
);
3599 if (mIsAppPackage
) {
3600 if (mHaveAppPackage
) {
3601 fprintf(stderr
, "Adding multiple application package resources; only one is allowed.\n"
3602 "Use -x to create extended resources.\n");
3605 mHaveAppPackage
= true;
3606 p
= new Package(package
, 127);
3608 p
= new Package(package
, mNextPackageId
);
3610 //printf("*** NEW PACKAGE: \"%s\" id=%d\n",
3611 // String8(package).string(), p->getAssignedId());
3612 mPackages
.add(package
, p
);
3613 mOrderedPackages
.add(p
);
3619 sp
<ResourceTable::Type
> ResourceTable::getType(const String16
& package
,
3620 const String16
& type
,
3621 const SourcePos
& sourcePos
,
3624 sp
<Package
> p
= getPackage(package
);
3628 return p
->getType(type
, sourcePos
, doSetIndex
);
3631 sp
<ResourceTable::Entry
> ResourceTable::getEntry(const String16
& package
,
3632 const String16
& type
,
3633 const String16
& name
,
3634 const SourcePos
& sourcePos
,
3636 const ResTable_config
* config
,
3639 sp
<Type
> t
= getType(package
, type
, sourcePos
, doSetIndex
);
3643 return t
->getEntry(name
, sourcePos
, config
, doSetIndex
, overlay
, mBundle
->getAutoAddOverlay());
3646 sp
<const ResourceTable::Entry
> ResourceTable::getEntry(uint32_t resID
,
3647 const ResTable_config
* config
) const
3649 int pid
= Res_GETPACKAGE(resID
)+1;
3650 const size_t N
= mOrderedPackages
.size();
3653 for (i
=0; i
<N
; i
++) {
3654 sp
<Package
> check
= mOrderedPackages
[i
];
3655 if (check
->getAssignedId() == pid
) {
3662 fprintf(stderr
, "warning: Package not found for resource #%08x\n", resID
);
3666 int tid
= Res_GETTYPE(resID
);
3667 if (tid
< 0 || tid
>= (int)p
->getOrderedTypes().size()) {
3668 fprintf(stderr
, "warning: Type not found for resource #%08x\n", resID
);
3671 sp
<Type
> t
= p
->getOrderedTypes()[tid
];
3673 int eid
= Res_GETENTRY(resID
);
3674 if (eid
< 0 || eid
>= (int)t
->getOrderedConfigs().size()) {
3675 fprintf(stderr
, "warning: Entry not found for resource #%08x\n", resID
);
3679 sp
<ConfigList
> c
= t
->getOrderedConfigs()[eid
];
3681 fprintf(stderr
, "warning: Entry not found for resource #%08x\n", resID
);
3685 ConfigDescription cdesc
;
3686 if (config
) cdesc
= *config
;
3687 sp
<Entry
> e
= c
->getEntries().valueFor(cdesc
);
3689 fprintf(stderr
, "warning: Entry configuration not found for resource #%08x\n", resID
);
3696 const ResourceTable::Item
* ResourceTable::getItem(uint32_t resID
, uint32_t attrID
) const
3698 sp
<const Entry
> e
= getEntry(resID
);
3703 const size_t N
= e
->getBag().size();
3704 for (size_t i
=0; i
<N
; i
++) {
3705 const Item
& it
= e
->getBag().valueAt(i
);
3706 if (it
.bagKeyId
== 0) {
3707 fprintf(stderr
, "warning: ID not yet assigned to '%s' in bag '%s'\n",
3708 String8(e
->getName()).string(),
3709 String8(e
->getBag().keyAt(i
)).string());
3711 if (it
.bagKeyId
== attrID
) {
3719 bool ResourceTable::getItemValue(
3720 uint32_t resID
, uint32_t attrID
, Res_value
* outValue
)
3722 const Item
* item
= getItem(resID
, attrID
);
3726 if (item
->evaluating
) {
3727 sp
<const Entry
> e
= getEntry(resID
);
3728 const size_t N
= e
->getBag().size();
3730 for (i
=0; i
<N
; i
++) {
3731 if (&e
->getBag().valueAt(i
) == item
) {
3735 fprintf(stderr
, "warning: Circular reference detected in key '%s' of bag '%s'\n",
3736 String8(e
->getName()).string(),
3737 String8(e
->getBag().keyAt(i
)).string());
3740 item
->evaluating
= true;
3741 res
= stringToValue(outValue
, NULL
, item
->value
, false, false, item
->bagKeyId
);
3744 printf("getItemValue of #%08x[#%08x] (%s): type=#%08x, data=#%08x\n",
3745 resID
, attrID
, String8(getEntry(resID
)->getName()).string(),
3746 outValue
->dataType
, outValue
->data
);
3748 printf("getItemValue of #%08x[#%08x]: failed\n",
3752 item
->evaluating
= false;