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