blob: d2cd2d64246c0e213f40181e0b31c1190abd6fd5 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001//
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 Bringertfb903a42013-03-18 21:17:26 +00009#include "pseudolocalize.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011#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 Gampe2412f842014-09-30 20:55:57 -070019// 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.
30static const bool kIsDebug = false;
31// Set to true for noisy debug output of parsing.
32static const bool kIsDebugParse = false;
33
34#if PRINT_STRING_METRICS
35static const bool kPrintStringMetrics = true;
36#else
37static const bool kPrintStringMetrics = false;
38#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039
40const char* const RESOURCES_ROOT_NAMESPACE = "http://schemas.android.com/apk/res/";
41const char* const RESOURCES_ANDROID_NAMESPACE = "http://schemas.android.com/apk/res/android";
Xavier Ducrohetd9fe8012012-02-23 16:59:27 -080042const char* const RESOURCES_AUTO_PACKAGE_NAMESPACE = "http://schemas.android.com/apk/res-auto";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043const char* const RESOURCES_ROOT_PRV_NAMESPACE = "http://schemas.android.com/apk/prv/res/";
44
45const char* const XLIFF_XMLNS = "urn:oasis:names:tc:xliff:document:1.2";
46const char* const ALLOWED_XLIFF_ELEMENTS[] = {
47 "bpt",
48 "ept",
49 "it",
50 "ph",
51 "g",
52 "bx",
53 "ex",
54 "x"
55 };
56
57bool isWhitespace(const char16_t* str)
58{
59 while (*str != 0 && *str < 128 && isspace(*str)) {
60 str++;
61 }
62 return *str == 0;
63}
64
65static const String16 RESOURCES_PREFIX(RESOURCES_ROOT_NAMESPACE);
inazaruke3489092011-05-22 15:09:06 -070066static const String16 RESOURCES_PREFIX_AUTO_PACKAGE(RESOURCES_AUTO_PACKAGE_NAMESPACE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067static const String16 RESOURCES_PRV_PREFIX(RESOURCES_ROOT_PRV_NAMESPACE);
Xavier Ducrohetf8aea992012-02-02 17:18:18 -080068static const String16 RESOURCES_TOOLS_NAMESPACE("http://schemas.android.com/tools");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069
inazaruke3489092011-05-22 15:09:06 -070070String16 getNamespaceResourcePackage(String16 appPackage, String16 namespaceUri, bool* outIsPublic)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071{
72 //printf("%s starts with %s?\n", String8(namespaceUri).string(),
73 // String8(RESOURCES_PREFIX).string());
74 size_t prefixSize;
75 bool isPublic = true;
inazaruke3489092011-05-22 15:09:06 -070076 if(namespaceUri.startsWith(RESOURCES_PREFIX_AUTO_PACKAGE)) {
Andreas Gampe2412f842014-09-30 20:55:57 -070077 if (kIsDebug) {
78 printf("Using default application package: %s -> %s\n", String8(namespaceUri).string(),
79 String8(appPackage).string());
80 }
inazaruke3489092011-05-22 15:09:06 -070081 isPublic = true;
82 return appPackage;
83 } else if (namespaceUri.startsWith(RESOURCES_PREFIX)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 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 Root15fe2cb2010-05-28 15:44:32 -070099status_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 Fischer98ee11d2010-08-13 14:49:55 -0700197 "did you mean to add the formatted=\"false\" attribute?\n");
Kenny Root15fe2cb2010-05-28 15:44:32 -0700198 return NOT_ENOUGH_DATA;
199 }
200
201 return NO_ERROR;
202}
203
Andreas Gampe2412f842014-09-30 20:55:57 -0700204status_t parseStyledString(Bundle* /* bundle */,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 const char* fileName,
206 ResXMLTree* inXml,
207 const String16& endTag,
208 String16* outString,
209 Vector<StringPool::entry_style_span>* outSpans,
Kenny Root15fe2cb2010-05-28 15:44:32 -0700210 bool isFormatted,
Anton Krumina2ef5c02014-03-12 14:46:44 -0700211 PseudolocalizationMethod pseudolocalize)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212{
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 Krumina2ef5c02014-03-12 14:46:44 -0700222 // Bracketing if pseudolocalization accented method specified.
223 if (pseudolocalize == PSEUDO_ACCENTED) {
224 curString.append(String16(String8("[")));
225 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 while ((code=inXml->next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 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 Krumina2ef5c02014-03-12 14:46:44 -0700233 pseudolocalize = NO_PSEUDOLOCALIZATION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 }
235 }
Anton Krumina2ef5c02014-03-12 14:46:44 -0700236 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 Project9066cfe2009-03-03 19:31:44 -0800246 } else {
Kenny Root15fe2cb2010-05-28 15:44:32 -0700247 if (isFormatted && hasSubstitutionErrors(fileName, inXml, text) != NO_ERROR) {
248 return UNKNOWN_ERROR;
249 } else {
250 curString.append(text);
251 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 }
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 Albertf348c152014-09-08 18:28:00 -0700258 const char16_t* ns = inXml->getElementNamespace(&nslen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 if (ns == NULL) {
Dan Albertf348c152014-09-08 18:28:00 -0700260 ns = (const char16_t*)"\0\0";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 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 }
280moveon:
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 Albertf348c152014-09-08 18:28:00 -0700315 const char16_t* ns = inXml->getElementNamespace(&nslen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 if (ns == NULL) {
Dan Albertf348c152014-09-08 18:28:00 -0700317 ns = (const char16_t*)"\0\0";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 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 Fischerc87d2522009-09-01 15:20:30 -0700369 /*
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 Nelissendd931862009-07-13 13:02:33 -0700375 fprintf(stderr, "%s:%d: warning: empty '%s' span found in text '%s'\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 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 Krumina2ef5c02014-03-12 14:46:44 -0700385 // 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 Project9066cfe2009-03-03 19:31:44 -0800404 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
430struct namespace_entry {
431 String8 prefix;
432 String8 uri;
433};
434
435static 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
445static String8 build_namespace(const Vector<namespace_entry>& namespaces,
Dan Albertf348c152014-09-08 18:28:00 -0700446 const char16_t* ns)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447{
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
464void printXMLBlock(ResXMLTree* block)
465{
466 block->restart();
467
468 Vector<namespace_entry> namespaces;
Vishwath Mohan9b754a12015-03-12 10:47:14 -0700469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 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 Albertf348c152014-09-08 18:28:00 -0700477 const char16_t* ns16 = block->getElementNamespace(&len);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 String8 elemNs = build_namespace(namespaces, ns16);
Dan Albertf348c152014-09-08 18:28:00 -0700479 const char16_t* com16 = block->getComment(&len);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 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 Shemesh9872bf42010-12-20 17:38:33 +0200510 ResTable::normalizeForOutput(String8(block->getAttributeStringValue(i,
511 &len)).string()).string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 } 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 Shemesh9872bf42010-12-20 17:38:33 +0200517 printf(" (Raw: \"%s\")", ResTable::normalizeForOutput(String8(val).string()).
518 string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 }
520 printf("\n");
521 }
522 } else if (code == ResXMLTree::END_TAG) {
Vishwath Mohan9b754a12015-03-12 10:47:14 -0700523 // Invalid tag nesting can be misused to break the parsing
524 // code below. Break if detected.
525 if (--depth < 0) {
526 printf("***BAD DEPTH in XMLBlock: %d\n", depth);
527 break;
528 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 } else if (code == ResXMLTree::START_NAMESPACE) {
530 namespace_entry ns;
531 size_t len;
Dan Albertf348c152014-09-08 18:28:00 -0700532 const char16_t* prefix16 = block->getNamespacePrefix(&len);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 if (prefix16) {
534 ns.prefix = String8(prefix16);
535 } else {
536 ns.prefix = "<DEF>";
537 }
538 ns.uri = String8(block->getNamespaceUri(&len));
539 namespaces.push(ns);
540 printf("%sN: %s=%s\n", prefix.string(), ns.prefix.string(),
541 ns.uri.string());
542 depth++;
543 } else if (code == ResXMLTree::END_NAMESPACE) {
Vishwath Mohan9b754a12015-03-12 10:47:14 -0700544 if (--depth < 0) {
545 printf("***BAD DEPTH in XMLBlock: %d\n", depth);
546 break;
547 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 const namespace_entry& ns = namespaces.top();
549 size_t len;
Dan Albertf348c152014-09-08 18:28:00 -0700550 const char16_t* prefix16 = block->getNamespacePrefix(&len);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 String8 pr;
552 if (prefix16) {
553 pr = String8(prefix16);
554 } else {
555 pr = "<DEF>";
556 }
557 if (ns.prefix != pr) {
558 prefix = make_prefix(depth);
559 printf("%s*** BAD END NS PREFIX: found=%s, expected=%s\n",
560 prefix.string(), pr.string(), ns.prefix.string());
561 }
562 String8 uri = String8(block->getNamespaceUri(&len));
563 if (ns.uri != uri) {
564 prefix = make_prefix(depth);
565 printf("%s *** BAD END NS URI: found=%s, expected=%s\n",
566 prefix.string(), uri.string(), ns.uri.string());
567 }
568 namespaces.pop();
569 } else if (code == ResXMLTree::TEXT) {
570 size_t len;
Shachar Shemesh429dad62012-07-08 06:37:48 +0300571 printf("%sC: \"%s\"\n", prefix.string(),
572 ResTable::normalizeForOutput(String8(block->getText(&len)).string()).string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 }
574 }
575
576 block->restart();
577}
578
579status_t parseXMLResource(const sp<AaptFile>& file, ResXMLTree* outTree,
580 bool stripAll, bool keepComments,
581 const char** cDataTags)
582{
583 sp<XMLNode> root = XMLNode::parse(file);
584 if (root == NULL) {
585 return UNKNOWN_ERROR;
586 }
587 root->removeWhitespace(stripAll, cDataTags);
588
Andreas Gampe2412f842014-09-30 20:55:57 -0700589 if (kIsDebug) {
590 printf("Input XML from %s:\n", (const char*)file->getPrintableSource());
591 root->print();
592 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 sp<AaptFile> rsc = new AaptFile(String8(), AaptGroupEntry(), String8());
594 status_t err = root->flatten(rsc, !keepComments, false);
595 if (err != NO_ERROR) {
596 return err;
597 }
598 err = outTree->setTo(rsc->getData(), rsc->getSize(), true);
599 if (err != NO_ERROR) {
600 return err;
601 }
602
Andreas Gampe2412f842014-09-30 20:55:57 -0700603 if (kIsDebug) {
604 printf("Output XML:\n");
605 printXMLBlock(outTree);
606 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607
608 return NO_ERROR;
609}
610
611sp<XMLNode> XMLNode::parse(const sp<AaptFile>& file)
612{
613 char buf[16384];
614 int fd = open(file->getSourceFile().string(), O_RDONLY | O_BINARY);
615 if (fd < 0) {
616 SourcePos(file->getSourceFile(), -1).error("Unable to open file for read: %s",
617 strerror(errno));
618 return NULL;
619 }
620
621 XML_Parser parser = XML_ParserCreateNS(NULL, 1);
622 ParseState state;
623 state.filename = file->getPrintableSource();
624 state.parser = parser;
625 XML_SetUserData(parser, &state);
626 XML_SetElementHandler(parser, startElement, endElement);
627 XML_SetNamespaceDeclHandler(parser, startNamespace, endNamespace);
628 XML_SetCharacterDataHandler(parser, characterData);
629 XML_SetCommentHandler(parser, commentData);
630
631 ssize_t len;
632 bool done;
633 do {
634 len = read(fd, buf, sizeof(buf));
635 done = len < (ssize_t)sizeof(buf);
636 if (len < 0) {
637 SourcePos(file->getSourceFile(), -1).error("Error reading file: %s\n", strerror(errno));
638 close(fd);
639 return NULL;
640 }
641 if (XML_Parse(parser, buf, len, done) == XML_STATUS_ERROR) {
642 SourcePos(file->getSourceFile(), (int)XML_GetCurrentLineNumber(parser)).error(
643 "Error parsing XML: %s\n", XML_ErrorString(XML_GetErrorCode(parser)));
644 close(fd);
645 return NULL;
646 }
647 } while (!done);
648
649 XML_ParserFree(parser);
650 if (state.root == NULL) {
651 SourcePos(file->getSourceFile(), -1).error("No XML data generated when parsing");
652 }
653 close(fd);
654 return state.root;
655}
656
Adam Lesinskie572c012014-09-19 15:10:04 -0700657XMLNode::XMLNode()
658 : mNextAttributeIndex(0x80000000)
659 , mStartLineNumber(0)
660 , mEndLineNumber(0)
661 , mUTF8(false) {}
662
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800663XMLNode::XMLNode(const String8& filename, const String16& s1, const String16& s2, bool isNamespace)
664 : mNextAttributeIndex(0x80000000)
665 , mFilename(filename)
666 , mStartLineNumber(0)
667 , mEndLineNumber(0)
Kenny Root19138462009-12-04 09:38:48 -0800668 , mUTF8(false)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669{
670 if (isNamespace) {
671 mNamespacePrefix = s1;
672 mNamespaceUri = s2;
673 } else {
674 mNamespaceUri = s1;
675 mElementName = s2;
676 }
677}
678
679XMLNode::XMLNode(const String8& filename)
680 : mFilename(filename)
681{
Marco Nelissen6a1fade2009-04-20 16:16:01 -0700682 memset(&mCharsValue, 0, sizeof(mCharsValue));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683}
684
685XMLNode::type XMLNode::getType() const
686{
687 if (mElementName.size() != 0) {
688 return TYPE_ELEMENT;
689 }
690 if (mNamespaceUri.size() != 0) {
691 return TYPE_NAMESPACE;
692 }
693 return TYPE_CDATA;
694}
695
696const String16& XMLNode::getNamespacePrefix() const
697{
698 return mNamespacePrefix;
699}
700
701const String16& XMLNode::getNamespaceUri() const
702{
703 return mNamespaceUri;
704}
705
706const String16& XMLNode::getElementNamespace() const
707{
708 return mNamespaceUri;
709}
710
711const String16& XMLNode::getElementName() const
712{
713 return mElementName;
714}
715
716const Vector<sp<XMLNode> >& XMLNode::getChildren() const
717{
718 return mChildren;
719}
720
Dianne Hackborn62da8462009-05-13 15:06:13 -0700721const String8& XMLNode::getFilename() const
722{
723 return mFilename;
724}
Vishwath Mohan9b754a12015-03-12 10:47:14 -0700725
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726const Vector<XMLNode::attribute_entry>&
727 XMLNode::getAttributes() const
728{
729 return mAttributes;
730}
731
Dianne Hackborn62da8462009-05-13 15:06:13 -0700732const XMLNode::attribute_entry* XMLNode::getAttribute(const String16& ns,
733 const String16& name) const
734{
735 for (size_t i=0; i<mAttributes.size(); i++) {
736 const attribute_entry& ae(mAttributes.itemAt(i));
737 if (ae.ns == ns && ae.name == name) {
738 return &ae;
739 }
740 }
Vishwath Mohan9b754a12015-03-12 10:47:14 -0700741
Dianne Hackborn62da8462009-05-13 15:06:13 -0700742 return NULL;
743}
744
Jeff Hamilton2fee0ed2010-01-06 15:46:38 -0600745XMLNode::attribute_entry* XMLNode::editAttribute(const String16& ns,
746 const String16& name)
747{
748 for (size_t i=0; i<mAttributes.size(); i++) {
749 attribute_entry * ae = &mAttributes.editItemAt(i);
750 if (ae->ns == ns && ae->name == name) {
751 return ae;
752 }
753 }
754
755 return NULL;
756}
757
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758const String16& XMLNode::getCData() const
759{
760 return mChars;
761}
762
763const String16& XMLNode::getComment() const
764{
765 return mComment;
766}
767
768int32_t XMLNode::getStartLineNumber() const
769{
770 return mStartLineNumber;
771}
772
773int32_t XMLNode::getEndLineNumber() const
774{
775 return mEndLineNumber;
776}
777
Dianne Hackborn62da8462009-05-13 15:06:13 -0700778sp<XMLNode> XMLNode::searchElement(const String16& tagNamespace, const String16& tagName)
779{
780 if (getType() == XMLNode::TYPE_ELEMENT
781 && mNamespaceUri == tagNamespace
782 && mElementName == tagName) {
783 return this;
784 }
Vishwath Mohan9b754a12015-03-12 10:47:14 -0700785
Dianne Hackborn62da8462009-05-13 15:06:13 -0700786 for (size_t i=0; i<mChildren.size(); i++) {
787 sp<XMLNode> found = mChildren.itemAt(i)->searchElement(tagNamespace, tagName);
788 if (found != NULL) {
789 return found;
790 }
791 }
Vishwath Mohan9b754a12015-03-12 10:47:14 -0700792
Dianne Hackborn62da8462009-05-13 15:06:13 -0700793 return NULL;
794}
795
796sp<XMLNode> XMLNode::getChildElement(const String16& tagNamespace, const String16& tagName)
797{
798 for (size_t i=0; i<mChildren.size(); i++) {
799 sp<XMLNode> child = mChildren.itemAt(i);
800 if (child->getType() == XMLNode::TYPE_ELEMENT
801 && child->mNamespaceUri == tagNamespace
802 && child->mElementName == tagName) {
803 return child;
804 }
805 }
Vishwath Mohan9b754a12015-03-12 10:47:14 -0700806
Dianne Hackborn62da8462009-05-13 15:06:13 -0700807 return NULL;
808}
809
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810status_t XMLNode::addChild(const sp<XMLNode>& child)
811{
812 if (getType() == TYPE_CDATA) {
813 SourcePos(mFilename, child->getStartLineNumber()).error("Child to CDATA node.");
814 return UNKNOWN_ERROR;
815 }
816 //printf("Adding child %p to parent %p\n", child.get(), this);
817 mChildren.add(child);
818 return NO_ERROR;
819}
820
Dianne Hackborn62da8462009-05-13 15:06:13 -0700821status_t XMLNode::insertChildAt(const sp<XMLNode>& child, size_t index)
822{
823 if (getType() == TYPE_CDATA) {
824 SourcePos(mFilename, child->getStartLineNumber()).error("Child to CDATA node.");
825 return UNKNOWN_ERROR;
826 }
827 //printf("Adding child %p to parent %p\n", child.get(), this);
828 mChildren.insertAt(child, index);
829 return NO_ERROR;
830}
831
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800832status_t XMLNode::addAttribute(const String16& ns, const String16& name,
833 const String16& value)
834{
835 if (getType() == TYPE_CDATA) {
836 SourcePos(mFilename, getStartLineNumber()).error("Child to CDATA node.");
837 return UNKNOWN_ERROR;
838 }
Xavier Ducrohetf8aea992012-02-02 17:18:18 -0800839
840 if (ns != RESOURCES_TOOLS_NAMESPACE) {
841 attribute_entry e;
842 e.index = mNextAttributeIndex++;
843 e.ns = ns;
844 e.name = name;
845 e.string = value;
846 mAttributes.add(e);
847 mAttributeOrder.add(e.index, mAttributes.size()-1);
848 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 return NO_ERROR;
850}
851
Adam Lesinskie572c012014-09-19 15:10:04 -0700852status_t XMLNode::removeAttribute(size_t index)
853{
854 if (getType() == TYPE_CDATA) {
855 return UNKNOWN_ERROR;
856 }
857
858 if (index >= mAttributes.size()) {
859 return UNKNOWN_ERROR;
860 }
861
862 const attribute_entry& e = mAttributes[index];
863 const uint32_t key = e.nameResId ? e.nameResId : e.index;
864 mAttributeOrder.removeItem(key);
865 mAttributes.removeAt(index);
866
867 // Shift all the indices.
868 const size_t attrCount = mAttributeOrder.size();
869 for (size_t i = 0; i < attrCount; i++) {
870 size_t attrIdx = mAttributeOrder[i];
871 if (attrIdx > index) {
872 mAttributeOrder.replaceValueAt(i, attrIdx - 1);
873 }
874 }
875 return NO_ERROR;
876}
877
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878void XMLNode::setAttributeResID(size_t attrIdx, uint32_t resId)
879{
880 attribute_entry& e = mAttributes.editItemAt(attrIdx);
881 if (e.nameResId) {
882 mAttributeOrder.removeItem(e.nameResId);
883 } else {
884 mAttributeOrder.removeItem(e.index);
885 }
Andreas Gampe2412f842014-09-30 20:55:57 -0700886 if (kIsDebug) {
887 printf("Elem %s %s=\"%s\": set res id = 0x%08x\n",
888 String8(getElementName()).string(),
889 String8(mAttributes.itemAt(attrIdx).name).string(),
890 String8(mAttributes.itemAt(attrIdx).string).string(),
891 resId);
892 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 mAttributes.editItemAt(attrIdx).nameResId = resId;
894 mAttributeOrder.add(resId, attrIdx);
895}
896
897status_t XMLNode::appendChars(const String16& chars)
898{
899 if (getType() != TYPE_CDATA) {
900 SourcePos(mFilename, getStartLineNumber()).error("Adding characters to element node.");
901 return UNKNOWN_ERROR;
902 }
903 mChars.append(chars);
904 return NO_ERROR;
905}
906
907status_t XMLNode::appendComment(const String16& comment)
908{
909 if (mComment.size() > 0) {
910 mComment.append(String16("\n"));
911 }
912 mComment.append(comment);
913 return NO_ERROR;
914}
915
916void XMLNode::setStartLineNumber(int32_t line)
917{
918 mStartLineNumber = line;
919}
920
921void XMLNode::setEndLineNumber(int32_t line)
922{
923 mEndLineNumber = line;
924}
925
926void XMLNode::removeWhitespace(bool stripAll, const char** cDataTags)
927{
928 //printf("Removing whitespace in %s\n", String8(mElementName).string());
929 size_t N = mChildren.size();
930 if (cDataTags) {
931 String8 tag(mElementName);
932 const char** p = cDataTags;
933 while (*p) {
934 if (tag == *p) {
935 stripAll = false;
936 break;
937 }
938 }
939 }
940 for (size_t i=0; i<N; i++) {
941 sp<XMLNode> node = mChildren.itemAt(i);
942 if (node->getType() == TYPE_CDATA) {
943 // This is a CDATA node...
944 const char16_t* p = node->mChars.string();
945 while (*p != 0 && *p < 128 && isspace(*p)) {
946 p++;
947 }
948 //printf("Space ends at %d in \"%s\"\n",
949 // (int)(p-node->mChars.string()),
950 // String8(node->mChars).string());
951 if (*p == 0) {
952 if (stripAll) {
953 // Remove this node!
954 mChildren.removeAt(i);
955 N--;
956 i--;
957 } else {
958 node->mChars = String16(" ");
959 }
960 } else {
961 // Compact leading/trailing whitespace.
962 const char16_t* e = node->mChars.string()+node->mChars.size()-1;
963 while (e > p && *e < 128 && isspace(*e)) {
964 e--;
965 }
966 if (p > node->mChars.string()) {
967 p--;
968 }
969 if (e < (node->mChars.string()+node->mChars.size()-1)) {
970 e++;
971 }
972 if (p > node->mChars.string() ||
973 e < (node->mChars.string()+node->mChars.size()-1)) {
974 String16 tmp(p, e-p+1);
975 node->mChars = tmp;
976 }
977 }
978 } else {
979 node->removeWhitespace(stripAll, cDataTags);
980 }
981 }
982}
983
984status_t XMLNode::parseValues(const sp<AaptAssets>& assets,
985 ResourceTable* table)
986{
987 bool hasErrors = false;
Vishwath Mohan9b754a12015-03-12 10:47:14 -0700988
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800989 if (getType() == TYPE_ELEMENT) {
990 const size_t N = mAttributes.size();
991 String16 defPackage(assets->getPackage());
992 for (size_t i=0; i<N; i++) {
993 attribute_entry& e = mAttributes.editItemAt(i);
994 AccessorCookie ac(SourcePos(mFilename, getStartLineNumber()), String8(e.name),
995 String8(e.string));
996 table->setCurrentXmlPos(SourcePos(mFilename, getStartLineNumber()));
997 if (!assets->getIncludedResources()
998 .stringToValue(&e.value, &e.string,
999 e.string.string(), e.string.size(), true, true,
1000 e.nameResId, NULL, &defPackage, table, &ac)) {
1001 hasErrors = true;
1002 }
Andreas Gampe2412f842014-09-30 20:55:57 -07001003 if (kIsDebug) {
1004 printf("Attr %s: type=0x%x, str=%s\n",
1005 String8(e.name).string(), e.value.dataType,
1006 String8(e.string).string());
1007 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 }
1009 }
1010 const size_t N = mChildren.size();
1011 for (size_t i=0; i<N; i++) {
1012 status_t err = mChildren.itemAt(i)->parseValues(assets, table);
1013 if (err != NO_ERROR) {
1014 hasErrors = true;
1015 }
1016 }
Andreas Gampe2412f842014-09-30 20:55:57 -07001017 return hasErrors ? STATUST(UNKNOWN_ERROR) : NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018}
1019
1020status_t XMLNode::assignResourceIds(const sp<AaptAssets>& assets,
1021 const ResourceTable* table)
1022{
1023 bool hasErrors = false;
Vishwath Mohan9b754a12015-03-12 10:47:14 -07001024
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 if (getType() == TYPE_ELEMENT) {
1026 String16 attr("attr");
1027 const char* errorMsg;
1028 const size_t N = mAttributes.size();
1029 for (size_t i=0; i<N; i++) {
1030 const attribute_entry& e = mAttributes.itemAt(i);
1031 if (e.ns.size() <= 0) continue;
Andreas Gampeb8dc7bc2014-10-01 19:07:51 -07001032 bool nsIsPublic = true;
inazaruke3489092011-05-22 15:09:06 -07001033 String16 pkg(getNamespaceResourcePackage(String16(assets->getPackage()), e.ns, &nsIsPublic));
Andreas Gampe2412f842014-09-30 20:55:57 -07001034 if (kIsDebug) {
1035 printf("Elem %s %s=\"%s\": namespace(%s) %s ===> %s\n",
1036 String8(getElementName()).string(),
1037 String8(e.name).string(),
1038 String8(e.string).string(),
1039 String8(e.ns).string(),
1040 (nsIsPublic) ? "public" : "private",
1041 String8(pkg).string());
1042 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001043 if (pkg.size() <= 0) continue;
1044 uint32_t res = table != NULL
1045 ? table->getResId(e.name, &attr, &pkg, &errorMsg, nsIsPublic)
1046 : assets->getIncludedResources().
1047 identifierForName(e.name.string(), e.name.size(),
1048 attr.string(), attr.size(),
1049 pkg.string(), pkg.size());
1050 if (res != 0) {
Andreas Gampe2412f842014-09-30 20:55:57 -07001051 if (kIsDebug) {
1052 printf("XML attribute name %s: resid=0x%08x\n",
1053 String8(e.name).string(), res);
1054 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 setAttributeResID(i, res);
1056 } else {
1057 SourcePos(mFilename, getStartLineNumber()).error(
1058 "No resource identifier found for attribute '%s' in package '%s'\n",
1059 String8(e.name).string(), String8(pkg).string());
1060 hasErrors = true;
1061 }
1062 }
1063 }
1064 const size_t N = mChildren.size();
1065 for (size_t i=0; i<N; i++) {
1066 status_t err = mChildren.itemAt(i)->assignResourceIds(assets, table);
1067 if (err < NO_ERROR) {
1068 hasErrors = true;
1069 }
1070 }
1071
Andreas Gampe2412f842014-09-30 20:55:57 -07001072 return hasErrors ? STATUST(UNKNOWN_ERROR) : NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073}
1074
Adam Lesinskie572c012014-09-19 15:10:04 -07001075sp<XMLNode> XMLNode::clone() const {
1076 sp<XMLNode> copy = new XMLNode();
1077 copy->mNamespacePrefix = mNamespacePrefix;
1078 copy->mNamespaceUri = mNamespaceUri;
1079 copy->mElementName = mElementName;
1080
1081 const size_t childCount = mChildren.size();
1082 for (size_t i = 0; i < childCount; i++) {
1083 copy->mChildren.add(mChildren[i]->clone());
1084 }
1085
1086 copy->mAttributes = mAttributes;
1087 copy->mAttributeOrder = mAttributeOrder;
1088 copy->mNextAttributeIndex = mNextAttributeIndex;
1089 copy->mChars = mChars;
1090 memcpy(&copy->mCharsValue, &mCharsValue, sizeof(mCharsValue));
1091 copy->mComment = mComment;
1092 copy->mFilename = mFilename;
1093 copy->mStartLineNumber = mStartLineNumber;
1094 copy->mEndLineNumber = mEndLineNumber;
1095 copy->mUTF8 = mUTF8;
1096 return copy;
1097}
1098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099status_t XMLNode::flatten(const sp<AaptFile>& dest,
1100 bool stripComments, bool stripRawValues) const
1101{
Jeff Brown345b7eb2012-03-16 15:25:17 -07001102 StringPool strings(mUTF8);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 Vector<uint32_t> resids;
Vishwath Mohan9b754a12015-03-12 10:47:14 -07001104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 // First collect just the strings for attribute names that have a
1106 // resource ID assigned to them. This ensures that the resource ID
1107 // array is compact, and makes it easier to deal with attribute names
1108 // in different namespaces (and thus with different resource IDs).
1109 collect_resid_strings(&strings, &resids);
1110
1111 // Next collect all remainibng strings.
1112 collect_strings(&strings, &resids, stripComments, stripRawValues);
1113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114 sp<AaptFile> stringPool = strings.createStringBlock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115
1116 ResXMLTree_header header;
1117 memset(&header, 0, sizeof(header));
1118 header.header.type = htods(RES_XML_TYPE);
1119 header.header.headerSize = htods(sizeof(header));
1120
1121 const size_t basePos = dest->getSize();
1122 dest->writeData(&header, sizeof(header));
1123 dest->writeData(stringPool->getData(), stringPool->getSize());
1124
1125 // If we have resource IDs, write them.
1126 if (resids.size() > 0) {
1127 const size_t resIdsPos = dest->getSize();
1128 const size_t resIdsSize =
1129 sizeof(ResChunk_header)+(sizeof(uint32_t)*resids.size());
1130 ResChunk_header* idsHeader = (ResChunk_header*)
1131 (((const uint8_t*)dest->editData(resIdsPos+resIdsSize))+resIdsPos);
1132 idsHeader->type = htods(RES_XML_RESOURCE_MAP_TYPE);
1133 idsHeader->headerSize = htods(sizeof(*idsHeader));
1134 idsHeader->size = htodl(resIdsSize);
1135 uint32_t* ids = (uint32_t*)(idsHeader+1);
1136 for (size_t i=0; i<resids.size(); i++) {
1137 *ids++ = htodl(resids[i]);
1138 }
1139 }
1140
1141 flatten_node(strings, dest, stripComments, stripRawValues);
1142
1143 void* data = dest->editData();
1144 ResXMLTree_header* hd = (ResXMLTree_header*)(((uint8_t*)data)+basePos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 hd->header.size = htodl(dest->getSize()-basePos);
1146
Andreas Gampe2412f842014-09-30 20:55:57 -07001147 if (kPrintStringMetrics) {
1148 fprintf(stderr, "**** total xml size: %zu / %zu%% strings (in %s)\n",
1149 dest->getSize(), (stringPool->getSize()*100)/dest->getSize(),
1150 dest->getPath().string());
1151 }
Vishwath Mohan9b754a12015-03-12 10:47:14 -07001152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 return NO_ERROR;
1154}
1155
1156void XMLNode::print(int indent)
1157{
1158 String8 prefix;
1159 int i;
1160 for (i=0; i<indent; i++) {
1161 prefix.append(" ");
1162 }
1163 if (getType() == TYPE_ELEMENT) {
1164 String8 elemNs(getNamespaceUri());
1165 if (elemNs.size() > 0) {
1166 elemNs.append(":");
1167 }
1168 printf("%s E: %s%s", prefix.string(),
1169 elemNs.string(), String8(getElementName()).string());
1170 int N = mAttributes.size();
1171 for (i=0; i<N; i++) {
1172 ssize_t idx = mAttributeOrder.valueAt(i);
1173 if (i == 0) {
1174 printf(" / ");
1175 } else {
1176 printf(", ");
1177 }
1178 const attribute_entry& attr = mAttributes.itemAt(idx);
1179 String8 attrNs(attr.ns);
1180 if (attrNs.size() > 0) {
1181 attrNs.append(":");
1182 }
1183 if (attr.nameResId) {
1184 printf("%s%s(0x%08x)", attrNs.string(),
1185 String8(attr.name).string(), attr.nameResId);
1186 } else {
1187 printf("%s%s", attrNs.string(), String8(attr.name).string());
1188 }
1189 printf("=%s", String8(attr.string).string());
1190 }
1191 printf("\n");
1192 } else if (getType() == TYPE_NAMESPACE) {
1193 printf("%s N: %s=%s\n", prefix.string(),
1194 getNamespacePrefix().size() > 0
1195 ? String8(getNamespacePrefix()).string() : "<DEF>",
1196 String8(getNamespaceUri()).string());
1197 } else {
1198 printf("%s C: \"%s\"\n", prefix.string(), String8(getCData()).string());
1199 }
1200 int N = mChildren.size();
1201 for (i=0; i<N; i++) {
1202 mChildren.itemAt(i)->print(indent+1);
1203 }
1204}
1205
1206static void splitName(const char* name, String16* outNs, String16* outName)
1207{
1208 const char* p = name;
1209 while (*p != 0 && *p != 1) {
1210 p++;
1211 }
1212 if (*p == 0) {
1213 *outNs = String16();
1214 *outName = String16(name);
1215 } else {
1216 *outNs = String16(name, (p-name));
1217 *outName = String16(p+1);
1218 }
1219}
1220
1221void XMLCALL
1222XMLNode::startNamespace(void *userData, const char *prefix, const char *uri)
1223{
Andreas Gampe2412f842014-09-30 20:55:57 -07001224 if (kIsDebugParse) {
1225 printf("Start Namespace: %s %s\n", prefix, uri);
1226 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 ParseState* st = (ParseState*)userData;
Vishwath Mohan9b754a12015-03-12 10:47:14 -07001228 sp<XMLNode> node = XMLNode::newNamespace(st->filename,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 String16(prefix != NULL ? prefix : ""), String16(uri));
1230 node->setStartLineNumber(XML_GetCurrentLineNumber(st->parser));
1231 if (st->stack.size() > 0) {
1232 st->stack.itemAt(st->stack.size()-1)->addChild(node);
1233 } else {
1234 st->root = node;
1235 }
1236 st->stack.push(node);
1237}
1238
1239void XMLCALL
1240XMLNode::startElement(void *userData, const char *name, const char **atts)
1241{
Andreas Gampe2412f842014-09-30 20:55:57 -07001242 if (kIsDebugParse) {
1243 printf("Start Element: %s\n", name);
1244 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 ParseState* st = (ParseState*)userData;
1246 String16 ns16, name16;
1247 splitName(name, &ns16, &name16);
1248 sp<XMLNode> node = XMLNode::newElement(st->filename, ns16, name16);
1249 node->setStartLineNumber(XML_GetCurrentLineNumber(st->parser));
1250 if (st->pendingComment.size() > 0) {
1251 node->appendComment(st->pendingComment);
1252 st->pendingComment = String16();
1253 }
1254 if (st->stack.size() > 0) {
1255 st->stack.itemAt(st->stack.size()-1)->addChild(node);
1256 } else {
1257 st->root = node;
1258 }
1259 st->stack.push(node);
1260
1261 for (int i = 0; atts[i]; i += 2) {
1262 splitName(atts[i], &ns16, &name16);
1263 node->addAttribute(ns16, name16, String16(atts[i+1]));
1264 }
1265}
1266
1267void XMLCALL
1268XMLNode::characterData(void *userData, const XML_Char *s, int len)
1269{
Andreas Gampe2412f842014-09-30 20:55:57 -07001270 if (kIsDebugParse) {
1271 printf("CDATA: \"%s\"\n", String8(s, len).string());
1272 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 ParseState* st = (ParseState*)userData;
1274 sp<XMLNode> node = NULL;
1275 if (st->stack.size() == 0) {
1276 return;
1277 }
1278 sp<XMLNode> parent = st->stack.itemAt(st->stack.size()-1);
1279 if (parent != NULL && parent->getChildren().size() > 0) {
1280 node = parent->getChildren()[parent->getChildren().size()-1];
1281 if (node->getType() != TYPE_CDATA) {
1282 // Last node is not CDATA, need to make a new node.
1283 node = NULL;
1284 }
1285 }
1286
1287 if (node == NULL) {
1288 node = XMLNode::newCData(st->filename);
1289 node->setStartLineNumber(XML_GetCurrentLineNumber(st->parser));
1290 parent->addChild(node);
1291 }
1292
1293 node->appendChars(String16(s, len));
1294}
1295
1296void XMLCALL
1297XMLNode::endElement(void *userData, const char *name)
1298{
Andreas Gampe2412f842014-09-30 20:55:57 -07001299 if (kIsDebugParse) {
1300 printf("End Element: %s\n", name);
1301 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001302 ParseState* st = (ParseState*)userData;
1303 sp<XMLNode> node = st->stack.itemAt(st->stack.size()-1);
1304 node->setEndLineNumber(XML_GetCurrentLineNumber(st->parser));
1305 if (st->pendingComment.size() > 0) {
1306 node->appendComment(st->pendingComment);
1307 st->pendingComment = String16();
1308 }
1309 String16 ns16, name16;
1310 splitName(name, &ns16, &name16);
1311 LOG_ALWAYS_FATAL_IF(node->getElementNamespace() != ns16
1312 || node->getElementName() != name16,
1313 "Bad end element %s", name);
1314 st->stack.pop();
1315}
1316
1317void XMLCALL
1318XMLNode::endNamespace(void *userData, const char *prefix)
1319{
1320 const char* nonNullPrefix = prefix != NULL ? prefix : "";
Andreas Gampe2412f842014-09-30 20:55:57 -07001321 if (kIsDebugParse) {
1322 printf("End Namespace: %s\n", prefix);
1323 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324 ParseState* st = (ParseState*)userData;
1325 sp<XMLNode> node = st->stack.itemAt(st->stack.size()-1);
1326 node->setEndLineNumber(XML_GetCurrentLineNumber(st->parser));
1327 LOG_ALWAYS_FATAL_IF(node->getNamespacePrefix() != String16(nonNullPrefix),
1328 "Bad end namespace %s", prefix);
1329 st->stack.pop();
1330}
1331
1332void XMLCALL
1333XMLNode::commentData(void *userData, const char *comment)
1334{
Andreas Gampe2412f842014-09-30 20:55:57 -07001335 if (kIsDebugParse) {
1336 printf("Comment: %s\n", comment);
1337 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 ParseState* st = (ParseState*)userData;
1339 if (st->pendingComment.size() > 0) {
1340 st->pendingComment.append(String16("\n"));
1341 }
1342 st->pendingComment.append(String16(comment));
1343}
1344
1345status_t XMLNode::collect_strings(StringPool* dest, Vector<uint32_t>* outResIds,
1346 bool stripComments, bool stripRawValues) const
1347{
1348 collect_attr_strings(dest, outResIds, true);
Vishwath Mohan9b754a12015-03-12 10:47:14 -07001349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 int i;
Xavier Ducrohetf8aea992012-02-02 17:18:18 -08001351 if (RESOURCES_TOOLS_NAMESPACE != mNamespaceUri) {
1352 if (mNamespacePrefix.size() > 0) {
1353 dest->add(mNamespacePrefix, true);
1354 }
1355 if (mNamespaceUri.size() > 0) {
1356 dest->add(mNamespaceUri, true);
1357 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001358 }
1359 if (mElementName.size() > 0) {
1360 dest->add(mElementName, true);
1361 }
1362
1363 if (!stripComments && mComment.size() > 0) {
1364 dest->add(mComment, true);
1365 }
1366
1367 const int NA = mAttributes.size();
1368
1369 for (i=0; i<NA; i++) {
1370 const attribute_entry& ae = mAttributes.itemAt(i);
1371 if (ae.ns.size() > 0) {
1372 dest->add(ae.ns, true);
1373 }
1374 if (!stripRawValues || ae.needStringValue()) {
1375 dest->add(ae.string, true);
1376 }
1377 /*
1378 if (ae.value.dataType == Res_value::TYPE_NULL
1379 || ae.value.dataType == Res_value::TYPE_STRING) {
1380 dest->add(ae.string, true);
1381 }
1382 */
1383 }
1384
1385 if (mElementName.size() == 0) {
1386 // If not an element, include the CDATA, even if it is empty.
1387 dest->add(mChars, true);
1388 }
1389
1390 const int NC = mChildren.size();
1391
1392 for (i=0; i<NC; i++) {
1393 mChildren.itemAt(i)->collect_strings(dest, outResIds,
1394 stripComments, stripRawValues);
1395 }
1396
1397 return NO_ERROR;
1398}
1399
1400status_t XMLNode::collect_attr_strings(StringPool* outPool,
1401 Vector<uint32_t>* outResIds, bool allAttrs) const {
1402 const int NA = mAttributes.size();
1403
1404 for (int i=0; i<NA; i++) {
1405 const attribute_entry& attr = mAttributes.itemAt(i);
1406 uint32_t id = attr.nameResId;
1407 if (id || allAttrs) {
1408 // See if we have already assigned this resource ID to a pooled
1409 // string...
1410 const Vector<size_t>* indices = outPool->offsetsForString(attr.name);
1411 ssize_t idx = -1;
1412 if (indices != NULL) {
1413 const int NJ = indices->size();
1414 const size_t NR = outResIds->size();
1415 for (int j=0; j<NJ; j++) {
1416 size_t strIdx = indices->itemAt(j);
1417 if (strIdx >= NR) {
1418 if (id == 0) {
1419 // We don't need to assign a resource ID for this one.
1420 idx = strIdx;
1421 break;
1422 }
1423 // Just ignore strings that are out of range of
1424 // the currently assigned resource IDs... we add
1425 // strings as we assign the first ID.
1426 } else if (outResIds->itemAt(strIdx) == id) {
1427 idx = strIdx;
1428 break;
1429 }
1430 }
1431 }
1432 if (idx < 0) {
1433 idx = outPool->add(attr.name);
Andreas Gampe2412f842014-09-30 20:55:57 -07001434 if (kIsDebug) {
1435 printf("Adding attr %s (resid 0x%08x) to pool: idx=%zd\n",
1436 String8(attr.name).string(), id, SSIZE(idx));
1437 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001438 if (id != 0) {
1439 while ((ssize_t)outResIds->size() <= idx) {
1440 outResIds->add(0);
1441 }
1442 outResIds->replaceAt(id, idx);
1443 }
1444 }
1445 attr.namePoolIdx = idx;
Andreas Gampe2412f842014-09-30 20:55:57 -07001446 if (kIsDebug) {
1447 printf("String %s offset=0x%08zd\n", String8(attr.name).string(), SSIZE(idx));
1448 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001449 }
1450 }
1451
1452 return NO_ERROR;
1453}
1454
1455status_t XMLNode::collect_resid_strings(StringPool* outPool,
1456 Vector<uint32_t>* outResIds) const
1457{
1458 collect_attr_strings(outPool, outResIds, false);
1459
1460 const int NC = mChildren.size();
1461
1462 for (int i=0; i<NC; i++) {
1463 mChildren.itemAt(i)->collect_resid_strings(outPool, outResIds);
1464 }
1465
1466 return NO_ERROR;
1467}
1468
1469status_t XMLNode::flatten_node(const StringPool& strings, const sp<AaptFile>& dest,
1470 bool stripComments, bool stripRawValues) const
1471{
1472 ResXMLTree_node node;
1473 ResXMLTree_cdataExt cdataExt;
1474 ResXMLTree_namespaceExt namespaceExt;
1475 ResXMLTree_attrExt attrExt;
1476 const void* extData = NULL;
1477 size_t extSize = 0;
1478 ResXMLTree_attribute attr;
Xavier Ducrohetf8aea992012-02-02 17:18:18 -08001479 bool writeCurrentNode = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480
1481 const size_t NA = mAttributes.size();
1482 const size_t NC = mChildren.size();
1483 size_t i;
1484
1485 LOG_ALWAYS_FATAL_IF(NA != mAttributeOrder.size(), "Attributes messed up!");
1486
1487 const String16 id16("id");
1488 const String16 class16("class");
1489 const String16 style16("style");
1490
1491 const type type = getType();
Xavier Ducrohetf8aea992012-02-02 17:18:18 -08001492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001493 memset(&node, 0, sizeof(node));
1494 memset(&attr, 0, sizeof(attr));
1495 node.header.headerSize = htods(sizeof(node));
1496 node.lineNumber = htodl(getStartLineNumber());
1497 if (!stripComments) {
1498 node.comment.index = htodl(
1499 mComment.size() > 0 ? strings.offsetForString(mComment) : -1);
1500 //if (mComment.size() > 0) {
1501 // printf("Flattening comment: %s\n", String8(mComment).string());
1502 //}
1503 } else {
1504 node.comment.index = htodl((uint32_t)-1);
1505 }
1506 if (type == TYPE_ELEMENT) {
1507 node.header.type = htods(RES_XML_START_ELEMENT_TYPE);
1508 extData = &attrExt;
1509 extSize = sizeof(attrExt);
1510 memset(&attrExt, 0, sizeof(attrExt));
1511 if (mNamespaceUri.size() > 0) {
1512 attrExt.ns.index = htodl(strings.offsetForString(mNamespaceUri));
1513 } else {
1514 attrExt.ns.index = htodl((uint32_t)-1);
1515 }
1516 attrExt.name.index = htodl(strings.offsetForString(mElementName));
1517 attrExt.attributeStart = htods(sizeof(attrExt));
1518 attrExt.attributeSize = htods(sizeof(attr));
1519 attrExt.attributeCount = htods(NA);
1520 attrExt.idIndex = htods(0);
1521 attrExt.classIndex = htods(0);
1522 attrExt.styleIndex = htods(0);
1523 for (i=0; i<NA; i++) {
1524 ssize_t idx = mAttributeOrder.valueAt(i);
1525 const attribute_entry& ae = mAttributes.itemAt(idx);
1526 if (ae.ns.size() == 0) {
1527 if (ae.name == id16) {
1528 attrExt.idIndex = htods(i+1);
1529 } else if (ae.name == class16) {
1530 attrExt.classIndex = htods(i+1);
1531 } else if (ae.name == style16) {
1532 attrExt.styleIndex = htods(i+1);
1533 }
1534 }
1535 }
1536 } else if (type == TYPE_NAMESPACE) {
Xavier Ducrohetf8aea992012-02-02 17:18:18 -08001537 if (mNamespaceUri == RESOURCES_TOOLS_NAMESPACE) {
1538 writeCurrentNode = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001539 } else {
Xavier Ducrohetf8aea992012-02-02 17:18:18 -08001540 node.header.type = htods(RES_XML_START_NAMESPACE_TYPE);
1541 extData = &namespaceExt;
1542 extSize = sizeof(namespaceExt);
1543 memset(&namespaceExt, 0, sizeof(namespaceExt));
1544 if (mNamespacePrefix.size() > 0) {
1545 namespaceExt.prefix.index = htodl(strings.offsetForString(mNamespacePrefix));
1546 } else {
1547 namespaceExt.prefix.index = htodl((uint32_t)-1);
1548 }
1549 namespaceExt.prefix.index = htodl(strings.offsetForString(mNamespacePrefix));
1550 namespaceExt.uri.index = htodl(strings.offsetForString(mNamespaceUri));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001552 LOG_ALWAYS_FATAL_IF(NA != 0, "Namespace nodes can't have attributes!");
1553 } else if (type == TYPE_CDATA) {
1554 node.header.type = htods(RES_XML_CDATA_TYPE);
1555 extData = &cdataExt;
1556 extSize = sizeof(cdataExt);
1557 memset(&cdataExt, 0, sizeof(cdataExt));
1558 cdataExt.data.index = htodl(strings.offsetForString(mChars));
1559 cdataExt.typedData.size = htods(sizeof(cdataExt.typedData));
1560 cdataExt.typedData.res0 = 0;
1561 cdataExt.typedData.dataType = mCharsValue.dataType;
1562 cdataExt.typedData.data = htodl(mCharsValue.data);
1563 LOG_ALWAYS_FATAL_IF(NA != 0, "CDATA nodes can't have attributes!");
1564 }
1565
1566 node.header.size = htodl(sizeof(node) + extSize + (sizeof(attr)*NA));
1567
Xavier Ducrohetf8aea992012-02-02 17:18:18 -08001568 if (writeCurrentNode) {
1569 dest->writeData(&node, sizeof(node));
1570 if (extSize > 0) {
1571 dest->writeData(extData, extSize);
1572 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001573 }
1574
1575 for (i=0; i<NA; i++) {
1576 ssize_t idx = mAttributeOrder.valueAt(i);
1577 const attribute_entry& ae = mAttributes.itemAt(idx);
1578 if (ae.ns.size() > 0) {
1579 attr.ns.index = htodl(strings.offsetForString(ae.ns));
1580 } else {
1581 attr.ns.index = htodl((uint32_t)-1);
1582 }
1583 attr.name.index = htodl(ae.namePoolIdx);
1584
1585 if (!stripRawValues || ae.needStringValue()) {
1586 attr.rawValue.index = htodl(strings.offsetForString(ae.string));
1587 } else {
1588 attr.rawValue.index = htodl((uint32_t)-1);
1589 }
1590 attr.typedValue.size = htods(sizeof(attr.typedValue));
1591 if (ae.value.dataType == Res_value::TYPE_NULL
1592 || ae.value.dataType == Res_value::TYPE_STRING) {
1593 attr.typedValue.res0 = 0;
1594 attr.typedValue.dataType = Res_value::TYPE_STRING;
1595 attr.typedValue.data = htodl(strings.offsetForString(ae.string));
1596 } else {
1597 attr.typedValue.res0 = 0;
1598 attr.typedValue.dataType = ae.value.dataType;
1599 attr.typedValue.data = htodl(ae.value.data);
1600 }
1601 dest->writeData(&attr, sizeof(attr));
1602 }
1603
1604 for (i=0; i<NC; i++) {
1605 status_t err = mChildren.itemAt(i)->flatten_node(strings, dest,
1606 stripComments, stripRawValues);
1607 if (err != NO_ERROR) {
1608 return err;
1609 }
1610 }
1611
1612 if (type == TYPE_ELEMENT) {
1613 ResXMLTree_endElementExt endElementExt;
1614 memset(&endElementExt, 0, sizeof(endElementExt));
1615 node.header.type = htods(RES_XML_END_ELEMENT_TYPE);
1616 node.header.size = htodl(sizeof(node)+sizeof(endElementExt));
1617 node.lineNumber = htodl(getEndLineNumber());
1618 node.comment.index = htodl((uint32_t)-1);
1619 endElementExt.ns.index = attrExt.ns.index;
1620 endElementExt.name.index = attrExt.name.index;
1621 dest->writeData(&node, sizeof(node));
1622 dest->writeData(&endElementExt, sizeof(endElementExt));
1623 } else if (type == TYPE_NAMESPACE) {
Xavier Ducrohetf8aea992012-02-02 17:18:18 -08001624 if (writeCurrentNode) {
1625 node.header.type = htods(RES_XML_END_NAMESPACE_TYPE);
1626 node.lineNumber = htodl(getEndLineNumber());
1627 node.comment.index = htodl((uint32_t)-1);
1628 node.header.size = htodl(sizeof(node)+extSize);
1629 dest->writeData(&node, sizeof(node));
1630 dest->writeData(extData, extSize);
1631 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001632 }
1633
1634 return NO_ERROR;
1635}