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