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
,
577 const bool overwrite
,
578 ResourceTable
* outTable
)
581 const String16
item16("item");
584 Vector
<StringPool::entry_style_span
> spans
;
585 err
= parseStyledString(bundle
, in
->getPrintableSource().string(),
586 block
, item16
, &str
, &spans
,
588 if (err
!= NO_ERROR
) {
592 NOISY(printf("Adding resource bag entry l=%c%c c=%c%c orien=%d d=%d "
593 " pid=%s, bag=%s, id=%s: %s\n",
594 config
.language
[0], config
.language
[1],
595 config
.country
[0], config
.country
[1],
596 config
.orientation
, config
.density
,
597 String8(parentIdent
).string(),
598 String8(ident
).string(),
599 String8(itemIdent
).string(),
600 String8(str
).string()));
602 err
= outTable
->addBag(SourcePos(in
->getPrintableSource(), block
->getLineNumber()),
603 myPackage
, curType
, ident
, parentIdent
, itemIdent
, str
,
604 &spans
, &config
, overwrite
, false, curFormat
);
609 status_t
parseAndAddEntry(Bundle
* bundle
,
610 const sp
<AaptFile
>& in
,
612 const ResTable_config
& config
,
613 const String16
& myPackage
,
614 const String16
& curType
,
615 const String16
& ident
,
616 const String16
& curTag
,
620 const bool overwrite
,
621 ResourceTable
* outTable
)
626 Vector
<StringPool::entry_style_span
> spans
;
627 err
= parseStyledString(bundle
, in
->getPrintableSource().string(), block
,
628 curTag
, &str
, curIsStyled
? &spans
: NULL
,
631 if (err
< NO_ERROR
) {
635 NOISY(printf("Adding resource entry l=%c%c c=%c%c orien=%d d=%d id=%s: %s\n",
636 config
.language
[0], config
.language
[1],
637 config
.country
[0], config
.country
[1],
638 config
.orientation
, config
.density
,
639 String8(ident
).string(), String8(str
).string()));
641 err
= outTable
->addEntry(SourcePos(in
->getPrintableSource(), block
->getLineNumber()),
642 myPackage
, curType
, ident
, str
, &spans
, &config
,
643 false, curFormat
, overwrite
);
648 status_t
compileResourceFile(Bundle
* bundle
,
649 const sp
<AaptAssets
>& assets
,
650 const sp
<AaptFile
>& in
,
651 const ResTable_config
& defParams
,
652 const bool overwrite
,
653 ResourceTable
* outTable
)
656 status_t err
= parseXMLResource(in
, &block
, false, true);
657 if (err
!= NO_ERROR
) {
662 const String16
resources16("resources");
664 // Identifier declaration tags.
665 const String16
declare_styleable16("declare-styleable");
666 const String16
attr16("attr");
668 // Data creation organizational tags.
669 const String16
string16("string");
670 const String16
drawable16("drawable");
671 const String16
color16("color");
672 const String16
bool16("bool");
673 const String16
integer16("integer");
674 const String16
dimen16("dimen");
675 const String16
fraction16("fraction");
676 const String16
style16("style");
677 const String16
plurals16("plurals");
678 const String16
array16("array");
679 const String16
string_array16("string-array");
680 const String16
integer_array16("integer-array");
681 const String16
public16("public");
682 const String16
public_padding16("public-padding");
683 const String16
private_symbols16("private-symbols");
684 const String16
add_resource16("add-resource");
685 const String16
skip16("skip");
686 const String16
eat_comment16("eat-comment");
688 // Data creation tags.
689 const String16
bag16("bag");
690 const String16
item16("item");
692 // Attribute type constants.
693 const String16
enum16("enum");
696 const String16
other16("other");
697 const String16
quantityOther16("^other");
698 const String16
zero16("zero");
699 const String16
quantityZero16("^zero");
700 const String16
one16("one");
701 const String16
quantityOne16("^one");
702 const String16
two16("two");
703 const String16
quantityTwo16("^two");
704 const String16
few16("few");
705 const String16
quantityFew16("^few");
706 const String16
many16("many");
707 const String16
quantityMany16("^many");
709 // useful attribute names and special values
710 const String16
name16("name");
711 const String16
translatable16("translatable");
712 const String16
false16("false");
714 const String16
myPackage(assets
->getPackage());
716 bool hasErrors
= false;
718 DefaultKeyedVector
<String16
, uint32_t> nextPublicId(0);
720 ResXMLTree::event_code_t code
;
723 } while (code
== ResXMLTree::START_NAMESPACE
);
726 if (code
!= ResXMLTree::START_TAG
) {
727 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
728 "No start tag found\n");
729 return UNKNOWN_ERROR
;
731 if (strcmp16(block
.getElementName(&len
), resources16
.string()) != 0) {
732 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
733 "Invalid start tag %s\n", String8(block
.getElementName(&len
)).string());
734 return UNKNOWN_ERROR
;
737 ResTable_config
curParams(defParams
);
739 ResTable_config
pseudoParams(curParams
);
740 pseudoParams
.language
[0] = 'z';
741 pseudoParams
.language
[1] = 'z';
742 pseudoParams
.country
[0] = 'Z';
743 pseudoParams
.country
[1] = 'Z';
745 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
746 if (code
== ResXMLTree::START_TAG
) {
747 const String16
* curTag
= NULL
;
749 int32_t curFormat
= ResTable_map::TYPE_ANY
;
750 bool curIsBag
= false;
751 bool curIsBagReplaceOnOverwrite
= false;
752 bool curIsStyled
= false;
753 bool curIsPseudolocalizable
= false;
754 bool localHasErrors
= false;
756 if (strcmp16(block
.getElementName(&len
), skip16
.string()) == 0) {
757 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
758 && code
!= ResXMLTree::BAD_DOCUMENT
) {
759 if (code
== ResXMLTree::END_TAG
) {
760 if (strcmp16(block
.getElementName(&len
), skip16
.string()) == 0) {
767 } else if (strcmp16(block
.getElementName(&len
), eat_comment16
.string()) == 0) {
768 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
769 && code
!= ResXMLTree::BAD_DOCUMENT
) {
770 if (code
== ResXMLTree::END_TAG
) {
771 if (strcmp16(block
.getElementName(&len
), eat_comment16
.string()) == 0) {
778 } else if (strcmp16(block
.getElementName(&len
), public16
.string()) == 0) {
779 SourcePos
srcPos(in
->getPrintableSource(), block
.getLineNumber());
782 ssize_t typeIdx
= block
.indexOfAttribute(NULL
, "type");
784 srcPos
.error("A 'type' attribute is required for <public>\n");
785 hasErrors
= localHasErrors
= true;
787 type
= String16(block
.getAttributeStringValue(typeIdx
, &len
));
790 ssize_t nameIdx
= block
.indexOfAttribute(NULL
, "name");
792 srcPos
.error("A 'name' attribute is required for <public>\n");
793 hasErrors
= localHasErrors
= true;
795 name
= String16(block
.getAttributeStringValue(nameIdx
, &len
));
798 ssize_t identIdx
= block
.indexOfAttribute(NULL
, "id");
800 const char16_t* identStr
= block
.getAttributeStringValue(identIdx
, &len
);
801 Res_value identValue
;
802 if (!ResTable::stringToInt(identStr
, len
, &identValue
)) {
803 srcPos
.error("Given 'id' attribute is not an integer: %s\n",
804 String8(block
.getAttributeStringValue(identIdx
, &len
)).string());
805 hasErrors
= localHasErrors
= true;
807 ident
= identValue
.data
;
808 nextPublicId
.replaceValueFor(type
, ident
+1);
810 } else if (nextPublicId
.indexOfKey(type
) < 0) {
811 srcPos
.error("No 'id' attribute supplied <public>,"
812 " and no previous id defined in this file.\n");
813 hasErrors
= localHasErrors
= true;
814 } else if (!localHasErrors
) {
815 ident
= nextPublicId
.valueFor(type
);
816 nextPublicId
.replaceValueFor(type
, ident
+1);
819 if (!localHasErrors
) {
820 err
= outTable
->addPublic(srcPos
, myPackage
, type
, name
, ident
);
821 if (err
< NO_ERROR
) {
822 hasErrors
= localHasErrors
= true;
825 if (!localHasErrors
) {
826 sp
<AaptSymbols
> symbols
= assets
->getSymbolsFor(String8("R"));
827 if (symbols
!= NULL
) {
828 symbols
= symbols
->addNestedSymbol(String8(type
), srcPos
);
830 if (symbols
!= NULL
) {
831 symbols
->makeSymbolPublic(String8(name
), srcPos
);
833 block
.getComment(&len
) ? block
.getComment(&len
) : nulStr
);
834 symbols
->appendComment(String8(name
), comment
, srcPos
);
836 srcPos
.error("Unable to create symbols!\n");
837 hasErrors
= localHasErrors
= true;
841 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
842 if (code
== ResXMLTree::END_TAG
) {
843 if (strcmp16(block
.getElementName(&len
), public16
.string()) == 0) {
850 } else if (strcmp16(block
.getElementName(&len
), public_padding16
.string()) == 0) {
851 SourcePos
srcPos(in
->getPrintableSource(), block
.getLineNumber());
854 ssize_t typeIdx
= block
.indexOfAttribute(NULL
, "type");
856 srcPos
.error("A 'type' attribute is required for <public-padding>\n");
857 hasErrors
= localHasErrors
= true;
859 type
= String16(block
.getAttributeStringValue(typeIdx
, &len
));
862 ssize_t nameIdx
= block
.indexOfAttribute(NULL
, "name");
864 srcPos
.error("A 'name' attribute is required for <public-padding>\n");
865 hasErrors
= localHasErrors
= true;
867 name
= String16(block
.getAttributeStringValue(nameIdx
, &len
));
870 ssize_t startIdx
= block
.indexOfAttribute(NULL
, "start");
872 const char16_t* startStr
= block
.getAttributeStringValue(startIdx
, &len
);
873 Res_value startValue
;
874 if (!ResTable::stringToInt(startStr
, len
, &startValue
)) {
875 srcPos
.error("Given 'start' attribute is not an integer: %s\n",
876 String8(block
.getAttributeStringValue(startIdx
, &len
)).string());
877 hasErrors
= localHasErrors
= true;
879 start
= startValue
.data
;
881 } else if (nextPublicId
.indexOfKey(type
) < 0) {
882 srcPos
.error("No 'start' attribute supplied <public-padding>,"
883 " and no previous id defined in this file.\n");
884 hasErrors
= localHasErrors
= true;
885 } else if (!localHasErrors
) {
886 start
= nextPublicId
.valueFor(type
);
890 ssize_t endIdx
= block
.indexOfAttribute(NULL
, "end");
892 const char16_t* endStr
= block
.getAttributeStringValue(endIdx
, &len
);
894 if (!ResTable::stringToInt(endStr
, len
, &endValue
)) {
895 srcPos
.error("Given 'end' attribute is not an integer: %s\n",
896 String8(block
.getAttributeStringValue(endIdx
, &len
)).string());
897 hasErrors
= localHasErrors
= true;
902 srcPos
.error("No 'end' attribute supplied <public-padding>\n");
903 hasErrors
= localHasErrors
= true;
907 nextPublicId
.replaceValueFor(type
, end
+1);
909 srcPos
.error("Padding start '%ul' is after end '%ul'\n",
911 hasErrors
= localHasErrors
= true;
915 block
.getComment(&len
) ? block
.getComment(&len
) : nulStr
);
916 for (uint32_t curIdent
=start
; curIdent
<=end
; curIdent
++) {
917 if (localHasErrors
) {
920 String16
curName(name
);
922 sprintf(buf
, "%d", (int)(end
-curIdent
+1));
923 curName
.append(String16(buf
));
925 err
= outTable
->addEntry(srcPos
, myPackage
, type
, curName
,
926 String16("padding"), NULL
, &curParams
, false,
927 ResTable_map::TYPE_STRING
, overwrite
);
928 if (err
< NO_ERROR
) {
929 hasErrors
= localHasErrors
= true;
932 err
= outTable
->addPublic(srcPos
, myPackage
, type
,
934 if (err
< NO_ERROR
) {
935 hasErrors
= localHasErrors
= true;
938 sp
<AaptSymbols
> symbols
= assets
->getSymbolsFor(String8("R"));
939 if (symbols
!= NULL
) {
940 symbols
= symbols
->addNestedSymbol(String8(type
), srcPos
);
942 if (symbols
!= NULL
) {
943 symbols
->makeSymbolPublic(String8(curName
), srcPos
);
944 symbols
->appendComment(String8(curName
), comment
, srcPos
);
946 srcPos
.error("Unable to create symbols!\n");
947 hasErrors
= localHasErrors
= true;
951 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
952 if (code
== ResXMLTree::END_TAG
) {
953 if (strcmp16(block
.getElementName(&len
), public_padding16
.string()) == 0) {
960 } else if (strcmp16(block
.getElementName(&len
), private_symbols16
.string()) == 0) {
962 ssize_t pkgIdx
= block
.indexOfAttribute(NULL
, "package");
964 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
965 "A 'package' attribute is required for <private-symbols>\n");
966 hasErrors
= localHasErrors
= true;
968 pkg
= String16(block
.getAttributeStringValue(pkgIdx
, &len
));
969 if (!localHasErrors
) {
970 assets
->setSymbolsPrivatePackage(String8(pkg
));
973 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
974 if (code
== ResXMLTree::END_TAG
) {
975 if (strcmp16(block
.getElementName(&len
), private_symbols16
.string()) == 0) {
982 } else if (strcmp16(block
.getElementName(&len
), add_resource16
.string()) == 0) {
983 SourcePos
srcPos(in
->getPrintableSource(), block
.getLineNumber());
986 ssize_t typeIdx
= block
.indexOfAttribute(NULL
, "type");
988 srcPos
.error("A 'type' attribute is required for <add-resource>\n");
989 hasErrors
= localHasErrors
= true;
991 typeName
= String16(block
.getAttributeStringValue(typeIdx
, &len
));
994 ssize_t nameIdx
= block
.indexOfAttribute(NULL
, "name");
996 srcPos
.error("A 'name' attribute is required for <add-resource>\n");
997 hasErrors
= localHasErrors
= true;
999 name
= String16(block
.getAttributeStringValue(nameIdx
, &len
));
1001 outTable
->canAddEntry(srcPos
, myPackage
, typeName
, name
);
1003 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
1004 if (code
== ResXMLTree::END_TAG
) {
1005 if (strcmp16(block
.getElementName(&len
), add_resource16
.string()) == 0) {
1012 } else if (strcmp16(block
.getElementName(&len
), declare_styleable16
.string()) == 0) {
1013 SourcePos
srcPos(in
->getPrintableSource(), block
.getLineNumber());
1016 ssize_t identIdx
= block
.indexOfAttribute(NULL
, "name");
1018 srcPos
.error("A 'name' attribute is required for <declare-styleable>\n");
1019 hasErrors
= localHasErrors
= true;
1021 ident
= String16(block
.getAttributeStringValue(identIdx
, &len
));
1023 sp
<AaptSymbols
> symbols
= assets
->getSymbolsFor(String8("R"));
1024 if (!localHasErrors
) {
1025 if (symbols
!= NULL
) {
1026 symbols
= symbols
->addNestedSymbol(String8("styleable"), srcPos
);
1028 sp
<AaptSymbols
> styleSymbols
= symbols
;
1029 if (symbols
!= NULL
) {
1030 symbols
= symbols
->addNestedSymbol(String8(ident
), srcPos
);
1032 if (symbols
== NULL
) {
1033 srcPos
.error("Unable to create symbols!\n");
1034 return UNKNOWN_ERROR
;
1038 block
.getComment(&len
) ? block
.getComment(&len
) : nulStr
);
1039 styleSymbols
->appendComment(String8(ident
), comment
, srcPos
);
1044 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
1045 if (code
== ResXMLTree::START_TAG
) {
1046 if (strcmp16(block
.getElementName(&len
), skip16
.string()) == 0) {
1047 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
1048 && code
!= ResXMLTree::BAD_DOCUMENT
) {
1049 if (code
== ResXMLTree::END_TAG
) {
1050 if (strcmp16(block
.getElementName(&len
), skip16
.string()) == 0) {
1056 } else if (strcmp16(block
.getElementName(&len
), eat_comment16
.string()) == 0) {
1057 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
1058 && code
!= ResXMLTree::BAD_DOCUMENT
) {
1059 if (code
== ResXMLTree::END_TAG
) {
1060 if (strcmp16(block
.getElementName(&len
), eat_comment16
.string()) == 0) {
1066 } else if (strcmp16(block
.getElementName(&len
), attr16
.string()) != 0) {
1067 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1068 "Tag <%s> can not appear inside <declare-styleable>, only <attr>\n",
1069 String8(block
.getElementName(&len
)).string());
1070 return UNKNOWN_ERROR
;
1074 block
.getComment(&len
) ? block
.getComment(&len
) : nulStr
);
1076 err
= compileAttribute(in
, block
, myPackage
, outTable
, &itemIdent
, true);
1077 if (err
!= NO_ERROR
) {
1078 hasErrors
= localHasErrors
= true;
1081 if (symbols
!= NULL
) {
1082 SourcePos
srcPos(String8(in
->getPrintableSource()), block
.getLineNumber());
1083 symbols
->addSymbol(String8(itemIdent
), 0, srcPos
);
1084 symbols
->appendComment(String8(itemIdent
), comment
, srcPos
);
1085 //printf("Attribute %s comment: %s\n", String8(itemIdent).string(),
1086 // String8(comment).string());
1088 } else if (code
== ResXMLTree::END_TAG
) {
1089 if (strcmp16(block
.getElementName(&len
), declare_styleable16
.string()) == 0) {
1093 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1094 "Found tag </%s> where </attr> is expected\n",
1095 String8(block
.getElementName(&len
)).string());
1096 return UNKNOWN_ERROR
;
1101 } else if (strcmp16(block
.getElementName(&len
), attr16
.string()) == 0) {
1102 err
= compileAttribute(in
, block
, myPackage
, outTable
, NULL
);
1103 if (err
!= NO_ERROR
) {
1108 } else if (strcmp16(block
.getElementName(&len
), item16
.string()) == 0) {
1110 ssize_t attri
= block
.indexOfAttribute(NULL
, "type");
1112 curType
= String16(block
.getAttributeStringValue(attri
, &len
));
1113 ssize_t formatIdx
= block
.indexOfAttribute(NULL
, "format");
1114 if (formatIdx
>= 0) {
1115 String16 formatStr
= String16(block
.getAttributeStringValue(
1117 curFormat
= parse_flags(formatStr
.string(), formatStr
.size(),
1119 if (curFormat
== 0) {
1120 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1121 "Tag <item> 'format' attribute value \"%s\" not valid\n",
1122 String8(formatStr
).string());
1123 hasErrors
= localHasErrors
= true;
1127 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1128 "A 'type' attribute is required for <item>\n");
1129 hasErrors
= localHasErrors
= true;
1132 } else if (strcmp16(block
.getElementName(&len
), string16
.string()) == 0) {
1133 // Note the existence and locale of every string we process
1135 curParams
.getLocale(rawLocale
);
1136 String8
locale(rawLocale
);
1138 String16 translatable
;
1140 size_t n
= block
.getAttributeCount();
1141 for (size_t i
= 0; i
< n
; i
++) {
1143 const uint16_t* attr
= block
.getAttributeName(i
, &length
);
1144 if (strcmp16(attr
, name16
.string()) == 0) {
1145 name
.setTo(block
.getAttributeStringValue(i
, &length
));
1146 } else if (strcmp16(attr
, translatable16
.string()) == 0) {
1147 translatable
.setTo(block
.getAttributeStringValue(i
, &length
));
1151 if (name
.size() > 0) {
1152 if (translatable
== false16
) {
1153 // Untranslatable strings must only exist in the default [empty] locale
1154 if (locale
.size() > 0) {
1155 fprintf(stderr
, "aapt: warning: string '%s' in %s marked untranslatable but exists"
1156 " in locale '%s'\n", String8(name
).string(),
1157 bundle
->getResourceSourceDirs()[0],
1159 // hasErrors = localHasErrors = true;
1161 // Intentionally empty block:
1163 // Don't add untranslatable strings to the localization table; that
1164 // way if we later see localizations of them, they'll be flagged as
1165 // having no default translation.
1168 outTable
->addLocalization(name
, locale
);
1174 curFormat
= ResTable_map::TYPE_REFERENCE
|ResTable_map::TYPE_STRING
;
1176 curIsPseudolocalizable
= true;
1177 } else if (strcmp16(block
.getElementName(&len
), drawable16
.string()) == 0) {
1178 curTag
= &drawable16
;
1179 curType
= drawable16
;
1180 curFormat
= ResTable_map::TYPE_REFERENCE
|ResTable_map::TYPE_COLOR
;
1181 } else if (strcmp16(block
.getElementName(&len
), color16
.string()) == 0) {
1184 curFormat
= ResTable_map::TYPE_REFERENCE
|ResTable_map::TYPE_COLOR
;
1185 } else if (strcmp16(block
.getElementName(&len
), bool16
.string()) == 0) {
1188 curFormat
= ResTable_map::TYPE_REFERENCE
|ResTable_map::TYPE_BOOLEAN
;
1189 } else if (strcmp16(block
.getElementName(&len
), integer16
.string()) == 0) {
1190 curTag
= &integer16
;
1191 curType
= integer16
;
1192 curFormat
= ResTable_map::TYPE_REFERENCE
|ResTable_map::TYPE_INTEGER
;
1193 } else if (strcmp16(block
.getElementName(&len
), dimen16
.string()) == 0) {
1196 curFormat
= ResTable_map::TYPE_REFERENCE
|ResTable_map::TYPE_DIMENSION
;
1197 } else if (strcmp16(block
.getElementName(&len
), fraction16
.string()) == 0) {
1198 curTag
= &fraction16
;
1199 curType
= fraction16
;
1200 curFormat
= ResTable_map::TYPE_REFERENCE
|ResTable_map::TYPE_FRACTION
;
1201 } else if (strcmp16(block
.getElementName(&len
), bag16
.string()) == 0) {
1204 ssize_t attri
= block
.indexOfAttribute(NULL
, "type");
1206 curType
= String16(block
.getAttributeStringValue(attri
, &len
));
1208 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1209 "A 'type' attribute is required for <bag>\n");
1210 hasErrors
= localHasErrors
= true;
1212 } else if (strcmp16(block
.getElementName(&len
), style16
.string()) == 0) {
1216 } else if (strcmp16(block
.getElementName(&len
), plurals16
.string()) == 0) {
1217 curTag
= &plurals16
;
1218 curType
= plurals16
;
1220 } else if (strcmp16(block
.getElementName(&len
), array16
.string()) == 0) {
1224 curIsBagReplaceOnOverwrite
= true;
1225 ssize_t formatIdx
= block
.indexOfAttribute(NULL
, "format");
1226 if (formatIdx
>= 0) {
1227 String16 formatStr
= String16(block
.getAttributeStringValue(
1229 curFormat
= parse_flags(formatStr
.string(), formatStr
.size(),
1231 if (curFormat
== 0) {
1232 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1233 "Tag <array> 'format' attribute value \"%s\" not valid\n",
1234 String8(formatStr
).string());
1235 hasErrors
= localHasErrors
= true;
1238 } else if (strcmp16(block
.getElementName(&len
), string_array16
.string()) == 0) {
1239 curTag
= &string_array16
;
1241 curFormat
= ResTable_map::TYPE_REFERENCE
|ResTable_map::TYPE_STRING
;
1243 curIsBagReplaceOnOverwrite
= true;
1244 curIsPseudolocalizable
= true;
1245 } else if (strcmp16(block
.getElementName(&len
), integer_array16
.string()) == 0) {
1246 curTag
= &integer_array16
;
1248 curFormat
= ResTable_map::TYPE_REFERENCE
|ResTable_map::TYPE_INTEGER
;
1250 curIsBagReplaceOnOverwrite
= true;
1252 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1253 "Found tag %s where item is expected\n",
1254 String8(block
.getElementName(&len
)).string());
1255 return UNKNOWN_ERROR
;
1259 ssize_t identIdx
= block
.indexOfAttribute(NULL
, "name");
1260 if (identIdx
>= 0) {
1261 ident
= String16(block
.getAttributeStringValue(identIdx
, &len
));
1263 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1264 "A 'name' attribute is required for <%s>\n",
1265 String8(*curTag
).string());
1266 hasErrors
= localHasErrors
= true;
1269 String16
comment(block
.getComment(&len
) ? block
.getComment(&len
) : nulStr
);
1272 // Figure out the parent of this bag...
1273 String16 parentIdent
;
1274 ssize_t parentIdentIdx
= block
.indexOfAttribute(NULL
, "parent");
1275 if (parentIdentIdx
>= 0) {
1276 parentIdent
= String16(block
.getAttributeStringValue(parentIdentIdx
, &len
));
1278 ssize_t sep
= ident
.findLast('.');
1280 parentIdent
.setTo(ident
, sep
);
1284 if (!localHasErrors
) {
1285 err
= outTable
->startBag(SourcePos(in
->getPrintableSource(),
1286 block
.getLineNumber()), myPackage
, curType
, ident
,
1287 parentIdent
, &curParams
,
1288 overwrite
, curIsBagReplaceOnOverwrite
);
1289 if (err
!= NO_ERROR
) {
1290 hasErrors
= localHasErrors
= true;
1294 ssize_t elmIndex
= 0;
1295 char elmIndexStr
[14];
1296 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
1297 && code
!= ResXMLTree::BAD_DOCUMENT
) {
1299 if (code
== ResXMLTree::START_TAG
) {
1300 if (strcmp16(block
.getElementName(&len
), item16
.string()) != 0) {
1301 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1302 "Tag <%s> can not appear inside <%s>, only <item>\n",
1303 String8(block
.getElementName(&len
)).string(),
1304 String8(*curTag
).string());
1305 return UNKNOWN_ERROR
;
1309 if (curType
== array16
) {
1310 sprintf(elmIndexStr
, "^index_%d", (int)elmIndex
++);
1311 itemIdent
= String16(elmIndexStr
);
1312 } else if (curType
== plurals16
) {
1313 ssize_t itemIdentIdx
= block
.indexOfAttribute(NULL
, "quantity");
1314 if (itemIdentIdx
>= 0) {
1315 String16
quantity16(block
.getAttributeStringValue(itemIdentIdx
, &len
));
1316 if (quantity16
== other16
) {
1317 itemIdent
= quantityOther16
;
1319 else if (quantity16
== zero16
) {
1320 itemIdent
= quantityZero16
;
1322 else if (quantity16
== one16
) {
1323 itemIdent
= quantityOne16
;
1325 else if (quantity16
== two16
) {
1326 itemIdent
= quantityTwo16
;
1328 else if (quantity16
== few16
) {
1329 itemIdent
= quantityFew16
;
1331 else if (quantity16
== many16
) {
1332 itemIdent
= quantityMany16
;
1335 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1336 "Illegal 'quantity' attribute is <item> inside <plurals>\n");
1337 hasErrors
= localHasErrors
= true;
1340 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1341 "A 'quantity' attribute is required for <item> inside <plurals>\n");
1342 hasErrors
= localHasErrors
= true;
1345 ssize_t itemIdentIdx
= block
.indexOfAttribute(NULL
, "name");
1346 if (itemIdentIdx
>= 0) {
1347 itemIdent
= String16(block
.getAttributeStringValue(itemIdentIdx
, &len
));
1349 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1350 "A 'name' attribute is required for <item>\n");
1351 hasErrors
= localHasErrors
= true;
1355 ResXMLParser::ResXMLPosition parserPosition
;
1356 block
.getPosition(&parserPosition
);
1358 err
= parseAndAddBag(bundle
, in
, &block
, curParams
, myPackage
, curType
,
1359 ident
, parentIdent
, itemIdent
, curFormat
,
1360 false, overwrite
, outTable
);
1361 if (err
== NO_ERROR
) {
1362 if (curIsPseudolocalizable
&& localeIsDefined(curParams
)
1363 && bundle
->getPseudolocalize()) {
1364 // pseudolocalize here
1366 block
.setPosition(parserPosition
);
1367 err
= parseAndAddBag(bundle
, in
, &block
, pseudoParams
, myPackage
,
1368 curType
, ident
, parentIdent
, itemIdent
, curFormat
, true,
1369 overwrite
, outTable
);
1373 if (err
!= NO_ERROR
) {
1374 hasErrors
= localHasErrors
= true;
1376 } else if (code
== ResXMLTree::END_TAG
) {
1377 if (strcmp16(block
.getElementName(&len
), curTag
->string()) != 0) {
1378 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1379 "Found tag </%s> where </%s> is expected\n",
1380 String8(block
.getElementName(&len
)).string(),
1381 String8(*curTag
).string());
1382 return UNKNOWN_ERROR
;
1388 ResXMLParser::ResXMLPosition parserPosition
;
1389 block
.getPosition(&parserPosition
);
1391 err
= parseAndAddEntry(bundle
, in
, &block
, curParams
, myPackage
, curType
, ident
,
1392 *curTag
, curIsStyled
, curFormat
, false, overwrite
, outTable
);
1394 if (err
< NO_ERROR
) { // Why err < NO_ERROR instead of err != NO_ERROR?
1395 hasErrors
= localHasErrors
= true;
1397 else if (err
== NO_ERROR
) {
1398 if (curIsPseudolocalizable
&& localeIsDefined(curParams
)
1399 && bundle
->getPseudolocalize()) {
1400 // pseudolocalize here
1401 block
.setPosition(parserPosition
);
1402 err
= parseAndAddEntry(bundle
, in
, &block
, pseudoParams
, myPackage
, curType
,
1403 ident
, *curTag
, curIsStyled
, curFormat
, true, overwrite
, outTable
);
1404 if (err
!= NO_ERROR
) {
1405 hasErrors
= localHasErrors
= true;
1412 if (comment
.size() > 0) {
1413 printf("Comment for @%s:%s/%s: %s\n", String8(myPackage
).string(),
1414 String8(curType
).string(), String8(ident
).string(),
1415 String8(comment
).string());
1418 if (!localHasErrors
) {
1419 outTable
->appendComment(myPackage
, curType
, ident
, comment
, false);
1422 else if (code
== ResXMLTree::END_TAG
) {
1423 if (strcmp16(block
.getElementName(&len
), resources16
.string()) != 0) {
1424 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1425 "Unexpected end tag %s\n", String8(block
.getElementName(&len
)).string());
1426 return UNKNOWN_ERROR
;
1429 else if (code
== ResXMLTree::START_NAMESPACE
|| code
== ResXMLTree::END_NAMESPACE
) {
1431 else if (code
== ResXMLTree::TEXT
) {
1432 if (isWhitespace(block
.getText(&len
))) {
1435 SourcePos(in
->getPrintableSource(), block
.getLineNumber()).error(
1436 "Found text \"%s\" where item tag is expected\n",
1437 String8(block
.getText(&len
)).string());
1438 return UNKNOWN_ERROR
;
1442 return hasErrors
? UNKNOWN_ERROR
: NO_ERROR
;
1445 ResourceTable::ResourceTable(Bundle
* bundle
, const String16
& assetsPackage
)
1446 : mAssetsPackage(assetsPackage
), mNextPackageId(1), mHaveAppPackage(false),
1447 mIsAppPackage(!bundle
->getExtending()),
1453 status_t
ResourceTable::addIncludedResources(Bundle
* bundle
, const sp
<AaptAssets
>& assets
)
1455 status_t err
= assets
->buildIncludedResources(bundle
);
1456 if (err
!= NO_ERROR
) {
1460 // For future reference to included resources.
1463 const ResTable
& incl
= assets
->getIncludedResources();
1465 // Retrieve all the packages.
1466 const size_t N
= incl
.getBasePackageCount();
1467 for (size_t phase
=0; phase
<2; phase
++) {
1468 for (size_t i
=0; i
<N
; i
++) {
1469 String16
name(incl
.getBasePackageName(i
));
1470 uint32_t id
= incl
.getBasePackageId(i
);
1471 // First time through: only add base packages (id
1472 // is not 0); second time through add the other
1476 // Skip base packages -- already one.
1479 // Assign a dynamic id.
1480 id
= mNextPackageId
;
1482 } else if (id
!= 0) {
1484 if (mHaveAppPackage
) {
1485 fprintf(stderr
, "Included resources have two application packages!\n");
1486 return UNKNOWN_ERROR
;
1488 mHaveAppPackage
= true;
1490 if (mNextPackageId
> id
) {
1491 fprintf(stderr
, "Included base package ID %d already in use!\n", id
);
1492 return UNKNOWN_ERROR
;
1496 NOISY(printf("Including package %s with ID=%d\n",
1497 String8(name
).string(), id
));
1498 sp
<Package
> p
= new Package(name
, id
);
1499 mPackages
.add(name
, p
);
1500 mOrderedPackages
.add(p
);
1502 if (id
>= mNextPackageId
) {
1503 mNextPackageId
= id
+1;
1509 // Every resource table always has one first entry, the bag attributes.
1510 const SourcePos
unknown(String8("????"), 0);
1511 sp
<Type
> attr
= getType(mAssetsPackage
, String16("attr"), unknown
);
1516 status_t
ResourceTable::addPublic(const SourcePos
& sourcePos
,
1517 const String16
& package
,
1518 const String16
& type
,
1519 const String16
& name
,
1520 const uint32_t ident
)
1522 uint32_t rid
= mAssets
->getIncludedResources()
1523 .identifierForName(name
.string(), name
.size(),
1524 type
.string(), type
.size(),
1525 package
.string(), package
.size());
1527 sourcePos
.error("Error declaring public resource %s/%s for included package %s\n",
1528 String8(type
).string(), String8(name
).string(),
1529 String8(package
).string());
1530 return UNKNOWN_ERROR
;
1533 sp
<Type
> t
= getType(package
, type
, sourcePos
);
1535 return UNKNOWN_ERROR
;
1537 return t
->addPublic(sourcePos
, name
, ident
);
1540 status_t
ResourceTable::addEntry(const SourcePos
& sourcePos
,
1541 const String16
& package
,
1542 const String16
& type
,
1543 const String16
& name
,
1544 const String16
& value
,
1545 const Vector
<StringPool::entry_style_span
>* style
,
1546 const ResTable_config
* params
,
1547 const bool doSetIndex
,
1548 const int32_t format
,
1549 const bool overwrite
)
1551 // Check for adding entries in other packages... for now we do
1552 // nothing. We need to do the right thing here to support skinning.
1553 uint32_t rid
= mAssets
->getIncludedResources()
1554 .identifierForName(name
.string(), name
.size(),
1555 type
.string(), type
.size(),
1556 package
.string(), package
.size());
1562 if (name
== String16("left")) {
1563 printf("Adding entry left: file=%s, line=%d, type=%s, value=%s\n",
1564 sourcePos
.file
.string(), sourcePos
.line
, String8(type
).string(),
1565 String8(value
).string());
1569 sp
<Entry
> e
= getEntry(package
, type
, name
, sourcePos
, overwrite
,
1570 params
, doSetIndex
);
1572 return UNKNOWN_ERROR
;
1574 status_t err
= e
->setItem(sourcePos
, value
, style
, format
, overwrite
);
1575 if (err
== NO_ERROR
) {
1581 status_t
ResourceTable::startBag(const SourcePos
& sourcePos
,
1582 const String16
& package
,
1583 const String16
& type
,
1584 const String16
& name
,
1585 const String16
& bagParent
,
1586 const ResTable_config
* params
,
1588 bool replace
, bool isId
)
1590 status_t result
= NO_ERROR
;
1592 // Check for adding entries in other packages... for now we do
1593 // nothing. We need to do the right thing here to support skinning.
1594 uint32_t rid
= mAssets
->getIncludedResources()
1595 .identifierForName(name
.string(), name
.size(),
1596 type
.string(), type
.size(),
1597 package
.string(), package
.size());
1603 if (name
== String16("left")) {
1604 printf("Adding bag left: file=%s, line=%d, type=%s\n",
1605 sourcePos
.file
.striing(), sourcePos
.line
, String8(type
).string());
1608 if (overlay
&& !mBundle
->getAutoAddOverlay() && !hasBagOrEntry(package
, type
, name
)) {
1609 bool canAdd
= false;
1610 sp
<Package
> p
= mPackages
.valueFor(package
);
1612 sp
<Type
> t
= p
->getTypes().valueFor(type
);
1614 if (t
->getCanAddEntries().indexOf(name
) >= 0) {
1620 sourcePos
.error("Resource does not already exist in overlay at '%s'; use <add-resource> to add.\n",
1621 String8(name
).string());
1622 return UNKNOWN_ERROR
;
1625 sp
<Entry
> e
= getEntry(package
, type
, name
, sourcePos
, overlay
, params
);
1627 return UNKNOWN_ERROR
;
1630 // If a parent is explicitly specified, set it.
1631 if (bagParent
.size() > 0) {
1632 String16 curPar
= e
->getParent();
1633 if (curPar
.size() > 0 && curPar
!= bagParent
) {
1634 sourcePos
.error("Conflicting parents specified, was '%s', now '%s'\n",
1635 String8(e
->getParent()).string(),
1636 String8(bagParent
).string());
1637 return UNKNOWN_ERROR
;
1639 e
->setParent(bagParent
);
1642 if ((result
= e
->makeItABag(sourcePos
)) != NO_ERROR
) {
1646 if (overlay
&& replace
) {
1647 return e
->emptyBag(sourcePos
);
1652 status_t
ResourceTable::addBag(const SourcePos
& sourcePos
,
1653 const String16
& package
,
1654 const String16
& type
,
1655 const String16
& name
,
1656 const String16
& bagParent
,
1657 const String16
& bagKey
,
1658 const String16
& value
,
1659 const Vector
<StringPool::entry_style_span
>* style
,
1660 const ResTable_config
* params
,
1661 bool replace
, bool isId
, const int32_t format
)
1663 // Check for adding entries in other packages... for now we do
1664 // nothing. We need to do the right thing here to support skinning.
1665 uint32_t rid
= mAssets
->getIncludedResources()
1666 .identifierForName(name
.string(), name
.size(),
1667 type
.string(), type
.size(),
1668 package
.string(), package
.size());
1674 if (name
== String16("left")) {
1675 printf("Adding bag left: file=%s, line=%d, type=%s\n",
1676 sourcePos
.file
.striing(), sourcePos
.line
, String8(type
).string());
1679 sp
<Entry
> e
= getEntry(package
, type
, name
, sourcePos
, replace
, params
);
1681 return UNKNOWN_ERROR
;
1684 // If a parent is explicitly specified, set it.
1685 if (bagParent
.size() > 0) {
1686 String16 curPar
= e
->getParent();
1687 if (curPar
.size() > 0 && curPar
!= bagParent
) {
1688 sourcePos
.error("Conflicting parents specified, was '%s', now '%s'\n",
1689 String8(e
->getParent()).string(),
1690 String8(bagParent
).string());
1691 return UNKNOWN_ERROR
;
1693 e
->setParent(bagParent
);
1696 const bool first
= e
->getBag().indexOfKey(bagKey
) < 0;
1697 status_t err
= e
->addToBag(sourcePos
, bagKey
, value
, style
, replace
, isId
, format
);
1698 if (err
== NO_ERROR
&& first
) {
1704 bool ResourceTable::hasBagOrEntry(const String16
& package
,
1705 const String16
& type
,
1706 const String16
& name
) const
1708 // First look for this in the included resources...
1709 uint32_t rid
= mAssets
->getIncludedResources()
1710 .identifierForName(name
.string(), name
.size(),
1711 type
.string(), type
.size(),
1712 package
.string(), package
.size());
1717 sp
<Package
> p
= mPackages
.valueFor(package
);
1719 sp
<Type
> t
= p
->getTypes().valueFor(type
);
1721 sp
<ConfigList
> c
= t
->getConfigs().valueFor(name
);
1722 if (c
!= NULL
) return true;
1729 bool ResourceTable::hasBagOrEntry(const String16
& ref
,
1730 const String16
* defType
,
1731 const String16
* defPackage
)
1733 String16 package
, type
, name
;
1734 if (!ResTable::expandResourceRef(ref
.string(), ref
.size(), &package
, &type
, &name
,
1735 defType
, defPackage
? defPackage
:&mAssetsPackage
, NULL
)) {
1738 return hasBagOrEntry(package
, type
, name
);
1741 bool ResourceTable::appendComment(const String16
& package
,
1742 const String16
& type
,
1743 const String16
& name
,
1744 const String16
& comment
,
1747 if (comment
.size() <= 0) {
1751 sp
<Package
> p
= mPackages
.valueFor(package
);
1753 sp
<Type
> t
= p
->getTypes().valueFor(type
);
1755 sp
<ConfigList
> c
= t
->getConfigs().valueFor(name
);
1757 c
->appendComment(comment
, onlyIfEmpty
);
1765 bool ResourceTable::appendTypeComment(const String16
& package
,
1766 const String16
& type
,
1767 const String16
& name
,
1768 const String16
& comment
)
1770 if (comment
.size() <= 0) {
1774 sp
<Package
> p
= mPackages
.valueFor(package
);
1776 sp
<Type
> t
= p
->getTypes().valueFor(type
);
1778 sp
<ConfigList
> c
= t
->getConfigs().valueFor(name
);
1780 c
->appendTypeComment(comment
);
1788 void ResourceTable::canAddEntry(const SourcePos
& pos
,
1789 const String16
& package
, const String16
& type
, const String16
& name
)
1791 sp
<Type
> t
= getType(package
, type
, pos
);
1793 t
->canAddEntry(name
);
1797 size_t ResourceTable::size() const {
1798 return mPackages
.size();
1801 size_t ResourceTable::numLocalResources() const {
1805 bool ResourceTable::hasResources() const {
1806 return mNumLocal
> 0;
1809 sp
<AaptFile
> ResourceTable::flatten(Bundle
* bundle
)
1811 sp
<AaptFile
> data
= new AaptFile(String8(), AaptGroupEntry(), String8());
1812 status_t err
= flatten(bundle
, data
);
1813 return err
== NO_ERROR
? data
: NULL
;
1816 inline uint32_t ResourceTable::getResId(const sp
<Package
>& p
,
1820 return makeResId(p
->getAssignedId(), t
->getIndex(), nameId
);
1823 uint32_t ResourceTable::getResId(const String16
& package
,
1824 const String16
& type
,
1825 const String16
& name
,
1826 bool onlyPublic
) const
1828 sp
<Package
> p
= mPackages
.valueFor(package
);
1829 if (p
== NULL
) return 0;
1831 // First look for this in the included resources...
1832 uint32_t specFlags
= 0;
1833 uint32_t rid
= mAssets
->getIncludedResources()
1834 .identifierForName(name
.string(), name
.size(),
1835 type
.string(), type
.size(),
1836 package
.string(), package
.size(),
1840 if ((specFlags
& ResTable_typeSpec::SPEC_PUBLIC
) == 0) {
1845 if (Res_INTERNALID(rid
)) {
1848 return Res_MAKEID(p
->getAssignedId()-1,
1853 sp
<Type
> t
= p
->getTypes().valueFor(type
);
1854 if (t
== NULL
) return 0;
1855 sp
<ConfigList
> c
= t
->getConfigs().valueFor(name
);
1856 if (c
== NULL
) return 0;
1857 int32_t ei
= c
->getEntryIndex();
1858 if (ei
< 0) return 0;
1859 return getResId(p
, t
, ei
);
1862 uint32_t ResourceTable::getResId(const String16
& ref
,
1863 const String16
* defType
,
1864 const String16
* defPackage
,
1865 const char** outErrorMsg
,
1866 bool onlyPublic
) const
1868 String16 package
, type
, name
;
1869 if (!ResTable::expandResourceRef(
1870 ref
.string(), ref
.size(), &package
, &type
, &name
,
1871 defType
, defPackage
? defPackage
:&mAssetsPackage
,
1873 NOISY(printf("Expanding resource: ref=%s\n",
1874 String8(ref
).string()));
1875 NOISY(printf("Expanding resource: defType=%s\n",
1876 defType
? String8(*defType
).string() : "NULL"));
1877 NOISY(printf("Expanding resource: defPackage=%s\n",
1878 defPackage
? String8(*defPackage
).string() : "NULL"));
1879 NOISY(printf("Expanding resource: ref=%s\n", String8(ref
).string()));
1880 NOISY(printf("Expanded resource: p=%s, t=%s, n=%s, res=0\n",
1881 String8(package
).string(), String8(type
).string(),
1882 String8(name
).string()));
1885 uint32_t res
= getResId(package
, type
, name
, onlyPublic
);
1886 NOISY(printf("Expanded resource: p=%s, t=%s, n=%s, res=%d\n",
1887 String8(package
).string(), String8(type
).string(),
1888 String8(name
).string(), res
));
1891 *outErrorMsg
= "No resource found that matches the given name";
1896 bool ResourceTable::isValidResourceName(const String16
& s
)
1898 const char16_t* p
= s
.string();
1901 if ((*p
>= 'a' && *p
<= 'z')
1902 || (*p
>= 'A' && *p
<= 'Z')
1904 || (!first
&& *p
>= '0' && *p
<= '9')) {
1914 bool ResourceTable::stringToValue(Res_value
* outValue
, StringPool
* pool
,
1915 const String16
& str
,
1916 bool preserveSpaces
, bool coerceType
,
1918 const Vector
<StringPool::entry_style_span
>* style
,
1919 String16
* outStr
, void* accessorCookie
,
1925 if (style
== NULL
|| style
->size() == 0) {
1926 // Text is not styled so it can be any type... let's figure it out.
1927 res
= mAssets
->getIncludedResources()
1928 .stringToValue(outValue
, &finalStr
, str
.string(), str
.size(), preserveSpaces
,
1929 coerceType
, attrID
, NULL
, &mAssetsPackage
, this,
1930 accessorCookie
, attrType
);
1932 // Styled text can only be a string, and while collecting the style
1933 // information we have already processed that string!
1934 outValue
->size
= sizeof(Res_value
);
1936 outValue
->dataType
= outValue
->TYPE_STRING
;
1945 if (outValue
->dataType
== outValue
->TYPE_STRING
) {
1946 // Should do better merging styles.
1948 if (style
!= NULL
&& style
->size() > 0) {
1949 outValue
->data
= pool
->add(finalStr
, *style
);
1951 outValue
->data
= pool
->add(finalStr
, true);
1954 // Caller will fill this in later.
1967 uint32_t ResourceTable::getCustomResource(
1968 const String16
& package
, const String16
& type
, const String16
& name
) const
1970 //printf("getCustomResource: %s %s %s\n", String8(package).string(),
1971 // String8(type).string(), String8(name).string());
1972 sp
<Package
> p
= mPackages
.valueFor(package
);
1973 if (p
== NULL
) return 0;
1974 sp
<Type
> t
= p
->getTypes().valueFor(type
);
1975 if (t
== NULL
) return 0;
1976 sp
<ConfigList
> c
= t
->getConfigs().valueFor(name
);
1977 if (c
== NULL
) return 0;
1978 int32_t ei
= c
->getEntryIndex();
1979 if (ei
< 0) return 0;
1980 return getResId(p
, t
, ei
);
1983 uint32_t ResourceTable::getCustomResourceWithCreation(
1984 const String16
& package
, const String16
& type
, const String16
& name
,
1985 const bool createIfNotFound
)
1987 uint32_t resId
= getCustomResource(package
, type
, name
);
1988 if (resId
!= 0 || !createIfNotFound
) {
1991 String16
value("false");
1993 status_t status
= addEntry(mCurrentXmlPos
, package
, type
, name
, value
, NULL
, NULL
, true);
1994 if (status
== NO_ERROR
) {
1995 resId
= getResId(package
, type
, name
);
2001 uint32_t ResourceTable::getRemappedPackage(uint32_t origPackage
) const
2006 bool ResourceTable::getAttributeType(uint32_t attrID
, uint32_t* outType
)
2008 //printf("getAttributeType #%08x\n", attrID);
2010 if (getItemValue(attrID
, ResTable_map::ATTR_TYPE
, &value
)) {
2011 //printf("getAttributeType #%08x (%s): #%08x\n", attrID,
2012 // String8(getEntry(attrID)->getName()).string(), value.data);
2013 *outType
= value
.data
;
2019 bool ResourceTable::getAttributeMin(uint32_t attrID
, uint32_t* outMin
)
2021 //printf("getAttributeMin #%08x\n", attrID);
2023 if (getItemValue(attrID
, ResTable_map::ATTR_MIN
, &value
)) {
2024 *outMin
= value
.data
;
2030 bool ResourceTable::getAttributeMax(uint32_t attrID
, uint32_t* outMax
)
2032 //printf("getAttributeMax #%08x\n", attrID);
2034 if (getItemValue(attrID
, ResTable_map::ATTR_MAX
, &value
)) {
2035 *outMax
= value
.data
;
2041 uint32_t ResourceTable::getAttributeL10N(uint32_t attrID
)
2043 //printf("getAttributeL10N #%08x\n", attrID);
2045 if (getItemValue(attrID
, ResTable_map::ATTR_L10N
, &value
)) {
2048 return ResTable_map::L10N_NOT_REQUIRED
;
2051 bool ResourceTable::getLocalizationSetting()
2053 return mBundle
->getRequireLocalization();
2056 void ResourceTable::reportError(void* accessorCookie
, const char* fmt
, ...)
2058 if (accessorCookie
!= NULL
&& fmt
!= NULL
) {
2059 AccessorCookie
* ac
= (AccessorCookie
*)accessorCookie
;
2064 retval
= vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
2066 ac
->sourcePos
.error("Error: %s (at '%s' with value '%s').\n",
2067 buf
, ac
->attr
.string(), ac
->value
.string());
2071 bool ResourceTable::getAttributeKeys(
2072 uint32_t attrID
, Vector
<String16
>* outKeys
)
2074 sp
<const Entry
> e
= getEntry(attrID
);
2076 const size_t N
= e
->getBag().size();
2077 for (size_t i
=0; i
<N
; i
++) {
2078 const String16
& key
= e
->getBag().keyAt(i
);
2079 if (key
.size() > 0 && key
.string()[0] != '^') {
2088 bool ResourceTable::getAttributeEnum(
2089 uint32_t attrID
, const char16_t* name
, size_t nameLen
,
2090 Res_value
* outValue
)
2092 //printf("getAttributeEnum #%08x %s\n", attrID, String8(name, nameLen).string());
2093 String16
nameStr(name
, nameLen
);
2094 sp
<const Entry
> e
= getEntry(attrID
);
2096 const size_t N
= e
->getBag().size();
2097 for (size_t i
=0; i
<N
; i
++) {
2098 //printf("Comparing %s to %s\n", String8(name, nameLen).string(),
2099 // String8(e->getBag().keyAt(i)).string());
2100 if (e
->getBag().keyAt(i
) == nameStr
) {
2101 return getItemValue(attrID
, e
->getBag().valueAt(i
).bagKeyId
, outValue
);
2108 bool ResourceTable::getAttributeFlags(
2109 uint32_t attrID
, const char16_t* name
, size_t nameLen
,
2110 Res_value
* outValue
)
2112 outValue
->dataType
= Res_value::TYPE_INT_HEX
;
2115 //printf("getAttributeFlags #%08x %s\n", attrID, String8(name, nameLen).string());
2116 String16
nameStr(name
, nameLen
);
2117 sp
<const Entry
> e
= getEntry(attrID
);
2119 const size_t N
= e
->getBag().size();
2121 const char16_t* end
= name
+ nameLen
;
2122 const char16_t* pos
= name
;
2123 bool failed
= false;
2124 while (pos
< end
&& !failed
) {
2125 const char16_t* start
= pos
;
2127 while (pos
< end
&& *pos
!= '|') {
2131 String16
nameStr(start
, pos
-start
);
2133 for (i
=0; i
<N
; i
++) {
2134 //printf("Comparing \"%s\" to \"%s\"\n", String8(nameStr).string(),
2135 // String8(e->getBag().keyAt(i)).string());
2136 if (e
->getBag().keyAt(i
) == nameStr
) {
2138 bool got
= getItemValue(attrID
, e
->getBag().valueAt(i
).bagKeyId
, &val
);
2142 //printf("Got value: 0x%08x\n", val.data);
2143 outValue
->data
|= val
.data
;
2149 // Didn't find this flag identifier.
2162 status_t
ResourceTable::assignResourceIds()
2164 const size_t N
= mOrderedPackages
.size();
2166 status_t firstError
= NO_ERROR
;
2168 // First generate all bag attributes and assign indices.
2169 for (pi
=0; pi
<N
; pi
++) {
2170 sp
<Package
> p
= mOrderedPackages
.itemAt(pi
);
2171 if (p
== NULL
|| p
->getTypes().size() == 0) {
2176 status_t err
= p
->applyPublicTypeOrder();
2177 if (err
!= NO_ERROR
&& firstError
== NO_ERROR
) {
2181 // Generate attributes...
2182 const size_t N
= p
->getOrderedTypes().size();
2184 for (ti
=0; ti
<N
; ti
++) {
2185 sp
<Type
> t
= p
->getOrderedTypes().itemAt(ti
);
2189 const size_t N
= t
->getOrderedConfigs().size();
2190 for (size_t ci
=0; ci
<N
; ci
++) {
2191 sp
<ConfigList
> c
= t
->getOrderedConfigs().itemAt(ci
);
2195 const size_t N
= c
->getEntries().size();
2196 for (size_t ei
=0; ei
<N
; ei
++) {
2197 sp
<Entry
> e
= c
->getEntries().valueAt(ei
);
2201 status_t err
= e
->generateAttributes(this, p
->getName());
2202 if (err
!= NO_ERROR
&& firstError
== NO_ERROR
) {
2209 const SourcePos
unknown(String8("????"), 0);
2210 sp
<Type
> attr
= p
->getType(String16("attr"), unknown
);
2212 // Assign indices...
2213 for (ti
=0; ti
<N
; ti
++) {
2214 sp
<Type
> t
= p
->getOrderedTypes().itemAt(ti
);
2218 err
= t
->applyPublicEntryOrder();
2219 if (err
!= NO_ERROR
&& firstError
== NO_ERROR
) {
2223 const size_t N
= t
->getOrderedConfigs().size();
2226 LOG_ALWAYS_FATAL_IF(ti
== 0 && attr
!= t
,
2227 "First type is not attr!");
2229 for (size_t ei
=0; ei
<N
; ei
++) {
2230 sp
<ConfigList
> c
= t
->getOrderedConfigs().itemAt(ei
);
2234 c
->setEntryIndex(ei
);
2238 // Assign resource IDs to keys in bags...
2239 for (ti
=0; ti
<N
; ti
++) {
2240 sp
<Type
> t
= p
->getOrderedTypes().itemAt(ti
);
2244 const size_t N
= t
->getOrderedConfigs().size();
2245 for (size_t ci
=0; ci
<N
; ci
++) {
2246 sp
<ConfigList
> c
= t
->getOrderedConfigs().itemAt(ci
);
2247 //printf("Ordered config #%d: %p\n", ci, c.get());
2248 const size_t N
= c
->getEntries().size();
2249 for (size_t ei
=0; ei
<N
; ei
++) {
2250 sp
<Entry
> e
= c
->getEntries().valueAt(ei
);
2254 status_t err
= e
->assignResourceIds(this, p
->getName());
2255 if (err
!= NO_ERROR
&& firstError
== NO_ERROR
) {
2265 status_t
ResourceTable::addSymbols(const sp
<AaptSymbols
>& outSymbols
) {
2266 const size_t N
= mOrderedPackages
.size();
2269 for (pi
=0; pi
<N
; pi
++) {
2270 sp
<Package
> p
= mOrderedPackages
.itemAt(pi
);
2271 if (p
->getTypes().size() == 0) {
2276 const size_t N
= p
->getOrderedTypes().size();
2279 for (ti
=0; ti
<N
; ti
++) {
2280 sp
<Type
> t
= p
->getOrderedTypes().itemAt(ti
);
2284 const size_t N
= t
->getOrderedConfigs().size();
2285 sp
<AaptSymbols
> typeSymbols
;
2286 typeSymbols
= outSymbols
->addNestedSymbol(String8(t
->getName()), t
->getPos());
2287 for (size_t ci
=0; ci
<N
; ci
++) {
2288 sp
<ConfigList
> c
= t
->getOrderedConfigs().itemAt(ci
);
2292 uint32_t rid
= getResId(p
, t
, ci
);
2294 return UNKNOWN_ERROR
;
2296 if (Res_GETPACKAGE(rid
) == (size_t)(p
->getAssignedId()-1)) {
2297 typeSymbols
->addSymbol(String8(c
->getName()), rid
, c
->getPos());
2299 String16
comment(c
->getComment());
2300 typeSymbols
->appendComment(String8(c
->getName()), comment
, c
->getPos());
2301 //printf("Type symbol %s comment: %s\n", String8(e->getName()).string(),
2302 // String8(comment).string());
2303 comment
= c
->getTypeComment();
2304 typeSymbols
->appendTypeComment(String8(c
->getName()), comment
);
2307 printf("**** NO MATCH: 0x%08x vs 0x%08x\n",
2308 Res_GETPACKAGE(rid
), p
->getAssignedId());
2319 ResourceTable::addLocalization(const String16
& name
, const String8
& locale
)
2321 mLocalizations
[name
].insert(locale
);
2326 * Flag various sorts of localization problems. '+' indicates checks already implemented;
2327 * '-' indicates checks that will be implemented in the future.
2329 * + A localized string for which no default-locale version exists => warning
2330 * + A string for which no version in an explicitly-requested locale exists => warning
2331 * + A localized translation of an translateable="false" string => warning
2332 * - A localized string not provided in every locale used by the table
2335 ResourceTable::validateLocalizations(void)
2337 status_t err
= NO_ERROR
;
2338 const String8 defaultLocale
;
2340 // For all strings...
2341 for (map
<String16
, set
<String8
> >::iterator nameIter
= mLocalizations
.begin();
2342 nameIter
!= mLocalizations
.end();
2344 const set
<String8
>& configSet
= nameIter
->second
; // naming convenience
2346 // Look for strings with no default localization
2347 if (configSet
.count(defaultLocale
) == 0) {
2348 fprintf(stdout
, "aapt: warning: string '%s' has no default translation in %s; found:",
2349 String8(nameIter
->first
).string(), mBundle
->getResourceSourceDirs()[0]);
2350 for (set
<String8
>::iterator locales
= configSet
.begin();
2351 locales
!= configSet
.end();
2353 fprintf(stdout
, " %s", (*locales
).string());
2355 fprintf(stdout
, "\n");
2356 // !!! TODO: throw an error here in some circumstances
2359 // Check that all requested localizations are present for this string
2360 if (mBundle
->getConfigurations() != NULL
&& mBundle
->getRequireLocalization()) {
2361 const char* allConfigs
= mBundle
->getConfigurations();
2362 const char* start
= allConfigs
;
2367 comma
= strchr(start
, ',');
2368 if (comma
!= NULL
) {
2369 config
.setTo(start
, comma
- start
);
2372 config
.setTo(start
);
2375 // don't bother with the pseudolocale "zz_ZZ"
2376 if (config
!= "zz_ZZ") {
2377 if (configSet
.find(config
) == configSet
.end()) {
2378 // okay, no specific localization found. it's possible that we are
2379 // requiring a specific regional localization [e.g. de_DE] but there is an
2380 // available string in the generic language localization [e.g. de];
2381 // consider that string to have fulfilled the localization requirement.
2382 String8
region(config
.string(), 2);
2383 if (configSet
.find(region
) == configSet
.end()) {
2384 if (configSet
.count(defaultLocale
) == 0) {
2385 fprintf(stdout
, "aapt: warning: "
2386 "**** string '%s' has no default or required localization "
2388 String8(nameIter
->first
).string(),
2390 mBundle
->getResourceSourceDirs()[0]);
2395 } while (comma
!= NULL
);
2404 ResourceFilter::parse(const char* arg
)
2410 const char* p
= arg
;
2419 String8
part(p
, q
-p
);
2421 if (part
== "zz_ZZ") {
2422 mContainsPseudo
= true;
2426 if (AaptGroupEntry::parseNamePart(part
, &axis
, &value
)) {
2427 fprintf(stderr
, "Invalid configuration: %s\n", arg
);
2428 fprintf(stderr
, " ");
2429 for (int i
=0; i
<p
-arg
; i
++) {
2430 fprintf(stderr
, " ");
2432 for (int i
=0; i
<q
-p
; i
++) {
2433 fprintf(stderr
, "^");
2435 fprintf(stderr
, "\n");
2439 ssize_t index
= mData
.indexOfKey(axis
);
2441 mData
.add(axis
, SortedVector
<uint32_t>());
2443 SortedVector
<uint32_t>& sv
= mData
.editValueFor(axis
);
2445 // if it's a locale with a region, also match an unmodified locale of the
2447 if (axis
== AXIS_LANGUAGE
) {
2448 if (value
& 0xffff0000) {
2449 sv
.add(value
& 0x0000ffff);
2461 ResourceFilter::match(int axis
, uint32_t value
)
2464 // they didn't specify anything so take everything
2467 ssize_t index
= mData
.indexOfKey(axis
);
2469 // we didn't request anything on this axis so take everything
2472 const SortedVector
<uint32_t>& sv
= mData
.valueAt(index
);
2473 return sv
.indexOf(value
) >= 0;
2477 ResourceFilter::match(const ResTable_config
& config
)
2479 if (config
.locale
) {
2480 uint32_t locale
= (config
.country
[1] << 24) | (config
.country
[0] << 16)
2481 | (config
.language
[1] << 8) | (config
.language
[0]);
2482 if (!match(AXIS_LANGUAGE
, locale
)) {
2486 if (!match(AXIS_ORIENTATION
, config
.orientation
)) {
2489 if (!match(AXIS_UIMODETYPE
, (config
.uiMode
&ResTable_config::MASK_UI_MODE_TYPE
))) {
2492 if (!match(AXIS_UIMODENIGHT
, (config
.uiMode
&ResTable_config::MASK_UI_MODE_NIGHT
))) {
2495 if (!match(AXIS_DENSITY
, config
.density
)) {
2498 if (!match(AXIS_TOUCHSCREEN
, config
.touchscreen
)) {
2501 if (!match(AXIS_KEYSHIDDEN
, config
.inputFlags
)) {
2504 if (!match(AXIS_KEYBOARD
, config
.keyboard
)) {
2507 if (!match(AXIS_NAVIGATION
, config
.navigation
)) {
2510 if (!match(AXIS_SCREENSIZE
, config
.screenSize
)) {
2513 if (!match(AXIS_VERSION
, config
.version
)) {
2519 status_t
ResourceTable::flatten(Bundle
* bundle
, const sp
<AaptFile
>& dest
)
2521 ResourceFilter filter
;
2522 status_t err
= filter
.parse(bundle
->getConfigurations());
2523 if (err
!= NO_ERROR
) {
2527 const size_t N
= mOrderedPackages
.size();
2530 // Iterate through all data, collecting all values (strings,
2531 // references, etc).
2532 StringPool valueStrings
= StringPool(false, bundle
->getUTF8());
2533 for (pi
=0; pi
<N
; pi
++) {
2534 sp
<Package
> p
= mOrderedPackages
.itemAt(pi
);
2535 if (p
->getTypes().size() == 0) {
2540 StringPool typeStrings
= StringPool(false, bundle
->getUTF8());
2541 StringPool keyStrings
= StringPool(false, bundle
->getUTF8());
2543 const size_t N
= p
->getOrderedTypes().size();
2544 for (size_t ti
=0; ti
<N
; ti
++) {
2545 sp
<Type
> t
= p
->getOrderedTypes().itemAt(ti
);
2547 typeStrings
.add(String16("<empty>"), false);
2550 typeStrings
.add(t
->getName(), false);
2552 const size_t N
= t
->getOrderedConfigs().size();
2553 for (size_t ci
=0; ci
<N
; ci
++) {
2554 sp
<ConfigList
> c
= t
->getOrderedConfigs().itemAt(ci
);
2558 const size_t N
= c
->getEntries().size();
2559 for (size_t ei
=0; ei
<N
; ei
++) {
2560 ConfigDescription config
= c
->getEntries().keyAt(ei
);
2561 if (!filter
.match(config
)) {
2564 sp
<Entry
> e
= c
->getEntries().valueAt(ei
);
2568 e
->setNameIndex(keyStrings
.add(e
->getName(), true));
2569 status_t err
= e
->prepareFlatten(&valueStrings
, this);
2570 if (err
!= NO_ERROR
) {
2577 p
->setTypeStrings(typeStrings
.createStringBlock());
2578 p
->setKeyStrings(keyStrings
.createStringBlock());
2583 // Now build the array of package chunks.
2584 Vector
<sp
<AaptFile
> > flatPackages
;
2585 for (pi
=0; pi
<N
; pi
++) {
2586 sp
<Package
> p
= mOrderedPackages
.itemAt(pi
);
2587 if (p
->getTypes().size() == 0) {
2592 const size_t N
= p
->getTypeStrings().size();
2594 const size_t baseSize
= sizeof(ResTable_package
);
2596 // Start the package data.
2597 sp
<AaptFile
> data
= new AaptFile(String8(), AaptGroupEntry(), String8());
2598 ResTable_package
* header
= (ResTable_package
*)data
->editData(baseSize
);
2599 if (header
== NULL
) {
2600 fprintf(stderr
, "ERROR: out of memory creating ResTable_package\n");
2603 memset(header
, 0, sizeof(*header
));
2604 header
->header
.type
= htods(RES_TABLE_PACKAGE_TYPE
);
2605 header
->header
.headerSize
= htods(sizeof(*header
));
2606 header
->id
= htodl(p
->getAssignedId());
2607 strcpy16_htod(header
->name
, p
->getName().string());
2609 // Write the string blocks.
2610 const size_t typeStringsStart
= data
->getSize();
2611 sp
<AaptFile
> strFile
= p
->getTypeStringsData();
2612 ssize_t amt
= data
->writeData(strFile
->getData(), strFile
->getSize());
2613 #if PRINT_STRING_METRICS
2614 fprintf(stderr
, "**** type strings: %d\n", amt
);
2620 const size_t keyStringsStart
= data
->getSize();
2621 strFile
= p
->getKeyStringsData();
2622 amt
= data
->writeData(strFile
->getData(), strFile
->getSize());
2623 #if PRINT_STRING_METRICS
2624 fprintf(stderr
, "**** key strings: %d\n", amt
);
2631 // Build the type chunks inside of this package.
2632 for (size_t ti
=0; ti
<N
; ti
++) {
2633 // Retrieve them in the same order as the type string block.
2635 String16
typeName(p
->getTypeStrings().stringAt(ti
, &len
));
2636 sp
<Type
> t
= p
->getTypes().valueFor(typeName
);
2637 LOG_ALWAYS_FATAL_IF(t
== NULL
&& typeName
!= String16("<empty>"),
2638 "Type name %s not found",
2639 String8(typeName
).string());
2641 const size_t N
= t
!= NULL
? t
->getOrderedConfigs().size() : 0;
2643 // First write the typeSpec chunk, containing information about
2644 // each resource entry in this type.
2646 const size_t typeSpecSize
= sizeof(ResTable_typeSpec
) + sizeof(uint32_t)*N
;
2647 const size_t typeSpecStart
= data
->getSize();
2648 ResTable_typeSpec
* tsHeader
= (ResTable_typeSpec
*)
2649 (((uint8_t*)data
->editData(typeSpecStart
+typeSpecSize
)) + typeSpecStart
);
2650 if (tsHeader
== NULL
) {
2651 fprintf(stderr
, "ERROR: out of memory creating ResTable_typeSpec\n");
2654 memset(tsHeader
, 0, sizeof(*tsHeader
));
2655 tsHeader
->header
.type
= htods(RES_TABLE_TYPE_SPEC_TYPE
);
2656 tsHeader
->header
.headerSize
= htods(sizeof(*tsHeader
));
2657 tsHeader
->header
.size
= htodl(typeSpecSize
);
2658 tsHeader
->id
= ti
+1;
2659 tsHeader
->entryCount
= htodl(N
);
2661 uint32_t* typeSpecFlags
= (uint32_t*)
2662 (((uint8_t*)data
->editData())
2663 + typeSpecStart
+ sizeof(ResTable_typeSpec
));
2664 memset(typeSpecFlags
, 0, sizeof(uint32_t)*N
);
2666 for (size_t ei
=0; ei
<N
; ei
++) {
2667 sp
<ConfigList
> cl
= t
->getOrderedConfigs().itemAt(ei
);
2668 if (cl
->getPublic()) {
2669 typeSpecFlags
[ei
] |= htodl(ResTable_typeSpec::SPEC_PUBLIC
);
2671 const size_t CN
= cl
->getEntries().size();
2672 for (size_t ci
=0; ci
<CN
; ci
++) {
2673 if (!filter
.match(cl
->getEntries().keyAt(ci
))) {
2676 for (size_t cj
=ci
+1; cj
<CN
; cj
++) {
2677 if (!filter
.match(cl
->getEntries().keyAt(cj
))) {
2680 typeSpecFlags
[ei
] |= htodl(
2681 cl
->getEntries().keyAt(ci
).diff(cl
->getEntries().keyAt(cj
)));
2687 // We need to write one type chunk for each configuration for
2688 // which we have entries in this type.
2689 const size_t NC
= t
->getUniqueConfigs().size();
2691 const size_t typeSize
= sizeof(ResTable_type
) + sizeof(uint32_t)*N
;
2693 for (size_t ci
=0; ci
<NC
; ci
++) {
2694 ConfigDescription config
= t
->getUniqueConfigs().itemAt(ci
);
2696 NOISY(printf("Writing config %d config: imsi:%d/%d lang:%c%c cnt:%c%c "
2697 "orien:%d ui:%d touch:%d density:%d key:%d inp:%d nav:%d w:%d h:%d\n",
2699 config
.mcc
, config
.mnc
,
2700 config
.language
[0] ? config
.language
[0] : '-',
2701 config
.language
[1] ? config
.language
[1] : '-',
2702 config
.country
[0] ? config
.country
[0] : '-',
2703 config
.country
[1] ? config
.country
[1] : '-',
2712 config
.screenHeight
));
2714 if (!filter
.match(config
)) {
2718 const size_t typeStart
= data
->getSize();
2720 ResTable_type
* tHeader
= (ResTable_type
*)
2721 (((uint8_t*)data
->editData(typeStart
+typeSize
)) + typeStart
);
2722 if (tHeader
== NULL
) {
2723 fprintf(stderr
, "ERROR: out of memory creating ResTable_type\n");
2727 memset(tHeader
, 0, sizeof(*tHeader
));
2728 tHeader
->header
.type
= htods(RES_TABLE_TYPE_TYPE
);
2729 tHeader
->header
.headerSize
= htods(sizeof(*tHeader
));
2731 tHeader
->entryCount
= htodl(N
);
2732 tHeader
->entriesStart
= htodl(typeSize
);
2733 tHeader
->config
= config
;
2734 NOISY(printf("Writing type %d config: imsi:%d/%d lang:%c%c cnt:%c%c "
2735 "orien:%d ui:%d touch:%d density:%d key:%d inp:%d nav:%d w:%d h:%d\n",
2737 tHeader
->config
.mcc
, tHeader
->config
.mnc
,
2738 tHeader
->config
.language
[0] ? tHeader
->config
.language
[0] : '-',
2739 tHeader
->config
.language
[1] ? tHeader
->config
.language
[1] : '-',
2740 tHeader
->config
.country
[0] ? tHeader
->config
.country
[0] : '-',
2741 tHeader
->config
.country
[1] ? tHeader
->config
.country
[1] : '-',
2742 tHeader
->config
.orientation
,
2743 tHeader
->config
.uiMode
,
2744 tHeader
->config
.touchscreen
,
2745 tHeader
->config
.density
,
2746 tHeader
->config
.keyboard
,
2747 tHeader
->config
.inputFlags
,
2748 tHeader
->config
.navigation
,
2749 tHeader
->config
.screenWidth
,
2750 tHeader
->config
.screenHeight
));
2751 tHeader
->config
.swapHtoD();
2753 // Build the entries inside of this type.
2754 for (size_t ei
=0; ei
<N
; ei
++) {
2755 sp
<ConfigList
> cl
= t
->getOrderedConfigs().itemAt(ei
);
2756 sp
<Entry
> e
= cl
->getEntries().valueFor(config
);
2758 // Set the offset for this entry in its type.
2759 uint32_t* index
= (uint32_t*)
2760 (((uint8_t*)data
->editData())
2761 + typeStart
+ sizeof(ResTable_type
));
2763 index
[ei
] = htodl(data
->getSize()-typeStart
-typeSize
);
2765 // Create the entry.
2766 ssize_t amt
= e
->flatten(bundle
, data
, cl
->getPublic());
2771 index
[ei
] = htodl(ResTable_type::NO_ENTRY
);
2775 // Fill in the rest of the type information.
2776 tHeader
= (ResTable_type
*)
2777 (((uint8_t*)data
->editData()) + typeStart
);
2778 tHeader
->header
.size
= htodl(data
->getSize()-typeStart
);
2782 // Fill in the rest of the package information.
2783 header
= (ResTable_package
*)data
->editData();
2784 header
->header
.size
= htodl(data
->getSize());
2785 header
->typeStrings
= htodl(typeStringsStart
);
2786 header
->lastPublicType
= htodl(p
->getTypeStrings().size());
2787 header
->keyStrings
= htodl(keyStringsStart
);
2788 header
->lastPublicKey
= htodl(p
->getKeyStrings().size());
2790 flatPackages
.add(data
);
2793 // And now write out the final chunks.
2794 const size_t dataStart
= dest
->getSize();
2798 ResTable_header header
;
2799 memset(&header
, 0, sizeof(header
));
2800 header
.header
.type
= htods(RES_TABLE_TYPE
);
2801 header
.header
.headerSize
= htods(sizeof(header
));
2802 header
.packageCount
= htodl(flatPackages
.size());
2803 status_t err
= dest
->writeData(&header
, sizeof(header
));
2804 if (err
!= NO_ERROR
) {
2805 fprintf(stderr
, "ERROR: out of memory creating ResTable_header\n");
2810 ssize_t strStart
= dest
->getSize();
2811 err
= valueStrings
.writeStringBlock(dest
);
2812 if (err
!= NO_ERROR
) {
2816 ssize_t amt
= (dest
->getSize()-strStart
);
2818 #if PRINT_STRING_METRICS
2819 fprintf(stderr
, "**** value strings: %d\n", amt
);
2820 fprintf(stderr
, "**** total strings: %d\n", strAmt
);
2823 for (pi
=0; pi
<flatPackages
.size(); pi
++) {
2824 err
= dest
->writeData(flatPackages
[pi
]->getData(),
2825 flatPackages
[pi
]->getSize());
2826 if (err
!= NO_ERROR
) {
2827 fprintf(stderr
, "ERROR: out of memory creating package chunk for ResTable_header\n");
2832 ResTable_header
* header
= (ResTable_header
*)
2833 (((uint8_t*)dest
->getData()) + dataStart
);
2834 header
->header
.size
= htodl(dest
->getSize() - dataStart
);
2836 NOISY(aout
<< "Resource table:"
2837 << HexDump(dest
->getData(), dest
->getSize()) << endl
);
2839 #if PRINT_STRING_METRICS
2840 fprintf(stderr
, "**** total resource table size: %d / %d%% strings\n",
2841 dest
->getSize(), (strAmt
*100)/dest
->getSize());
2847 void ResourceTable::writePublicDefinitions(const String16
& package
, FILE* fp
)
2850 "<!-- This file contains <public> resource definitions for all\n"
2851 " resources that were generated from the source data. -->\n"
2855 writePublicDefinitions(package
, fp
, true);
2856 writePublicDefinitions(package
, fp
, false);
2863 void ResourceTable::writePublicDefinitions(const String16
& package
, FILE* fp
, bool pub
)
2865 bool didHeader
= false;
2867 sp
<Package
> pkg
= mPackages
.valueFor(package
);
2869 const size_t NT
= pkg
->getOrderedTypes().size();
2870 for (size_t i
=0; i
<NT
; i
++) {
2871 sp
<Type
> t
= pkg
->getOrderedTypes().itemAt(i
);
2876 bool didType
= false;
2878 const size_t NC
= t
->getOrderedConfigs().size();
2879 for (size_t j
=0; j
<NC
; j
++) {
2880 sp
<ConfigList
> c
= t
->getOrderedConfigs().itemAt(j
);
2885 if (c
->getPublic() != pub
) {
2895 fprintf(fp
," <!-- PUBLIC SECTION. These resources have been declared public.\n");
2896 fprintf(fp
," Changes to these definitions will break binary compatibility. -->\n\n");
2898 fprintf(fp
," <!-- PRIVATE SECTION. These resources have not been declared public.\n");
2899 fprintf(fp
," You can make them public my moving these lines into a file in res/values. -->\n\n");
2904 const size_t NE
= c
->getEntries().size();
2905 for (size_t k
=0; k
<NE
; k
++) {
2906 const SourcePos
& pos
= c
->getEntries().valueAt(k
)->getPos();
2907 if (pos
.file
!= "") {
2908 fprintf(fp
," <!-- Declared at %s:%d -->\n",
2909 pos
.file
.string(), pos
.line
);
2913 fprintf(fp
, " <public type=\"%s\" name=\"%s\" id=\"0x%08x\" />\n",
2914 String8(t
->getName()).string(),
2915 String8(c
->getName()).string(),
2916 getResId(pkg
, t
, c
->getEntryIndex()));
2922 ResourceTable::Item::Item(const SourcePos
& _sourcePos
,
2924 const String16
& _value
,
2925 const Vector
<StringPool::entry_style_span
>* _style
,
2927 : sourcePos(_sourcePos
)
2939 status_t
ResourceTable::Entry::makeItABag(const SourcePos
& sourcePos
)
2941 if (mType
== TYPE_BAG
) {
2944 if (mType
== TYPE_UNKNOWN
) {
2948 sourcePos
.error("Resource entry %s is already defined as a single item.\n"
2949 "%s:%d: Originally defined here.\n",
2950 String8(mName
).string(),
2951 mItem
.sourcePos
.file
.string(), mItem
.sourcePos
.line
);
2952 return UNKNOWN_ERROR
;
2955 status_t
ResourceTable::Entry::setItem(const SourcePos
& sourcePos
,
2956 const String16
& value
,
2957 const Vector
<StringPool::entry_style_span
>* style
,
2959 const bool overwrite
)
2961 Item
item(sourcePos
, false, value
, style
);
2963 if (mType
== TYPE_BAG
) {
2964 const Item
& item(mBag
.valueAt(0));
2965 sourcePos
.error("Resource entry %s is already defined as a bag.\n"
2966 "%s:%d: Originally defined here.\n",
2967 String8(mName
).string(),
2968 item
.sourcePos
.file
.string(), item
.sourcePos
.line
);
2969 return UNKNOWN_ERROR
;
2971 if ( (mType
!= TYPE_UNKNOWN
) && (overwrite
== false) ) {
2972 sourcePos
.error("Resource entry %s is already defined.\n"
2973 "%s:%d: Originally defined here.\n",
2974 String8(mName
).string(),
2975 mItem
.sourcePos
.file
.string(), mItem
.sourcePos
.line
);
2976 return UNKNOWN_ERROR
;
2981 mItemFormat
= format
;
2985 status_t
ResourceTable::Entry::addToBag(const SourcePos
& sourcePos
,
2986 const String16
& key
, const String16
& value
,
2987 const Vector
<StringPool::entry_style_span
>* style
,
2988 bool replace
, bool isId
, int32_t format
)
2990 status_t err
= makeItABag(sourcePos
);
2991 if (err
!= NO_ERROR
) {
2995 Item
item(sourcePos
, isId
, value
, style
, format
);
2997 // XXX NOTE: there is an error if you try to have a bag with two keys,
2998 // one an attr and one an id, with the same name. Not something we
2999 // currently ever have to worry about.
3000 ssize_t origKey
= mBag
.indexOfKey(key
);
3003 const Item
& item(mBag
.valueAt(origKey
));
3004 sourcePos
.error("Resource entry %s already has bag item %s.\n"
3005 "%s:%d: Originally defined here.\n",
3006 String8(mName
).string(), String8(key
).string(),
3007 item
.sourcePos
.file
.string(), item
.sourcePos
.line
);
3008 return UNKNOWN_ERROR
;
3010 //printf("Replacing %s with %s\n",
3011 // String8(mBag.valueFor(key).value).string(), String8(value).string());
3012 mBag
.replaceValueFor(key
, item
);
3015 mBag
.add(key
, item
);
3019 status_t
ResourceTable::Entry::emptyBag(const SourcePos
& sourcePos
)
3021 status_t err
= makeItABag(sourcePos
);
3022 if (err
!= NO_ERROR
) {
3030 status_t
ResourceTable::Entry::generateAttributes(ResourceTable
* table
,
3031 const String16
& package
)
3033 const String16
attr16("attr");
3034 const String16
id16("id");
3035 const size_t N
= mBag
.size();
3036 for (size_t i
=0; i
<N
; i
++) {
3037 const String16
& key
= mBag
.keyAt(i
);
3038 const Item
& it
= mBag
.valueAt(i
);
3040 if (!table
->hasBagOrEntry(key
, &id16
, &package
)) {
3041 String16
value("false");
3042 status_t err
= table
->addEntry(SourcePos(String8("<generated>"), 0), package
,
3044 if (err
!= NO_ERROR
) {
3048 } else if (!table
->hasBagOrEntry(key
, &attr16
, &package
)) {
3051 // fprintf(stderr, "ERROR: Bag attribute '%s' has not been defined.\n",
3052 // String8(key).string());
3053 // const Item& item(mBag.valueAt(i));
3054 // fprintf(stderr, "Referenced from file %s line %d\n",
3055 // item.sourcePos.file.string(), item.sourcePos.line);
3056 // return UNKNOWN_ERROR;
3059 sprintf(numberStr
, "%d", ResTable_map::TYPE_ANY
);
3060 status_t err
= table
->addBag(SourcePos("<generated>", 0), package
,
3061 attr16
, key
, String16(""),
3063 String16(numberStr
), NULL
, NULL
);
3064 if (err
!= NO_ERROR
) {
3073 status_t
ResourceTable::Entry::assignResourceIds(ResourceTable
* table
,
3074 const String16
& package
)
3076 bool hasErrors
= false;
3078 if (mType
== TYPE_BAG
) {
3079 const char* errorMsg
;
3080 const String16
style16("style");
3081 const String16
attr16("attr");
3082 const String16
id16("id");
3084 if (mParent
.size() > 0) {
3085 mParentId
= table
->getResId(mParent
, &style16
, NULL
, &errorMsg
);
3086 if (mParentId
== 0) {
3087 mPos
.error("Error retrieving parent for item: %s '%s'.\n",
3088 errorMsg
, String8(mParent
).string());
3092 const size_t N
= mBag
.size();
3093 for (size_t i
=0; i
<N
; i
++) {
3094 const String16
& key
= mBag
.keyAt(i
);
3095 Item
& it
= mBag
.editValueAt(i
);
3096 it
.bagKeyId
= table
->getResId(key
,
3097 it
.isId
? &id16
: &attr16
, NULL
, &errorMsg
);
3098 //printf("Bag key of %s: #%08x\n", String8(key).string(), it.bagKeyId);
3099 if (it
.bagKeyId
== 0) {
3100 it
.sourcePos
.error("Error: %s: %s '%s'.\n", errorMsg
,
3101 String8(it
.isId
? id16
: attr16
).string(),
3102 String8(key
).string());
3107 return hasErrors
? UNKNOWN_ERROR
: NO_ERROR
;
3110 status_t
ResourceTable::Entry::prepareFlatten(StringPool
* strings
, ResourceTable
* table
)
3112 if (mType
== TYPE_ITEM
) {
3114 AccessorCookie
ac(it
.sourcePos
, String8(mName
), String8(it
.value
));
3115 if (!table
->stringToValue(&it
.parsedValue
, strings
,
3116 it
.value
, false, true, 0,
3117 &it
.style
, NULL
, &ac
, mItemFormat
)) {
3118 return UNKNOWN_ERROR
;
3120 } else if (mType
== TYPE_BAG
) {
3121 const size_t N
= mBag
.size();
3122 for (size_t i
=0; i
<N
; i
++) {
3123 const String16
& key
= mBag
.keyAt(i
);
3124 Item
& it
= mBag
.editValueAt(i
);
3125 AccessorCookie
ac(it
.sourcePos
, String8(key
), String8(it
.value
));
3126 if (!table
->stringToValue(&it
.parsedValue
, strings
,
3127 it
.value
, false, true, it
.bagKeyId
,
3128 &it
.style
, NULL
, &ac
, it
.format
)) {
3129 return UNKNOWN_ERROR
;
3133 mPos
.error("Error: entry %s is not a single item or a bag.\n",
3134 String8(mName
).string());
3135 return UNKNOWN_ERROR
;
3140 ssize_t
ResourceTable::Entry::flatten(Bundle
* bundle
, const sp
<AaptFile
>& data
, bool isPublic
)
3143 ResTable_entry header
;
3144 memset(&header
, 0, sizeof(header
));
3145 header
.size
= htods(sizeof(header
));
3146 const type ty
= this != NULL
? mType
: TYPE_ITEM
;
3148 if (ty
== TYPE_BAG
) {
3149 header
.flags
|= htods(header
.FLAG_COMPLEX
);
3152 header
.flags
|= htods(header
.FLAG_PUBLIC
);
3154 header
.key
.index
= htodl(mNameIndex
);
3156 if (ty
!= TYPE_BAG
) {
3157 status_t err
= data
->writeData(&header
, sizeof(header
));
3158 if (err
!= NO_ERROR
) {
3159 fprintf(stderr
, "ERROR: out of memory creating ResTable_entry\n");
3163 const Item
& it
= mItem
;
3165 memset(&par
, 0, sizeof(par
));
3166 par
.size
= htods(it
.parsedValue
.size
);
3167 par
.dataType
= it
.parsedValue
.dataType
;
3168 par
.res0
= it
.parsedValue
.res0
;
3169 par
.data
= htodl(it
.parsedValue
.data
);
3171 printf("Writing item (%s): type=%d, data=0x%x, res0=0x%x\n",
3172 String8(mName
).string(), it
.parsedValue
.dataType
,
3173 it
.parsedValue
.data
, par
.res0
);
3175 err
= data
->writeData(&par
, it
.parsedValue
.size
);
3176 if (err
!= NO_ERROR
) {
3177 fprintf(stderr
, "ERROR: out of memory creating Res_value\n");
3180 amt
+= it
.parsedValue
.size
;
3182 size_t N
= mBag
.size();
3184 // Create correct ordering of items.
3185 KeyedVector
<uint32_t, const Item
*> items
;
3186 for (i
=0; i
<N
; i
++) {
3187 const Item
& it
= mBag
.valueAt(i
);
3188 items
.add(it
.bagKeyId
, &it
);
3192 ResTable_map_entry mapHeader
;
3193 memcpy(&mapHeader
, &header
, sizeof(header
));
3194 mapHeader
.size
= htods(sizeof(mapHeader
));
3195 mapHeader
.parent
.ident
= htodl(mParentId
);
3196 mapHeader
.count
= htodl(N
);
3197 status_t err
= data
->writeData(&mapHeader
, sizeof(mapHeader
));
3198 if (err
!= NO_ERROR
) {
3199 fprintf(stderr
, "ERROR: out of memory creating ResTable_entry\n");
3203 for (i
=0; i
<N
; i
++) {
3204 const Item
& it
= *items
.valueAt(i
);
3206 map
.name
.ident
= htodl(it
.bagKeyId
);
3207 map
.value
.size
= htods(it
.parsedValue
.size
);
3208 map
.value
.dataType
= it
.parsedValue
.dataType
;
3209 map
.value
.res0
= it
.parsedValue
.res0
;
3210 map
.value
.data
= htodl(it
.parsedValue
.data
);
3211 err
= data
->writeData(&map
, sizeof(map
));
3212 if (err
!= NO_ERROR
) {
3213 fprintf(stderr
, "ERROR: out of memory creating Res_value\n");
3222 void ResourceTable::ConfigList::appendComment(const String16
& comment
,
3225 if (comment
.size() <= 0) {
3228 if (onlyIfEmpty
&& mComment
.size() > 0) {
3231 if (mComment
.size() > 0) {
3232 mComment
.append(String16("\n"));
3234 mComment
.append(comment
);
3237 void ResourceTable::ConfigList::appendTypeComment(const String16
& comment
)
3239 if (comment
.size() <= 0) {
3242 if (mTypeComment
.size() > 0) {
3243 mTypeComment
.append(String16("\n"));
3245 mTypeComment
.append(comment
);
3248 status_t
ResourceTable::Type::addPublic(const SourcePos
& sourcePos
,
3249 const String16
& name
,
3250 const uint32_t ident
)
3253 int32_t entryIdx
= Res_GETENTRY(ident
);
3255 sourcePos
.error("Public resource %s/%s has an invalid 0 identifier (0x%08x).\n",
3256 String8(mName
).string(), String8(name
).string(), ident
);
3257 return UNKNOWN_ERROR
;
3261 int32_t typeIdx
= Res_GETTYPE(ident
);
3264 if (mPublicIndex
> 0 && mPublicIndex
!= typeIdx
) {
3265 sourcePos
.error("Public resource %s/%s has conflicting type codes for its"
3266 " public identifiers (0x%x vs 0x%x).\n",
3267 String8(mName
).string(), String8(name
).string(),
3268 mPublicIndex
, typeIdx
);
3269 return UNKNOWN_ERROR
;
3271 mPublicIndex
= typeIdx
;
3274 if (mFirstPublicSourcePos
== NULL
) {
3275 mFirstPublicSourcePos
= new SourcePos(sourcePos
);
3278 if (mPublic
.indexOfKey(name
) < 0) {
3279 mPublic
.add(name
, Public(sourcePos
, String16(), ident
));
3281 Public
& p
= mPublic
.editValueFor(name
);
3282 if (p
.ident
!= ident
) {
3283 sourcePos
.error("Public resource %s/%s has conflicting public identifiers"
3284 " (0x%08x vs 0x%08x).\n"
3285 "%s:%d: Originally defined here.\n",
3286 String8(mName
).string(), String8(name
).string(), p
.ident
, ident
,
3287 p
.sourcePos
.file
.string(), p
.sourcePos
.line
);
3288 return UNKNOWN_ERROR
;
3295 void ResourceTable::Type::canAddEntry(const String16
& name
)
3297 mCanAddEntries
.add(name
);
3300 sp
<ResourceTable::Entry
> ResourceTable::Type::getEntry(const String16
& entry
,
3301 const SourcePos
& sourcePos
,
3302 const ResTable_config
* config
,
3305 bool autoAddOverlay
)
3308 sp
<ConfigList
> c
= mConfigs
.valueFor(entry
);
3310 if (overlay
&& !autoAddOverlay
&& mCanAddEntries
.indexOf(entry
) < 0) {
3311 sourcePos
.error("Resource at %s appears in overlay but not"
3312 " in the base package; use <add-resource> to add.\n",
3313 String8(entry
).string());
3316 c
= new ConfigList(entry
, sourcePos
);
3317 mConfigs
.add(entry
, c
);
3318 pos
= (int)mOrderedConfigs
.size();
3319 mOrderedConfigs
.add(c
);
3321 c
->setEntryIndex(pos
);
3325 ConfigDescription cdesc
;
3326 if (config
) cdesc
= *config
;
3328 sp
<Entry
> e
= c
->getEntries().valueFor(cdesc
);
3330 if (config
!= NULL
) {
3331 NOISY(printf("New entry at %s:%d: imsi:%d/%d lang:%c%c cnt:%c%c "
3332 "orien:%d touch:%d density:%d key:%d inp:%d nav:%d w:%d h:%d\n",
3333 sourcePos
.file
.string(), sourcePos
.line
,
3334 config
->mcc
, config
->mnc
,
3335 config
->language
[0] ? config
->language
[0] : '-',
3336 config
->language
[1] ? config
->language
[1] : '-',
3337 config
->country
[0] ? config
->country
[0] : '-',
3338 config
->country
[1] ? config
->country
[1] : '-',
3339 config
->orientation
,
3340 config
->touchscreen
,
3345 config
->screenWidth
,
3346 config
->screenHeight
));
3348 NOISY(printf("New entry at %s:%d: NULL config\n",
3349 sourcePos
.file
.string(), sourcePos
.line
));
3351 e
= new Entry(entry
, sourcePos
);
3352 c
->addEntry(cdesc
, e
);
3356 for (pos=0; pos<(int)mOrderedConfigs.size(); pos++) {
3357 if (mOrderedConfigs[pos] == c) {
3361 if (pos >= (int)mOrderedConfigs.size()) {
3362 sourcePos.error("Internal error: config not found in mOrderedConfigs when adding entry");
3366 e->setEntryIndex(pos);
3371 mUniqueConfigs
.add(cdesc
);
3376 status_t
ResourceTable::Type::applyPublicEntryOrder()
3378 size_t N
= mOrderedConfigs
.size();
3379 Vector
<sp
<ConfigList
> > origOrder(mOrderedConfigs
);
3380 bool hasError
= false;
3383 for (i
=0; i
<N
; i
++) {
3384 mOrderedConfigs
.replaceAt(NULL
, i
);
3387 const size_t NP
= mPublic
.size();
3388 //printf("Ordering %d configs from %d public defs\n", N, NP);
3390 for (j
=0; j
<NP
; j
++) {
3391 const String16
& name
= mPublic
.keyAt(j
);
3392 const Public
& p
= mPublic
.valueAt(j
);
3393 int32_t idx
= Res_GETENTRY(p
.ident
);
3394 //printf("Looking for entry \"%s\"/\"%s\" (0x%08x) in %d...\n",
3395 // String8(mName).string(), String8(name).string(), p.ident, N);
3397 for (i
=0; i
<N
; i
++) {
3398 sp
<ConfigList
> e
= origOrder
.itemAt(i
);
3399 //printf("#%d: \"%s\"\n", i, String8(e->getName()).string());
3400 if (e
->getName() == name
) {
3401 if (idx
>= (int32_t)mOrderedConfigs
.size()) {
3402 p
.sourcePos
.error("Public entry identifier 0x%x entry index "
3403 "is larger than available symbols (index %d, total symbols %d).\n",
3404 p
.ident
, idx
, mOrderedConfigs
.size());
3406 } else if (mOrderedConfigs
.itemAt(idx
) == NULL
) {
3408 e
->setPublicSourcePos(p
.sourcePos
);
3409 mOrderedConfigs
.replaceAt(e
, idx
);
3410 origOrder
.removeAt(i
);
3415 sp
<ConfigList
> oe
= mOrderedConfigs
.itemAt(idx
);
3417 p
.sourcePos
.error("Multiple entry names declared for public entry"
3418 " identifier 0x%x in type %s (%s vs %s).\n"
3419 "%s:%d: Originally defined here.",
3420 idx
+1, String8(mName
).string(),
3421 String8(oe
->getName()).string(),
3422 String8(name
).string(),
3423 oe
->getPublicSourcePos().file
.string(),
3424 oe
->getPublicSourcePos().line
);
3431 p
.sourcePos
.error("Public symbol %s/%s declared here is not defined.",
3432 String8(mName
).string(), String8(name
).string());
3437 //printf("Copying back in %d non-public configs, have %d\n", N, origOrder.size());
3439 if (N
!= origOrder
.size()) {
3440 printf("Internal error: remaining private symbol count mismatch\n");
3441 N
= origOrder
.size();
3445 for (i
=0; i
<N
; i
++) {
3446 sp
<ConfigList
> e
= origOrder
.itemAt(i
);
3447 // There will always be enough room for the remaining entries.
3448 while (mOrderedConfigs
.itemAt(j
) != NULL
) {
3451 mOrderedConfigs
.replaceAt(e
, j
);
3455 return hasError
? UNKNOWN_ERROR
: NO_ERROR
;
3458 ResourceTable::Package::Package(const String16
& name
, ssize_t includedId
)
3459 : mName(name
), mIncludedId(includedId
),
3460 mTypeStringsMapping(0xffffffff),
3461 mKeyStringsMapping(0xffffffff)
3465 sp
<ResourceTable::Type
> ResourceTable::Package::getType(const String16
& type
,
3466 const SourcePos
& sourcePos
,
3469 sp
<Type
> t
= mTypes
.valueFor(type
);
3471 t
= new Type(type
, sourcePos
);
3472 mTypes
.add(type
, t
);
3473 mOrderedTypes
.add(t
);
3475 // For some reason the type's index is set to one plus the index
3476 // in the mOrderedTypes list, rather than just the index.
3477 t
->setIndex(mOrderedTypes
.size());
3483 status_t
ResourceTable::Package::setTypeStrings(const sp
<AaptFile
>& data
)
3485 mTypeStringsData
= data
;
3486 status_t err
= setStrings(data
, &mTypeStrings
, &mTypeStringsMapping
);
3487 if (err
!= NO_ERROR
) {
3488 fprintf(stderr
, "ERROR: Type string data is corrupt!\n");
3493 status_t
ResourceTable::Package::setKeyStrings(const sp
<AaptFile
>& data
)
3495 mKeyStringsData
= data
;
3496 status_t err
= setStrings(data
, &mKeyStrings
, &mKeyStringsMapping
);
3497 if (err
!= NO_ERROR
) {
3498 fprintf(stderr
, "ERROR: Key string data is corrupt!\n");
3503 status_t
ResourceTable::Package::setStrings(const sp
<AaptFile
>& data
,
3504 ResStringPool
* strings
,
3505 DefaultKeyedVector
<String16
, uint32_t>* mappings
)
3507 if (data
->getData() == NULL
) {
3508 return UNKNOWN_ERROR
;
3511 NOISY(aout
<< "Setting restable string pool: "
3512 << HexDump(data
->getData(), data
->getSize()) << endl
);
3514 status_t err
= strings
->setTo(data
->getData(), data
->getSize());
3515 if (err
== NO_ERROR
) {
3516 const size_t N
= strings
->size();
3517 for (size_t i
=0; i
<N
; i
++) {
3519 mappings
->add(String16(strings
->stringAt(i
, &len
)), i
);
3525 status_t
ResourceTable::Package::applyPublicTypeOrder()
3527 size_t N
= mOrderedTypes
.size();
3528 Vector
<sp
<Type
> > origOrder(mOrderedTypes
);
3531 for (i
=0; i
<N
; i
++) {
3532 mOrderedTypes
.replaceAt(NULL
, i
);
3535 for (i
=0; i
<N
; i
++) {
3536 sp
<Type
> t
= origOrder
.itemAt(i
);
3537 int32_t idx
= t
->getPublicIndex();
3540 while (idx
>= (int32_t)mOrderedTypes
.size()) {
3541 mOrderedTypes
.add();
3543 if (mOrderedTypes
.itemAt(idx
) != NULL
) {
3544 sp
<Type
> ot
= mOrderedTypes
.itemAt(idx
);
3545 t
->getFirstPublicSourcePos().error("Multiple type names declared for public type"
3546 " identifier 0x%x (%s vs %s).\n"
3547 "%s:%d: Originally defined here.",
3548 idx
, String8(ot
->getName()).string(),
3549 String8(t
->getName()).string(),
3550 ot
->getFirstPublicSourcePos().file
.string(),
3551 ot
->getFirstPublicSourcePos().line
);
3552 return UNKNOWN_ERROR
;
3554 mOrderedTypes
.replaceAt(t
, idx
);
3555 origOrder
.removeAt(i
);
3562 for (i
=0; i
<N
; i
++) {
3563 sp
<Type
> t
= origOrder
.itemAt(i
);
3564 // There will always be enough room for the remaining types.
3565 while (mOrderedTypes
.itemAt(j
) != NULL
) {
3568 mOrderedTypes
.replaceAt(t
, j
);
3574 sp
<ResourceTable::Package
> ResourceTable::getPackage(const String16
& package
)
3576 sp
<Package
> p
= mPackages
.valueFor(package
);
3578 if (mIsAppPackage
) {
3579 if (mHaveAppPackage
) {
3580 fprintf(stderr
, "Adding multiple application package resources; only one is allowed.\n"
3581 "Use -x to create extended resources.\n");
3584 mHaveAppPackage
= true;
3585 p
= new Package(package
, 127);
3587 p
= new Package(package
, mNextPackageId
);
3589 //printf("*** NEW PACKAGE: \"%s\" id=%d\n",
3590 // String8(package).string(), p->getAssignedId());
3591 mPackages
.add(package
, p
);
3592 mOrderedPackages
.add(p
);
3598 sp
<ResourceTable::Type
> ResourceTable::getType(const String16
& package
,
3599 const String16
& type
,
3600 const SourcePos
& sourcePos
,
3603 sp
<Package
> p
= getPackage(package
);
3607 return p
->getType(type
, sourcePos
, doSetIndex
);
3610 sp
<ResourceTable::Entry
> ResourceTable::getEntry(const String16
& package
,
3611 const String16
& type
,
3612 const String16
& name
,
3613 const SourcePos
& sourcePos
,
3615 const ResTable_config
* config
,
3618 sp
<Type
> t
= getType(package
, type
, sourcePos
, doSetIndex
);
3622 return t
->getEntry(name
, sourcePos
, config
, doSetIndex
, overlay
, mBundle
->getAutoAddOverlay());
3625 sp
<const ResourceTable::Entry
> ResourceTable::getEntry(uint32_t resID
,
3626 const ResTable_config
* config
) const
3628 int pid
= Res_GETPACKAGE(resID
)+1;
3629 const size_t N
= mOrderedPackages
.size();
3632 for (i
=0; i
<N
; i
++) {
3633 sp
<Package
> check
= mOrderedPackages
[i
];
3634 if (check
->getAssignedId() == pid
) {
3641 fprintf(stderr
, "warning: Package not found for resource #%08x\n", resID
);
3645 int tid
= Res_GETTYPE(resID
);
3646 if (tid
< 0 || tid
>= (int)p
->getOrderedTypes().size()) {
3647 fprintf(stderr
, "warning: Type not found for resource #%08x\n", resID
);
3650 sp
<Type
> t
= p
->getOrderedTypes()[tid
];
3652 int eid
= Res_GETENTRY(resID
);
3653 if (eid
< 0 || eid
>= (int)t
->getOrderedConfigs().size()) {
3654 fprintf(stderr
, "warning: Entry not found for resource #%08x\n", resID
);
3658 sp
<ConfigList
> c
= t
->getOrderedConfigs()[eid
];
3660 fprintf(stderr
, "warning: Entry not found for resource #%08x\n", resID
);
3664 ConfigDescription cdesc
;
3665 if (config
) cdesc
= *config
;
3666 sp
<Entry
> e
= c
->getEntries().valueFor(cdesc
);
3668 fprintf(stderr
, "warning: Entry configuration not found for resource #%08x\n", resID
);
3675 const ResourceTable::Item
* ResourceTable::getItem(uint32_t resID
, uint32_t attrID
) const
3677 sp
<const Entry
> e
= getEntry(resID
);
3682 const size_t N
= e
->getBag().size();
3683 for (size_t i
=0; i
<N
; i
++) {
3684 const Item
& it
= e
->getBag().valueAt(i
);
3685 if (it
.bagKeyId
== 0) {
3686 fprintf(stderr
, "warning: ID not yet assigned to '%s' in bag '%s'\n",
3687 String8(e
->getName()).string(),
3688 String8(e
->getBag().keyAt(i
)).string());
3690 if (it
.bagKeyId
== attrID
) {
3698 bool ResourceTable::getItemValue(
3699 uint32_t resID
, uint32_t attrID
, Res_value
* outValue
)
3701 const Item
* item
= getItem(resID
, attrID
);
3705 if (item
->evaluating
) {
3706 sp
<const Entry
> e
= getEntry(resID
);
3707 const size_t N
= e
->getBag().size();
3709 for (i
=0; i
<N
; i
++) {
3710 if (&e
->getBag().valueAt(i
) == item
) {
3714 fprintf(stderr
, "warning: Circular reference detected in key '%s' of bag '%s'\n",
3715 String8(e
->getName()).string(),
3716 String8(e
->getBag().keyAt(i
)).string());
3719 item
->evaluating
= true;
3720 res
= stringToValue(outValue
, NULL
, item
->value
, false, false, item
->bagKeyId
);
3723 printf("getItemValue of #%08x[#%08x] (%s): type=#%08x, data=#%08x\n",
3724 resID
, attrID
, String8(getEntry(resID
)->getName()).string(),
3725 outValue
->dataType
, outValue
->data
);
3727 printf("getItemValue of #%08x[#%08x]: failed\n",
3731 item
->evaluating
= false;