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
14 #include "ConfigDescription.h"
15 #include "ResourceFilter.h"
16 #include "SourcePos.h"
17 #include "StringPool.h"
24 XML_COMPILE_STRIP_COMMENTS = 1<<0,
25 XML_COMPILE_ASSIGN_ATTRIBUTE_IDS = 1<<1,
26 XML_COMPILE_COMPACT_WHITESPACE = 1<<2,
27 XML_COMPILE_STRIP_WHITESPACE = 1<<3,
28 XML_COMPILE_STRIP_RAW_VALUES = 1<<4,
29 XML_COMPILE_UTF8 = 1<<5,
31 XML_COMPILE_STANDARD_RESOURCE =
32 XML_COMPILE_STRIP_COMMENTS | XML_COMPILE_ASSIGN_ATTRIBUTE_IDS
33 | XML_COMPILE_STRIP_WHITESPACE | XML_COMPILE_STRIP_RAW_VALUES
36 status_t compileXmlFile(const Bundle* bundle,
37 const sp<AaptAssets>& assets,
38 const String16& resourceName,
39 const sp<AaptFile>& target,
41 int options = XML_COMPILE_STANDARD_RESOURCE);
43 status_t compileXmlFile(const Bundle* bundle,
44 const sp<AaptAssets>& assets,
45 const String16& resourceName,
46 const sp<AaptFile>& target,
47 const sp<AaptFile>& outTarget,
49 int options = XML_COMPILE_STANDARD_RESOURCE);
51 status_t compileXmlFile(const Bundle* bundle,
52 const sp<AaptAssets>& assets,
53 const String16& resourceName,
54 const sp<XMLNode>& xmlTree,
55 const sp<AaptFile>& target,
57 int options = XML_COMPILE_STANDARD_RESOURCE);
59 status_t compileResourceFile(Bundle* bundle,
60 const sp<AaptAssets>& assets,
61 const sp<AaptFile>& in,
62 const ResTable_config& defParams,
64 ResourceTable* outTable);
72 AccessorCookie(const SourcePos&p, const String8& a, const String8& v)
80 // Holds the necessary information to compile the
82 struct CompileResourceWorkItem {
83 String16 resourceName;
88 class ResourceTable : public ResTable::Accessor
91 // The type of package to build.
103 ResourceTable(Bundle* bundle, const String16& assetsPackage, PackageType type);
105 const String16& getAssetsPackage() const {
106 return mAssetsPackage;
110 * Returns the queue of resources that need to be compiled.
111 * This is only used for resources that have been generated
112 * during the compilation phase. If they were just added
113 * to the AaptAssets, then they may be skipped over
114 * and would mess up iteration order for the existing
117 std::queue<CompileResourceWorkItem>& getWorkQueue() {
121 status_t addIncludedResources(Bundle* bundle, const sp<AaptAssets>& assets);
123 status_t addPublic(const SourcePos& pos,
124 const String16& package,
125 const String16& type,
126 const String16& name,
127 const uint32_t ident);
129 status_t addEntry(const SourcePos& pos,
130 const String16& package,
131 const String16& type,
132 const String16& name,
133 const String16& value,
134 const Vector<StringPool::entry_style_span>* style = NULL,
135 const ResTable_config* params = NULL,
136 const bool doSetIndex = false,
137 const int32_t format = ResTable_map::TYPE_ANY,
138 const bool overwrite = false);
140 status_t startBag(const SourcePos& pos,
141 const String16& package,
142 const String16& type,
143 const String16& name,
144 const String16& bagParent,
145 const ResTable_config* params = NULL,
146 bool overlay = false,
147 bool replace = false,
150 status_t addBag(const SourcePos& pos,
151 const String16& package,
152 const String16& type,
153 const String16& name,
154 const String16& bagParent,
155 const String16& bagKey,
156 const String16& value,
157 const Vector<StringPool::entry_style_span>* style = NULL,
158 const ResTable_config* params = NULL,
159 bool replace = false,
161 const int32_t format = ResTable_map::TYPE_ANY);
163 bool hasBagOrEntry(const String16& package,
164 const String16& type,
165 const String16& name) const;
167 bool hasBagOrEntry(const String16& package,
168 const String16& type,
169 const String16& name,
170 const ResTable_config& config) const;
172 bool hasBagOrEntry(const String16& ref,
173 const String16* defType = NULL,
174 const String16* defPackage = NULL);
176 bool appendComment(const String16& package,
177 const String16& type,
178 const String16& name,
179 const String16& comment,
180 bool onlyIfEmpty = false);
182 bool appendTypeComment(const String16& package,
183 const String16& type,
184 const String16& name,
185 const String16& comment);
187 void canAddEntry(const SourcePos& pos,
188 const String16& package, const String16& type, const String16& name);
191 size_t numLocalResources() const;
192 bool hasResources() const;
194 status_t modifyForCompat(const Bundle* bundle);
195 status_t modifyForCompat(const Bundle* bundle,
196 const String16& resourceName,
197 const sp<AaptFile>& file,
198 const sp<XMLNode>& root);
200 sp<AaptFile> flatten(Bundle* bundle, const sp<const ResourceFilter>& filter,
203 static inline uint32_t makeResId(uint32_t packageId,
207 return nameId | (typeId<<16) | (packageId<<24);
210 static inline uint32_t getResId(const sp<Package>& p,
214 uint32_t getResId(const String16& package,
215 const String16& type,
216 const String16& name,
217 bool onlyPublic = true) const;
219 uint32_t getResId(const String16& ref,
220 const String16* defType = NULL,
221 const String16* defPackage = NULL,
222 const char** outErrorMsg = NULL,
223 bool onlyPublic = true) const;
225 static bool isValidResourceName(const String16& s);
227 bool stringToValue(Res_value* outValue, StringPool* pool,
229 bool preserveSpaces, bool coerceType,
231 const Vector<StringPool::entry_style_span>* style = NULL,
232 String16* outStr = NULL, void* accessorCookie = NULL,
233 uint32_t attrType = ResTable_map::TYPE_ANY,
234 const String8* configTypeName = NULL,
235 const ConfigDescription* config = NULL);
237 status_t assignResourceIds();
238 status_t addSymbols(const sp<AaptSymbols>& outSymbols = NULL);
239 void addLocalization(const String16& name, const String8& locale, const SourcePos& src);
240 status_t validateLocalizations(void);
242 status_t flatten(Bundle* bundle, const sp<const ResourceFilter>& filter,
243 const sp<AaptFile>& dest, const bool isBase);
244 status_t flattenLibraryTable(const sp<AaptFile>& dest, const Vector<sp<Package> >& libs);
246 void writePublicDefinitions(const String16& package, FILE* fp);
248 virtual uint32_t getCustomResource(const String16& package,
249 const String16& type,
250 const String16& name) const;
251 virtual uint32_t getCustomResourceWithCreation(const String16& package,
252 const String16& type,
253 const String16& name,
254 const bool createIfNeeded);
255 virtual uint32_t getRemappedPackage(uint32_t origPackage) const;
256 virtual bool getAttributeType(uint32_t attrID, uint32_t* outType);
257 virtual bool getAttributeMin(uint32_t attrID, uint32_t* outMin);
258 virtual bool getAttributeMax(uint32_t attrID, uint32_t* outMax);
259 virtual bool getAttributeKeys(uint32_t attrID, Vector<String16>* outKeys);
260 virtual bool getAttributeEnum(uint32_t attrID,
261 const char16_t* name, size_t nameLen,
262 Res_value* outValue);
263 virtual bool getAttributeFlags(uint32_t attrID,
264 const char16_t* name, size_t nameLen,
265 Res_value* outValue);
266 virtual uint32_t getAttributeL10N(uint32_t attrID);
268 virtual bool getLocalizationSetting();
269 virtual void reportError(void* accessorCookie, const char* fmt, ...);
271 void setCurrentXmlPos(const SourcePos& pos) { mCurrentXmlPos = pos; }
275 Item() : isId(false), format(ResTable_map::TYPE_ANY), bagKeyId(0), evaluating(false)
276 { memset(&parsedValue, 0, sizeof(parsedValue)); }
277 Item(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 Item(const Item& o) : sourcePos(o.sourcePos),
283 isId(o.isId), value(o.value), style(o.style),
284 format(o.format), bagKeyId(o.bagKeyId), evaluating(false) {
285 memset(&parsedValue, 0, sizeof(parsedValue));
289 Item& operator=(const Item& o) {
290 sourcePos = o.sourcePos;
295 bagKeyId = o.bagKeyId;
296 parsedValue = o.parsedValue;
303 Vector<StringPool::entry_style_span> style;
306 mutable bool evaluating;
307 Res_value parsedValue;
310 class Entry : public RefBase {
312 Entry(const String16& name, const SourcePos& pos)
313 : mName(name), mType(TYPE_UNKNOWN),
314 mItemFormat(ResTable_map::TYPE_ANY), mNameIndex(-1), mPos(pos)
317 Entry(const Entry& entry);
318 Entry& operator=(const Entry& entry);
328 String16 getName() const { return mName; }
329 type getType() const { return mType; }
331 void setParent(const String16& parent) { mParent = parent; }
332 String16 getParent() const { return mParent; }
334 status_t makeItABag(const SourcePos& sourcePos);
336 status_t emptyBag(const SourcePos& sourcePos);
338 status_t setItem(const SourcePos& pos,
339 const String16& value,
340 const Vector<StringPool::entry_style_span>* style = NULL,
341 int32_t format = ResTable_map::TYPE_ANY,
342 const bool overwrite = false);
344 status_t addToBag(const SourcePos& pos,
345 const String16& key, const String16& value,
346 const Vector<StringPool::entry_style_span>* style = NULL,
347 bool replace=false, bool isId = false,
348 int32_t format = ResTable_map::TYPE_ANY);
350 status_t removeFromBag(const String16& key);
352 // Index of the entry's name string in the key pool.
353 int32_t getNameIndex() const { return mNameIndex; }
354 void setNameIndex(int32_t index) { mNameIndex = index; }
356 const Item* getItem() const { return mType == TYPE_ITEM ? &mItem : NULL; }
357 const KeyedVector<String16, Item>& getBag() const { return mBag; }
359 status_t generateAttributes(ResourceTable* table,
360 const String16& package);
362 status_t assignResourceIds(ResourceTable* table,
363 const String16& package);
365 status_t prepareFlatten(StringPool* strings, ResourceTable* table,
366 const String8* configTypeName, const ConfigDescription* config);
368 status_t remapStringValue(StringPool* strings);
370 ssize_t flatten(Bundle*, const sp<AaptFile>& data, bool isPublic);
372 const SourcePos& getPos() const { return mPos; }
380 KeyedVector<String16, Item> mBag;
386 class ConfigList : public RefBase {
388 ConfigList(const String16& name, const SourcePos& pos)
389 : mName(name), mPos(pos), mPublic(false), mEntryIndex(-1) { }
390 virtual ~ConfigList() { }
392 String16 getName() const { return mName; }
393 const SourcePos& getPos() const { return mPos; }
395 void appendComment(const String16& comment, bool onlyIfEmpty = false);
396 const String16& getComment() const { return mComment; }
398 void appendTypeComment(const String16& comment);
399 const String16& getTypeComment() const { return mTypeComment; }
401 // Index of this entry in its Type.
402 int32_t getEntryIndex() const { return mEntryIndex; }
403 void setEntryIndex(int32_t index) { mEntryIndex = index; }
405 void setPublic(bool pub) { mPublic = pub; }
406 bool getPublic() const { return mPublic; }
407 void setPublicSourcePos(const SourcePos& pos) { mPublicSourcePos = pos; }
408 const SourcePos& getPublicSourcePos() { return mPublicSourcePos; }
410 void addEntry(const ResTable_config& config, const sp<Entry>& entry) {
411 mEntries.add(config, entry);
414 const DefaultKeyedVector<ConfigDescription, sp<Entry> >& getEntries() const { return mEntries; }
416 const String16 mName;
417 const SourcePos mPos;
419 String16 mTypeComment;
421 SourcePos mPublicSourcePos;
423 DefaultKeyedVector<ConfigDescription, sp<Entry> > mEntries;
428 Public() : sourcePos(), ident(0) { }
429 Public(const SourcePos& pos,
430 const String16& _comment,
433 comment(_comment), ident(_ident) { }
434 Public(const Public& o) : sourcePos(o.sourcePos),
435 comment(o.comment), ident(o.ident) { }
438 Public& operator=(const Public& o) {
439 sourcePos = o.sourcePos;
450 class Type : public RefBase {
452 Type(const String16& name, const SourcePos& pos)
453 : mName(name), mFirstPublicSourcePos(NULL), mPublicIndex(-1), mIndex(-1), mPos(pos)
455 virtual ~Type() { delete mFirstPublicSourcePos; }
457 status_t addPublic(const SourcePos& pos,
458 const String16& name,
459 const uint32_t ident);
461 void canAddEntry(const String16& name);
463 String16 getName() const { return mName; }
464 sp<Entry> getEntry(const String16& entry,
465 const SourcePos& pos,
466 const ResTable_config* config = NULL,
467 bool doSetIndex = false,
468 bool overlay = false,
469 bool autoAddOverlay = false);
471 bool isPublic(const String16& entry) const {
472 return mPublic.indexOfKey(entry) >= 0;
475 sp<ConfigList> removeEntry(const String16& entry);
477 SortedVector<ConfigDescription> getUniqueConfigs() const;
479 const SourcePos& getFirstPublicSourcePos() const { return *mFirstPublicSourcePos; }
481 int32_t getPublicIndex() const { return mPublicIndex; }
483 int32_t getIndex() const { return mIndex; }
484 void setIndex(int32_t index) { mIndex = index; }
486 status_t applyPublicEntryOrder();
488 const DefaultKeyedVector<String16, sp<ConfigList> >& getConfigs() const { return mConfigs; }
489 const Vector<sp<ConfigList> >& getOrderedConfigs() const { return mOrderedConfigs; }
490 const SortedVector<String16>& getCanAddEntries() const { return mCanAddEntries; }
492 const SourcePos& getPos() const { return mPos; }
496 SourcePos* mFirstPublicSourcePos;
497 DefaultKeyedVector<String16, Public> mPublic;
498 DefaultKeyedVector<String16, sp<ConfigList> > mConfigs;
499 Vector<sp<ConfigList> > mOrderedConfigs;
500 SortedVector<String16> mCanAddEntries;
501 int32_t mPublicIndex;
506 class Package : public RefBase {
508 Package(const String16& name, size_t packageId);
509 virtual ~Package() { }
511 String16 getName() const { return mName; }
512 sp<Type> getType(const String16& type,
513 const SourcePos& pos,
514 bool doSetIndex = false);
516 size_t getAssignedId() const { return mPackageId; }
518 const ResStringPool& getTypeStrings() const { return mTypeStrings; }
519 uint32_t indexOfTypeString(const String16& s) const { return mTypeStringsMapping.valueFor(s); }
520 const sp<AaptFile> getTypeStringsData() const { return mTypeStringsData; }
521 status_t setTypeStrings(const sp<AaptFile>& data);
523 const ResStringPool& getKeyStrings() const { return mKeyStrings; }
524 uint32_t indexOfKeyString(const String16& s) const { return mKeyStringsMapping.valueFor(s); }
525 const sp<AaptFile> getKeyStringsData() const { return mKeyStringsData; }
526 status_t setKeyStrings(const sp<AaptFile>& data);
528 status_t applyPublicTypeOrder();
530 const DefaultKeyedVector<String16, sp<Type> >& getTypes() const { return mTypes; }
531 const Vector<sp<Type> >& getOrderedTypes() const { return mOrderedTypes; }
533 void movePrivateAttrs();
536 status_t setStrings(const sp<AaptFile>& data,
537 ResStringPool* strings,
538 DefaultKeyedVector<String16, uint32_t>* mappings);
540 const String16 mName;
541 const size_t mPackageId;
542 DefaultKeyedVector<String16, sp<Type> > mTypes;
543 Vector<sp<Type> > mOrderedTypes;
544 sp<AaptFile> mTypeStringsData;
545 sp<AaptFile> mKeyStringsData;
546 ResStringPool mTypeStrings;
547 ResStringPool mKeyStrings;
548 DefaultKeyedVector<String16, uint32_t> mTypeStringsMapping;
549 DefaultKeyedVector<String16, uint32_t> mKeyStringsMapping;
552 void getDensityVaryingResources(KeyedVector<Symbol, Vector<SymbolDefinition> >& resources);
555 void writePublicDefinitions(const String16& package, FILE* fp, bool pub);
556 sp<Package> getPackage(const String16& package);
557 sp<Type> getType(const String16& package,
558 const String16& type,
559 const SourcePos& pos,
560 bool doSetIndex = false);
561 sp<Entry> getEntry(const String16& package,
562 const String16& type,
563 const String16& name,
564 const SourcePos& pos,
566 const ResTable_config* config = NULL,
567 bool doSetIndex = false);
568 sp<const Entry> getEntry(uint32_t resID,
569 const ResTable_config* config = NULL) const;
570 sp<ConfigList> getConfigList(const String16& package,
571 const String16& type,
572 const String16& name) const;
573 const Item* getItem(uint32_t resID, uint32_t attrID) const;
574 bool getItemValue(uint32_t resID, uint32_t attrID,
575 Res_value* outValue);
576 int getPublicAttributeSdkLevel(uint32_t attrId) const;
579 String16 mAssetsPackage;
580 PackageType mPackageType;
581 sp<AaptAssets> mAssets;
582 uint32_t mTypeIdOffset;
583 DefaultKeyedVector<String16, sp<Package> > mPackages;
584 Vector<sp<Package> > mOrderedPackages;
586 SourcePos mCurrentXmlPos;
589 // key = string resource name, value = set of locales in which that name is defined
590 std::map<String16, std::map<String8, SourcePos>> mLocalizations;
591 std::queue<CompileResourceWorkItem> mWorkQueue;