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
);
136 size_t numLocalResources() const;
137 bool hasResources() const;
139 sp
<AaptFile
> flatten(Bundle
*);
141 static inline uint32_t makeResId(uint32_t packageId
,
145 return nameId
| (typeId
<<16) | (packageId
<<24);
148 static inline uint32_t getResId(const sp
<Package
>& p
,
152 uint32_t getResId(const String16
& package
,
153 const String16
& type
,
154 const String16
& name
,
155 bool onlyPublic
= false) const;
157 uint32_t getResId(const String16
& ref
,
158 const String16
* defType
= NULL
,
159 const String16
* defPackage
= NULL
,
160 const char** outErrorMsg
= NULL
,
161 bool onlyPublic
= false) const;
163 static bool isValidResourceName(const String16
& s
);
165 bool stringToValue(Res_value
* outValue
, StringPool
* pool
,
167 bool preserveSpaces
, bool coerceType
,
169 const Vector
<StringPool::entry_style_span
>* style
= NULL
,
170 String16
* outStr
= NULL
, void* accessorCookie
= NULL
,
171 uint32_t attrType
= ResTable_map::TYPE_ANY
);
173 status_t
assignResourceIds();
174 status_t
addSymbols(const sp
<AaptSymbols
>& outSymbols
= NULL
);
175 void addLocalization(const String16
& name
, const String8
& locale
);
176 status_t
validateLocalizations(void);
178 status_t
flatten(Bundle
*, const sp
<AaptFile
>& dest
);
180 void writePublicDefinitions(const String16
& package
, FILE* fp
);
182 virtual uint32_t getCustomResource(const String16
& package
,
183 const String16
& type
,
184 const String16
& name
) const;
185 virtual uint32_t getCustomResourceWithCreation(const String16
& package
,
186 const String16
& type
,
187 const String16
& name
,
188 const bool createIfNeeded
);
189 virtual uint32_t getRemappedPackage(uint32_t origPackage
) const;
190 virtual bool getAttributeType(uint32_t attrID
, uint32_t* outType
);
191 virtual bool getAttributeMin(uint32_t attrID
, uint32_t* outMin
);
192 virtual bool getAttributeMax(uint32_t attrID
, uint32_t* outMax
);
193 virtual bool getAttributeKeys(uint32_t attrID
, Vector
<String16
>* outKeys
);
194 virtual bool getAttributeEnum(uint32_t attrID
,
195 const char16_t* name
, size_t nameLen
,
196 Res_value
* outValue
);
197 virtual bool getAttributeFlags(uint32_t attrID
,
198 const char16_t* name
, size_t nameLen
,
199 Res_value
* outValue
);
200 virtual uint32_t getAttributeL10N(uint32_t attrID
);
202 virtual bool getLocalizationSetting();
203 virtual void reportError(void* accessorCookie
, const char* fmt
, ...);
205 void setCurrentXmlPos(const SourcePos
& pos
) { mCurrentXmlPos
= pos
; }
209 Item() : isId(false), format(ResTable_map::TYPE_ANY
), bagKeyId(0), evaluating(false)
210 { memset(&parsedValue
, 0, sizeof(parsedValue
)); }
211 Item(const SourcePos
& pos
,
213 const String16
& _value
,
214 const Vector
<StringPool::entry_style_span
>* _style
= NULL
,
215 int32_t format
= ResTable_map::TYPE_ANY
);
216 Item(const Item
& o
) : sourcePos(o
.sourcePos
),
217 isId(o
.isId
), value(o
.value
), style(o
.style
),
218 format(o
.format
), bagKeyId(o
.bagKeyId
), evaluating(false) {
219 memset(&parsedValue
, 0, sizeof(parsedValue
));
223 Item
& operator=(const Item
& o
) {
224 sourcePos
= o
.sourcePos
;
229 bagKeyId
= o
.bagKeyId
;
230 parsedValue
= o
.parsedValue
;
237 Vector
<StringPool::entry_style_span
> style
;
240 mutable bool evaluating
;
241 Res_value parsedValue
;
244 class Entry
: public RefBase
{
246 Entry(const String16
& name
, const SourcePos
& pos
)
247 : mName(name
), mType(TYPE_UNKNOWN
),
248 mItemFormat(ResTable_map::TYPE_ANY
), mNameIndex(-1), mPos(pos
)
258 String16
getName() const { return mName
; }
259 type
getType() const { return mType
; }
261 void setParent(const String16
& parent
) { mParent
= parent
; }
262 String16
getParent() const { return mParent
; }
264 status_t
makeItABag(const SourcePos
& sourcePos
);
266 status_t
emptyBag(const SourcePos
& sourcePos
);
268 status_t
setItem(const SourcePos
& pos
,
269 const String16
& value
,
270 const Vector
<StringPool::entry_style_span
>* style
= NULL
,
271 int32_t format
= ResTable_map::TYPE_ANY
,
272 const bool overwrite
= false);
274 status_t
addToBag(const SourcePos
& pos
,
275 const String16
& key
, const String16
& value
,
276 const Vector
<StringPool::entry_style_span
>* style
= NULL
,
277 bool replace
=false, bool isId
= false,
278 int32_t format
= ResTable_map::TYPE_ANY
);
280 // Index of the entry's name string in the key pool.
281 int32_t getNameIndex() const { return mNameIndex
; }
282 void setNameIndex(int32_t index
) { mNameIndex
= index
; }
284 const Item
* getItem() const { return mType
== TYPE_ITEM
? &mItem
: NULL
; }
285 const KeyedVector
<String16
, Item
>& getBag() const { return mBag
; }
287 status_t
generateAttributes(ResourceTable
* table
,
288 const String16
& package
);
290 status_t
assignResourceIds(ResourceTable
* table
,
291 const String16
& package
);
293 status_t
prepareFlatten(StringPool
* strings
, ResourceTable
* table
);
295 ssize_t
flatten(Bundle
*, const sp
<AaptFile
>& data
, bool isPublic
);
297 const SourcePos
& getPos() const { return mPos
; }
305 KeyedVector
<String16
, Item
> mBag
;
311 struct ConfigDescription
: public ResTable_config
{
312 ConfigDescription() {
313 memset(this, 0, sizeof(*this));
314 size
= sizeof(ResTable_config
);
316 ConfigDescription(const ResTable_config
&o
) {
317 *static_cast<ResTable_config
*>(this) = o
;
318 size
= sizeof(ResTable_config
);
320 ConfigDescription(const ConfigDescription
&o
) {
321 *static_cast<ResTable_config
*>(this) = o
;
324 ConfigDescription
& operator=(const ResTable_config
& o
) {
325 *static_cast<ResTable_config
*>(this) = o
;
326 size
= sizeof(ResTable_config
);
329 ConfigDescription
& operator=(const ConfigDescription
& o
) {
330 *static_cast<ResTable_config
*>(this) = o
;
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; }
339 inline bool operator>(const ConfigDescription
& o
) const { return compare(o
) > 0; }
342 class ConfigList
: public RefBase
{
344 ConfigList(const String16
& name
, const SourcePos
& pos
)
345 : mName(name
), mPos(pos
), mPublic(false), mEntryIndex(-1) { }
346 virtual ~ConfigList() { }
348 String16
getName() const { return mName
; }
349 const SourcePos
& getPos() const { return mPos
; }
351 void appendComment(const String16
& comment
, bool onlyIfEmpty
= false);
352 const String16
& getComment() const { return mComment
; }
354 void appendTypeComment(const String16
& comment
);
355 const String16
& getTypeComment() const { return mTypeComment
; }
357 // Index of this entry in its Type.
358 int32_t getEntryIndex() const { return mEntryIndex
; }
359 void setEntryIndex(int32_t index
) { mEntryIndex
= index
; }
361 void setPublic(bool pub
) { mPublic
= pub
; }
362 bool getPublic() const { return mPublic
; }
363 void setPublicSourcePos(const SourcePos
& pos
) { mPublicSourcePos
= pos
; }
364 const SourcePos
& getPublicSourcePos() { return mPublicSourcePos
; }
366 void addEntry(const ResTable_config
& config
, const sp
<Entry
>& entry
) {
367 mEntries
.add(config
, entry
);
370 const DefaultKeyedVector
<ConfigDescription
, sp
<Entry
> >& getEntries() const { return mEntries
; }
372 const String16 mName
;
373 const SourcePos mPos
;
375 String16 mTypeComment
;
377 SourcePos mPublicSourcePos
;
379 DefaultKeyedVector
<ConfigDescription
, sp
<Entry
> > mEntries
;
384 Public() : sourcePos(), ident(0) { }
385 Public(const SourcePos
& pos
,
386 const String16
& _comment
,
389 comment(_comment
), ident(_ident
) { }
390 Public(const Public
& o
) : sourcePos(o
.sourcePos
),
391 comment(o
.comment
), ident(o
.ident
) { }
394 Public
& operator=(const Public
& o
) {
395 sourcePos
= o
.sourcePos
;
406 class Type
: public RefBase
{
408 Type(const String16
& name
, const SourcePos
& pos
)
409 : mName(name
), mFirstPublicSourcePos(NULL
), mPublicIndex(-1), mIndex(-1), mPos(pos
)
411 virtual ~Type() { delete mFirstPublicSourcePos
; }
413 status_t
addPublic(const SourcePos
& pos
,
414 const String16
& name
,
415 const uint32_t ident
);
417 String16
getName() const { return mName
; }
418 sp
<Entry
> getEntry(const String16
& entry
,
419 const SourcePos
& pos
,
420 const ResTable_config
* config
= NULL
,
421 bool doSetIndex
= false,
422 bool overlay
= false);
424 const SourcePos
& getFirstPublicSourcePos() const { return *mFirstPublicSourcePos
; }
426 int32_t getPublicIndex() const { return mPublicIndex
; }
428 int32_t getIndex() const { return mIndex
; }
429 void setIndex(int32_t index
) { mIndex
= index
; }
431 status_t
applyPublicEntryOrder();
433 const SortedVector
<ConfigDescription
>& getUniqueConfigs() const { return mUniqueConfigs
; }
435 const DefaultKeyedVector
<String16
, sp
<ConfigList
> >& getConfigs() const { return mConfigs
; }
436 const Vector
<sp
<ConfigList
> >& getOrderedConfigs() const { return mOrderedConfigs
; }
438 const SourcePos
& getPos() const { return mPos
; }
441 SourcePos
* mFirstPublicSourcePos
;
442 DefaultKeyedVector
<String16
, Public
> mPublic
;
443 SortedVector
<ConfigDescription
> mUniqueConfigs
;
444 DefaultKeyedVector
<String16
, sp
<ConfigList
> > mConfigs
;
445 Vector
<sp
<ConfigList
> > mOrderedConfigs
;
446 int32_t mPublicIndex
;
451 class Package
: public RefBase
{
453 Package(const String16
& name
, ssize_t includedId
=-1);
454 virtual ~Package() { }
456 String16
getName() const { return mName
; }
457 sp
<Type
> getType(const String16
& type
,
458 const SourcePos
& pos
,
459 bool doSetIndex
= false);
461 ssize_t
getAssignedId() const { return mIncludedId
; }
463 const ResStringPool
& getTypeStrings() const { return mTypeStrings
; }
464 uint32_t indexOfTypeString(const String16
& s
) const { return mTypeStringsMapping
.valueFor(s
); }
465 const sp
<AaptFile
> getTypeStringsData() const { return mTypeStringsData
; }
466 status_t
setTypeStrings(const sp
<AaptFile
>& data
);
468 const ResStringPool
& getKeyStrings() const { return mKeyStrings
; }
469 uint32_t indexOfKeyString(const String16
& s
) const { return mKeyStringsMapping
.valueFor(s
); }
470 const sp
<AaptFile
> getKeyStringsData() const { return mKeyStringsData
; }
471 status_t
setKeyStrings(const sp
<AaptFile
>& data
);
473 status_t
applyPublicTypeOrder();
475 const DefaultKeyedVector
<String16
, sp
<Type
> >& getTypes() const { return mTypes
; }
476 const Vector
<sp
<Type
> >& getOrderedTypes() const { return mOrderedTypes
; }
479 status_t
setStrings(const sp
<AaptFile
>& data
,
480 ResStringPool
* strings
,
481 DefaultKeyedVector
<String16
, uint32_t>* mappings
);
483 const String16 mName
;
484 const ssize_t mIncludedId
;
485 DefaultKeyedVector
<String16
, sp
<Type
> > mTypes
;
486 Vector
<sp
<Type
> > mOrderedTypes
;
487 sp
<AaptFile
> mTypeStringsData
;
488 sp
<AaptFile
> mKeyStringsData
;
489 ResStringPool mTypeStrings
;
490 ResStringPool mKeyStrings
;
491 DefaultKeyedVector
<String16
, uint32_t> mTypeStringsMapping
;
492 DefaultKeyedVector
<String16
, uint32_t> mKeyStringsMapping
;
496 void writePublicDefinitions(const String16
& package
, FILE* fp
, bool pub
);
497 sp
<Package
> getPackage(const String16
& package
);
498 sp
<Type
> getType(const String16
& package
,
499 const String16
& type
,
500 const SourcePos
& pos
,
501 bool doSetIndex
= false);
502 sp
<Entry
> getEntry(const String16
& package
,
503 const String16
& type
,
504 const String16
& name
,
505 const SourcePos
& pos
,
507 const ResTable_config
* config
= NULL
,
508 bool doSetIndex
= false);
509 sp
<const Entry
> getEntry(uint32_t resID
,
510 const ResTable_config
* config
= NULL
) const;
511 const Item
* getItem(uint32_t resID
, uint32_t attrID
) const;
512 bool getItemValue(uint32_t resID
, uint32_t attrID
,
513 Res_value
* outValue
);
516 String16 mAssetsPackage
;
517 sp
<AaptAssets
> mAssets
;
518 DefaultKeyedVector
<String16
, sp
<Package
> > mPackages
;
519 Vector
<sp
<Package
> > mOrderedPackages
;
520 uint32_t mNextPackageId
;
521 bool mHaveAppPackage
;
524 SourcePos mCurrentXmlPos
;
527 // key = string resource name, value = set of locales in which that name is defined
528 map
<String16
, set
<String8
> > mLocalizations
;
534 ResourceFilter() : mData(), mContainsPseudo(false) {}
535 status_t
parse(const char* arg
);
536 bool match(int axis
, uint32_t value
);
537 bool match(const ResTable_config
& config
);
538 inline bool containsPseudo() { return mContainsPseudo
; }
541 KeyedVector
<int,SortedVector
<uint32_t> > mData
;
542 bool mContainsPseudo
;