Home | History | Annotate | Download | only in aapt
      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 isFormatted,
     29                            bool isPseudolocalizable);
     30 
     31 void printXMLBlock(ResXMLTree* block);
     32 
     33 status_t parseXMLResource(const sp<AaptFile>& file, ResXMLTree* outTree,
     34                           bool stripAll=true, bool keepComments=false,
     35                           const char** cDataTags=NULL);
     36 
     37 class XMLNode : public RefBase
     38 {
     39 public:
     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 
     72     const String8& getFilename() const;
     73 
     74     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 
     97     const attribute_entry* getAttribute(const String16& ns, const String16& name) const;
     98 
     99     attribute_entry* editAttribute(const String16& ns, const String16& name);
    100 
    101     const String16& getCData() const;
    102 
    103     const String16& getComment() const;
    104 
    105     int32_t getStartLineNumber() const;
    106     int32_t getEndLineNumber() const;
    107 
    108     sp<XMLNode> searchElement(const String16& tagNamespace, const String16& tagName);
    109 
    110     sp<XMLNode> getChildElement(const String16& tagNamespace, const String16& tagName);
    111 
    112     status_t addChild(const sp<XMLNode>& child);
    113 
    114     status_t insertChildAt(const sp<XMLNode>& child, size_t index);
    115 
    116     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 
    130     void setUTF8(bool val) { mUTF8 = val; }
    131 
    132     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 
    142 private:
    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;
    197 
    198     // Encode compiled XML with UTF-8 StringPools?
    199     bool mUTF8;
    200 };
    201 
    202 #endif
    203