2 // Copyright 2006 The Android Open Source Project
4 // Build resource files from raw assets.
8 #include "ResourceTable.h"
9 #include "pseudolocalize.h"
11 #include <utils/ByteOrder.h>
19 // SSIZE: mingw does not have signed size_t == ssize_t.
20 // STATUST: mingw does seem to redefine UNKNOWN_ERROR from our enum value, so a cast is necessary.
25 # define SSIZE(x) (signed size_t)x
26 # define STATUST(x) (status_t)x
29 // Set to true for noisy debug output.
30 static const bool kIsDebug = false;
31 // Set to true for noisy debug output of parsing.
32 static const bool kIsDebugParse = false;
34 #if PRINT_STRING_METRICS
35 static const bool kPrintStringMetrics = true;
37 static const bool kPrintStringMetrics = false;
40 const char* const RESOURCES_ROOT_NAMESPACE = "http://schemas.android.com/apk/res/";
41 const char* const RESOURCES_ANDROID_NAMESPACE = "http://schemas.android.com/apk/res/android";
42 const char* const RESOURCES_AUTO_PACKAGE_NAMESPACE = "http://schemas.android.com/apk/res-auto";
43 const char* const RESOURCES_ROOT_PRV_NAMESPACE = "http://schemas.android.com/apk/prv/res/";
45 const char* const XLIFF_XMLNS = "urn:oasis:names:tc:xliff:document:1.2";
46 const char* const ALLOWED_XLIFF_ELEMENTS[] = {
57 bool isWhitespace(const char16_t* str)
59 while (*str != 0 && *str < 128 && isspace(*str)) {
65 static const String16 RESOURCES_PREFIX(RESOURCES_ROOT_NAMESPACE);
66 static const String16 RESOURCES_PREFIX_AUTO_PACKAGE(RESOURCES_AUTO_PACKAGE_NAMESPACE);
67 static const String16 RESOURCES_PRV_PREFIX(RESOURCES_ROOT_PRV_NAMESPACE);
68 static const String16 RESOURCES_TOOLS_NAMESPACE("http://schemas.android.com/tools");
70 String16 getNamespaceResourcePackage(String16 appPackage, String16 namespaceUri, bool* outIsPublic)
72 //printf("%s starts with %s?\n", String8(namespaceUri).string(),
73 // String8(RESOURCES_PREFIX).string());
76 if(namespaceUri.startsWith(RESOURCES_PREFIX_AUTO_PACKAGE)) {
78 printf("Using default application package: %s -> %s\n", String8(namespaceUri).string(),
79 String8(appPackage).string());
83 } else if (namespaceUri.startsWith(RESOURCES_PREFIX)) {
84 prefixSize = RESOURCES_PREFIX.size();
85 } else if (namespaceUri.startsWith(RESOURCES_PRV_PREFIX)) {
87 prefixSize = RESOURCES_PRV_PREFIX.size();
89 if (outIsPublic) *outIsPublic = isPublic; // = true
94 //printf("namespace: %s\n", String8(String16(namespaceUri, namespaceUri.size()-prefixSize, prefixSize)).string());
95 if (outIsPublic) *outIsPublic = isPublic;
96 return String16(namespaceUri, namespaceUri.size()-prefixSize, prefixSize);
99 status_t hasSubstitutionErrors(const char* fileName,
103 const char16_t* str = str16.string();
104 const char16_t* p = str;
105 const char16_t* end = str + str16.size();
107 bool nonpositional = false;
112 * Look for the start of a Java-style substitution sequence.
114 if (*p == '%' && p + 1 < end) {
117 // A literal percent sign represented by %%
125 if (*p >= '0' && *p <= '9') {
128 } while (*p >= '0' && *p <= '9');
130 // This must be a size specification instead of position.
131 nonpositional = true;
133 } else if (*p == '<') {
134 // Reusing last argument; bad idea since it can be re-arranged.
135 nonpositional = true;
138 // Optionally '$' can be specified at the end.
139 if (p < end && *p == '$') {
143 nonpositional = true;
146 // Ignore flags and widths
147 while (p < end && (*p == '-' ||
153 (*p >= '0' && *p <= '9'))) {
158 * This is a shortcut to detect strings that are going to Time.format()
159 * instead of String.format()
161 * Comparison of String.format() and Time.format() args:
163 * String: ABC E GH ST X abcdefgh nost x
164 * Time: DEFGHKMS W Za d hkm s w yz
166 * Therefore we know it's definitely Time if we have:
191 * If we have more than one substitution in this string and any of them
192 * are not in positional form, give the user an error.
194 if (argCount > 1 && nonpositional) {
195 SourcePos(String8(fileName), inXml->getLineNumber()).error(
196 "Multiple substitutions specified in non-positional format; "
197 "did you mean to add the formatted=\"false\" attribute?\n");
198 return NOT_ENOUGH_DATA;
204 status_t parseStyledString(Bundle* /* bundle */,
205 const char* fileName,
207 const String16& endTag,
209 Vector<StringPool::entry_style_span>* outSpans,
211 PseudolocalizationMethod pseudolocalize)
213 Vector<StringPool::entry_style_span> spanStack;
216 Pseudolocalizer pseudo(pseudolocalize);
217 const char* errorMsg;
219 bool firstTime = true;
222 ResXMLTree::event_code_t code;
223 curString.append(pseudo.start());
224 while ((code=inXml->next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) {
225 if (code == ResXMLTree::TEXT) {
226 String16 text(inXml->getText(&len));
227 if (firstTime && text.size() > 0) {
229 if (text.string()[0] == '@') {
230 // If this is a resource reference, don't do the pseudoloc.
231 pseudolocalize = NO_PSEUDOLOCALIZATION;
232 pseudo.setMethod(pseudolocalize);
233 curString = String16();
236 if (xliffDepth == 0 && pseudolocalize > 0) {
237 curString.append(pseudo.text(text));
239 if (isFormatted && hasSubstitutionErrors(fileName, inXml, text) != NO_ERROR) {
240 return UNKNOWN_ERROR;
242 curString.append(text);
245 } else if (code == ResXMLTree::START_TAG) {
246 const String16 element16(inXml->getElementName(&len));
247 const String8 element8(element16);
250 const char16_t* ns = inXml->getElementNamespace(&nslen);
252 ns = (const char16_t*)"\0\0";
255 const String8 nspace(String16(ns, nslen));
256 if (nspace == XLIFF_XMLNS) {
257 const int N = sizeof(ALLOWED_XLIFF_ELEMENTS)/sizeof(ALLOWED_XLIFF_ELEMENTS[0]);
258 for (int i=0; i<N; i++) {
259 if (element8 == ALLOWED_XLIFF_ELEMENTS[i]) {
261 // in this case, treat it like it was just text, in other words, do nothing
262 // here and silently drop this element
267 SourcePos(String8(fileName), inXml->getLineNumber()).error(
268 "Found unsupported XLIFF tag <%s>\n",
270 return UNKNOWN_ERROR;
276 if (outSpans == NULL) {
277 SourcePos(String8(fileName), inXml->getLineNumber()).error(
278 "Found style tag <%s> where styles are not allowed\n", element8.string());
279 return UNKNOWN_ERROR;
282 if (!ResTable::collectString(outString, curString.string(),
283 curString.size(), false, &errorMsg, true)) {
284 SourcePos(String8(fileName), inXml->getLineNumber()).error("%s (in %s)\n",
285 errorMsg, String8(curString).string());
286 return UNKNOWN_ERROR;
288 rawString.append(curString);
289 curString = String16();
291 StringPool::entry_style_span span;
292 span.name = element16;
293 for (size_t ai=0; ai<inXml->getAttributeCount(); ai++) {
294 span.name.append(String16(";"));
295 const char16_t* str = inXml->getAttributeName(ai, &len);
296 span.name.append(str, len);
297 span.name.append(String16("="));
298 str = inXml->getAttributeStringValue(ai, &len);
299 span.name.append(str, len);
301 //printf("Span: %s\n", String8(span.name).string());
302 span.span.firstChar = span.span.lastChar = outString->size();
303 spanStack.push(span);
305 } else if (code == ResXMLTree::END_TAG) {
307 const char16_t* ns = inXml->getElementNamespace(&nslen);
309 ns = (const char16_t*)"\0\0";
312 const String8 nspace(String16(ns, nslen));
313 if (nspace == XLIFF_XMLNS) {
317 if (!ResTable::collectString(outString, curString.string(),
318 curString.size(), false, &errorMsg, true)) {
319 SourcePos(String8(fileName), inXml->getLineNumber()).error("%s (in %s)\n",
320 errorMsg, String8(curString).string());
321 return UNKNOWN_ERROR;
323 rawString.append(curString);
324 curString = String16();
326 if (spanStack.size() == 0) {
327 if (strcmp16(inXml->getElementName(&len), endTag.string()) != 0) {
328 SourcePos(String8(fileName), inXml->getLineNumber()).error(
329 "Found tag %s where <%s> close is expected\n",
330 String8(inXml->getElementName(&len)).string(),
331 String8(endTag).string());
332 return UNKNOWN_ERROR;
336 StringPool::entry_style_span span = spanStack.top();
338 ssize_t semi = span.name.findFirst(';');
340 spanTag.setTo(span.name.string(), semi);
342 spanTag.setTo(span.name);
344 if (strcmp16(inXml->getElementName(&len), spanTag.string()) != 0) {
345 SourcePos(String8(fileName), inXml->getLineNumber()).error(
346 "Found close tag %s where close tag %s is expected\n",
347 String8(inXml->getElementName(&len)).string(),
348 String8(spanTag).string());
349 return UNKNOWN_ERROR;
352 if (outString->size() > 0) {
353 span.span.lastChar = outString->size()-1;
354 if (span.span.lastChar >= span.span.firstChar) {
362 * This warning seems to be just an irritation to most people,
363 * since it is typically introduced by translators who then never
367 fprintf(stderr, "%s:%d: warning: empty '%s' span found in text '%s'\n",
368 fileName, inXml->getLineNumber(),
369 String8(spanTag).string(), String8(*outString).string());
372 } else if (code == ResXMLTree::START_NAMESPACE) {
377 curString.append(pseudo.end());
379 if (code == ResXMLTree::BAD_DOCUMENT) {
380 SourcePos(String8(fileName), inXml->getLineNumber()).error(
381 "Error parsing XML\n");
384 if (outSpans != NULL && outSpans->size() > 0) {
385 if (curString.size() > 0) {
386 if (!ResTable::collectString(outString, curString.string(),
387 curString.size(), false, &errorMsg, true)) {
388 SourcePos(String8(fileName), inXml->getLineNumber()).error(
390 errorMsg, String8(curString).string());
391 return UNKNOWN_ERROR;
395 // There is no style information, so string processing will happen
396 // later as part of the overall type conversion. Return to the
397 // client the raw unprocessed text.
398 rawString.append(curString);
399 outString->setTo(rawString);
405 struct namespace_entry {
410 static String8 make_prefix(int depth)
414 for (i=0; i<depth; i++) {
420 static String8 build_namespace(const Vector<namespace_entry>& namespaces,
426 const size_t N = namespaces.size();
427 for (size_t i=0; i<N; i++) {
428 const namespace_entry& ne = namespaces.itemAt(i);
439 void printXMLBlock(ResXMLTree* block)
443 Vector<namespace_entry> namespaces;
445 ResXMLTree::event_code_t code;
447 while ((code=block->next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) {
448 String8 prefix = make_prefix(depth);
450 if (code == ResXMLTree::START_TAG) {
452 const char16_t* ns16 = block->getElementNamespace(&len);
453 String8 elemNs = build_namespace(namespaces, ns16);
454 const char16_t* com16 = block->getComment(&len);
456 printf("%s <!-- %s -->\n", prefix.string(), String8(com16).string());
458 printf("%sE: %s%s (line=%d)\n", prefix.string(), elemNs.string(),
459 String8(block->getElementName(&len)).string(),
460 block->getLineNumber());
461 int N = block->getAttributeCount();
463 prefix = make_prefix(depth);
464 for (i=0; i<N; i++) {
465 uint32_t res = block->getAttributeNameResID(i);
466 ns16 = block->getAttributeNamespace(i, &len);
467 String8 ns = build_namespace(namespaces, ns16);
468 String8 name(block->getAttributeName(i, &len));
469 printf("%sA: ", prefix.string());
471 printf("%s%s(0x%08x)", ns.string(), name.string(), res);
473 printf("%s%s", ns.string(), name.string());
476 block->getAttributeValue(i, &value);
477 if (value.dataType == Res_value::TYPE_NULL) {
479 } else if (value.dataType == Res_value::TYPE_REFERENCE) {
480 printf("=@0x%x", (int)value.data);
481 } else if (value.dataType == Res_value::TYPE_ATTRIBUTE) {
482 printf("=?0x%x", (int)value.data);
483 } else if (value.dataType == Res_value::TYPE_STRING) {
485 ResTable::normalizeForOutput(String8(block->getAttributeStringValue(i,
486 &len)).string()).string());
488 printf("=(type 0x%x)0x%x", (int)value.dataType, (int)value.data);
490 const char16_t* val = block->getAttributeStringValue(i, &len);
492 printf(" (Raw: \"%s\")", ResTable::normalizeForOutput(String8(val).string()).
497 } else if (code == ResXMLTree::END_TAG) {
498 // Invalid tag nesting can be misused to break the parsing
499 // code below. Break if detected.
501 printf("***BAD DEPTH in XMLBlock: %d\n", depth);
504 } else if (code == ResXMLTree::START_NAMESPACE) {
507 const char16_t* prefix16 = block->getNamespacePrefix(&len);
509 ns.prefix = String8(prefix16);
513 ns.uri = String8(block->getNamespaceUri(&len));
515 printf("%sN: %s=%s\n", prefix.string(), ns.prefix.string(),
518 } else if (code == ResXMLTree::END_NAMESPACE) {
520 printf("***BAD DEPTH in XMLBlock: %d\n", depth);
523 const namespace_entry& ns = namespaces.top();
525 const char16_t* prefix16 = block->getNamespacePrefix(&len);
528 pr = String8(prefix16);
532 if (ns.prefix != pr) {
533 prefix = make_prefix(depth);
534 printf("%s*** BAD END NS PREFIX: found=%s, expected=%s\n",
535 prefix.string(), pr.string(), ns.prefix.string());
537 String8 uri = String8(block->getNamespaceUri(&len));
539 prefix = make_prefix(depth);
540 printf("%s *** BAD END NS URI: found=%s, expected=%s\n",
541 prefix.string(), uri.string(), ns.uri.string());
544 } else if (code == ResXMLTree::TEXT) {
546 printf("%sC: \"%s\"\n", prefix.string(),
547 ResTable::normalizeForOutput(String8(block->getText(&len)).string()).string());
554 status_t parseXMLResource(const sp<AaptFile>& file, ResXMLTree* outTree,
555 bool stripAll, bool keepComments,
556 const char** cDataTags)
558 sp<XMLNode> root = XMLNode::parse(file);
560 return UNKNOWN_ERROR;
562 root->removeWhitespace(stripAll, cDataTags);
565 printf("Input XML from %s:\n", (const char*)file->getPrintableSource());
568 sp<AaptFile> rsc = new AaptFile(String8(), AaptGroupEntry(), String8());
569 status_t err = root->flatten(rsc, !keepComments, false);
570 if (err != NO_ERROR) {
573 err = outTree->setTo(rsc->getData(), rsc->getSize(), true);
574 if (err != NO_ERROR) {
579 printf("Output XML:\n");
580 printXMLBlock(outTree);
586 sp<XMLNode> XMLNode::parse(const sp<AaptFile>& file)
589 int fd = open(file->getSourceFile().string(), O_RDONLY | O_BINARY);
591 SourcePos(file->getSourceFile(), -1).error("Unable to open file for read: %s",
596 XML_Parser parser = XML_ParserCreateNS(NULL, 1);
598 state.filename = file->getPrintableSource();
599 state.parser = parser;
600 XML_SetUserData(parser, &state);
601 XML_SetElementHandler(parser, startElement, endElement);
602 XML_SetNamespaceDeclHandler(parser, startNamespace, endNamespace);
603 XML_SetCharacterDataHandler(parser, characterData);
604 XML_SetCommentHandler(parser, commentData);
609 len = read(fd, buf, sizeof(buf));
610 done = len < (ssize_t)sizeof(buf);
612 SourcePos(file->getSourceFile(), -1).error("Error reading file: %s\n", strerror(errno));
616 if (XML_Parse(parser, buf, len, done) == XML_STATUS_ERROR) {
617 SourcePos(file->getSourceFile(), (int)XML_GetCurrentLineNumber(parser)).error(
618 "Error parsing XML: %s\n", XML_ErrorString(XML_GetErrorCode(parser)));
624 XML_ParserFree(parser);
625 if (state.root == NULL) {
626 SourcePos(file->getSourceFile(), -1).error("No XML data generated when parsing");
633 : mNextAttributeIndex(0x80000000)
634 , mStartLineNumber(0)
638 XMLNode::XMLNode(const String8& filename, const String16& s1, const String16& s2, bool isNamespace)
639 : mNextAttributeIndex(0x80000000)
640 , mFilename(filename)
641 , mStartLineNumber(0)
646 mNamespacePrefix = s1;
654 XMLNode::XMLNode(const String8& filename)
655 : mFilename(filename)
657 memset(&mCharsValue, 0, sizeof(mCharsValue));
660 XMLNode::type XMLNode::getType() const
662 if (mElementName.size() != 0) {
665 if (mNamespaceUri.size() != 0) {
666 return TYPE_NAMESPACE;
671 const String16& XMLNode::getNamespacePrefix() const
673 return mNamespacePrefix;
676 const String16& XMLNode::getNamespaceUri() const
678 return mNamespaceUri;
681 const String16& XMLNode::getElementNamespace() const
683 return mNamespaceUri;
686 const String16& XMLNode::getElementName() const
691 const Vector<sp<XMLNode> >& XMLNode::getChildren() const
697 Vector<sp<XMLNode> >& XMLNode::getChildren()
702 const String8& XMLNode::getFilename() const
707 const Vector<XMLNode::attribute_entry>&
708 XMLNode::getAttributes() const
713 const XMLNode::attribute_entry* XMLNode::getAttribute(const String16& ns,
714 const String16& name) const
716 for (size_t i=0; i<mAttributes.size(); i++) {
717 const attribute_entry& ae(mAttributes.itemAt(i));
718 if (ae.ns == ns && ae.name == name) {
726 bool XMLNode::removeAttribute(const String16& ns, const String16& name)
728 for (size_t i = 0; i < mAttributes.size(); i++) {
729 const attribute_entry& ae(mAttributes.itemAt(i));
730 if (ae.ns == ns && ae.name == name) {
738 XMLNode::attribute_entry* XMLNode::editAttribute(const String16& ns,
739 const String16& name)
741 for (size_t i=0; i<mAttributes.size(); i++) {
742 attribute_entry * ae = &mAttributes.editItemAt(i);
743 if (ae->ns == ns && ae->name == name) {
751 const String16& XMLNode::getCData() const
756 const String16& XMLNode::getComment() const
761 int32_t XMLNode::getStartLineNumber() const
763 return mStartLineNumber;
766 int32_t XMLNode::getEndLineNumber() const
768 return mEndLineNumber;
771 sp<XMLNode> XMLNode::searchElement(const String16& tagNamespace, const String16& tagName)
773 if (getType() == XMLNode::TYPE_ELEMENT
774 && mNamespaceUri == tagNamespace
775 && mElementName == tagName) {
779 for (size_t i=0; i<mChildren.size(); i++) {
780 sp<XMLNode> found = mChildren.itemAt(i)->searchElement(tagNamespace, tagName);
789 sp<XMLNode> XMLNode::getChildElement(const String16& tagNamespace, const String16& tagName)
791 for (size_t i=0; i<mChildren.size(); i++) {
792 sp<XMLNode> child = mChildren.itemAt(i);
793 if (child->getType() == XMLNode::TYPE_ELEMENT
794 && child->mNamespaceUri == tagNamespace
795 && child->mElementName == tagName) {
803 status_t XMLNode::addChild(const sp<XMLNode>& child)
805 if (getType() == TYPE_CDATA) {
806 SourcePos(mFilename, child->getStartLineNumber()).error("Child to CDATA node.");
807 return UNKNOWN_ERROR;
809 //printf("Adding child %p to parent %p\n", child.get(), this);
810 mChildren.add(child);
814 status_t XMLNode::insertChildAt(const sp<XMLNode>& child, size_t index)
816 if (getType() == TYPE_CDATA) {
817 SourcePos(mFilename, child->getStartLineNumber()).error("Child to CDATA node.");
818 return UNKNOWN_ERROR;
820 //printf("Adding child %p to parent %p\n", child.get(), this);
821 mChildren.insertAt(child, index);
825 status_t XMLNode::addAttribute(const String16& ns, const String16& name,
826 const String16& value)
828 if (getType() == TYPE_CDATA) {
829 SourcePos(mFilename, getStartLineNumber()).error("Child to CDATA node.");
830 return UNKNOWN_ERROR;
833 if (ns != RESOURCES_TOOLS_NAMESPACE) {
835 e.index = mNextAttributeIndex++;
840 mAttributeOrder.add(e.index, mAttributes.size()-1);
845 status_t XMLNode::removeAttribute(size_t index)
847 if (getType() == TYPE_CDATA) {
848 return UNKNOWN_ERROR;
851 if (index >= mAttributes.size()) {
852 return UNKNOWN_ERROR;
855 const attribute_entry& e = mAttributes[index];
856 const uint32_t key = e.nameResId ? e.nameResId : e.index;
857 mAttributeOrder.removeItem(key);
858 mAttributes.removeAt(index);
860 // Shift all the indices.
861 const size_t attrCount = mAttributeOrder.size();
862 for (size_t i = 0; i < attrCount; i++) {
863 size_t attrIdx = mAttributeOrder[i];
864 if (attrIdx > index) {
865 mAttributeOrder.replaceValueAt(i, attrIdx - 1);
871 void XMLNode::setAttributeResID(size_t attrIdx, uint32_t resId)
873 attribute_entry& e = mAttributes.editItemAt(attrIdx);
875 mAttributeOrder.removeItem(e.nameResId);
877 mAttributeOrder.removeItem(e.index);
880 printf("Elem %s %s=\"%s\": set res id = 0x%08x\n",
881 String8(getElementName()).string(),
882 String8(mAttributes.itemAt(attrIdx).name).string(),
883 String8(mAttributes.itemAt(attrIdx).string).string(),
886 mAttributes.editItemAt(attrIdx).nameResId = resId;
887 mAttributeOrder.add(resId, attrIdx);
890 status_t XMLNode::appendChars(const String16& chars)
892 if (getType() != TYPE_CDATA) {
893 SourcePos(mFilename, getStartLineNumber()).error("Adding characters to element node.");
894 return UNKNOWN_ERROR;
896 mChars.append(chars);
900 status_t XMLNode::appendComment(const String16& comment)
902 if (mComment.size() > 0) {
903 mComment.append(String16("\n"));
905 mComment.append(comment);
909 void XMLNode::setStartLineNumber(int32_t line)
911 mStartLineNumber = line;
914 void XMLNode::setEndLineNumber(int32_t line)
916 mEndLineNumber = line;
919 void XMLNode::removeWhitespace(bool stripAll, const char** cDataTags)
921 //printf("Removing whitespace in %s\n", String8(mElementName).string());
922 size_t N = mChildren.size();
924 String8 tag(mElementName);
925 const char** p = cDataTags;
933 for (size_t i=0; i<N; i++) {
934 sp<XMLNode> node = mChildren.itemAt(i);
935 if (node->getType() == TYPE_CDATA) {
936 // This is a CDATA node...
937 const char16_t* p = node->mChars.string();
938 while (*p != 0 && *p < 128 && isspace(*p)) {
941 //printf("Space ends at %d in \"%s\"\n",
942 // (int)(p-node->mChars.string()),
943 // String8(node->mChars).string());
947 mChildren.removeAt(i);
951 node->mChars = String16(" ");
954 // Compact leading/trailing whitespace.
955 const char16_t* e = node->mChars.string()+node->mChars.size()-1;
956 while (e > p && *e < 128 && isspace(*e)) {
959 if (p > node->mChars.string()) {
962 if (e < (node->mChars.string()+node->mChars.size()-1)) {
965 if (p > node->mChars.string() ||
966 e < (node->mChars.string()+node->mChars.size()-1)) {
967 String16 tmp(p, e-p+1);
972 node->removeWhitespace(stripAll, cDataTags);
977 status_t XMLNode::parseValues(const sp<AaptAssets>& assets,
978 ResourceTable* table)
980 bool hasErrors = false;
982 if (getType() == TYPE_ELEMENT) {
983 const size_t N = mAttributes.size();
984 String16 defPackage(assets->getPackage());
985 for (size_t i=0; i<N; i++) {
986 attribute_entry& e = mAttributes.editItemAt(i);
987 AccessorCookie ac(SourcePos(mFilename, getStartLineNumber()), String8(e.name),
989 table->setCurrentXmlPos(SourcePos(mFilename, getStartLineNumber()));
990 if (!assets->getIncludedResources()
991 .stringToValue(&e.value, &e.string,
992 e.string.string(), e.string.size(), true, true,
993 e.nameResId, NULL, &defPackage, table, &ac)) {
997 printf("Attr %s: type=0x%x, str=%s\n",
998 String8(e.name).string(), e.value.dataType,
999 String8(e.string).string());
1003 const size_t N = mChildren.size();
1004 for (size_t i=0; i<N; i++) {
1005 status_t err = mChildren.itemAt(i)->parseValues(assets, table);
1006 if (err != NO_ERROR) {
1010 return hasErrors ? STATUST(UNKNOWN_ERROR) : NO_ERROR;
1013 status_t XMLNode::assignResourceIds(const sp<AaptAssets>& assets,
1014 const ResourceTable* table)
1016 bool hasErrors = false;
1018 if (getType() == TYPE_ELEMENT) {
1019 String16 attr("attr");
1020 const char* errorMsg;
1021 const size_t N = mAttributes.size();
1022 for (size_t i=0; i<N; i++) {
1023 const attribute_entry& e = mAttributes.itemAt(i);
1024 if (e.ns.size() <= 0) continue;
1025 bool nsIsPublic = true;
1026 String16 pkg(getNamespaceResourcePackage(String16(assets->getPackage()), e.ns, &nsIsPublic));
1028 printf("Elem %s %s=\"%s\": namespace(%s) %s ===> %s\n",
1029 String8(getElementName()).string(),
1030 String8(e.name).string(),
1031 String8(e.string).string(),
1032 String8(e.ns).string(),
1033 (nsIsPublic) ? "public" : "private",
1034 String8(pkg).string());
1036 if (pkg.size() <= 0) continue;
1037 uint32_t res = table != NULL
1038 ? table->getResId(e.name, &attr, &pkg, &errorMsg, nsIsPublic)
1039 : assets->getIncludedResources().
1040 identifierForName(e.name.string(), e.name.size(),
1041 attr.string(), attr.size(),
1042 pkg.string(), pkg.size());
1045 printf("XML attribute name %s: resid=0x%08x\n",
1046 String8(e.name).string(), res);
1048 setAttributeResID(i, res);
1050 SourcePos(mFilename, getStartLineNumber()).error(
1051 "No resource identifier found for attribute '%s' in package '%s'\n",
1052 String8(e.name).string(), String8(pkg).string());
1057 const size_t N = mChildren.size();
1058 for (size_t i=0; i<N; i++) {
1059 status_t err = mChildren.itemAt(i)->assignResourceIds(assets, table);
1060 if (err < NO_ERROR) {
1065 return hasErrors ? STATUST(UNKNOWN_ERROR) : NO_ERROR;
1068 sp<XMLNode> XMLNode::clone() const {
1069 sp<XMLNode> copy = new XMLNode();
1070 copy->mNamespacePrefix = mNamespacePrefix;
1071 copy->mNamespaceUri = mNamespaceUri;
1072 copy->mElementName = mElementName;
1074 const size_t childCount = mChildren.size();
1075 for (size_t i = 0; i < childCount; i++) {
1076 copy->mChildren.add(mChildren[i]->clone());
1079 copy->mAttributes = mAttributes;
1080 copy->mAttributeOrder = mAttributeOrder;
1081 copy->mNextAttributeIndex = mNextAttributeIndex;
1082 copy->mChars = mChars;
1083 memcpy(©->mCharsValue, &mCharsValue, sizeof(mCharsValue));
1084 copy->mComment = mComment;
1085 copy->mFilename = mFilename;
1086 copy->mStartLineNumber = mStartLineNumber;
1087 copy->mEndLineNumber = mEndLineNumber;
1088 copy->mUTF8 = mUTF8;
1092 status_t XMLNode::flatten(const sp<AaptFile>& dest,
1093 bool stripComments, bool stripRawValues) const
1095 StringPool strings(mUTF8);
1096 Vector<uint32_t> resids;
1098 // First collect just the strings for attribute names that have a
1099 // resource ID assigned to them. This ensures that the resource ID
1100 // array is compact, and makes it easier to deal with attribute names
1101 // in different namespaces (and thus with different resource IDs).
1102 collect_resid_strings(&strings, &resids);
1104 // Next collect all remainibng strings.
1105 collect_strings(&strings, &resids, stripComments, stripRawValues);
1107 sp<AaptFile> stringPool = strings.createStringBlock();
1109 ResXMLTree_header header;
1110 memset(&header, 0, sizeof(header));
1111 header.header.type = htods(RES_XML_TYPE);
1112 header.header.headerSize = htods(sizeof(header));
1114 const size_t basePos = dest->getSize();
1115 dest->writeData(&header, sizeof(header));
1116 dest->writeData(stringPool->getData(), stringPool->getSize());
1118 // If we have resource IDs, write them.
1119 if (resids.size() > 0) {
1120 const size_t resIdsPos = dest->getSize();
1121 const size_t resIdsSize =
1122 sizeof(ResChunk_header)+(sizeof(uint32_t)*resids.size());
1123 ResChunk_header* idsHeader = (ResChunk_header*)
1124 (((const uint8_t*)dest->editData(resIdsPos+resIdsSize))+resIdsPos);
1125 idsHeader->type = htods(RES_XML_RESOURCE_MAP_TYPE);
1126 idsHeader->headerSize = htods(sizeof(*idsHeader));
1127 idsHeader->size = htodl(resIdsSize);
1128 uint32_t* ids = (uint32_t*)(idsHeader+1);
1129 for (size_t i=0; i<resids.size(); i++) {
1130 *ids++ = htodl(resids[i]);
1134 flatten_node(strings, dest, stripComments, stripRawValues);
1136 void* data = dest->editData();
1137 ResXMLTree_header* hd = (ResXMLTree_header*)(((uint8_t*)data)+basePos);
1138 hd->header.size = htodl(dest->getSize()-basePos);
1140 if (kPrintStringMetrics) {
1141 fprintf(stderr, "**** total xml size: %zu / %zu%% strings (in %s)\n",
1142 dest->getSize(), (stringPool->getSize()*100)/dest->getSize(),
1143 dest->getPath().string());
1149 void XMLNode::print(int indent)
1153 for (i=0; i<indent; i++) {
1156 if (getType() == TYPE_ELEMENT) {
1157 String8 elemNs(getNamespaceUri());
1158 if (elemNs.size() > 0) {
1161 printf("%s E: %s%s", prefix.string(),
1162 elemNs.string(), String8(getElementName()).string());
1163 int N = mAttributes.size();
1164 for (i=0; i<N; i++) {
1165 ssize_t idx = mAttributeOrder.valueAt(i);
1171 const attribute_entry& attr = mAttributes.itemAt(idx);
1172 String8 attrNs(attr.ns);
1173 if (attrNs.size() > 0) {
1176 if (attr.nameResId) {
1177 printf("%s%s(0x%08x)", attrNs.string(),
1178 String8(attr.name).string(), attr.nameResId);
1180 printf("%s%s", attrNs.string(), String8(attr.name).string());
1182 printf("=%s", String8(attr.string).string());
1185 } else if (getType() == TYPE_NAMESPACE) {
1186 printf("%s N: %s=%s\n", prefix.string(),
1187 getNamespacePrefix().size() > 0
1188 ? String8(getNamespacePrefix()).string() : "<DEF>",
1189 String8(getNamespaceUri()).string());
1191 printf("%s C: \"%s\"\n", prefix.string(), String8(getCData()).string());
1193 int N = mChildren.size();
1194 for (i=0; i<N; i++) {
1195 mChildren.itemAt(i)->print(indent+1);
1199 static void splitName(const char* name, String16* outNs, String16* outName)
1201 const char* p = name;
1202 while (*p != 0 && *p != 1) {
1206 *outNs = String16();
1207 *outName = String16(name);
1209 *outNs = String16(name, (p-name));
1210 *outName = String16(p+1);
1215 XMLNode::startNamespace(void *userData, const char *prefix, const char *uri)
1217 if (kIsDebugParse) {
1218 printf("Start Namespace: %s %s\n", prefix, uri);
1220 ParseState* st = (ParseState*)userData;
1221 sp<XMLNode> node = XMLNode::newNamespace(st->filename,
1222 String16(prefix != NULL ? prefix : ""), String16(uri));
1223 node->setStartLineNumber(XML_GetCurrentLineNumber(st->parser));
1224 if (st->stack.size() > 0) {
1225 st->stack.itemAt(st->stack.size()-1)->addChild(node);
1229 st->stack.push(node);
1233 XMLNode::startElement(void *userData, const char *name, const char **atts)
1235 if (kIsDebugParse) {
1236 printf("Start Element: %s\n", name);
1238 ParseState* st = (ParseState*)userData;
1239 String16 ns16, name16;
1240 splitName(name, &ns16, &name16);
1241 sp<XMLNode> node = XMLNode::newElement(st->filename, ns16, name16);
1242 node->setStartLineNumber(XML_GetCurrentLineNumber(st->parser));
1243 if (st->pendingComment.size() > 0) {
1244 node->appendComment(st->pendingComment);
1245 st->pendingComment = String16();
1247 if (st->stack.size() > 0) {
1248 st->stack.itemAt(st->stack.size()-1)->addChild(node);
1252 st->stack.push(node);
1254 for (int i = 0; atts[i]; i += 2) {
1255 splitName(atts[i], &ns16, &name16);
1256 node->addAttribute(ns16, name16, String16(atts[i+1]));
1261 XMLNode::characterData(void *userData, const XML_Char *s, int len)
1263 if (kIsDebugParse) {
1264 printf("CDATA: \"%s\"\n", String8(s, len).string());
1266 ParseState* st = (ParseState*)userData;
1267 sp<XMLNode> node = NULL;
1268 if (st->stack.size() == 0) {
1271 sp<XMLNode> parent = st->stack.itemAt(st->stack.size()-1);
1272 if (parent != NULL && parent->getChildren().size() > 0) {
1273 node = parent->getChildren()[parent->getChildren().size()-1];
1274 if (node->getType() != TYPE_CDATA) {
1275 // Last node is not CDATA, need to make a new node.
1281 node = XMLNode::newCData(st->filename);
1282 node->setStartLineNumber(XML_GetCurrentLineNumber(st->parser));
1283 parent->addChild(node);
1286 node->appendChars(String16(s, len));
1290 XMLNode::endElement(void *userData, const char *name)
1292 if (kIsDebugParse) {
1293 printf("End Element: %s\n", name);
1295 ParseState* st = (ParseState*)userData;
1296 sp<XMLNode> node = st->stack.itemAt(st->stack.size()-1);
1297 node->setEndLineNumber(XML_GetCurrentLineNumber(st->parser));
1298 if (st->pendingComment.size() > 0) {
1299 node->appendComment(st->pendingComment);
1300 st->pendingComment = String16();
1302 String16 ns16, name16;
1303 splitName(name, &ns16, &name16);
1304 LOG_ALWAYS_FATAL_IF(node->getElementNamespace() != ns16
1305 || node->getElementName() != name16,
1306 "Bad end element %s", name);
1311 XMLNode::endNamespace(void *userData, const char *prefix)
1313 const char* nonNullPrefix = prefix != NULL ? prefix : "";
1314 if (kIsDebugParse) {
1315 printf("End Namespace: %s\n", prefix);
1317 ParseState* st = (ParseState*)userData;
1318 sp<XMLNode> node = st->stack.itemAt(st->stack.size()-1);
1319 node->setEndLineNumber(XML_GetCurrentLineNumber(st->parser));
1320 LOG_ALWAYS_FATAL_IF(node->getNamespacePrefix() != String16(nonNullPrefix),
1321 "Bad end namespace %s", prefix);
1326 XMLNode::commentData(void *userData, const char *comment)
1328 if (kIsDebugParse) {
1329 printf("Comment: %s\n", comment);
1331 ParseState* st = (ParseState*)userData;
1332 if (st->pendingComment.size() > 0) {
1333 st->pendingComment.append(String16("\n"));
1335 st->pendingComment.append(String16(comment));
1338 status_t XMLNode::collect_strings(StringPool* dest, Vector<uint32_t>* outResIds,
1339 bool stripComments, bool stripRawValues) const
1341 collect_attr_strings(dest, outResIds, true);
1344 if (RESOURCES_TOOLS_NAMESPACE != mNamespaceUri) {
1345 if (mNamespacePrefix.size() > 0) {
1346 dest->add(mNamespacePrefix, true);
1348 if (mNamespaceUri.size() > 0) {
1349 dest->add(mNamespaceUri, true);
1352 if (mElementName.size() > 0) {
1353 dest->add(mElementName, true);
1356 if (!stripComments && mComment.size() > 0) {
1357 dest->add(mComment, true);
1360 const int NA = mAttributes.size();
1362 for (i=0; i<NA; i++) {
1363 const attribute_entry& ae = mAttributes.itemAt(i);
1364 if (ae.ns.size() > 0) {
1365 dest->add(ae.ns, true);
1367 if (!stripRawValues || ae.needStringValue()) {
1368 dest->add(ae.string, true);
1371 if (ae.value.dataType == Res_value::TYPE_NULL
1372 || ae.value.dataType == Res_value::TYPE_STRING) {
1373 dest->add(ae.string, true);
1378 if (mElementName.size() == 0) {
1379 // If not an element, include the CDATA, even if it is empty.
1380 dest->add(mChars, true);
1383 const int NC = mChildren.size();
1385 for (i=0; i<NC; i++) {
1386 mChildren.itemAt(i)->collect_strings(dest, outResIds,
1387 stripComments, stripRawValues);
1393 status_t XMLNode::collect_attr_strings(StringPool* outPool,
1394 Vector<uint32_t>* outResIds, bool allAttrs) const {
1395 const int NA = mAttributes.size();
1397 for (int i=0; i<NA; i++) {
1398 const attribute_entry& attr = mAttributes.itemAt(i);
1399 uint32_t id = attr.nameResId;
1400 if (id || allAttrs) {
1401 // See if we have already assigned this resource ID to a pooled
1403 const Vector<size_t>* indices = outPool->offsetsForString(attr.name);
1405 if (indices != NULL) {
1406 const int NJ = indices->size();
1407 const size_t NR = outResIds->size();
1408 for (int j=0; j<NJ; j++) {
1409 size_t strIdx = indices->itemAt(j);
1412 // We don't need to assign a resource ID for this one.
1416 // Just ignore strings that are out of range of
1417 // the currently assigned resource IDs... we add
1418 // strings as we assign the first ID.
1419 } else if (outResIds->itemAt(strIdx) == id) {
1426 idx = outPool->add(attr.name);
1428 printf("Adding attr %s (resid 0x%08x) to pool: idx=%zd\n",
1429 String8(attr.name).string(), id, SSIZE(idx));
1432 while ((ssize_t)outResIds->size() <= idx) {
1435 outResIds->replaceAt(id, idx);
1438 attr.namePoolIdx = idx;
1440 printf("String %s offset=0x%08zd\n", String8(attr.name).string(), SSIZE(idx));
1448 status_t XMLNode::collect_resid_strings(StringPool* outPool,
1449 Vector<uint32_t>* outResIds) const
1451 collect_attr_strings(outPool, outResIds, false);
1453 const int NC = mChildren.size();
1455 for (int i=0; i<NC; i++) {
1456 mChildren.itemAt(i)->collect_resid_strings(outPool, outResIds);
1462 status_t XMLNode::flatten_node(const StringPool& strings, const sp<AaptFile>& dest,
1463 bool stripComments, bool stripRawValues) const
1465 ResXMLTree_node node;
1466 ResXMLTree_cdataExt cdataExt;
1467 ResXMLTree_namespaceExt namespaceExt;
1468 ResXMLTree_attrExt attrExt;
1469 const void* extData = NULL;
1471 ResXMLTree_attribute attr;
1472 bool writeCurrentNode = true;
1474 const size_t NA = mAttributes.size();
1475 const size_t NC = mChildren.size();
1478 LOG_ALWAYS_FATAL_IF(NA != mAttributeOrder.size(), "Attributes messed up!");
1480 const String16 id16("id");
1481 const String16 class16("class");
1482 const String16 style16("style");
1484 const type type = getType();
1486 memset(&node, 0, sizeof(node));
1487 memset(&attr, 0, sizeof(attr));
1488 node.header.headerSize = htods(sizeof(node));
1489 node.lineNumber = htodl(getStartLineNumber());
1490 if (!stripComments) {
1491 node.comment.index = htodl(
1492 mComment.size() > 0 ? strings.offsetForString(mComment) : -1);
1493 //if (mComment.size() > 0) {
1494 // printf("Flattening comment: %s\n", String8(mComment).string());
1497 node.comment.index = htodl((uint32_t)-1);
1499 if (type == TYPE_ELEMENT) {
1500 node.header.type = htods(RES_XML_START_ELEMENT_TYPE);
1502 extSize = sizeof(attrExt);
1503 memset(&attrExt, 0, sizeof(attrExt));
1504 if (mNamespaceUri.size() > 0) {
1505 attrExt.ns.index = htodl(strings.offsetForString(mNamespaceUri));
1507 attrExt.ns.index = htodl((uint32_t)-1);
1509 attrExt.name.index = htodl(strings.offsetForString(mElementName));
1510 attrExt.attributeStart = htods(sizeof(attrExt));
1511 attrExt.attributeSize = htods(sizeof(attr));
1512 attrExt.attributeCount = htods(NA);
1513 attrExt.idIndex = htods(0);
1514 attrExt.classIndex = htods(0);
1515 attrExt.styleIndex = htods(0);
1516 for (i=0; i<NA; i++) {
1517 ssize_t idx = mAttributeOrder.valueAt(i);
1518 const attribute_entry& ae = mAttributes.itemAt(idx);
1519 if (ae.ns.size() == 0) {
1520 if (ae.name == id16) {
1521 attrExt.idIndex = htods(i+1);
1522 } else if (ae.name == class16) {
1523 attrExt.classIndex = htods(i+1);
1524 } else if (ae.name == style16) {
1525 attrExt.styleIndex = htods(i+1);
1529 } else if (type == TYPE_NAMESPACE) {
1530 if (mNamespaceUri == RESOURCES_TOOLS_NAMESPACE) {
1531 writeCurrentNode = false;
1533 node.header.type = htods(RES_XML_START_NAMESPACE_TYPE);
1534 extData = &namespaceExt;
1535 extSize = sizeof(namespaceExt);
1536 memset(&namespaceExt, 0, sizeof(namespaceExt));
1537 if (mNamespacePrefix.size() > 0) {
1538 namespaceExt.prefix.index = htodl(strings.offsetForString(mNamespacePrefix));
1540 namespaceExt.prefix.index = htodl((uint32_t)-1);
1542 namespaceExt.prefix.index = htodl(strings.offsetForString(mNamespacePrefix));
1543 namespaceExt.uri.index = htodl(strings.offsetForString(mNamespaceUri));
1545 LOG_ALWAYS_FATAL_IF(NA != 0, "Namespace nodes can't have attributes!");
1546 } else if (type == TYPE_CDATA) {
1547 node.header.type = htods(RES_XML_CDATA_TYPE);
1548 extData = &cdataExt;
1549 extSize = sizeof(cdataExt);
1550 memset(&cdataExt, 0, sizeof(cdataExt));
1551 cdataExt.data.index = htodl(strings.offsetForString(mChars));
1552 cdataExt.typedData.size = htods(sizeof(cdataExt.typedData));
1553 cdataExt.typedData.res0 = 0;
1554 cdataExt.typedData.dataType = mCharsValue.dataType;
1555 cdataExt.typedData.data = htodl(mCharsValue.data);
1556 LOG_ALWAYS_FATAL_IF(NA != 0, "CDATA nodes can't have attributes!");
1559 node.header.size = htodl(sizeof(node) + extSize + (sizeof(attr)*NA));
1561 if (writeCurrentNode) {
1562 dest->writeData(&node, sizeof(node));
1564 dest->writeData(extData, extSize);
1568 for (i=0; i<NA; i++) {
1569 ssize_t idx = mAttributeOrder.valueAt(i);
1570 const attribute_entry& ae = mAttributes.itemAt(idx);
1571 if (ae.ns.size() > 0) {
1572 attr.ns.index = htodl(strings.offsetForString(ae.ns));
1574 attr.ns.index = htodl((uint32_t)-1);
1576 attr.name.index = htodl(ae.namePoolIdx);
1578 if (!stripRawValues || ae.needStringValue()) {
1579 attr.rawValue.index = htodl(strings.offsetForString(ae.string));
1581 attr.rawValue.index = htodl((uint32_t)-1);
1583 attr.typedValue.size = htods(sizeof(attr.typedValue));
1584 if (ae.value.dataType == Res_value::TYPE_NULL
1585 || ae.value.dataType == Res_value::TYPE_STRING) {
1586 attr.typedValue.res0 = 0;
1587 attr.typedValue.dataType = Res_value::TYPE_STRING;
1588 attr.typedValue.data = htodl(strings.offsetForString(ae.string));
1590 attr.typedValue.res0 = 0;
1591 attr.typedValue.dataType = ae.value.dataType;
1592 attr.typedValue.data = htodl(ae.value.data);
1594 dest->writeData(&attr, sizeof(attr));
1597 for (i=0; i<NC; i++) {
1598 status_t err = mChildren.itemAt(i)->flatten_node(strings, dest,
1599 stripComments, stripRawValues);
1600 if (err != NO_ERROR) {
1605 if (type == TYPE_ELEMENT) {
1606 ResXMLTree_endElementExt endElementExt;
1607 memset(&endElementExt, 0, sizeof(endElementExt));
1608 node.header.type = htods(RES_XML_END_ELEMENT_TYPE);
1609 node.header.size = htodl(sizeof(node)+sizeof(endElementExt));
1610 node.lineNumber = htodl(getEndLineNumber());
1611 node.comment.index = htodl((uint32_t)-1);
1612 endElementExt.ns.index = attrExt.ns.index;
1613 endElementExt.name.index = attrExt.name.index;
1614 dest->writeData(&node, sizeof(node));
1615 dest->writeData(&endElementExt, sizeof(endElementExt));
1616 } else if (type == TYPE_NAMESPACE) {
1617 if (writeCurrentNode) {
1618 node.header.type = htods(RES_XML_END_NAMESPACE_TYPE);
1619 node.lineNumber = htodl(getEndLineNumber());
1620 node.comment.index = htodl((uint32_t)-1);
1621 node.header.size = htodl(sizeof(node)+extSize);
1622 dest->writeData(&node, sizeof(node));
1623 dest->writeData(extData, extSize);