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
,
100 bool replace
= false,
103 status_t
addBag(const SourcePos
& pos
,
104 const String16
& package
,
105 const String16
& type
,
106 const String16
& name
,
107 const String16
& bagParent
,
108 const String16
& bagKey
,
109 const String16
& value
,
110 const Vector
<StringPool::entry_style_span
>* style
= NULL
,
111 const ResTable_config
* params
= NULL
,
112 bool replace
= false,
114 const int32_t format
= ResTable_map::TYPE_ANY
);
116 bool hasBagOrEntry(const String16
& package
,
117 const String16
& type
,
118 const String16
& name
) const;
120 bool hasBagOrEntry(const String16
& ref
,
121 const String16
* defType
= NULL
,
122 const String16
* defPackage
= NULL
);
124 bool appendComment(const String16
& package
,
125 const String16
& type
,
126 const String16
& name
,
127 const String16
& comment
,
128 bool onlyIfEmpty
= false);
130 bool appendTypeComment(const String16
& package
,
131 const String16
& type
,
132 const String16
& name
,
133 const String16
& comment
);
135 void canAddEntry(const SourcePos
& pos
,
136 const String16
& package
, const String16
& type
, const String16
& name
);
139 size_t numLocalResources() const;
140 bool hasResources() const;
142 sp
<AaptFile
> flatten(Bundle
*);
144 static inline uint32_t makeResId(uint32_t packageId
,
148 return nameId
| (typeId
<<16) | (packageId
<<24);
151 static inline uint32_t getResId(const sp
<Package
>& p
,
155 uint32_t getResId(const String16
& package
,
156 const String16
& type
,
157 const String16
& name
,
158 bool onlyPublic
= false) const;
160 uint32_t getResId(const String16
& ref
,
161 const String16
* defType
= NULL
,
162 const String16
* defPackage
= NULL
,
163 const char** outErrorMsg
= NULL
,
164 bool onlyPublic
= false) const;
166 static bool isValidResourceName(const String16
& s
);
168 bool stringToValue(Res_value
* outValue
, StringPool
* pool
,
170 bool preserveSpaces
, bool coerceType
,
172 const Vector
<StringPool::entry_style_span
>* style
= NULL
,
173 String16
* outStr
= NULL
, void* accessorCookie
= NULL
,
174 uint32_t attrType
= ResTable_map::TYPE_ANY
);
176 status_t
assignResourceIds();
177 status_t
addSymbols(const sp
<AaptSymbols
>& outSymbols
= NULL
);
178 void addLocalization(const String16
& name
, const String8
& locale
);
179 status_t
validateLocalizations(void);
181 status_t
flatten(Bundle
*, const sp
<AaptFile
>& dest
);
183 void writePublicDefinitions(const String16
& package
, FILE* fp
);
185 virtual uint32_t getCustomResource(const String16
& package
,
186 const String16
& type
,
187 const String16
& name
) const;
188 virtual uint32_t getCustomResourceWithCreation(const String16
& package
,
189 const String16
& type
,
190 const String16
& name
,
191 const bool createIfNeeded
);
192 virtual uint32_t getRemappedPackage(uint32_t origPackage
) const;
193 virtual bool getAttributeType(uint32_t attrID
, uint32_t* outType
);
194 virtual bool getAttributeMin(uint32_t attrID
, uint32_t* outMin
);
195 virtual bool getAttributeMax(uint32_t attrID
, uint32_t* outMax
);
196 virtual bool getAttributeKeys(uint32_t attrID
, Vector
<String16
>* outKeys
);
197 virtual bool getAttributeEnum(uint32_t attrID
,
198 const char16_t* name
, size_t nameLen
,
199 Res_value
* outValue
);
200 virtual bool getAttributeFlags(uint32_t attrID
,
201 const char16_t* name
, size_t nameLen
,
202 Res_value
* outValue
);
203 virtual uint32_t getAttributeL10N(uint32_t attrID
);
205 virtual bool getLocalizationSetting();
206 virtual void reportError(void* accessorCookie
, const char* fmt
, ...);
208 void setCurrentXmlPos(const SourcePos
& pos
) { mCurrentXmlPos
= pos
; }
212 Item() : isId(false), format(ResTable_map::TYPE_ANY
), bagKeyId(0), evaluating(false)
213 { memset(&parsedValue
, 0, sizeof(parsedValue
)); }
214 Item(const SourcePos
& pos
,
216 const String16
& _value
,
217 const Vector
<StringPool::entry_style_span
>* _style
= NULL
,
218 int32_t format
= ResTable_map::TYPE_ANY
);
219 Item(const Item
& o
) : sourcePos(o
.sourcePos
),
220 isId(o
.isId
), value(o
.value
), style(o
.style
),
221 format(o
.format
), bagKeyId(o
.bagKeyId
), evaluating(false) {
222 memset(&parsedValue
, 0, sizeof(parsedValue
));
226 Item
& operator=(const Item
& o
) {
227 sourcePos
= o
.sourcePos
;
232 bagKeyId
= o
.bagKeyId
;
233 parsedValue
= o
.parsedValue
;
240 Vector
<StringPool::entry_style_span
> style
;
243 mutable bool evaluating
;
244 Res_value parsedValue
;
247 class Entry
: public RefBase
{
249 Entry(const String16
& name
, const SourcePos
& pos
)
250 : mName(name
), mType(TYPE_UNKNOWN
),
251 mItemFormat(ResTable_map::TYPE_ANY
), mNameIndex(-1), mPos(pos
)
261 String16
getName() const { return mName
; }
262 type
getType() const { return mType
; }
264 void setParent(const String16
& parent
) { mParent
= parent
; }
265 String16
getParent() const { return mParent
; }
267 status_t
makeItABag(const SourcePos
& sourcePos
);
269 status_t
emptyBag(const SourcePos
& sourcePos
);
271 status_t
setItem(const SourcePos
& pos
,
272 const String16
& value
,
273 const Vector
<StringPool::entry_style_span
>* style
= NULL
,
274 int32_t format
= ResTable_map::TYPE_ANY
,
275 const bool overwrite
= false);
277 status_t
addToBag(const SourcePos
& pos
,
278 const String16
& key
, const String16
& value
,
279 const Vector
<StringPool::entry_style_span
>* style
= NULL
,
280 bool replace
=false, bool isId
= false,
281 int32_t format
= ResTable_map::TYPE_ANY
);
283 // Index of the entry's name string in the key pool.
284 int32_t getNameIndex() const { return mNameIndex
; }
285 void setNameIndex(int32_t index
) { mNameIndex
= index
; }
287 const Item
* getItem() const { return mType
== TYPE_ITEM
? &mItem
: NULL
; }
288 const KeyedVector
<String16
, Item
>& getBag() const { return mBag
; }
290 status_t
generateAttributes(ResourceTable
* table
,
291 const String16
& package
);
293 status_t
assignResourceIds(ResourceTable
* table
,
294 const String16
& package
);
296 status_t
prepareFlatten(StringPool
* strings
, ResourceTable
* table
);
298 ssize_t
flatten(Bundle
*, const sp
<AaptFile
>& data
, bool isPublic
);
300 const SourcePos
& getPos() const { return mPos
; }
308 KeyedVector
<String16
, Item
> mBag
;
314 struct ConfigDescription
: public ResTable_config
{
315 ConfigDescription() {
316 memset(this, 0, sizeof(*this));
317 size
= sizeof(ResTable_config
);
319 ConfigDescription(const ResTable_config
&o
) {
320 *static_cast<ResTable_config
*>(this) = o
;
321 size
= sizeof(ResTable_config
);
323 ConfigDescription(const ConfigDescription
&o
) {
324 *static_cast<ResTable_config
*>(this) = o
;
327 ConfigDescription
& operator=(const ResTable_config
& o
) {
328 *static_cast<ResTable_config
*>(this) = o
;
329 size
= sizeof(ResTable_config
);
332 ConfigDescription
& operator=(const ConfigDescription
& o
) {
333 *static_cast<ResTable_config
*>(this) = o
;
337 inline bool operator<(const ConfigDescription
& o
) const { return compare(o
) < 0; }
338 inline bool operator<=(const ConfigDescription
& o
) const { return compare(o
) <= 0; }
339 inline bool operator==(const ConfigDescription
& o
) const { return compare(o
) == 0; }
340 inline bool operator!=(const ConfigDescription
& o
) const { return compare(o
) != 0; }
341 inline bool operator>=(const ConfigDescription
& o
) const { return compare(o
) >= 0; }
342 inline bool operator>(const ConfigDescription
& o
) const { return compare(o
) > 0; }
345 class ConfigList
: public RefBase
{
347 ConfigList(const String16
& name
, const SourcePos
& pos
)
348 : mName(name
), mPos(pos
), mPublic(false), mEntryIndex(-1) { }
349 virtual ~ConfigList() { }
351 String16
getName() const { return mName
; }
352 const SourcePos
& getPos() const { return mPos
; }
354 void appendComment(const String16
& comment
, bool onlyIfEmpty
= false);
355 const String16
& getComment() const { return mComment
; }
357 void appendTypeComment(const String16
& comment
);
358 const String16
& getTypeComment() const { return mTypeComment
; }
360 // Index of this entry in its Type.
361 int32_t getEntryIndex() const { return mEntryIndex
; }
362 void setEntryIndex(int32_t index
) { mEntryIndex
= index
; }
364 void setPublic(bool pub
) { mPublic
= pub
; }
365 bool getPublic() const { return mPublic
; }
366 void setPublicSourcePos(const SourcePos
& pos
) { mPublicSourcePos
= pos
; }
367 const SourcePos
& getPublicSourcePos() { return mPublicSourcePos
; }
369 void addEntry(const ResTable_config
& config
, const sp
<Entry
>& entry
) {
370 mEntries
.add(config
, entry
);
373 const DefaultKeyedVector
<ConfigDescription
, sp
<Entry
> >& getEntries() const { return mEntries
; }
375 const String16 mName
;
376 const SourcePos mPos
;
378 String16 mTypeComment
;
380 SourcePos mPublicSourcePos
;
382 DefaultKeyedVector
<ConfigDescription
, sp
<Entry
> > mEntries
;
387 Public() : sourcePos(), ident(0) { }
388 Public(const SourcePos
& pos
,
389 const String16
& _comment
,
392 comment(_comment
), ident(_ident
) { }
393 Public(const Public
& o
) : sourcePos(o
.sourcePos
),
394 comment(o
.comment
), ident(o
.ident
) { }
397 Public
& operator=(const Public
& o
) {
398 sourcePos
= o
.sourcePos
;
409 class Type
: public RefBase
{
411 Type(const String16
& name
, const SourcePos
& pos
)
412 : mName(name
), mFirstPublicSourcePos(NULL
), mPublicIndex(-1), mIndex(-1), mPos(pos
)
414 virtual ~Type() { delete mFirstPublicSourcePos
; }
416 status_t
addPublic(const SourcePos
& pos
,
417 const String16
& name
,
418 const uint32_t ident
);
420 void canAddEntry(const String16
& name
);
422 String16
getName() const { return mName
; }
423 sp
<Entry
> getEntry(const String16
& entry
,
424 const SourcePos
& pos
,
425 const ResTable_config
* config
= NULL
,
426 bool doSetIndex
= false,
427 bool overlay
= false,
428 bool autoAddOverlay
= false);
430 const SourcePos
& getFirstPublicSourcePos() const { return *mFirstPublicSourcePos
; }
432 int32_t getPublicIndex() const { return mPublicIndex
; }
434 int32_t getIndex() const { return mIndex
; }
435 void setIndex(int32_t index
) { mIndex
= index
; }
437 status_t
applyPublicEntryOrder();
439 const SortedVector
<ConfigDescription
>& getUniqueConfigs() const { return mUniqueConfigs
; }
441 const DefaultKeyedVector
<String16
, sp
<ConfigList
> >& getConfigs() const { return mConfigs
; }
442 const Vector
<sp
<ConfigList
> >& getOrderedConfigs() const { return mOrderedConfigs
; }
444 const SortedVector
<String16
>& getCanAddEntries() const { return mCanAddEntries
; }
446 const SourcePos
& getPos() const { return mPos
; }
449 SourcePos
* mFirstPublicSourcePos
;
450 DefaultKeyedVector
<String16
, Public
> mPublic
;
451 SortedVector
<ConfigDescription
> mUniqueConfigs
;
452 DefaultKeyedVector
<String16
, sp
<ConfigList
> > mConfigs
;
453 Vector
<sp
<ConfigList
> > mOrderedConfigs
;
454 SortedVector
<String16
> mCanAddEntries
;
455 int32_t mPublicIndex
;
460 class Package
: public RefBase
{
462 Package(const String16
& name
, ssize_t includedId
=-1);
463 virtual ~Package() { }
465 String16
getName() const { return mName
; }
466 sp
<Type
> getType(const String16
& type
,
467 const SourcePos
& pos
,
468 bool doSetIndex
= false);
470 ssize_t
getAssignedId() const { return mIncludedId
; }
472 const ResStringPool
& getTypeStrings() const { return mTypeStrings
; }
473 uint32_t indexOfTypeString(const String16
& s
) const { return mTypeStringsMapping
.valueFor(s
); }
474 const sp
<AaptFile
> getTypeStringsData() const { return mTypeStringsData
; }
475 status_t
setTypeStrings(const sp
<AaptFile
>& data
);
477 const ResStringPool
& getKeyStrings() const { return mKeyStrings
; }
478 uint32_t indexOfKeyString(const String16
& s
) const { return mKeyStringsMapping
.valueFor(s
); }
479 const sp
<AaptFile
> getKeyStringsData() const { return mKeyStringsData
; }
480 status_t
setKeyStrings(const sp
<AaptFile
>& data
);
482 status_t
applyPublicTypeOrder();
484 const DefaultKeyedVector
<String16
, sp
<Type
> >& getTypes() const { return mTypes
; }
485 const Vector
<sp
<Type
> >& getOrderedTypes() const { return mOrderedTypes
; }
488 status_t
setStrings(const sp
<AaptFile
>& data
,
489 ResStringPool
* strings
,
490 DefaultKeyedVector
<String16
, uint32_t>* mappings
);
492 const String16 mName
;
493 const ssize_t mIncludedId
;
494 DefaultKeyedVector
<String16
, sp
<Type
> > mTypes
;
495 Vector
<sp
<Type
> > mOrderedTypes
;
496 sp
<AaptFile
> mTypeStringsData
;
497 sp
<AaptFile
> mKeyStringsData
;
498 ResStringPool mTypeStrings
;
499 ResStringPool mKeyStrings
;
500 DefaultKeyedVector
<String16
, uint32_t> mTypeStringsMapping
;
501 DefaultKeyedVector
<String16
, uint32_t> mKeyStringsMapping
;
505 void writePublicDefinitions(const String16
& package
, FILE* fp
, bool pub
);
506 sp
<Package
> getPackage(const String16
& package
);
507 sp
<Type
> getType(const String16
& package
,
508 const String16
& type
,
509 const SourcePos
& pos
,
510 bool doSetIndex
= false);
511 sp
<Entry
> getEntry(const String16
& package
,
512 const String16
& type
,
513 const String16
& name
,
514 const SourcePos
& pos
,
516 const ResTable_config
* config
= NULL
,
517 bool doSetIndex
= false);
518 sp
<const Entry
> getEntry(uint32_t resID
,
519 const ResTable_config
* config
= NULL
) const;
520 const Item
* getItem(uint32_t resID
, uint32_t attrID
) const;
521 bool getItemValue(uint32_t resID
, uint32_t attrID
,
522 Res_value
* outValue
);
525 String16 mAssetsPackage
;
526 sp
<AaptAssets
> mAssets
;
527 DefaultKeyedVector
<String16
, sp
<Package
> > mPackages
;
528 Vector
<sp
<Package
> > mOrderedPackages
;
529 uint32_t mNextPackageId
;
530 bool mHaveAppPackage
;
533 SourcePos mCurrentXmlPos
;
536 // key = string resource name, value = set of locales in which that name is defined
537 map
<String16
, set
<String8
> > mLocalizations
;
543 ResourceFilter() : mData(), mContainsPseudo(false) {}
544 status_t
parse(const char* arg
);
545 bool match(int axis
, uint32_t value
);
546 bool match(const ResTable_config
& config
);
547 inline bool containsPseudo() { return mContainsPseudo
; }
550 KeyedVector
<int,SortedVector
<uint32_t> > mData
;
551 bool mContainsPseudo
;