blob: ac920186fe6316cbd13e0c803cb6fed2b1771b06 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001//
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
Elliott Hughes51348d22015-07-21 11:39:21 -070013#include <expat.h>
14
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015class 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,
Kenny Root15fe2cb2010-05-28 15:44:32 -070030 bool isFormatted,
Anton Krumina2ef5c02014-03-12 14:46:44 -070031 PseudolocalizationMethod isPseudolocalizable);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032
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 }
Adam Lesinski07dfd2d2015-10-28 15:44:27 -070058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 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;
Adam Lesinski07dfd2d2015-10-28 15:44:27 -070073 Vector<sp<XMLNode> >& getChildren();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074
Dianne Hackborn62da8462009-05-13 15:06:13 -070075 const String8& getFilename() const;
76
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 struct attribute_entry {
78 attribute_entry() : index(~(uint32_t)0), nameResId(0)
79 {
80 value.dataType = Res_value::TYPE_NULL;
81 }
82
83 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
Dianne Hackborn62da8462009-05-13 15:06:13 -0700100 const attribute_entry* getAttribute(const String16& ns, const String16& name) const;
Adam Lesinski07dfd2d2015-10-28 15:44:27 -0700101 bool removeAttribute(const String16& ns, const String16& name);
Dianne Hackborn62da8462009-05-13 15:06:13 -0700102
Jeff Hamilton2fee0ed2010-01-06 15:46:38 -0600103 attribute_entry* editAttribute(const String16& ns, const String16& name);
104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 const String16& getCData() const;
106
107 const String16& getComment() const;
108
109 int32_t getStartLineNumber() const;
110 int32_t getEndLineNumber() const;
111
Dianne Hackborn62da8462009-05-13 15:06:13 -0700112 sp<XMLNode> searchElement(const String16& tagNamespace, const String16& tagName);
113
114 sp<XMLNode> getChildElement(const String16& tagNamespace, const String16& tagName);
115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 status_t addChild(const sp<XMLNode>& child);
117
Dianne Hackborn62da8462009-05-13 15:06:13 -0700118 status_t insertChildAt(const sp<XMLNode>& child, size_t index);
119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 status_t addAttribute(const String16& ns, const String16& name,
121 const String16& value);
122
Adam Lesinskie572c012014-09-19 15:10:04 -0700123 status_t removeAttribute(size_t index);
124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 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
Kenny Root19138462009-12-04 09:38:48 -0800136 void setUTF8(bool val) { mUTF8 = val; }
137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 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
Adam Lesinskie572c012014-09-19 15:10:04 -0700146 sp<XMLNode> clone() const;
147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 void print(int indent=0);
149
150private:
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
Adam Lesinskie572c012014-09-19 15:10:04 -0700174 // For cloning
175 XMLNode();
176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 // Creating an element node.
178 XMLNode(const String8& filename, const String16& s1, const String16& s2, bool isNamespace);
179
180 // Creating a CDATA node.
Chih-Hung Hsieh9b8528f2016-08-10 14:15:30 -0700181 explicit XMLNode(const String8& filename);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182
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;
Kenny Root19138462009-12-04 09:38:48 -0800208
209 // Encode compiled XML with UTF-8 StringPools?
210 bool mUTF8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211};
212
213#endif