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