1 //
2 // Copyright 2006 The Android Open Source Project
3 //
4 // Build resource files from raw assets.
5 //
6 
7 #ifndef XML_NODE_H
8 #define XML_NODE_H
9 
10 #include "StringPool.h"
11 #include "ResourceTable.h"
12 
13 #include <expat.h>
14 
15 class XMLNode;
16 
17 extern const char* const RESOURCES_ROOT_NAMESPACE;
18 extern const char* const RESOURCES_ANDROID_NAMESPACE;
19 
20 bool isWhitespace(const char16_t* str);
21 
22 String16 getNamespaceResourcePackage(String16 namespaceUri, bool* outIsPublic = NULL);
23 
24 status_t parseStyledString(Bundle* bundle,
25                            const char* fileName,
26                            ResXMLTree* inXml,
27                            const String16& endTag,
28                            String16* outString,
29                            Vector<StringPool::entry_style_span>* outSpans,
30                            bool isFormatted,
31                            PseudolocalizationMethod isPseudolocalizable);
32 
33 void printXMLBlock(ResXMLTree* block);
34 
35 status_t parseXMLResource(const sp<AaptFile>& file, ResXMLTree* outTree,
36                           bool stripAll=true, bool keepComments=false,
37                           const char** cDataTags=NULL);
38 
39 class XMLNode : public RefBase
40 {
41 public:
42     static sp<XMLNode> parse(const sp<AaptFile>& file);
43 
44     static inline
newNamespace(const String8 & filename,const String16 & prefix,const String16 & uri)45     sp<XMLNode> newNamespace(const String8& filename, const String16& prefix, const String16& uri) {
46         return new XMLNode(filename, prefix, uri, true);
47     }
48 
49     static inline
newElement(const String8 & filename,const String16 & ns,const String16 & name)50     sp<XMLNode> newElement(const String8& filename, const String16& ns, const String16& name) {
51         return new XMLNode(filename, ns, name, false);
52     }
53 
54     static inline
newCData(const String8 & filename)55     sp<XMLNode> newCData(const String8& filename) {
56         return new XMLNode(filename);
57     }
58 
59     enum type {
60         TYPE_NAMESPACE,
61         TYPE_ELEMENT,
62         TYPE_CDATA
63     };
64 
65     type getType() const;
66 
67     const String16& getNamespacePrefix() const;
68     const String16& getNamespaceUri() const;
69 
70     const String16& getElementNamespace() const;
71     const String16& getElementName() const;
72     const Vector<sp<XMLNode> >& getChildren() const;
73     Vector<sp<XMLNode> >& getChildren();
74 
75     const String8& getFilename() const;
76 
77     struct attribute_entry {
attribute_entryattribute_entry78         attribute_entry() : index(~(uint32_t)0), nameResId(0)
79         {
80             value.dataType = Res_value::TYPE_NULL;
81         }
82 
needStringValueattribute_entry83         bool needStringValue() const {
84             return nameResId == 0
85                 || value.dataType == Res_value::TYPE_NULL
86                 || value.dataType == Res_value::TYPE_STRING;
87         }
88 
89         String16 ns;
90         String16 name;
91         String16 string;
92         Res_value value;
93         uint32_t index;
94         uint32_t nameResId;
95         mutable uint32_t namePoolIdx;
96     };
97 
98     const Vector<attribute_entry>& getAttributes() const;
99 
100     const attribute_entry* getAttribute(const String16& ns, const String16& name) const;
101     bool removeAttribute(const String16& ns, const String16& name);
102 
103     attribute_entry* editAttribute(const String16& ns, const String16& name);
104 
105     const String16& getCData() const;
106 
107     const String16& getComment() const;
108 
109     int32_t getStartLineNumber() const;
110     int32_t getEndLineNumber() const;
111 
112     sp<XMLNode> searchElement(const String16& tagNamespace, const String16& tagName);
113 
114     sp<XMLNode> getChildElement(const String16& tagNamespace, const String16& tagName);
115 
116     status_t addChild(const sp<XMLNode>& child);
117 
118     status_t insertChildAt(const sp<XMLNode>& child, size_t index);
119 
120     status_t addAttribute(const String16& ns, const String16& name,
121                           const String16& value);
122 
123     status_t removeAttribute(size_t index);
124 
125     void setAttributeResID(size_t attrIdx, uint32_t resId);
126 
127     status_t appendChars(const String16& chars);
128 
129     status_t appendComment(const String16& comment);
130 
131     void setStartLineNumber(int32_t line);
132     void setEndLineNumber(int32_t line);
133 
134     void removeWhitespace(bool stripAll=true, const char** cDataTags=NULL);
135 
setUTF8(bool val)136     void setUTF8(bool val) { mUTF8 = val; }
137 
138     status_t parseValues(const sp<AaptAssets>& assets, ResourceTable* table);
139 
140     status_t assignResourceIds(const sp<AaptAssets>& assets,
141                                const ResourceTable* table = NULL);
142 
143     status_t flatten(const sp<AaptFile>& dest, bool stripComments,
144             bool stripRawValues) const;
145 
146     sp<XMLNode> clone() const;
147 
148     void print(int indent=0);
149 
150 private:
151     struct ParseState
152     {
153         String8 filename;
154         XML_Parser parser;
155         sp<XMLNode> root;
156         Vector<sp<XMLNode> > stack;
157         String16 pendingComment;
158     };
159 
160     static void XMLCALL
161     startNamespace(void *userData, const char *prefix, const char *uri);
162     static void XMLCALL
163     startElement(void *userData, const char *name, const char **atts);
164     static void XMLCALL
165     characterData(void *userData, const XML_Char *s, int len);
166     static void XMLCALL
167     endElement(void *userData, const char *name);
168     static void XMLCALL
169     endNamespace(void *userData, const char *prefix);
170 
171     static void XMLCALL
172     commentData(void *userData, const char *comment);
173 
174     // For cloning
175     XMLNode();
176 
177     // Creating an element node.
178     XMLNode(const String8& filename, const String16& s1, const String16& s2, bool isNamespace);
179 
180     // Creating a CDATA node.
181     explicit XMLNode(const String8& filename);
182 
183     status_t collect_strings(StringPool* dest, Vector<uint32_t>* outResIds,
184             bool stripComments, bool stripRawValues) const;
185 
186     status_t collect_attr_strings(StringPool* outPool,
187         Vector<uint32_t>* outResIds, bool allAttrs) const;
188 
189     status_t collect_resid_strings(StringPool* outPool,
190             Vector<uint32_t>* outResIds) const;
191 
192     status_t flatten_node(const StringPool& strings, const sp<AaptFile>& dest,
193             bool stripComments, bool stripRawValues) const;
194 
195     String16 mNamespacePrefix;
196     String16 mNamespaceUri;
197     String16 mElementName;
198     Vector<sp<XMLNode> > mChildren;
199     Vector<attribute_entry> mAttributes;
200     KeyedVector<uint32_t, uint32_t> mAttributeOrder;
201     uint32_t mNextAttributeIndex;
202     String16 mChars;
203     Res_value mCharsValue;
204     String16 mComment;
205     String8 mFilename;
206     int32_t mStartLineNumber;
207     int32_t mEndLineNumber;
208 
209     // Encode compiled XML with UTF-8 StringPools?
210     bool mUTF8;
211 };
212 
213 #endif
214