XMLNode.h revision b48adad2ede148b62c7025da4d2320cb5d9af58c
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
15class XMLNode;
16
17extern const char* const RESOURCES_ROOT_NAMESPACE;
18extern const char* const RESOURCES_ANDROID_NAMESPACE;
19
20bool isWhitespace(const char16_t* str);
21
22String16 getNamespaceResourcePackage(String16 namespaceUri, bool* outIsPublic = NULL);
23
24status_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
33void printXMLBlock(ResXMLTree* block);
34
35status_t parseXMLResource(const sp<AaptFile>& file, ResXMLTree* outTree,
36                          bool stripAll=true, bool keepComments=false,
37                          const char** cDataTags=NULL);
38
39class XMLNode : public RefBase
40{
41public:
42    static sp<XMLNode> parse(const sp<AaptFile>& file);
43
44    static inline
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
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
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
74    const String8& getFilename() const;
75
76    struct attribute_entry {
77        attribute_entry() : index(~(uint32_t)0), nameResId(0)
78        {
79            value.dataType = Res_value::TYPE_NULL;
80        }
81
82        bool needStringValue() const {
83            return nameResId == 0
84                || value.dataType == Res_value::TYPE_NULL
85                || value.dataType == Res_value::TYPE_STRING;
86        }
87
88        String16 ns;
89        String16 name;
90        String16 string;
91        Res_value value;
92        uint32_t index;
93        uint32_t nameResId;
94        mutable uint32_t namePoolIdx;
95    };
96
97    const Vector<attribute_entry>& getAttributes() const;
98
99    const attribute_entry* getAttribute(const String16& ns, const String16& name) const;
100
101    attribute_entry* editAttribute(const String16& ns, const String16& name);
102
103    const String16& getCData() const;
104
105    const String16& getComment() const;
106
107    int32_t getStartLineNumber() const;
108    int32_t getEndLineNumber() const;
109
110    sp<XMLNode> searchElement(const String16& tagNamespace, const String16& tagName);
111
112    sp<XMLNode> getChildElement(const String16& tagNamespace, const String16& tagName);
113
114    status_t addChild(const sp<XMLNode>& child);
115
116    status_t insertChildAt(const sp<XMLNode>& child, size_t index);
117
118    status_t addAttribute(const String16& ns, const String16& name,
119                          const String16& value);
120
121    status_t removeAttribute(size_t index);
122
123    void setAttributeResID(size_t attrIdx, uint32_t resId);
124
125    status_t appendChars(const String16& chars);
126
127    status_t appendComment(const String16& comment);
128
129    void setStartLineNumber(int32_t line);
130    void setEndLineNumber(int32_t line);
131
132    void removeWhitespace(bool stripAll=true, const char** cDataTags=NULL);
133
134    void setUTF8(bool val) { mUTF8 = val; }
135
136    status_t parseValues(const sp<AaptAssets>& assets, ResourceTable* table);
137
138    status_t assignResourceIds(const sp<AaptAssets>& assets,
139                               const ResourceTable* table = NULL);
140
141    status_t flatten(const sp<AaptFile>& dest, bool stripComments,
142            bool stripRawValues) const;
143
144    sp<XMLNode> clone() const;
145
146    void print(int indent=0);
147
148private:
149    struct ParseState
150    {
151        String8 filename;
152        XML_Parser parser;
153        sp<XMLNode> root;
154        Vector<sp<XMLNode> > stack;
155        String16 pendingComment;
156    };
157
158    static void XMLCALL
159    startNamespace(void *userData, const char *prefix, const char *uri);
160    static void XMLCALL
161    startElement(void *userData, const char *name, const char **atts);
162    static void XMLCALL
163    characterData(void *userData, const XML_Char *s, int len);
164    static void XMLCALL
165    endElement(void *userData, const char *name);
166    static void XMLCALL
167    endNamespace(void *userData, const char *prefix);
168
169    static void XMLCALL
170    commentData(void *userData, const char *comment);
171
172    // For cloning
173    XMLNode();
174
175    // Creating an element node.
176    XMLNode(const String8& filename, const String16& s1, const String16& s2, bool isNamespace);
177
178    // Creating a CDATA node.
179    XMLNode(const String8& filename);
180
181    status_t collect_strings(StringPool* dest, Vector<uint32_t>* outResIds,
182            bool stripComments, bool stripRawValues) const;
183
184    status_t collect_attr_strings(StringPool* outPool,
185        Vector<uint32_t>* outResIds, bool allAttrs) const;
186
187    status_t collect_resid_strings(StringPool* outPool,
188            Vector<uint32_t>* outResIds) const;
189
190    status_t flatten_node(const StringPool& strings, const sp<AaptFile>& dest,
191            bool stripComments, bool stripRawValues) const;
192
193    String16 mNamespacePrefix;
194    String16 mNamespaceUri;
195    String16 mElementName;
196    Vector<sp<XMLNode> > mChildren;
197    Vector<attribute_entry> mAttributes;
198    KeyedVector<uint32_t, uint32_t> mAttributeOrder;
199    uint32_t mNextAttributeIndex;
200    String16 mChars;
201    Res_value mCharsValue;
202    String16 mComment;
203    String8 mFilename;
204    int32_t mStartLineNumber;
205    int32_t mEndLineNumber;
206
207    // Encode compiled XML with UTF-8 StringPools?
208    bool mUTF8;
209};
210
211#endif
212