blob: 05624b7451766e8cb830210533214f0418f3511d [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,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029 bool isPseudolocalizable);
30
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
119 void setAttributeResID(size_t attrIdx, uint32_t resId);
120
121 status_t appendChars(const String16& chars);
122
123 status_t appendComment(const String16& comment);
124
125 void setStartLineNumber(int32_t line);
126 void setEndLineNumber(int32_t line);
127
128 void removeWhitespace(bool stripAll=true, const char** cDataTags=NULL);
129
Kenny Root19138462009-12-04 09:38:48 -0800130 void setUTF8(bool val) { mUTF8 = val; }
131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 status_t parseValues(const sp<AaptAssets>& assets, ResourceTable* table);
133
134 status_t assignResourceIds(const sp<AaptAssets>& assets,
135 const ResourceTable* table = NULL);
136
137 status_t flatten(const sp<AaptFile>& dest, bool stripComments,
138 bool stripRawValues) const;
139
140 void print(int indent=0);
141
142private:
143 struct ParseState
144 {
145 String8 filename;
146 XML_Parser parser;
147 sp<XMLNode> root;
148 Vector<sp<XMLNode> > stack;
149 String16 pendingComment;
150 };
151
152 static void XMLCALL
153 startNamespace(void *userData, const char *prefix, const char *uri);
154 static void XMLCALL
155 startElement(void *userData, const char *name, const char **atts);
156 static void XMLCALL
157 characterData(void *userData, const XML_Char *s, int len);
158 static void XMLCALL
159 endElement(void *userData, const char *name);
160 static void XMLCALL
161 endNamespace(void *userData, const char *prefix);
162
163 static void XMLCALL
164 commentData(void *userData, const char *comment);
165
166 // Creating an element node.
167 XMLNode(const String8& filename, const String16& s1, const String16& s2, bool isNamespace);
168
169 // Creating a CDATA node.
170 XMLNode(const String8& filename);
171
172 status_t collect_strings(StringPool* dest, Vector<uint32_t>* outResIds,
173 bool stripComments, bool stripRawValues) const;
174
175 status_t collect_attr_strings(StringPool* outPool,
176 Vector<uint32_t>* outResIds, bool allAttrs) const;
177
178 status_t collect_resid_strings(StringPool* outPool,
179 Vector<uint32_t>* outResIds) const;
180
181 status_t flatten_node(const StringPool& strings, const sp<AaptFile>& dest,
182 bool stripComments, bool stripRawValues) const;
183
184 String16 mNamespacePrefix;
185 String16 mNamespaceUri;
186 String16 mElementName;
187 Vector<sp<XMLNode> > mChildren;
188 Vector<attribute_entry> mAttributes;
189 KeyedVector<uint32_t, uint32_t> mAttributeOrder;
190 uint32_t mNextAttributeIndex;
191 String16 mChars;
192 Res_value mCharsValue;
193 String16 mComment;
194 String8 mFilename;
195 int32_t mStartLineNumber;
196 int32_t mEndLineNumber;
Kenny Root19138462009-12-04 09:38:48 -0800197
198 // Encode compiled XML with UTF-8 StringPools?
199 bool mUTF8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200};
201
202#endif