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"
22 XML_COMPILE_STRIP_COMMENTS
= 1<<0,
23 XML_COMPILE_ASSIGN_ATTRIBUTE_IDS
= 1<<1,
24 XML_COMPILE_COMPACT_WHITESPACE
= 1<<2,
25 XML_COMPILE_STRIP_WHITESPACE
= 1<<3,
26 XML_COMPILE_STRIP_RAW_VALUES
= 1<<4,
28 XML_COMPILE_STANDARD_RESOURCE
=
29 XML_COMPILE_STRIP_COMMENTS
| XML_COMPILE_ASSIGN_ATTRIBUTE_IDS
30 | XML_COMPILE_STRIP_WHITESPACE
| XML_COMPILE_STRIP_RAW_VALUES
33 status_t
compileXmlFile(const sp
<AaptAssets
>& assets
,
34 const sp
<AaptFile
>& target
,
36 int options
= XML_COMPILE_STANDARD_RESOURCE
);
38 status_t
compileXmlFile(const sp
<AaptAssets
>& assets
,
39 const sp
<XMLNode
>& xmlTree
,
40 const sp
<AaptFile
>& target
,
42 int options
= XML_COMPILE_STANDARD_RESOURCE
);
44 status_t
compileResourceFile(Bundle
* bundle
,
45 const sp
<AaptAssets
>& assets
,
46 const sp
<AaptFile
>& in
,
47 const ResTable_config
& defParams
,
49 ResourceTable
* outTable
);
57 AccessorCookie(const SourcePos
&p
, const String8
& a
, const String8
& v
)
65 class ResourceTable
: public ResTable::Accessor
72 ResourceTable(Bundle
* bundle
, const String16
& assetsPackage
);
74 status_t
addIncludedResources(Bundle
* bundle
, const sp
<AaptAssets
>& assets
);
76 status_t
addPublic(const SourcePos
& pos
,
77 const String16
& package
,
80 const uint32_t ident
);
82 status_t
addEntry(const SourcePos
& pos
,
83 const String16
& package
,
86 const String16
& value
,
87 const Vector
<StringPool::entry_style_span
>* style
= NULL
,
88 const ResTable_config
* params
= NULL
,
89 const bool doSetIndex
= false,
90 const int32_t format
= ResTable_map::TYPE_ANY
,
91 const bool overwrite
= false);
93 status_t
startBag(const SourcePos
& pos
,
94 const String16
& package
,
97 const String16
& bagParent
,
98 const ResTable_config
* params
= NULL
,
102 status_t
addBag(const SourcePos
& pos
,
103 const String16
& package
,
104 const String16
& type
,
105 const String16
& name
,
106 const String16
& bagParent
,
107 const String16
& bagKey
,
108 const String16
& value
,
109 const Vector
<StringPool::entry_style_span
>* style
= NULL
,
110 const ResTable_config
* params
= NULL
,
111 bool replace
= false,
113 const int32_t format
= ResTable_map::TYPE_ANY
);
115 bool hasBagOrEntry(const String16
& package
,
116 const String16
& type
,
117 const String16
& name
) const;
119 bool hasBagOrEntry(const String16
& ref
,
120 const String16
* defType
= NULL
,
121 const String16
* defPackage
= NULL
);
123 bool appendComment(const String16
& package
,
124 const String16
& type
,
125 const String16
& name
,
126 const String16
& comment
,
127 bool onlyIfEmpty
= false);
129 bool appendTypeComment(const String16
& package
,
130 const String16
& type
,
131 const String16
& name
,
132 const String16
& comment
);
135 size_t numLocalResources() const;
136 bool hasResources() const;
138 sp
<AaptFile
> flatten(Bundle
*);
140 static inline uint32_t makeResId(uint32_t packageId
,
144 return nameId
| (typeId
<<16) | (packageId
<<24);
147 static inline uint32_t getResId(const sp
<Package
>& p
,
151 uint32_t getResId(const String16
& package
,
152 const String16
& type
,
153 const String16
& name
,
154 bool onlyPublic
= false) const;
156 uint32_t getResId(const String16
& ref
,
157 const String16
* defType
= NULL
,
158 const String16
* defPackage
= NULL
,
159 const char** outErrorMsg
= NULL
,
160 bool onlyPublic
= false) const;
162 static bool isValidResourceName(const String16
& s
);
164 bool stringToValue(Res_value
* outValue
, StringPool
* pool
,
166 bool preserveSpaces
, bool coerceType
,
168 const Vector
<StringPool::entry_style_span
>* style
= NULL
,
169 String16
* outStr
= NULL
, void* accessorCookie
= NULL
,
170 uint32_t attrType
= ResTable_map::TYPE_ANY
);
172 status_t
assignResourceIds();
173 status_t
addSymbols(const sp
<AaptSymbols
>& outSymbols
= NULL
);
174 void addLocalization(const String16
& name
, const String8
& locale
);
175 status_t
validateLocalizations(void);
177 status_t
flatten(Bundle
*, const sp
<AaptFile
>& dest
);
179 void writePublicDefinitions(const String16
& package
, FILE* fp
);
181 virtual uint32_t getCustomResource(const String16
& package
,
182 const String16
& type
,
183 const String16
& name
) const;
184 virtual uint32_t getCustomResourceWithCreation(const String16
& package
,
185 const String16
& type
,
186 const String16
& name
,
187 const bool createIfNeeded
);
188 virtual uint32_t getRemappedPackage(uint32_t origPackage
) const;
189 virtual bool getAttributeType(uint32_t attrID
, uint32_t* outType
);
190 virtual bool getAttributeMin(uint32_t attrID
, uint32_t* outMin
);
191 virtual bool getAttributeMax(uint32_t attrID
, uint32_t* outMax
);
192 virtual bool getAttributeKeys(uint32_t attrID
, Vector
<String16
>* outKeys
);
193 virtual bool getAttributeEnum(uint32_t attrID
,
194 const char16_t* name
, size_t nameLen
,
195 Res_value
* outValue
);
196 virtual bool getAttributeFlags(uint32_t attrID
,
197 const char16_t* name
, size_t nameLen
,
198 Res_value
* outValue
);
199 virtual uint32_t getAttributeL10N(uint32_t attrID
);
201 virtual bool getLocalizationSetting();
202 virtual void reportError(void* accessorCookie
, const char* fmt
, ...);
204 void setCurrentXmlPos(const SourcePos
& pos
) { mCurrentXmlPos
= pos
; }
208 Item() : isId(false), format(ResTable_map::TYPE_ANY
), bagKeyId(0), evaluating(false)
209 { memset(&parsedValue
, 0, sizeof(parsedValue
)); }
210 Item(const SourcePos
& pos
,
212 const String16
& _value
,
213 const Vector
<StringPool::entry_style_span
>* _style
= NULL
,
214 int32_t format
= ResTable_map::TYPE_ANY
);
215 Item(const Item
& o
) : sourcePos(o
.sourcePos
),
216 isId(o
.isId
), value(o
.value
), style(o
.style
),
217 format(o
.format
), bagKeyId(o
.bagKeyId
), evaluating(false) {
218 memset(&parsedValue
, 0, sizeof(parsedValue
));
222 Item
& operator=(const Item
& o
) {
223 sourcePos
= o
.sourcePos
;
228 bagKeyId
= o
.bagKeyId
;
229 parsedValue
= o
.parsedValue
;
236 Vector
<StringPool::entry_style_span
> style
;
239 mutable bool evaluating
;
240 Res_value parsedValue
;
243 class Entry
: public RefBase
{
245 Entry(const String16
& name
, const SourcePos
& pos
)
246 : mName(name
), mType(TYPE_UNKNOWN
),
247 mItemFormat(ResTable_map::TYPE_ANY
), mNameIndex(-1), mPos(pos
)
257 String16
getName() const { return mName
; }
258 type
getType() const { return mType
; }
260 void setParent(const String16
& parent
) { mParent
= parent
; }
261 String16
getParent() const { return mParent
; }
263 status_t
makeItABag(const SourcePos
& sourcePos
);
265 status_t
emptyBag(const SourcePos
& sourcePos
);
267 status_t
setItem(const SourcePos
& pos
,
268 const String16
& value
,
269 const Vector
<StringPool::entry_style_span
>* style
= NULL
,
270 int32_t format
= ResTable_map::TYPE_ANY
,
271 const bool overwrite
= false);
273 status_t
addToBag(const SourcePos
& pos
,
274 const String16
& key
, const String16
& value
,
275 const Vector
<StringPool::entry_style_span
>* style
= NULL
,
276 bool replace
=false, bool isId
= false,
277 int32_t format
= ResTable_map::TYPE_ANY
);
279 // Index of the entry's name string in the key pool.
280 int32_t getNameIndex() const { return mNameIndex
; }
281 void setNameIndex(int32_t index
) { mNameIndex
= index
; }
283 const Item
* getItem() const { return mType
== TYPE_ITEM
? &mItem
: NULL
; }
284 const KeyedVector
<String16
, Item
>& getBag() const { return mBag
; }
286 status_t
generateAttributes(ResourceTable
* table
,
287 const String16
& package
);
289 status_t
assignResourceIds(ResourceTable
* table
,
290 const String16
& package
);
292 status_t
prepareFlatten(StringPool
* strings
, ResourceTable
* table
);
294 ssize_t
flatten(Bundle
*, const sp
<AaptFile
>& data
, bool isPublic
);
296 const SourcePos
& getPos() const { return mPos
; }
304 KeyedVector
<String16
, Item
> mBag
;
310 struct ConfigDescription
: public ResTable_config
{
311 ConfigDescription() {
312 memset(this, 0, sizeof(*this));
313 size
= sizeof(ResTable_config
);
315 ConfigDescription(const ResTable_config
&o
) {
316 *static_cast<ResTable_config
*>(this) = o
;
317 size
= sizeof(ResTable_config
);
319 ConfigDescription(const ConfigDescription
&o
) {
320 *static_cast<ResTable_config
*>(this) = o
;
323 ConfigDescription
& operator=(const ResTable_config
& o
) {
324 *static_cast<ResTable_config
*>(this) = o
;
325 size
= sizeof(ResTable_config
);
328 ConfigDescription
& operator=(const ConfigDescription
& o
) {
329 *static_cast<ResTable_config
*>(this) = o
;
333 inline bool operator<(const ConfigDescription
& o
) const { return compare(o
) < 0; }
334 inline bool operator<=(const ConfigDescription
& o
) const { return compare(o
) <= 0; }
335 inline bool operator==(const ConfigDescription
& o
) const { return compare(o
) == 0; }
336 inline bool operator!=(const ConfigDescription
& o
) const { return compare(o
) != 0; }
337 inline bool operator>=(const ConfigDescription
& o
) const { return compare(o
) >= 0; }
338 inline bool operator>(const ConfigDescription
& o
) const { return compare(o
) > 0; }
341 class ConfigList
: public RefBase
{
343 ConfigList(const String16
& name
, const SourcePos
& pos
)
344 : mName(name
), mPos(pos
), mPublic(false), mEntryIndex(-1) { }
345 virtual ~ConfigList() { }
347 String16
getName() const { return mName
; }
348 const SourcePos
& getPos() const { return mPos
; }
350 void appendComment(const String16
& comment
, bool onlyIfEmpty
= false);
351 const String16
& getComment() const { return mComment
; }
353 void appendTypeComment(const String16
& comment
);
354 const String16
& getTypeComment() const { return mTypeComment
; }
356 // Index of this entry in its Type.
357 int32_t getEntryIndex() const { return mEntryIndex
; }
358 void setEntryIndex(int32_t index
) { mEntryIndex
= index
; }
360 void setPublic(bool pub
) { mPublic
= pub
; }
361 bool getPublic() const { return mPublic
; }
362 void setPublicSourcePos(const SourcePos
& pos
) { mPublicSourcePos
= pos
; }
363 const SourcePos
& getPublicSourcePos() { return mPublicSourcePos
; }
365 void addEntry(const ResTable_config
& config
, const sp
<Entry
>& entry
) {
366 mEntries
.add(config
, entry
);
369 const DefaultKeyedVector
<ConfigDescription
, sp
<Entry
> >& getEntries() const { return mEntries
; }
371 const String16 mName
;
372 const SourcePos mPos
;
374 String16 mTypeComment
;
376 SourcePos mPublicSourcePos
;
378 DefaultKeyedVector
<ConfigDescription
, sp
<Entry
> > mEntries
;
383 Public() : sourcePos(), ident(0) { }
384 Public(const SourcePos
& pos
,
385 const String16
& _comment
,
388 comment(_comment
), ident(_ident
) { }
389 Public(const Public
& o
) : sourcePos(o
.sourcePos
),
390 comment(o
.comment
), ident(o
.ident
) { }
393 Public
& operator=(const Public
& o
) {
394 sourcePos
= o
.sourcePos
;
405 class Type
: public RefBase
{
407 Type(const String16
& name
, const SourcePos
& pos
)
408 : mName(name
), mFirstPublicSourcePos(NULL
), mPublicIndex(-1), mIndex(-1), mPos(pos
)
410 virtual ~Type() { delete mFirstPublicSourcePos
; }
412 status_t
addPublic(const SourcePos
& pos
,
413 const String16
& name
,
414 const uint32_t ident
);
416 String16
getName() const { return mName
; }
417 sp
<Entry
> getEntry(const String16
& entry
,
418 const SourcePos
& pos
,
419 const ResTable_config
* config
= NULL
,
420 bool doSetIndex
= false);
422 const SourcePos
& getFirstPublicSourcePos() const { return *mFirstPublicSourcePos
; }
424 int32_t getPublicIndex() const { return mPublicIndex
; }
426 int32_t getIndex() const { return mIndex
; }
427 void setIndex(int32_t index
) { mIndex
= index
; }
429 status_t
applyPublicEntryOrder();
431 const SortedVector
<ConfigDescription
>& getUniqueConfigs() const { return mUniqueConfigs
; }
433 const DefaultKeyedVector
<String16
, sp
<ConfigList
> >& getConfigs() const { return mConfigs
; }
434 const Vector
<sp
<ConfigList
> >& getOrderedConfigs() const { return mOrderedConfigs
; }
436 const SourcePos
& getPos() const { return mPos
; }
439 SourcePos
* mFirstPublicSourcePos
;
440 DefaultKeyedVector
<String16
, Public
> mPublic
;
441 SortedVector
<ConfigDescription
> mUniqueConfigs
;
442 DefaultKeyedVector
<String16
, sp
<ConfigList
> > mConfigs
;
443 Vector
<sp
<ConfigList
> > mOrderedConfigs
;
444 int32_t mPublicIndex
;
449 class Package
: public RefBase
{
451 Package(const String16
& name
, ssize_t includedId
=-1);
452 virtual ~Package() { }
454 String16
getName() const { return mName
; }
455 sp
<Type
> getType(const String16
& type
,
456 const SourcePos
& pos
,
457 bool doSetIndex
= false);
459 ssize_t
getAssignedId() const { return mIncludedId
; }
461 const ResStringPool
& getTypeStrings() const { return mTypeStrings
; }
462 uint32_t indexOfTypeString(const String16
& s
) const { return mTypeStringsMapping
.valueFor(s
); }
463 const sp
<AaptFile
> getTypeStringsData() const { return mTypeStringsData
; }
464 status_t
setTypeStrings(const sp
<AaptFile
>& data
);
466 const ResStringPool
& getKeyStrings() const { return mKeyStrings
; }
467 uint32_t indexOfKeyString(const String16
& s
) const { return mKeyStringsMapping
.valueFor(s
); }
468 const sp
<AaptFile
> getKeyStringsData() const { return mKeyStringsData
; }
469 status_t
setKeyStrings(const sp
<AaptFile
>& data
);
471 status_t
applyPublicTypeOrder();
473 const DefaultKeyedVector
<String16
, sp
<Type
> >& getTypes() const { return mTypes
; }
474 const Vector
<sp
<Type
> >& getOrderedTypes() const { return mOrderedTypes
; }
477 status_t
setStrings(const sp
<AaptFile
>& data
,
478 ResStringPool
* strings
,
479 DefaultKeyedVector
<String16
, uint32_t>* mappings
);
481 const String16 mName
;
482 const ssize_t mIncludedId
;
483 DefaultKeyedVector
<String16
, sp
<Type
> > mTypes
;
484 Vector
<sp
<Type
> > mOrderedTypes
;
485 sp
<AaptFile
> mTypeStringsData
;
486 sp
<AaptFile
> mKeyStringsData
;
487 ResStringPool mTypeStrings
;
488 ResStringPool mKeyStrings
;
489 DefaultKeyedVector
<String16
, uint32_t> mTypeStringsMapping
;
490 DefaultKeyedVector
<String16
, uint32_t> mKeyStringsMapping
;
494 void writePublicDefinitions(const String16
& package
, FILE* fp
, bool pub
);
495 sp
<Package
> getPackage(const String16
& package
);
496 sp
<Type
> getType(const String16
& package
,
497 const String16
& type
,
498 const SourcePos
& pos
,
499 bool doSetIndex
= false);
500 sp
<Entry
> getEntry(const String16
& package
,
501 const String16
& type
,
502 const String16
& name
,
503 const SourcePos
& pos
,
504 const ResTable_config
* config
= NULL
,
505 bool doSetIndex
= false);
506 sp
<const Entry
> getEntry(uint32_t resID
,
507 const ResTable_config
* config
= NULL
) const;
508 const Item
* getItem(uint32_t resID
, uint32_t attrID
) const;
509 bool getItemValue(uint32_t resID
, uint32_t attrID
,
510 Res_value
* outValue
);
513 String16 mAssetsPackage
;
514 sp
<AaptAssets
> mAssets
;
515 DefaultKeyedVector
<String16
, sp
<Package
> > mPackages
;
516 Vector
<sp
<Package
> > mOrderedPackages
;
517 uint32_t mNextPackageId
;
518 bool mHaveAppPackage
;
521 SourcePos mCurrentXmlPos
;
524 // key = string resource name, value = set of locales in which that name is defined
525 map
<String16
, set
<String8
> > mLocalizations
;
531 ResourceFilter() : mData(), mContainsPseudo(false) {}
532 status_t
parse(const char* arg
);
533 bool match(int axis
, uint32_t value
);
534 bool match(const ResTable_config
& config
);
535 inline bool containsPseudo() { return mContainsPseudo
; }
538 KeyedVector
<int,SortedVector
<uint32_t> > mData
;
539 bool mContainsPseudo
;