2 // Copyright 2006 The Android Open Source Project
4 // Build resource files from raw assets.
8 #include "ResourceTable.h"
10 #include <host/pseudolocalize.h>
11 #include <utils/ByteOrder.h>
15 #ifndef HAVE_MS_C_RUNTIME
20 #define NOISY_PARSE(x) //x
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/";
26 const char* const XLIFF_XMLNS
= "urn:oasis:names:tc:xliff:document:1.2";
27 const char* const ALLOWED_XLIFF_ELEMENTS
[] = {
38 bool isWhitespace(const char16_t* str
)
40 while (*str
!= 0 && *str
< 128 && isspace(*str
)) {
46 static const String16
RESOURCES_PREFIX(RESOURCES_ROOT_NAMESPACE
);
47 static const String16
RESOURCES_PRV_PREFIX(RESOURCES_ROOT_PRV_NAMESPACE
);
49 String16
getNamespaceResourcePackage(String16 namespaceUri
, bool* outIsPublic
)
51 //printf("%s starts with %s?\n", String8(namespaceUri).string(),
52 // String8(RESOURCES_PREFIX).string());
55 if (namespaceUri
.startsWith(RESOURCES_PREFIX
)) {
56 prefixSize
= RESOURCES_PREFIX
.size();
57 } else if (namespaceUri
.startsWith(RESOURCES_PRV_PREFIX
)) {
59 prefixSize
= RESOURCES_PRV_PREFIX
.size();
61 if (outIsPublic
) *outIsPublic
= isPublic
; // = true
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
);
71 status_t
parseStyledString(Bundle
* bundle
,
74 const String16
& endTag
,
76 Vector
<StringPool::entry_style_span
>* outSpans
,
79 Vector
<StringPool::entry_style_span
> spanStack
;
84 bool firstTime
= true;
87 ResXMLTree::event_code_t code
;
88 while ((code
=inXml
->next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
90 if (code
== ResXMLTree::TEXT
) {
91 String16
text(inXml
->getText(&len
));
92 if (firstTime
&& text
.size() > 0) {
94 if (text
.string()[0] == '@') {
95 // If this is a resource reference, don't do the pseudoloc.
96 pseudolocalize
= false;
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())));
104 curString
.append(text
);
106 } else if (code
== ResXMLTree::START_TAG
) {
107 const String16
element16(inXml
->getElementName(&len
));
108 const String8
element8(element16
);
111 const uint16_t* ns
= inXml
->getElementNamespace(&nslen
);
113 ns
= (const uint16_t*)"\0\0";
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
]) {
122 // in this case, treat it like it was just text, in other words, do nothing
123 // here and silently drop this element
128 SourcePos(String8(fileName
), inXml
->getLineNumber()).error(
129 "Found unsupported XLIFF tag <%s>\n",
131 return UNKNOWN_ERROR
;
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
;
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
;
149 rawString
.append(curString
);
150 curString
= String16();
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
);
162 //printf("Span: %s\n", String8(span.name).string());
163 span
.span
.firstChar
= span
.span
.lastChar
= outString
->size();
164 spanStack
.push(span
);
166 } else if (code
== ResXMLTree::END_TAG
) {
168 const uint16_t* ns
= inXml
->getElementNamespace(&nslen
);
170 ns
= (const uint16_t*)"\0\0";
173 const String8
nspace(String16(ns
, nslen
));
174 if (nspace
== XLIFF_XMLNS
) {
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
;
184 rawString
.append(curString
);
185 curString
= String16();
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
;
197 StringPool::entry_style_span span
= spanStack
.top();
199 ssize_t semi
= span
.name
.findFirst(';');
201 spanTag
.setTo(span
.name
.string(), semi
);
203 spanTag
.setTo(span
.name
);
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
;
213 if (outString
->size() > 0) {
214 span
.span
.lastChar
= outString
->size()-1;
215 if (span
.span
.lastChar
>= span
.span
.firstChar
) {
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());
228 } else if (code
== ResXMLTree::START_NAMESPACE
) {
233 if (code
== ResXMLTree::BAD_DOCUMENT
) {
234 SourcePos(String8(fileName
), inXml
->getLineNumber()).error(
235 "Error parsing XML\n");
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(
244 errorMsg
, String8(curString
).string());
245 return UNKNOWN_ERROR
;
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
);
259 struct namespace_entry
{
264 static String8
make_prefix(int depth
)
268 for (i
=0; i
<depth
; i
++) {
274 static String8
build_namespace(const Vector
<namespace_entry
>& namespaces
,
280 const size_t N
= namespaces
.size();
281 for (size_t i
=0; i
<N
; i
++) {
282 const namespace_entry
& ne
= namespaces
.itemAt(i
);
293 void printXMLBlock(ResXMLTree
* block
)
297 Vector
<namespace_entry
> namespaces
;
299 ResXMLTree::event_code_t code
;
301 while ((code
=block
->next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
302 String8 prefix
= make_prefix(depth
);
304 if (code
== ResXMLTree::START_TAG
) {
306 const uint16_t* ns16
= block
->getElementNamespace(&len
);
307 String8 elemNs
= build_namespace(namespaces
, ns16
);
308 const uint16_t* com16
= block
->getComment(&len
);
310 printf("%s <!-- %s -->\n", prefix
.string(), String8(com16
).string());
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();
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());
325 printf("%s%s(0x%08x)", ns
.string(), name
.string(), res
);
327 printf("%s%s", ns
.string(), name
.string());
330 block
->getAttributeValue(i
, &value
);
331 if (value
.dataType
== Res_value::TYPE_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
) {
339 String8(block
->getAttributeStringValue(i
, &len
)).string());
341 printf("=(type 0x%x)0x%x", (int)value
.dataType
, (int)value
.data
);
343 const char16_t* val
= block
->getAttributeStringValue(i
, &len
);
345 printf(" (Raw: \"%s\")", String8(val
).string());
349 } else if (code
== ResXMLTree::END_TAG
) {
351 } else if (code
== ResXMLTree::START_NAMESPACE
) {
354 const uint16_t* prefix16
= block
->getNamespacePrefix(&len
);
356 ns
.prefix
= String8(prefix16
);
360 ns
.uri
= String8(block
->getNamespaceUri(&len
));
362 printf("%sN: %s=%s\n", prefix
.string(), ns
.prefix
.string(),
365 } else if (code
== ResXMLTree::END_NAMESPACE
) {
367 const namespace_entry
& ns
= namespaces
.top();
369 const uint16_t* prefix16
= block
->getNamespacePrefix(&len
);
372 pr
= String8(prefix16
);
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());
381 String8 uri
= String8(block
->getNamespaceUri(&len
));
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());
388 } else if (code
== ResXMLTree::TEXT
) {
390 printf("%sC: \"%s\"\n", prefix
.string(), String8(block
->getText(&len
)).string());
397 status_t
parseXMLResource(const sp
<AaptFile
>& file
, ResXMLTree
* outTree
,
398 bool stripAll
, bool keepComments
,
399 const char** cDataTags
)
401 sp
<XMLNode
> root
= XMLNode::parse(file
);
403 return UNKNOWN_ERROR
;
405 root
->removeWhitespace(stripAll
, cDataTags
);
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
) {
414 err
= outTree
->setTo(rsc
->getData(), rsc
->getSize(), true);
415 if (err
!= NO_ERROR
) {
419 NOISY(printf("Output XML:\n"));
420 NOISY(printXMLBlock(outTree
));
425 sp
<XMLNode
> XMLNode::parse(const sp
<AaptFile
>& file
)
428 int fd
= open(file
->getSourceFile().string(), O_RDONLY
| O_BINARY
);
430 SourcePos(file
->getSourceFile(), -1).error("Unable to open file for read: %s",
435 XML_Parser parser
= XML_ParserCreateNS(NULL
, 1);
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
);
448 len
= read(fd
, buf
, sizeof(buf
));
449 done
= len
< (ssize_t
)sizeof(buf
);
451 SourcePos(file
->getSourceFile(), -1).error("Error reading file: %s\n", strerror(errno
));
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
)));
463 XML_ParserFree(parser
);
464 if (state
.root
== NULL
) {
465 SourcePos(file
->getSourceFile(), -1).error("No XML data generated when parsing");
471 XMLNode::XMLNode(const String8
& filename
, const String16
& s1
, const String16
& s2
, bool isNamespace
)
472 : mNextAttributeIndex(0x80000000)
473 , mFilename(filename
)
474 , mStartLineNumber(0)
478 mNamespacePrefix
= s1
;
486 XMLNode::XMLNode(const String8
& filename
)
487 : mFilename(filename
)
489 memset(&mCharsValue
, 0, sizeof(mCharsValue
));
492 XMLNode::type
XMLNode::getType() const
494 if (mElementName
.size() != 0) {
497 if (mNamespaceUri
.size() != 0) {
498 return TYPE_NAMESPACE
;
503 const String16
& XMLNode::getNamespacePrefix() const
505 return mNamespacePrefix
;
508 const String16
& XMLNode::getNamespaceUri() const
510 return mNamespaceUri
;
513 const String16
& XMLNode::getElementNamespace() const
515 return mNamespaceUri
;
518 const String16
& XMLNode::getElementName() const
523 const Vector
<sp
<XMLNode
> >& XMLNode::getChildren() const
528 const Vector
<XMLNode::attribute_entry
>&
529 XMLNode::getAttributes() const
534 const String16
& XMLNode::getCData() const
539 const String16
& XMLNode::getComment() const
544 int32_t XMLNode::getStartLineNumber() const
546 return mStartLineNumber
;
549 int32_t XMLNode::getEndLineNumber() const
551 return mEndLineNumber
;
554 status_t
XMLNode::addChild(const sp
<XMLNode
>& child
)
556 if (getType() == TYPE_CDATA
) {
557 SourcePos(mFilename
, child
->getStartLineNumber()).error("Child to CDATA node.");
558 return UNKNOWN_ERROR
;
560 //printf("Adding child %p to parent %p\n", child.get(), this);
561 mChildren
.add(child
);
565 status_t
XMLNode::addAttribute(const String16
& ns
, const String16
& name
,
566 const String16
& value
)
568 if (getType() == TYPE_CDATA
) {
569 SourcePos(mFilename
, getStartLineNumber()).error("Child to CDATA node.");
570 return UNKNOWN_ERROR
;
573 e
.index
= mNextAttributeIndex
++;
578 mAttributeOrder
.add(e
.index
, mAttributes
.size()-1);
582 void XMLNode::setAttributeResID(size_t attrIdx
, uint32_t resId
)
584 attribute_entry
& e
= mAttributes
.editItemAt(attrIdx
);
586 mAttributeOrder
.removeItem(e
.nameResId
);
588 mAttributeOrder
.removeItem(e
.index
);
590 NOISY(printf("Elem %s %s=\"%s\": set res id = 0x%08x\n",
591 String8(getElementName()).string(),
592 String8(mAttributes
.itemAt(attrIdx
).name
).string(),
593 String8(mAttributes
.itemAt(attrIdx
).string
).string(),
595 mAttributes
.editItemAt(attrIdx
).nameResId
= resId
;
596 mAttributeOrder
.add(resId
, attrIdx
);
599 status_t
XMLNode::appendChars(const String16
& chars
)
601 if (getType() != TYPE_CDATA
) {
602 SourcePos(mFilename
, getStartLineNumber()).error("Adding characters to element node.");
603 return UNKNOWN_ERROR
;
605 mChars
.append(chars
);
609 status_t
XMLNode::appendComment(const String16
& comment
)
611 if (mComment
.size() > 0) {
612 mComment
.append(String16("\n"));
614 mComment
.append(comment
);
618 void XMLNode::setStartLineNumber(int32_t line
)
620 mStartLineNumber
= line
;
623 void XMLNode::setEndLineNumber(int32_t line
)
625 mEndLineNumber
= line
;
628 void XMLNode::removeWhitespace(bool stripAll
, const char** cDataTags
)
630 //printf("Removing whitespace in %s\n", String8(mElementName).string());
631 size_t N
= mChildren
.size();
633 String8
tag(mElementName
);
634 const char** p
= cDataTags
;
642 for (size_t i
=0; i
<N
; i
++) {
643 sp
<XMLNode
> node
= mChildren
.itemAt(i
);
644 if (node
->getType() == TYPE_CDATA
) {
645 // This is a CDATA node...
646 const char16_t* p
= node
->mChars
.string();
647 while (*p
!= 0 && *p
< 128 && isspace(*p
)) {
650 //printf("Space ends at %d in \"%s\"\n",
651 // (int)(p-node->mChars.string()),
652 // String8(node->mChars).string());
656 mChildren
.removeAt(i
);
660 node
->mChars
= String16(" ");
663 // Compact leading/trailing whitespace.
664 const char16_t* e
= node
->mChars
.string()+node
->mChars
.size()-1;
665 while (e
> p
&& *e
< 128 && isspace(*e
)) {
668 if (p
> node
->mChars
.string()) {
671 if (e
< (node
->mChars
.string()+node
->mChars
.size()-1)) {
674 if (p
> node
->mChars
.string() ||
675 e
< (node
->mChars
.string()+node
->mChars
.size()-1)) {
676 String16
tmp(p
, e
-p
+1);
681 node
->removeWhitespace(stripAll
, cDataTags
);
686 status_t
XMLNode::parseValues(const sp
<AaptAssets
>& assets
,
687 ResourceTable
* table
)
689 bool hasErrors
= false;
691 if (getType() == TYPE_ELEMENT
) {
692 const size_t N
= mAttributes
.size();
693 String16
defPackage(assets
->getPackage());
694 for (size_t i
=0; i
<N
; i
++) {
695 attribute_entry
& e
= mAttributes
.editItemAt(i
);
696 AccessorCookie
ac(SourcePos(mFilename
, getStartLineNumber()), String8(e
.name
),
698 table
->setCurrentXmlPos(SourcePos(mFilename
, getStartLineNumber()));
699 if (!assets
->getIncludedResources()
700 .stringToValue(&e
.value
, &e
.string
,
701 e
.string
.string(), e
.string
.size(), true, true,
702 e
.nameResId
, NULL
, &defPackage
, table
, &ac
)) {
705 NOISY(printf("Attr %s: type=0x%x, str=%s\n",
706 String8(e
.name
).string(), e
.value
.dataType
,
707 String8(e
.string
).string()));
710 const size_t N
= mChildren
.size();
711 for (size_t i
=0; i
<N
; i
++) {
712 status_t err
= mChildren
.itemAt(i
)->parseValues(assets
, table
);
713 if (err
!= NO_ERROR
) {
717 return hasErrors
? UNKNOWN_ERROR
: NO_ERROR
;
720 status_t
XMLNode::assignResourceIds(const sp
<AaptAssets
>& assets
,
721 const ResourceTable
* table
)
723 bool hasErrors
= false;
725 if (getType() == TYPE_ELEMENT
) {
726 String16
attr("attr");
727 const char* errorMsg
;
728 const size_t N
= mAttributes
.size();
729 for (size_t i
=0; i
<N
; i
++) {
730 const attribute_entry
& e
= mAttributes
.itemAt(i
);
731 if (e
.ns
.size() <= 0) continue;
733 String16
pkg(getNamespaceResourcePackage(e
.ns
, &nsIsPublic
));
734 NOISY(printf("Elem %s %s=\"%s\": namespace(%s) %s ===> %s\n",
735 String8(getElementName()).string(),
736 String8(e
.name
).string(),
737 String8(e
.string
).string(),
738 String8(e
.ns
).string(),
739 (nsIsPublic
) ? "public" : "private",
740 String8(pkg
).string()));
741 if (pkg
.size() <= 0) continue;
742 uint32_t res
= table
!= NULL
743 ? table
->getResId(e
.name
, &attr
, &pkg
, &errorMsg
, nsIsPublic
)
744 : assets
->getIncludedResources().
745 identifierForName(e
.name
.string(), e
.name
.size(),
746 attr
.string(), attr
.size(),
747 pkg
.string(), pkg
.size());
749 NOISY(printf("XML attribute name %s: resid=0x%08x\n",
750 String8(e
.name
).string(), res
));
751 setAttributeResID(i
, res
);
753 SourcePos(mFilename
, getStartLineNumber()).error(
754 "No resource identifier found for attribute '%s' in package '%s'\n",
755 String8(e
.name
).string(), String8(pkg
).string());
760 const size_t N
= mChildren
.size();
761 for (size_t i
=0; i
<N
; i
++) {
762 status_t err
= mChildren
.itemAt(i
)->assignResourceIds(assets
, table
);
763 if (err
< NO_ERROR
) {
768 return hasErrors
? UNKNOWN_ERROR
: NO_ERROR
;
771 status_t
XMLNode::flatten(const sp
<AaptFile
>& dest
,
772 bool stripComments
, bool stripRawValues
) const
775 Vector
<uint32_t> resids
;
777 // First collect just the strings for attribute names that have a
778 // resource ID assigned to them. This ensures that the resource ID
779 // array is compact, and makes it easier to deal with attribute names
780 // in different namespaces (and thus with different resource IDs).
781 collect_resid_strings(&strings
, &resids
);
783 // Next collect all remainibng strings.
784 collect_strings(&strings
, &resids
, stripComments
, stripRawValues
);
786 #if 0 // No longer compiles
787 NOISY(printf("Found strings:\n");
788 const size_t N
= strings
.size();
789 for (size_t i
=0; i
<N
; i
++) {
790 printf("%s\n", String8(strings
.entryAt(i
).string
).string());
795 sp
<AaptFile
> stringPool
= strings
.createStringBlock();
796 NOISY(aout
<< "String pool:"
797 << HexDump(stringPool
->getData(), stringPool
->getSize()) << endl
);
799 ResXMLTree_header header
;
800 memset(&header
, 0, sizeof(header
));
801 header
.header
.type
= htods(RES_XML_TYPE
);
802 header
.header
.headerSize
= htods(sizeof(header
));
804 const size_t basePos
= dest
->getSize();
805 dest
->writeData(&header
, sizeof(header
));
806 dest
->writeData(stringPool
->getData(), stringPool
->getSize());
808 // If we have resource IDs, write them.
809 if (resids
.size() > 0) {
810 const size_t resIdsPos
= dest
->getSize();
811 const size_t resIdsSize
=
812 sizeof(ResChunk_header
)+(sizeof(uint32_t)*resids
.size());
813 ResChunk_header
* idsHeader
= (ResChunk_header
*)
814 (((const uint8_t*)dest
->editData(resIdsPos
+resIdsSize
))+resIdsPos
);
815 idsHeader
->type
= htods(RES_XML_RESOURCE_MAP_TYPE
);
816 idsHeader
->headerSize
= htods(sizeof(*idsHeader
));
817 idsHeader
->size
= htodl(resIdsSize
);
818 uint32_t* ids
= (uint32_t*)(idsHeader
+1);
819 for (size_t i
=0; i
<resids
.size(); i
++) {
820 *ids
++ = htodl(resids
[i
]);
824 flatten_node(strings
, dest
, stripComments
, stripRawValues
);
826 void* data
= dest
->editData();
827 ResXMLTree_header
* hd
= (ResXMLTree_header
*)(((uint8_t*)data
)+basePos
);
828 size_t size
= dest
->getSize()-basePos
;
829 hd
->header
.size
= htodl(dest
->getSize()-basePos
);
831 NOISY(aout
<< "XML resource:"
832 << HexDump(dest
->getData(), dest
->getSize()) << endl
);
834 #if PRINT_STRING_METRICS
835 fprintf(stderr
, "**** total xml size: %d / %d%% strings (in %s)\n",
836 dest
->getSize(), (stringPool
->getSize()*100)/dest
->getSize(),
837 dest
->getPath().string());
843 void XMLNode::print(int indent
)
847 for (i
=0; i
<indent
; i
++) {
850 if (getType() == TYPE_ELEMENT
) {
851 String8
elemNs(getNamespaceUri());
852 if (elemNs
.size() > 0) {
855 printf("%s E: %s%s", prefix
.string(),
856 elemNs
.string(), String8(getElementName()).string());
857 int N
= mAttributes
.size();
858 for (i
=0; i
<N
; i
++) {
859 ssize_t idx
= mAttributeOrder
.valueAt(i
);
865 const attribute_entry
& attr
= mAttributes
.itemAt(idx
);
866 String8
attrNs(attr
.ns
);
867 if (attrNs
.size() > 0) {
870 if (attr
.nameResId
) {
871 printf("%s%s(0x%08x)", attrNs
.string(),
872 String8(attr
.name
).string(), attr
.nameResId
);
874 printf("%s%s", attrNs
.string(), String8(attr
.name
).string());
876 printf("=%s", String8(attr
.string
).string());
879 } else if (getType() == TYPE_NAMESPACE
) {
880 printf("%s N: %s=%s\n", prefix
.string(),
881 getNamespacePrefix().size() > 0
882 ? String8(getNamespacePrefix()).string() : "<DEF>",
883 String8(getNamespaceUri()).string());
885 printf("%s C: \"%s\"\n", prefix
.string(), String8(getCData()).string());
887 int N
= mChildren
.size();
888 for (i
=0; i
<N
; i
++) {
889 mChildren
.itemAt(i
)->print(indent
+1);
893 static void splitName(const char* name
, String16
* outNs
, String16
* outName
)
895 const char* p
= name
;
896 while (*p
!= 0 && *p
!= 1) {
901 *outName
= String16(name
);
903 *outNs
= String16(name
, (p
-name
));
904 *outName
= String16(p
+1);
909 XMLNode::startNamespace(void *userData
, const char *prefix
, const char *uri
)
911 NOISY_PARSE(printf("Start Namespace: %s %s\n", prefix
, uri
));
912 ParseState
* st
= (ParseState
*)userData
;
913 sp
<XMLNode
> node
= XMLNode::newNamespace(st
->filename
,
914 String16(prefix
!= NULL
? prefix
: ""), String16(uri
));
915 node
->setStartLineNumber(XML_GetCurrentLineNumber(st
->parser
));
916 if (st
->stack
.size() > 0) {
917 st
->stack
.itemAt(st
->stack
.size()-1)->addChild(node
);
921 st
->stack
.push(node
);
925 XMLNode::startElement(void *userData
, const char *name
, const char **atts
)
927 NOISY_PARSE(printf("Start Element: %s\n", name
));
928 ParseState
* st
= (ParseState
*)userData
;
929 String16 ns16
, name16
;
930 splitName(name
, &ns16
, &name16
);
931 sp
<XMLNode
> node
= XMLNode::newElement(st
->filename
, ns16
, name16
);
932 node
->setStartLineNumber(XML_GetCurrentLineNumber(st
->parser
));
933 if (st
->pendingComment
.size() > 0) {
934 node
->appendComment(st
->pendingComment
);
935 st
->pendingComment
= String16();
937 if (st
->stack
.size() > 0) {
938 st
->stack
.itemAt(st
->stack
.size()-1)->addChild(node
);
942 st
->stack
.push(node
);
944 for (int i
= 0; atts
[i
]; i
+= 2) {
945 splitName(atts
[i
], &ns16
, &name16
);
946 node
->addAttribute(ns16
, name16
, String16(atts
[i
+1]));
951 XMLNode::characterData(void *userData
, const XML_Char
*s
, int len
)
953 NOISY_PARSE(printf("CDATA: \"%s\"\n", String8(s
, len
).string()));
954 ParseState
* st
= (ParseState
*)userData
;
955 sp
<XMLNode
> node
= NULL
;
956 if (st
->stack
.size() == 0) {
959 sp
<XMLNode
> parent
= st
->stack
.itemAt(st
->stack
.size()-1);
960 if (parent
!= NULL
&& parent
->getChildren().size() > 0) {
961 node
= parent
->getChildren()[parent
->getChildren().size()-1];
962 if (node
->getType() != TYPE_CDATA
) {
963 // Last node is not CDATA, need to make a new node.
969 node
= XMLNode::newCData(st
->filename
);
970 node
->setStartLineNumber(XML_GetCurrentLineNumber(st
->parser
));
971 parent
->addChild(node
);
974 node
->appendChars(String16(s
, len
));
978 XMLNode::endElement(void *userData
, const char *name
)
980 NOISY_PARSE(printf("End Element: %s\n", name
));
981 ParseState
* st
= (ParseState
*)userData
;
982 sp
<XMLNode
> node
= st
->stack
.itemAt(st
->stack
.size()-1);
983 node
->setEndLineNumber(XML_GetCurrentLineNumber(st
->parser
));
984 if (st
->pendingComment
.size() > 0) {
985 node
->appendComment(st
->pendingComment
);
986 st
->pendingComment
= String16();
988 String16 ns16
, name16
;
989 splitName(name
, &ns16
, &name16
);
990 LOG_ALWAYS_FATAL_IF(node
->getElementNamespace() != ns16
991 || node
->getElementName() != name16
,
992 "Bad end element %s", name
);
997 XMLNode::endNamespace(void *userData
, const char *prefix
)
999 const char* nonNullPrefix
= prefix
!= NULL
? prefix
: "";
1000 NOISY_PARSE(printf("End Namespace: %s\n", prefix
));
1001 ParseState
* st
= (ParseState
*)userData
;
1002 sp
<XMLNode
> node
= st
->stack
.itemAt(st
->stack
.size()-1);
1003 node
->setEndLineNumber(XML_GetCurrentLineNumber(st
->parser
));
1004 LOG_ALWAYS_FATAL_IF(node
->getNamespacePrefix() != String16(nonNullPrefix
),
1005 "Bad end namespace %s", prefix
);
1010 XMLNode::commentData(void *userData
, const char *comment
)
1012 NOISY_PARSE(printf("Comment: %s\n", comment
));
1013 ParseState
* st
= (ParseState
*)userData
;
1014 if (st
->pendingComment
.size() > 0) {
1015 st
->pendingComment
.append(String16("\n"));
1017 st
->pendingComment
.append(String16(comment
));
1020 status_t
XMLNode::collect_strings(StringPool
* dest
, Vector
<uint32_t>* outResIds
,
1021 bool stripComments
, bool stripRawValues
) const
1023 collect_attr_strings(dest
, outResIds
, true);
1026 if (mNamespacePrefix
.size() > 0) {
1027 dest
->add(mNamespacePrefix
, true);
1029 if (mNamespaceUri
.size() > 0) {
1030 dest
->add(mNamespaceUri
, true);
1032 if (mElementName
.size() > 0) {
1033 dest
->add(mElementName
, true);
1036 if (!stripComments
&& mComment
.size() > 0) {
1037 dest
->add(mComment
, true);
1040 const int NA
= mAttributes
.size();
1042 for (i
=0; i
<NA
; i
++) {
1043 const attribute_entry
& ae
= mAttributes
.itemAt(i
);
1044 if (ae
.ns
.size() > 0) {
1045 dest
->add(ae
.ns
, true);
1047 if (!stripRawValues
|| ae
.needStringValue()) {
1048 dest
->add(ae
.string
, true);
1051 if (ae.value.dataType == Res_value::TYPE_NULL
1052 || ae.value.dataType == Res_value::TYPE_STRING) {
1053 dest->add(ae.string, true);
1058 if (mElementName
.size() == 0) {
1059 // If not an element, include the CDATA, even if it is empty.
1060 dest
->add(mChars
, true);
1063 const int NC
= mChildren
.size();
1065 for (i
=0; i
<NC
; i
++) {
1066 mChildren
.itemAt(i
)->collect_strings(dest
, outResIds
,
1067 stripComments
, stripRawValues
);
1073 status_t
XMLNode::collect_attr_strings(StringPool
* outPool
,
1074 Vector
<uint32_t>* outResIds
, bool allAttrs
) const {
1075 const int NA
= mAttributes
.size();
1077 for (int i
=0; i
<NA
; i
++) {
1078 const attribute_entry
& attr
= mAttributes
.itemAt(i
);
1079 uint32_t id
= attr
.nameResId
;
1080 if (id
|| allAttrs
) {
1081 // See if we have already assigned this resource ID to a pooled
1083 const Vector
<size_t>* indices
= outPool
->offsetsForString(attr
.name
);
1085 if (indices
!= NULL
) {
1086 const int NJ
= indices
->size();
1087 const size_t NR
= outResIds
->size();
1088 for (int j
=0; j
<NJ
; j
++) {
1089 size_t strIdx
= indices
->itemAt(j
);
1092 // We don't need to assign a resource ID for this one.
1096 // Just ignore strings that are out of range of
1097 // the currently assigned resource IDs... we add
1098 // strings as we assign the first ID.
1099 } else if (outResIds
->itemAt(strIdx
) == id
) {
1106 idx
= outPool
->add(attr
.name
);
1107 NOISY(printf("Adding attr %s (resid 0x%08x) to pool: idx=%d\n",
1108 String8(attr
.name
).string(), id
, idx
));
1110 while ((ssize_t
)outResIds
->size() <= idx
) {
1113 outResIds
->replaceAt(id
, idx
);
1116 attr
.namePoolIdx
= idx
;
1117 NOISY(printf("String %s offset=0x%08x\n",
1118 String8(attr
.name
).string(), idx
));
1125 status_t
XMLNode::collect_resid_strings(StringPool
* outPool
,
1126 Vector
<uint32_t>* outResIds
) const
1128 collect_attr_strings(outPool
, outResIds
, false);
1130 const int NC
= mChildren
.size();
1132 for (int i
=0; i
<NC
; i
++) {
1133 mChildren
.itemAt(i
)->collect_resid_strings(outPool
, outResIds
);
1139 status_t
XMLNode::flatten_node(const StringPool
& strings
, const sp
<AaptFile
>& dest
,
1140 bool stripComments
, bool stripRawValues
) const
1142 ResXMLTree_node node
;
1143 ResXMLTree_cdataExt cdataExt
;
1144 ResXMLTree_namespaceExt namespaceExt
;
1145 ResXMLTree_attrExt attrExt
;
1146 const void* extData
= NULL
;
1148 ResXMLTree_attribute attr
;
1150 const size_t NA
= mAttributes
.size();
1151 const size_t NC
= mChildren
.size();
1154 LOG_ALWAYS_FATAL_IF(NA
!= mAttributeOrder
.size(), "Attributes messed up!");
1156 const String16
id16("id");
1157 const String16
class16("class");
1158 const String16
style16("style");
1160 const type type
= getType();
1162 memset(&node
, 0, sizeof(node
));
1163 memset(&attr
, 0, sizeof(attr
));
1164 node
.header
.headerSize
= htods(sizeof(node
));
1165 node
.lineNumber
= htodl(getStartLineNumber());
1166 if (!stripComments
) {
1167 node
.comment
.index
= htodl(
1168 mComment
.size() > 0 ? strings
.offsetForString(mComment
) : -1);
1169 //if (mComment.size() > 0) {
1170 // printf("Flattening comment: %s\n", String8(mComment).string());
1173 node
.comment
.index
= htodl((uint32_t)-1);
1175 if (type
== TYPE_ELEMENT
) {
1176 node
.header
.type
= htods(RES_XML_START_ELEMENT_TYPE
);
1178 extSize
= sizeof(attrExt
);
1179 memset(&attrExt
, 0, sizeof(attrExt
));
1180 if (mNamespaceUri
.size() > 0) {
1181 attrExt
.ns
.index
= htodl(strings
.offsetForString(mNamespaceUri
));
1183 attrExt
.ns
.index
= htodl((uint32_t)-1);
1185 attrExt
.name
.index
= htodl(strings
.offsetForString(mElementName
));
1186 attrExt
.attributeStart
= htods(sizeof(attrExt
));
1187 attrExt
.attributeSize
= htods(sizeof(attr
));
1188 attrExt
.attributeCount
= htods(NA
);
1189 attrExt
.idIndex
= htods(0);
1190 attrExt
.classIndex
= htods(0);
1191 attrExt
.styleIndex
= htods(0);
1192 for (i
=0; i
<NA
; i
++) {
1193 ssize_t idx
= mAttributeOrder
.valueAt(i
);
1194 const attribute_entry
& ae
= mAttributes
.itemAt(idx
);
1195 if (ae
.ns
.size() == 0) {
1196 if (ae
.name
== id16
) {
1197 attrExt
.idIndex
= htods(i
+1);
1198 } else if (ae
.name
== class16
) {
1199 attrExt
.classIndex
= htods(i
+1);
1200 } else if (ae
.name
== style16
) {
1201 attrExt
.styleIndex
= htods(i
+1);
1205 } else if (type
== TYPE_NAMESPACE
) {
1206 node
.header
.type
= htods(RES_XML_START_NAMESPACE_TYPE
);
1207 extData
= &namespaceExt
;
1208 extSize
= sizeof(namespaceExt
);
1209 memset(&namespaceExt
, 0, sizeof(namespaceExt
));
1210 if (mNamespacePrefix
.size() > 0) {
1211 namespaceExt
.prefix
.index
= htodl(strings
.offsetForString(mNamespacePrefix
));
1213 namespaceExt
.prefix
.index
= htodl((uint32_t)-1);
1215 namespaceExt
.prefix
.index
= htodl(strings
.offsetForString(mNamespacePrefix
));
1216 namespaceExt
.uri
.index
= htodl(strings
.offsetForString(mNamespaceUri
));
1217 LOG_ALWAYS_FATAL_IF(NA
!= 0, "Namespace nodes can't have attributes!");
1218 } else if (type
== TYPE_CDATA
) {
1219 node
.header
.type
= htods(RES_XML_CDATA_TYPE
);
1220 extData
= &cdataExt
;
1221 extSize
= sizeof(cdataExt
);
1222 memset(&cdataExt
, 0, sizeof(cdataExt
));
1223 cdataExt
.data
.index
= htodl(strings
.offsetForString(mChars
));
1224 cdataExt
.typedData
.size
= htods(sizeof(cdataExt
.typedData
));
1225 cdataExt
.typedData
.res0
= 0;
1226 cdataExt
.typedData
.dataType
= mCharsValue
.dataType
;
1227 cdataExt
.typedData
.data
= htodl(mCharsValue
.data
);
1228 LOG_ALWAYS_FATAL_IF(NA
!= 0, "CDATA nodes can't have attributes!");
1231 node
.header
.size
= htodl(sizeof(node
) + extSize
+ (sizeof(attr
)*NA
));
1233 dest
->writeData(&node
, sizeof(node
));
1235 dest
->writeData(extData
, extSize
);
1238 for (i
=0; i
<NA
; i
++) {
1239 ssize_t idx
= mAttributeOrder
.valueAt(i
);
1240 const attribute_entry
& ae
= mAttributes
.itemAt(idx
);
1241 if (ae
.ns
.size() > 0) {
1242 attr
.ns
.index
= htodl(strings
.offsetForString(ae
.ns
));
1244 attr
.ns
.index
= htodl((uint32_t)-1);
1246 attr
.name
.index
= htodl(ae
.namePoolIdx
);
1248 if (!stripRawValues
|| ae
.needStringValue()) {
1249 attr
.rawValue
.index
= htodl(strings
.offsetForString(ae
.string
));
1251 attr
.rawValue
.index
= htodl((uint32_t)-1);
1253 attr
.typedValue
.size
= htods(sizeof(attr
.typedValue
));
1254 if (ae
.value
.dataType
== Res_value::TYPE_NULL
1255 || ae
.value
.dataType
== Res_value::TYPE_STRING
) {
1256 attr
.typedValue
.res0
= 0;
1257 attr
.typedValue
.dataType
= Res_value::TYPE_STRING
;
1258 attr
.typedValue
.data
= htodl(strings
.offsetForString(ae
.string
));
1260 attr
.typedValue
.res0
= 0;
1261 attr
.typedValue
.dataType
= ae
.value
.dataType
;
1262 attr
.typedValue
.data
= htodl(ae
.value
.data
);
1264 dest
->writeData(&attr
, sizeof(attr
));
1267 for (i
=0; i
<NC
; i
++) {
1268 status_t err
= mChildren
.itemAt(i
)->flatten_node(strings
, dest
,
1269 stripComments
, stripRawValues
);
1270 if (err
!= NO_ERROR
) {
1275 if (type
== TYPE_ELEMENT
) {
1276 ResXMLTree_endElementExt endElementExt
;
1277 memset(&endElementExt
, 0, sizeof(endElementExt
));
1278 node
.header
.type
= htods(RES_XML_END_ELEMENT_TYPE
);
1279 node
.header
.size
= htodl(sizeof(node
)+sizeof(endElementExt
));
1280 node
.lineNumber
= htodl(getEndLineNumber());
1281 node
.comment
.index
= htodl((uint32_t)-1);
1282 endElementExt
.ns
.index
= attrExt
.ns
.index
;
1283 endElementExt
.name
.index
= attrExt
.name
.index
;
1284 dest
->writeData(&node
, sizeof(node
));
1285 dest
->writeData(&endElementExt
, sizeof(endElementExt
));
1286 } else if (type
== TYPE_NAMESPACE
) {
1287 node
.header
.type
= htods(RES_XML_END_NAMESPACE_TYPE
);
1288 node
.lineNumber
= htodl(getEndLineNumber());
1289 node
.comment
.index
= htodl((uint32_t)-1);
1290 node
.header
.size
= htodl(sizeof(node
)+extSize
);
1291 dest
->writeData(&node
, sizeof(node
));
1292 dest
->writeData(extData
, extSize
);