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