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 struct ConfigDescription
: public ResTable_config
{
81 memset(this, 0, sizeof(*this));
82 size
= sizeof(ResTable_config
);
84 ConfigDescription(const ResTable_config
&o
) {
85 *static_cast<ResTable_config
*>(this) = o
;
86 size
= sizeof(ResTable_config
);
88 ConfigDescription(const ConfigDescription
&o
) {
89 *static_cast<ResTable_config
*>(this) = o
;
92 ConfigDescription
& operator=(const ResTable_config
& o
) {
93 *static_cast<ResTable_config
*>(this) = o
;
94 size
= sizeof(ResTable_config
);
97 ConfigDescription
& operator=(const ConfigDescription
& o
) {
98 *static_cast<ResTable_config
*>(this) = o
;
102 inline bool operator<(const ConfigDescription
& o
) const { return compare(o
) < 0; }
103 inline bool operator<=(const ConfigDescription
& o
) const { return compare(o
) <= 0; }
104 inline bool operator==(const ConfigDescription
& o
) const { return compare(o
) == 0; }
105 inline bool operator!=(const ConfigDescription
& o
) const { return compare(o
) != 0; }
106 inline bool operator>=(const ConfigDescription
& o
) const { return compare(o
) >= 0; }
107 inline bool operator>(const ConfigDescription
& o
) const { return compare(o
) > 0; }
110 ResourceTable(Bundle
* bundle
, const String16
& assetsPackage
);
112 status_t
addIncludedResources(Bundle
* bundle
, const sp
<AaptAssets
>& assets
);
114 status_t
addPublic(const SourcePos
& pos
,
115 const String16
& package
,
116 const String16
& type
,
117 const String16
& name
,
118 const uint32_t ident
);
120 status_t
addEntry(const SourcePos
& pos
,
121 const String16
& package
,
122 const String16
& type
,
123 const String16
& name
,
124 const String16
& value
,
125 const Vector
<StringPool::entry_style_span
>* style
= NULL
,
126 const ResTable_config
* params
= NULL
,
127 const bool doSetIndex
= false,
128 const int32_t format
= ResTable_map::TYPE_ANY
,
129 const bool overwrite
= false);
131 status_t
startBag(const SourcePos
& pos
,
132 const String16
& package
,
133 const String16
& type
,
134 const String16
& name
,
135 const String16
& bagParent
,
136 const ResTable_config
* params
= NULL
,
137 bool overlay
= false,
138 bool replace
= false,
141 status_t
addBag(const SourcePos
& pos
,
142 const String16
& package
,
143 const String16
& type
,
144 const String16
& name
,
145 const String16
& bagParent
,
146 const String16
& bagKey
,
147 const String16
& value
,
148 const Vector
<StringPool::entry_style_span
>* style
= NULL
,
149 const ResTable_config
* params
= NULL
,
150 bool replace
= false,
152 const int32_t format
= ResTable_map::TYPE_ANY
);
154 bool hasBagOrEntry(const String16
& package
,
155 const String16
& type
,
156 const String16
& name
) const;
158 bool hasBagOrEntry(const String16
& package
,
159 const String16
& type
,
160 const String16
& name
,
161 const ResTable_config
& config
) const;
163 bool hasBagOrEntry(const String16
& ref
,
164 const String16
* defType
= NULL
,
165 const String16
* defPackage
= NULL
);
167 bool appendComment(const String16
& package
,
168 const String16
& type
,
169 const String16
& name
,
170 const String16
& comment
,
171 bool onlyIfEmpty
= false);
173 bool appendTypeComment(const String16
& package
,
174 const String16
& type
,
175 const String16
& name
,
176 const String16
& comment
);
178 void canAddEntry(const SourcePos
& pos
,
179 const String16
& package
, const String16
& type
, const String16
& name
);
182 size_t numLocalResources() const;
183 bool hasResources() const;
185 sp
<AaptFile
> flatten(Bundle
*);
187 static inline uint32_t makeResId(uint32_t packageId
,
191 return nameId
| (typeId
<<16) | (packageId
<<24);
194 static inline uint32_t getResId(const sp
<Package
>& p
,
198 uint32_t getResId(const String16
& package
,
199 const String16
& type
,
200 const String16
& name
,
201 bool onlyPublic
= true) const;
203 uint32_t getResId(const String16
& ref
,
204 const String16
* defType
= NULL
,
205 const String16
* defPackage
= NULL
,
206 const char** outErrorMsg
= NULL
,
207 bool onlyPublic
= true) const;
209 static bool isValidResourceName(const String16
& s
);
211 bool stringToValue(Res_value
* outValue
, StringPool
* pool
,
213 bool preserveSpaces
, bool coerceType
,
215 const Vector
<StringPool::entry_style_span
>* style
= NULL
,
216 String16
* outStr
= NULL
, void* accessorCookie
= NULL
,
217 uint32_t attrType
= ResTable_map::TYPE_ANY
,
218 const String8
* configTypeName
= NULL
,
219 const ConfigDescription
* config
= NULL
);
221 status_t
assignResourceIds();
222 status_t
addSymbols(const sp
<AaptSymbols
>& outSymbols
= NULL
);
223 void addLocalization(const String16
& name
, const String8
& locale
);
224 status_t
validateLocalizations(void);
226 status_t
flatten(Bundle
*, const sp
<AaptFile
>& dest
);
228 void writePublicDefinitions(const String16
& package
, FILE* fp
);
230 virtual uint32_t getCustomResource(const String16
& package
,
231 const String16
& type
,
232 const String16
& name
) const;
233 virtual uint32_t getCustomResourceWithCreation(const String16
& package
,
234 const String16
& type
,
235 const String16
& name
,
236 const bool createIfNeeded
);
237 virtual uint32_t getRemappedPackage(uint32_t origPackage
) const;
238 virtual bool getAttributeType(uint32_t attrID
, uint32_t* outType
);
239 virtual bool getAttributeMin(uint32_t attrID
, uint32_t* outMin
);
240 virtual bool getAttributeMax(uint32_t attrID
, uint32_t* outMax
);
241 virtual bool getAttributeKeys(uint32_t attrID
, Vector
<String16
>* outKeys
);
242 virtual bool getAttributeEnum(uint32_t attrID
,
243 const char16_t* name
, size_t nameLen
,
244 Res_value
* outValue
);
245 virtual bool getAttributeFlags(uint32_t attrID
,
246 const char16_t* name
, size_t nameLen
,
247 Res_value
* outValue
);
248 virtual uint32_t getAttributeL10N(uint32_t attrID
);
250 virtual bool getLocalizationSetting();
251 virtual void reportError(void* accessorCookie
, const char* fmt
, ...);
253 void setCurrentXmlPos(const SourcePos
& pos
) { mCurrentXmlPos
= pos
; }
257 Item() : isId(false), format(ResTable_map::TYPE_ANY
), bagKeyId(0), evaluating(false)
258 { memset(&parsedValue
, 0, sizeof(parsedValue
)); }
259 Item(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 Item(const Item
& o
) : sourcePos(o
.sourcePos
),
265 isId(o
.isId
), value(o
.value
), style(o
.style
),
266 format(o
.format
), bagKeyId(o
.bagKeyId
), evaluating(false) {
267 memset(&parsedValue
, 0, sizeof(parsedValue
));
271 Item
& operator=(const Item
& o
) {
272 sourcePos
= o
.sourcePos
;
277 bagKeyId
= o
.bagKeyId
;
278 parsedValue
= o
.parsedValue
;
285 Vector
<StringPool::entry_style_span
> style
;
288 mutable bool evaluating
;
289 Res_value parsedValue
;
292 class Entry
: public RefBase
{
294 Entry(const String16
& name
, const SourcePos
& pos
)
295 : mName(name
), mType(TYPE_UNKNOWN
),
296 mItemFormat(ResTable_map::TYPE_ANY
), mNameIndex(-1), mPos(pos
)
306 String16
getName() const { return mName
; }
307 type
getType() const { return mType
; }
309 void setParent(const String16
& parent
) { mParent
= parent
; }
310 String16
getParent() const { return mParent
; }
312 status_t
makeItABag(const SourcePos
& sourcePos
);
314 status_t
emptyBag(const SourcePos
& sourcePos
);
316 status_t
setItem(const SourcePos
& pos
,
317 const String16
& value
,
318 const Vector
<StringPool::entry_style_span
>* style
= NULL
,
319 int32_t format
= ResTable_map::TYPE_ANY
,
320 const bool overwrite
= false);
322 status_t
addToBag(const SourcePos
& pos
,
323 const String16
& key
, const String16
& value
,
324 const Vector
<StringPool::entry_style_span
>* style
= NULL
,
325 bool replace
=false, bool isId
= false,
326 int32_t format
= ResTable_map::TYPE_ANY
);
328 // Index of the entry's name string in the key pool.
329 int32_t getNameIndex() const { return mNameIndex
; }
330 void setNameIndex(int32_t index
) { mNameIndex
= index
; }
332 const Item
* getItem() const { return mType
== TYPE_ITEM
? &mItem
: NULL
; }
333 const KeyedVector
<String16
, Item
>& getBag() const { return mBag
; }
335 status_t
generateAttributes(ResourceTable
* table
,
336 const String16
& package
);
338 status_t
assignResourceIds(ResourceTable
* table
,
339 const String16
& package
);
341 status_t
prepareFlatten(StringPool
* strings
, ResourceTable
* table
,
342 const String8
* configTypeName
, const ConfigDescription
* config
);
344 status_t
remapStringValue(StringPool
* strings
);
346 ssize_t
flatten(Bundle
*, const sp
<AaptFile
>& data
, bool isPublic
);
348 const SourcePos
& getPos() const { return mPos
; }
356 KeyedVector
<String16
, Item
> mBag
;
362 class ConfigList
: public RefBase
{
364 ConfigList(const String16
& name
, const SourcePos
& pos
)
365 : mName(name
), mPos(pos
), mPublic(false), mEntryIndex(-1) { }
366 virtual ~ConfigList() { }
368 String16
getName() const { return mName
; }
369 const SourcePos
& getPos() const { return mPos
; }
371 void appendComment(const String16
& comment
, bool onlyIfEmpty
= false);
372 const String16
& getComment() const { return mComment
; }
374 void appendTypeComment(const String16
& comment
);
375 const String16
& getTypeComment() const { return mTypeComment
; }
377 // Index of this entry in its Type.
378 int32_t getEntryIndex() const { return mEntryIndex
; }
379 void setEntryIndex(int32_t index
) { mEntryIndex
= index
; }
381 void setPublic(bool pub
) { mPublic
= pub
; }
382 bool getPublic() const { return mPublic
; }
383 void setPublicSourcePos(const SourcePos
& pos
) { mPublicSourcePos
= pos
; }
384 const SourcePos
& getPublicSourcePos() { return mPublicSourcePos
; }
386 void addEntry(const ResTable_config
& config
, const sp
<Entry
>& entry
) {
387 mEntries
.add(config
, entry
);
390 const DefaultKeyedVector
<ConfigDescription
, sp
<Entry
> >& getEntries() const { return mEntries
; }
392 const String16 mName
;
393 const SourcePos mPos
;
395 String16 mTypeComment
;
397 SourcePos mPublicSourcePos
;
399 DefaultKeyedVector
<ConfigDescription
, sp
<Entry
> > mEntries
;
404 Public() : sourcePos(), ident(0) { }
405 Public(const SourcePos
& pos
,
406 const String16
& _comment
,
409 comment(_comment
), ident(_ident
) { }
410 Public(const Public
& o
) : sourcePos(o
.sourcePos
),
411 comment(o
.comment
), ident(o
.ident
) { }
414 Public
& operator=(const Public
& o
) {
415 sourcePos
= o
.sourcePos
;
426 class Type
: public RefBase
{
428 Type(const String16
& name
, const SourcePos
& pos
)
429 : mName(name
), mFirstPublicSourcePos(NULL
), mPublicIndex(-1), mIndex(-1), mPos(pos
)
431 virtual ~Type() { delete mFirstPublicSourcePos
; }
433 status_t
addPublic(const SourcePos
& pos
,
434 const String16
& name
,
435 const uint32_t ident
);
437 void canAddEntry(const String16
& name
);
439 String16
getName() const { return mName
; }
440 sp
<Entry
> getEntry(const String16
& entry
,
441 const SourcePos
& pos
,
442 const ResTable_config
* config
= NULL
,
443 bool doSetIndex
= false,
444 bool overlay
= false,
445 bool autoAddOverlay
= false);
447 const SourcePos
& getFirstPublicSourcePos() const { return *mFirstPublicSourcePos
; }
449 int32_t getPublicIndex() const { return mPublicIndex
; }
451 int32_t getIndex() const { return mIndex
; }
452 void setIndex(int32_t index
) { mIndex
= index
; }
454 status_t
applyPublicEntryOrder();
456 const SortedVector
<ConfigDescription
>& getUniqueConfigs() const { return mUniqueConfigs
; }
458 const DefaultKeyedVector
<String16
, sp
<ConfigList
> >& getConfigs() const { return mConfigs
; }
459 const Vector
<sp
<ConfigList
> >& getOrderedConfigs() const { return mOrderedConfigs
; }
461 const SortedVector
<String16
>& getCanAddEntries() const { return mCanAddEntries
; }
463 const SourcePos
& getPos() const { return mPos
; }
466 SourcePos
* mFirstPublicSourcePos
;
467 DefaultKeyedVector
<String16
, Public
> mPublic
;
468 SortedVector
<ConfigDescription
> mUniqueConfigs
;
469 DefaultKeyedVector
<String16
, sp
<ConfigList
> > mConfigs
;
470 Vector
<sp
<ConfigList
> > mOrderedConfigs
;
471 SortedVector
<String16
> mCanAddEntries
;
472 int32_t mPublicIndex
;
477 class Package
: public RefBase
{
479 Package(const String16
& name
, ssize_t includedId
=-1);
480 virtual ~Package() { }
482 String16
getName() const { return mName
; }
483 sp
<Type
> getType(const String16
& type
,
484 const SourcePos
& pos
,
485 bool doSetIndex
= false);
487 ssize_t
getAssignedId() const { return mIncludedId
; }
489 const ResStringPool
& getTypeStrings() const { return mTypeStrings
; }
490 uint32_t indexOfTypeString(const String16
& s
) const { return mTypeStringsMapping
.valueFor(s
); }
491 const sp
<AaptFile
> getTypeStringsData() const { return mTypeStringsData
; }
492 status_t
setTypeStrings(const sp
<AaptFile
>& data
);
494 const ResStringPool
& getKeyStrings() const { return mKeyStrings
; }
495 uint32_t indexOfKeyString(const String16
& s
) const { return mKeyStringsMapping
.valueFor(s
); }
496 const sp
<AaptFile
> getKeyStringsData() const { return mKeyStringsData
; }
497 status_t
setKeyStrings(const sp
<AaptFile
>& data
);
499 status_t
applyPublicTypeOrder();
501 const DefaultKeyedVector
<String16
, sp
<Type
> >& getTypes() const { return mTypes
; }
502 const Vector
<sp
<Type
> >& getOrderedTypes() const { return mOrderedTypes
; }
505 status_t
setStrings(const sp
<AaptFile
>& data
,
506 ResStringPool
* strings
,
507 DefaultKeyedVector
<String16
, uint32_t>* mappings
);
509 const String16 mName
;
510 const ssize_t mIncludedId
;
511 DefaultKeyedVector
<String16
, sp
<Type
> > mTypes
;
512 Vector
<sp
<Type
> > mOrderedTypes
;
513 sp
<AaptFile
> mTypeStringsData
;
514 sp
<AaptFile
> mKeyStringsData
;
515 ResStringPool mTypeStrings
;
516 ResStringPool mKeyStrings
;
517 DefaultKeyedVector
<String16
, uint32_t> mTypeStringsMapping
;
518 DefaultKeyedVector
<String16
, uint32_t> mKeyStringsMapping
;
522 void writePublicDefinitions(const String16
& package
, FILE* fp
, bool pub
);
523 sp
<Package
> getPackage(const String16
& package
);
524 sp
<Type
> getType(const String16
& package
,
525 const String16
& type
,
526 const SourcePos
& pos
,
527 bool doSetIndex
= false);
528 sp
<Entry
> getEntry(const String16
& package
,
529 const String16
& type
,
530 const String16
& name
,
531 const SourcePos
& pos
,
533 const ResTable_config
* config
= NULL
,
534 bool doSetIndex
= false);
535 sp
<const Entry
> getEntry(uint32_t resID
,
536 const ResTable_config
* config
= NULL
) const;
537 const Item
* getItem(uint32_t resID
, uint32_t attrID
) const;
538 bool getItemValue(uint32_t resID
, uint32_t attrID
,
539 Res_value
* outValue
);
542 String16 mAssetsPackage
;
543 sp
<AaptAssets
> mAssets
;
544 DefaultKeyedVector
<String16
, sp
<Package
> > mPackages
;
545 Vector
<sp
<Package
> > mOrderedPackages
;
546 uint32_t mNextPackageId
;
547 bool mHaveAppPackage
;
550 SourcePos mCurrentXmlPos
;
553 // key = string resource name, value = set of locales in which that name is defined
554 map
<String16
, set
<String8
> > mLocalizations
;