The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | // |
| 2 | // Copyright 2006 The Android Open Source Project |
| 3 | // |
| 4 | // Build resource files from raw assets. |
| 5 | // |
| 6 | |
| 7 | #include "XMLNode.h" |
| 8 | #include "ResourceTable.h" |
| 9 | |
| 10 | #include <host/pseudolocalize.h> |
| 11 | #include <utils/ByteOrder.h> |
| 12 | #include <errno.h> |
| 13 | #include <string.h> |
| 14 | |
| 15 | #ifndef HAVE_MS_C_RUNTIME |
| 16 | #define O_BINARY 0 |
| 17 | #endif |
| 18 | |
| 19 | #define NOISY(x) //x |
| 20 | #define NOISY_PARSE(x) //x |
| 21 | |
| 22 | const char* const RESOURCES_ROOT_NAMESPACE = "http://schemas.android.com/apk/res/"; |
| 23 | const char* const RESOURCES_ANDROID_NAMESPACE = "http://schemas.android.com/apk/res/android"; |
| 24 | const char* const RESOURCES_ROOT_PRV_NAMESPACE = "http://schemas.android.com/apk/prv/res/"; |
| 25 | |
| 26 | const char* const XLIFF_XMLNS = "urn:oasis:names:tc:xliff:document:1.2"; |
| 27 | const char* const ALLOWED_XLIFF_ELEMENTS[] = { |
| 28 | "bpt", |
| 29 | "ept", |
| 30 | "it", |
| 31 | "ph", |
| 32 | "g", |
| 33 | "bx", |
| 34 | "ex", |
| 35 | "x" |
| 36 | }; |
| 37 | |
| 38 | bool isWhitespace(const char16_t* str) |
| 39 | { |
| 40 | while (*str != 0 && *str < 128 && isspace(*str)) { |
| 41 | str++; |
| 42 | } |
| 43 | return *str == 0; |
| 44 | } |
| 45 | |
| 46 | static const String16 RESOURCES_PREFIX(RESOURCES_ROOT_NAMESPACE); |
| 47 | static const String16 RESOURCES_PRV_PREFIX(RESOURCES_ROOT_PRV_NAMESPACE); |
| 48 | |
| 49 | String16 getNamespaceResourcePackage(String16 namespaceUri, bool* outIsPublic) |
| 50 | { |
| 51 | //printf("%s starts with %s?\n", String8(namespaceUri).string(), |
| 52 | // String8(RESOURCES_PREFIX).string()); |
| 53 | size_t prefixSize; |
| 54 | bool isPublic = true; |
| 55 | if (namespaceUri.startsWith(RESOURCES_PREFIX)) { |
| 56 | prefixSize = RESOURCES_PREFIX.size(); |
| 57 | } else if (namespaceUri.startsWith(RESOURCES_PRV_PREFIX)) { |
| 58 | isPublic = false; |
| 59 | prefixSize = RESOURCES_PRV_PREFIX.size(); |
| 60 | } else { |
| 61 | if (outIsPublic) *outIsPublic = isPublic; // = true |
| 62 | return String16(); |
| 63 | } |
| 64 | |
| 65 | //printf("YES!\n"); |
| 66 | //printf("namespace: %s\n", String8(String16(namespaceUri, namespaceUri.size()-prefixSize, prefixSize)).string()); |
| 67 | if (outIsPublic) *outIsPublic = isPublic; |
| 68 | return String16(namespaceUri, namespaceUri.size()-prefixSize, prefixSize); |
| 69 | } |
| 70 | |
| 71 | status_t parseStyledString(Bundle* bundle, |
| 72 | const char* fileName, |
| 73 | ResXMLTree* inXml, |
| 74 | const String16& endTag, |
| 75 | String16* outString, |
| 76 | Vector<StringPool::entry_style_span>* outSpans, |
| 77 | bool pseudolocalize) |
| 78 | { |
| 79 | Vector<StringPool::entry_style_span> spanStack; |
| 80 | String16 curString; |
| 81 | String16 rawString; |
| 82 | const char* errorMsg; |
| 83 | int xliffDepth = 0; |
| 84 | bool firstTime = true; |
| 85 | |
| 86 | size_t len; |
| 87 | ResXMLTree::event_code_t code; |
| 88 | while ((code=inXml->next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) { |
| 89 | |
| 90 | if (code == ResXMLTree::TEXT) { |
| 91 | String16 text(inXml->getText(&len)); |
| 92 | if (firstTime && text.size() > 0) { |
| 93 | firstTime = false; |
| 94 | if (text.string()[0] == '@') { |
| 95 | // If this is a resource reference, don't do the pseudoloc. |
| 96 | pseudolocalize = false; |
| 97 | } |
| 98 | } |
| 99 | if (xliffDepth == 0 && pseudolocalize) { |
| 100 | std::string orig(String8(text).string()); |
| 101 | std::string pseudo = pseudolocalize_string(orig); |
| 102 | curString.append(String16(String8(pseudo.c_str()))); |
| 103 | } else { |
| 104 | curString.append(text); |
| 105 | } |
| 106 | } else if (code == ResXMLTree::START_TAG) { |
| 107 | const String16 element16(inXml->getElementName(&len)); |
| 108 | const String8 element8(element16); |
| 109 | |
| 110 | size_t nslen; |
| 111 | const uint16_t* ns = inXml->getElementNamespace(&nslen); |
| 112 | if (ns == NULL) { |
| 113 | ns = (const uint16_t*)"\0\0"; |
| 114 | nslen = 0; |
| 115 | } |
| 116 | const String8 nspace(String16(ns, nslen)); |
| 117 | if (nspace == XLIFF_XMLNS) { |
| 118 | const int N = sizeof(ALLOWED_XLIFF_ELEMENTS)/sizeof(ALLOWED_XLIFF_ELEMENTS[0]); |
| 119 | for (int i=0; i<N; i++) { |
| 120 | if (element8 == ALLOWED_XLIFF_ELEMENTS[i]) { |
| 121 | xliffDepth++; |
| 122 | // in this case, treat it like it was just text, in other words, do nothing |
| 123 | // here and silently drop this element |
| 124 | goto moveon; |
| 125 | } |
| 126 | } |
| 127 | { |
| 128 | SourcePos(String8(fileName), inXml->getLineNumber()).error( |
| 129 | "Found unsupported XLIFF tag <%s>\n", |
| 130 | element8.string()); |
| 131 | return UNKNOWN_ERROR; |
| 132 | } |
| 133 | moveon: |
| 134 | continue; |
| 135 | } |
| 136 | |
| 137 | if (outSpans == NULL) { |
| 138 | SourcePos(String8(fileName), inXml->getLineNumber()).error( |
| 139 | "Found style tag <%s> where styles are not allowed\n", element8.string()); |
| 140 | return UNKNOWN_ERROR; |
| 141 | } |
| 142 | |
| 143 | if (!ResTable::collectString(outString, curString.string(), |
| 144 | curString.size(), false, &errorMsg, true)) { |
| 145 | SourcePos(String8(fileName), inXml->getLineNumber()).error("%s (in %s)\n", |
| 146 | errorMsg, String8(curString).string()); |
| 147 | return UNKNOWN_ERROR; |
| 148 | } |
| 149 | rawString.append(curString); |
| 150 | curString = String16(); |
| 151 | |
| 152 | StringPool::entry_style_span span; |
| 153 | span.name = element16; |
| 154 | for (size_t ai=0; ai<inXml->getAttributeCount(); ai++) { |
| 155 | span.name.append(String16(";")); |
| 156 | const char16_t* str = inXml->getAttributeName(ai, &len); |
| 157 | span.name.append(str, len); |
| 158 | span.name.append(String16("=")); |
| 159 | str = inXml->getAttributeStringValue(ai, &len); |
| 160 | span.name.append(str, len); |
| 161 | } |
| 162 | //printf("Span: %s\n", String8(span.name).string()); |
| 163 | span.span.firstChar = span.span.lastChar = outString->size(); |
| 164 | spanStack.push(span); |
| 165 | |
| 166 | } else if (code == ResXMLTree::END_TAG) { |
| 167 | size_t nslen; |
| 168 | const uint16_t* ns = inXml->getElementNamespace(&nslen); |
| 169 | if (ns == NULL) { |
| 170 | ns = (const uint16_t*)"\0\0"; |
| 171 | nslen = 0; |
| 172 | } |
| 173 | const String8 nspace(String16(ns, nslen)); |
| 174 | if (nspace == XLIFF_XMLNS) { |
| 175 | xliffDepth--; |
| 176 | continue; |
| 177 | } |
| 178 | if (!ResTable::collectString(outString, curString.string(), |
| 179 | curString.size(), false, &errorMsg, true)) { |
| 180 | SourcePos(String8(fileName), inXml->getLineNumber()).error("%s (in %s)\n", |
| 181 | errorMsg, String8(curString).string()); |
| 182 | return UNKNOWN_ERROR; |
| 183 | } |
| 184 | rawString.append(curString); |
| 185 | curString = String16(); |
| 186 | |
| 187 | if (spanStack.size() == 0) { |
| 188 | if (strcmp16(inXml->getElementName(&len), endTag.string()) != 0) { |
| 189 | SourcePos(String8(fileName), inXml->getLineNumber()).error( |
| 190 | "Found tag %s where <%s> close is expected\n", |
| 191 | String8(inXml->getElementName(&len)).string(), |
| 192 | String8(endTag).string()); |
| 193 | return UNKNOWN_ERROR; |
| 194 | } |
| 195 | break; |
| 196 | } |
| 197 | StringPool::entry_style_span span = spanStack.top(); |
| 198 | String16 spanTag; |
| 199 | ssize_t semi = span.name.findFirst(';'); |
| 200 | if (semi >= 0) { |
| 201 | spanTag.setTo(span.name.string(), semi); |
| 202 | } else { |
| 203 | spanTag.setTo(span.name); |
| 204 | } |
| 205 | if (strcmp16(inXml->getElementName(&len), spanTag.string()) != 0) { |
| 206 | SourcePos(String8(fileName), inXml->getLineNumber()).error( |
| 207 | "Found close tag %s where close tag %s is expected\n", |
| 208 | String8(inXml->getElementName(&len)).string(), |
| 209 | String8(spanTag).string()); |
| 210 | return UNKNOWN_ERROR; |
| 211 | } |
| 212 | bool empty = true; |
| 213 | if (outString->size() > 0) { |
| 214 | span.span.lastChar = outString->size()-1; |
| 215 | if (span.span.lastChar >= span.span.firstChar) { |
| 216 | empty = false; |
| 217 | outSpans->add(span); |
| 218 | } |
| 219 | } |
| 220 | spanStack.pop(); |
| 221 | |
Eric Fischer | c87d252 | 2009-09-01 15:20:30 -0700 | [diff] [blame^] | 222 | /* |
| 223 | * This warning seems to be just an irritation to most people, |
| 224 | * since it is typically introduced by translators who then never |
| 225 | * see the warning. |
| 226 | */ |
| 227 | if (0 && empty) { |
Marco Nelissen | dd93186 | 2009-07-13 13:02:33 -0700 | [diff] [blame] | 228 | fprintf(stderr, "%s:%d: warning: empty '%s' span found in text '%s'\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | fileName, inXml->getLineNumber(), |
| 230 | String8(spanTag).string(), String8(*outString).string()); |
| 231 | |
| 232 | } |
| 233 | } else if (code == ResXMLTree::START_NAMESPACE) { |
| 234 | // nothing |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | if (code == ResXMLTree::BAD_DOCUMENT) { |
| 239 | SourcePos(String8(fileName), inXml->getLineNumber()).error( |
| 240 | "Error parsing XML\n"); |
| 241 | } |
| 242 | |
| 243 | if (outSpans != NULL && outSpans->size() > 0) { |
| 244 | if (curString.size() > 0) { |
| 245 | if (!ResTable::collectString(outString, curString.string(), |
| 246 | curString.size(), false, &errorMsg, true)) { |
| 247 | SourcePos(String8(fileName), inXml->getLineNumber()).error( |
| 248 | "%s (in %s)\n", |
| 249 | errorMsg, String8(curString).string()); |
| 250 | return UNKNOWN_ERROR; |
| 251 | } |
| 252 | } |
| 253 | } else { |
| 254 | // There is no style information, so string processing will happen |
| 255 | // later as part of the overall type conversion. Return to the |
| 256 | // client the raw unprocessed text. |
| 257 | rawString.append(curString); |
| 258 | outString->setTo(rawString); |
| 259 | } |
| 260 | |
| 261 | return NO_ERROR; |
| 262 | } |
| 263 | |
| 264 | struct namespace_entry { |
| 265 | String8 prefix; |
| 266 | String8 uri; |
| 267 | }; |
| 268 | |
| 269 | static String8 make_prefix(int depth) |
| 270 | { |
| 271 | String8 prefix; |
| 272 | int i; |
| 273 | for (i=0; i<depth; i++) { |
| 274 | prefix.append(" "); |
| 275 | } |
| 276 | return prefix; |
| 277 | } |
| 278 | |
| 279 | static String8 build_namespace(const Vector<namespace_entry>& namespaces, |
| 280 | const uint16_t* ns) |
| 281 | { |
| 282 | String8 str; |
| 283 | if (ns != NULL) { |
| 284 | str = String8(ns); |
| 285 | const size_t N = namespaces.size(); |
| 286 | for (size_t i=0; i<N; i++) { |
| 287 | const namespace_entry& ne = namespaces.itemAt(i); |
| 288 | if (ne.uri == str) { |
| 289 | str = ne.prefix; |
| 290 | break; |
| 291 | } |
| 292 | } |
| 293 | str.append(":"); |
| 294 | } |
| 295 | return str; |
| 296 | } |
| 297 | |
| 298 | void printXMLBlock(ResXMLTree* block) |
| 299 | { |
| 300 | block->restart(); |
| 301 | |
| 302 | Vector<namespace_entry> namespaces; |
| 303 | |
| 304 | ResXMLTree::event_code_t code; |
| 305 | int depth = 0; |
| 306 | while ((code=block->next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) { |
| 307 | String8 prefix = make_prefix(depth); |
| 308 | int i; |
| 309 | if (code == ResXMLTree::START_TAG) { |
| 310 | size_t len; |
| 311 | const uint16_t* ns16 = block->getElementNamespace(&len); |
| 312 | String8 elemNs = build_namespace(namespaces, ns16); |
| 313 | const uint16_t* com16 = block->getComment(&len); |
| 314 | if (com16) { |
| 315 | printf("%s <!-- %s -->\n", prefix.string(), String8(com16).string()); |
| 316 | } |
| 317 | printf("%sE: %s%s (line=%d)\n", prefix.string(), elemNs.string(), |
| 318 | String8(block->getElementName(&len)).string(), |
| 319 | block->getLineNumber()); |
| 320 | int N = block->getAttributeCount(); |
| 321 | depth++; |
| 322 | prefix = make_prefix(depth); |
| 323 | for (i=0; i<N; i++) { |
| 324 | uint32_t res = block->getAttributeNameResID(i); |
| 325 | ns16 = block->getAttributeNamespace(i, &len); |
| 326 | String8 ns = build_namespace(namespaces, ns16); |
| 327 | String8 name(block->getAttributeName(i, &len)); |
| 328 | printf("%sA: ", prefix.string()); |
| 329 | if (res) { |
| 330 | printf("%s%s(0x%08x)", ns.string(), name.string(), res); |
| 331 | } else { |
| 332 | printf("%s%s", ns.string(), name.string()); |
| 333 | } |
| 334 | Res_value value; |
| 335 | block->getAttributeValue(i, &value); |
| 336 | if (value.dataType == Res_value::TYPE_NULL) { |
| 337 | printf("=(null)"); |
| 338 | } else if (value.dataType == Res_value::TYPE_REFERENCE) { |
| 339 | printf("=@0x%x", (int)value.data); |
| 340 | } else if (value.dataType == Res_value::TYPE_ATTRIBUTE) { |
| 341 | printf("=?0x%x", (int)value.data); |
| 342 | } else if (value.dataType == Res_value::TYPE_STRING) { |
| 343 | printf("=\"%s\"", |
| 344 | String8(block->getAttributeStringValue(i, &len)).string()); |
| 345 | } else { |
| 346 | printf("=(type 0x%x)0x%x", (int)value.dataType, (int)value.data); |
| 347 | } |
| 348 | const char16_t* val = block->getAttributeStringValue(i, &len); |
| 349 | if (val != NULL) { |
| 350 | printf(" (Raw: \"%s\")", String8(val).string()); |
| 351 | } |
| 352 | printf("\n"); |
| 353 | } |
| 354 | } else if (code == ResXMLTree::END_TAG) { |
| 355 | depth--; |
| 356 | } else if (code == ResXMLTree::START_NAMESPACE) { |
| 357 | namespace_entry ns; |
| 358 | size_t len; |
| 359 | const uint16_t* prefix16 = block->getNamespacePrefix(&len); |
| 360 | if (prefix16) { |
| 361 | ns.prefix = String8(prefix16); |
| 362 | } else { |
| 363 | ns.prefix = "<DEF>"; |
| 364 | } |
| 365 | ns.uri = String8(block->getNamespaceUri(&len)); |
| 366 | namespaces.push(ns); |
| 367 | printf("%sN: %s=%s\n", prefix.string(), ns.prefix.string(), |
| 368 | ns.uri.string()); |
| 369 | depth++; |
| 370 | } else if (code == ResXMLTree::END_NAMESPACE) { |
| 371 | depth--; |
| 372 | const namespace_entry& ns = namespaces.top(); |
| 373 | size_t len; |
| 374 | const uint16_t* prefix16 = block->getNamespacePrefix(&len); |
| 375 | String8 pr; |
| 376 | if (prefix16) { |
| 377 | pr = String8(prefix16); |
| 378 | } else { |
| 379 | pr = "<DEF>"; |
| 380 | } |
| 381 | if (ns.prefix != pr) { |
| 382 | prefix = make_prefix(depth); |
| 383 | printf("%s*** BAD END NS PREFIX: found=%s, expected=%s\n", |
| 384 | prefix.string(), pr.string(), ns.prefix.string()); |
| 385 | } |
| 386 | String8 uri = String8(block->getNamespaceUri(&len)); |
| 387 | if (ns.uri != uri) { |
| 388 | prefix = make_prefix(depth); |
| 389 | printf("%s *** BAD END NS URI: found=%s, expected=%s\n", |
| 390 | prefix.string(), uri.string(), ns.uri.string()); |
| 391 | } |
| 392 | namespaces.pop(); |
| 393 | } else if (code == ResXMLTree::TEXT) { |
| 394 | size_t len; |
| 395 | printf("%sC: \"%s\"\n", prefix.string(), String8(block->getText(&len)).string()); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | block->restart(); |
| 400 | } |
| 401 | |
| 402 | status_t parseXMLResource(const sp<AaptFile>& file, ResXMLTree* outTree, |
| 403 | bool stripAll, bool keepComments, |
| 404 | const char** cDataTags) |
| 405 | { |
| 406 | sp<XMLNode> root = XMLNode::parse(file); |
| 407 | if (root == NULL) { |
| 408 | return UNKNOWN_ERROR; |
| 409 | } |
| 410 | root->removeWhitespace(stripAll, cDataTags); |
| 411 | |
| 412 | NOISY(printf("Input XML from %s:\n", (const char*)file->getPrintableSource())); |
| 413 | NOISY(root->print()); |
| 414 | sp<AaptFile> rsc = new AaptFile(String8(), AaptGroupEntry(), String8()); |
| 415 | status_t err = root->flatten(rsc, !keepComments, false); |
| 416 | if (err != NO_ERROR) { |
| 417 | return err; |
| 418 | } |
| 419 | err = outTree->setTo(rsc->getData(), rsc->getSize(), true); |
| 420 | if (err != NO_ERROR) { |
| 421 | return err; |
| 422 | } |
| 423 | |
| 424 | NOISY(printf("Output XML:\n")); |
| 425 | NOISY(printXMLBlock(outTree)); |
| 426 | |
| 427 | return NO_ERROR; |
| 428 | } |
| 429 | |
| 430 | sp<XMLNode> XMLNode::parse(const sp<AaptFile>& file) |
| 431 | { |
| 432 | char buf[16384]; |
| 433 | int fd = open(file->getSourceFile().string(), O_RDONLY | O_BINARY); |
| 434 | if (fd < 0) { |
| 435 | SourcePos(file->getSourceFile(), -1).error("Unable to open file for read: %s", |
| 436 | strerror(errno)); |
| 437 | return NULL; |
| 438 | } |
| 439 | |
| 440 | XML_Parser parser = XML_ParserCreateNS(NULL, 1); |
| 441 | ParseState state; |
| 442 | state.filename = file->getPrintableSource(); |
| 443 | state.parser = parser; |
| 444 | XML_SetUserData(parser, &state); |
| 445 | XML_SetElementHandler(parser, startElement, endElement); |
| 446 | XML_SetNamespaceDeclHandler(parser, startNamespace, endNamespace); |
| 447 | XML_SetCharacterDataHandler(parser, characterData); |
| 448 | XML_SetCommentHandler(parser, commentData); |
| 449 | |
| 450 | ssize_t len; |
| 451 | bool done; |
| 452 | do { |
| 453 | len = read(fd, buf, sizeof(buf)); |
| 454 | done = len < (ssize_t)sizeof(buf); |
| 455 | if (len < 0) { |
| 456 | SourcePos(file->getSourceFile(), -1).error("Error reading file: %s\n", strerror(errno)); |
| 457 | close(fd); |
| 458 | return NULL; |
| 459 | } |
| 460 | if (XML_Parse(parser, buf, len, done) == XML_STATUS_ERROR) { |
| 461 | SourcePos(file->getSourceFile(), (int)XML_GetCurrentLineNumber(parser)).error( |
| 462 | "Error parsing XML: %s\n", XML_ErrorString(XML_GetErrorCode(parser))); |
| 463 | close(fd); |
| 464 | return NULL; |
| 465 | } |
| 466 | } while (!done); |
| 467 | |
| 468 | XML_ParserFree(parser); |
| 469 | if (state.root == NULL) { |
| 470 | SourcePos(file->getSourceFile(), -1).error("No XML data generated when parsing"); |
| 471 | } |
| 472 | close(fd); |
| 473 | return state.root; |
| 474 | } |
| 475 | |
| 476 | XMLNode::XMLNode(const String8& filename, const String16& s1, const String16& s2, bool isNamespace) |
| 477 | : mNextAttributeIndex(0x80000000) |
| 478 | , mFilename(filename) |
| 479 | , mStartLineNumber(0) |
| 480 | , mEndLineNumber(0) |
| 481 | { |
| 482 | if (isNamespace) { |
| 483 | mNamespacePrefix = s1; |
| 484 | mNamespaceUri = s2; |
| 485 | } else { |
| 486 | mNamespaceUri = s1; |
| 487 | mElementName = s2; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | XMLNode::XMLNode(const String8& filename) |
| 492 | : mFilename(filename) |
| 493 | { |
Marco Nelissen | 6a1fade | 2009-04-20 16:16:01 -0700 | [diff] [blame] | 494 | memset(&mCharsValue, 0, sizeof(mCharsValue)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | XMLNode::type XMLNode::getType() const |
| 498 | { |
| 499 | if (mElementName.size() != 0) { |
| 500 | return TYPE_ELEMENT; |
| 501 | } |
| 502 | if (mNamespaceUri.size() != 0) { |
| 503 | return TYPE_NAMESPACE; |
| 504 | } |
| 505 | return TYPE_CDATA; |
| 506 | } |
| 507 | |
| 508 | const String16& XMLNode::getNamespacePrefix() const |
| 509 | { |
| 510 | return mNamespacePrefix; |
| 511 | } |
| 512 | |
| 513 | const String16& XMLNode::getNamespaceUri() const |
| 514 | { |
| 515 | return mNamespaceUri; |
| 516 | } |
| 517 | |
| 518 | const String16& XMLNode::getElementNamespace() const |
| 519 | { |
| 520 | return mNamespaceUri; |
| 521 | } |
| 522 | |
| 523 | const String16& XMLNode::getElementName() const |
| 524 | { |
| 525 | return mElementName; |
| 526 | } |
| 527 | |
| 528 | const Vector<sp<XMLNode> >& XMLNode::getChildren() const |
| 529 | { |
| 530 | return mChildren; |
| 531 | } |
| 532 | |
Dianne Hackborn | 62da846 | 2009-05-13 15:06:13 -0700 | [diff] [blame] | 533 | const String8& XMLNode::getFilename() const |
| 534 | { |
| 535 | return mFilename; |
| 536 | } |
| 537 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 538 | const Vector<XMLNode::attribute_entry>& |
| 539 | XMLNode::getAttributes() const |
| 540 | { |
| 541 | return mAttributes; |
| 542 | } |
| 543 | |
Dianne Hackborn | 62da846 | 2009-05-13 15:06:13 -0700 | [diff] [blame] | 544 | const XMLNode::attribute_entry* XMLNode::getAttribute(const String16& ns, |
| 545 | const String16& name) const |
| 546 | { |
| 547 | for (size_t i=0; i<mAttributes.size(); i++) { |
| 548 | const attribute_entry& ae(mAttributes.itemAt(i)); |
| 549 | if (ae.ns == ns && ae.name == name) { |
| 550 | return &ae; |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | return NULL; |
| 555 | } |
| 556 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 557 | const String16& XMLNode::getCData() const |
| 558 | { |
| 559 | return mChars; |
| 560 | } |
| 561 | |
| 562 | const String16& XMLNode::getComment() const |
| 563 | { |
| 564 | return mComment; |
| 565 | } |
| 566 | |
| 567 | int32_t XMLNode::getStartLineNumber() const |
| 568 | { |
| 569 | return mStartLineNumber; |
| 570 | } |
| 571 | |
| 572 | int32_t XMLNode::getEndLineNumber() const |
| 573 | { |
| 574 | return mEndLineNumber; |
| 575 | } |
| 576 | |
Dianne Hackborn | 62da846 | 2009-05-13 15:06:13 -0700 | [diff] [blame] | 577 | sp<XMLNode> XMLNode::searchElement(const String16& tagNamespace, const String16& tagName) |
| 578 | { |
| 579 | if (getType() == XMLNode::TYPE_ELEMENT |
| 580 | && mNamespaceUri == tagNamespace |
| 581 | && mElementName == tagName) { |
| 582 | return this; |
| 583 | } |
| 584 | |
| 585 | for (size_t i=0; i<mChildren.size(); i++) { |
| 586 | sp<XMLNode> found = mChildren.itemAt(i)->searchElement(tagNamespace, tagName); |
| 587 | if (found != NULL) { |
| 588 | return found; |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | return NULL; |
| 593 | } |
| 594 | |
| 595 | sp<XMLNode> XMLNode::getChildElement(const String16& tagNamespace, const String16& tagName) |
| 596 | { |
| 597 | for (size_t i=0; i<mChildren.size(); i++) { |
| 598 | sp<XMLNode> child = mChildren.itemAt(i); |
| 599 | if (child->getType() == XMLNode::TYPE_ELEMENT |
| 600 | && child->mNamespaceUri == tagNamespace |
| 601 | && child->mElementName == tagName) { |
| 602 | return child; |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | return NULL; |
| 607 | } |
| 608 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 609 | status_t XMLNode::addChild(const sp<XMLNode>& child) |
| 610 | { |
| 611 | if (getType() == TYPE_CDATA) { |
| 612 | SourcePos(mFilename, child->getStartLineNumber()).error("Child to CDATA node."); |
| 613 | return UNKNOWN_ERROR; |
| 614 | } |
| 615 | //printf("Adding child %p to parent %p\n", child.get(), this); |
| 616 | mChildren.add(child); |
| 617 | return NO_ERROR; |
| 618 | } |
| 619 | |
Dianne Hackborn | 62da846 | 2009-05-13 15:06:13 -0700 | [diff] [blame] | 620 | status_t XMLNode::insertChildAt(const sp<XMLNode>& child, size_t index) |
| 621 | { |
| 622 | if (getType() == TYPE_CDATA) { |
| 623 | SourcePos(mFilename, child->getStartLineNumber()).error("Child to CDATA node."); |
| 624 | return UNKNOWN_ERROR; |
| 625 | } |
| 626 | //printf("Adding child %p to parent %p\n", child.get(), this); |
| 627 | mChildren.insertAt(child, index); |
| 628 | return NO_ERROR; |
| 629 | } |
| 630 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 631 | status_t XMLNode::addAttribute(const String16& ns, const String16& name, |
| 632 | const String16& value) |
| 633 | { |
| 634 | if (getType() == TYPE_CDATA) { |
| 635 | SourcePos(mFilename, getStartLineNumber()).error("Child to CDATA node."); |
| 636 | return UNKNOWN_ERROR; |
| 637 | } |
| 638 | attribute_entry e; |
| 639 | e.index = mNextAttributeIndex++; |
| 640 | e.ns = ns; |
| 641 | e.name = name; |
| 642 | e.string = value; |
| 643 | mAttributes.add(e); |
| 644 | mAttributeOrder.add(e.index, mAttributes.size()-1); |
| 645 | return NO_ERROR; |
| 646 | } |
| 647 | |
| 648 | void XMLNode::setAttributeResID(size_t attrIdx, uint32_t resId) |
| 649 | { |
| 650 | attribute_entry& e = mAttributes.editItemAt(attrIdx); |
| 651 | if (e.nameResId) { |
| 652 | mAttributeOrder.removeItem(e.nameResId); |
| 653 | } else { |
| 654 | mAttributeOrder.removeItem(e.index); |
| 655 | } |
| 656 | NOISY(printf("Elem %s %s=\"%s\": set res id = 0x%08x\n", |
| 657 | String8(getElementName()).string(), |
| 658 | String8(mAttributes.itemAt(attrIdx).name).string(), |
| 659 | String8(mAttributes.itemAt(attrIdx).string).string(), |
| 660 | resId)); |
| 661 | mAttributes.editItemAt(attrIdx).nameResId = resId; |
| 662 | mAttributeOrder.add(resId, attrIdx); |
| 663 | } |
| 664 | |
| 665 | status_t XMLNode::appendChars(const String16& chars) |
| 666 | { |
| 667 | if (getType() != TYPE_CDATA) { |
| 668 | SourcePos(mFilename, getStartLineNumber()).error("Adding characters to element node."); |
| 669 | return UNKNOWN_ERROR; |
| 670 | } |
| 671 | mChars.append(chars); |
| 672 | return NO_ERROR; |
| 673 | } |
| 674 | |
| 675 | status_t XMLNode::appendComment(const String16& comment) |
| 676 | { |
| 677 | if (mComment.size() > 0) { |
| 678 | mComment.append(String16("\n")); |
| 679 | } |
| 680 | mComment.append(comment); |
| 681 | return NO_ERROR; |
| 682 | } |
| 683 | |
| 684 | void XMLNode::setStartLineNumber(int32_t line) |
| 685 | { |
| 686 | mStartLineNumber = line; |
| 687 | } |
| 688 | |
| 689 | void XMLNode::setEndLineNumber(int32_t line) |
| 690 | { |
| 691 | mEndLineNumber = line; |
| 692 | } |
| 693 | |
| 694 | void XMLNode::removeWhitespace(bool stripAll, const char** cDataTags) |
| 695 | { |
| 696 | //printf("Removing whitespace in %s\n", String8(mElementName).string()); |
| 697 | size_t N = mChildren.size(); |
| 698 | if (cDataTags) { |
| 699 | String8 tag(mElementName); |
| 700 | const char** p = cDataTags; |
| 701 | while (*p) { |
| 702 | if (tag == *p) { |
| 703 | stripAll = false; |
| 704 | break; |
| 705 | } |
| 706 | } |
| 707 | } |
| 708 | for (size_t i=0; i<N; i++) { |
| 709 | sp<XMLNode> node = mChildren.itemAt(i); |
| 710 | if (node->getType() == TYPE_CDATA) { |
| 711 | // This is a CDATA node... |
| 712 | const char16_t* p = node->mChars.string(); |
| 713 | while (*p != 0 && *p < 128 && isspace(*p)) { |
| 714 | p++; |
| 715 | } |
| 716 | //printf("Space ends at %d in \"%s\"\n", |
| 717 | // (int)(p-node->mChars.string()), |
| 718 | // String8(node->mChars).string()); |
| 719 | if (*p == 0) { |
| 720 | if (stripAll) { |
| 721 | // Remove this node! |
| 722 | mChildren.removeAt(i); |
| 723 | N--; |
| 724 | i--; |
| 725 | } else { |
| 726 | node->mChars = String16(" "); |
| 727 | } |
| 728 | } else { |
| 729 | // Compact leading/trailing whitespace. |
| 730 | const char16_t* e = node->mChars.string()+node->mChars.size()-1; |
| 731 | while (e > p && *e < 128 && isspace(*e)) { |
| 732 | e--; |
| 733 | } |
| 734 | if (p > node->mChars.string()) { |
| 735 | p--; |
| 736 | } |
| 737 | if (e < (node->mChars.string()+node->mChars.size()-1)) { |
| 738 | e++; |
| 739 | } |
| 740 | if (p > node->mChars.string() || |
| 741 | e < (node->mChars.string()+node->mChars.size()-1)) { |
| 742 | String16 tmp(p, e-p+1); |
| 743 | node->mChars = tmp; |
| 744 | } |
| 745 | } |
| 746 | } else { |
| 747 | node->removeWhitespace(stripAll, cDataTags); |
| 748 | } |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | status_t XMLNode::parseValues(const sp<AaptAssets>& assets, |
| 753 | ResourceTable* table) |
| 754 | { |
| 755 | bool hasErrors = false; |
| 756 | |
| 757 | if (getType() == TYPE_ELEMENT) { |
| 758 | const size_t N = mAttributes.size(); |
| 759 | String16 defPackage(assets->getPackage()); |
| 760 | for (size_t i=0; i<N; i++) { |
| 761 | attribute_entry& e = mAttributes.editItemAt(i); |
| 762 | AccessorCookie ac(SourcePos(mFilename, getStartLineNumber()), String8(e.name), |
| 763 | String8(e.string)); |
| 764 | table->setCurrentXmlPos(SourcePos(mFilename, getStartLineNumber())); |
| 765 | if (!assets->getIncludedResources() |
| 766 | .stringToValue(&e.value, &e.string, |
| 767 | e.string.string(), e.string.size(), true, true, |
| 768 | e.nameResId, NULL, &defPackage, table, &ac)) { |
| 769 | hasErrors = true; |
| 770 | } |
| 771 | NOISY(printf("Attr %s: type=0x%x, str=%s\n", |
| 772 | String8(e.name).string(), e.value.dataType, |
| 773 | String8(e.string).string())); |
| 774 | } |
| 775 | } |
| 776 | const size_t N = mChildren.size(); |
| 777 | for (size_t i=0; i<N; i++) { |
| 778 | status_t err = mChildren.itemAt(i)->parseValues(assets, table); |
| 779 | if (err != NO_ERROR) { |
| 780 | hasErrors = true; |
| 781 | } |
| 782 | } |
| 783 | return hasErrors ? UNKNOWN_ERROR : NO_ERROR; |
| 784 | } |
| 785 | |
| 786 | status_t XMLNode::assignResourceIds(const sp<AaptAssets>& assets, |
| 787 | const ResourceTable* table) |
| 788 | { |
| 789 | bool hasErrors = false; |
| 790 | |
| 791 | if (getType() == TYPE_ELEMENT) { |
| 792 | String16 attr("attr"); |
| 793 | const char* errorMsg; |
| 794 | const size_t N = mAttributes.size(); |
| 795 | for (size_t i=0; i<N; i++) { |
| 796 | const attribute_entry& e = mAttributes.itemAt(i); |
| 797 | if (e.ns.size() <= 0) continue; |
| 798 | bool nsIsPublic; |
| 799 | String16 pkg(getNamespaceResourcePackage(e.ns, &nsIsPublic)); |
| 800 | NOISY(printf("Elem %s %s=\"%s\": namespace(%s) %s ===> %s\n", |
| 801 | String8(getElementName()).string(), |
| 802 | String8(e.name).string(), |
| 803 | String8(e.string).string(), |
| 804 | String8(e.ns).string(), |
| 805 | (nsIsPublic) ? "public" : "private", |
| 806 | String8(pkg).string())); |
| 807 | if (pkg.size() <= 0) continue; |
| 808 | uint32_t res = table != NULL |
| 809 | ? table->getResId(e.name, &attr, &pkg, &errorMsg, nsIsPublic) |
| 810 | : assets->getIncludedResources(). |
| 811 | identifierForName(e.name.string(), e.name.size(), |
| 812 | attr.string(), attr.size(), |
| 813 | pkg.string(), pkg.size()); |
| 814 | if (res != 0) { |
| 815 | NOISY(printf("XML attribute name %s: resid=0x%08x\n", |
| 816 | String8(e.name).string(), res)); |
| 817 | setAttributeResID(i, res); |
| 818 | } else { |
| 819 | SourcePos(mFilename, getStartLineNumber()).error( |
| 820 | "No resource identifier found for attribute '%s' in package '%s'\n", |
| 821 | String8(e.name).string(), String8(pkg).string()); |
| 822 | hasErrors = true; |
| 823 | } |
| 824 | } |
| 825 | } |
| 826 | const size_t N = mChildren.size(); |
| 827 | for (size_t i=0; i<N; i++) { |
| 828 | status_t err = mChildren.itemAt(i)->assignResourceIds(assets, table); |
| 829 | if (err < NO_ERROR) { |
| 830 | hasErrors = true; |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | return hasErrors ? UNKNOWN_ERROR : NO_ERROR; |
| 835 | } |
| 836 | |
| 837 | status_t XMLNode::flatten(const sp<AaptFile>& dest, |
| 838 | bool stripComments, bool stripRawValues) const |
| 839 | { |
| 840 | StringPool strings; |
| 841 | Vector<uint32_t> resids; |
| 842 | |
| 843 | // First collect just the strings for attribute names that have a |
| 844 | // resource ID assigned to them. This ensures that the resource ID |
| 845 | // array is compact, and makes it easier to deal with attribute names |
| 846 | // in different namespaces (and thus with different resource IDs). |
| 847 | collect_resid_strings(&strings, &resids); |
| 848 | |
| 849 | // Next collect all remainibng strings. |
| 850 | collect_strings(&strings, &resids, stripComments, stripRawValues); |
| 851 | |
| 852 | #if 0 // No longer compiles |
| 853 | NOISY(printf("Found strings:\n"); |
| 854 | const size_t N = strings.size(); |
| 855 | for (size_t i=0; i<N; i++) { |
| 856 | printf("%s\n", String8(strings.entryAt(i).string).string()); |
| 857 | } |
| 858 | ); |
| 859 | #endif |
| 860 | |
| 861 | sp<AaptFile> stringPool = strings.createStringBlock(); |
| 862 | NOISY(aout << "String pool:" |
| 863 | << HexDump(stringPool->getData(), stringPool->getSize()) << endl); |
| 864 | |
| 865 | ResXMLTree_header header; |
| 866 | memset(&header, 0, sizeof(header)); |
| 867 | header.header.type = htods(RES_XML_TYPE); |
| 868 | header.header.headerSize = htods(sizeof(header)); |
| 869 | |
| 870 | const size_t basePos = dest->getSize(); |
| 871 | dest->writeData(&header, sizeof(header)); |
| 872 | dest->writeData(stringPool->getData(), stringPool->getSize()); |
| 873 | |
| 874 | // If we have resource IDs, write them. |
| 875 | if (resids.size() > 0) { |
| 876 | const size_t resIdsPos = dest->getSize(); |
| 877 | const size_t resIdsSize = |
| 878 | sizeof(ResChunk_header)+(sizeof(uint32_t)*resids.size()); |
| 879 | ResChunk_header* idsHeader = (ResChunk_header*) |
| 880 | (((const uint8_t*)dest->editData(resIdsPos+resIdsSize))+resIdsPos); |
| 881 | idsHeader->type = htods(RES_XML_RESOURCE_MAP_TYPE); |
| 882 | idsHeader->headerSize = htods(sizeof(*idsHeader)); |
| 883 | idsHeader->size = htodl(resIdsSize); |
| 884 | uint32_t* ids = (uint32_t*)(idsHeader+1); |
| 885 | for (size_t i=0; i<resids.size(); i++) { |
| 886 | *ids++ = htodl(resids[i]); |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | flatten_node(strings, dest, stripComments, stripRawValues); |
| 891 | |
| 892 | void* data = dest->editData(); |
| 893 | ResXMLTree_header* hd = (ResXMLTree_header*)(((uint8_t*)data)+basePos); |
| 894 | size_t size = dest->getSize()-basePos; |
| 895 | hd->header.size = htodl(dest->getSize()-basePos); |
| 896 | |
| 897 | NOISY(aout << "XML resource:" |
| 898 | << HexDump(dest->getData(), dest->getSize()) << endl); |
| 899 | |
| 900 | #if PRINT_STRING_METRICS |
| 901 | fprintf(stderr, "**** total xml size: %d / %d%% strings (in %s)\n", |
| 902 | dest->getSize(), (stringPool->getSize()*100)/dest->getSize(), |
| 903 | dest->getPath().string()); |
| 904 | #endif |
| 905 | |
| 906 | return NO_ERROR; |
| 907 | } |
| 908 | |
| 909 | void XMLNode::print(int indent) |
| 910 | { |
| 911 | String8 prefix; |
| 912 | int i; |
| 913 | for (i=0; i<indent; i++) { |
| 914 | prefix.append(" "); |
| 915 | } |
| 916 | if (getType() == TYPE_ELEMENT) { |
| 917 | String8 elemNs(getNamespaceUri()); |
| 918 | if (elemNs.size() > 0) { |
| 919 | elemNs.append(":"); |
| 920 | } |
| 921 | printf("%s E: %s%s", prefix.string(), |
| 922 | elemNs.string(), String8(getElementName()).string()); |
| 923 | int N = mAttributes.size(); |
| 924 | for (i=0; i<N; i++) { |
| 925 | ssize_t idx = mAttributeOrder.valueAt(i); |
| 926 | if (i == 0) { |
| 927 | printf(" / "); |
| 928 | } else { |
| 929 | printf(", "); |
| 930 | } |
| 931 | const attribute_entry& attr = mAttributes.itemAt(idx); |
| 932 | String8 attrNs(attr.ns); |
| 933 | if (attrNs.size() > 0) { |
| 934 | attrNs.append(":"); |
| 935 | } |
| 936 | if (attr.nameResId) { |
| 937 | printf("%s%s(0x%08x)", attrNs.string(), |
| 938 | String8(attr.name).string(), attr.nameResId); |
| 939 | } else { |
| 940 | printf("%s%s", attrNs.string(), String8(attr.name).string()); |
| 941 | } |
| 942 | printf("=%s", String8(attr.string).string()); |
| 943 | } |
| 944 | printf("\n"); |
| 945 | } else if (getType() == TYPE_NAMESPACE) { |
| 946 | printf("%s N: %s=%s\n", prefix.string(), |
| 947 | getNamespacePrefix().size() > 0 |
| 948 | ? String8(getNamespacePrefix()).string() : "<DEF>", |
| 949 | String8(getNamespaceUri()).string()); |
| 950 | } else { |
| 951 | printf("%s C: \"%s\"\n", prefix.string(), String8(getCData()).string()); |
| 952 | } |
| 953 | int N = mChildren.size(); |
| 954 | for (i=0; i<N; i++) { |
| 955 | mChildren.itemAt(i)->print(indent+1); |
| 956 | } |
| 957 | } |
| 958 | |
| 959 | static void splitName(const char* name, String16* outNs, String16* outName) |
| 960 | { |
| 961 | const char* p = name; |
| 962 | while (*p != 0 && *p != 1) { |
| 963 | p++; |
| 964 | } |
| 965 | if (*p == 0) { |
| 966 | *outNs = String16(); |
| 967 | *outName = String16(name); |
| 968 | } else { |
| 969 | *outNs = String16(name, (p-name)); |
| 970 | *outName = String16(p+1); |
| 971 | } |
| 972 | } |
| 973 | |
| 974 | void XMLCALL |
| 975 | XMLNode::startNamespace(void *userData, const char *prefix, const char *uri) |
| 976 | { |
| 977 | NOISY_PARSE(printf("Start Namespace: %s %s\n", prefix, uri)); |
| 978 | ParseState* st = (ParseState*)userData; |
| 979 | sp<XMLNode> node = XMLNode::newNamespace(st->filename, |
| 980 | String16(prefix != NULL ? prefix : ""), String16(uri)); |
| 981 | node->setStartLineNumber(XML_GetCurrentLineNumber(st->parser)); |
| 982 | if (st->stack.size() > 0) { |
| 983 | st->stack.itemAt(st->stack.size()-1)->addChild(node); |
| 984 | } else { |
| 985 | st->root = node; |
| 986 | } |
| 987 | st->stack.push(node); |
| 988 | } |
| 989 | |
| 990 | void XMLCALL |
| 991 | XMLNode::startElement(void *userData, const char *name, const char **atts) |
| 992 | { |
| 993 | NOISY_PARSE(printf("Start Element: %s\n", name)); |
| 994 | ParseState* st = (ParseState*)userData; |
| 995 | String16 ns16, name16; |
| 996 | splitName(name, &ns16, &name16); |
| 997 | sp<XMLNode> node = XMLNode::newElement(st->filename, ns16, name16); |
| 998 | node->setStartLineNumber(XML_GetCurrentLineNumber(st->parser)); |
| 999 | if (st->pendingComment.size() > 0) { |
| 1000 | node->appendComment(st->pendingComment); |
| 1001 | st->pendingComment = String16(); |
| 1002 | } |
| 1003 | if (st->stack.size() > 0) { |
| 1004 | st->stack.itemAt(st->stack.size()-1)->addChild(node); |
| 1005 | } else { |
| 1006 | st->root = node; |
| 1007 | } |
| 1008 | st->stack.push(node); |
| 1009 | |
| 1010 | for (int i = 0; atts[i]; i += 2) { |
| 1011 | splitName(atts[i], &ns16, &name16); |
| 1012 | node->addAttribute(ns16, name16, String16(atts[i+1])); |
| 1013 | } |
| 1014 | } |
| 1015 | |
| 1016 | void XMLCALL |
| 1017 | XMLNode::characterData(void *userData, const XML_Char *s, int len) |
| 1018 | { |
| 1019 | NOISY_PARSE(printf("CDATA: \"%s\"\n", String8(s, len).string())); |
| 1020 | ParseState* st = (ParseState*)userData; |
| 1021 | sp<XMLNode> node = NULL; |
| 1022 | if (st->stack.size() == 0) { |
| 1023 | return; |
| 1024 | } |
| 1025 | sp<XMLNode> parent = st->stack.itemAt(st->stack.size()-1); |
| 1026 | if (parent != NULL && parent->getChildren().size() > 0) { |
| 1027 | node = parent->getChildren()[parent->getChildren().size()-1]; |
| 1028 | if (node->getType() != TYPE_CDATA) { |
| 1029 | // Last node is not CDATA, need to make a new node. |
| 1030 | node = NULL; |
| 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | if (node == NULL) { |
| 1035 | node = XMLNode::newCData(st->filename); |
| 1036 | node->setStartLineNumber(XML_GetCurrentLineNumber(st->parser)); |
| 1037 | parent->addChild(node); |
| 1038 | } |
| 1039 | |
| 1040 | node->appendChars(String16(s, len)); |
| 1041 | } |
| 1042 | |
| 1043 | void XMLCALL |
| 1044 | XMLNode::endElement(void *userData, const char *name) |
| 1045 | { |
| 1046 | NOISY_PARSE(printf("End Element: %s\n", name)); |
| 1047 | ParseState* st = (ParseState*)userData; |
| 1048 | sp<XMLNode> node = st->stack.itemAt(st->stack.size()-1); |
| 1049 | node->setEndLineNumber(XML_GetCurrentLineNumber(st->parser)); |
| 1050 | if (st->pendingComment.size() > 0) { |
| 1051 | node->appendComment(st->pendingComment); |
| 1052 | st->pendingComment = String16(); |
| 1053 | } |
| 1054 | String16 ns16, name16; |
| 1055 | splitName(name, &ns16, &name16); |
| 1056 | LOG_ALWAYS_FATAL_IF(node->getElementNamespace() != ns16 |
| 1057 | || node->getElementName() != name16, |
| 1058 | "Bad end element %s", name); |
| 1059 | st->stack.pop(); |
| 1060 | } |
| 1061 | |
| 1062 | void XMLCALL |
| 1063 | XMLNode::endNamespace(void *userData, const char *prefix) |
| 1064 | { |
| 1065 | const char* nonNullPrefix = prefix != NULL ? prefix : ""; |
| 1066 | NOISY_PARSE(printf("End Namespace: %s\n", prefix)); |
| 1067 | ParseState* st = (ParseState*)userData; |
| 1068 | sp<XMLNode> node = st->stack.itemAt(st->stack.size()-1); |
| 1069 | node->setEndLineNumber(XML_GetCurrentLineNumber(st->parser)); |
| 1070 | LOG_ALWAYS_FATAL_IF(node->getNamespacePrefix() != String16(nonNullPrefix), |
| 1071 | "Bad end namespace %s", prefix); |
| 1072 | st->stack.pop(); |
| 1073 | } |
| 1074 | |
| 1075 | void XMLCALL |
| 1076 | XMLNode::commentData(void *userData, const char *comment) |
| 1077 | { |
| 1078 | NOISY_PARSE(printf("Comment: %s\n", comment)); |
| 1079 | ParseState* st = (ParseState*)userData; |
| 1080 | if (st->pendingComment.size() > 0) { |
| 1081 | st->pendingComment.append(String16("\n")); |
| 1082 | } |
| 1083 | st->pendingComment.append(String16(comment)); |
| 1084 | } |
| 1085 | |
| 1086 | status_t XMLNode::collect_strings(StringPool* dest, Vector<uint32_t>* outResIds, |
| 1087 | bool stripComments, bool stripRawValues) const |
| 1088 | { |
| 1089 | collect_attr_strings(dest, outResIds, true); |
| 1090 | |
| 1091 | int i; |
| 1092 | if (mNamespacePrefix.size() > 0) { |
| 1093 | dest->add(mNamespacePrefix, true); |
| 1094 | } |
| 1095 | if (mNamespaceUri.size() > 0) { |
| 1096 | dest->add(mNamespaceUri, true); |
| 1097 | } |
| 1098 | if (mElementName.size() > 0) { |
| 1099 | dest->add(mElementName, true); |
| 1100 | } |
| 1101 | |
| 1102 | if (!stripComments && mComment.size() > 0) { |
| 1103 | dest->add(mComment, true); |
| 1104 | } |
| 1105 | |
| 1106 | const int NA = mAttributes.size(); |
| 1107 | |
| 1108 | for (i=0; i<NA; i++) { |
| 1109 | const attribute_entry& ae = mAttributes.itemAt(i); |
| 1110 | if (ae.ns.size() > 0) { |
| 1111 | dest->add(ae.ns, true); |
| 1112 | } |
| 1113 | if (!stripRawValues || ae.needStringValue()) { |
| 1114 | dest->add(ae.string, true); |
| 1115 | } |
| 1116 | /* |
| 1117 | if (ae.value.dataType == Res_value::TYPE_NULL |
| 1118 | || ae.value.dataType == Res_value::TYPE_STRING) { |
| 1119 | dest->add(ae.string, true); |
| 1120 | } |
| 1121 | */ |
| 1122 | } |
| 1123 | |
| 1124 | if (mElementName.size() == 0) { |
| 1125 | // If not an element, include the CDATA, even if it is empty. |
| 1126 | dest->add(mChars, true); |
| 1127 | } |
| 1128 | |
| 1129 | const int NC = mChildren.size(); |
| 1130 | |
| 1131 | for (i=0; i<NC; i++) { |
| 1132 | mChildren.itemAt(i)->collect_strings(dest, outResIds, |
| 1133 | stripComments, stripRawValues); |
| 1134 | } |
| 1135 | |
| 1136 | return NO_ERROR; |
| 1137 | } |
| 1138 | |
| 1139 | status_t XMLNode::collect_attr_strings(StringPool* outPool, |
| 1140 | Vector<uint32_t>* outResIds, bool allAttrs) const { |
| 1141 | const int NA = mAttributes.size(); |
| 1142 | |
| 1143 | for (int i=0; i<NA; i++) { |
| 1144 | const attribute_entry& attr = mAttributes.itemAt(i); |
| 1145 | uint32_t id = attr.nameResId; |
| 1146 | if (id || allAttrs) { |
| 1147 | // See if we have already assigned this resource ID to a pooled |
| 1148 | // string... |
| 1149 | const Vector<size_t>* indices = outPool->offsetsForString(attr.name); |
| 1150 | ssize_t idx = -1; |
| 1151 | if (indices != NULL) { |
| 1152 | const int NJ = indices->size(); |
| 1153 | const size_t NR = outResIds->size(); |
| 1154 | for (int j=0; j<NJ; j++) { |
| 1155 | size_t strIdx = indices->itemAt(j); |
| 1156 | if (strIdx >= NR) { |
| 1157 | if (id == 0) { |
| 1158 | // We don't need to assign a resource ID for this one. |
| 1159 | idx = strIdx; |
| 1160 | break; |
| 1161 | } |
| 1162 | // Just ignore strings that are out of range of |
| 1163 | // the currently assigned resource IDs... we add |
| 1164 | // strings as we assign the first ID. |
| 1165 | } else if (outResIds->itemAt(strIdx) == id) { |
| 1166 | idx = strIdx; |
| 1167 | break; |
| 1168 | } |
| 1169 | } |
| 1170 | } |
| 1171 | if (idx < 0) { |
| 1172 | idx = outPool->add(attr.name); |
| 1173 | NOISY(printf("Adding attr %s (resid 0x%08x) to pool: idx=%d\n", |
| 1174 | String8(attr.name).string(), id, idx)); |
| 1175 | if (id != 0) { |
| 1176 | while ((ssize_t)outResIds->size() <= idx) { |
| 1177 | outResIds->add(0); |
| 1178 | } |
| 1179 | outResIds->replaceAt(id, idx); |
| 1180 | } |
| 1181 | } |
| 1182 | attr.namePoolIdx = idx; |
| 1183 | NOISY(printf("String %s offset=0x%08x\n", |
| 1184 | String8(attr.name).string(), idx)); |
| 1185 | } |
| 1186 | } |
| 1187 | |
| 1188 | return NO_ERROR; |
| 1189 | } |
| 1190 | |
| 1191 | status_t XMLNode::collect_resid_strings(StringPool* outPool, |
| 1192 | Vector<uint32_t>* outResIds) const |
| 1193 | { |
| 1194 | collect_attr_strings(outPool, outResIds, false); |
| 1195 | |
| 1196 | const int NC = mChildren.size(); |
| 1197 | |
| 1198 | for (int i=0; i<NC; i++) { |
| 1199 | mChildren.itemAt(i)->collect_resid_strings(outPool, outResIds); |
| 1200 | } |
| 1201 | |
| 1202 | return NO_ERROR; |
| 1203 | } |
| 1204 | |
| 1205 | status_t XMLNode::flatten_node(const StringPool& strings, const sp<AaptFile>& dest, |
| 1206 | bool stripComments, bool stripRawValues) const |
| 1207 | { |
| 1208 | ResXMLTree_node node; |
| 1209 | ResXMLTree_cdataExt cdataExt; |
| 1210 | ResXMLTree_namespaceExt namespaceExt; |
| 1211 | ResXMLTree_attrExt attrExt; |
| 1212 | const void* extData = NULL; |
| 1213 | size_t extSize = 0; |
| 1214 | ResXMLTree_attribute attr; |
| 1215 | |
| 1216 | const size_t NA = mAttributes.size(); |
| 1217 | const size_t NC = mChildren.size(); |
| 1218 | size_t i; |
| 1219 | |
| 1220 | LOG_ALWAYS_FATAL_IF(NA != mAttributeOrder.size(), "Attributes messed up!"); |
| 1221 | |
| 1222 | const String16 id16("id"); |
| 1223 | const String16 class16("class"); |
| 1224 | const String16 style16("style"); |
| 1225 | |
| 1226 | const type type = getType(); |
| 1227 | |
| 1228 | memset(&node, 0, sizeof(node)); |
| 1229 | memset(&attr, 0, sizeof(attr)); |
| 1230 | node.header.headerSize = htods(sizeof(node)); |
| 1231 | node.lineNumber = htodl(getStartLineNumber()); |
| 1232 | if (!stripComments) { |
| 1233 | node.comment.index = htodl( |
| 1234 | mComment.size() > 0 ? strings.offsetForString(mComment) : -1); |
| 1235 | //if (mComment.size() > 0) { |
| 1236 | // printf("Flattening comment: %s\n", String8(mComment).string()); |
| 1237 | //} |
| 1238 | } else { |
| 1239 | node.comment.index = htodl((uint32_t)-1); |
| 1240 | } |
| 1241 | if (type == TYPE_ELEMENT) { |
| 1242 | node.header.type = htods(RES_XML_START_ELEMENT_TYPE); |
| 1243 | extData = &attrExt; |
| 1244 | extSize = sizeof(attrExt); |
| 1245 | memset(&attrExt, 0, sizeof(attrExt)); |
| 1246 | if (mNamespaceUri.size() > 0) { |
| 1247 | attrExt.ns.index = htodl(strings.offsetForString(mNamespaceUri)); |
| 1248 | } else { |
| 1249 | attrExt.ns.index = htodl((uint32_t)-1); |
| 1250 | } |
| 1251 | attrExt.name.index = htodl(strings.offsetForString(mElementName)); |
| 1252 | attrExt.attributeStart = htods(sizeof(attrExt)); |
| 1253 | attrExt.attributeSize = htods(sizeof(attr)); |
| 1254 | attrExt.attributeCount = htods(NA); |
| 1255 | attrExt.idIndex = htods(0); |
| 1256 | attrExt.classIndex = htods(0); |
| 1257 | attrExt.styleIndex = htods(0); |
| 1258 | for (i=0; i<NA; i++) { |
| 1259 | ssize_t idx = mAttributeOrder.valueAt(i); |
| 1260 | const attribute_entry& ae = mAttributes.itemAt(idx); |
| 1261 | if (ae.ns.size() == 0) { |
| 1262 | if (ae.name == id16) { |
| 1263 | attrExt.idIndex = htods(i+1); |
| 1264 | } else if (ae.name == class16) { |
| 1265 | attrExt.classIndex = htods(i+1); |
| 1266 | } else if (ae.name == style16) { |
| 1267 | attrExt.styleIndex = htods(i+1); |
| 1268 | } |
| 1269 | } |
| 1270 | } |
| 1271 | } else if (type == TYPE_NAMESPACE) { |
| 1272 | node.header.type = htods(RES_XML_START_NAMESPACE_TYPE); |
| 1273 | extData = &namespaceExt; |
| 1274 | extSize = sizeof(namespaceExt); |
| 1275 | memset(&namespaceExt, 0, sizeof(namespaceExt)); |
| 1276 | if (mNamespacePrefix.size() > 0) { |
| 1277 | namespaceExt.prefix.index = htodl(strings.offsetForString(mNamespacePrefix)); |
| 1278 | } else { |
| 1279 | namespaceExt.prefix.index = htodl((uint32_t)-1); |
| 1280 | } |
| 1281 | namespaceExt.prefix.index = htodl(strings.offsetForString(mNamespacePrefix)); |
| 1282 | namespaceExt.uri.index = htodl(strings.offsetForString(mNamespaceUri)); |
| 1283 | LOG_ALWAYS_FATAL_IF(NA != 0, "Namespace nodes can't have attributes!"); |
| 1284 | } else if (type == TYPE_CDATA) { |
| 1285 | node.header.type = htods(RES_XML_CDATA_TYPE); |
| 1286 | extData = &cdataExt; |
| 1287 | extSize = sizeof(cdataExt); |
| 1288 | memset(&cdataExt, 0, sizeof(cdataExt)); |
| 1289 | cdataExt.data.index = htodl(strings.offsetForString(mChars)); |
| 1290 | cdataExt.typedData.size = htods(sizeof(cdataExt.typedData)); |
| 1291 | cdataExt.typedData.res0 = 0; |
| 1292 | cdataExt.typedData.dataType = mCharsValue.dataType; |
| 1293 | cdataExt.typedData.data = htodl(mCharsValue.data); |
| 1294 | LOG_ALWAYS_FATAL_IF(NA != 0, "CDATA nodes can't have attributes!"); |
| 1295 | } |
| 1296 | |
| 1297 | node.header.size = htodl(sizeof(node) + extSize + (sizeof(attr)*NA)); |
| 1298 | |
| 1299 | dest->writeData(&node, sizeof(node)); |
| 1300 | if (extSize > 0) { |
| 1301 | dest->writeData(extData, extSize); |
| 1302 | } |
| 1303 | |
| 1304 | for (i=0; i<NA; i++) { |
| 1305 | ssize_t idx = mAttributeOrder.valueAt(i); |
| 1306 | const attribute_entry& ae = mAttributes.itemAt(idx); |
| 1307 | if (ae.ns.size() > 0) { |
| 1308 | attr.ns.index = htodl(strings.offsetForString(ae.ns)); |
| 1309 | } else { |
| 1310 | attr.ns.index = htodl((uint32_t)-1); |
| 1311 | } |
| 1312 | attr.name.index = htodl(ae.namePoolIdx); |
| 1313 | |
| 1314 | if (!stripRawValues || ae.needStringValue()) { |
| 1315 | attr.rawValue.index = htodl(strings.offsetForString(ae.string)); |
| 1316 | } else { |
| 1317 | attr.rawValue.index = htodl((uint32_t)-1); |
| 1318 | } |
| 1319 | attr.typedValue.size = htods(sizeof(attr.typedValue)); |
| 1320 | if (ae.value.dataType == Res_value::TYPE_NULL |
| 1321 | || ae.value.dataType == Res_value::TYPE_STRING) { |
| 1322 | attr.typedValue.res0 = 0; |
| 1323 | attr.typedValue.dataType = Res_value::TYPE_STRING; |
| 1324 | attr.typedValue.data = htodl(strings.offsetForString(ae.string)); |
| 1325 | } else { |
| 1326 | attr.typedValue.res0 = 0; |
| 1327 | attr.typedValue.dataType = ae.value.dataType; |
| 1328 | attr.typedValue.data = htodl(ae.value.data); |
| 1329 | } |
| 1330 | dest->writeData(&attr, sizeof(attr)); |
| 1331 | } |
| 1332 | |
| 1333 | for (i=0; i<NC; i++) { |
| 1334 | status_t err = mChildren.itemAt(i)->flatten_node(strings, dest, |
| 1335 | stripComments, stripRawValues); |
| 1336 | if (err != NO_ERROR) { |
| 1337 | return err; |
| 1338 | } |
| 1339 | } |
| 1340 | |
| 1341 | if (type == TYPE_ELEMENT) { |
| 1342 | ResXMLTree_endElementExt endElementExt; |
| 1343 | memset(&endElementExt, 0, sizeof(endElementExt)); |
| 1344 | node.header.type = htods(RES_XML_END_ELEMENT_TYPE); |
| 1345 | node.header.size = htodl(sizeof(node)+sizeof(endElementExt)); |
| 1346 | node.lineNumber = htodl(getEndLineNumber()); |
| 1347 | node.comment.index = htodl((uint32_t)-1); |
| 1348 | endElementExt.ns.index = attrExt.ns.index; |
| 1349 | endElementExt.name.index = attrExt.name.index; |
| 1350 | dest->writeData(&node, sizeof(node)); |
| 1351 | dest->writeData(&endElementExt, sizeof(endElementExt)); |
| 1352 | } else if (type == TYPE_NAMESPACE) { |
| 1353 | node.header.type = htods(RES_XML_END_NAMESPACE_TYPE); |
| 1354 | node.lineNumber = htodl(getEndLineNumber()); |
| 1355 | node.comment.index = htodl((uint32_t)-1); |
| 1356 | node.header.size = htodl(sizeof(node)+extSize); |
| 1357 | dest->writeData(&node, sizeof(node)); |
| 1358 | dest->writeData(extData, extSize); |
| 1359 | } |
| 1360 | |
| 1361 | return NO_ERROR; |
| 1362 | } |