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