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,
27 XML_COMPILE_UTF8
= 1<<5,
29 XML_COMPILE_STANDARD_RESOURCE
=
30 XML_COMPILE_STRIP_COMMENTS
| XML_COMPILE_ASSIGN_ATTRIBUTE_IDS
31 | XML_COMPILE_STRIP_WHITESPACE
| XML_COMPILE_STRIP_RAW_VALUES
34 status_t
compileXmlFile(const sp
<AaptAssets
>& assets
,
35 const sp
<AaptFile
>& target
,
37 int options
= XML_COMPILE_STANDARD_RESOURCE
);
39 status_t
compileXmlFile(const sp
<AaptAssets
>& assets
,
40 const sp
<AaptFile
>& target
,
41 const sp
<AaptFile
>& outTarget
,
43 int options
= XML_COMPILE_STANDARD_RESOURCE
);
45 status_t
compileXmlFile(const sp
<AaptAssets
>& assets
,
46 const sp
<XMLNode
>& xmlTree
,
47 const sp
<AaptFile
>& target
,
49 int options
= XML_COMPILE_STANDARD_RESOURCE
);
51 status_t
compileResourceFile(Bundle
* bundle
,
52 const sp
<AaptAssets
>& assets
,
53 const sp
<AaptFile
>& in
,
54 const ResTable_config
& defParams
,
56 ResourceTable
* outTable
);
64 AccessorCookie(const SourcePos
&p
, const String8
& a
, const String8
& v
)
72 class ResourceTable
: public ResTable::Accessor
79 ResourceTable(Bundle
* bundle
, const String16
& assetsPackage
);
81 status_t
addIncludedResources(Bundle
* bundle
, const sp
<AaptAssets
>& assets
);
83 status_t
addPublic(const SourcePos
& pos
,
84 const String16
& package
,
87 const uint32_t ident
);
89 status_t
addEntry(const SourcePos
& pos
,
90 const String16
& package
,
93 const String16
& value
,
94 const Vector
<StringPool::entry_style_span
>* style
= NULL
,
95 const ResTable_config
* params
= NULL
,
96 const bool doSetIndex
= false,
97 const int32_t format
= ResTable_map::TYPE_ANY
,
98 const bool overwrite
= false);
100 status_t
startBag(const SourcePos
& pos
,
101 const String16
& package
,
102 const String16
& type
,
103 const String16
& name
,
104 const String16
& bagParent
,
105 const ResTable_config
* params
= NULL
,
106 bool overlay
= false,
107 bool replace
= false,
110 status_t
addBag(const SourcePos
& pos
,
111 const String16
& package
,
112 const String16
& type
,
113 const String16
& name
,
114 const String16
& bagParent
,
115 const String16
& bagKey
,
116 const String16
& value
,
117 const Vector
<StringPool::entry_style_span
>* style
= NULL
,
118 const ResTable_config
* params
= NULL
,
119 bool replace
= false,
121 const int32_t format
= ResTable_map::TYPE_ANY
);
123 bool hasBagOrEntry(const String16
& package
,
124 const String16
& type
,
125 const String16
& name
) const;
127 bool hasBagOrEntry(const String16
& ref
,
128 const String16
* defType
= NULL
,
129 const String16
* defPackage
= NULL
);
131 bool appendComment(const String16
& package
,
132 const String16
& type
,
133 const String16
& name
,
134 const String16
& comment
,
135 bool onlyIfEmpty
= false);
137 bool appendTypeComment(const String16
& package
,
138 const String16
& type
,
139 const String16
& name
,
140 const String16
& comment
);
142 void canAddEntry(const SourcePos
& pos
,
143 const String16
& package
, const String16
& type
, const String16
& name
);
146 size_t numLocalResources() const;
147 bool hasResources() const;
149 sp
<AaptFile
> flatten(Bundle
*);
151 static inline uint32_t makeResId(uint32_t packageId
,
155 return nameId
| (typeId
<<16) | (packageId
<<24);
158 static inline uint32_t getResId(const sp
<Package
>& p
,
162 uint32_t getResId(const String16
& package
,
163 const String16
& type
,
164 const String16
& name
,
165 bool onlyPublic
= false) const;
167 uint32_t getResId(const String16
& ref
,
168 const String16
* defType
= NULL
,
169 const String16
* defPackage
= NULL
,
170 const char** outErrorMsg
= NULL
,
171 bool onlyPublic
= false) const;
173 static bool isValidResourceName(const String16
& s
);
175 bool stringToValue(Res_value
* outValue
, StringPool
* pool
,
177 bool preserveSpaces
, bool coerceType
,
179 const Vector
<StringPool::entry_style_span
>* style
= NULL
,
180 String16
* outStr
= NULL
, void* accessorCookie
= NULL
,
181 uint32_t attrType
= ResTable_map::TYPE_ANY
);
183 status_t
assignResourceIds();
184 status_t
addSymbols(const sp
<AaptSymbols
>& outSymbols
= NULL
);
185 void addLocalization(const String16
& name
, const String8
& locale
);
186 status_t
validateLocalizations(void);
188 status_t
flatten(Bundle
*, const sp
<AaptFile
>& dest
);
190 void writePublicDefinitions(const String16
& package
, FILE* fp
);
192 virtual uint32_t getCustomResource(const String16
& package
,
193 const String16
& type
,
194 const String16
& name
) const;
195 virtual uint32_t getCustomResourceWithCreation(const String16
& package
,
196 const String16
& type
,
197 const String16
& name
,
198 const bool createIfNeeded
);
199 virtual uint32_t getRemappedPackage(uint32_t origPackage
) const;
200 virtual bool getAttributeType(uint32_t attrID
, uint32_t* outType
);
201 virtual bool getAttributeMin(uint32_t attrID
, uint32_t* outMin
);
202 virtual bool getAttributeMax(uint32_t attrID
, uint32_t* outMax
);
203 virtual bool getAttributeKeys(uint32_t attrID
, Vector
<String16
>* outKeys
);
204 virtual bool getAttributeEnum(uint32_t attrID
,
205 const char16_t* name
, size_t nameLen
,
206 Res_value
* outValue
);
207 virtual bool getAttributeFlags(uint32_t attrID
,
208 const char16_t* name
, size_t nameLen
,
209 Res_value
* outValue
);
210 virtual uint32_t getAttributeL10N(uint32_t attrID
);
212 virtual bool getLocalizationSetting();
213 virtual void reportError(void* accessorCookie
, const char* fmt
, ...);
215 void setCurrentXmlPos(const SourcePos
& pos
) { mCurrentXmlPos
= pos
; }
219 Item() : isId(false), format(ResTable_map::TYPE_ANY
), bagKeyId(0), evaluating(false)
220 { memset(&parsedValue
, 0, sizeof(parsedValue
)); }
221 Item(const SourcePos
& pos
,
223 const String16
& _value
,
224 const Vector
<StringPool::entry_style_span
>* _style
= NULL
,
225 int32_t format
= ResTable_map::TYPE_ANY
);
226 Item(const Item
& o
) : sourcePos(o
.sourcePos
),
227 isId(o
.isId
), value(o
.value
), style(o
.style
),
228 format(o
.format
), bagKeyId(o
.bagKeyId
), evaluating(false) {
229 memset(&parsedValue
, 0, sizeof(parsedValue
));
233 Item
& operator=(const Item
& o
) {
234 sourcePos
= o
.sourcePos
;
239 bagKeyId
= o
.bagKeyId
;
240 parsedValue
= o
.parsedValue
;
247 Vector
<StringPool::entry_style_span
> style
;
250 mutable bool evaluating
;
251 Res_value parsedValue
;
254 class Entry
: public RefBase
{
256 Entry(const String16
& name
, const SourcePos
& pos
)
257 : mName(name
), mType(TYPE_UNKNOWN
),
258 mItemFormat(ResTable_map::TYPE_ANY
), mNameIndex(-1), mPos(pos
)
268 String16
getName() const { return mName
; }
269 type
getType() const { return mType
; }
271 void setParent(const String16
& parent
) { mParent
= parent
; }
272 String16
getParent() const { return mParent
; }
274 status_t
makeItABag(const SourcePos
& sourcePos
);
276 status_t
emptyBag(const SourcePos
& sourcePos
);
278 status_t
setItem(const SourcePos
& pos
,
279 const String16
& value
,
280 const Vector
<StringPool::entry_style_span
>* style
= NULL
,
281 int32_t format
= ResTable_map::TYPE_ANY
,
282 const bool overwrite
= false);
284 status_t
addToBag(const SourcePos
& pos
,
285 const String16
& key
, const String16
& value
,
286 const Vector
<StringPool::entry_style_span
>* style
= NULL
,
287 bool replace
=false, bool isId
= false,
288 int32_t format
= ResTable_map::TYPE_ANY
);
290 // Index of the entry's name string in the key pool.
291 int32_t getNameIndex() const { return mNameIndex
; }
292 void setNameIndex(int32_t index
) { mNameIndex
= index
; }
294 const Item
* getItem() const { return mType
== TYPE_ITEM
? &mItem
: NULL
; }
295 const KeyedVector
<String16
, Item
>& getBag() const { return mBag
; }
297 status_t
generateAttributes(ResourceTable
* table
,
298 const String16
& package
);
300 status_t
assignResourceIds(ResourceTable
* table
,
301 const String16
& package
);
303 status_t
prepareFlatten(StringPool
* strings
, ResourceTable
* table
);
305 ssize_t
flatten(Bundle
*, const sp
<AaptFile
>& data
, bool isPublic
);
307 const SourcePos
& getPos() const { return mPos
; }
315 KeyedVector
<String16
, Item
> mBag
;
321 struct ConfigDescription
: public ResTable_config
{
322 ConfigDescription() {
323 memset(this, 0, sizeof(*this));
324 size
= sizeof(ResTable_config
);
326 ConfigDescription(const ResTable_config
&o
) {
327 *static_cast<ResTable_config
*>(this) = o
;
328 size
= sizeof(ResTable_config
);
330 ConfigDescription(const ConfigDescription
&o
) {
331 *static_cast<ResTable_config
*>(this) = o
;
334 ConfigDescription
& operator=(const ResTable_config
& o
) {
335 *static_cast<ResTable_config
*>(this) = o
;
336 size
= sizeof(ResTable_config
);
339 ConfigDescription
& operator=(const ConfigDescription
& o
) {
340 *static_cast<ResTable_config
*>(this) = o
;
344 inline bool operator<(const ConfigDescription
& o
) const { return compare(o
) < 0; }
345 inline bool operator<=(const ConfigDescription
& o
) const { return compare(o
) <= 0; }
346 inline bool operator==(const ConfigDescription
& o
) const { return compare(o
) == 0; }
347 inline bool operator!=(const ConfigDescription
& o
) const { return compare(o
) != 0; }
348 inline bool operator>=(const ConfigDescription
& o
) const { return compare(o
) >= 0; }
349 inline bool operator>(const ConfigDescription
& o
) const { return compare(o
) > 0; }
352 class ConfigList
: public RefBase
{
354 ConfigList(const String16
& name
, const SourcePos
& pos
)
355 : mName(name
), mPos(pos
), mPublic(false), mEntryIndex(-1) { }
356 virtual ~ConfigList() { }
358 String16
getName() const { return mName
; }
359 const SourcePos
& getPos() const { return mPos
; }
361 void appendComment(const String16
& comment
, bool onlyIfEmpty
= false);
362 const String16
& getComment() const { return mComment
; }
364 void appendTypeComment(const String16
& comment
);
365 const String16
& getTypeComment() const { return mTypeComment
; }
367 // Index of this entry in its Type.
368 int32_t getEntryIndex() const { return mEntryIndex
; }
369 void setEntryIndex(int32_t index
) { mEntryIndex
= index
; }
371 void setPublic(bool pub
) { mPublic
= pub
; }
372 bool getPublic() const { return mPublic
; }
373 void setPublicSourcePos(const SourcePos
& pos
) { mPublicSourcePos
= pos
; }
374 const SourcePos
& getPublicSourcePos() { return mPublicSourcePos
; }
376 void addEntry(const ResTable_config
& config
, const sp
<Entry
>& entry
) {
377 mEntries
.add(config
, entry
);
380 const DefaultKeyedVector
<ConfigDescription
, sp
<Entry
> >& getEntries() const { return mEntries
; }
382 const String16 mName
;
383 const SourcePos mPos
;
385 String16 mTypeComment
;
387 SourcePos mPublicSourcePos
;
389 DefaultKeyedVector
<ConfigDescription
, sp
<Entry
> > mEntries
;
394 Public() : sourcePos(), ident(0) { }
395 Public(const SourcePos
& pos
,
396 const String16
& _comment
,
399 comment(_comment
), ident(_ident
) { }
400 Public(const Public
& o
) : sourcePos(o
.sourcePos
),
401 comment(o
.comment
), ident(o
.ident
) { }
404 Public
& operator=(const Public
& o
) {
405 sourcePos
= o
.sourcePos
;
416 class Type
: public RefBase
{
418 Type(const String16
& name
, const SourcePos
& pos
)
419 : mName(name
), mFirstPublicSourcePos(NULL
), mPublicIndex(-1), mIndex(-1), mPos(pos
)
421 virtual ~Type() { delete mFirstPublicSourcePos
; }
423 status_t
addPublic(const SourcePos
& pos
,
424 const String16
& name
,
425 const uint32_t ident
);
427 void canAddEntry(const String16
& name
);
429 String16
getName() const { return mName
; }
430 sp
<Entry
> getEntry(const String16
& entry
,
431 const SourcePos
& pos
,
432 const ResTable_config
* config
= NULL
,
433 bool doSetIndex
= false,
434 bool overlay
= false,
435 bool autoAddOverlay
= false);
437 const SourcePos
& getFirstPublicSourcePos() const { return *mFirstPublicSourcePos
; }
439 int32_t getPublicIndex() const { return mPublicIndex
; }
441 int32_t getIndex() const { return mIndex
; }
442 void setIndex(int32_t index
) { mIndex
= index
; }
444 status_t
applyPublicEntryOrder();
446 const SortedVector
<ConfigDescription
>& getUniqueConfigs() const { return mUniqueConfigs
; }
448 const DefaultKeyedVector
<String16
, sp
<ConfigList
> >& getConfigs() const { return mConfigs
; }
449 const Vector
<sp
<ConfigList
> >& getOrderedConfigs() const { return mOrderedConfigs
; }
451 const SortedVector
<String16
>& getCanAddEntries() const { return mCanAddEntries
; }
453 const SourcePos
& getPos() const { return mPos
; }
456 SourcePos
* mFirstPublicSourcePos
;
457 DefaultKeyedVector
<String16
, Public
> mPublic
;
458 SortedVector
<ConfigDescription
> mUniqueConfigs
;
459 DefaultKeyedVector
<String16
, sp
<ConfigList
> > mConfigs
;
460 Vector
<sp
<ConfigList
> > mOrderedConfigs
;
461 SortedVector
<String16
> mCanAddEntries
;
462 int32_t mPublicIndex
;
467 class Package
: public RefBase
{
469 Package(const String16
& name
, ssize_t includedId
=-1);
470 virtual ~Package() { }
472 String16
getName() const { return mName
; }
473 sp
<Type
> getType(const String16
& type
,
474 const SourcePos
& pos
,
475 bool doSetIndex
= false);
477 ssize_t
getAssignedId() const { return mIncludedId
; }
479 const ResStringPool
& getTypeStrings() const { return mTypeStrings
; }
480 uint32_t indexOfTypeString(const String16
& s
) const { return mTypeStringsMapping
.valueFor(s
); }
481 const sp
<AaptFile
> getTypeStringsData() const { return mTypeStringsData
; }
482 status_t
setTypeStrings(const sp
<AaptFile
>& data
);
484 const ResStringPool
& getKeyStrings() const { return mKeyStrings
; }
485 uint32_t indexOfKeyString(const String16
& s
) const { return mKeyStringsMapping
.valueFor(s
); }
486 const sp
<AaptFile
> getKeyStringsData() const { return mKeyStringsData
; }
487 status_t
setKeyStrings(const sp
<AaptFile
>& data
);
489 status_t
applyPublicTypeOrder();
491 const DefaultKeyedVector
<String16
, sp
<Type
> >& getTypes() const { return mTypes
; }
492 const Vector
<sp
<Type
> >& getOrderedTypes() const { return mOrderedTypes
; }
495 status_t
setStrings(const sp
<AaptFile
>& data
,
496 ResStringPool
* strings
,
497 DefaultKeyedVector
<String16
, uint32_t>* mappings
);
499 const String16 mName
;
500 const ssize_t mIncludedId
;
501 DefaultKeyedVector
<String16
, sp
<Type
> > mTypes
;
502 Vector
<sp
<Type
> > mOrderedTypes
;
503 sp
<AaptFile
> mTypeStringsData
;
504 sp
<AaptFile
> mKeyStringsData
;
505 ResStringPool mTypeStrings
;
506 ResStringPool mKeyStrings
;
507 DefaultKeyedVector
<String16
, uint32_t> mTypeStringsMapping
;
508 DefaultKeyedVector
<String16
, uint32_t> mKeyStringsMapping
;
512 void writePublicDefinitions(const String16
& package
, FILE* fp
, bool pub
);
513 sp
<Package
> getPackage(const String16
& package
);
514 sp
<Type
> getType(const String16
& package
,
515 const String16
& type
,
516 const SourcePos
& pos
,
517 bool doSetIndex
= false);
518 sp
<Entry
> getEntry(const String16
& package
,
519 const String16
& type
,
520 const String16
& name
,
521 const SourcePos
& pos
,
523 const ResTable_config
* config
= NULL
,
524 bool doSetIndex
= false);
525 sp
<const Entry
> getEntry(uint32_t resID
,
526 const ResTable_config
* config
= NULL
) const;
527 const Item
* getItem(uint32_t resID
, uint32_t attrID
) const;
528 bool getItemValue(uint32_t resID
, uint32_t attrID
,
529 Res_value
* outValue
);
532 String16 mAssetsPackage
;
533 sp
<AaptAssets
> mAssets
;
534 DefaultKeyedVector
<String16
, sp
<Package
> > mPackages
;
535 Vector
<sp
<Package
> > mOrderedPackages
;
536 uint32_t mNextPackageId
;
537 bool mHaveAppPackage
;
540 SourcePos mCurrentXmlPos
;
543 // key = string resource name, value = set of locales in which that name is defined
544 map
<String16
, set
<String8
> > mLocalizations
;
550 ResourceFilter() : mData(), mContainsPseudo(false) {}
551 status_t
parse(const char* arg
);
552 bool match(int axis
, uint32_t value
);
553 bool match(const ResTable_config
& config
);
554 inline bool containsPseudo() { return mContainsPseudo
; }
557 KeyedVector
<int,SortedVector
<uint32_t> > mData
;
558 bool mContainsPseudo
;