blob: 3161f6500291d18e0d09a13c078b1a1915144b5a [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
13class XMLNode;
14
15extern const char* const RESOURCES_ROOT_NAMESPACE;
16extern const char* const RESOURCES_ANDROID_NAMESPACE;
17
18bool isWhitespace(const char16_t* str);
19
20String16 getNamespaceResourcePackage(String16 namespaceUri, bool* outIsPublic = NULL);
21
22status_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,
Kenny Root15fe2cb2010-05-28 15:44:32 -070028 bool isFormatted,
Anton Krumina2ef5c02014-03-12 14:46:44 -070029 PseudolocalizationMethod isPseudolocalizable);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030
31void printXMLBlock(ResXMLTree* block);
32
33status_t parseXMLResource(const sp<AaptFile>& file, ResXMLTree* outTree,
34 bool stripAll=true, bool keepComments=false,
35 const char** cDataTags=NULL);
36
37class XMLNode : public RefBase
38{
39public:
40 static sp<XMLNode> parse(const sp<AaptFile>& file);
41
42 static inline
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
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
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
Dianne Hackborn62da8462009-05-13 15:06:13 -070072 const String8& getFilename() const;
73
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 struct attribute_entry {
75 attribute_entry() : index(~(uint32_t)0), nameResId(0)
76 {
77 value.dataType = Res_value::TYPE_NULL;
78 }
79
80 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
Dianne Hackborn62da8462009-05-13 15:06:13 -070097 const attribute_entry* getAttribute(const String16& ns, const String16& name) const;
98
Jeff Hamilton2fee0ed2010-01-06 15:46:38 -060099 attribute_entry* editAttribute(const String16& ns, const String16& name);
100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 const String16& getCData() const;
102
103 const String16& getComment() const;
104
105 int32_t getStartLineNumber() const;
106 int32_t getEndLineNumber() const;
107
Dianne Hackborn62da8462009-05-13 15:06:13 -0700108 sp<XMLNode> searchElement(const String16& tagNamespace, const String16& tagName);
109
110 sp<XMLNode> getChildElement(const String16& tagNamespace, const String16& tagName);
111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 status_t addChild(const sp<XMLNode>& child);
113
Dianne Hackborn62da8462009-05-13 15:06:13 -0700114 status_t insertChildAt(const sp<XMLNode>& child, size_t index);
115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 status_t addAttribute(const String16& ns, const String16& name,
117 const String16& value);
118
Adam Lesinskie572c012014-09-19 15:10:04 -0700119 status_t removeAttribute(size_t index);
120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 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
Kenny Root19138462009-12-04 09:38:48 -0800132 void setUTF8(bool val) { mUTF8 = val; }
133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 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
Adam Lesinskie572c012014-09-19 15:10:04 -0700142 sp<XMLNode> clone() const;
143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 void print(int indent=0);
145
146private:
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
Adam Lesinskie572c012014-09-19 15:10:04 -0700170 // For cloning
171 XMLNode();
172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 // 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;
Kenny Root19138462009-12-04 09:38:48 -0800204
205 // Encode compiled XML with UTF-8 StringPools?
206 bool mUTF8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207};
208
209#endif