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"
16 XML_COMPILE_STRIP_COMMENTS
= 1<<0,
17 XML_COMPILE_ASSIGN_ATTRIBUTE_IDS
= 1<<1,
18 XML_COMPILE_COMPACT_WHITESPACE
= 1<<2,
19 XML_COMPILE_STRIP_WHITESPACE
= 1<<3,
20 XML_COMPILE_STRIP_RAW_VALUES
= 1<<4,
22 XML_COMPILE_STANDARD_RESOURCE
=
23 XML_COMPILE_STRIP_COMMENTS
| XML_COMPILE_ASSIGN_ATTRIBUTE_IDS
24 | XML_COMPILE_STRIP_WHITESPACE
| XML_COMPILE_STRIP_RAW_VALUES
27 status_t
compileXmlFile(const sp
<AaptAssets
>& assets
,
28 const sp
<AaptFile
>& target
,
30 int options
= XML_COMPILE_STANDARD_RESOURCE
);
32 status_t
compileResourceFile(Bundle
* bundle
,
33 const sp
<AaptAssets
>& assets
,
34 const sp
<AaptFile
>& in
,
35 const ResTable_config
& defParams
,
36 ResourceTable
* outTable
);
44 AccessorCookie(const SourcePos
&p
, const String8
& a
, const String8
& v
)
52 class ResourceTable
: public ResTable::Accessor
59 ResourceTable(Bundle
* bundle
, const String16
& assetsPackage
);
61 status_t
addIncludedResources(Bundle
* bundle
, const sp
<AaptAssets
>& assets
);
63 status_t
addPublic(const SourcePos
& pos
,
64 const String16
& package
,
67 const uint32_t ident
);
69 status_t
addEntry(const SourcePos
& pos
,
70 const String16
& package
,
73 const String16
& value
,
74 const Vector
<StringPool::entry_style_span
>* style
= NULL
,
75 const ResTable_config
* params
= NULL
,
76 const bool doSetIndex
= false,
77 const int32_t format
= ResTable_map::TYPE_ANY
);
79 status_t
startBag(const SourcePos
& pos
,
80 const String16
& package
,
83 const String16
& bagParent
,
84 const ResTable_config
* params
= NULL
,
88 status_t
addBag(const SourcePos
& pos
,
89 const String16
& package
,
92 const String16
& bagParent
,
93 const String16
& bagKey
,
94 const String16
& value
,
95 const Vector
<StringPool::entry_style_span
>* style
= NULL
,
96 const ResTable_config
* params
= NULL
,
99 const int32_t format
= ResTable_map::TYPE_ANY
);
101 bool hasBagOrEntry(const String16
& package
,
102 const String16
& type
,
103 const String16
& name
) const;
105 bool hasBagOrEntry(const String16
& ref
,
106 const String16
* defType
= NULL
,
107 const String16
* defPackage
= NULL
);
109 bool appendComment(const String16
& package
,
110 const String16
& type
,
111 const String16
& name
,
112 const String16
& comment
,
113 bool onlyIfEmpty
= false);
115 bool appendTypeComment(const String16
& package
,
116 const String16
& type
,
117 const String16
& name
,
118 const String16
& comment
);
121 size_t numLocalResources() const;
122 bool hasResources() const;
124 sp
<AaptFile
> flatten(Bundle
*);
126 static inline uint32_t makeResId(uint32_t packageId
,
130 return nameId
| (typeId
<<16) | (packageId
<<24);
133 static inline uint32_t getResId(const sp
<Package
>& p
,
137 uint32_t getResId(const String16
& package
,
138 const String16
& type
,
139 const String16
& name
) const;
141 uint32_t getResId(const String16
& ref
,
142 const String16
* defType
= NULL
,
143 const String16
* defPackage
= NULL
,
144 const char** outErrorMsg
= NULL
) const;
146 static bool isValidResourceName(const String16
& s
);
148 bool stringToValue(Res_value
* outValue
, StringPool
* pool
,
150 bool preserveSpaces
, bool coerceType
,
152 const Vector
<StringPool::entry_style_span
>* style
= NULL
,
153 String16
* outStr
= NULL
, void* accessorCookie
= NULL
,
154 uint32_t attrType
= ResTable_map::TYPE_ANY
);
156 status_t
assignResourceIds();
157 status_t
addSymbols(const sp
<AaptSymbols
>& outSymbols
= NULL
);
159 status_t
flatten(Bundle
*, const sp
<AaptFile
>& dest
);
161 void writePublicDefinitions(const String16
& package
, FILE* fp
);
163 virtual uint32_t getCustomResource(const String16
& package
,
164 const String16
& type
,
165 const String16
& name
) const;
166 virtual uint32_t getCustomResourceWithCreation(const String16
& package
,
167 const String16
& type
,
168 const String16
& name
,
169 const bool createIfNeeded
);
170 virtual uint32_t getRemappedPackage(uint32_t origPackage
) const;
171 virtual bool getAttributeType(uint32_t attrID
, uint32_t* outType
);
172 virtual bool getAttributeMin(uint32_t attrID
, uint32_t* outMin
);
173 virtual bool getAttributeMax(uint32_t attrID
, uint32_t* outMax
);
174 virtual bool getAttributeKeys(uint32_t attrID
, Vector
<String16
>* outKeys
);
175 virtual bool getAttributeEnum(uint32_t attrID
,
176 const char16_t* name
, size_t nameLen
,
177 Res_value
* outValue
);
178 virtual bool getAttributeFlags(uint32_t attrID
,
179 const char16_t* name
, size_t nameLen
,
180 Res_value
* outValue
);
181 virtual uint32_t getAttributeL10N(uint32_t attrID
);
183 virtual bool getLocalizationSetting();
184 virtual void reportError(void* accessorCookie
, const char* fmt
, ...);
186 void setCurrentXmlPos(const SourcePos
& pos
) { mCurrentXmlPos
= pos
; }
190 Item() : isId(false), format(ResTable_map::TYPE_ANY
), bagKeyId(0), evaluating(false)
191 { memset(&parsedValue
, 0, sizeof(parsedValue
)); }
192 Item(const SourcePos
& pos
,
194 const String16
& _value
,
195 const Vector
<StringPool::entry_style_span
>* _style
= NULL
,
196 int32_t format
= ResTable_map::TYPE_ANY
);
197 Item(const Item
& o
) : sourcePos(o
.sourcePos
),
198 isId(o
.isId
), value(o
.value
), style(o
.style
),
199 format(o
.format
), bagKeyId(o
.bagKeyId
), evaluating(false) {
200 memset(&parsedValue
, 0, sizeof(parsedValue
));
204 Item
& operator=(const Item
& o
) {
205 sourcePos
= o
.sourcePos
;
210 bagKeyId
= o
.bagKeyId
;
211 parsedValue
= o
.parsedValue
;
218 Vector
<StringPool::entry_style_span
> style
;
221 mutable bool evaluating
;
222 Res_value parsedValue
;
225 class Entry
: public RefBase
{
227 Entry(const String16
& name
, const SourcePos
& pos
)
228 : mName(name
), mType(TYPE_UNKNOWN
),
229 mItemFormat(ResTable_map::TYPE_ANY
), mNameIndex(-1), mPos(pos
)
239 String16
getName() const { return mName
; }
240 type
getType() const { return mType
; }
242 void setParent(const String16
& parent
) { mParent
= parent
; }
243 String16
getParent() const { return mParent
; }
245 status_t
makeItABag(const SourcePos
& sourcePos
);
247 status_t
setItem(const SourcePos
& pos
,
248 const String16
& value
,
249 const Vector
<StringPool::entry_style_span
>* style
= NULL
,
250 int32_t format
= ResTable_map::TYPE_ANY
);
252 status_t
addToBag(const SourcePos
& pos
,
253 const String16
& key
, const String16
& value
,
254 const Vector
<StringPool::entry_style_span
>* style
= NULL
,
255 bool replace
=false, bool isId
= false,
256 int32_t format
= ResTable_map::TYPE_ANY
);
258 // Index of the entry's name string in the key pool.
259 int32_t getNameIndex() const { return mNameIndex
; }
260 void setNameIndex(int32_t index
) { mNameIndex
= index
; }
262 const Item
* getItem() const { return mType
== TYPE_ITEM
? &mItem
: NULL
; }
263 const KeyedVector
<String16
, Item
>& getBag() const { return mBag
; }
265 status_t
generateAttributes(ResourceTable
* table
,
266 const String16
& package
);
268 status_t
assignResourceIds(ResourceTable
* table
,
269 const String16
& package
);
271 status_t
prepareFlatten(StringPool
* strings
, ResourceTable
* table
);
273 ssize_t
flatten(Bundle
*, const sp
<AaptFile
>& data
, bool isPublic
);
275 const SourcePos
& getPos() const { return mPos
; }
283 KeyedVector
<String16
, Item
> mBag
;
289 struct ConfigDescription
: public ResTable_config
{
290 ConfigDescription() {
291 memset(this, 0, sizeof(*this));
292 size
= sizeof(ResTable_config
);
294 ConfigDescription(const ResTable_config
&o
) {
295 *static_cast<ResTable_config
*>(this) = o
;
296 size
= sizeof(ResTable_config
);
298 ConfigDescription(const ConfigDescription
&o
) {
299 *static_cast<ResTable_config
*>(this) = o
;
302 ConfigDescription
& operator=(const ResTable_config
& o
) {
303 *static_cast<ResTable_config
*>(this) = o
;
304 size
= sizeof(ResTable_config
);
307 ConfigDescription
& operator=(const ConfigDescription
& o
) {
308 *static_cast<ResTable_config
*>(this) = o
;
312 inline bool operator<(const ConfigDescription
& o
) const { return compare(o
) < 0; }
313 inline bool operator<=(const ConfigDescription
& o
) const { return compare(o
) <= 0; }
314 inline bool operator==(const ConfigDescription
& o
) const { return compare(o
) == 0; }
315 inline bool operator!=(const ConfigDescription
& o
) const { return compare(o
) != 0; }
316 inline bool operator>=(const ConfigDescription
& o
) const { return compare(o
) >= 0; }
317 inline bool operator>(const ConfigDescription
& o
) const { return compare(o
) > 0; }
320 class ConfigList
: public RefBase
{
322 ConfigList(const String16
& name
, const SourcePos
& pos
)
323 : mName(name
), mPos(pos
), mPublic(false), mEntryIndex(-1) { }
324 virtual ~ConfigList() { }
326 String16
getName() const { return mName
; }
327 const SourcePos
& getPos() const { return mPos
; }
329 void appendComment(const String16
& comment
, bool onlyIfEmpty
= false);
330 const String16
& getComment() const { return mComment
; }
332 void appendTypeComment(const String16
& comment
);
333 const String16
& getTypeComment() const { return mTypeComment
; }
335 // Index of this entry in its Type.
336 int32_t getEntryIndex() const { return mEntryIndex
; }
337 void setEntryIndex(int32_t index
) { mEntryIndex
= index
; }
339 void setPublic(bool pub
) { mPublic
= pub
; }
340 bool getPublic() const { return mPublic
; }
341 void setPublicSourcePos(const SourcePos
& pos
) { mPublicSourcePos
= pos
; }
342 const SourcePos
& getPublicSourcePos() { return mPublicSourcePos
; }
344 void addEntry(const ResTable_config
& config
, const sp
<Entry
>& entry
) {
345 mEntries
.add(config
, entry
);
348 const DefaultKeyedVector
<ConfigDescription
, sp
<Entry
> >& getEntries() const { return mEntries
; }
350 const String16 mName
;
351 const SourcePos mPos
;
353 String16 mTypeComment
;
355 SourcePos mPublicSourcePos
;
357 DefaultKeyedVector
<ConfigDescription
, sp
<Entry
> > mEntries
;
362 Public() : sourcePos(), ident(0) { }
363 Public(const SourcePos
& pos
,
364 const String16
& _comment
,
367 comment(_comment
), ident(_ident
) { }
368 Public(const Public
& o
) : sourcePos(o
.sourcePos
),
369 comment(o
.comment
), ident(o
.ident
) { }
372 Public
& operator=(const Public
& o
) {
373 sourcePos
= o
.sourcePos
;
384 class Type
: public RefBase
{
386 Type(const String16
& name
, const SourcePos
& pos
)
387 : mName(name
), mFirstPublicSourcePos(NULL
), mPublicIndex(-1), mIndex(-1), mPos(pos
)
389 virtual ~Type() { delete mFirstPublicSourcePos
; }
391 status_t
addPublic(const SourcePos
& pos
,
392 const String16
& name
,
393 const uint32_t ident
);
395 String16
getName() const { return mName
; }
396 sp
<Entry
> getEntry(const String16
& entry
,
397 const SourcePos
& pos
,
398 const ResTable_config
* config
= NULL
,
399 bool doSetIndex
= false);
401 const SourcePos
& getFirstPublicSourcePos() const { return *mFirstPublicSourcePos
; }
403 int32_t getPublicIndex() const { return mPublicIndex
; }
405 int32_t getIndex() const { return mIndex
; }
406 void setIndex(int32_t index
) { mIndex
= index
; }
408 status_t
applyPublicEntryOrder();
410 const SortedVector
<ConfigDescription
>& getUniqueConfigs() const { return mUniqueConfigs
; }
412 const DefaultKeyedVector
<String16
, sp
<ConfigList
> >& getConfigs() const { return mConfigs
; }
413 const Vector
<sp
<ConfigList
> >& getOrderedConfigs() const { return mOrderedConfigs
; }
415 const SourcePos
& getPos() const { return mPos
; }
418 SourcePos
* mFirstPublicSourcePos
;
419 DefaultKeyedVector
<String16
, Public
> mPublic
;
420 SortedVector
<ConfigDescription
> mUniqueConfigs
;
421 DefaultKeyedVector
<String16
, sp
<ConfigList
> > mConfigs
;
422 Vector
<sp
<ConfigList
> > mOrderedConfigs
;
423 int32_t mPublicIndex
;
428 class Package
: public RefBase
{
430 Package(const String16
& name
, ssize_t includedId
=-1);
431 virtual ~Package() { }
433 String16
getName() const { return mName
; }
434 sp
<Type
> getType(const String16
& type
,
435 const SourcePos
& pos
,
436 bool doSetIndex
= false);
438 ssize_t
getAssignedId() const { return mIncludedId
; }
440 const ResStringPool
& getTypeStrings() const { return mTypeStrings
; }
441 uint32_t indexOfTypeString(const String16
& s
) const { return mTypeStringsMapping
.valueFor(s
); }
442 const sp
<AaptFile
> getTypeStringsData() const { return mTypeStringsData
; }
443 status_t
setTypeStrings(const sp
<AaptFile
>& data
);
445 const ResStringPool
& getKeyStrings() const { return mKeyStrings
; }
446 uint32_t indexOfKeyString(const String16
& s
) const { return mKeyStringsMapping
.valueFor(s
); }
447 const sp
<AaptFile
> getKeyStringsData() const { return mKeyStringsData
; }
448 status_t
setKeyStrings(const sp
<AaptFile
>& data
);
450 status_t
applyPublicTypeOrder();
452 const DefaultKeyedVector
<String16
, sp
<Type
> >& getTypes() const { return mTypes
; }
453 const Vector
<sp
<Type
> >& getOrderedTypes() const { return mOrderedTypes
; }
456 status_t
setStrings(const sp
<AaptFile
>& data
,
457 ResStringPool
* strings
,
458 DefaultKeyedVector
<String16
, uint32_t>* mappings
);
460 const String16 mName
;
461 const ssize_t mIncludedId
;
462 DefaultKeyedVector
<String16
, sp
<Type
> > mTypes
;
463 Vector
<sp
<Type
> > mOrderedTypes
;
464 sp
<AaptFile
> mTypeStringsData
;
465 sp
<AaptFile
> mKeyStringsData
;
466 ResStringPool mTypeStrings
;
467 ResStringPool mKeyStrings
;
468 DefaultKeyedVector
<String16
, uint32_t> mTypeStringsMapping
;
469 DefaultKeyedVector
<String16
, uint32_t> mKeyStringsMapping
;
473 void writePublicDefinitions(const String16
& package
, FILE* fp
, bool pub
);
474 sp
<Package
> getPackage(const String16
& package
);
475 sp
<Type
> getType(const String16
& package
,
476 const String16
& type
,
477 const SourcePos
& pos
,
478 bool doSetIndex
= false);
479 sp
<Entry
> getEntry(const String16
& package
,
480 const String16
& type
,
481 const String16
& name
,
482 const SourcePos
& pos
,
483 const ResTable_config
* config
= NULL
,
484 bool doSetIndex
= false);
485 sp
<const Entry
> getEntry(uint32_t resID
,
486 const ResTable_config
* config
= NULL
) const;
487 const Item
* getItem(uint32_t resID
, uint32_t attrID
) const;
488 bool getItemValue(uint32_t resID
, uint32_t attrID
,
489 Res_value
* outValue
);
492 String16 mAssetsPackage
;
493 sp
<AaptAssets
> mAssets
;
494 DefaultKeyedVector
<String16
, DefaultKeyedVector
<String16
, uint32_t> > mPublicNames
;
495 DefaultKeyedVector
<String16
, sp
<Package
> > mPackages
;
496 Vector
<sp
<Package
> > mOrderedPackages
;
497 uint32_t mNextPackageId
;
498 bool mHaveAppPackage
;
501 SourcePos mCurrentXmlPos
;
508 ResourceFilter() : mData(), mContainsPseudo(false) {}
509 status_t
parse(const char* arg
);
510 bool match(int axis
, uint32_t value
);
511 bool match(const ResTable_config
& config
);
512 inline bool containsPseudo() { return mContainsPseudo
; }
515 KeyedVector
<int,SortedVector
<uint32_t> > mData
;
516 bool mContainsPseudo
;