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