OSDN Git Service

am 400fd04f: am b1dc614d: am f80370c6: Merge "docs: build tools 22.0.1 revision updat...
[android-x86/frameworks-base.git] / tools / aapt / 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 <map>
11 #include <queue>
12 #include <set>
13
14 #include "ConfigDescription.h"
15 #include "ResourceFilter.h"
16 #include "SourcePos.h"
17 #include "StringPool.h"
18 #include "Symbol.h"
19
20 class XMLNode;
21 class ResourceTable;
22
23 enum {
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,
30
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
34 };
35
36 status_t compileXmlFile(const Bundle* bundle,
37                         const sp<AaptAssets>& assets,
38                         const String16& resourceName,
39                         const sp<AaptFile>& target,
40                         ResourceTable* table,
41                         int options = XML_COMPILE_STANDARD_RESOURCE);
42
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,
48                         ResourceTable* table,
49                         int options = XML_COMPILE_STANDARD_RESOURCE);
50
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,
56                         ResourceTable* table,
57                         int options = XML_COMPILE_STANDARD_RESOURCE);
58
59 status_t compileResourceFile(Bundle* bundle,
60                              const sp<AaptAssets>& assets,
61                              const sp<AaptFile>& in,
62                              const ResTable_config& defParams,
63                              const bool overwrite,
64                              ResourceTable* outTable);
65
66 struct AccessorCookie
67 {
68     SourcePos sourcePos;
69     String8 attr;
70     String8 value;
71
72     AccessorCookie(const SourcePos&p, const String8& a, const String8& v)
73         :sourcePos(p),
74          attr(a),
75          value(v)
76     {
77     }
78 };
79
80 // Holds the necessary information to compile the
81 // resource.
82 struct CompileResourceWorkItem {
83     String16 resourceName;
84     String8 resPath;
85     sp<AaptFile> file;
86 };
87
88 class ResourceTable : public ResTable::Accessor
89 {
90 public:
91     // The type of package to build.
92     enum PackageType {
93         App,
94         System,
95         SharedLibrary,
96         AppFeature
97     };
98
99     class Package;
100     class Type;
101     class Entry;
102
103     ResourceTable(Bundle* bundle, const String16& assetsPackage, PackageType type);
104
105     const String16& getAssetsPackage() const {
106         return mAssetsPackage;
107     }
108
109     /**
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
115      * resources.
116      */
117     std::queue<CompileResourceWorkItem>& getWorkQueue() {
118         return mWorkQueue;
119     }
120
121     status_t addIncludedResources(Bundle* bundle, const sp<AaptAssets>& assets);
122
123     status_t addPublic(const SourcePos& pos,
124                        const String16& package,
125                        const String16& type,
126                        const String16& name,
127                        const uint32_t ident);
128
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);
139
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,
148                     bool isId = false);
149     
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,
160                     bool isId = false,
161                     const int32_t format = ResTable_map::TYPE_ANY);
162
163     bool hasBagOrEntry(const String16& package,
164                        const String16& type,
165                        const String16& name) const;
166
167     bool hasBagOrEntry(const String16& package,
168                        const String16& type,
169                        const String16& name,
170                        const ResTable_config& config) const;
171
172     bool hasBagOrEntry(const String16& ref,
173                        const String16* defType = NULL,
174                        const String16* defPackage = NULL);
175
176     bool appendComment(const String16& package,
177                        const String16& type,
178                        const String16& name,
179                        const String16& comment,
180                        bool onlyIfEmpty = false);
181
182     bool appendTypeComment(const String16& package,
183                            const String16& type,
184                            const String16& name,
185                            const String16& comment);
186     
187     void canAddEntry(const SourcePos& pos,
188         const String16& package, const String16& type, const String16& name);
189         
190     size_t size() const;
191     size_t numLocalResources() const;
192     bool hasResources() const;
193
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);
199
200     sp<AaptFile> flatten(Bundle* bundle, const sp<const ResourceFilter>& filter,
201             const bool isBase);
202
203     static inline uint32_t makeResId(uint32_t packageId,
204                                      uint32_t typeId,
205                                      uint32_t nameId)
206     {
207         return nameId | (typeId<<16) | (packageId<<24);
208     }
209
210     static inline uint32_t getResId(const sp<Package>& p,
211                                     const sp<Type>& t,
212                                     uint32_t nameId);
213
214     uint32_t getResId(const String16& package,
215                       const String16& type,
216                       const String16& name,
217                       bool onlyPublic = true) const;
218
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;
224
225     static bool isValidResourceName(const String16& s);
226     
227     bool stringToValue(Res_value* outValue, StringPool* pool,
228                        const String16& str,
229                        bool preserveSpaces, bool coerceType,
230                        uint32_t attrID,
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);
236
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);
241
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);
245
246     void writePublicDefinitions(const String16& package, FILE* fp);
247
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);
267
268     virtual bool getLocalizationSetting();
269     virtual void reportError(void* accessorCookie, const char* fmt, ...);
270
271     void setCurrentXmlPos(const SourcePos& pos) { mCurrentXmlPos = pos; }
272
273     class Item {
274     public:
275         Item() : isId(false), format(ResTable_map::TYPE_ANY), bagKeyId(0), evaluating(false)
276             { memset(&parsedValue, 0, sizeof(parsedValue)); }
277         Item(const SourcePos& pos,
278              bool _isId,
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));
286         }
287         ~Item() { }
288
289         Item& operator=(const Item& o) {
290             sourcePos = o.sourcePos;
291             isId = o.isId;
292             value = o.value;
293             style = o.style;
294             format = o.format;
295             bagKeyId = o.bagKeyId;
296             parsedValue = o.parsedValue;
297             return *this;
298         }
299
300         SourcePos                               sourcePos;
301         mutable bool                            isId;
302         String16                                value;
303         Vector<StringPool::entry_style_span>    style;
304         int32_t                                 format;
305         uint32_t                                bagKeyId;
306         mutable bool                            evaluating;
307         Res_value                               parsedValue;
308     };
309
310     class Entry : public RefBase {
311     public:
312         Entry(const String16& name, const SourcePos& pos)
313             : mName(name), mType(TYPE_UNKNOWN),
314               mItemFormat(ResTable_map::TYPE_ANY), mNameIndex(-1), mPos(pos)
315         { }
316
317         Entry(const Entry& entry);
318         Entry& operator=(const Entry& entry);
319
320         virtual ~Entry() { }
321
322         enum type {
323             TYPE_UNKNOWN = 0,
324             TYPE_ITEM,
325             TYPE_BAG
326         };
327         
328         String16 getName() const { return mName; }
329         type getType() const { return mType; }
330
331         void setParent(const String16& parent) { mParent = parent; }
332         String16 getParent() const { return mParent; }
333
334         status_t makeItABag(const SourcePos& sourcePos);
335
336         status_t emptyBag(const SourcePos& sourcePos);
337  
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);
343
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);
349
350         status_t removeFromBag(const String16& key);
351
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; }
355
356         const Item* getItem() const { return mType == TYPE_ITEM ? &mItem : NULL; }
357         const KeyedVector<String16, Item>& getBag() const { return mBag; }
358
359         status_t generateAttributes(ResourceTable* table,
360                                     const String16& package);
361
362         status_t assignResourceIds(ResourceTable* table,
363                                    const String16& package);
364
365         status_t prepareFlatten(StringPool* strings, ResourceTable* table,
366                const String8* configTypeName, const ConfigDescription* config);
367
368         status_t remapStringValue(StringPool* strings);
369
370         ssize_t flatten(Bundle*, const sp<AaptFile>& data, bool isPublic);
371
372         const SourcePos& getPos() const { return mPos; }
373
374     private:
375         String16 mName;
376         String16 mParent;
377         type mType;
378         Item mItem;
379         int32_t mItemFormat;
380         KeyedVector<String16, Item> mBag;
381         int32_t mNameIndex;
382         uint32_t mParentId;
383         SourcePos mPos;
384     };
385     
386     class ConfigList : public RefBase {
387     public:
388         ConfigList(const String16& name, const SourcePos& pos)
389             : mName(name), mPos(pos), mPublic(false), mEntryIndex(-1) { }
390         virtual ~ConfigList() { }
391         
392         String16 getName() const { return mName; }
393         const SourcePos& getPos() const { return mPos; }
394         
395         void appendComment(const String16& comment, bool onlyIfEmpty = false);
396         const String16& getComment() const { return mComment; }
397         
398         void appendTypeComment(const String16& comment);
399         const String16& getTypeComment() const { return mTypeComment; }
400         
401         // Index of this entry in its Type.
402         int32_t getEntryIndex() const { return mEntryIndex; }
403         void setEntryIndex(int32_t index) { mEntryIndex = index; }
404         
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; }
409         
410         void addEntry(const ResTable_config& config, const sp<Entry>& entry) {
411             mEntries.add(config, entry);
412         }
413         
414         const DefaultKeyedVector<ConfigDescription, sp<Entry> >& getEntries() const { return mEntries; }
415     private:
416         const String16 mName;
417         const SourcePos mPos;
418         String16 mComment;
419         String16 mTypeComment;
420         bool mPublic;
421         SourcePos mPublicSourcePos;
422         int32_t mEntryIndex;
423         DefaultKeyedVector<ConfigDescription, sp<Entry> > mEntries;
424     };
425     
426     class Public {
427     public:
428         Public() : sourcePos(), ident(0) { }
429         Public(const SourcePos& pos,
430                const String16& _comment,
431                uint32_t _ident)
432             : sourcePos(pos),
433             comment(_comment), ident(_ident) { }
434         Public(const Public& o) : sourcePos(o.sourcePos),
435             comment(o.comment), ident(o.ident) { }
436         ~Public() { }
437         
438         Public& operator=(const Public& o) {
439             sourcePos = o.sourcePos;
440             comment = o.comment;
441             ident = o.ident;
442             return *this;
443         }
444         
445         SourcePos   sourcePos;
446         String16    comment;
447         uint32_t    ident;
448     };
449     
450     class Type : public RefBase {
451     public:
452         Type(const String16& name, const SourcePos& pos)
453                 : mName(name), mFirstPublicSourcePos(NULL), mPublicIndex(-1), mIndex(-1), mPos(pos)
454         { }
455         virtual ~Type() { delete mFirstPublicSourcePos; }
456
457         status_t addPublic(const SourcePos& pos,
458                            const String16& name,
459                            const uint32_t ident);
460                            
461         void canAddEntry(const String16& name);
462         
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);
470
471         bool isPublic(const String16& entry) const {
472             return mPublic.indexOfKey(entry) >= 0;
473         }
474
475         sp<ConfigList> removeEntry(const String16& entry);
476
477         SortedVector<ConfigDescription> getUniqueConfigs() const;
478
479         const SourcePos& getFirstPublicSourcePos() const { return *mFirstPublicSourcePos; }
480
481         int32_t getPublicIndex() const { return mPublicIndex; }
482
483         int32_t getIndex() const { return mIndex; }
484         void setIndex(int32_t index) { mIndex = index; }
485
486         status_t applyPublicEntryOrder();
487
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; }
491         
492         const SourcePos& getPos() const { return mPos; }
493
494     private:
495         String16 mName;
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;
502         int32_t mIndex;
503         SourcePos mPos;
504     };
505
506     class Package : public RefBase {
507     public:
508         Package(const String16& name, size_t packageId);
509         virtual ~Package() { }
510
511         String16 getName() const { return mName; }
512         sp<Type> getType(const String16& type,
513                          const SourcePos& pos,
514                          bool doSetIndex = false);
515
516         size_t getAssignedId() const { return mPackageId; }
517
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);
522
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);
527
528         status_t applyPublicTypeOrder();
529
530         const DefaultKeyedVector<String16, sp<Type> >& getTypes() const { return mTypes; }
531         const Vector<sp<Type> >& getOrderedTypes() const { return mOrderedTypes; }
532
533         void movePrivateAttrs();
534
535     private:
536         status_t setStrings(const sp<AaptFile>& data,
537                             ResStringPool* strings,
538                             DefaultKeyedVector<String16, uint32_t>* mappings);
539
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;
550     };
551
552     void getDensityVaryingResources(KeyedVector<Symbol, Vector<SymbolDefinition> >& resources);
553
554 private:
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,
565                        bool overlay,
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;
577
578
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;
585     size_t mNumLocal;
586     SourcePos mCurrentXmlPos;
587     Bundle* mBundle;
588
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;
592 };
593
594 #endif