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