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
emptyBag(const SourcePos
& sourcePos
);
260 status_t
setItem(const SourcePos
& pos
,
261 const String16
& value
,
262 const Vector
<StringPool::entry_style_span
>* style
= NULL
,
263 int32_t format
= ResTable_map::TYPE_ANY
,
264 const bool overwrite
= false);
266 status_t
addToBag(const SourcePos
& pos
,
267 const String16
& key
, const String16
& value
,
268 const Vector
<StringPool::entry_style_span
>* style
= NULL
,
269 bool replace
=false, bool isId
= false,
270 int32_t format
= ResTable_map::TYPE_ANY
);
272 // Index of the entry's name string in the key pool.
273 int32_t getNameIndex() const { return mNameIndex
; }
274 void setNameIndex(int32_t index
) { mNameIndex
= index
; }
276 const Item
* getItem() const { return mType
== TYPE_ITEM
? &mItem
: NULL
; }
277 const KeyedVector
<String16
, Item
>& getBag() const { return mBag
; }
279 status_t
generateAttributes(ResourceTable
* table
,
280 const String16
& package
);
282 status_t
assignResourceIds(ResourceTable
* table
,
283 const String16
& package
);
285 status_t
prepareFlatten(StringPool
* strings
, ResourceTable
* table
);
287 ssize_t
flatten(Bundle
*, const sp
<AaptFile
>& data
, bool isPublic
);
289 const SourcePos
& getPos() const { return mPos
; }
297 KeyedVector
<String16
, Item
> mBag
;
303 struct ConfigDescription
: public ResTable_config
{
304 ConfigDescription() {
305 memset(this, 0, sizeof(*this));
306 size
= sizeof(ResTable_config
);
308 ConfigDescription(const ResTable_config
&o
) {
309 *static_cast<ResTable_config
*>(this) = o
;
310 size
= sizeof(ResTable_config
);
312 ConfigDescription(const ConfigDescription
&o
) {
313 *static_cast<ResTable_config
*>(this) = o
;
316 ConfigDescription
& operator=(const ResTable_config
& o
) {
317 *static_cast<ResTable_config
*>(this) = o
;
318 size
= sizeof(ResTable_config
);
321 ConfigDescription
& operator=(const ConfigDescription
& o
) {
322 *static_cast<ResTable_config
*>(this) = o
;
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; }
330 inline bool operator>=(const ConfigDescription
& o
) const { return compare(o
) >= 0; }
331 inline bool operator>(const ConfigDescription
& o
) const { return compare(o
) > 0; }
334 class ConfigList
: public RefBase
{
336 ConfigList(const String16
& name
, const SourcePos
& pos
)
337 : mName(name
), mPos(pos
), mPublic(false), mEntryIndex(-1) { }
338 virtual ~ConfigList() { }
340 String16
getName() const { return mName
; }
341 const SourcePos
& getPos() const { return mPos
; }
343 void appendComment(const String16
& comment
, bool onlyIfEmpty
= false);
344 const String16
& getComment() const { return mComment
; }
346 void appendTypeComment(const String16
& comment
);
347 const String16
& getTypeComment() const { return mTypeComment
; }
349 // Index of this entry in its Type.
350 int32_t getEntryIndex() const { return mEntryIndex
; }
351 void setEntryIndex(int32_t index
) { mEntryIndex
= index
; }
353 void setPublic(bool pub
) { mPublic
= pub
; }
354 bool getPublic() const { return mPublic
; }
355 void setPublicSourcePos(const SourcePos
& pos
) { mPublicSourcePos
= pos
; }
356 const SourcePos
& getPublicSourcePos() { return mPublicSourcePos
; }
358 void addEntry(const ResTable_config
& config
, const sp
<Entry
>& entry
) {
359 mEntries
.add(config
, entry
);
362 const DefaultKeyedVector
<ConfigDescription
, sp
<Entry
> >& getEntries() const { return mEntries
; }
364 const String16 mName
;
365 const SourcePos mPos
;
367 String16 mTypeComment
;
369 SourcePos mPublicSourcePos
;
371 DefaultKeyedVector
<ConfigDescription
, sp
<Entry
> > mEntries
;
376 Public() : sourcePos(), ident(0) { }
377 Public(const SourcePos
& pos
,
378 const String16
& _comment
,
381 comment(_comment
), ident(_ident
) { }
382 Public(const Public
& o
) : sourcePos(o
.sourcePos
),
383 comment(o
.comment
), ident(o
.ident
) { }
386 Public
& operator=(const Public
& o
) {
387 sourcePos
= o
.sourcePos
;
398 class Type
: public RefBase
{
400 Type(const String16
& name
, const SourcePos
& pos
)
401 : mName(name
), mFirstPublicSourcePos(NULL
), mPublicIndex(-1), mIndex(-1), mPos(pos
)
403 virtual ~Type() { delete mFirstPublicSourcePos
; }
405 status_t
addPublic(const SourcePos
& pos
,
406 const String16
& name
,
407 const uint32_t ident
);
409 String16
getName() const { return mName
; }
410 sp
<Entry
> getEntry(const String16
& entry
,
411 const SourcePos
& pos
,
412 const ResTable_config
* config
= NULL
,
413 bool doSetIndex
= false);
415 const SourcePos
& getFirstPublicSourcePos() const { return *mFirstPublicSourcePos
; }
417 int32_t getPublicIndex() const { return mPublicIndex
; }
419 int32_t getIndex() const { return mIndex
; }
420 void setIndex(int32_t index
) { mIndex
= index
; }
422 status_t
applyPublicEntryOrder();
424 const SortedVector
<ConfigDescription
>& getUniqueConfigs() const { return mUniqueConfigs
; }
426 const DefaultKeyedVector
<String16
, sp
<ConfigList
> >& getConfigs() const { return mConfigs
; }
427 const Vector
<sp
<ConfigList
> >& getOrderedConfigs() const { return mOrderedConfigs
; }
429 const SourcePos
& getPos() const { return mPos
; }
432 SourcePos
* mFirstPublicSourcePos
;
433 DefaultKeyedVector
<String16
, Public
> mPublic
;
434 SortedVector
<ConfigDescription
> mUniqueConfigs
;
435 DefaultKeyedVector
<String16
, sp
<ConfigList
> > mConfigs
;
436 Vector
<sp
<ConfigList
> > mOrderedConfigs
;
437 int32_t mPublicIndex
;
442 class Package
: public RefBase
{
444 Package(const String16
& name
, ssize_t includedId
=-1);
445 virtual ~Package() { }
447 String16
getName() const { return mName
; }
448 sp
<Type
> getType(const String16
& type
,
449 const SourcePos
& pos
,
450 bool doSetIndex
= false);
452 ssize_t
getAssignedId() const { return mIncludedId
; }
454 const ResStringPool
& getTypeStrings() const { return mTypeStrings
; }
455 uint32_t indexOfTypeString(const String16
& s
) const { return mTypeStringsMapping
.valueFor(s
); }
456 const sp
<AaptFile
> getTypeStringsData() const { return mTypeStringsData
; }
457 status_t
setTypeStrings(const sp
<AaptFile
>& data
);
459 const ResStringPool
& getKeyStrings() const { return mKeyStrings
; }
460 uint32_t indexOfKeyString(const String16
& s
) const { return mKeyStringsMapping
.valueFor(s
); }
461 const sp
<AaptFile
> getKeyStringsData() const { return mKeyStringsData
; }
462 status_t
setKeyStrings(const sp
<AaptFile
>& data
);
464 status_t
applyPublicTypeOrder();
466 const DefaultKeyedVector
<String16
, sp
<Type
> >& getTypes() const { return mTypes
; }
467 const Vector
<sp
<Type
> >& getOrderedTypes() const { return mOrderedTypes
; }
470 status_t
setStrings(const sp
<AaptFile
>& data
,
471 ResStringPool
* strings
,
472 DefaultKeyedVector
<String16
, uint32_t>* mappings
);
474 const String16 mName
;
475 const ssize_t mIncludedId
;
476 DefaultKeyedVector
<String16
, sp
<Type
> > mTypes
;
477 Vector
<sp
<Type
> > mOrderedTypes
;
478 sp
<AaptFile
> mTypeStringsData
;
479 sp
<AaptFile
> mKeyStringsData
;
480 ResStringPool mTypeStrings
;
481 ResStringPool mKeyStrings
;
482 DefaultKeyedVector
<String16
, uint32_t> mTypeStringsMapping
;
483 DefaultKeyedVector
<String16
, uint32_t> mKeyStringsMapping
;
487 void writePublicDefinitions(const String16
& package
, FILE* fp
, bool pub
);
488 sp
<Package
> getPackage(const String16
& package
);
489 sp
<Type
> getType(const String16
& package
,
490 const String16
& type
,
491 const SourcePos
& pos
,
492 bool doSetIndex
= false);
493 sp
<Entry
> getEntry(const String16
& package
,
494 const String16
& type
,
495 const String16
& name
,
496 const SourcePos
& pos
,
497 const ResTable_config
* config
= NULL
,
498 bool doSetIndex
= false);
499 sp
<const Entry
> getEntry(uint32_t resID
,
500 const ResTable_config
* config
= NULL
) const;
501 const Item
* getItem(uint32_t resID
, uint32_t attrID
) const;
502 bool getItemValue(uint32_t resID
, uint32_t attrID
,
503 Res_value
* outValue
);
506 String16 mAssetsPackage
;
507 sp
<AaptAssets
> mAssets
;
508 DefaultKeyedVector
<String16
, sp
<Package
> > mPackages
;
509 Vector
<sp
<Package
> > mOrderedPackages
;
510 uint32_t mNextPackageId
;
511 bool mHaveAppPackage
;
514 SourcePos mCurrentXmlPos
;
517 // key = string resource name, value = set of locales in which that name is defined
518 map
<String16
, set
<String8
> > mLocalizations
;
524 ResourceFilter() : mData(), mContainsPseudo(false) {}
525 status_t
parse(const char* arg
);
526 bool match(int axis
, uint32_t value
);
527 bool match(const ResTable_config
& config
);
528 inline bool containsPseudo() { return mContainsPseudo
; }
531 KeyedVector
<int,SortedVector
<uint32_t> > mData
;
532 bool mContainsPseudo
;