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