]> git.saurik.com Git - android/aapt.git/blob - ResourceTable.h
auto import from //branches/cupcake/...@132569
[android/aapt.git] / ResourceTable.h
1 //
2 // Copyright 2006 The Android Open Source Project
3 //
4 // Build resource files from raw assets.
5 //
6
7 #ifndef RESOURCE_TABLE_H
8 #define RESOURCE_TABLE_H
9
10 #include "StringPool.h"
11 #include "SourcePos.h"
12
13 #include <set>
14 #include <map>
15
16 using namespace std;
17
18 class ResourceTable;
19
20 enum {
21 XML_COMPILE_STRIP_COMMENTS = 1<<0,
22 XML_COMPILE_ASSIGN_ATTRIBUTE_IDS = 1<<1,
23 XML_COMPILE_COMPACT_WHITESPACE = 1<<2,
24 XML_COMPILE_STRIP_WHITESPACE = 1<<3,
25 XML_COMPILE_STRIP_RAW_VALUES = 1<<4,
26
27 XML_COMPILE_STANDARD_RESOURCE =
28 XML_COMPILE_STRIP_COMMENTS | XML_COMPILE_ASSIGN_ATTRIBUTE_IDS
29 | XML_COMPILE_STRIP_WHITESPACE | XML_COMPILE_STRIP_RAW_VALUES
30 };
31
32 status_t compileXmlFile(const sp<AaptAssets>& assets,
33 const sp<AaptFile>& target,
34 ResourceTable* table,
35 int options = XML_COMPILE_STANDARD_RESOURCE);
36
37 status_t compileResourceFile(Bundle* bundle,
38 const sp<AaptAssets>& assets,
39 const sp<AaptFile>& in,
40 const ResTable_config& defParams,
41 const bool overwrite,
42 ResourceTable* outTable);
43
44 struct AccessorCookie
45 {
46 SourcePos sourcePos;
47 String8 attr;
48 String8 value;
49
50 AccessorCookie(const SourcePos&p, const String8& a, const String8& v)
51 :sourcePos(p),
52 attr(a),
53 value(v)
54 {
55 }
56 };
57
58 class ResourceTable : public ResTable::Accessor
59 {
60 public:
61 class Package;
62 class Type;
63 class Entry;
64
65 ResourceTable(Bundle* bundle, const String16& assetsPackage);
66
67 status_t addIncludedResources(Bundle* bundle, const sp<AaptAssets>& assets);
68
69 status_t addPublic(const SourcePos& pos,
70 const String16& package,
71 const String16& type,
72 const String16& name,
73 const uint32_t ident);
74
75 status_t addEntry(const SourcePos& pos,
76 const String16& package,
77 const String16& type,
78 const String16& name,
79 const String16& value,
80 const Vector<StringPool::entry_style_span>* style = NULL,
81 const ResTable_config* params = NULL,
82 const bool doSetIndex = false,
83 const int32_t format = ResTable_map::TYPE_ANY,
84 const bool overwrite = false);
85
86 status_t startBag(const SourcePos& pos,
87 const String16& package,
88 const String16& type,
89 const String16& name,
90 const String16& bagParent,
91 const ResTable_config* params = NULL,
92 bool replace = false,
93 bool isId = false);
94
95 status_t addBag(const SourcePos& pos,
96 const String16& package,
97 const String16& type,
98 const String16& name,
99 const String16& bagParent,
100 const String16& bagKey,
101 const String16& value,
102 const Vector<StringPool::entry_style_span>* style = NULL,
103 const ResTable_config* params = NULL,
104 bool replace = false,
105 bool isId = false,
106 const int32_t format = ResTable_map::TYPE_ANY);
107
108 bool hasBagOrEntry(const String16& package,
109 const String16& type,
110 const String16& name) const;
111
112 bool hasBagOrEntry(const String16& ref,
113 const String16* defType = NULL,
114 const String16* defPackage = NULL);
115
116 bool appendComment(const String16& package,
117 const String16& type,
118 const String16& name,
119 const String16& comment,
120 bool onlyIfEmpty = false);
121
122 bool appendTypeComment(const String16& package,
123 const String16& type,
124 const String16& name,
125 const String16& comment);
126
127 size_t size() const;
128 size_t numLocalResources() const;
129 bool hasResources() const;
130
131 sp<AaptFile> flatten(Bundle*);
132
133 static inline uint32_t makeResId(uint32_t packageId,
134 uint32_t typeId,
135 uint32_t nameId)
136 {
137 return nameId | (typeId<<16) | (packageId<<24);
138 }
139
140 static inline uint32_t getResId(const sp<Package>& p,
141 const sp<Type>& t,
142 uint32_t nameId);
143
144 uint32_t getResId(const String16& package,
145 const String16& type,
146 const String16& name,
147 bool onlyPublic = false) const;
148
149 uint32_t getResId(const String16& ref,
150 const String16* defType = NULL,
151 const String16* defPackage = NULL,
152 const char** outErrorMsg = NULL,
153 bool onlyPublic = false) const;
154
155 static bool isValidResourceName(const String16& s);
156
157 bool stringToValue(Res_value* outValue, StringPool* pool,
158 const String16& str,
159 bool preserveSpaces, bool coerceType,
160 uint32_t attrID,
161 const Vector<StringPool::entry_style_span>* style = NULL,
162 String16* outStr = NULL, void* accessorCookie = NULL,
163 uint32_t attrType = ResTable_map::TYPE_ANY);
164
165 status_t assignResourceIds();
166 status_t addSymbols(const sp<AaptSymbols>& outSymbols = NULL);
167 void addLocalization(const String16& name, const String8& locale);
168 status_t validateLocalizations(void);
169
170 status_t flatten(Bundle*, const sp<AaptFile>& dest);
171
172 void writePublicDefinitions(const String16& package, FILE* fp);
173
174 virtual uint32_t getCustomResource(const String16& package,
175 const String16& type,
176 const String16& name) const;
177 virtual uint32_t getCustomResourceWithCreation(const String16& package,
178 const String16& type,
179 const String16& name,
180 const bool createIfNeeded);
181 virtual uint32_t getRemappedPackage(uint32_t origPackage) const;
182 virtual bool getAttributeType(uint32_t attrID, uint32_t* outType);
183 virtual bool getAttributeMin(uint32_t attrID, uint32_t* outMin);
184 virtual bool getAttributeMax(uint32_t attrID, uint32_t* outMax);
185 virtual bool getAttributeKeys(uint32_t attrID, Vector<String16>* outKeys);
186 virtual bool getAttributeEnum(uint32_t attrID,
187 const char16_t* name, size_t nameLen,
188 Res_value* outValue);
189 virtual bool getAttributeFlags(uint32_t attrID,
190 const char16_t* name, size_t nameLen,
191 Res_value* outValue);
192 virtual uint32_t getAttributeL10N(uint32_t attrID);
193
194 virtual bool getLocalizationSetting();
195 virtual void reportError(void* accessorCookie, const char* fmt, ...);
196
197 void setCurrentXmlPos(const SourcePos& pos) { mCurrentXmlPos = pos; }
198
199 class Item {
200 public:
201 Item() : isId(false), format(ResTable_map::TYPE_ANY), bagKeyId(0), evaluating(false)
202 { memset(&parsedValue, 0, sizeof(parsedValue)); }
203 Item(const SourcePos& pos,
204 bool _isId,
205 const String16& _value,
206 const Vector<StringPool::entry_style_span>* _style = NULL,
207 int32_t format = ResTable_map::TYPE_ANY);
208 Item(const Item& o) : sourcePos(o.sourcePos),
209 isId(o.isId), value(o.value), style(o.style),
210 format(o.format), bagKeyId(o.bagKeyId), evaluating(false) {
211 memset(&parsedValue, 0, sizeof(parsedValue));
212 }
213 ~Item() { }
214
215 Item& operator=(const Item& o) {
216 sourcePos = o.sourcePos;
217 isId = o.isId;
218 value = o.value;
219 style = o.style;
220 format = o.format;
221 bagKeyId = o.bagKeyId;
222 parsedValue = o.parsedValue;
223 return *this;
224 }
225
226 SourcePos sourcePos;
227 mutable bool isId;
228 String16 value;
229 Vector<StringPool::entry_style_span> style;
230 int32_t format;
231 uint32_t bagKeyId;
232 mutable bool evaluating;
233 Res_value parsedValue;
234 };
235
236 class Entry : public RefBase {
237 public:
238 Entry(const String16& name, const SourcePos& pos)
239 : mName(name), mType(TYPE_UNKNOWN),
240 mItemFormat(ResTable_map::TYPE_ANY), mNameIndex(-1), mPos(pos)
241 { }
242 virtual ~Entry() { }
243
244 enum type {
245 TYPE_UNKNOWN = 0,
246 TYPE_ITEM,
247 TYPE_BAG
248 };
249
250 String16 getName() const { return mName; }
251 type getType() const { return mType; }
252
253 void setParent(const String16& parent) { mParent = parent; }
254 String16 getParent() const { return mParent; }
255
256 status_t makeItABag(const SourcePos& sourcePos);
257
258 status_t setItem(const SourcePos& pos,
259 const String16& value,
260 const Vector<StringPool::entry_style_span>* style = NULL,
261 int32_t format = ResTable_map::TYPE_ANY,
262 const bool overwrite = false);
263
264 status_t addToBag(const SourcePos& pos,
265 const String16& key, const String16& value,
266 const Vector<StringPool::entry_style_span>* style = NULL,
267 bool replace=false, bool isId = false,
268 int32_t format = ResTable_map::TYPE_ANY);
269
270 // Index of the entry's name string in the key pool.
271 int32_t getNameIndex() const { return mNameIndex; }
272 void setNameIndex(int32_t index) { mNameIndex = index; }
273
274 const Item* getItem() const { return mType == TYPE_ITEM ? &mItem : NULL; }
275 const KeyedVector<String16, Item>& getBag() const { return mBag; }
276
277 status_t generateAttributes(ResourceTable* table,
278 const String16& package);
279
280 status_t assignResourceIds(ResourceTable* table,
281 const String16& package);
282
283 status_t prepareFlatten(StringPool* strings, ResourceTable* table);
284
285 ssize_t flatten(Bundle*, const sp<AaptFile>& data, bool isPublic);
286
287 const SourcePos& getPos() const { return mPos; }
288
289 private:
290 String16 mName;
291 String16 mParent;
292 type mType;
293 Item mItem;
294 int32_t mItemFormat;
295 KeyedVector<String16, Item> mBag;
296 int32_t mNameIndex;
297 uint32_t mParentId;
298 SourcePos mPos;
299 };
300
301 struct ConfigDescription : public ResTable_config {
302 ConfigDescription() {
303 memset(this, 0, sizeof(*this));
304 size = sizeof(ResTable_config);
305 }
306 ConfigDescription(const ResTable_config&o) {
307 *static_cast<ResTable_config*>(this) = o;
308 size = sizeof(ResTable_config);
309 }
310 ConfigDescription(const ConfigDescription&o) {
311 *static_cast<ResTable_config*>(this) = o;
312 }
313
314 ConfigDescription& operator=(const ResTable_config& o) {
315 *static_cast<ResTable_config*>(this) = o;
316 size = sizeof(ResTable_config);
317 return *this;
318 }
319 ConfigDescription& operator=(const ConfigDescription& o) {
320 *static_cast<ResTable_config*>(this) = o;
321 return *this;
322 }
323
324 inline bool operator<(const ConfigDescription& o) const { return compare(o) < 0; }
325 inline bool operator<=(const ConfigDescription& o) const { return compare(o) <= 0; }
326 inline bool operator==(const ConfigDescription& o) const { return compare(o) == 0; }
327 inline bool operator!=(const ConfigDescription& o) const { return compare(o) != 0; }
328 inline bool operator>=(const ConfigDescription& o) const { return compare(o) >= 0; }
329 inline bool operator>(const ConfigDescription& o) const { return compare(o) > 0; }
330 };
331
332 class ConfigList : public RefBase {
333 public:
334 ConfigList(const String16& name, const SourcePos& pos)
335 : mName(name), mPos(pos), mPublic(false), mEntryIndex(-1) { }
336 virtual ~ConfigList() { }
337
338 String16 getName() const { return mName; }
339 const SourcePos& getPos() const { return mPos; }
340
341 void appendComment(const String16& comment, bool onlyIfEmpty = false);
342 const String16& getComment() const { return mComment; }
343
344 void appendTypeComment(const String16& comment);
345 const String16& getTypeComment() const { return mTypeComment; }
346
347 // Index of this entry in its Type.
348 int32_t getEntryIndex() const { return mEntryIndex; }
349 void setEntryIndex(int32_t index) { mEntryIndex = index; }
350
351 void setPublic(bool pub) { mPublic = pub; }
352 bool getPublic() const { return mPublic; }
353 void setPublicSourcePos(const SourcePos& pos) { mPublicSourcePos = pos; }
354 const SourcePos& getPublicSourcePos() { return mPublicSourcePos; }
355
356 void addEntry(const ResTable_config& config, const sp<Entry>& entry) {
357 mEntries.add(config, entry);
358 }
359
360 const DefaultKeyedVector<ConfigDescription, sp<Entry> >& getEntries() const { return mEntries; }
361 private:
362 const String16 mName;
363 const SourcePos mPos;
364 String16 mComment;
365 String16 mTypeComment;
366 bool mPublic;
367 SourcePos mPublicSourcePos;
368 int32_t mEntryIndex;
369 DefaultKeyedVector<ConfigDescription, sp<Entry> > mEntries;
370 };
371
372 class Public {
373 public:
374 Public() : sourcePos(), ident(0) { }
375 Public(const SourcePos& pos,
376 const String16& _comment,
377 uint32_t _ident)
378 : sourcePos(pos),
379 comment(_comment), ident(_ident) { }
380 Public(const Public& o) : sourcePos(o.sourcePos),
381 comment(o.comment), ident(o.ident) { }
382 ~Public() { }
383
384 Public& operator=(const Public& o) {
385 sourcePos = o.sourcePos;
386 comment = o.comment;
387 ident = o.ident;
388 return *this;
389 }
390
391 SourcePos sourcePos;
392 String16 comment;
393 uint32_t ident;
394 };
395
396 class Type : public RefBase {
397 public:
398 Type(const String16& name, const SourcePos& pos)
399 : mName(name), mFirstPublicSourcePos(NULL), mPublicIndex(-1), mIndex(-1), mPos(pos)
400 { }
401 virtual ~Type() { delete mFirstPublicSourcePos; }
402
403 status_t addPublic(const SourcePos& pos,
404 const String16& name,
405 const uint32_t ident);
406
407 String16 getName() const { return mName; }
408 sp<Entry> getEntry(const String16& entry,
409 const SourcePos& pos,
410 const ResTable_config* config = NULL,
411 bool doSetIndex = false);
412
413 const SourcePos& getFirstPublicSourcePos() const { return *mFirstPublicSourcePos; }
414
415 int32_t getPublicIndex() const { return mPublicIndex; }
416
417 int32_t getIndex() const { return mIndex; }
418 void setIndex(int32_t index) { mIndex = index; }
419
420 status_t applyPublicEntryOrder();
421
422 const SortedVector<ConfigDescription>& getUniqueConfigs() const { return mUniqueConfigs; }
423
424 const DefaultKeyedVector<String16, sp<ConfigList> >& getConfigs() const { return mConfigs; }
425 const Vector<sp<ConfigList> >& getOrderedConfigs() const { return mOrderedConfigs; }
426
427 const SourcePos& getPos() const { return mPos; }
428 private:
429 String16 mName;
430 SourcePos* mFirstPublicSourcePos;
431 DefaultKeyedVector<String16, Public> mPublic;
432 SortedVector<ConfigDescription> mUniqueConfigs;
433 DefaultKeyedVector<String16, sp<ConfigList> > mConfigs;
434 Vector<sp<ConfigList> > mOrderedConfigs;
435 int32_t mPublicIndex;
436 int32_t mIndex;
437 SourcePos mPos;
438 };
439
440 class Package : public RefBase {
441 public:
442 Package(const String16& name, ssize_t includedId=-1);
443 virtual ~Package() { }
444
445 String16 getName() const { return mName; }
446 sp<Type> getType(const String16& type,
447 const SourcePos& pos,
448 bool doSetIndex = false);
449
450 ssize_t getAssignedId() const { return mIncludedId; }
451
452 const ResStringPool& getTypeStrings() const { return mTypeStrings; }
453 uint32_t indexOfTypeString(const String16& s) const { return mTypeStringsMapping.valueFor(s); }
454 const sp<AaptFile> getTypeStringsData() const { return mTypeStringsData; }
455 status_t setTypeStrings(const sp<AaptFile>& data);
456
457 const ResStringPool& getKeyStrings() const { return mKeyStrings; }
458 uint32_t indexOfKeyString(const String16& s) const { return mKeyStringsMapping.valueFor(s); }
459 const sp<AaptFile> getKeyStringsData() const { return mKeyStringsData; }
460 status_t setKeyStrings(const sp<AaptFile>& data);
461
462 status_t applyPublicTypeOrder();
463
464 const DefaultKeyedVector<String16, sp<Type> >& getTypes() const { return mTypes; }
465 const Vector<sp<Type> >& getOrderedTypes() const { return mOrderedTypes; }
466
467 private:
468 status_t setStrings(const sp<AaptFile>& data,
469 ResStringPool* strings,
470 DefaultKeyedVector<String16, uint32_t>* mappings);
471
472 const String16 mName;
473 const ssize_t mIncludedId;
474 DefaultKeyedVector<String16, sp<Type> > mTypes;
475 Vector<sp<Type> > mOrderedTypes;
476 sp<AaptFile> mTypeStringsData;
477 sp<AaptFile> mKeyStringsData;
478 ResStringPool mTypeStrings;
479 ResStringPool mKeyStrings;
480 DefaultKeyedVector<String16, uint32_t> mTypeStringsMapping;
481 DefaultKeyedVector<String16, uint32_t> mKeyStringsMapping;
482 };
483
484 private:
485 void writePublicDefinitions(const String16& package, FILE* fp, bool pub);
486 sp<Package> getPackage(const String16& package);
487 sp<Type> getType(const String16& package,
488 const String16& type,
489 const SourcePos& pos,
490 bool doSetIndex = false);
491 sp<Entry> getEntry(const String16& package,
492 const String16& type,
493 const String16& name,
494 const SourcePos& pos,
495 const ResTable_config* config = NULL,
496 bool doSetIndex = false);
497 sp<const Entry> getEntry(uint32_t resID,
498 const ResTable_config* config = NULL) const;
499 const Item* getItem(uint32_t resID, uint32_t attrID) const;
500 bool getItemValue(uint32_t resID, uint32_t attrID,
501 Res_value* outValue);
502
503
504 String16 mAssetsPackage;
505 sp<AaptAssets> mAssets;
506 DefaultKeyedVector<String16, sp<Package> > mPackages;
507 Vector<sp<Package> > mOrderedPackages;
508 uint32_t mNextPackageId;
509 bool mHaveAppPackage;
510 bool mIsAppPackage;
511 size_t mNumLocal;
512 SourcePos mCurrentXmlPos;
513 Bundle* mBundle;
514
515 // key = string resource name, value = set of locales in which that name is defined
516 map<String16, set<String8> > mLocalizations;
517 };
518
519 class ResourceFilter
520 {
521 public:
522 ResourceFilter() : mData(), mContainsPseudo(false) {}
523 status_t parse(const char* arg);
524 bool match(int axis, uint32_t value);
525 bool match(const ResTable_config& config);
526 inline bool containsPseudo() { return mContainsPseudo; }
527
528 private:
529 KeyedVector<int,SortedVector<uint32_t> > mData;
530 bool mContainsPseudo;
531 };
532
533
534 #endif