]>
Commit | Line | Data |
---|---|---|
dadd9c1f TAOSP |
1 | // |
2 | // Copyright 2006 The Android Open Source Project | |
3 | // | |
4 | // Build resource files from raw assets. | |
5 | // | |
6 | ||
7 | #include "ResourceTable.h" | |
8 | ||
9 | #include "XMLNode.h" | |
10 | ||
11 | #include <utils/ByteOrder.h> | |
12 | #include <utils/ResourceTypes.h> | |
13 | #include <stdarg.h> | |
14 | ||
15 | #define NOISY(x) //x | |
16 | ||
17 | status_t compileXmlFile(const sp<AaptAssets>& assets, | |
18 | const sp<AaptFile>& target, | |
19 | ResourceTable* table, | |
20 | int options) | |
21 | { | |
22 | sp<XMLNode> root = XMLNode::parse(target); | |
23 | if (root == NULL) { | |
24 | return UNKNOWN_ERROR; | |
25 | } | |
26 | if ((options&XML_COMPILE_STRIP_WHITESPACE) != 0) { | |
27 | root->removeWhitespace(true, NULL); | |
28 | } else if ((options&XML_COMPILE_COMPACT_WHITESPACE) != 0) { | |
29 | root->removeWhitespace(false, NULL); | |
30 | } | |
31 | ||
32 | bool hasErrors = false; | |
33 | ||
34 | if ((options&XML_COMPILE_ASSIGN_ATTRIBUTE_IDS) != 0) { | |
35 | status_t err = root->assignResourceIds(assets, table); | |
36 | if (err != NO_ERROR) { | |
37 | hasErrors = true; | |
38 | } | |
39 | } | |
40 | ||
41 | status_t err = root->parseValues(assets, table); | |
42 | if (err != NO_ERROR) { | |
43 | hasErrors = true; | |
44 | } | |
45 | ||
46 | if (hasErrors) { | |
47 | return UNKNOWN_ERROR; | |
48 | } | |
49 | ||
50 | NOISY(printf("Input XML Resource:\n")); | |
51 | NOISY(root->print()); | |
52 | err = root->flatten(target, | |
53 | (options&XML_COMPILE_STRIP_COMMENTS) != 0, | |
54 | (options&XML_COMPILE_STRIP_RAW_VALUES) != 0); | |
55 | if (err != NO_ERROR) { | |
56 | return err; | |
57 | } | |
58 | ||
59 | NOISY(printf("Output XML Resource:\n")); | |
60 | NOISY(ResXMLTree tree; | |
61 | tree.setTo(target->getData(), target->getSize()); | |
62 | printXMLBlock(&tree)); | |
63 | ||
64 | target->setCompressionMethod(ZipEntry::kCompressDeflated); | |
65 | ||
66 | return err; | |
67 | } | |
68 | ||
69 | #undef NOISY | |
70 | #define NOISY(x) //x | |
71 | ||
72 | struct flag_entry | |
73 | { | |
74 | const char16_t* name; | |
75 | size_t nameLen; | |
76 | uint32_t value; | |
77 | const char* description; | |
78 | }; | |
79 | ||
80 | static const char16_t referenceArray[] = | |
81 | { 'r', 'e', 'f', 'e', 'r', 'e', 'n', 'c', 'e' }; | |
82 | static const char16_t stringArray[] = | |
83 | { 's', 't', 'r', 'i', 'n', 'g' }; | |
84 | static const char16_t integerArray[] = | |
85 | { 'i', 'n', 't', 'e', 'g', 'e', 'r' }; | |
86 | static const char16_t booleanArray[] = | |
87 | { 'b', 'o', 'o', 'l', 'e', 'a', 'n' }; | |
88 | static const char16_t colorArray[] = | |
89 | { 'c', 'o', 'l', 'o', 'r' }; | |
90 | static const char16_t floatArray[] = | |
91 | { 'f', 'l', 'o', 'a', 't' }; | |
92 | static const char16_t dimensionArray[] = | |
93 | { 'd', 'i', 'm', 'e', 'n', 's', 'i', 'o', 'n' }; | |
94 | static const char16_t fractionArray[] = | |
95 | { 'f', 'r', 'a', 'c', 't', 'i', 'o', 'n' }; | |
96 | static const char16_t enumArray[] = | |
97 | { 'e', 'n', 'u', 'm' }; | |
98 | static const char16_t flagsArray[] = | |
99 | { 'f', 'l', 'a', 'g', 's' }; | |
100 | ||
101 | static const flag_entry gFormatFlags[] = { | |
102 | { referenceArray, sizeof(referenceArray)/2, ResTable_map::TYPE_REFERENCE, | |
103 | "a reference to another resource, in the form \"<code>@[+][<i>package</i>:]<i>type</i>:<i>name</i></code>\"\n" | |
104 | "or to a theme attribute in the form \"<code>?[<i>package</i>:][<i>type</i>:]<i>name</i></code>\"."}, | |
105 | { stringArray, sizeof(stringArray)/2, ResTable_map::TYPE_STRING, | |
106 | "a string value, using '\\\\;' to escape characters such as '\\\\n' or '\\\\uxxxx' for a unicode character." }, | |
107 | { integerArray, sizeof(integerArray)/2, ResTable_map::TYPE_INTEGER, | |
108 | "an integer value, such as \"<code>100</code>\"." }, | |
109 | { booleanArray, sizeof(booleanArray)/2, ResTable_map::TYPE_BOOLEAN, | |
110 | "a boolean value, either \"<code>true</code>\" or \"<code>false</code>\"." }, | |
111 | { colorArray, sizeof(colorArray)/2, ResTable_map::TYPE_COLOR, | |
112 | "a color value, in the form of \"<code>#<i>rgb</i></code>\", \"<code>#<i>argb</i></code>\",\n" | |
113 | "\"<code>#<i>rrggbb</i></code>\", or \"<code>#<i>aarrggbb</i></code>\"." }, | |
114 | { floatArray, sizeof(floatArray)/2, ResTable_map::TYPE_FLOAT, | |
115 | "a floating point value, such as \"<code>1.2</code>\"."}, | |
116 | { dimensionArray, sizeof(dimensionArray)/2, ResTable_map::TYPE_DIMENSION, | |
117 | "a dimension value, which is a floating point number appended with a unit such as \"<code>14.5sp</code>\".\n" | |
118 | "Available units are: px (pixels), db (density-independent pixels), sp (scaled pixels based on preferred font size),\n" | |
119 | "in (inches), mm (millimeters)." }, | |
120 | { fractionArray, sizeof(fractionArray)/2, ResTable_map::TYPE_FRACTION, | |
121 | "a fractional value, which is a floating point number appended with either % or %p, such as \"<code>14.5%</code>\".\n" | |
122 | "The % suffix always means a percentage of the base size; the optional %p suffix provides a size relative to\n" | |
123 | "some parent container." }, | |
124 | { enumArray, sizeof(enumArray)/2, ResTable_map::TYPE_ENUM, NULL }, | |
125 | { flagsArray, sizeof(flagsArray)/2, ResTable_map::TYPE_FLAGS, NULL }, | |
126 | { NULL, 0, 0, NULL } | |
127 | }; | |
128 | ||
129 | static const char16_t suggestedArray[] = { 's', 'u', 'g', 'g', 'e', 's', 't', 'e', 'd' }; | |
130 | ||
131 | static const flag_entry l10nRequiredFlags[] = { | |
132 | { suggestedArray, sizeof(suggestedArray)/2, ResTable_map::L10N_SUGGESTED, NULL }, | |
133 | { NULL, 0, 0, NULL } | |
134 | }; | |
135 | ||
136 | static const char16_t nulStr[] = { 0 }; | |
137 | ||
138 | static uint32_t parse_flags(const char16_t* str, size_t len, | |
139 | const flag_entry* flags, bool* outError = NULL) | |
140 | { | |
141 | while (len > 0 && isspace(*str)) { | |
142 | str++; | |
143 | len--; | |
144 | } | |
145 | while (len > 0 && isspace(str[len-1])) { | |
146 | len--; | |
147 | } | |
148 | ||
149 | const char16_t* const end = str + len; | |
150 | uint32_t value = 0; | |
151 | ||
152 | while (str < end) { | |
153 | const char16_t* div = str; | |
154 | while (div < end && *div != '|') { | |
155 | div++; | |
156 | } | |
157 | ||
158 | const flag_entry* cur = flags; | |
159 | while (cur->name) { | |
160 | if (strzcmp16(cur->name, cur->nameLen, str, div-str) == 0) { | |
161 | value |= cur->value; | |
162 | break; | |
163 | } | |
164 | cur++; | |
165 | } | |
166 | ||
167 | if (!cur->name) { | |
168 | if (outError) *outError = true; | |
169 | return 0; | |
170 | } | |
171 | ||
172 | str = div < end ? div+1 : div; | |
173 | } | |
174 | ||
175 | if (outError) *outError = false; | |
176 | return value; | |
177 | } | |
178 | ||
179 | static String16 mayOrMust(int type, int flags) | |
180 | { | |
181 | if ((type&(~flags)) == 0) { | |
182 | return String16("<p>Must"); | |
183 | } | |
184 | ||
185 | return String16("<p>May"); | |
186 | } | |
187 | ||
188 | static void appendTypeInfo(ResourceTable* outTable, const String16& pkg, | |
189 | const String16& typeName, const String16& ident, int type, | |
190 | const flag_entry* flags) | |
191 | { | |
192 | bool hadType = false; | |
193 | while (flags->name) { | |
194 | if ((type&flags->value) != 0 && flags->description != NULL) { | |
195 | String16 fullMsg(mayOrMust(type, flags->value)); | |
196 | fullMsg.append(String16(" be ")); | |
197 | fullMsg.append(String16(flags->description)); | |
198 | outTable->appendTypeComment(pkg, typeName, ident, fullMsg); | |
199 | hadType = true; | |
200 | } | |
201 | flags++; | |
202 | } | |
203 | if (hadType && (type&ResTable_map::TYPE_REFERENCE) == 0) { | |
204 | outTable->appendTypeComment(pkg, typeName, ident, | |
205 | String16("<p>This may also be a reference to a resource (in the form\n" | |
206 | "\"<code>@[<i>package</i>:]<i>type</i>:<i>name</i></code>\") or\n" | |
207 | "theme attribute (in the form\n" | |
208 | "\"<code>?[<i>package</i>:][<i>type</i>:]<i>name</i></code>\")\n" | |
209 | "containing a value of this type.")); | |
210 | } | |
211 | } | |
212 | ||
213 | struct PendingAttribute | |
214 | { | |
215 | const String16 myPackage; | |
216 | const SourcePos sourcePos; | |
217 | const bool appendComment; | |
218 | int32_t type; | |
219 | String16 ident; | |
220 | String16 comment; | |
221 | bool hasErrors; | |
222 | bool added; | |
223 | ||
224 | PendingAttribute(String16 _package, const sp<AaptFile>& in, | |
225 | ResXMLTree& block, bool _appendComment) | |
226 | : myPackage(_package) | |
227 | , sourcePos(in->getPrintableSource(), block.getLineNumber()) | |
228 | , appendComment(_appendComment) | |
229 | , type(ResTable_map::TYPE_ANY) | |
230 | , hasErrors(false) | |
231 | , added(false) | |
232 | { | |
233 | } | |
234 | ||
235 | status_t createIfNeeded(ResourceTable* outTable) | |
236 | { | |
237 | if (added || hasErrors) { | |
238 | return NO_ERROR; | |
239 | } | |
240 | added = true; | |
241 | ||
242 | String16 attr16("attr"); | |
243 | ||
244 | if (outTable->hasBagOrEntry(myPackage, attr16, ident)) { | |
245 | sourcePos.error("Attribute \"%s\" has already been defined\n", | |
246 | String8(ident).string()); | |
247 | hasErrors = true; | |
248 | return UNKNOWN_ERROR; | |
249 | } | |
250 | ||
251 | char numberStr[16]; | |
252 | sprintf(numberStr, "%d", type); | |
253 | status_t err = outTable->addBag(sourcePos, myPackage, | |
254 | attr16, ident, String16(""), | |
255 | String16("^type"), | |
256 | String16(numberStr), NULL, NULL); | |
257 | if (err != NO_ERROR) { | |
258 | hasErrors = true; | |
259 | return err; | |
260 | } | |
261 | outTable->appendComment(myPackage, attr16, ident, comment, appendComment); | |
262 | //printf("Attribute %s comment: %s\n", String8(ident).string(), | |
263 | // String8(comment).string()); | |
264 | return err; | |
265 | } | |
266 | }; | |
267 | ||
268 | static status_t compileAttribute(const sp<AaptFile>& in, | |
269 | ResXMLTree& block, | |
270 | const String16& myPackage, | |
271 | ResourceTable* outTable, | |
272 | String16* outIdent = NULL, | |
273 | bool inStyleable = false) | |
274 | { | |
275 | PendingAttribute attr(myPackage, in, block, inStyleable); | |
276 | ||
277 | const String16 attr16("attr"); | |
278 | const String16 id16("id"); | |
279 | ||
280 | // Attribute type constants. | |
281 | const String16 enum16("enum"); | |
282 | const String16 flag16("flag"); | |
283 | ||
284 | ResXMLTree::event_code_t code; | |
285 | size_t len; | |
286 | status_t err; | |
287 | ||
288 | ssize_t identIdx = block.indexOfAttribute(NULL, "name"); | |
289 | if (identIdx >= 0) { | |
290 | attr.ident = String16(block.getAttributeStringValue(identIdx, &len)); | |
291 | if (outIdent) { | |
292 | *outIdent = attr.ident; | |
293 | } | |
294 | } else { | |
295 | attr.sourcePos.error("A 'name' attribute is required for <attr>\n"); | |
296 | attr.hasErrors = true; | |
297 | } | |
298 | ||
299 | attr.comment = String16( | |
300 | block.getComment(&len) ? block.getComment(&len) : nulStr); | |
301 | ||
302 | ssize_t typeIdx = block.indexOfAttribute(NULL, "format"); | |
303 | if (typeIdx >= 0) { | |
304 | String16 typeStr = String16(block.getAttributeStringValue(typeIdx, &len)); | |
305 | attr.type = parse_flags(typeStr.string(), typeStr.size(), gFormatFlags); | |
306 | if (attr.type == 0) { | |
307 | attr.sourcePos.error("Tag <attr> 'format' attribute value \"%s\" not valid\n", | |
308 | String8(typeStr).string()); | |
309 | attr.hasErrors = true; | |
310 | } | |
311 | attr.createIfNeeded(outTable); | |
312 | } else if (!inStyleable) { | |
313 | // Attribute definitions outside of styleables always define the | |
314 | // attribute as a generic value. | |
315 | attr.createIfNeeded(outTable); | |
316 | } | |
317 | ||
318 | //printf("Attribute %s: type=0x%08x\n", String8(attr.ident).string(), attr.type); | |
319 | ||
320 | ssize_t minIdx = block.indexOfAttribute(NULL, "min"); | |
321 | if (minIdx >= 0) { | |
322 | String16 val = String16(block.getAttributeStringValue(minIdx, &len)); | |
323 | if (!ResTable::stringToInt(val.string(), val.size(), NULL)) { | |
324 | attr.sourcePos.error("Tag <attr> 'min' attribute must be a number, not \"%s\"\n", | |
325 | String8(val).string()); | |
326 | attr.hasErrors = true; | |
327 | } | |
328 | attr.createIfNeeded(outTable); | |
329 | if (!attr.hasErrors) { | |
330 | err = outTable->addBag(attr.sourcePos, myPackage, attr16, attr.ident, | |
331 | String16(""), String16("^min"), String16(val), NULL, NULL); | |
332 | if (err != NO_ERROR) { | |
333 | attr.hasErrors = true; | |
334 | } | |
335 | } | |
336 | } | |
337 | ||
338 | ssize_t maxIdx = block.indexOfAttribute(NULL, "max"); | |
339 | if (maxIdx >= 0) { | |
340 | String16 val = String16(block.getAttributeStringValue(maxIdx, &len)); | |
341 | if (!ResTable::stringToInt(val.string(), val.size(), NULL)) { | |
342 | attr.sourcePos.error("Tag <attr> 'max' attribute must be a number, not \"%s\"\n", | |
343 | String8(val).string()); | |
344 | attr.hasErrors = true; | |
345 | } | |
346 | attr.createIfNeeded(outTable); | |
347 | if (!attr.hasErrors) { | |
348 | err = outTable->addBag(attr.sourcePos, myPackage, attr16, attr.ident, | |
349 | String16(""), String16("^max"), String16(val), NULL, NULL); | |
350 | attr.hasErrors = true; | |
351 | } | |
352 | } | |
353 | ||
354 | if ((minIdx >= 0 || maxIdx >= 0) && (attr.type&ResTable_map::TYPE_INTEGER) == 0) { | |
355 | attr.sourcePos.error("Tag <attr> must have format=integer attribute if using max or min\n"); | |
356 | attr.hasErrors = true; | |
357 | } | |
358 | ||
359 | ssize_t l10nIdx = block.indexOfAttribute(NULL, "localization"); | |
360 | if (l10nIdx >= 0) { | |
361 | const uint16_t* str = block.getAttributeStringValue(l10nIdx, &len); | |
362 | bool error; | |
363 | uint32_t l10n_required = parse_flags(str, len, l10nRequiredFlags, &error); | |
364 | if (error) { | |
365 | attr.sourcePos.error("Tag <attr> 'localization' attribute value \"%s\" not valid\n", | |
366 | String8(str).string()); | |
367 | attr.hasErrors = true; | |
368 | } | |
369 | attr.createIfNeeded(outTable); | |
370 | if (!attr.hasErrors) { | |
371 | char buf[10]; | |
372 | sprintf(buf, "%d", l10n_required); | |
373 | err = outTable->addBag(attr.sourcePos, myPackage, attr16, attr.ident, | |
374 | String16(""), String16("^l10n"), String16(buf), NULL, NULL); | |
375 | if (err != NO_ERROR) { | |
376 | attr.hasErrors = true; | |
377 | } | |
378 | } | |
379 | } | |
380 | ||
381 | String16 enumOrFlagsComment; | |
382 | ||
383 | while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) { | |
384 | if (code == ResXMLTree::START_TAG) { | |
385 | uint32_t localType = 0; | |
386 | if (strcmp16(block.getElementName(&len), enum16.string()) == 0) { | |
387 | localType = ResTable_map::TYPE_ENUM; | |
388 | } else if (strcmp16(block.getElementName(&len), flag16.string()) == 0) { | |
389 | localType = ResTable_map::TYPE_FLAGS; | |
390 | } else { | |
391 | SourcePos(in->getPrintableSource(), block.getLineNumber()) | |
392 | .error("Tag <%s> can not appear inside <attr>, only <enum> or <flag>\n", | |
393 | String8(block.getElementName(&len)).string()); | |
394 | return UNKNOWN_ERROR; | |
395 | } | |
396 | ||
397 | attr.createIfNeeded(outTable); | |
398 | ||
399 | if (attr.type == ResTable_map::TYPE_ANY) { | |
400 | // No type was explicitly stated, so supplying enum tags | |
401 | // implicitly creates an enum or flag. | |
402 | attr.type = 0; | |
403 | } | |
404 | ||
405 | if ((attr.type&(ResTable_map::TYPE_ENUM|ResTable_map::TYPE_FLAGS)) == 0) { | |
406 | // Wasn't originally specified as an enum, so update its type. | |
407 | attr.type |= localType; | |
408 | if (!attr.hasErrors) { | |
409 | char numberStr[16]; | |
410 | sprintf(numberStr, "%d", attr.type); | |
411 | err = outTable->addBag(SourcePos(in->getPrintableSource(), block.getLineNumber()), | |
412 | myPackage, attr16, attr.ident, String16(""), | |
413 | String16("^type"), String16(numberStr), NULL, NULL, true); | |
414 | if (err != NO_ERROR) { | |
415 | attr.hasErrors = true; | |
416 | } | |
417 | } | |
418 | } else if ((uint32_t)(attr.type&(ResTable_map::TYPE_ENUM|ResTable_map::TYPE_FLAGS)) != localType) { | |
419 | if (localType == ResTable_map::TYPE_ENUM) { | |
420 | SourcePos(in->getPrintableSource(), block.getLineNumber()) | |
421 | .error("<enum> attribute can not be used inside a flags format\n"); | |
422 | attr.hasErrors = true; | |
423 | } else { | |
424 | SourcePos(in->getPrintableSource(), block.getLineNumber()) | |
425 | .error("<flag> attribute can not be used inside a enum format\n"); | |
426 | attr.hasErrors = true; | |
427 | } | |
428 | } | |
429 | ||
430 | String16 itemIdent; | |
431 | ssize_t itemIdentIdx = block.indexOfAttribute(NULL, "name"); | |
432 | if (itemIdentIdx >= 0) { | |
433 | itemIdent = String16(block.getAttributeStringValue(itemIdentIdx, &len)); | |
434 | } else { | |
435 | SourcePos(in->getPrintableSource(), block.getLineNumber()) | |
436 | .error("A 'name' attribute is required for <enum> or <flag>\n"); | |
437 | attr.hasErrors = true; | |
438 | } | |
439 | ||
440 | String16 value; | |
441 | ssize_t valueIdx = block.indexOfAttribute(NULL, "value"); | |
442 | if (valueIdx >= 0) { | |
443 | value = String16(block.getAttributeStringValue(valueIdx, &len)); | |
444 | } else { | |
445 | SourcePos(in->getPrintableSource(), block.getLineNumber()) | |
446 | .error("A 'value' attribute is required for <enum> or <flag>\n"); | |
447 | attr.hasErrors = true; | |
448 | } | |
449 | if (!attr.hasErrors && !ResTable::stringToInt(value.string(), value.size(), NULL)) { | |
450 | SourcePos(in->getPrintableSource(), block.getLineNumber()) | |
451 | .error("Tag <enum> or <flag> 'value' attribute must be a number," | |
452 | " not \"%s\"\n", | |
453 | String8(value).string()); | |
454 | attr.hasErrors = true; | |
455 | } | |
456 | ||
457 | // Make sure an id is defined for this enum/flag identifier... | |
458 | if (!attr.hasErrors && !outTable->hasBagOrEntry(itemIdent, &id16, &myPackage)) { | |
459 | err = outTable->startBag(SourcePos(in->getPrintableSource(), block.getLineNumber()), | |
460 | myPackage, id16, itemIdent, String16(), NULL); | |
461 | if (err != NO_ERROR) { | |
462 | attr.hasErrors = true; | |
463 | } | |
464 | } | |
465 | ||
466 | if (!attr.hasErrors) { | |
467 | if (enumOrFlagsComment.size() == 0) { | |
468 | enumOrFlagsComment.append(mayOrMust(attr.type, | |
469 | ResTable_map::TYPE_ENUM|ResTable_map::TYPE_FLAGS)); | |
470 | enumOrFlagsComment.append((attr.type&ResTable_map::TYPE_ENUM) | |
471 | ? String16(" be one of the following constant values.") | |
472 | : String16(" be one or more (separated by '|') of the following constant values.")); | |
473 | enumOrFlagsComment.append(String16("</p>\n<table border=\"2\" width=\"85%\" align=\"center\" frame=\"hsides\" rules=\"all\" cellpadding=\"5\">\n" | |
474 | "<colgroup align=\"left\" />\n" | |
475 | "<colgroup align=\"left\" />\n" | |
476 | "<colgroup align=\"left\" />\n" | |
477 | "<tr><th>Constant<th>Value<th>Description</tr>")); | |
478 | } | |
479 | ||
480 | enumOrFlagsComment.append(String16("\n<tr><th><code>")); | |
481 | enumOrFlagsComment.append(itemIdent); | |
482 | enumOrFlagsComment.append(String16("</code><td>")); | |
483 | enumOrFlagsComment.append(value); | |
484 | enumOrFlagsComment.append(String16("<td>")); | |
485 | if (block.getComment(&len)) { | |
486 | enumOrFlagsComment.append(String16(block.getComment(&len))); | |
487 | } | |
488 | enumOrFlagsComment.append(String16("</tr>")); | |
489 | ||
490 | err = outTable->addBag(SourcePos(in->getPrintableSource(), block.getLineNumber()), | |
491 | myPackage, | |
492 | attr16, attr.ident, String16(""), | |
493 | itemIdent, value, NULL, NULL, false, true); | |
494 | if (err != NO_ERROR) { | |
495 | attr.hasErrors = true; | |
496 | } | |
497 | } | |
498 | } else if (code == ResXMLTree::END_TAG) { | |
499 | if (strcmp16(block.getElementName(&len), attr16.string()) == 0) { | |
500 | break; | |
501 | } | |
502 | if ((attr.type&ResTable_map::TYPE_ENUM) != 0) { | |
503 | if (strcmp16(block.getElementName(&len), enum16.string()) != 0) { | |
504 | SourcePos(in->getPrintableSource(), block.getLineNumber()) | |
505 | .error("Found tag </%s> where </enum> is expected\n", | |
506 | String8(block.getElementName(&len)).string()); | |
507 | return UNKNOWN_ERROR; | |
508 | } | |
509 | } else { | |
510 | if (strcmp16(block.getElementName(&len), flag16.string()) != 0) { | |
511 | SourcePos(in->getPrintableSource(), block.getLineNumber()) | |
512 | .error("Found tag </%s> where </flag> is expected\n", | |
513 | String8(block.getElementName(&len)).string()); | |
514 | return UNKNOWN_ERROR; | |
515 | } | |
516 | } | |
517 | } | |
518 | } | |
519 | ||
520 | if (!attr.hasErrors && attr.added) { | |
521 | appendTypeInfo(outTable, myPackage, attr16, attr.ident, attr.type, gFormatFlags); | |
522 | } | |
523 | ||
524 | if (!attr.hasErrors && enumOrFlagsComment.size() > 0) { | |
525 | enumOrFlagsComment.append(String16("\n</table>")); | |
526 | outTable->appendTypeComment(myPackage, attr16, attr.ident, enumOrFlagsComment); | |
527 | } | |
528 | ||
529 | ||
530 | return NO_ERROR; | |
531 | } | |
532 | ||
533 | bool localeIsDefined(const ResTable_config& config) | |
534 | { | |
535 | return config.locale == 0; | |
536 | } | |
537 | ||
538 | status_t parseAndAddBag(Bundle* bundle, | |
539 | const sp<AaptFile>& in, | |
540 | ResXMLTree* block, | |
541 | const ResTable_config& config, | |
542 | const String16& myPackage, | |
543 | const String16& curType, | |
544 | const String16& ident, | |
545 | const String16& parentIdent, | |
546 | const String16& itemIdent, | |
547 | int32_t curFormat, | |
548 | bool pseudolocalize, | |
549 | ResourceTable* outTable) | |
550 | { | |
551 | status_t err; | |
552 | const String16 item16("item"); | |
553 | ||
554 | String16 str; | |
555 | Vector<StringPool::entry_style_span> spans; | |
556 | err = parseStyledString(bundle, in->getPrintableSource().string(), | |
557 | block, item16, &str, &spans, | |
558 | pseudolocalize); | |
559 | if (err != NO_ERROR) { | |
560 | return err; | |
561 | } | |
562 | ||
563 | NOISY(printf("Adding resource bag entry l=%c%c c=%c%c orien=%d d=%d " | |
564 | " pid=%s, bag=%s, id=%s: %s\n", | |
565 | config.language[0], config.language[1], | |
566 | config.country[0], config.country[1], | |
567 | config.orientation, config.density, | |
568 | String8(parentIdent).string(), | |
569 | String8(ident).string(), | |
570 | String8(itemIdent).string(), | |
571 | String8(str).string())); | |
572 | ||
573 | err = outTable->addBag(SourcePos(in->getPrintableSource(), block->getLineNumber()), | |
574 | myPackage, curType, ident, parentIdent, itemIdent, str, | |
575 | &spans, &config, false, false, curFormat); | |
576 | return err; | |
577 | } | |
578 | ||
579 | ||
580 | status_t parseAndAddEntry(Bundle* bundle, | |
581 | const sp<AaptFile>& in, | |
582 | ResXMLTree* block, | |
583 | const ResTable_config& config, | |
584 | const String16& myPackage, | |
585 | const String16& curType, | |
586 | const String16& ident, | |
587 | const String16& curTag, | |
588 | bool curIsStyled, | |
589 | int32_t curFormat, | |
590 | bool pseudolocalize, | |
591 | ResourceTable* outTable) | |
592 | { | |
593 | status_t err; | |
594 | ||
595 | String16 str; | |
596 | Vector<StringPool::entry_style_span> spans; | |
597 | err = parseStyledString(bundle, in->getPrintableSource().string(), block, | |
598 | curTag, &str, curIsStyled ? &spans : NULL, | |
599 | pseudolocalize); | |
600 | ||
601 | if (err < NO_ERROR) { | |
602 | return err; | |
603 | } | |
604 | ||
605 | NOISY(printf("Adding resource entry l=%c%c c=%c%c orien=%d d=%d id=%s: %s\n", | |
606 | config.language[0], config.language[1], | |
607 | config.country[0], config.country[1], | |
608 | config.orientation, config.density, | |
609 | String8(ident).string(), String8(str).string())); | |
610 | ||
611 | err = outTable->addEntry(SourcePos(in->getPrintableSource(), block->getLineNumber()), | |
612 | myPackage, curType, ident, str, &spans, &config, | |
613 | false, curFormat); | |
614 | ||
615 | return err; | |
616 | } | |
617 | ||
618 | status_t compileResourceFile(Bundle* bundle, | |
619 | const sp<AaptAssets>& assets, | |
620 | const sp<AaptFile>& in, | |
621 | const ResTable_config& defParams, | |
622 | ResourceTable* outTable) | |
623 | { | |
624 | ResXMLTree block; | |
625 | status_t err = parseXMLResource(in, &block, false, true); | |
626 | if (err != NO_ERROR) { | |
627 | return err; | |
628 | } | |
629 | ||
630 | // Top-level tag. | |
631 | const String16 resources16("resources"); | |
632 | ||
633 | // Identifier declaration tags. | |
634 | const String16 declare_styleable16("declare-styleable"); | |
635 | const String16 attr16("attr"); | |
636 | ||
637 | // Data creation organizational tags. | |
638 | const String16 string16("string"); | |
639 | const String16 drawable16("drawable"); | |
640 | const String16 color16("color"); | |
641 | const String16 integer16("integer"); | |
642 | const String16 dimen16("dimen"); | |
643 | const String16 style16("style"); | |
644 | const String16 plurals16("plurals"); | |
645 | const String16 array16("array"); | |
646 | const String16 string_array16("string-array"); | |
647 | const String16 integer_array16("integer-array"); | |
648 | const String16 public16("public"); | |
649 | const String16 private_symbols16("private-symbols"); | |
650 | const String16 skip16("skip"); | |
651 | const String16 eat_comment16("eat-comment"); | |
652 | ||
653 | // Data creation tags. | |
654 | const String16 bag16("bag"); | |
655 | const String16 item16("item"); | |
656 | ||
657 | // Attribute type constants. | |
658 | const String16 enum16("enum"); | |
659 | ||
660 | // plural values | |
661 | const String16 other16("other"); | |
662 | const String16 quantityOther16("^other"); | |
663 | const String16 zero16("zero"); | |
664 | const String16 quantityZero16("^zero"); | |
665 | const String16 one16("one"); | |
666 | const String16 quantityOne16("^one"); | |
667 | const String16 two16("two"); | |
668 | const String16 quantityTwo16("^two"); | |
669 | const String16 few16("few"); | |
670 | const String16 quantityFew16("^few"); | |
671 | const String16 many16("many"); | |
672 | const String16 quantityMany16("^many"); | |
673 | ||
674 | ||
675 | const String16 myPackage(assets->getPackage()); | |
676 | ||
677 | bool hasErrors = false; | |
678 | ||
679 | uint32_t nextPublicId = 0; | |
680 | ||
681 | ResXMLTree::event_code_t code; | |
682 | do { | |
683 | code = block.next(); | |
684 | } while (code == ResXMLTree::START_NAMESPACE); | |
685 | ||
686 | size_t len; | |
687 | if (code != ResXMLTree::START_TAG) { | |
688 | SourcePos(in->getPrintableSource(), block.getLineNumber()).error( | |
689 | "No start tag found\n"); | |
690 | return UNKNOWN_ERROR; | |
691 | } | |
692 | if (strcmp16(block.getElementName(&len), resources16.string()) != 0) { | |
693 | SourcePos(in->getPrintableSource(), block.getLineNumber()).error( | |
694 | "Invalid start tag %s\n", String8(block.getElementName(&len)).string()); | |
695 | return UNKNOWN_ERROR; | |
696 | } | |
697 | ||
698 | ResTable_config curParams(defParams); | |
699 | ||
700 | ResTable_config pseudoParams(curParams); | |
701 | pseudoParams.language[0] = 'z'; | |
702 | pseudoParams.language[1] = 'z'; | |
703 | pseudoParams.country[0] = 'Z'; | |
704 | pseudoParams.country[1] = 'Z'; | |
705 | ||
706 | while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) { | |
707 | if (code == ResXMLTree::START_TAG) { | |
708 | const String16* curTag = NULL; | |
709 | String16 curType; | |
710 | int32_t curFormat = ResTable_map::TYPE_ANY; | |
711 | bool curIsBag = false; | |
712 | bool curIsStyled = false; | |
713 | bool curIsPseudolocalizable = false; | |
714 | bool localHasErrors = false; | |
715 | ||
716 | if (strcmp16(block.getElementName(&len), skip16.string()) == 0) { | |
717 | while ((code=block.next()) != ResXMLTree::END_DOCUMENT | |
718 | && code != ResXMLTree::BAD_DOCUMENT) { | |
719 | if (code == ResXMLTree::END_TAG) { | |
720 | if (strcmp16(block.getElementName(&len), skip16.string()) == 0) { | |
721 | break; | |
722 | } | |
723 | } | |
724 | } | |
725 | continue; | |
726 | ||
727 | } else if (strcmp16(block.getElementName(&len), eat_comment16.string()) == 0) { | |
728 | while ((code=block.next()) != ResXMLTree::END_DOCUMENT | |
729 | && code != ResXMLTree::BAD_DOCUMENT) { | |
730 | if (code == ResXMLTree::END_TAG) { | |
731 | if (strcmp16(block.getElementName(&len), eat_comment16.string()) == 0) { | |
732 | break; | |
733 | } | |
734 | } | |
735 | } | |
736 | continue; | |
737 | ||
738 | } else if (strcmp16(block.getElementName(&len), public16.string()) == 0) { | |
739 | SourcePos srcPos(in->getPrintableSource(), block.getLineNumber()); | |
740 | ||
741 | String16 type; | |
742 | ssize_t typeIdx = block.indexOfAttribute(NULL, "type"); | |
743 | if (typeIdx < 0) { | |
744 | srcPos.error("A 'type' attribute is required for <public>\n"); | |
745 | hasErrors = localHasErrors = true; | |
746 | } | |
747 | type = String16(block.getAttributeStringValue(typeIdx, &len)); | |
748 | ||
749 | String16 name; | |
750 | ssize_t nameIdx = block.indexOfAttribute(NULL, "name"); | |
751 | if (nameIdx < 0) { | |
752 | srcPos.error("A 'name' attribute is required for <public>\n"); | |
753 | hasErrors = localHasErrors = true; | |
754 | } | |
755 | name = String16(block.getAttributeStringValue(nameIdx, &len)); | |
756 | ||
757 | uint32_t ident = 0; | |
758 | ssize_t identIdx = block.indexOfAttribute(NULL, "id"); | |
759 | if (identIdx >= 0) { | |
760 | const char16_t* identStr = block.getAttributeStringValue(identIdx, &len); | |
761 | Res_value identValue; | |
762 | if (!ResTable::stringToInt(identStr, len, &identValue)) { | |
763 | srcPos.error("Given 'id' attribute is not an integer: %s\n", | |
764 | String8(block.getAttributeStringValue(identIdx, &len)).string()); | |
765 | hasErrors = localHasErrors = true; | |
766 | } else { | |
767 | ident = identValue.data; | |
768 | nextPublicId = ident+1; | |
769 | } | |
770 | } else if (nextPublicId == 0) { | |
771 | srcPos.error("No 'id' attribute supplied <public>," | |
772 | " and no previous id defined in this file.\n"); | |
773 | hasErrors = localHasErrors = true; | |
774 | } else if (!localHasErrors) { | |
775 | ident = nextPublicId; | |
776 | nextPublicId++; | |
777 | } | |
778 | ||
779 | if (!localHasErrors) { | |
780 | err = outTable->addPublic(srcPos, myPackage, type, name, ident); | |
781 | if (err < NO_ERROR) { | |
782 | hasErrors = localHasErrors = true; | |
783 | } | |
784 | } | |
785 | if (!localHasErrors) { | |
786 | sp<AaptSymbols> symbols = assets->getSymbolsFor(String8("R")); | |
787 | if (symbols != NULL) { | |
788 | symbols = symbols->addNestedSymbol(String8(type), srcPos); | |
789 | } | |
790 | if (symbols != NULL) { | |
791 | symbols->makeSymbolPublic(String8(name), srcPos); | |
792 | String16 comment( | |
793 | block.getComment(&len) ? block.getComment(&len) : nulStr); | |
794 | symbols->appendComment(String8(name), comment, srcPos); | |
795 | } else { | |
796 | srcPos.error("Unable to create symbols!\n"); | |
797 | hasErrors = localHasErrors = true; | |
798 | } | |
799 | } | |
800 | ||
801 | while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) { | |
802 | if (code == ResXMLTree::END_TAG) { | |
803 | if (strcmp16(block.getElementName(&len), public16.string()) == 0) { | |
804 | break; | |
805 | } | |
806 | } | |
807 | } | |
808 | continue; | |
809 | ||
810 | } else if (strcmp16(block.getElementName(&len), private_symbols16.string()) == 0) { | |
811 | String16 pkg; | |
812 | ssize_t pkgIdx = block.indexOfAttribute(NULL, "package"); | |
813 | if (pkgIdx < 0) { | |
814 | SourcePos(in->getPrintableSource(), block.getLineNumber()).error( | |
815 | "A 'package' attribute is required for <private-symbols>\n"); | |
816 | hasErrors = localHasErrors = true; | |
817 | } | |
818 | pkg = String16(block.getAttributeStringValue(pkgIdx, &len)); | |
819 | if (!localHasErrors) { | |
820 | assets->setSymbolsPrivatePackage(String8(pkg)); | |
821 | } | |
822 | ||
823 | while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) { | |
824 | if (code == ResXMLTree::END_TAG) { | |
825 | if (strcmp16(block.getElementName(&len), private_symbols16.string()) == 0) { | |
826 | break; | |
827 | } | |
828 | } | |
829 | } | |
830 | continue; | |
831 | ||
832 | } else if (strcmp16(block.getElementName(&len), declare_styleable16.string()) == 0) { | |
833 | SourcePos srcPos(in->getPrintableSource(), block.getLineNumber()); | |
834 | ||
835 | String16 ident; | |
836 | ssize_t identIdx = block.indexOfAttribute(NULL, "name"); | |
837 | if (identIdx < 0) { | |
838 | srcPos.error("A 'name' attribute is required for <declare-styleable>\n"); | |
839 | hasErrors = localHasErrors = true; | |
840 | } | |
841 | ident = String16(block.getAttributeStringValue(identIdx, &len)); | |
842 | ||
843 | sp<AaptSymbols> symbols = assets->getSymbolsFor(String8("R")); | |
844 | if (!localHasErrors) { | |
845 | if (symbols != NULL) { | |
846 | symbols = symbols->addNestedSymbol(String8("styleable"), srcPos); | |
847 | } | |
848 | sp<AaptSymbols> styleSymbols = symbols; | |
849 | if (symbols != NULL) { | |
850 | symbols = symbols->addNestedSymbol(String8(ident), srcPos); | |
851 | } | |
852 | if (symbols == NULL) { | |
853 | srcPos.error("Unable to create symbols!\n"); | |
854 | return UNKNOWN_ERROR; | |
855 | } | |
856 | ||
857 | String16 comment( | |
858 | block.getComment(&len) ? block.getComment(&len) : nulStr); | |
859 | styleSymbols->appendComment(String8(ident), comment, srcPos); | |
860 | } else { | |
861 | symbols = NULL; | |
862 | } | |
863 | ||
864 | while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) { | |
865 | if (code == ResXMLTree::START_TAG) { | |
866 | if (strcmp16(block.getElementName(&len), skip16.string()) == 0) { | |
867 | while ((code=block.next()) != ResXMLTree::END_DOCUMENT | |
868 | && code != ResXMLTree::BAD_DOCUMENT) { | |
869 | if (code == ResXMLTree::END_TAG) { | |
870 | if (strcmp16(block.getElementName(&len), skip16.string()) == 0) { | |
871 | break; | |
872 | } | |
873 | } | |
874 | } | |
875 | continue; | |
876 | } else if (strcmp16(block.getElementName(&len), eat_comment16.string()) == 0) { | |
877 | while ((code=block.next()) != ResXMLTree::END_DOCUMENT | |
878 | && code != ResXMLTree::BAD_DOCUMENT) { | |
879 | if (code == ResXMLTree::END_TAG) { | |
880 | if (strcmp16(block.getElementName(&len), eat_comment16.string()) == 0) { | |
881 | break; | |
882 | } | |
883 | } | |
884 | } | |
885 | continue; | |
886 | } else if (strcmp16(block.getElementName(&len), attr16.string()) != 0) { | |
887 | SourcePos(in->getPrintableSource(), block.getLineNumber()).error( | |
888 | "Tag <%s> can not appear inside <declare-styleable>, only <attr>\n", | |
889 | String8(block.getElementName(&len)).string()); | |
890 | return UNKNOWN_ERROR; | |
891 | } | |
892 | ||
893 | String16 comment( | |
894 | block.getComment(&len) ? block.getComment(&len) : nulStr); | |
895 | String16 itemIdent; | |
896 | err = compileAttribute(in, block, myPackage, outTable, &itemIdent, true); | |
897 | if (err != NO_ERROR) { | |
898 | hasErrors = localHasErrors = true; | |
899 | } | |
900 | ||
901 | if (symbols != NULL) { | |
902 | SourcePos srcPos(String8(in->getPrintableSource()), block.getLineNumber()); | |
903 | symbols->addSymbol(String8(itemIdent), 0, srcPos); | |
904 | symbols->appendComment(String8(itemIdent), comment, srcPos); | |
905 | //printf("Attribute %s comment: %s\n", String8(itemIdent).string(), | |
906 | // String8(comment).string()); | |
907 | } | |
908 | } else if (code == ResXMLTree::END_TAG) { | |
909 | if (strcmp16(block.getElementName(&len), declare_styleable16.string()) == 0) { | |
910 | break; | |
911 | } | |
912 | ||
913 | SourcePos(in->getPrintableSource(), block.getLineNumber()).error( | |
914 | "Found tag </%s> where </attr> is expected\n", | |
915 | String8(block.getElementName(&len)).string()); | |
916 | return UNKNOWN_ERROR; | |
917 | } | |
918 | } | |
919 | continue; | |
920 | ||
921 | } else if (strcmp16(block.getElementName(&len), attr16.string()) == 0) { | |
922 | err = compileAttribute(in, block, myPackage, outTable, NULL); | |
923 | if (err != NO_ERROR) { | |
924 | hasErrors = true; | |
925 | } | |
926 | continue; | |
927 | ||
928 | } else if (strcmp16(block.getElementName(&len), item16.string()) == 0) { | |
929 | curTag = &item16; | |
930 | ssize_t attri = block.indexOfAttribute(NULL, "type"); | |
931 | if (attri >= 0) { | |
932 | curType = String16(block.getAttributeStringValue(attri, &len)); | |
933 | ssize_t formatIdx = block.indexOfAttribute(NULL, "format"); | |
934 | if (formatIdx >= 0) { | |
935 | String16 formatStr = String16(block.getAttributeStringValue( | |
936 | formatIdx, &len)); | |
937 | curFormat = parse_flags(formatStr.string(), formatStr.size(), | |
938 | gFormatFlags); | |
939 | if (curFormat == 0) { | |
940 | SourcePos(in->getPrintableSource(), block.getLineNumber()).error( | |
941 | "Tag <item> 'format' attribute value \"%s\" not valid\n", | |
942 | String8(formatStr).string()); | |
943 | hasErrors = localHasErrors = true; | |
944 | } | |
945 | } | |
946 | } else { | |
947 | SourcePos(in->getPrintableSource(), block.getLineNumber()).error( | |
948 | "A 'type' attribute is required for <item>\n"); | |
949 | hasErrors = localHasErrors = true; | |
950 | } | |
951 | curIsStyled = true; | |
952 | } else if (strcmp16(block.getElementName(&len), string16.string()) == 0) { | |
953 | curTag = &string16; | |
954 | curType = string16; | |
955 | curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_STRING; | |
956 | curIsStyled = true; | |
957 | curIsPseudolocalizable = true; | |
958 | } else if (strcmp16(block.getElementName(&len), drawable16.string()) == 0) { | |
959 | curTag = &drawable16; | |
960 | curType = drawable16; | |
961 | curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_COLOR; | |
962 | } else if (strcmp16(block.getElementName(&len), color16.string()) == 0) { | |
963 | curTag = &color16; | |
964 | curType = color16; | |
965 | curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_COLOR; | |
966 | } else if (strcmp16(block.getElementName(&len), integer16.string()) == 0) { | |
967 | curTag = &integer16; | |
968 | curType = integer16; | |
969 | curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_INTEGER; | |
970 | } else if (strcmp16(block.getElementName(&len), dimen16.string()) == 0) { | |
971 | curTag = &dimen16; | |
972 | curType = dimen16; | |
973 | curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_DIMENSION; | |
974 | } else if (strcmp16(block.getElementName(&len), bag16.string()) == 0) { | |
975 | curTag = &bag16; | |
976 | curIsBag = true; | |
977 | ssize_t attri = block.indexOfAttribute(NULL, "type"); | |
978 | if (attri >= 0) { | |
979 | curType = String16(block.getAttributeStringValue(attri, &len)); | |
980 | } else { | |
981 | SourcePos(in->getPrintableSource(), block.getLineNumber()).error( | |
982 | "A 'type' attribute is required for <bag>\n"); | |
983 | hasErrors = localHasErrors = true; | |
984 | } | |
985 | } else if (strcmp16(block.getElementName(&len), style16.string()) == 0) { | |
986 | curTag = &style16; | |
987 | curType = style16; | |
988 | curIsBag = true; | |
989 | } else if (strcmp16(block.getElementName(&len), plurals16.string()) == 0) { | |
990 | curTag = &plurals16; | |
991 | curType = plurals16; | |
992 | curIsBag = true; | |
993 | } else if (strcmp16(block.getElementName(&len), array16.string()) == 0) { | |
994 | curTag = &array16; | |
995 | curType = array16; | |
996 | curIsBag = true; | |
997 | ssize_t formatIdx = block.indexOfAttribute(NULL, "format"); | |
998 | if (formatIdx >= 0) { | |
999 | String16 formatStr = String16(block.getAttributeStringValue( | |
1000 | formatIdx, &len)); | |
1001 | curFormat = parse_flags(formatStr.string(), formatStr.size(), | |
1002 | gFormatFlags); | |
1003 | if (curFormat == 0) { | |
1004 | SourcePos(in->getPrintableSource(), block.getLineNumber()).error( | |
1005 | "Tag <array> 'format' attribute value \"%s\" not valid\n", | |
1006 | String8(formatStr).string()); | |
1007 | hasErrors = localHasErrors = true; | |
1008 | } | |
1009 | } | |
1010 | } else if (strcmp16(block.getElementName(&len), string_array16.string()) == 0) { | |
1011 | curTag = &string_array16; | |
1012 | curType = array16; | |
1013 | curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_STRING; | |
1014 | curIsBag = true; | |
1015 | curIsPseudolocalizable = true; | |
1016 | } else if (strcmp16(block.getElementName(&len), integer_array16.string()) == 0) { | |
1017 | curTag = &integer_array16; | |
1018 | curType = array16; | |
1019 | curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_INTEGER; | |
1020 | curIsBag = true; | |
1021 | } else { | |
1022 | SourcePos(in->getPrintableSource(), block.getLineNumber()).error( | |
1023 | "Found tag %s where item is expected\n", | |
1024 | String8(block.getElementName(&len)).string()); | |
1025 | return UNKNOWN_ERROR; | |
1026 | } | |
1027 | ||
1028 | String16 ident; | |
1029 | ssize_t identIdx = block.indexOfAttribute(NULL, "name"); | |
1030 | if (identIdx >= 0) { | |
1031 | ident = String16(block.getAttributeStringValue(identIdx, &len)); | |
1032 | } else { | |
1033 | SourcePos(in->getPrintableSource(), block.getLineNumber()).error( | |
1034 | "A 'name' attribute is required for <%s>\n", | |
1035 | String8(*curTag).string()); | |
1036 | hasErrors = localHasErrors = true; | |
1037 | } | |
1038 | ||
1039 | String16 comment(block.getComment(&len) ? block.getComment(&len) : nulStr); | |
1040 | ||
1041 | if (curIsBag) { | |
1042 | // Figure out the parent of this bag... | |
1043 | String16 parentIdent; | |
1044 | ssize_t parentIdentIdx = block.indexOfAttribute(NULL, "parent"); | |
1045 | if (parentIdentIdx >= 0) { | |
1046 | parentIdent = String16(block.getAttributeStringValue(parentIdentIdx, &len)); | |
1047 | } else { | |
1048 | ssize_t sep = ident.findLast('.'); | |
1049 | if (sep >= 0) { | |
1050 | parentIdent.setTo(ident, sep); | |
1051 | } | |
1052 | } | |
1053 | ||
1054 | if (!localHasErrors) { | |
1055 | err = outTable->startBag(SourcePos(in->getPrintableSource(), block.getLineNumber()), | |
1056 | myPackage, curType, ident, parentIdent, &curParams); | |
1057 | if (err != NO_ERROR) { | |
1058 | hasErrors = localHasErrors = true; | |
1059 | } | |
1060 | } | |
1061 | ||
1062 | ssize_t elmIndex = 0; | |
1063 | char elmIndexStr[14]; | |
1064 | while ((code=block.next()) != ResXMLTree::END_DOCUMENT | |
1065 | && code != ResXMLTree::BAD_DOCUMENT) { | |
1066 | ||
1067 | if (code == ResXMLTree::START_TAG) { | |
1068 | if (strcmp16(block.getElementName(&len), item16.string()) != 0) { | |
1069 | SourcePos(in->getPrintableSource(), block.getLineNumber()).error( | |
1070 | "Tag <%s> can not appear inside <%s>, only <item>\n", | |
1071 | String8(block.getElementName(&len)).string(), | |
1072 | String8(*curTag).string()); | |
1073 | return UNKNOWN_ERROR; | |
1074 | } | |
1075 | ||
1076 | String16 itemIdent; | |
1077 | if (curType == array16) { | |
1078 | sprintf(elmIndexStr, "^index_%d", (int)elmIndex++); | |
1079 | itemIdent = String16(elmIndexStr); | |
1080 | } else if (curType == plurals16) { | |
1081 | ssize_t itemIdentIdx = block.indexOfAttribute(NULL, "quantity"); | |
1082 | if (itemIdentIdx >= 0) { | |
1083 | String16 quantity16(block.getAttributeStringValue(itemIdentIdx, &len)); | |
1084 | if (quantity16 == other16) { | |
1085 | itemIdent = quantityOther16; | |
1086 | } | |
1087 | else if (quantity16 == zero16) { | |
1088 | itemIdent = quantityZero16; | |
1089 | } | |
1090 | else if (quantity16 == one16) { | |
1091 | itemIdent = quantityOne16; | |
1092 | } | |
1093 | else if (quantity16 == two16) { | |
1094 | itemIdent = quantityTwo16; | |
1095 | } | |
1096 | else if (quantity16 == few16) { | |
1097 | itemIdent = quantityFew16; | |
1098 | } | |
1099 | else if (quantity16 == many16) { | |
1100 | itemIdent = quantityMany16; | |
1101 | } | |
1102 | else { | |
1103 | SourcePos(in->getPrintableSource(), block.getLineNumber()).error( | |
1104 | "Illegal 'quantity' attribute is <item> inside <plurals>\n"); | |
1105 | hasErrors = localHasErrors = true; | |
1106 | } | |
1107 | } else { | |
1108 | SourcePos(in->getPrintableSource(), block.getLineNumber()).error( | |
1109 | "A 'quantity' attribute is required for <item> inside <plurals>\n"); | |
1110 | hasErrors = localHasErrors = true; | |
1111 | } | |
1112 | } else { | |
1113 | ssize_t itemIdentIdx = block.indexOfAttribute(NULL, "name"); | |
1114 | if (itemIdentIdx >= 0) { | |
1115 | itemIdent = String16(block.getAttributeStringValue(itemIdentIdx, &len)); | |
1116 | } else { | |
1117 | SourcePos(in->getPrintableSource(), block.getLineNumber()).error( | |
1118 | "A 'name' attribute is required for <item>\n"); | |
1119 | hasErrors = localHasErrors = true; | |
1120 | } | |
1121 | } | |
1122 | ||
1123 | ResXMLParser::ResXMLPosition parserPosition; | |
1124 | block.getPosition(&parserPosition); | |
1125 | ||
1126 | err = parseAndAddBag(bundle, in, &block, curParams, myPackage, curType, | |
1127 | ident, parentIdent, itemIdent, curFormat, false, outTable); | |
1128 | if (err == NO_ERROR) { | |
1129 | if (curIsPseudolocalizable && localeIsDefined(curParams) | |
1130 | && bundle->getPseudolocalize()) { | |
1131 | // pseudolocalize here | |
1132 | #if 1 | |
1133 | block.setPosition(parserPosition); | |
1134 | err = parseAndAddBag(bundle, in, &block, pseudoParams, myPackage, | |
1135 | curType, ident, parentIdent, itemIdent, curFormat, true, | |
1136 | outTable); | |
1137 | #endif | |
1138 | } | |
1139 | } | |
1140 | if (err != NO_ERROR) { | |
1141 | hasErrors = localHasErrors = true; | |
1142 | } | |
1143 | } else if (code == ResXMLTree::END_TAG) { | |
1144 | if (strcmp16(block.getElementName(&len), curTag->string()) != 0) { | |
1145 | SourcePos(in->getPrintableSource(), block.getLineNumber()).error( | |
1146 | "Found tag </%s> where </%s> is expected\n", | |
1147 | String8(block.getElementName(&len)).string(), | |
1148 | String8(*curTag).string()); | |
1149 | return UNKNOWN_ERROR; | |
1150 | } | |
1151 | break; | |
1152 | } | |
1153 | } | |
1154 | } else { | |
1155 | ResXMLParser::ResXMLPosition parserPosition; | |
1156 | block.getPosition(&parserPosition); | |
1157 | ||
1158 | err = parseAndAddEntry(bundle, in, &block, curParams, myPackage, curType, ident, | |
1159 | *curTag, curIsStyled, curFormat, false, outTable); | |
1160 | ||
1161 | if (err < NO_ERROR) { // Why err < NO_ERROR instead of err != NO_ERROR? | |
1162 | hasErrors = localHasErrors = true; | |
1163 | } | |
1164 | else if (err == NO_ERROR) { | |
1165 | if (curIsPseudolocalizable && localeIsDefined(curParams) | |
1166 | && bundle->getPseudolocalize()) { | |
1167 | // pseudolocalize here | |
1168 | block.setPosition(parserPosition); | |
1169 | err = parseAndAddEntry(bundle, in, &block, pseudoParams, myPackage, curType, | |
1170 | ident, *curTag, curIsStyled, curFormat, true, outTable); | |
1171 | if (err != NO_ERROR) { | |
1172 | hasErrors = localHasErrors = true; | |
1173 | } | |
1174 | } | |
1175 | } | |
1176 | } | |
1177 | ||
1178 | #if 0 | |
1179 | if (comment.size() > 0) { | |
1180 | printf("Comment for @%s:%s/%s: %s\n", String8(myPackage).string(), | |
1181 | String8(curType).string(), String8(ident).string(), | |
1182 | String8(comment).string()); | |
1183 | } | |
1184 | #endif | |
1185 | if (!localHasErrors) { | |
1186 | outTable->appendComment(myPackage, curType, ident, comment, false); | |
1187 | } | |
1188 | } | |
1189 | else if (code == ResXMLTree::END_TAG) { | |
1190 | if (strcmp16(block.getElementName(&len), resources16.string()) != 0) { | |
1191 | SourcePos(in->getPrintableSource(), block.getLineNumber()).error( | |
1192 | "Unexpected end tag %s\n", String8(block.getElementName(&len)).string()); | |
1193 | return UNKNOWN_ERROR; | |
1194 | } | |
1195 | } | |
1196 | else if (code == ResXMLTree::START_NAMESPACE || code == ResXMLTree::END_NAMESPACE) { | |
1197 | } | |
1198 | else if (code == ResXMLTree::TEXT) { | |
1199 | if (isWhitespace(block.getText(&len))) { | |
1200 | continue; | |
1201 | } | |
1202 | SourcePos(in->getPrintableSource(), block.getLineNumber()).error( | |
1203 | "Found text \"%s\" where item tag is expected\n", | |
1204 | String8(block.getText(&len)).string()); | |
1205 | return UNKNOWN_ERROR; | |
1206 | } | |
1207 | } | |
1208 | ||
1209 | return hasErrors ? UNKNOWN_ERROR : NO_ERROR; | |
1210 | } | |
1211 | ||
1212 | ResourceTable::ResourceTable(Bundle* bundle, const String16& assetsPackage) | |
1213 | : mAssetsPackage(assetsPackage), mNextPackageId(1), mHaveAppPackage(false), | |
1214 | mIsAppPackage(!bundle->getExtending()), | |
1215 | mNumLocal(0), | |
1216 | mBundle(bundle) | |
1217 | { | |
1218 | } | |
1219 | ||
1220 | status_t ResourceTable::addIncludedResources(Bundle* bundle, const sp<AaptAssets>& assets) | |
1221 | { | |
1222 | status_t err = assets->buildIncludedResources(bundle); | |
1223 | if (err != NO_ERROR) { | |
1224 | return err; | |
1225 | } | |
1226 | ||
1227 | // For future reference to included resources. | |
1228 | mAssets = assets; | |
1229 | ||
1230 | const ResTable& incl = assets->getIncludedResources(); | |
1231 | ||
1232 | // Retrieve all the packages. | |
1233 | const size_t N = incl.getBasePackageCount(); | |
1234 | for (size_t phase=0; phase<2; phase++) { | |
1235 | for (size_t i=0; i<N; i++) { | |
1236 | String16 name(incl.getBasePackageName(i)); | |
1237 | uint32_t id = incl.getBasePackageId(i); | |
1238 | // First time through: only add base packages (id | |
1239 | // is not 0); second time through add the other | |
1240 | // packages. | |
1241 | if (phase != 0) { | |
1242 | if (id != 0) { | |
1243 | // Skip base packages -- already one. | |
1244 | id = 0; | |
1245 | } else { | |
1246 | // Assign a dynamic id. | |
1247 | id = mNextPackageId; | |
1248 | } | |
1249 | } else if (id != 0) { | |
1250 | if (id == 127) { | |
1251 | if (mHaveAppPackage) { | |
1252 | fprintf(stderr, "Included resource have two application packages!\n"); | |
1253 | return UNKNOWN_ERROR; | |
1254 | } | |
1255 | mHaveAppPackage = true; | |
1256 | } | |
1257 | if (mNextPackageId > id) { | |
1258 | fprintf(stderr, "Included base package ID %d already in use!\n", id); | |
1259 | return UNKNOWN_ERROR; | |
1260 | } | |
1261 | } | |
1262 | if (id != 0) { | |
1263 | NOISY(printf("Including package %s with ID=%d\n", | |
1264 | String8(name).string(), id)); | |
1265 | sp<Package> p = new Package(name, id); | |
1266 | mPackages.add(name, p); | |
1267 | mOrderedPackages.add(p); | |
1268 | ||
1269 | if (id >= mNextPackageId) { | |
1270 | mNextPackageId = id+1; | |
1271 | } | |
1272 | } | |
1273 | } | |
1274 | } | |
1275 | ||
1276 | // Every resource table always has one first entry, the bag attributes. | |
1277 | const SourcePos unknown(String8("????"), 0); | |
1278 | sp<Type> attr = getType(mAssetsPackage, String16("attr"), unknown); | |
1279 | ||
1280 | return NO_ERROR; | |
1281 | } | |
1282 | ||
1283 | status_t ResourceTable::addPublic(const SourcePos& sourcePos, | |
1284 | const String16& package, | |
1285 | const String16& type, | |
1286 | const String16& name, | |
1287 | const uint32_t ident) | |
1288 | { | |
1289 | uint32_t rid = mAssets->getIncludedResources() | |
1290 | .identifierForName(name.string(), name.size(), | |
1291 | type.string(), type.size(), | |
1292 | package.string(), package.size()); | |
1293 | if (rid != 0) { | |
1294 | sourcePos.error("Error declaring public resource %s/%s for included package %s\n", | |
1295 | String8(type).string(), String8(name).string(), | |
1296 | String8(package).string()); | |
1297 | return UNKNOWN_ERROR; | |
1298 | } | |
1299 | ||
1300 | sp<Type> t = getType(package, type, sourcePos); | |
1301 | if (t == NULL) { | |
1302 | return UNKNOWN_ERROR; | |
1303 | } | |
1304 | return t->addPublic(sourcePos, name, ident); | |
1305 | } | |
1306 | ||
1307 | status_t ResourceTable::addEntry(const SourcePos& sourcePos, | |
1308 | const String16& package, | |
1309 | const String16& type, | |
1310 | const String16& name, | |
1311 | const String16& value, | |
1312 | const Vector<StringPool::entry_style_span>* style, | |
1313 | const ResTable_config* params, | |
1314 | const bool doSetIndex, | |
1315 | const int32_t format) | |
1316 | { | |
1317 | // Check for adding entries in other packages... for now we do | |
1318 | // nothing. We need to do the right thing here to support skinning. | |
1319 | uint32_t rid = mAssets->getIncludedResources() | |
1320 | .identifierForName(name.string(), name.size(), | |
1321 | type.string(), type.size(), | |
1322 | package.string(), package.size()); | |
1323 | if (rid != 0) { | |
1324 | return NO_ERROR; | |
1325 | } | |
1326 | ||
1327 | #if 0 | |
1328 | if (name == String16("left")) { | |
1329 | printf("Adding entry left: file=%s, line=%d, type=%s, value=%s\n", | |
1330 | sourcePos.file.string(), sourcePos.line, String8(type).string(), | |
1331 | String8(value).string()); | |
1332 | } | |
1333 | #endif | |
1334 | ||
1335 | sp<Entry> e = getEntry(package, type, name, sourcePos, params, doSetIndex); | |
1336 | if (e == NULL) { | |
1337 | return UNKNOWN_ERROR; | |
1338 | } | |
1339 | status_t err = e->setItem(sourcePos, value, style, format); | |
1340 | if (err == NO_ERROR) { | |
1341 | mNumLocal++; | |
1342 | } | |
1343 | return err; | |
1344 | } | |
1345 | ||
1346 | status_t ResourceTable::startBag(const SourcePos& sourcePos, | |
1347 | const String16& package, | |
1348 | const String16& type, | |
1349 | const String16& name, | |
1350 | const String16& bagParent, | |
1351 | const ResTable_config* params, | |
1352 | bool replace, bool isId) | |
1353 | { | |
1354 | // Check for adding entries in other packages... for now we do | |
1355 | // nothing. We need to do the right thing here to support skinning. | |
1356 | uint32_t rid = mAssets->getIncludedResources() | |
1357 | .identifierForName(name.string(), name.size(), | |
1358 | type.string(), type.size(), | |
1359 | package.string(), package.size()); | |
1360 | if (rid != 0) { | |
1361 | return NO_ERROR; | |
1362 | } | |
1363 | ||
1364 | #if 0 | |
1365 | if (name == String16("left")) { | |
1366 | printf("Adding bag left: file=%s, line=%d, type=%s\n", | |
1367 | sourcePos.file.striing(), sourcePos.line, String8(type).string()); | |
1368 | } | |
1369 | #endif | |
1370 | ||
1371 | sp<Entry> e = getEntry(package, type, name, sourcePos, params); | |
1372 | if (e == NULL) { | |
1373 | return UNKNOWN_ERROR; | |
1374 | } | |
1375 | ||
1376 | // If a parent is explicitly specified, set it. | |
1377 | if (bagParent.size() > 0) { | |
1378 | String16 curPar = e->getParent(); | |
1379 | if (curPar.size() > 0 && curPar != bagParent) { | |
1380 | sourcePos.error("Conflicting parents specified, was '%s', now '%s'\n", | |
1381 | String8(e->getParent()).string(), | |
1382 | String8(bagParent).string()); | |
1383 | return UNKNOWN_ERROR; | |
1384 | } | |
1385 | e->setParent(bagParent); | |
1386 | } | |
1387 | ||
1388 | return e->makeItABag(sourcePos); | |
1389 | } | |
1390 | ||
1391 | status_t ResourceTable::addBag(const SourcePos& sourcePos, | |
1392 | const String16& package, | |
1393 | const String16& type, | |
1394 | const String16& name, | |
1395 | const String16& bagParent, | |
1396 | const String16& bagKey, | |
1397 | const String16& value, | |
1398 | const Vector<StringPool::entry_style_span>* style, | |
1399 | const ResTable_config* params, | |
1400 | bool replace, bool isId, const int32_t format) | |
1401 | { | |
1402 | // Check for adding entries in other packages... for now we do | |
1403 | // nothing. We need to do the right thing here to support skinning. | |
1404 | uint32_t rid = mAssets->getIncludedResources() | |
1405 | .identifierForName(name.string(), name.size(), | |
1406 | type.string(), type.size(), | |
1407 | package.string(), package.size()); | |
1408 | if (rid != 0) { | |
1409 | return NO_ERROR; | |
1410 | } | |
1411 | ||
1412 | #if 0 | |
1413 | if (name == String16("left")) { | |
1414 | printf("Adding bag left: file=%s, line=%d, type=%s\n", | |
1415 | sourcePos.file.striing(), sourcePos.line, String8(type).string()); | |
1416 | } | |
1417 | #endif | |
1418 | ||
1419 | sp<Entry> e = getEntry(package, type, name, sourcePos, params); | |
1420 | if (e == NULL) { | |
1421 | return UNKNOWN_ERROR; | |
1422 | } | |
1423 | ||
1424 | // If a parent is explicitly specified, set it. | |
1425 | if (bagParent.size() > 0) { | |
1426 | String16 curPar = e->getParent(); | |
1427 | if (curPar.size() > 0 && curPar != bagParent) { | |
1428 | sourcePos.error("Conflicting parents specified, was '%s', now '%s'\n", | |
1429 | String8(e->getParent()).string(), | |
1430 | String8(bagParent).string()); | |
1431 | return UNKNOWN_ERROR; | |
1432 | } | |
1433 | e->setParent(bagParent); | |
1434 | } | |
1435 | ||
1436 | const bool first = e->getBag().indexOfKey(bagKey) < 0; | |
1437 | status_t err = e->addToBag(sourcePos, bagKey, value, style, replace, isId, format); | |
1438 | if (err == NO_ERROR && first) { | |
1439 | mNumLocal++; | |
1440 | } | |
1441 | return err; | |
1442 | } | |
1443 | ||
1444 | bool ResourceTable::hasBagOrEntry(const String16& package, | |
1445 | const String16& type, | |
1446 | const String16& name) const | |
1447 | { | |
1448 | // First look for this in the included resources... | |
1449 | uint32_t rid = mAssets->getIncludedResources() | |
1450 | .identifierForName(name.string(), name.size(), | |
1451 | type.string(), type.size(), | |
1452 | package.string(), package.size()); | |
1453 | if (rid != 0) { | |
1454 | return true; | |
1455 | } | |
1456 | ||
1457 | sp<Package> p = mPackages.valueFor(package); | |
1458 | if (p != NULL) { | |
1459 | sp<Type> t = p->getTypes().valueFor(type); | |
1460 | if (t != NULL) { | |
1461 | sp<ConfigList> c = t->getConfigs().valueFor(name); | |
1462 | if (c != NULL) return true; | |
1463 | } | |
1464 | } | |
1465 | ||
1466 | return false; | |
1467 | } | |
1468 | ||
1469 | bool ResourceTable::hasBagOrEntry(const String16& ref, | |
1470 | const String16* defType, | |
1471 | const String16* defPackage) | |
1472 | { | |
1473 | String16 package, type, name; | |
1474 | if (!ResTable::expandResourceRef(ref.string(), ref.size(), &package, &type, &name, | |
1475 | defType, defPackage ? defPackage:&mAssetsPackage, NULL)) { | |
1476 | return false; | |
1477 | } | |
1478 | return hasBagOrEntry(package, type, name); | |
1479 | } | |
1480 | ||
1481 | bool ResourceTable::appendComment(const String16& package, | |
1482 | const String16& type, | |
1483 | const String16& name, | |
1484 | const String16& comment, | |
1485 | bool onlyIfEmpty) | |
1486 | { | |
1487 | if (comment.size() <= 0) { | |
1488 | return true; | |
1489 | } | |
1490 | ||
1491 | sp<Package> p = mPackages.valueFor(package); | |
1492 | if (p != NULL) { | |
1493 | sp<Type> t = p->getTypes().valueFor(type); | |
1494 | if (t != NULL) { | |
1495 | sp<ConfigList> c = t->getConfigs().valueFor(name); | |
1496 | if (c != NULL) { | |
1497 | c->appendComment(comment, onlyIfEmpty); | |
1498 | return true; | |
1499 | } | |
1500 | } | |
1501 | } | |
1502 | return false; | |
1503 | } | |
1504 | ||
1505 | bool ResourceTable::appendTypeComment(const String16& package, | |
1506 | const String16& type, | |
1507 | const String16& name, | |
1508 | const String16& comment) | |
1509 | { | |
1510 | if (comment.size() <= 0) { | |
1511 | return true; | |
1512 | } | |
1513 | ||
1514 | sp<Package> p = mPackages.valueFor(package); | |
1515 | if (p != NULL) { | |
1516 | sp<Type> t = p->getTypes().valueFor(type); | |
1517 | if (t != NULL) { | |
1518 | sp<ConfigList> c = t->getConfigs().valueFor(name); | |
1519 | if (c != NULL) { | |
1520 | c->appendTypeComment(comment); | |
1521 | return true; | |
1522 | } | |
1523 | } | |
1524 | } | |
1525 | return false; | |
1526 | } | |
1527 | ||
1528 | size_t ResourceTable::size() const { | |
1529 | return mPackages.size(); | |
1530 | } | |
1531 | ||
1532 | size_t ResourceTable::numLocalResources() const { | |
1533 | return mNumLocal; | |
1534 | } | |
1535 | ||
1536 | bool ResourceTable::hasResources() const { | |
1537 | return mNumLocal > 0; | |
1538 | } | |
1539 | ||
1540 | sp<AaptFile> ResourceTable::flatten(Bundle* bundle) | |
1541 | { | |
1542 | sp<AaptFile> data = new AaptFile(String8(), AaptGroupEntry(), String8()); | |
1543 | status_t err = flatten(bundle, data); | |
1544 | return err == NO_ERROR ? data : NULL; | |
1545 | } | |
1546 | ||
1547 | inline uint32_t ResourceTable::getResId(const sp<Package>& p, | |
1548 | const sp<Type>& t, | |
1549 | uint32_t nameId) | |
1550 | { | |
1551 | return makeResId(p->getAssignedId(), t->getIndex(), nameId); | |
1552 | } | |
1553 | ||
1554 | uint32_t ResourceTable::getResId(const String16& package, | |
1555 | const String16& type, | |
1556 | const String16& name) const | |
1557 | { | |
1558 | sp<Package> p = mPackages.valueFor(package); | |
1559 | if (p == NULL) return 0; | |
1560 | ||
1561 | // First look for this in the included resources... | |
1562 | uint32_t rid = mAssets->getIncludedResources() | |
1563 | .identifierForName(name.string(), name.size(), | |
1564 | type.string(), type.size(), | |
1565 | package.string(), package.size()); | |
1566 | if (rid != 0) { | |
1567 | if (Res_INTERNALID(rid)) { | |
1568 | return rid; | |
1569 | } | |
1570 | return Res_MAKEID(p->getAssignedId()-1, | |
1571 | Res_GETTYPE(rid), | |
1572 | Res_GETENTRY(rid)); | |
1573 | } | |
1574 | ||
1575 | sp<Type> t = p->getTypes().valueFor(type); | |
1576 | if (t == NULL) return 0; | |
1577 | sp<ConfigList> c = t->getConfigs().valueFor(name); | |
1578 | if (c == NULL) return 0; | |
1579 | int32_t ei = c->getEntryIndex(); | |
1580 | if (ei < 0) return 0; | |
1581 | return getResId(p, t, ei); | |
1582 | } | |
1583 | ||
1584 | uint32_t ResourceTable::getResId(const String16& ref, | |
1585 | const String16* defType, | |
1586 | const String16* defPackage, | |
1587 | const char** outErrorMsg) const | |
1588 | { | |
1589 | String16 package, type, name; | |
1590 | if (!ResTable::expandResourceRef( | |
1591 | ref.string(), ref.size(), &package, &type, &name, | |
1592 | defType, defPackage ? defPackage:&mAssetsPackage, | |
1593 | outErrorMsg)) { | |
1594 | NOISY(printf("Expanding resource: ref=%s\n", | |
1595 | String8(ref).string())); | |
1596 | NOISY(printf("Expanding resource: defType=%s\n", | |
1597 | defType ? String8(*defType).string() : "NULL")); | |
1598 | NOISY(printf("Expanding resource: defPackage=%s\n", | |
1599 | defPackage ? String8(*defPackage).string() : "NULL")); | |
1600 | NOISY(printf("Expanding resource: ref=%s\n", String8(ref).string())); | |
1601 | NOISY(printf("Expanded resource: p=%s, t=%s, n=%s, res=0\n", | |
1602 | String8(package).string(), String8(type).string(), | |
1603 | String8(name).string())); | |
1604 | return 0; | |
1605 | } | |
1606 | uint32_t res = getResId(package, type, name); | |
1607 | NOISY(printf("Expanded resource: p=%s, t=%s, n=%s, res=%d\n", | |
1608 | String8(package).string(), String8(type).string(), | |
1609 | String8(name).string(), res)); | |
1610 | if (res == 0) { | |
1611 | if (outErrorMsg) | |
1612 | *outErrorMsg = "No resource found that matches the given name"; | |
1613 | } | |
1614 | return res; | |
1615 | } | |
1616 | ||
1617 | bool ResourceTable::isValidResourceName(const String16& s) | |
1618 | { | |
1619 | const char16_t* p = s.string(); | |
1620 | bool first = true; | |
1621 | while (*p) { | |
1622 | if ((*p >= 'a' && *p <= 'z') | |
1623 | || (*p >= 'A' && *p <= 'Z') | |
1624 | || *p == '_' | |
1625 | || (!first && *p >= '0' && *p <= '9')) { | |
1626 | first = false; | |
1627 | p++; | |
1628 | continue; | |
1629 | } | |
1630 | return false; | |
1631 | } | |
1632 | return true; | |
1633 | } | |
1634 | ||
1635 | bool ResourceTable::stringToValue(Res_value* outValue, StringPool* pool, | |
1636 | const String16& str, | |
1637 | bool preserveSpaces, bool coerceType, | |
1638 | uint32_t attrID, | |
1639 | const Vector<StringPool::entry_style_span>* style, | |
1640 | String16* outStr, void* accessorCookie, | |
1641 | uint32_t attrType) | |
1642 | { | |
1643 | String16 finalStr; | |
1644 | ||
1645 | bool res = true; | |
1646 | if (style == NULL || style->size() == 0) { | |
1647 | // Text is not styled so it can be any type... let's figure it out. | |
1648 | res = mAssets->getIncludedResources() | |
1649 | .stringToValue(outValue, &finalStr, str.string(), str.size(), preserveSpaces, | |
1650 | coerceType, attrID, NULL, &mAssetsPackage, this, | |
1651 | accessorCookie, attrType); | |
1652 | } else { | |
1653 | // Styled text can only be a string, and while collecting the style | |
1654 | // information we have already processed that string! | |
1655 | outValue->size = sizeof(Res_value); | |
1656 | outValue->res0 = 0; | |
1657 | outValue->dataType = outValue->TYPE_STRING; | |
1658 | outValue->data = 0; | |
1659 | finalStr = str; | |
1660 | } | |
1661 | ||
1662 | if (!res) { | |
1663 | return false; | |
1664 | } | |
1665 | ||
1666 | if (outValue->dataType == outValue->TYPE_STRING) { | |
1667 | // Should do better merging styles. | |
1668 | if (pool) { | |
1669 | if (style != NULL && style->size() > 0) { | |
1670 | outValue->data = pool->add(finalStr, *style); | |
1671 | } else { | |
1672 | outValue->data = pool->add(finalStr, true); | |
1673 | } | |
1674 | } else { | |
1675 | // Caller will fill this in later. | |
1676 | outValue->data = 0; | |
1677 | } | |
1678 | ||
1679 | if (outStr) { | |
1680 | *outStr = finalStr; | |
1681 | } | |
1682 | ||
1683 | } | |
1684 | ||
1685 | return true; | |
1686 | } | |
1687 | ||
1688 | uint32_t ResourceTable::getCustomResource( | |
1689 | const String16& package, const String16& type, const String16& name) const | |
1690 | { | |
1691 | //printf("getCustomResource: %s %s %s\n", String8(package).string(), | |
1692 | // String8(type).string(), String8(name).string()); | |
1693 | sp<Package> p = mPackages.valueFor(package); | |
1694 | if (p == NULL) return 0; | |
1695 | sp<Type> t = p->getTypes().valueFor(type); | |
1696 | if (t == NULL) return 0; | |
1697 | sp<ConfigList> c = t->getConfigs().valueFor(name); | |
1698 | if (c == NULL) return 0; | |
1699 | int32_t ei = c->getEntryIndex(); | |
1700 | if (ei < 0) return 0; | |
1701 | return getResId(p, t, ei); | |
1702 | } | |
1703 | ||
1704 | uint32_t ResourceTable::getCustomResourceWithCreation( | |
1705 | const String16& package, const String16& type, const String16& name, | |
1706 | const bool createIfNotFound) | |
1707 | { | |
1708 | uint32_t resId = getCustomResource(package, type, name); | |
1709 | if (resId != 0 || !createIfNotFound) { | |
1710 | return resId; | |
1711 | } | |
1712 | String16 value("false"); | |
1713 | ||
1714 | status_t status = addEntry(mCurrentXmlPos, package, type, name, value, NULL, NULL, true); | |
1715 | if (status == NO_ERROR) { | |
1716 | resId = getResId(package, type, name); | |
1717 | return resId; | |
1718 | } | |
1719 | return 0; | |
1720 | } | |
1721 | ||
1722 | uint32_t ResourceTable::getRemappedPackage(uint32_t origPackage) const | |
1723 | { | |
1724 | return origPackage; | |
1725 | } | |
1726 | ||
1727 | bool ResourceTable::getAttributeType(uint32_t attrID, uint32_t* outType) | |
1728 | { | |
1729 | //printf("getAttributeType #%08x\n", attrID); | |
1730 | Res_value value; | |
1731 | if (getItemValue(attrID, ResTable_map::ATTR_TYPE, &value)) { | |
1732 | //printf("getAttributeType #%08x (%s): #%08x\n", attrID, | |
1733 | // String8(getEntry(attrID)->getName()).string(), value.data); | |
1734 | *outType = value.data; | |
1735 | return true; | |
1736 | } | |
1737 | return false; | |
1738 | } | |
1739 | ||
1740 | bool ResourceTable::getAttributeMin(uint32_t attrID, uint32_t* outMin) | |
1741 | { | |
1742 | //printf("getAttributeMin #%08x\n", attrID); | |
1743 | Res_value value; | |
1744 | if (getItemValue(attrID, ResTable_map::ATTR_MIN, &value)) { | |
1745 | *outMin = value.data; | |
1746 | return true; | |
1747 | } | |
1748 | return false; | |
1749 | } | |
1750 | ||
1751 | bool ResourceTable::getAttributeMax(uint32_t attrID, uint32_t* outMax) | |
1752 | { | |
1753 | //printf("getAttributeMax #%08x\n", attrID); | |
1754 | Res_value value; | |
1755 | if (getItemValue(attrID, ResTable_map::ATTR_MAX, &value)) { | |
1756 | *outMax = value.data; | |
1757 | return true; | |
1758 | } | |
1759 | return false; | |
1760 | } | |
1761 | ||
1762 | uint32_t ResourceTable::getAttributeL10N(uint32_t attrID) | |
1763 | { | |
1764 | //printf("getAttributeL10N #%08x\n", attrID); | |
1765 | Res_value value; | |
1766 | if (getItemValue(attrID, ResTable_map::ATTR_L10N, &value)) { | |
1767 | return value.data; | |
1768 | } | |
1769 | return ResTable_map::L10N_NOT_REQUIRED; | |
1770 | } | |
1771 | ||
1772 | bool ResourceTable::getLocalizationSetting() | |
1773 | { | |
1774 | return mBundle->getRequireLocalization(); | |
1775 | } | |
1776 | ||
1777 | void ResourceTable::reportError(void* accessorCookie, const char* fmt, ...) | |
1778 | { | |
1779 | if (accessorCookie != NULL && fmt != NULL) { | |
1780 | AccessorCookie* ac = (AccessorCookie*)accessorCookie; | |
1781 | int retval=0; | |
1782 | char buf[1024]; | |
1783 | va_list ap; | |
1784 | va_start(ap, fmt); | |
1785 | retval = vsnprintf(buf, sizeof(buf), fmt, ap); | |
1786 | va_end(ap); | |
1787 | ac->sourcePos.error("Error: %s (at '%s' with value '%s').\n", | |
1788 | buf, ac->attr.string(), ac->value.string()); | |
1789 | } | |
1790 | } | |
1791 | ||
1792 | bool ResourceTable::getAttributeKeys( | |
1793 | uint32_t attrID, Vector<String16>* outKeys) | |
1794 | { | |
1795 | sp<const Entry> e = getEntry(attrID); | |
1796 | if (e != NULL) { | |
1797 | const size_t N = e->getBag().size(); | |
1798 | for (size_t i=0; i<N; i++) { | |
1799 | const String16& key = e->getBag().keyAt(i); | |
1800 | if (key.size() > 0 && key.string()[0] != '^') { | |
1801 | outKeys->add(key); | |
1802 | } | |
1803 | } | |
1804 | return true; | |
1805 | } | |
1806 | return false; | |
1807 | } | |
1808 | ||
1809 | bool ResourceTable::getAttributeEnum( | |
1810 | uint32_t attrID, const char16_t* name, size_t nameLen, | |
1811 | Res_value* outValue) | |
1812 | { | |
1813 | //printf("getAttributeEnum #%08x %s\n", attrID, String8(name, nameLen).string()); | |
1814 | String16 nameStr(name, nameLen); | |
1815 | sp<const Entry> e = getEntry(attrID); | |
1816 | if (e != NULL) { | |
1817 | const size_t N = e->getBag().size(); | |
1818 | for (size_t i=0; i<N; i++) { | |
1819 | //printf("Comparing %s to %s\n", String8(name, nameLen).string(), | |
1820 | // String8(e->getBag().keyAt(i)).string()); | |
1821 | if (e->getBag().keyAt(i) == nameStr) { | |
1822 | return getItemValue(attrID, e->getBag().valueAt(i).bagKeyId, outValue); | |
1823 | } | |
1824 | } | |
1825 | } | |
1826 | return false; | |
1827 | } | |
1828 | ||
1829 | bool ResourceTable::getAttributeFlags( | |
1830 | uint32_t attrID, const char16_t* name, size_t nameLen, | |
1831 | Res_value* outValue) | |
1832 | { | |
1833 | outValue->dataType = Res_value::TYPE_INT_HEX; | |
1834 | outValue->data = 0; | |
1835 | ||
1836 | //printf("getAttributeFlags #%08x %s\n", attrID, String8(name, nameLen).string()); | |
1837 | String16 nameStr(name, nameLen); | |
1838 | sp<const Entry> e = getEntry(attrID); | |
1839 | if (e != NULL) { | |
1840 | const size_t N = e->getBag().size(); | |
1841 | ||
1842 | const char16_t* end = name + nameLen; | |
1843 | const char16_t* pos = name; | |
1844 | bool failed = false; | |
1845 | while (pos < end && !failed) { | |
1846 | const char16_t* start = pos; | |
1847 | end++; | |
1848 | while (pos < end && *pos != '|') { | |
1849 | pos++; | |
1850 | } | |
1851 | ||
1852 | String16 nameStr(start, pos-start); | |
1853 | size_t i; | |
1854 | for (i=0; i<N; i++) { | |
1855 | //printf("Comparing \"%s\" to \"%s\"\n", String8(nameStr).string(), | |
1856 | // String8(e->getBag().keyAt(i)).string()); | |
1857 | if (e->getBag().keyAt(i) == nameStr) { | |
1858 | Res_value val; | |
1859 | bool got = getItemValue(attrID, e->getBag().valueAt(i).bagKeyId, &val); | |
1860 | if (!got) { | |
1861 | return false; | |
1862 | } | |
1863 | //printf("Got value: 0x%08x\n", val.data); | |
1864 | outValue->data |= val.data; | |
1865 | break; | |
1866 | } | |
1867 | } | |
1868 | ||
1869 | if (i >= N) { | |
1870 | // Didn't find this flag identifier. | |
1871 | return false; | |
1872 | } | |
1873 | if (pos < end) { | |
1874 | pos++; | |
1875 | } | |
1876 | } | |
1877 | ||
1878 | return true; | |
1879 | } | |
1880 | return false; | |
1881 | } | |
1882 | ||
1883 | status_t ResourceTable::assignResourceIds() | |
1884 | { | |
1885 | const size_t N = mOrderedPackages.size(); | |
1886 | size_t pi; | |
1887 | status_t firstError = NO_ERROR; | |
1888 | ||
1889 | // First generate all bag attributes and assign indices. | |
1890 | for (pi=0; pi<N; pi++) { | |
1891 | sp<Package> p = mOrderedPackages.itemAt(pi); | |
1892 | if (p == NULL || p->getTypes().size() == 0) { | |
1893 | // Empty, skip! | |
1894 | continue; | |
1895 | } | |
1896 | ||
1897 | status_t err = p->applyPublicTypeOrder(); | |
1898 | if (err != NO_ERROR && firstError == NO_ERROR) { | |
1899 | firstError = err; | |
1900 | } | |
1901 | ||
1902 | // Generate attributes... | |
1903 | const size_t N = p->getOrderedTypes().size(); | |
1904 | size_t ti; | |
1905 | for (ti=0; ti<N; ti++) { | |
1906 | sp<Type> t = p->getOrderedTypes().itemAt(ti); | |
1907 | if (t == NULL) { | |
1908 | continue; | |
1909 | } | |
1910 | const size_t N = t->getOrderedConfigs().size(); | |
1911 | for (size_t ci=0; ci<N; ci++) { | |
1912 | sp<ConfigList> c = t->getOrderedConfigs().itemAt(ci); | |
1913 | if (c == NULL) { | |
1914 | continue; | |
1915 | } | |
1916 | const size_t N = c->getEntries().size(); | |
1917 | for (size_t ei=0; ei<N; ei++) { | |
1918 | sp<Entry> e = c->getEntries().valueAt(ei); | |
1919 | if (e == NULL) { | |
1920 | continue; | |
1921 | } | |
1922 | status_t err = e->generateAttributes(this, p->getName()); | |
1923 | if (err != NO_ERROR && firstError == NO_ERROR) { | |
1924 | firstError = err; | |
1925 | } | |
1926 | } | |
1927 | } | |
1928 | } | |
1929 | ||
1930 | const SourcePos unknown(String8("????"), 0); | |
1931 | sp<Type> attr = p->getType(String16("attr"), unknown); | |
1932 | ||
1933 | // Assign indices... | |
1934 | for (ti=0; ti<N; ti++) { | |
1935 | sp<Type> t = p->getOrderedTypes().itemAt(ti); | |
1936 | if (t == NULL) { | |
1937 | continue; | |
1938 | } | |
1939 | err = t->applyPublicEntryOrder(); | |
1940 | if (err != NO_ERROR && firstError == NO_ERROR) { | |
1941 | firstError = err; | |
1942 | } | |
1943 | ||
1944 | const size_t N = t->getOrderedConfigs().size(); | |
1945 | t->setIndex(ti+1); | |
1946 | ||
1947 | LOG_ALWAYS_FATAL_IF(ti == 0 && attr != t, | |
1948 | "First type is not attr!"); | |
1949 | ||
1950 | for (size_t ei=0; ei<N; ei++) { | |
1951 | sp<ConfigList> c = t->getOrderedConfigs().itemAt(ei); | |
1952 | if (c == NULL) { | |
1953 | continue; | |
1954 | } | |
1955 | c->setEntryIndex(ei); | |
1956 | } | |
1957 | } | |
1958 | ||
1959 | // Assign resource IDs to keys in bags... | |
1960 | for (ti=0; ti<N; ti++) { | |
1961 | sp<Type> t = p->getOrderedTypes().itemAt(ti); | |
1962 | if (t == NULL) { | |
1963 | continue; | |
1964 | } | |
1965 | const size_t N = t->getOrderedConfigs().size(); | |
1966 | for (size_t ci=0; ci<N; ci++) { | |
1967 | sp<ConfigList> c = t->getOrderedConfigs().itemAt(ci); | |
1968 | //printf("Ordered config #%d: %p\n", ci, c.get()); | |
1969 | const size_t N = c->getEntries().size(); | |
1970 | for (size_t ei=0; ei<N; ei++) { | |
1971 | sp<Entry> e = c->getEntries().valueAt(ei); | |
1972 | if (e == NULL) { | |
1973 | continue; | |
1974 | } | |
1975 | status_t err = e->assignResourceIds(this, p->getName()); | |
1976 | if (err != NO_ERROR && firstError == NO_ERROR) { | |
1977 | firstError = err; | |
1978 | } | |
1979 | } | |
1980 | } | |
1981 | } | |
1982 | } | |
1983 | return firstError; | |
1984 | } | |
1985 | ||
1986 | status_t ResourceTable::addSymbols(const sp<AaptSymbols>& outSymbols) { | |
1987 | const size_t N = mOrderedPackages.size(); | |
1988 | size_t pi; | |
1989 | ||
1990 | for (pi=0; pi<N; pi++) { | |
1991 | sp<Package> p = mOrderedPackages.itemAt(pi); | |
1992 | if (p->getTypes().size() == 0) { | |
1993 | // Empty, skip! | |
1994 | continue; | |
1995 | } | |
1996 | ||
1997 | const size_t N = p->getOrderedTypes().size(); | |
1998 | size_t ti; | |
1999 | ||
2000 | for (ti=0; ti<N; ti++) { | |
2001 | sp<Type> t = p->getOrderedTypes().itemAt(ti); | |
2002 | if (t == NULL) { | |
2003 | continue; | |
2004 | } | |
2005 | const size_t N = t->getOrderedConfigs().size(); | |
2006 | sp<AaptSymbols> typeSymbols; | |
2007 | typeSymbols = outSymbols->addNestedSymbol(String8(t->getName()), t->getPos()); | |
2008 | for (size_t ci=0; ci<N; ci++) { | |
2009 | sp<ConfigList> c = t->getOrderedConfigs().itemAt(ci); | |
2010 | if (c == NULL) { | |
2011 | continue; | |
2012 | } | |
2013 | uint32_t rid = getResId(p, t, ci); | |
2014 | if (rid == 0) { | |
2015 | return UNKNOWN_ERROR; | |
2016 | } | |
2017 | if (Res_GETPACKAGE(rid) == (size_t)(p->getAssignedId()-1)) { | |
2018 | typeSymbols->addSymbol(String8(c->getName()), rid, c->getPos()); | |
2019 | ||
2020 | String16 comment(c->getComment()); | |
2021 | typeSymbols->appendComment(String8(c->getName()), comment, c->getPos()); | |
2022 | //printf("Type symbol %s comment: %s\n", String8(e->getName()).string(), | |
2023 | // String8(comment).string()); | |
2024 | comment = c->getTypeComment(); | |
2025 | typeSymbols->appendTypeComment(String8(c->getName()), comment); | |
2026 | } else { | |
2027 | #if 0 | |
2028 | printf("**** NO MATCH: 0x%08x vs 0x%08x\n", | |
2029 | Res_GETPACKAGE(rid), p->getAssignedId()); | |
2030 | #endif | |
2031 | } | |
2032 | } | |
2033 | } | |
2034 | } | |
2035 | return NO_ERROR; | |
2036 | } | |
2037 | ||
2038 | ||
2039 | status_t | |
2040 | ResourceFilter::parse(const char* arg) | |
2041 | { | |
2042 | if (arg == NULL) { | |
2043 | return 0; | |
2044 | } | |
2045 | ||
2046 | const char* p = arg; | |
2047 | const char* q; | |
2048 | ||
2049 | while (true) { | |
2050 | q = strchr(p, ','); | |
2051 | if (q == NULL) { | |
2052 | q = p + strlen(p); | |
2053 | } | |
2054 | ||
2055 | String8 part(p, q-p); | |
2056 | ||
2057 | if (part == "zz_ZZ") { | |
2058 | mContainsPseudo = true; | |
2059 | } | |
2060 | int axis; | |
2061 | uint32_t value; | |
2062 | if (AaptGroupEntry::parseNamePart(part, &axis, &value)) { | |
2063 | fprintf(stderr, "Invalid configuration: %s\n", arg); | |
2064 | fprintf(stderr, " "); | |
2065 | for (int i=0; i<p-arg; i++) { | |
2066 | fprintf(stderr, " "); | |
2067 | } | |
2068 | for (int i=0; i<q-p; i++) { | |
2069 | fprintf(stderr, "^"); | |
2070 | } | |
2071 | fprintf(stderr, "\n"); | |
2072 | return 1; | |
2073 | } | |
2074 | ||
2075 | ssize_t index = mData.indexOfKey(axis); | |
2076 | if (index < 0) { | |
2077 | mData.add(axis, SortedVector<uint32_t>()); | |
2078 | } | |
2079 | SortedVector<uint32_t>& sv = mData.editValueFor(axis); | |
2080 | sv.add(value); | |
2081 | // if it's a locale with a region, also match an unmodified locale of the | |
2082 | // same language | |
2083 | if (axis == AXIS_LANGUAGE) { | |
2084 | if (value & 0xffff0000) { | |
2085 | sv.add(value & 0x0000ffff); | |
2086 | } | |
2087 | } | |
2088 | p = q; | |
2089 | if (!*p) break; | |
2090 | p++; | |
2091 | } | |
2092 | ||
2093 | return NO_ERROR; | |
2094 | } | |
2095 | ||
2096 | bool | |
2097 | ResourceFilter::match(int axis, uint32_t value) | |
2098 | { | |
2099 | if (value == 0) { | |
2100 | // they didn't specify anything so take everything | |
2101 | return true; | |
2102 | } | |
2103 | ssize_t index = mData.indexOfKey(axis); | |
2104 | if (index < 0) { | |
2105 | // we didn't request anything on this axis so take everything | |
2106 | return true; | |
2107 | } | |
2108 | const SortedVector<uint32_t>& sv = mData.valueAt(index); | |
2109 | return sv.indexOf(value) >= 0; | |
2110 | } | |
2111 | ||
2112 | bool | |
2113 | ResourceFilter::match(const ResTable_config& config) | |
2114 | { | |
2115 | if (config.locale) { | |
2116 | uint32_t locale = (config.country[1] << 24) | (config.country[0] << 16) | |
2117 | | (config.language[1] << 8) | (config.language[0]); | |
2118 | if (!match(AXIS_LANGUAGE, locale)) { | |
2119 | return false; | |
2120 | } | |
2121 | } | |
2122 | if (!match(AXIS_ORIENTATION, config.orientation)) { | |
2123 | return false; | |
2124 | } | |
2125 | if (!match(AXIS_DENSITY, config.density)) { | |
2126 | return false; | |
2127 | } | |
2128 | if (!match(AXIS_TOUCHSCREEN, config.touchscreen)) { | |
2129 | return false; | |
2130 | } | |
2131 | if (!match(AXIS_KEYSHIDDEN, config.inputFlags)) { | |
2132 | return false; | |
2133 | } | |
2134 | if (!match(AXIS_KEYBOARD, config.keyboard)) { | |
2135 | return false; | |
2136 | } | |
2137 | if (!match(AXIS_NAVIGATION, config.navigation)) { | |
2138 | return false; | |
2139 | } | |
2140 | if (!match(AXIS_SCREENSIZE, config.screenSize)) { | |
2141 | return false; | |
2142 | } | |
2143 | if (!match(AXIS_VERSION, config.version)) { | |
2144 | return false; | |
2145 | } | |
2146 | return true; | |
2147 | } | |
2148 | ||
2149 | status_t ResourceTable::flatten(Bundle* bundle, const sp<AaptFile>& dest) | |
2150 | { | |
2151 | ResourceFilter filter; | |
2152 | status_t err = filter.parse(bundle->getConfigurations()); | |
2153 | if (err != NO_ERROR) { | |
2154 | return err; | |
2155 | } | |
2156 | ||
2157 | const size_t N = mOrderedPackages.size(); | |
2158 | size_t pi; | |
2159 | ||
2160 | // Iterate through all data, collecting all values (strings, | |
2161 | // references, etc). | |
2162 | StringPool valueStrings; | |
2163 | for (pi=0; pi<N; pi++) { | |
2164 | sp<Package> p = mOrderedPackages.itemAt(pi); | |
2165 | if (p->getTypes().size() == 0) { | |
2166 | // Empty, skip! | |
2167 | continue; | |
2168 | } | |
2169 | ||
2170 | StringPool typeStrings; | |
2171 | StringPool keyStrings; | |
2172 | ||
2173 | const size_t N = p->getOrderedTypes().size(); | |
2174 | for (size_t ti=0; ti<N; ti++) { | |
2175 | sp<Type> t = p->getOrderedTypes().itemAt(ti); | |
2176 | if (t == NULL) { | |
2177 | typeStrings.add(String16("<empty>"), false); | |
2178 | continue; | |
2179 | } | |
2180 | typeStrings.add(t->getName(), false); | |
2181 | ||
2182 | const size_t N = t->getOrderedConfigs().size(); | |
2183 | for (size_t ci=0; ci<N; ci++) { | |
2184 | sp<ConfigList> c = t->getOrderedConfigs().itemAt(ci); | |
2185 | if (c == NULL) { | |
2186 | continue; | |
2187 | } | |
2188 | const size_t N = c->getEntries().size(); | |
2189 | for (size_t ei=0; ei<N; ei++) { | |
2190 | sp<Entry> e = c->getEntries().valueAt(ei); | |
2191 | if (e == NULL) { | |
2192 | continue; | |
2193 | } | |
2194 | e->setNameIndex(keyStrings.add(e->getName(), true)); | |
2195 | status_t err = e->prepareFlatten(&valueStrings, this); | |
2196 | if (err != NO_ERROR) { | |
2197 | return err; | |
2198 | } | |
2199 | } | |
2200 | } | |
2201 | } | |
2202 | ||
2203 | p->setTypeStrings(typeStrings.createStringBlock()); | |
2204 | p->setKeyStrings(keyStrings.createStringBlock()); | |
2205 | } | |
2206 | ||
2207 | ssize_t strAmt = 0; | |
2208 | ||
2209 | // Now build the array of package chunks. | |
2210 | Vector<sp<AaptFile> > flatPackages; | |
2211 | for (pi=0; pi<N; pi++) { | |
2212 | sp<Package> p = mOrderedPackages.itemAt(pi); | |
2213 | if (p->getTypes().size() == 0) { | |
2214 | // Empty, skip! | |
2215 | continue; | |
2216 | } | |
2217 | ||
2218 | const size_t N = p->getTypeStrings().size(); | |
2219 | ||
2220 | const size_t baseSize = sizeof(ResTable_package); | |
2221 | ||
2222 | // Start the package data. | |
2223 | sp<AaptFile> data = new AaptFile(String8(), AaptGroupEntry(), String8()); | |
2224 | ResTable_package* header = (ResTable_package*)data->editData(baseSize); | |
2225 | if (header == NULL) { | |
2226 | fprintf(stderr, "ERROR: out of memory creating ResTable_package\n"); | |
2227 | return NO_MEMORY; | |
2228 | } | |
2229 | memset(header, 0, sizeof(*header)); | |
2230 | header->header.type = htods(RES_TABLE_PACKAGE_TYPE); | |
2231 | header->header.headerSize = htods(sizeof(*header)); | |
2232 | header->id = htodl(p->getAssignedId()); | |
2233 | strcpy16_htod(header->name, p->getName().string()); | |
2234 | ||
2235 | // Write the string blocks. | |
2236 | const size_t typeStringsStart = data->getSize(); | |
2237 | sp<AaptFile> strFile = p->getTypeStringsData(); | |
2238 | ssize_t amt = data->writeData(strFile->getData(), strFile->getSize()); | |
2239 | #if PRINT_STRING_METRICS | |
2240 | fprintf(stderr, "**** type strings: %d\n", amt); | |
2241 | #endif | |
2242 | strAmt += amt; | |
2243 | if (amt < 0) { | |
2244 | return amt; | |
2245 | } | |
2246 | const size_t keyStringsStart = data->getSize(); | |
2247 | strFile = p->getKeyStringsData(); | |
2248 | amt = data->writeData(strFile->getData(), strFile->getSize()); | |
2249 | #if PRINT_STRING_METRICS | |
2250 | fprintf(stderr, "**** key strings: %d\n", amt); | |
2251 | #endif | |
2252 | strAmt += amt; | |
2253 | if (amt < 0) { | |
2254 | return amt; | |
2255 | } | |
2256 | ||
2257 | // Build the type chunks inside of this package. | |
2258 | for (size_t ti=0; ti<N; ti++) { | |
2259 | // Retrieve them in the same order as the type string block. | |
2260 | size_t len; | |
2261 | String16 typeName(p->getTypeStrings().stringAt(ti, &len)); | |
2262 | sp<Type> t = p->getTypes().valueFor(typeName); | |
2263 | LOG_ALWAYS_FATAL_IF(t == NULL && typeName != String16("<empty>"), | |
2264 | "Type name %s not found", | |
2265 | String8(typeName).string()); | |
2266 | ||
2267 | const size_t N = t != NULL ? t->getOrderedConfigs().size() : 0; | |
2268 | ||
2269 | // First write the typeSpec chunk, containing information about | |
2270 | // each resource entry in this type. | |
2271 | { | |
2272 | const size_t typeSpecSize = sizeof(ResTable_typeSpec) + sizeof(uint32_t)*N; | |
2273 | const size_t typeSpecStart = data->getSize(); | |
2274 | ResTable_typeSpec* tsHeader = (ResTable_typeSpec*) | |
2275 | (((uint8_t*)data->editData(typeSpecStart+typeSpecSize)) + typeSpecStart); | |
2276 | if (tsHeader == NULL) { | |
2277 | fprintf(stderr, "ERROR: out of memory creating ResTable_typeSpec\n"); | |
2278 | return NO_MEMORY; | |
2279 | } | |
2280 | memset(tsHeader, 0, sizeof(*tsHeader)); | |
2281 | tsHeader->header.type = htods(RES_TABLE_TYPE_SPEC_TYPE); | |
2282 | tsHeader->header.headerSize = htods(sizeof(*tsHeader)); | |
2283 | tsHeader->header.size = htodl(typeSpecSize); | |
2284 | tsHeader->id = ti+1; | |
2285 | tsHeader->entryCount = htodl(N); | |
2286 | ||
2287 | uint32_t* typeSpecFlags = (uint32_t*) | |
2288 | (((uint8_t*)data->editData()) | |
2289 | + typeSpecStart + sizeof(ResTable_typeSpec)); | |
2290 | memset(typeSpecFlags, 0, sizeof(uint32_t)*N); | |
2291 | ||
2292 | for (size_t ei=0; ei<N; ei++) { | |
2293 | sp<ConfigList> cl = t->getOrderedConfigs().itemAt(ei); | |
2294 | if (cl->getPublic()) { | |
2295 | typeSpecFlags[ei] |= htodl(ResTable_typeSpec::SPEC_PUBLIC); | |
2296 | } | |
2297 | const size_t CN = cl->getEntries().size(); | |
2298 | for (size_t ci=0; ci<CN; ci++) { | |
2299 | if (!filter.match(cl->getEntries().keyAt(ci))) { | |
2300 | continue; | |
2301 | } | |
2302 | for (size_t cj=ci+1; cj<CN; cj++) { | |
2303 | if (!filter.match(cl->getEntries().keyAt(cj))) { | |
2304 | continue; | |
2305 | } | |
2306 | typeSpecFlags[ei] |= htodl( | |
2307 | cl->getEntries().keyAt(ci).diff(cl->getEntries().keyAt(cj))); | |
2308 | } | |
2309 | } | |
2310 | } | |
2311 | } | |
2312 | ||
2313 | // We need to write one type chunk for each configuration for | |
2314 | // which we have entries in this type. | |
2315 | const size_t NC = t->getUniqueConfigs().size(); | |
2316 | ||
2317 | const size_t typeSize = sizeof(ResTable_type) + sizeof(uint32_t)*N; | |
2318 | ||
2319 | for (size_t ci=0; ci<NC; ci++) { | |
2320 | ConfigDescription config = t->getUniqueConfigs().itemAt(ci); | |
2321 | ||
2322 | NOISY(printf("Writing config %d config: imsi:%d/%d lang:%c%c cnt:%c%c " | |
2323 | "orien:%d touch:%d density:%d key:%d inp:%d nav:%d w:%d h:%d\n", | |
2324 | ti+1, | |
2325 | config.mcc, config.mnc, | |
2326 | config.language[0] ? config.language[0] : '-', | |
2327 | config.language[1] ? config.language[1] : '-', | |
2328 | config.country[0] ? config.country[0] : '-', | |
2329 | config.country[1] ? config.country[1] : '-', | |
2330 | config.orientation, | |
2331 | config.touchscreen, | |
2332 | config.density, | |
2333 | config.keyboard, | |
2334 | config.inputFlags, | |
2335 | config.navigation, | |
2336 | config.screenWidth, | |
2337 | config.screenHeight)); | |
2338 | ||
2339 | if (!filter.match(config)) { | |
2340 | continue; | |
2341 | } | |
2342 | ||
2343 | const size_t typeStart = data->getSize(); | |
2344 | ||
2345 | ResTable_type* tHeader = (ResTable_type*) | |
2346 | (((uint8_t*)data->editData(typeStart+typeSize)) + typeStart); | |
2347 | if (tHeader == NULL) { | |
2348 | fprintf(stderr, "ERROR: out of memory creating ResTable_type\n"); | |
2349 | return NO_MEMORY; | |
2350 | } | |
2351 | ||
2352 | memset(tHeader, 0, sizeof(*tHeader)); | |
2353 | tHeader->header.type = htods(RES_TABLE_TYPE_TYPE); | |
2354 | tHeader->header.headerSize = htods(sizeof(*tHeader)); | |
2355 | tHeader->id = ti+1; | |
2356 | tHeader->entryCount = htodl(N); | |
2357 | tHeader->entriesStart = htodl(typeSize); | |
2358 | tHeader->config = config; | |
2359 | NOISY(printf("Writing type %d config: imsi:%d/%d lang:%c%c cnt:%c%c " | |
2360 | "orien:%d touch:%d density:%d key:%d inp:%d nav:%d w:%d h:%d\n", | |
2361 | ti+1, | |
2362 | tHeader->config.mcc, tHeader->config.mnc, | |
2363 | tHeader->config.language[0] ? tHeader->config.language[0] : '-', | |
2364 | tHeader->config.language[1] ? tHeader->config.language[1] : '-', | |
2365 | tHeader->config.country[0] ? tHeader->config.country[0] : '-', | |
2366 | tHeader->config.country[1] ? tHeader->config.country[1] : '-', | |
2367 | tHeader->config.orientation, | |
2368 | tHeader->config.touchscreen, | |
2369 | tHeader->config.density, | |
2370 | tHeader->config.keyboard, | |
2371 | tHeader->config.inputFlags, | |
2372 | tHeader->config.navigation, | |
2373 | tHeader->config.screenWidth, | |
2374 | tHeader->config.screenHeight)); | |
2375 | tHeader->config.swapHtoD(); | |
2376 | ||
2377 | // Build the entries inside of this type. | |
2378 | for (size_t ei=0; ei<N; ei++) { | |
2379 | sp<ConfigList> cl = t->getOrderedConfigs().itemAt(ei); | |
2380 | sp<Entry> e = cl->getEntries().valueFor(config); | |
2381 | ||
2382 | // Set the offset for this entry in its type. | |
2383 | uint32_t* index = (uint32_t*) | |
2384 | (((uint8_t*)data->editData()) | |
2385 | + typeStart + sizeof(ResTable_type)); | |
2386 | if (e != NULL) { | |
2387 | index[ei] = htodl(data->getSize()-typeStart-typeSize); | |
2388 | ||
2389 | // Create the entry. | |
2390 | ssize_t amt = e->flatten(bundle, data, cl->getPublic()); | |
2391 | if (amt < 0) { | |
2392 | return amt; | |
2393 | } | |
2394 | } else { | |
2395 | index[ei] = htodl(ResTable_type::NO_ENTRY); | |
2396 | } | |
2397 | } | |
2398 | ||
2399 | // Fill in the rest of the type information. | |
2400 | tHeader = (ResTable_type*) | |
2401 | (((uint8_t*)data->editData()) + typeStart); | |
2402 | tHeader->header.size = htodl(data->getSize()-typeStart); | |
2403 | } | |
2404 | } | |
2405 | ||
2406 | // Fill in the rest of the package information. | |
2407 | header = (ResTable_package*)data->editData(); | |
2408 | header->header.size = htodl(data->getSize()); | |
2409 | header->typeStrings = htodl(typeStringsStart); | |
2410 | header->lastPublicType = htodl(p->getTypeStrings().size()); | |
2411 | header->keyStrings = htodl(keyStringsStart); | |
2412 | header->lastPublicKey = htodl(p->getKeyStrings().size()); | |
2413 | ||
2414 | flatPackages.add(data); | |
2415 | } | |
2416 | ||
2417 | // And now write out the final chunks. | |
2418 | const size_t dataStart = dest->getSize(); | |
2419 | ||
2420 | { | |
2421 | // blah | |
2422 | ResTable_header header; | |
2423 | memset(&header, 0, sizeof(header)); | |
2424 | header.header.type = htods(RES_TABLE_TYPE); | |
2425 | header.header.headerSize = htods(sizeof(header)); | |
2426 | header.packageCount = htodl(flatPackages.size()); | |
2427 | status_t err = dest->writeData(&header, sizeof(header)); | |
2428 | if (err != NO_ERROR) { | |
2429 | fprintf(stderr, "ERROR: out of memory creating ResTable_header\n"); | |
2430 | return err; | |
2431 | } | |
2432 | } | |
2433 | ||
2434 | ssize_t strStart = dest->getSize(); | |
2435 | err = valueStrings.writeStringBlock(dest); | |
2436 | if (err != NO_ERROR) { | |
2437 | return err; | |
2438 | } | |
2439 | ||
2440 | ssize_t amt = (dest->getSize()-strStart); | |
2441 | strAmt += amt; | |
2442 | #if PRINT_STRING_METRICS | |
2443 | fprintf(stderr, "**** value strings: %d\n", amt); | |
2444 | fprintf(stderr, "**** total strings: %d\n", strAmt); | |
2445 | #endif | |
2446 | ||
2447 | for (pi=0; pi<flatPackages.size(); pi++) { | |
2448 | err = dest->writeData(flatPackages[pi]->getData(), | |
2449 | flatPackages[pi]->getSize()); | |
2450 | if (err != NO_ERROR) { | |
2451 | fprintf(stderr, "ERROR: out of memory creating package chunk for ResTable_header\n"); | |
2452 | return err; | |
2453 | } | |
2454 | } | |
2455 | ||
2456 | ResTable_header* header = (ResTable_header*) | |
2457 | (((uint8_t*)dest->getData()) + dataStart); | |
2458 | header->header.size = htodl(dest->getSize() - dataStart); | |
2459 | ||
2460 | NOISY(aout << "Resource table:" | |
2461 | << HexDump(dest->getData(), dest->getSize()) << endl); | |
2462 | ||
2463 | #if PRINT_STRING_METRICS | |
2464 | fprintf(stderr, "**** total resource table size: %d / %d%% strings\n", | |
2465 | dest->getSize(), (strAmt*100)/dest->getSize()); | |
2466 | #endif | |
2467 | ||
2468 | return NO_ERROR; | |
2469 | } | |
2470 | ||
2471 | void ResourceTable::writePublicDefinitions(const String16& package, FILE* fp) | |
2472 | { | |
2473 | fprintf(fp, | |
2474 | "<!-- This file contains <public> resource definitions for all\n" | |
2475 | " resources that were generated from the source data. -->\n" | |
2476 | "\n" | |
2477 | "<resources>\n"); | |
2478 | ||
2479 | writePublicDefinitions(package, fp, true); | |
2480 | writePublicDefinitions(package, fp, false); | |
2481 | ||
2482 | fprintf(fp, | |
2483 | "\n" | |
2484 | "</resources>\n"); | |
2485 | } | |
2486 | ||
2487 | void ResourceTable::writePublicDefinitions(const String16& package, FILE* fp, bool pub) | |
2488 | { | |
2489 | bool didHeader = false; | |
2490 | ||
2491 | sp<Package> pkg = mPackages.valueFor(package); | |
2492 | if (pkg != NULL) { | |
2493 | const size_t NT = pkg->getOrderedTypes().size(); | |
2494 | for (size_t i=0; i<NT; i++) { | |
2495 | sp<Type> t = pkg->getOrderedTypes().itemAt(i); | |
2496 | if (t == NULL) { | |
2497 | continue; | |
2498 | } | |
2499 | ||
2500 | bool didType = false; | |
2501 | ||
2502 | const size_t NC = t->getOrderedConfigs().size(); | |
2503 | for (size_t j=0; j<NC; j++) { | |
2504 | sp<ConfigList> c = t->getOrderedConfigs().itemAt(j); | |
2505 | if (c == NULL) { | |
2506 | continue; | |
2507 | } | |
2508 | ||
2509 | if (c->getPublic() != pub) { | |
2510 | continue; | |
2511 | } | |
2512 | ||
2513 | if (!didType) { | |
2514 | fprintf(fp, "\n"); | |
2515 | didType = true; | |
2516 | } | |
2517 | if (!didHeader) { | |
2518 | if (pub) { | |
2519 | fprintf(fp," <!-- PUBLIC SECTION. These resources have been declared public.\n"); | |
2520 | fprintf(fp," Changes to these definitions will break binary compatibility. -->\n\n"); | |
2521 | } else { | |
2522 | fprintf(fp," <!-- PRIVATE SECTION. These resources have not been declared public.\n"); | |
2523 | fprintf(fp," You can make them public my moving these lines into a file in res/values. -->\n\n"); | |
2524 | } | |
2525 | didHeader = true; | |
2526 | } | |
2527 | if (!pub) { | |
2528 | const size_t NE = c->getEntries().size(); | |
2529 | for (size_t k=0; k<NE; k++) { | |
2530 | const SourcePos& pos = c->getEntries().valueAt(k)->getPos(); | |
2531 | if (pos.file != "") { | |
2532 | fprintf(fp," <!-- Declared at %s:%d -->\n", | |
2533 | pos.file.string(), pos.line); | |
2534 | } | |
2535 | } | |
2536 | } | |
2537 | fprintf(fp, " <public type=\"%s\" name=\"%s\" id=\"0x%08x\" />\n", | |
2538 | String8(t->getName()).string(), | |
2539 | String8(c->getName()).string(), | |
2540 | getResId(pkg, t, c->getEntryIndex())); | |
2541 | } | |
2542 | } | |
2543 | } | |
2544 | } | |
2545 | ||
2546 | ResourceTable::Item::Item(const SourcePos& _sourcePos, | |
2547 | bool _isId, | |
2548 | const String16& _value, | |
2549 | const Vector<StringPool::entry_style_span>* _style, | |
2550 | int32_t _format) | |
2551 | : sourcePos(_sourcePos) | |
2552 | , isId(_isId) | |
2553 | , value(_value) | |
2554 | , format(_format) | |
2555 | , bagKeyId(0) | |
2556 | , evaluating(false) | |
2557 | { | |
2558 | if (_style) { | |
2559 | style = *_style; | |
2560 | } | |
2561 | } | |
2562 | ||
2563 | status_t ResourceTable::Entry::makeItABag(const SourcePos& sourcePos) | |
2564 | { | |
2565 | if (mType == TYPE_BAG) { | |
2566 | return NO_ERROR; | |
2567 | } | |
2568 | if (mType == TYPE_UNKNOWN) { | |
2569 | mType = TYPE_BAG; | |
2570 | return NO_ERROR; | |
2571 | } | |
2572 | sourcePos.error("Resource entry %s is already defined as a single item.\n" | |
2573 | "%s:%d: Originally defined here.\n", | |
2574 | String8(mName).string(), | |
2575 | mItem.sourcePos.file.string(), mItem.sourcePos.line); | |
2576 | return UNKNOWN_ERROR; | |
2577 | } | |
2578 | ||
2579 | status_t ResourceTable::Entry::setItem(const SourcePos& sourcePos, | |
2580 | const String16& value, | |
2581 | const Vector<StringPool::entry_style_span>* style, | |
2582 | int32_t format) | |
2583 | { | |
2584 | Item item(sourcePos, false, value, style); | |
2585 | ||
2586 | if (mType == TYPE_BAG) { | |
2587 | const Item& item(mBag.valueAt(0)); | |
2588 | sourcePos.error("Resource entry %s is already defined as a bag.\n" | |
2589 | "%s:%d: Originally defined here.\n", | |
2590 | String8(mName).string(), | |
2591 | item.sourcePos.file.string(), item.sourcePos.line); | |
2592 | return UNKNOWN_ERROR; | |
2593 | } | |
2594 | if (mType != TYPE_UNKNOWN) { | |
2595 | sourcePos.error("Resource entry %s is already defined.\n" | |
2596 | "%s:%d: Originally defined here.\n", | |
2597 | String8(mName).string(), | |
2598 | mItem.sourcePos.file.string(), mItem.sourcePos.line); | |
2599 | return UNKNOWN_ERROR; | |
2600 | } | |
2601 | ||
2602 | mType = TYPE_ITEM; | |
2603 | mItem = item; | |
2604 | mItemFormat = format; | |
2605 | return NO_ERROR; | |
2606 | } | |
2607 | ||
2608 | status_t ResourceTable::Entry::addToBag(const SourcePos& sourcePos, | |
2609 | const String16& key, const String16& value, | |
2610 | const Vector<StringPool::entry_style_span>* style, | |
2611 | bool replace, bool isId, int32_t format) | |
2612 | { | |
2613 | status_t err = makeItABag(sourcePos); | |
2614 | if (err != NO_ERROR) { | |
2615 | return err; | |
2616 | } | |
2617 | ||
2618 | Item item(sourcePos, isId, value, style, format); | |
2619 | ||
2620 | // XXX NOTE: there is an error if you try to have a bag with two keys, | |
2621 | // one an attr and one an id, with the same name. Not something we | |
2622 | // currently ever have to worry about. | |
2623 | ssize_t origKey = mBag.indexOfKey(key); | |
2624 | if (origKey >= 0) { | |
2625 | if (!replace) { | |
2626 | const Item& item(mBag.valueAt(origKey)); | |
2627 | sourcePos.error("Resource entry %s already has bag item %s.\n" | |
2628 | "%s:%d: Originally defined here.\n", | |
2629 | String8(mName).string(), String8(key).string(), | |
2630 | item.sourcePos.file.string(), item.sourcePos.line); | |
2631 | return UNKNOWN_ERROR; | |
2632 | } | |
2633 | //printf("Replacing %s with %s\n", | |
2634 | // String8(mBag.valueFor(key).value).string(), String8(value).string()); | |
2635 | mBag.replaceValueFor(key, item); | |
2636 | } | |
2637 | ||
2638 | mBag.add(key, item); | |
2639 | return NO_ERROR; | |
2640 | } | |
2641 | ||
2642 | status_t ResourceTable::Entry::generateAttributes(ResourceTable* table, | |
2643 | const String16& package) | |
2644 | { | |
2645 | const String16 attr16("attr"); | |
2646 | const String16 id16("id"); | |
2647 | const size_t N = mBag.size(); | |
2648 | for (size_t i=0; i<N; i++) { | |
2649 | const String16& key = mBag.keyAt(i); | |
2650 | const Item& it = mBag.valueAt(i); | |
2651 | if (it.isId) { | |
2652 | if (!table->hasBagOrEntry(key, &id16, &package)) { | |
2653 | String16 value("false"); | |
2654 | status_t err = table->addEntry(SourcePos(String8("<generated>"), 0), package, | |
2655 | id16, key, value); | |
2656 | if (err != NO_ERROR) { | |
2657 | return err; | |
2658 | } | |
2659 | } | |
2660 | } else if (!table->hasBagOrEntry(key, &attr16, &package)) { | |
2661 | ||
2662 | #if 1 | |
2663 | // fprintf(stderr, "ERROR: Bag attribute '%s' has not been defined.\n", | |
2664 | // String8(key).string()); | |
2665 | // const Item& item(mBag.valueAt(i)); | |
2666 | // fprintf(stderr, "Referenced from file %s line %d\n", | |
2667 | // item.sourcePos.file.string(), item.sourcePos.line); | |
2668 | // return UNKNOWN_ERROR; | |
2669 | #else | |
2670 | char numberStr[16]; | |
2671 | sprintf(numberStr, "%d", ResTable_map::TYPE_ANY); | |
2672 | status_t err = table->addBag(SourcePos("<generated>", 0), package, | |
2673 | attr16, key, String16(""), | |
2674 | String16("^type"), | |
2675 | String16(numberStr), NULL, NULL); | |
2676 | if (err != NO_ERROR) { | |
2677 | return err; | |
2678 | } | |
2679 | #endif | |
2680 | } | |
2681 | } | |
2682 | return NO_ERROR; | |
2683 | } | |
2684 | ||
2685 | status_t ResourceTable::Entry::assignResourceIds(ResourceTable* table, | |
2686 | const String16& package) | |
2687 | { | |
2688 | bool hasErrors = false; | |
2689 | ||
2690 | if (mType == TYPE_BAG) { | |
2691 | const char* errorMsg; | |
2692 | const String16 style16("style"); | |
2693 | const String16 attr16("attr"); | |
2694 | const String16 id16("id"); | |
2695 | mParentId = 0; | |
2696 | if (mParent.size() > 0) { | |
2697 | mParentId = table->getResId(mParent, &style16, NULL, &errorMsg); | |
2698 | if (mParentId == 0) { | |
2699 | mPos.error("Error retrieving parent for item: %s '%s'.\n", | |
2700 | errorMsg, String8(mParent).string()); | |
2701 | hasErrors = true; | |
2702 | } | |
2703 | } | |
2704 | const size_t N = mBag.size(); | |
2705 | for (size_t i=0; i<N; i++) { | |
2706 | const String16& key = mBag.keyAt(i); | |
2707 | Item& it = mBag.editValueAt(i); | |
2708 | it.bagKeyId = table->getResId(key, | |
2709 | it.isId ? &id16 : &attr16, NULL, &errorMsg); | |
2710 | //printf("Bag key of %s: #%08x\n", String8(key).string(), it.bagKeyId); | |
2711 | if (it.bagKeyId == 0) { | |
2712 | it.sourcePos.error("Error: %s: %s '%s'.\n", errorMsg, | |
2713 | String8(it.isId ? id16 : attr16).string(), | |
2714 | String8(key).string()); | |
2715 | hasErrors = true; | |
2716 | } | |
2717 | } | |
2718 | } | |
2719 | return hasErrors ? UNKNOWN_ERROR : NO_ERROR; | |
2720 | } | |
2721 | ||
2722 | status_t ResourceTable::Entry::prepareFlatten(StringPool* strings, ResourceTable* table) | |
2723 | { | |
2724 | if (mType == TYPE_ITEM) { | |
2725 | Item& it = mItem; | |
2726 | AccessorCookie ac(it.sourcePos, String8(mName), String8(it.value)); | |
2727 | if (!table->stringToValue(&it.parsedValue, strings, | |
2728 | it.value, false, true, 0, | |
2729 | &it.style, NULL, &ac, mItemFormat)) { | |
2730 | return UNKNOWN_ERROR; | |
2731 | } | |
2732 | } else if (mType == TYPE_BAG) { | |
2733 | const size_t N = mBag.size(); | |
2734 | for (size_t i=0; i<N; i++) { | |
2735 | const String16& key = mBag.keyAt(i); | |
2736 | Item& it = mBag.editValueAt(i); | |
2737 | AccessorCookie ac(it.sourcePos, String8(key), String8(it.value)); | |
2738 | if (!table->stringToValue(&it.parsedValue, strings, | |
2739 | it.value, false, true, it.bagKeyId, | |
2740 | &it.style, NULL, &ac, it.format)) { | |
2741 | return UNKNOWN_ERROR; | |
2742 | } | |
2743 | } | |
2744 | } else { | |
2745 | mPos.error("Error: entry %s is not a single item or a bag.\n", | |
2746 | String8(mName).string()); | |
2747 | return UNKNOWN_ERROR; | |
2748 | } | |
2749 | return NO_ERROR; | |
2750 | } | |
2751 | ||
2752 | ssize_t ResourceTable::Entry::flatten(Bundle* bundle, const sp<AaptFile>& data, bool isPublic) | |
2753 | { | |
2754 | size_t amt = 0; | |
2755 | ResTable_entry header; | |
2756 | memset(&header, 0, sizeof(header)); | |
2757 | header.size = htods(sizeof(header)); | |
2758 | const type ty = this != NULL ? mType : TYPE_ITEM; | |
2759 | if (this != NULL) { | |
2760 | if (ty == TYPE_BAG) { | |
2761 | header.flags |= htods(header.FLAG_COMPLEX); | |
2762 | } | |
2763 | if (isPublic) { | |
2764 | header.flags |= htods(header.FLAG_PUBLIC); | |
2765 | } | |
2766 | header.key.index = htodl(mNameIndex); | |
2767 | } | |
2768 | if (ty != TYPE_BAG) { | |
2769 | status_t err = data->writeData(&header, sizeof(header)); | |
2770 | if (err != NO_ERROR) { | |
2771 | fprintf(stderr, "ERROR: out of memory creating ResTable_entry\n"); | |
2772 | return err; | |
2773 | } | |
2774 | ||
2775 | const Item& it = mItem; | |
2776 | Res_value par; | |
2777 | memset(&par, 0, sizeof(par)); | |
2778 | par.size = htods(it.parsedValue.size); | |
2779 | par.dataType = it.parsedValue.dataType; | |
2780 | par.res0 = it.parsedValue.res0; | |
2781 | par.data = htodl(it.parsedValue.data); | |
2782 | #if 0 | |
2783 | printf("Writing item (%s): type=%d, data=0x%x, res0=0x%x\n", | |
2784 | String8(mName).string(), it.parsedValue.dataType, | |
2785 | it.parsedValue.data, par.res0); | |
2786 | #endif | |
2787 | err = data->writeData(&par, it.parsedValue.size); | |
2788 | if (err != NO_ERROR) { | |
2789 | fprintf(stderr, "ERROR: out of memory creating Res_value\n"); | |
2790 | return err; | |
2791 | } | |
2792 | amt += it.parsedValue.size; | |
2793 | } else { | |
2794 | size_t N = mBag.size(); | |
2795 | size_t i; | |
2796 | // Create correct ordering of items. | |
2797 | KeyedVector<uint32_t, const Item*> items; | |
2798 | for (i=0; i<N; i++) { | |
2799 | const Item& it = mBag.valueAt(i); | |
2800 | items.add(it.bagKeyId, &it); | |
2801 | } | |
2802 | N = items.size(); | |
2803 | ||
2804 | ResTable_map_entry mapHeader; | |
2805 | memcpy(&mapHeader, &header, sizeof(header)); | |
2806 | mapHeader.size = htods(sizeof(mapHeader)); | |
2807 | mapHeader.parent.ident = htodl(mParentId); | |
2808 | mapHeader.count = htodl(N); | |
2809 | status_t err = data->writeData(&mapHeader, sizeof(mapHeader)); | |
2810 | if (err != NO_ERROR) { | |
2811 | fprintf(stderr, "ERROR: out of memory creating ResTable_entry\n"); | |
2812 | return err; | |
2813 | } | |
2814 | ||
2815 | for (i=0; i<N; i++) { | |
2816 | const Item& it = *items.valueAt(i); | |
2817 | ResTable_map map; | |
2818 | map.name.ident = htodl(it.bagKeyId); | |
2819 | map.value.size = htods(it.parsedValue.size); | |
2820 | map.value.dataType = it.parsedValue.dataType; | |
2821 | map.value.res0 = it.parsedValue.res0; | |
2822 | map.value.data = htodl(it.parsedValue.data); | |
2823 | err = data->writeData(&map, sizeof(map)); | |
2824 | if (err != NO_ERROR) { | |
2825 | fprintf(stderr, "ERROR: out of memory creating Res_value\n"); | |
2826 | return err; | |
2827 | } | |
2828 | amt += sizeof(map); | |
2829 | } | |
2830 | } | |
2831 | return amt; | |
2832 | } | |
2833 | ||
2834 | void ResourceTable::ConfigList::appendComment(const String16& comment, | |
2835 | bool onlyIfEmpty) | |
2836 | { | |
2837 | if (comment.size() <= 0) { | |
2838 | return; | |
2839 | } | |
2840 | if (onlyIfEmpty && mComment.size() > 0) { | |
2841 | return; | |
2842 | } | |
2843 | if (mComment.size() > 0) { | |
2844 | mComment.append(String16("\n")); | |
2845 | } | |
2846 | mComment.append(comment); | |
2847 | } | |
2848 | ||
2849 | void ResourceTable::ConfigList::appendTypeComment(const String16& comment) | |
2850 | { | |
2851 | if (comment.size() <= 0) { | |
2852 | return; | |
2853 | } | |
2854 | if (mTypeComment.size() > 0) { | |
2855 | mTypeComment.append(String16("\n")); | |
2856 | } | |
2857 | mTypeComment.append(comment); | |
2858 | } | |
2859 | ||
2860 | status_t ResourceTable::Type::addPublic(const SourcePos& sourcePos, | |
2861 | const String16& name, | |
2862 | const uint32_t ident) | |
2863 | { | |
2864 | #if 0 | |
2865 | int32_t entryIdx = Res_GETENTRY(ident); | |
2866 | if (entryIdx < 0) { | |
2867 | sourcePos.error("Public resource %s/%s has an invalid 0 identifier (0x%08x).\n", | |
2868 | String8(mName).string(), String8(name).string(), ident); | |
2869 | return UNKNOWN_ERROR; | |
2870 | } | |
2871 | #endif | |
2872 | ||
2873 | int32_t typeIdx = Res_GETTYPE(ident); | |
2874 | if (typeIdx >= 0) { | |
2875 | typeIdx++; | |
2876 | if (mPublicIndex > 0 && mPublicIndex != typeIdx) { | |
2877 | sourcePos.error("Public resource %s/%s has conflicting type codes for its" | |
2878 | " public identifiers (0x%x vs 0x%x).\n", | |
2879 | String8(mName).string(), String8(name).string(), | |
2880 | mPublicIndex, typeIdx); | |
2881 | return UNKNOWN_ERROR; | |
2882 | } | |
2883 | mPublicIndex = typeIdx; | |
2884 | } | |
2885 | ||
2886 | if (mFirstPublicSourcePos == NULL) { | |
2887 | mFirstPublicSourcePos = new SourcePos(sourcePos); | |
2888 | } | |
2889 | ||
2890 | if (mPublic.indexOfKey(name) < 0) { | |
2891 | mPublic.add(name, Public(sourcePos, String16(), ident)); | |
2892 | } else { | |
2893 | Public& p = mPublic.editValueFor(name); | |
2894 | if (p.ident != ident) { | |
2895 | sourcePos.error("Public resource %s/%s has conflicting public identifiers" | |
2896 | " (0x%08x vs 0x%08x).\n" | |
2897 | "%s:%d: Originally defined here.\n", | |
2898 | String8(mName).string(), String8(name).string(), p.ident, ident, | |
2899 | p.sourcePos.file.string(), p.sourcePos.line); | |
2900 | return UNKNOWN_ERROR; | |
2901 | } | |
2902 | } | |
2903 | ||
2904 | return NO_ERROR; | |
2905 | } | |
2906 | ||
2907 | sp<ResourceTable::Entry> ResourceTable::Type::getEntry(const String16& entry, | |
2908 | const SourcePos& sourcePos, | |
2909 | const ResTable_config* config, | |
2910 | bool doSetIndex) | |
2911 | { | |
2912 | int pos = -1; | |
2913 | sp<ConfigList> c = mConfigs.valueFor(entry); | |
2914 | if (c == NULL) { | |
2915 | c = new ConfigList(entry, sourcePos); | |
2916 | mConfigs.add(entry, c); | |
2917 | pos = (int)mOrderedConfigs.size(); | |
2918 | mOrderedConfigs.add(c); | |
2919 | if (doSetIndex) { | |
2920 | c->setEntryIndex(pos); | |
2921 | } | |
2922 | } | |
2923 | ||
2924 | ConfigDescription cdesc; | |
2925 | if (config) cdesc = *config; | |
2926 | ||
2927 | sp<Entry> e = c->getEntries().valueFor(cdesc); | |
2928 | if (e == NULL) { | |
2929 | if (config != NULL) { | |
2930 | NOISY(printf("New entry at %s:%d: imsi:%d/%d lang:%c%c cnt:%c%c " | |
2931 | "orien:%d touch:%d density:%d key:%d inp:%d nav:%d w:%d h:%d\n", | |
2932 | sourcePos.file.string(), sourcePos.line, | |
2933 | config->mcc, config->mnc, | |
2934 | config->language[0] ? config->language[0] : '-', | |
2935 | config->language[1] ? config->language[1] : '-', | |
2936 | config->country[0] ? config->country[0] : '-', | |
2937 | config->country[1] ? config->country[1] : '-', | |
2938 | config->orientation, | |
2939 | config->touchscreen, | |
2940 | config->density, | |
2941 | config->keyboard, | |
2942 | config->inputFlags, | |
2943 | config->navigation, | |
2944 | config->screenWidth, | |
2945 | config->screenHeight)); | |
2946 | } else { | |
2947 | NOISY(printf("New entry at %s:%d: NULL config\n", | |
2948 | sourcePos.file.string(), sourcePos.line)); | |
2949 | } | |
2950 | e = new Entry(entry, sourcePos); | |
2951 | c->addEntry(cdesc, e); | |
2952 | /* | |
2953 | if (doSetIndex) { | |
2954 | if (pos < 0) { | |
2955 | for (pos=0; pos<(int)mOrderedConfigs.size(); pos++) { | |
2956 | if (mOrderedConfigs[pos] == c) { | |
2957 | break; | |
2958 | } | |
2959 | } | |
2960 | if (pos >= (int)mOrderedConfigs.size()) { | |
2961 | sourcePos.error("Internal error: config not found in mOrderedConfigs when adding entry"); | |
2962 | return NULL; | |
2963 | } | |
2964 | } | |
2965 | e->setEntryIndex(pos); | |
2966 | } | |
2967 | */ | |
2968 | } | |
2969 | ||
2970 | mUniqueConfigs.add(cdesc); | |
2971 | ||
2972 | return e; | |
2973 | } | |
2974 | ||
2975 | status_t ResourceTable::Type::applyPublicEntryOrder() | |
2976 | { | |
2977 | size_t N = mOrderedConfigs.size(); | |
2978 | Vector<sp<ConfigList> > origOrder(mOrderedConfigs); | |
2979 | bool hasError = false; | |
2980 | ||
2981 | size_t i; | |
2982 | for (i=0; i<N; i++) { | |
2983 | mOrderedConfigs.replaceAt(NULL, i); | |
2984 | } | |
2985 | ||
2986 | const size_t NP = mPublic.size(); | |
2987 | //printf("Ordering %d configs from %d public defs\n", N, NP); | |
2988 | size_t j; | |
2989 | for (j=0; j<NP; j++) { | |
2990 | const String16& name = mPublic.keyAt(j); | |
2991 | const Public& p = mPublic.valueAt(j); | |
2992 | int32_t idx = Res_GETENTRY(p.ident); | |
2993 | //printf("Looking for entry \"%s\"/\"%s\" (0x%08x) in %d...\n", | |
2994 | // String8(mName).string(), String8(name).string(), p.ident, N); | |
2995 | bool found = false; | |
2996 | for (i=0; i<N; i++) { | |
2997 | sp<ConfigList> e = origOrder.itemAt(i); | |
2998 | //printf("#%d: \"%s\"\n", i, String8(e->getName()).string()); | |
2999 | if (e->getName() == name) { | |
3000 | if (idx >= (int32_t)mOrderedConfigs.size()) { | |
3001 | p.sourcePos.error("Public entry identifier 0x%x entry index " | |
3002 | "is larger than available symbols (index %d, total symbols %d).\n", | |
3003 | p.ident, idx, mOrderedConfigs.size()); | |
3004 | hasError = true; | |
3005 | } else if (mOrderedConfigs.itemAt(idx) == NULL) { | |
3006 | e->setPublic(true); | |
3007 | e->setPublicSourcePos(p.sourcePos); | |
3008 | mOrderedConfigs.replaceAt(e, idx); | |
3009 | origOrder.removeAt(i); | |
3010 | N--; | |
3011 | found = true; | |
3012 | break; | |
3013 | } else { | |
3014 | sp<ConfigList> oe = mOrderedConfigs.itemAt(idx); | |
3015 | ||
3016 | p.sourcePos.error("Multiple entry names declared for public entry" | |
3017 | " identifier 0x%x in type %s (%s vs %s).\n" | |
3018 | "%s:%d: Originally defined here.", | |
3019 | idx+1, String8(mName).string(), | |
3020 | String8(oe->getName()).string(), | |
3021 | String8(name).string(), | |
3022 | oe->getPublicSourcePos().file.string(), | |
3023 | oe->getPublicSourcePos().line); | |
3024 | hasError = true; | |
3025 | } | |
3026 | } | |
3027 | } | |
3028 | ||
3029 | if (!found) { | |
3030 | p.sourcePos.error("Public symbol %s/%s declared here is not defined.", | |
3031 | String8(mName).string(), String8(name).string()); | |
3032 | hasError = true; | |
3033 | } | |
3034 | } | |
3035 | ||
3036 | //printf("Copying back in %d non-public configs, have %d\n", N, origOrder.size()); | |
3037 | ||
3038 | if (N != origOrder.size()) { | |
3039 | printf("Internal error: remaining private symbol count mismatch\n"); | |
3040 | N = origOrder.size(); | |
3041 | } | |
3042 | ||
3043 | j = 0; | |
3044 | for (i=0; i<N; i++) { | |
3045 | sp<ConfigList> e = origOrder.itemAt(i); | |
3046 | // There will always be enough room for the remaining entries. | |
3047 | while (mOrderedConfigs.itemAt(j) != NULL) { | |
3048 | j++; | |
3049 | } | |
3050 | mOrderedConfigs.replaceAt(e, j); | |
3051 | j++; | |
3052 | } | |
3053 | ||
3054 | return hasError ? UNKNOWN_ERROR : NO_ERROR; | |
3055 | } | |
3056 | ||
3057 | ResourceTable::Package::Package(const String16& name, ssize_t includedId) | |
3058 | : mName(name), mIncludedId(includedId), | |
3059 | mTypeStringsMapping(0xffffffff), | |
3060 | mKeyStringsMapping(0xffffffff) | |
3061 | { | |
3062 | } | |
3063 | ||
3064 | sp<ResourceTable::Type> ResourceTable::Package::getType(const String16& type, | |
3065 | const SourcePos& sourcePos, | |
3066 | bool doSetIndex) | |
3067 | { | |
3068 | sp<Type> t = mTypes.valueFor(type); | |
3069 | if (t == NULL) { | |
3070 | t = new Type(type, sourcePos); | |
3071 | mTypes.add(type, t); | |
3072 | mOrderedTypes.add(t); | |
3073 | if (doSetIndex) { | |
3074 | // For some reason the type's index is set to one plus the index | |
3075 | // in the mOrderedTypes list, rather than just the index. | |
3076 | t->setIndex(mOrderedTypes.size()); | |
3077 | } | |
3078 | } | |
3079 | return t; | |
3080 | } | |
3081 | ||
3082 | status_t ResourceTable::Package::setTypeStrings(const sp<AaptFile>& data) | |
3083 | { | |
3084 | mTypeStringsData = data; | |
3085 | status_t err = setStrings(data, &mTypeStrings, &mTypeStringsMapping); | |
3086 | if (err != NO_ERROR) { | |
3087 | fprintf(stderr, "ERROR: Type string data is corrupt!\n"); | |
3088 | } | |
3089 | return err; | |
3090 | } | |
3091 | ||
3092 | status_t ResourceTable::Package::setKeyStrings(const sp<AaptFile>& data) | |
3093 | { | |
3094 | mKeyStringsData = data; | |
3095 | status_t err = setStrings(data, &mKeyStrings, &mKeyStringsMapping); | |
3096 | if (err != NO_ERROR) { | |
3097 | fprintf(stderr, "ERROR: Key string data is corrupt!\n"); | |
3098 | } | |
3099 | return err; | |
3100 | } | |
3101 | ||
3102 | status_t ResourceTable::Package::setStrings(const sp<AaptFile>& data, | |
3103 | ResStringPool* strings, | |
3104 | DefaultKeyedVector<String16, uint32_t>* mappings) | |
3105 | { | |
3106 | if (data->getData() == NULL) { | |
3107 | return UNKNOWN_ERROR; | |
3108 | } | |
3109 | ||
3110 | NOISY(aout << "Setting restable string pool: " | |
3111 | << HexDump(data->getData(), data->getSize()) << endl); | |
3112 | ||
3113 | status_t err = strings->setTo(data->getData(), data->getSize()); | |
3114 | if (err == NO_ERROR) { | |
3115 | const size_t N = strings->size(); | |
3116 | for (size_t i=0; i<N; i++) { | |
3117 | size_t len; | |
3118 | mappings->add(String16(strings->stringAt(i, &len)), i); | |
3119 | } | |
3120 | } | |
3121 | return err; | |
3122 | } | |
3123 | ||
3124 | status_t ResourceTable::Package::applyPublicTypeOrder() | |
3125 | { | |
3126 | size_t N = mOrderedTypes.size(); | |
3127 | Vector<sp<Type> > origOrder(mOrderedTypes); | |
3128 | ||
3129 | size_t i; | |
3130 | for (i=0; i<N; i++) { | |
3131 | mOrderedTypes.replaceAt(NULL, i); | |
3132 | } | |
3133 | ||
3134 | for (i=0; i<N; i++) { | |
3135 | sp<Type> t = origOrder.itemAt(i); | |
3136 | int32_t idx = t->getPublicIndex(); | |
3137 | if (idx > 0) { | |
3138 | idx--; | |
3139 | while (idx >= (int32_t)mOrderedTypes.size()) { | |
3140 | mOrderedTypes.add(); | |
3141 | } | |
3142 | if (mOrderedTypes.itemAt(idx) != NULL) { | |
3143 | sp<Type> ot = mOrderedTypes.itemAt(idx); | |
3144 | t->getFirstPublicSourcePos().error("Multiple type names declared for public type" | |
3145 | " identifier 0x%x (%s vs %s).\n" | |
3146 | "%s:%d: Originally defined here.", | |
3147 | idx, String8(ot->getName()).string(), | |
3148 | String8(t->getName()).string(), | |
3149 | ot->getFirstPublicSourcePos().file.string(), | |
3150 | ot->getFirstPublicSourcePos().line); | |
3151 | return UNKNOWN_ERROR; | |
3152 | } | |
3153 | mOrderedTypes.replaceAt(t, idx); | |
3154 | origOrder.removeAt(i); | |
3155 | i--; | |
3156 | N--; | |
3157 | } | |
3158 | } | |
3159 | ||
3160 | size_t j=0; | |
3161 | for (i=0; i<N; i++) { | |
3162 | sp<Type> t = origOrder.itemAt(i); | |
3163 | // There will always be enough room for the remaining types. | |
3164 | while (mOrderedTypes.itemAt(j) != NULL) { | |
3165 | j++; | |
3166 | } | |
3167 | mOrderedTypes.replaceAt(t, j); | |
3168 | } | |
3169 | ||
3170 | return NO_ERROR; | |
3171 | } | |
3172 | ||
3173 | sp<ResourceTable::Package> ResourceTable::getPackage(const String16& package) | |
3174 | { | |
3175 | sp<Package> p = mPackages.valueFor(package); | |
3176 | if (p == NULL) { | |
3177 | if (mIsAppPackage) { | |
3178 | if (mHaveAppPackage) { | |
3179 | fprintf(stderr, "Adding multiple application package resources; only one is allowed.\n" | |
3180 | "Use -x to create extended resources.\n"); | |
3181 | return NULL; | |
3182 | } | |
3183 | mHaveAppPackage = true; | |
3184 | p = new Package(package, 127); | |
3185 | } else { | |
3186 | p = new Package(package, mNextPackageId); | |
3187 | } | |
3188 | //printf("*** NEW PACKAGE: \"%s\" id=%d\n", | |
3189 | // String8(package).string(), p->getAssignedId()); | |
3190 | mPackages.add(package, p); | |
3191 | mOrderedPackages.add(p); | |
3192 | mNextPackageId++; | |
3193 | } | |
3194 | return p; | |
3195 | } | |
3196 | ||
3197 | sp<ResourceTable::Type> ResourceTable::getType(const String16& package, | |
3198 | const String16& type, | |
3199 | const SourcePos& sourcePos, | |
3200 | bool doSetIndex) | |
3201 | { | |
3202 | sp<Package> p = getPackage(package); | |
3203 | if (p == NULL) { | |
3204 | return NULL; | |
3205 | } | |
3206 | return p->getType(type, sourcePos, doSetIndex); | |
3207 | } | |
3208 | ||
3209 | sp<ResourceTable::Entry> ResourceTable::getEntry(const String16& package, | |
3210 | const String16& type, | |
3211 | const String16& name, | |
3212 | const SourcePos& sourcePos, | |
3213 | const ResTable_config* config, | |
3214 | bool doSetIndex) | |
3215 | { | |
3216 | sp<Type> t = getType(package, type, sourcePos, doSetIndex); | |
3217 | if (t == NULL) { | |
3218 | return NULL; | |
3219 | } | |
3220 | return t->getEntry(name, sourcePos, config, doSetIndex); | |
3221 | } | |
3222 | ||
3223 | sp<const ResourceTable::Entry> ResourceTable::getEntry(uint32_t resID, | |
3224 | const ResTable_config* config) const | |
3225 | { | |
3226 | int pid = Res_GETPACKAGE(resID)+1; | |
3227 | const size_t N = mOrderedPackages.size(); | |
3228 | size_t i; | |
3229 | sp<Package> p; | |
3230 | for (i=0; i<N; i++) { | |
3231 | sp<Package> check = mOrderedPackages[i]; | |
3232 | if (check->getAssignedId() == pid) { | |
3233 | p = check; | |
3234 | break; | |
3235 | } | |
3236 | ||
3237 | } | |
3238 | if (p == NULL) { | |
3239 | fprintf(stderr, "WARNING: Package not found for resource #%08x\n", resID); | |
3240 | return NULL; | |
3241 | } | |
3242 | ||
3243 | int tid = Res_GETTYPE(resID); | |
3244 | if (tid < 0 || tid >= (int)p->getOrderedTypes().size()) { | |
3245 | fprintf(stderr, "WARNING: Type not found for resource #%08x\n", resID); | |
3246 | return NULL; | |
3247 | } | |
3248 | sp<Type> t = p->getOrderedTypes()[tid]; | |
3249 | ||
3250 | int eid = Res_GETENTRY(resID); | |
3251 | if (eid < 0 || eid >= (int)t->getOrderedConfigs().size()) { | |
3252 | fprintf(stderr, "WARNING: Entry not found for resource #%08x\n", resID); | |
3253 | return NULL; | |
3254 | } | |
3255 | ||
3256 | sp<ConfigList> c = t->getOrderedConfigs()[eid]; | |
3257 | if (c == NULL) { | |
3258 | fprintf(stderr, "WARNING: Entry not found for resource #%08x\n", resID); | |
3259 | return NULL; | |
3260 | } | |
3261 | ||
3262 | ConfigDescription cdesc; | |
3263 | if (config) cdesc = *config; | |
3264 | sp<Entry> e = c->getEntries().valueFor(cdesc); | |
3265 | if (c == NULL) { | |
3266 | fprintf(stderr, "WARNING: Entry configuration not found for resource #%08x\n", resID); | |
3267 | return NULL; | |
3268 | } | |
3269 | ||
3270 | return e; | |
3271 | } | |
3272 | ||
3273 | const ResourceTable::Item* ResourceTable::getItem(uint32_t resID, uint32_t attrID) const | |
3274 | { | |
3275 | sp<const Entry> e = getEntry(resID); | |
3276 | if (e == NULL) { | |
3277 | return NULL; | |
3278 | } | |
3279 | ||
3280 | const size_t N = e->getBag().size(); | |
3281 | for (size_t i=0; i<N; i++) { | |
3282 | const Item& it = e->getBag().valueAt(i); | |
3283 | if (it.bagKeyId == 0) { | |
3284 | fprintf(stderr, "WARNING: ID not yet assigned to '%s' in bag '%s'\n", | |
3285 | String8(e->getName()).string(), | |
3286 | String8(e->getBag().keyAt(i)).string()); | |
3287 | } | |
3288 | if (it.bagKeyId == attrID) { | |
3289 | return ⁢ | |
3290 | } | |
3291 | } | |
3292 | ||
3293 | return NULL; | |
3294 | } | |
3295 | ||
3296 | bool ResourceTable::getItemValue( | |
3297 | uint32_t resID, uint32_t attrID, Res_value* outValue) | |
3298 | { | |
3299 | const Item* item = getItem(resID, attrID); | |
3300 | ||
3301 | bool res = false; | |
3302 | if (item != NULL) { | |
3303 | if (item->evaluating) { | |
3304 | sp<const Entry> e = getEntry(resID); | |
3305 | const size_t N = e->getBag().size(); | |
3306 | size_t i; | |
3307 | for (i=0; i<N; i++) { | |
3308 | if (&e->getBag().valueAt(i) == item) { | |
3309 | break; | |
3310 | } | |
3311 | } | |
3312 | fprintf(stderr, "WARNING: Circular reference detected in key '%s' of bag '%s'\n", | |
3313 | String8(e->getName()).string(), | |
3314 | String8(e->getBag().keyAt(i)).string()); | |
3315 | return false; | |
3316 | } | |
3317 | item->evaluating = true; | |
3318 | res = stringToValue(outValue, NULL, item->value, false, false, item->bagKeyId); | |
3319 | NOISY( | |
3320 | if (res) { | |
3321 | printf("getItemValue of #%08x[#%08x] (%s): type=#%08x, data=#%08x\n", | |
3322 | resID, attrID, String8(getEntry(resID)->getName()).string(), | |
3323 | outValue->dataType, outValue->data); | |
3324 | } else { | |
3325 | printf("getItemValue of #%08x[#%08x]: failed\n", | |
3326 | resID, attrID); | |
3327 | } | |
3328 | ); | |
3329 | item->evaluating = false; | |
3330 | } | |
3331 | return res; | |
3332 | } | |
3333 |