1 //
2 // Copyright 2006 The Android Open Source Project
3 //
4 // Build resource files from raw assets.
5 //
6 
7 #include "XMLNode.h"
8 #include "ResourceTable.h"
9 #include "pseudolocalize.h"
10 
11 #include <utils/ByteOrder.h>
12 #include <errno.h>
13 #include <string.h>
14 
15 #ifndef _WIN32
16 #define O_BINARY 0
17 #endif
18 
19 // STATUST: mingw does seem to redefine UNKNOWN_ERROR from our enum value, so a cast is necessary.
20 #if !defined(_WIN32)
21 #  define STATUST(x) x
22 #else
23 #  define STATUST(x) (status_t)x
24 #endif
25 
26 // Set to true for noisy debug output.
27 static const bool kIsDebug = false;
28 // Set to true for noisy debug output of parsing.
29 static const bool kIsDebugParse = false;
30 
31 #if PRINT_STRING_METRICS
32 static const bool kPrintStringMetrics = true;
33 #else
34 static const bool kPrintStringMetrics = false;
35 #endif
36 
37 const char* const RESOURCES_ROOT_NAMESPACE = "http://schemas.android.com/apk/res/";
38 const char* const RESOURCES_ANDROID_NAMESPACE = "http://schemas.android.com/apk/res/android";
39 const char* const RESOURCES_AUTO_PACKAGE_NAMESPACE = "http://schemas.android.com/apk/res-auto";
40 const char* const RESOURCES_ROOT_PRV_NAMESPACE = "http://schemas.android.com/apk/prv/res/";
41 
42 const char* const XLIFF_XMLNS = "urn:oasis:names:tc:xliff:document:1.2";
43 const char* const ALLOWED_XLIFF_ELEMENTS[] = {
44         "bpt",
45         "ept",
46         "it",
47         "ph",
48         "g",
49         "bx",
50         "ex",
51         "x"
52     };
53 
isWhitespace(const char16_t * str)54 bool isWhitespace(const char16_t* str)
55 {
56     while (*str != 0 && *str < 128 && isspace(*str)) {
57         str++;
58     }
59     return *str == 0;
60 }
61 
62 static const String16 RESOURCES_PREFIX(RESOURCES_ROOT_NAMESPACE);
63 static const String16 RESOURCES_PREFIX_AUTO_PACKAGE(RESOURCES_AUTO_PACKAGE_NAMESPACE);
64 static const String16 RESOURCES_PRV_PREFIX(RESOURCES_ROOT_PRV_NAMESPACE);
65 static const String16 RESOURCES_TOOLS_NAMESPACE("http://schemas.android.com/tools");
66 
getNamespaceResourcePackage(const String16 & appPackage,const String16 & namespaceUri,bool * outIsPublic)67 String16 getNamespaceResourcePackage(const String16& appPackage, const String16& namespaceUri, bool* outIsPublic)
68 {
69     //printf("%s starts with %s?\n", String8(namespaceUri).string(),
70     //       String8(RESOURCES_PREFIX).string());
71     size_t prefixSize;
72     bool isPublic = true;
73     if(namespaceUri.startsWith(RESOURCES_PREFIX_AUTO_PACKAGE)) {
74         if (kIsDebug) {
75             printf("Using default application package: %s -> %s\n", String8(namespaceUri).string(),
76                    String8(appPackage).string());
77         }
78         isPublic = true;
79         return appPackage;
80     } else if (namespaceUri.startsWith(RESOURCES_PREFIX)) {
81         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 
hasSubstitutionErrors(const char * fileName,ResXMLTree * inXml,const String16 & str16)96 status_t hasSubstitutionErrors(const char* fileName,
97                                ResXMLTree* inXml,
98                                const String16& str16)
99 {
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; "
194                 "did you mean to add the formatted=\"false\" attribute?\n");
195         return NOT_ENOUGH_DATA;
196     }
197 
198     return NO_ERROR;
199 }
200 
parseStyledString(Bundle *,const char * fileName,ResXMLTree * inXml,const String16 & endTag,String16 * outString,Vector<StringPool::entry_style_span> * outSpans,bool isFormatted,PseudolocalizationMethod pseudolocalize)201 status_t parseStyledString(Bundle* /* bundle */,
202                            const char* fileName,
203                            ResXMLTree* inXml,
204                            const String16& endTag,
205                            String16* outString,
206                            Vector<StringPool::entry_style_span>* outSpans,
207                            bool isFormatted,
208                            PseudolocalizationMethod pseudolocalize)
209 {
210     Vector<StringPool::entry_style_span> spanStack;
211     String16 curString;
212     String16 rawString;
213     Pseudolocalizer pseudo(pseudolocalize);
214     const char* errorMsg;
215     int xliffDepth = 0;
216     bool firstTime = true;
217 
218     size_t len;
219     ResXMLTree::event_code_t code;
220     curString.append(pseudo.start());
221     while ((code=inXml->next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) {
222         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.
228                     pseudolocalize = NO_PSEUDOLOCALIZATION;
229                     pseudo.setMethod(pseudolocalize);
230                     curString = String16();
231                 }
232             }
233             if (xliffDepth == 0 && pseudolocalize > 0) {
234                 curString.append(pseudo.text(text));
235             } else {
236                 if (isFormatted && hasSubstitutionErrors(fileName, inXml, text) != NO_ERROR) {
237                     return UNKNOWN_ERROR;
238                 } else {
239                     curString.append(text);
240                 }
241             }
242         } else if (code == ResXMLTree::START_TAG) {
243             const String16 element16(inXml->getElementName(&len));
244             const String8 element8(element16);
245 
246             size_t nslen;
247             const char16_t* ns = inXml->getElementNamespace(&nslen);
248             if (ns == NULL) {
249                 ns = (const char16_t*)"\0\0";
250                 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                 }
269 moveon:
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;
304             const char16_t* ns = inXml->getElementNamespace(&nslen);
305             if (ns == NULL) {
306                 ns = (const char16_t*)"\0\0";
307                 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 
358             /*
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) {
364                 fprintf(stderr, "%s:%d: warning: empty '%s' span found in text '%s'\n",
365                         fileName, inXml->getLineNumber(),
366                         String8(spanTag).string(), String8(*outString).string());
367 
368             }
369         } else if (code == ResXMLTree::START_NAMESPACE) {
370             // nothing
371         }
372     }
373 
374     curString.append(pseudo.end());
375 
376     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 
402 struct namespace_entry {
403     String8 prefix;
404     String8 uri;
405 };
406 
make_prefix(int depth)407 static 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 
build_namespace(const Vector<namespace_entry> & namespaces,const char16_t * ns)417 static String8 build_namespace(const Vector<namespace_entry>& namespaces,
418         const char16_t* ns)
419 {
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 
printXMLBlock(ResXMLTree * block)436 void printXMLBlock(ResXMLTree* block)
437 {
438     block->restart();
439 
440     Vector<namespace_entry> namespaces;
441 
442     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;
449             const char16_t* ns16 = block->getElementNamespace(&len);
450             String8 elemNs = build_namespace(namespaces, ns16);
451             const char16_t* com16 = block->getComment(&len);
452             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) {
477                     printf("=@0x%08x", (int)value.data);
478                 } else if (value.dataType == Res_value::TYPE_ATTRIBUTE) {
479                     printf("=?0x%08x", (int)value.data);
480                 } else if (value.dataType == Res_value::TYPE_STRING) {
481                     printf("=\"%s\"",
482                             ResTable::normalizeForOutput(String8(block->getAttributeStringValue(i,
483                                         &len)).string()).string());
484                 } 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) {
489                     printf(" (Raw: \"%s\")", ResTable::normalizeForOutput(String8(val).string()).
490                             string());
491                 }
492                 printf("\n");
493             }
494         } else if (code == ResXMLTree::END_TAG) {
495             // 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             }
501         } else if (code == ResXMLTree::START_NAMESPACE) {
502             namespace_entry ns;
503             size_t len;
504             const char16_t* prefix16 = block->getNamespacePrefix(&len);
505             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) {
516             if (--depth < 0) {
517                 printf("***BAD DEPTH in XMLBlock: %d\n", depth);
518                 break;
519             }
520             const namespace_entry& ns = namespaces.top();
521             size_t len;
522             const char16_t* prefix16 = block->getNamespacePrefix(&len);
523             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;
543             printf("%sC: \"%s\"\n", prefix.string(),
544                     ResTable::normalizeForOutput(String8(block->getText(&len)).string()).string());
545         }
546     }
547 
548     block->restart();
549 }
550 
parseXMLResource(const sp<AaptFile> & file,ResXMLTree * outTree,bool stripAll,bool keepComments,const char ** cDataTags)551 status_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 
561     if (kIsDebug) {
562         printf("Input XML from %s:\n", (const char*)file->getPrintableSource());
563         root->print();
564     }
565     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 
575     if (kIsDebug) {
576         printf("Output XML:\n");
577         printXMLBlock(outTree);
578     }
579 
580     return NO_ERROR;
581 }
582 
parse(const sp<AaptFile> & file)583 sp<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 
XMLNode()629 XMLNode::XMLNode()
630     : mNextAttributeIndex(0x80000000)
631     , mStartLineNumber(0)
632     , mEndLineNumber(0)
633     , mUTF8(false) {}
634 
XMLNode(const String8 & filename,const String16 & s1,const String16 & s2,bool isNamespace)635 XMLNode::XMLNode(const String8& filename, const String16& s1, const String16& s2, bool isNamespace)
636     : mNextAttributeIndex(0x80000000)
637     , mFilename(filename)
638     , mStartLineNumber(0)
639     , mEndLineNumber(0)
640     , mUTF8(false)
641 {
642     if (isNamespace) {
643         mNamespacePrefix = s1;
644         mNamespaceUri = s2;
645     } else {
646         mNamespaceUri = s1;
647         mElementName = s2;
648     }
649 }
650 
XMLNode(const String8 & filename)651 XMLNode::XMLNode(const String8& filename)
652     : mFilename(filename)
653 {
654     memset(&mCharsValue, 0, sizeof(mCharsValue));
655 }
656 
getType() const657 XMLNode::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 
getNamespacePrefix() const668 const String16& XMLNode::getNamespacePrefix() const
669 {
670     return mNamespacePrefix;
671 }
672 
getNamespaceUri() const673 const String16& XMLNode::getNamespaceUri() const
674 {
675     return mNamespaceUri;
676 }
677 
getElementNamespace() const678 const String16& XMLNode::getElementNamespace() const
679 {
680     return mNamespaceUri;
681 }
682 
getElementName() const683 const String16& XMLNode::getElementName() const
684 {
685     return mElementName;
686 }
687 
getChildren() const688 const Vector<sp<XMLNode> >& XMLNode::getChildren() const
689 {
690     return mChildren;
691 }
692 
693 
getChildren()694 Vector<sp<XMLNode> >& XMLNode::getChildren()
695 {
696     return mChildren;
697 }
698 
getFilename() const699 const String8& XMLNode::getFilename() const
700 {
701     return mFilename;
702 }
703 
704 const Vector<XMLNode::attribute_entry>&
getAttributes() const705     XMLNode::getAttributes() const
706 {
707     return mAttributes;
708 }
709 
getAttribute(const String16 & ns,const String16 & name) const710 const 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     }
719 
720     return NULL;
721 }
722 
removeAttribute(const String16 & ns,const String16 & name)723 bool 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 
editAttribute(const String16 & ns,const String16 & name)735 XMLNode::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 
getCData() const748 const String16& XMLNode::getCData() const
749 {
750     return mChars;
751 }
752 
getComment() const753 const String16& XMLNode::getComment() const
754 {
755     return mComment;
756 }
757 
getStartLineNumber() const758 int32_t XMLNode::getStartLineNumber() const
759 {
760     return mStartLineNumber;
761 }
762 
getEndLineNumber() const763 int32_t XMLNode::getEndLineNumber() const
764 {
765     return mEndLineNumber;
766 }
767 
searchElement(const String16 & tagNamespace,const String16 & tagName)768 sp<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     }
775 
776     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     }
782 
783     return NULL;
784 }
785 
getChildElement(const String16 & tagNamespace,const String16 & tagName)786 sp<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     }
796 
797     return NULL;
798 }
799 
addChild(const sp<XMLNode> & child)800 status_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 
insertChildAt(const sp<XMLNode> & child,size_t index)811 status_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 
addAttribute(const String16 & ns,const String16 & name,const String16 & value)822 status_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     }
829 
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     }
839     return NO_ERROR;
840 }
841 
removeAttribute(size_t index)842 status_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 
setAttributeResID(size_t attrIdx,uint32_t resId)868 void 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     }
876     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     }
883     mAttributes.editItemAt(attrIdx).nameResId = resId;
884     mAttributeOrder.add(resId, attrIdx);
885 }
886 
appendChars(const String16 & chars)887 status_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 
appendComment(const String16 & comment)897 status_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 
setStartLineNumber(int32_t line)906 void XMLNode::setStartLineNumber(int32_t line)
907 {
908     mStartLineNumber = line;
909 }
910 
setEndLineNumber(int32_t line)911 void XMLNode::setEndLineNumber(int32_t line)
912 {
913     mEndLineNumber = line;
914 }
915 
removeWhitespace(bool stripAll,const char ** cDataTags)916 void 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 
parseValues(const sp<AaptAssets> & assets,ResourceTable * table)974 status_t XMLNode::parseValues(const sp<AaptAssets>& assets,
975                               ResourceTable* table)
976 {
977     bool hasErrors = false;
978 
979     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             }
993             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             }
998         }
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     }
1007     return hasErrors ? STATUST(UNKNOWN_ERROR) : NO_ERROR;
1008 }
1009 
assignResourceIds(const sp<AaptAssets> & assets,const ResourceTable * table)1010 status_t XMLNode::assignResourceIds(const sp<AaptAssets>& assets,
1011                                     const ResourceTable* table)
1012 {
1013     bool hasErrors = false;
1014 
1015     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;
1022             bool nsIsPublic = true;
1023             String16 pkg(getNamespaceResourcePackage(String16(assets->getPackage()), e.ns, &nsIsPublic));
1024             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             }
1033             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) {
1041                 if (kIsDebug) {
1042                     printf("XML attribute name %s: resid=0x%08x\n",
1043                             String8(e.name).string(), res);
1044                 }
1045                 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 
1062     return hasErrors ? STATUST(UNKNOWN_ERROR) : NO_ERROR;
1063 }
1064 
clone() const1065 sp<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 
flatten(const sp<AaptFile> & dest,bool stripComments,bool stripRawValues) const1089 status_t XMLNode::flatten(const sp<AaptFile>& dest,
1090         bool stripComments, bool stripRawValues) const
1091 {
1092     StringPool strings(mUTF8);
1093     Vector<uint32_t> resids;
1094 
1095     // 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 
1104     sp<AaptFile> stringPool = strings.createStringBlock();
1105 
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);
1135     hd->header.size = htodl(dest->getSize()-basePos);
1136 
1137     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     }
1142 
1143     return NO_ERROR;
1144 }
1145 
print(int indent)1146 void 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 
splitName(const char * name,String16 * outNs,String16 * outName)1196 static 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 
1211 void XMLCALL
startNamespace(void * userData,const char * prefix,const char * uri)1212 XMLNode::startNamespace(void *userData, const char *prefix, const char *uri)
1213 {
1214     if (kIsDebugParse) {
1215         printf("Start Namespace: %s %s\n", prefix, uri);
1216     }
1217     ParseState* st = (ParseState*)userData;
1218     sp<XMLNode> node = XMLNode::newNamespace(st->filename,
1219             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 
1229 void XMLCALL
startElement(void * userData,const char * name,const char ** atts)1230 XMLNode::startElement(void *userData, const char *name, const char **atts)
1231 {
1232     if (kIsDebugParse) {
1233         printf("Start Element: %s\n", name);
1234     }
1235     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 
1257 void XMLCALL
characterData(void * userData,const XML_Char * s,int len)1258 XMLNode::characterData(void *userData, const XML_Char *s, int len)
1259 {
1260     if (kIsDebugParse) {
1261         printf("CDATA: \"%s\"\n", String8(s, len).string());
1262     }
1263     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 
1286 void XMLCALL
endElement(void * userData,const char * name)1287 XMLNode::endElement(void *userData, const char *name)
1288 {
1289     if (kIsDebugParse) {
1290         printf("End Element: %s\n", name);
1291     }
1292     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 
1307 void XMLCALL
endNamespace(void * userData,const char * prefix)1308 XMLNode::endNamespace(void *userData, const char *prefix)
1309 {
1310     const char* nonNullPrefix = prefix != NULL ? prefix : "";
1311     if (kIsDebugParse) {
1312         printf("End Namespace: %s\n", prefix);
1313     }
1314     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 
1322 void XMLCALL
commentData(void * userData,const char * comment)1323 XMLNode::commentData(void *userData, const char *comment)
1324 {
1325     if (kIsDebugParse) {
1326         printf("Comment: %s\n", comment);
1327     }
1328     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 
collect_strings(StringPool * dest,Vector<uint32_t> * outResIds,bool stripComments,bool stripRawValues) const1335 status_t XMLNode::collect_strings(StringPool* dest, Vector<uint32_t>* outResIds,
1336         bool stripComments, bool stripRawValues) const
1337 {
1338     collect_attr_strings(dest, outResIds, true);
1339 
1340     int i;
1341     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         }
1348     }
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 
collect_attr_strings(StringPool * outPool,Vector<uint32_t> * outResIds,bool allAttrs) const1390 status_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);
1424                 if (kIsDebug) {
1425                     printf("Adding attr %s (resid 0x%08x) to pool: idx=%zd\n",
1426                             String8(attr.name).string(), id, idx);
1427                 }
1428                 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;
1436             if (kIsDebug) {
1437                 printf("String %s offset=0x%08zd\n", String8(attr.name).string(), idx);
1438             }
1439         }
1440     }
1441 
1442     return NO_ERROR;
1443 }
1444 
collect_resid_strings(StringPool * outPool,Vector<uint32_t> * outResIds) const1445 status_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 
flatten_node(const StringPool & strings,const sp<AaptFile> & dest,bool stripComments,bool stripRawValues) const1459 status_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;
1469     bool writeCurrentNode = true;
1470 
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();
1482 
1483     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) {
1527         if (mNamespaceUri == RESOURCES_TOOLS_NAMESPACE) {
1528             writeCurrentNode = false;
1529         } else {
1530             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));
1541         }
1542         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 
1558     if (writeCurrentNode) {
1559         dest->writeData(&node, sizeof(node));
1560         if (extSize > 0) {
1561             dest->writeData(extData, extSize);
1562         }
1563     }
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) {
1614         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         }
1622     }
1623 
1624     return NO_ERROR;
1625 }
1626