2 // Copyright 2006 The Android Open Source Project 
   4 // Build resource files from raw assets. 
   7 #ifndef RESOURCE_TABLE_H 
   8 #define RESOURCE_TABLE_H 
  10 #include "StringPool.h" 
  11 #include "SourcePos.h" 
  21     XML_COMPILE_STRIP_COMMENTS 
= 1<<0, 
  22     XML_COMPILE_ASSIGN_ATTRIBUTE_IDS 
= 1<<1, 
  23     XML_COMPILE_COMPACT_WHITESPACE 
= 1<<2, 
  24     XML_COMPILE_STRIP_WHITESPACE 
= 1<<3, 
  25     XML_COMPILE_STRIP_RAW_VALUES 
= 1<<4, 
  27     XML_COMPILE_STANDARD_RESOURCE 
= 
  28             XML_COMPILE_STRIP_COMMENTS 
| XML_COMPILE_ASSIGN_ATTRIBUTE_IDS
 
  29             | XML_COMPILE_STRIP_WHITESPACE 
| XML_COMPILE_STRIP_RAW_VALUES
 
  32 status_t 
compileXmlFile(const sp
<AaptAssets
>& assets
, 
  33                         const sp
<AaptFile
>& target
, 
  35                         int options 
= XML_COMPILE_STANDARD_RESOURCE
); 
  37 status_t 
compileResourceFile(Bundle
* bundle
, 
  38                              const sp
<AaptAssets
>& assets
, 
  39                              const sp
<AaptFile
>& in
, 
  40                              const ResTable_config
& defParams
, 
  42                              ResourceTable
* outTable
); 
  50     AccessorCookie(const SourcePos
&p
, const String8
& a
, const String8
& v
) 
  58 class ResourceTable 
: public ResTable::Accessor
 
  65     ResourceTable(Bundle
* bundle
, const String16
& assetsPackage
); 
  67     status_t 
addIncludedResources(Bundle
* bundle
, const sp
<AaptAssets
>& assets
); 
  69     status_t 
addPublic(const SourcePos
& pos
, 
  70                        const String16
& package
, 
  73                        const uint32_t ident
); 
  75     status_t 
addEntry(const SourcePos
& pos
, 
  76                       const String16
& package
, 
  79                       const String16
& value
, 
  80                       const Vector
<StringPool::entry_style_span
>* style 
= NULL
, 
  81                       const ResTable_config
* params 
= NULL
, 
  82                       const bool doSetIndex 
= false, 
  83                       const int32_t format 
= ResTable_map::TYPE_ANY
, 
  84                       const bool overwrite 
= false); 
  86     status_t 
startBag(const SourcePos
& pos
, 
  87                     const String16
& package
, 
  90                     const String16
& bagParent
, 
  91                     const ResTable_config
* params 
= NULL
, 
  95     status_t 
addBag(const SourcePos
& pos
, 
  96                     const String16
& package
, 
  99                     const String16
& bagParent
, 
 100                     const String16
& bagKey
, 
 101                     const String16
& value
, 
 102                     const Vector
<StringPool::entry_style_span
>* style 
= NULL
, 
 103                     const ResTable_config
* params 
= NULL
, 
 104                     bool replace 
= false, 
 106                     const int32_t format 
= ResTable_map::TYPE_ANY
); 
 108     bool hasBagOrEntry(const String16
& package
, 
 109                        const String16
& type
, 
 110                        const String16
& name
) const; 
 112     bool hasBagOrEntry(const String16
& ref
, 
 113                        const String16
* defType 
= NULL
, 
 114                        const String16
* defPackage 
= NULL
); 
 116     bool appendComment(const String16
& package
, 
 117                        const String16
& type
, 
 118                        const String16
& name
, 
 119                        const String16
& comment
, 
 120                        bool onlyIfEmpty 
= false); 
 122     bool appendTypeComment(const String16
& package
, 
 123                            const String16
& type
, 
 124                            const String16
& name
, 
 125                            const String16
& comment
); 
 128     size_t numLocalResources() const; 
 129     bool hasResources() const; 
 131     sp
<AaptFile
> flatten(Bundle
*); 
 133     static inline uint32_t makeResId(uint32_t packageId
, 
 137         return nameId 
| (typeId
<<16) | (packageId
<<24); 
 140     static inline uint32_t getResId(const sp
<Package
>& p
, 
 144     uint32_t getResId(const String16
& package
, 
 145                       const String16
& type
, 
 146                       const String16
& name
, 
 147                       bool onlyPublic 
= false) const; 
 149     uint32_t getResId(const String16
& ref
, 
 150                       const String16
* defType 
= NULL
, 
 151                       const String16
* defPackage 
= NULL
, 
 152                       const char** outErrorMsg 
= NULL
, 
 153                       bool onlyPublic 
= false) const; 
 155     static bool isValidResourceName(const String16
& s
); 
 157     bool stringToValue(Res_value
* outValue
, StringPool
* pool
, 
 159                        bool preserveSpaces
, bool coerceType
, 
 161                        const Vector
<StringPool::entry_style_span
>* style 
= NULL
, 
 162                        String16
* outStr 
= NULL
, void* accessorCookie 
= NULL
, 
 163                        uint32_t attrType 
= ResTable_map::TYPE_ANY
); 
 165     status_t 
assignResourceIds(); 
 166     status_t 
addSymbols(const sp
<AaptSymbols
>& outSymbols 
= NULL
); 
 167     void addLocalization(const String16
& name
, const String8
& locale
); 
 168     status_t 
validateLocalizations(void); 
 170     status_t 
flatten(Bundle
*, const sp
<AaptFile
>& dest
); 
 172     void writePublicDefinitions(const String16
& package
, FILE* fp
); 
 174     virtual uint32_t getCustomResource(const String16
& package
, 
 175                                        const String16
& type
, 
 176                                        const String16
& name
) const; 
 177     virtual uint32_t getCustomResourceWithCreation(const String16
& package
, 
 178                                                    const String16
& type
, 
 179                                                    const String16
& name
, 
 180                                                    const bool createIfNeeded
); 
 181     virtual uint32_t getRemappedPackage(uint32_t origPackage
) const; 
 182     virtual bool getAttributeType(uint32_t attrID
, uint32_t* outType
); 
 183     virtual bool getAttributeMin(uint32_t attrID
, uint32_t* outMin
); 
 184     virtual bool getAttributeMax(uint32_t attrID
, uint32_t* outMax
); 
 185     virtual bool getAttributeKeys(uint32_t attrID
, Vector
<String16
>* outKeys
); 
 186     virtual bool getAttributeEnum(uint32_t attrID
, 
 187                                   const char16_t* name
, size_t nameLen
, 
 188                                   Res_value
* outValue
); 
 189     virtual bool getAttributeFlags(uint32_t attrID
, 
 190                                    const char16_t* name
, size_t nameLen
, 
 191                                    Res_value
* outValue
); 
 192     virtual uint32_t getAttributeL10N(uint32_t attrID
); 
 194     virtual bool getLocalizationSetting(); 
 195     virtual void reportError(void* accessorCookie
, const char* fmt
, ...); 
 197     void setCurrentXmlPos(const SourcePos
& pos
) { mCurrentXmlPos 
= pos
; } 
 201         Item() : isId(false), format(ResTable_map::TYPE_ANY
), bagKeyId(0), evaluating(false) 
 202             { memset(&parsedValue
, 0, sizeof(parsedValue
)); } 
 203         Item(const SourcePos
& pos
, 
 205              const String16
& _value
, 
 206              const Vector
<StringPool::entry_style_span
>* _style 
= NULL
, 
 207              int32_t format 
= ResTable_map::TYPE_ANY
); 
 208         Item(const Item
& o
) : sourcePos(o
.sourcePos
), 
 209             isId(o
.isId
), value(o
.value
), style(o
.style
), 
 210             format(o
.format
), bagKeyId(o
.bagKeyId
), evaluating(false) { 
 211             memset(&parsedValue
, 0, sizeof(parsedValue
)); 
 215         Item
& operator=(const Item
& o
) { 
 216             sourcePos 
= o
.sourcePos
; 
 221             bagKeyId 
= o
.bagKeyId
; 
 222             parsedValue 
= o
.parsedValue
; 
 229         Vector
<StringPool::entry_style_span
>    style
; 
 232         mutable bool                            evaluating
; 
 233         Res_value                               parsedValue
; 
 236     class Entry 
: public RefBase 
{ 
 238         Entry(const String16
& name
, const SourcePos
& pos
) 
 239             : mName(name
), mType(TYPE_UNKNOWN
), 
 240               mItemFormat(ResTable_map::TYPE_ANY
), mNameIndex(-1), mPos(pos
) 
 250         String16 
getName() const { return mName
; } 
 251         type 
getType() const { return mType
; } 
 253         void setParent(const String16
& parent
) { mParent 
= parent
; } 
 254         String16 
getParent() const { return mParent
; } 
 256         status_t 
makeItABag(const SourcePos
& sourcePos
); 
 258         status_t 
setItem(const SourcePos
& pos
, 
 259                          const String16
& value
, 
 260                          const Vector
<StringPool::entry_style_span
>* style 
= NULL
, 
 261                          int32_t format 
= ResTable_map::TYPE_ANY
, 
 262                          const bool overwrite 
= false); 
 264         status_t 
addToBag(const SourcePos
& pos
, 
 265                           const String16
& key
, const String16
& value
, 
 266                           const Vector
<StringPool::entry_style_span
>* style 
= NULL
, 
 267                           bool replace
=false, bool isId 
= false, 
 268                           int32_t format 
= ResTable_map::TYPE_ANY
); 
 270         // Index of the entry's name string in the key pool. 
 271         int32_t getNameIndex() const { return mNameIndex
; } 
 272         void setNameIndex(int32_t index
) { mNameIndex 
= index
; } 
 274         const Item
* getItem() const { return mType 
== TYPE_ITEM 
? &mItem 
: NULL
; } 
 275         const KeyedVector
<String16
, Item
>& getBag() const { return mBag
; } 
 277         status_t 
generateAttributes(ResourceTable
* table
, 
 278                                     const String16
& package
); 
 280         status_t 
assignResourceIds(ResourceTable
* table
, 
 281                                    const String16
& package
); 
 283         status_t 
prepareFlatten(StringPool
* strings
, ResourceTable
* table
); 
 285         ssize_t 
flatten(Bundle
*, const sp
<AaptFile
>& data
, bool isPublic
); 
 287         const SourcePos
& getPos() const { return mPos
; } 
 295         KeyedVector
<String16
, Item
> mBag
; 
 301     struct ConfigDescription 
: public ResTable_config 
{ 
 302         ConfigDescription() { 
 303             memset(this, 0, sizeof(*this)); 
 304             size 
= sizeof(ResTable_config
); 
 306         ConfigDescription(const ResTable_config
&o
) { 
 307             *static_cast<ResTable_config
*>(this) = o
; 
 308             size 
= sizeof(ResTable_config
); 
 310         ConfigDescription(const ConfigDescription
&o
) { 
 311             *static_cast<ResTable_config
*>(this) = o
; 
 314         ConfigDescription
& operator=(const ResTable_config
& o
) { 
 315             *static_cast<ResTable_config
*>(this) = o
; 
 316             size 
= sizeof(ResTable_config
); 
 319         ConfigDescription
& operator=(const ConfigDescription
& o
) { 
 320             *static_cast<ResTable_config
*>(this) = o
; 
 324         inline bool operator<(const ConfigDescription
& o
) const { return compare(o
) < 0; } 
 325         inline bool operator<=(const ConfigDescription
& o
) const { return compare(o
) <= 0; } 
 326         inline bool operator==(const ConfigDescription
& o
) const { return compare(o
) == 0; } 
 327         inline bool operator!=(const ConfigDescription
& o
) const { return compare(o
) != 0; } 
 328         inline bool operator>=(const ConfigDescription
& o
) const { return compare(o
) >= 0; } 
 329         inline bool operator>(const ConfigDescription
& o
) const { return compare(o
) > 0; } 
 332     class ConfigList 
: public RefBase 
{ 
 334         ConfigList(const String16
& name
, const SourcePos
& pos
) 
 335             : mName(name
), mPos(pos
), mPublic(false), mEntryIndex(-1) { } 
 336         virtual ~ConfigList() { } 
 338         String16 
getName() const { return mName
; } 
 339         const SourcePos
& getPos() const { return mPos
; } 
 341         void appendComment(const String16
& comment
, bool onlyIfEmpty 
= false); 
 342         const String16
& getComment() const { return mComment
; } 
 344         void appendTypeComment(const String16
& comment
); 
 345         const String16
& getTypeComment() const { return mTypeComment
; } 
 347         // Index of this entry in its Type. 
 348         int32_t getEntryIndex() const { return mEntryIndex
; } 
 349         void setEntryIndex(int32_t index
) { mEntryIndex 
= index
; } 
 351         void setPublic(bool pub
) { mPublic 
= pub
; } 
 352         bool getPublic() const { return mPublic
; } 
 353         void setPublicSourcePos(const SourcePos
& pos
) { mPublicSourcePos 
= pos
; } 
 354         const SourcePos
& getPublicSourcePos() { return mPublicSourcePos
; } 
 356         void addEntry(const ResTable_config
& config
, const sp
<Entry
>& entry
) { 
 357             mEntries
.add(config
, entry
); 
 360         const DefaultKeyedVector
<ConfigDescription
, sp
<Entry
> >& getEntries() const { return mEntries
; } 
 362         const String16 mName
; 
 363         const SourcePos mPos
; 
 365         String16 mTypeComment
; 
 367         SourcePos mPublicSourcePos
; 
 369         DefaultKeyedVector
<ConfigDescription
, sp
<Entry
> > mEntries
; 
 374         Public() : sourcePos(), ident(0) { } 
 375         Public(const SourcePos
& pos
, 
 376                const String16
& _comment
, 
 379             comment(_comment
), ident(_ident
) { } 
 380         Public(const Public
& o
) : sourcePos(o
.sourcePos
), 
 381             comment(o
.comment
), ident(o
.ident
) { } 
 384         Public
& operator=(const Public
& o
) { 
 385             sourcePos 
= o
.sourcePos
; 
 396     class Type 
: public RefBase 
{ 
 398         Type(const String16
& name
, const SourcePos
& pos
) 
 399                 : mName(name
), mFirstPublicSourcePos(NULL
), mPublicIndex(-1), mIndex(-1), mPos(pos
) 
 401         virtual ~Type() { delete mFirstPublicSourcePos
; } 
 403         status_t 
addPublic(const SourcePos
& pos
, 
 404                            const String16
& name
, 
 405                            const uint32_t ident
); 
 407         String16 
getName() const { return mName
; } 
 408         sp
<Entry
> getEntry(const String16
& entry
, 
 409                            const SourcePos
& pos
, 
 410                            const ResTable_config
* config 
= NULL
, 
 411                            bool doSetIndex 
= false); 
 413         const SourcePos
& getFirstPublicSourcePos() const { return *mFirstPublicSourcePos
; } 
 415         int32_t getPublicIndex() const { return mPublicIndex
; } 
 417         int32_t getIndex() const { return mIndex
; } 
 418         void setIndex(int32_t index
) { mIndex 
= index
; } 
 420         status_t 
applyPublicEntryOrder(); 
 422         const SortedVector
<ConfigDescription
>& getUniqueConfigs() const { return mUniqueConfigs
; } 
 424         const DefaultKeyedVector
<String16
, sp
<ConfigList
> >& getConfigs() const { return mConfigs
; } 
 425         const Vector
<sp
<ConfigList
> >& getOrderedConfigs() const { return mOrderedConfigs
; } 
 427         const SourcePos
& getPos() const { return mPos
; } 
 430         SourcePos
* mFirstPublicSourcePos
; 
 431         DefaultKeyedVector
<String16
, Public
> mPublic
; 
 432         SortedVector
<ConfigDescription
> mUniqueConfigs
; 
 433         DefaultKeyedVector
<String16
, sp
<ConfigList
> > mConfigs
; 
 434         Vector
<sp
<ConfigList
> > mOrderedConfigs
; 
 435         int32_t mPublicIndex
; 
 440     class Package 
: public RefBase 
{ 
 442         Package(const String16
& name
, ssize_t includedId
=-1); 
 443         virtual ~Package() { } 
 445         String16 
getName() const { return mName
; } 
 446         sp
<Type
> getType(const String16
& type
, 
 447                          const SourcePos
& pos
, 
 448                          bool doSetIndex 
= false); 
 450         ssize_t 
getAssignedId() const { return mIncludedId
; } 
 452         const ResStringPool
& getTypeStrings() const { return mTypeStrings
; } 
 453         uint32_t indexOfTypeString(const String16
& s
) const { return mTypeStringsMapping
.valueFor(s
); } 
 454         const sp
<AaptFile
> getTypeStringsData() const { return mTypeStringsData
; } 
 455         status_t 
setTypeStrings(const sp
<AaptFile
>& data
); 
 457         const ResStringPool
& getKeyStrings() const { return mKeyStrings
; } 
 458         uint32_t indexOfKeyString(const String16
& s
) const { return mKeyStringsMapping
.valueFor(s
); } 
 459         const sp
<AaptFile
> getKeyStringsData() const { return mKeyStringsData
; } 
 460         status_t 
setKeyStrings(const sp
<AaptFile
>& data
); 
 462         status_t 
applyPublicTypeOrder(); 
 464         const DefaultKeyedVector
<String16
, sp
<Type
> >& getTypes() const { return mTypes
; } 
 465         const Vector
<sp
<Type
> >& getOrderedTypes() const { return mOrderedTypes
; } 
 468         status_t 
setStrings(const sp
<AaptFile
>& data
, 
 469                             ResStringPool
* strings
, 
 470                             DefaultKeyedVector
<String16
, uint32_t>* mappings
); 
 472         const String16 mName
; 
 473         const ssize_t mIncludedId
; 
 474         DefaultKeyedVector
<String16
, sp
<Type
> > mTypes
; 
 475         Vector
<sp
<Type
> > mOrderedTypes
; 
 476         sp
<AaptFile
> mTypeStringsData
; 
 477         sp
<AaptFile
> mKeyStringsData
; 
 478         ResStringPool mTypeStrings
; 
 479         ResStringPool mKeyStrings
; 
 480         DefaultKeyedVector
<String16
, uint32_t> mTypeStringsMapping
; 
 481         DefaultKeyedVector
<String16
, uint32_t> mKeyStringsMapping
; 
 485     void writePublicDefinitions(const String16
& package
, FILE* fp
, bool pub
); 
 486     sp
<Package
> getPackage(const String16
& package
); 
 487     sp
<Type
> getType(const String16
& package
, 
 488                      const String16
& type
, 
 489                      const SourcePos
& pos
, 
 490                      bool doSetIndex 
= false); 
 491     sp
<Entry
> getEntry(const String16
& package
, 
 492                        const String16
& type
, 
 493                        const String16
& name
, 
 494                        const SourcePos
& pos
, 
 495                        const ResTable_config
* config 
= NULL
, 
 496                        bool doSetIndex 
= false); 
 497     sp
<const Entry
> getEntry(uint32_t resID
, 
 498                              const ResTable_config
* config 
= NULL
) const; 
 499     const Item
* getItem(uint32_t resID
, uint32_t attrID
) const; 
 500     bool getItemValue(uint32_t resID
, uint32_t attrID
, 
 501                       Res_value
* outValue
); 
 504     String16 mAssetsPackage
; 
 505     sp
<AaptAssets
> mAssets
; 
 506     DefaultKeyedVector
<String16
, sp
<Package
> > mPackages
; 
 507     Vector
<sp
<Package
> > mOrderedPackages
; 
 508     uint32_t mNextPackageId
; 
 509     bool mHaveAppPackage
; 
 512     SourcePos mCurrentXmlPos
; 
 515     // key = string resource name, value = set of locales in which that name is defined 
 516     map
<String16
, set
<String8
> > mLocalizations
; 
 522     ResourceFilter() : mData(), mContainsPseudo(false) {} 
 523     status_t 
parse(const char* arg
); 
 524     bool match(int axis
, uint32_t value
); 
 525     bool match(const ResTable_config
& config
); 
 526     inline bool containsPseudo() { return mContainsPseudo
; } 
 529     KeyedVector
<int,SortedVector
<uint32_t> > mData
; 
 530     bool mContainsPseudo
;