2 // Copyright 2006 The Android Open Source Project
4 // Information about assets being operated on.
6 #ifndef __AAPT_ASSETS_H
7 #define __AAPT_ASSETS_H
10 #include <utils/AssetManager.h>
11 #include <utils/KeyedVector.h>
12 #include <utils/String8.h>
13 #include <utils/ResourceTypes.h>
14 #include <utils/SortedVector.h>
15 #include <utils/String8.h>
16 #include <utils/Vector.h>
17 #include <utils/RefBase.h>
21 #include "SourcePos.h"
23 using namespace android
;
25 bool valid_symbol_name(const String8
& str
);
33 AXIS_SCREENLAYOUTSIZE
,
34 AXIS_SCREENLAYOUTLONG
,
47 * This structure contains a specific variation of a single file out
48 * of all the variations it can have that we can have.
54 AaptGroupEntry(const String8
& _locale
, const String8
& _vendor
)
55 : locale(_locale
), vendor(_vendor
) { }
61 String8 screenLayoutSize
;
62 String8 screenLayoutLong
;
73 bool initFromDirName(const char* dir
, String8
* resType
);
75 static status_t
parseNamePart(const String8
& part
, int* axis
, uint32_t* value
);
77 static bool getMccName(const char* name
, ResTable_config
* out
= NULL
);
78 static bool getMncName(const char* name
, ResTable_config
* out
= NULL
);
79 static bool getLocaleName(const char* name
, ResTable_config
* out
= NULL
);
80 static bool getScreenLayoutSizeName(const char* name
, ResTable_config
* out
= NULL
);
81 static bool getScreenLayoutLongName(const char* name
, ResTable_config
* out
= NULL
);
82 static bool getOrientationName(const char* name
, ResTable_config
* out
= NULL
);
83 static bool getDensityName(const char* name
, ResTable_config
* out
= NULL
);
84 static bool getTouchscreenName(const char* name
, ResTable_config
* out
= NULL
);
85 static bool getKeysHiddenName(const char* name
, ResTable_config
* out
= NULL
);
86 static bool getKeyboardName(const char* name
, ResTable_config
* out
= NULL
);
87 static bool getNavigationName(const char* name
, ResTable_config
* out
= NULL
);
88 static bool getNavHiddenName(const char* name
, ResTable_config
* out
= NULL
);
89 static bool getScreenSizeName(const char* name
, ResTable_config
* out
= NULL
);
90 static bool getVersionName(const char* name
, ResTable_config
* out
= NULL
);
92 int compare(const AaptGroupEntry
& o
) const;
94 ResTable_config
toParams() const;
96 inline bool operator<(const AaptGroupEntry
& o
) const { return compare(o
) < 0; }
97 inline bool operator<=(const AaptGroupEntry
& o
) const { return compare(o
) <= 0; }
98 inline bool operator==(const AaptGroupEntry
& o
) const { return compare(o
) == 0; }
99 inline bool operator!=(const AaptGroupEntry
& o
) const { return compare(o
) != 0; }
100 inline bool operator>=(const AaptGroupEntry
& o
) const { return compare(o
) >= 0; }
101 inline bool operator>(const AaptGroupEntry
& o
) const { return compare(o
) > 0; }
103 String8
toString() const;
104 String8
toDirName(const String8
& resType
) const;
107 inline int compare_type(const AaptGroupEntry
& lhs
, const AaptGroupEntry
& rhs
)
109 return lhs
.compare(rhs
);
112 inline int strictly_order_type(const AaptGroupEntry
& lhs
, const AaptGroupEntry
& rhs
)
114 return compare_type(lhs
, rhs
) < 0;
120 * A single asset file we know about.
122 class AaptFile
: public RefBase
125 AaptFile(const String8
& sourceFile
, const AaptGroupEntry
& groupEntry
,
126 const String8
& resType
)
127 : mGroupEntry(groupEntry
)
128 , mResourceType(resType
)
129 , mSourceFile(sourceFile
)
133 , mCompression(ZipEntry::kCompressStored
)
135 //printf("new AaptFile created %s\n", (const char*)sourceFile);
137 virtual ~AaptFile() {
141 const String8
& getPath() const { return mPath
; }
142 const AaptGroupEntry
& getGroupEntry() const { return mGroupEntry
; }
144 // Data API. If there is data attached to the file,
145 // getSourceFile() is not used.
146 bool hasData() const { return mData
!= NULL
; }
147 const void* getData() const { return mData
; }
148 size_t getSize() const { return mDataSize
; }
149 void* editData(size_t size
);
150 void* editData(size_t* outSize
= NULL
);
151 void* padData(size_t wordSize
);
152 status_t
writeData(const void* data
, size_t size
);
155 const String8
& getResourceType() const { return mResourceType
; }
157 // File API. If the file does not hold raw data, this is
158 // a full path to a file on the filesystem that holds its data.
159 const String8
& getSourceFile() const { return mSourceFile
; }
161 String8
getPrintableSource() const;
163 // Desired compression method, as per utils/ZipEntry.h. For example,
164 // no compression is ZipEntry::kCompressStored.
165 int getCompressionMethod() const { return mCompression
; }
166 void setCompressionMethod(int c
) { mCompression
= c
; }
168 friend class AaptGroup
;
171 AaptGroupEntry mGroupEntry
;
172 String8 mResourceType
;
181 * A group of related files (the same file, with different
182 * vendor/locale variations).
184 class AaptGroup
: public RefBase
187 AaptGroup(const String8
& leaf
, const String8
& path
)
188 : mLeaf(leaf
), mPath(path
) { }
189 virtual ~AaptGroup() { }
191 const String8
& getLeaf() const { return mLeaf
; }
193 // Returns the relative path after the AaptGroupEntry dirs.
194 const String8
& getPath() const { return mPath
; }
196 const DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> >& getFiles() const
199 status_t
addFile(const sp
<AaptFile
>& file
);
200 void removeFile(size_t index
);
204 String8
getPrintableSource() const;
210 DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> > mFiles
;
214 * A single directory of assets, which can contain for files and other
217 class AaptDir
: public RefBase
220 AaptDir(const String8
& leaf
, const String8
& path
)
221 : mLeaf(leaf
), mPath(path
) { }
222 virtual ~AaptDir() { }
224 const String8
& getLeaf() const { return mLeaf
; }
226 const String8
& getPath() const { return mPath
; }
228 const DefaultKeyedVector
<String8
, sp
<AaptGroup
> >& getFiles() const { return mFiles
; }
229 const DefaultKeyedVector
<String8
, sp
<AaptDir
> >& getDirs() const { return mDirs
; }
231 status_t
addFile(const String8
& name
, const sp
<AaptGroup
>& file
);
232 status_t
addDir(const String8
& name
, const sp
<AaptDir
>& dir
);
234 sp
<AaptDir
> makeDir(const String8
& name
);
236 void removeFile(const String8
& name
);
237 void removeDir(const String8
& name
);
239 status_t
renameFile(const sp
<AaptFile
>& file
, const String8
& newName
);
241 status_t
addLeafFile(const String8
& leafName
,
242 const sp
<AaptFile
>& file
);
244 virtual ssize_t
slurpFullTree(Bundle
* bundle
,
245 const String8
& srcDir
,
246 const AaptGroupEntry
& kind
,
247 const String8
& resType
);
250 * Perform some sanity checks on the names of files and directories here.
252 * - Check for illegal chars in filenames.
253 * - Check filename length.
254 * - Check for presence of ".gz" and non-".gz" copies of same file.
255 * - Check for multiple files whose names match in a case-insensitive
256 * fashion (problematic for some systems).
258 * Comparing names against all other names is O(n^2). We could speed
259 * it up some by sorting the entries and being smarter about what we
260 * compare against, but I'm not expecting to have enough files in a
261 * single directory to make a noticeable difference in speed.
263 * Note that sorting here is not enough to guarantee that the package
264 * contents are sorted -- subsequent updates can rearrange things.
266 status_t
validate() const;
270 String8
getPrintableSource() const;
276 DefaultKeyedVector
<String8
, sp
<AaptGroup
> > mFiles
;
277 DefaultKeyedVector
<String8
, sp
<AaptDir
> > mDirs
;
281 * All information we know about a particular symbol.
283 class AaptSymbolEntry
287 : isPublic(false), typeCode(TYPE_UNKNOWN
)
290 AaptSymbolEntry(const String8
& _name
)
291 : name(_name
), isPublic(false), typeCode(TYPE_UNKNOWN
)
294 AaptSymbolEntry(const AaptSymbolEntry
& o
)
295 : name(o
.name
), sourcePos(o
.sourcePos
), isPublic(o
.isPublic
)
296 , comment(o
.comment
), typeComment(o
.typeComment
)
297 , typeCode(o
.typeCode
), int32Val(o
.int32Val
), stringVal(o
.stringVal
)
300 AaptSymbolEntry
operator=(const AaptSymbolEntry
& o
)
302 sourcePos
= o
.sourcePos
;
303 isPublic
= o
.isPublic
;
305 typeComment
= o
.typeComment
;
306 typeCode
= o
.typeCode
;
307 int32Val
= o
.int32Val
;
308 stringVal
= o
.stringVal
;
318 String16 typeComment
;
328 // Value. May be one of these.
334 * A group of related symbols (such as indices into a string block)
335 * that have been generated from the assets.
337 class AaptSymbols
: public RefBase
341 virtual ~AaptSymbols() { }
343 status_t
addSymbol(const String8
& name
, int32_t value
, const SourcePos
& pos
) {
344 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
347 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
348 sym
.typeCode
= AaptSymbolEntry::TYPE_INT32
;
349 sym
.int32Val
= value
;
353 status_t
addStringSymbol(const String8
& name
, const String8
& value
,
354 const SourcePos
& pos
) {
355 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
358 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
359 sym
.typeCode
= AaptSymbolEntry::TYPE_STRING
;
360 sym
.stringVal
= value
;
364 status_t
makeSymbolPublic(const String8
& name
, const SourcePos
& pos
) {
365 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
368 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
373 void appendComment(const String8
& name
, const String16
& comment
, const SourcePos
& pos
) {
374 if (comment
.size() <= 0) {
377 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
378 if (sym
.comment
.size() == 0) {
379 sym
.comment
= comment
;
381 sym
.comment
.append(String16("\n"));
382 sym
.comment
.append(comment
);
386 void appendTypeComment(const String8
& name
, const String16
& comment
) {
387 if (comment
.size() <= 0) {
390 AaptSymbolEntry
& sym
= edit_symbol(name
, NULL
);
391 if (sym
.typeComment
.size() == 0) {
392 sym
.typeComment
= comment
;
394 sym
.typeComment
.append(String16("\n"));
395 sym
.typeComment
.append(comment
);
399 sp
<AaptSymbols
> addNestedSymbol(const String8
& name
, const SourcePos
& pos
) {
400 if (!check_valid_symbol_name(name
, pos
, "nested symbol")) {
404 sp
<AaptSymbols
> sym
= mNestedSymbols
.valueFor(name
);
406 sym
= new AaptSymbols();
407 mNestedSymbols
.add(name
, sym
);
413 const KeyedVector
<String8
, AaptSymbolEntry
>& getSymbols() const
415 const DefaultKeyedVector
<String8
, sp
<AaptSymbols
> >& getNestedSymbols() const
416 { return mNestedSymbols
; }
418 const String16
& getComment(const String8
& name
) const
419 { return get_symbol(name
).comment
; }
420 const String16
& getTypeComment(const String8
& name
) const
421 { return get_symbol(name
).typeComment
; }
424 bool check_valid_symbol_name(const String8
& symbol
, const SourcePos
& pos
, const char* label
) {
425 if (valid_symbol_name(symbol
)) {
428 pos
.error("invalid %s: '%s'\n", label
, symbol
.string());
431 AaptSymbolEntry
& edit_symbol(const String8
& symbol
, const SourcePos
* pos
) {
432 ssize_t i
= mSymbols
.indexOfKey(symbol
);
434 i
= mSymbols
.add(symbol
, AaptSymbolEntry(symbol
));
436 AaptSymbolEntry
& sym
= mSymbols
.editValueAt(i
);
437 if (pos
!= NULL
&& sym
.sourcePos
.line
< 0) {
438 sym
.sourcePos
= *pos
;
442 const AaptSymbolEntry
& get_symbol(const String8
& symbol
) const {
443 ssize_t i
= mSymbols
.indexOfKey(symbol
);
445 return mSymbols
.valueAt(i
);
450 KeyedVector
<String8
, AaptSymbolEntry
> mSymbols
;
451 DefaultKeyedVector
<String8
, sp
<AaptSymbols
> > mNestedSymbols
;
452 AaptSymbolEntry mDefSymbol
;
455 class ResourceTypeSet
: public RefBase
,
456 public KeyedVector
<String8
,sp
<AaptGroup
> >
464 * Asset hierarchy being operated on.
466 class AaptAssets
: public AaptDir
469 AaptAssets() : AaptDir(String8(), String8()), mHaveIncludedAssets(false), mRes(NULL
) { }
470 virtual ~AaptAssets() { delete mRes
; }
472 const String8
& getPackage() const { return mPackage
; }
473 void setPackage(const String8
& package
) { mPackage
= package
; mSymbolsPrivatePackage
= package
; }
475 const SortedVector
<AaptGroupEntry
>& getGroupEntries() const { return mGroupEntries
; }
477 sp
<AaptFile
> addFile(const String8
& filePath
,
478 const AaptGroupEntry
& entry
,
479 const String8
& srcDir
,
480 sp
<AaptGroup
>* outGroup
,
481 const String8
& resType
);
483 void addResource(const String8
& leafName
,
485 const sp
<AaptFile
>& file
,
486 const String8
& resType
);
488 void addGroupEntry(const AaptGroupEntry
& entry
) { mGroupEntries
.add(entry
); }
490 ssize_t
slurpFromArgs(Bundle
* bundle
);
492 virtual ssize_t
slurpFullTree(Bundle
* bundle
,
493 const String8
& srcDir
,
494 const AaptGroupEntry
& kind
,
495 const String8
& resType
);
497 ssize_t
slurpResourceTree(Bundle
* bundle
, const String8
& srcDir
);
498 ssize_t
slurpResourceZip(Bundle
* bundle
, const char* filename
);
500 sp
<AaptSymbols
> getSymbolsFor(const String8
& name
);
502 const DefaultKeyedVector
<String8
, sp
<AaptSymbols
> >& getSymbols() const { return mSymbols
; }
504 String8
getSymbolsPrivatePackage() const { return mSymbolsPrivatePackage
; }
505 void setSymbolsPrivatePackage(const String8
& pkg
) { mSymbolsPrivatePackage
= pkg
; }
507 status_t
buildIncludedResources(Bundle
* bundle
);
508 status_t
addIncludedResources(const sp
<AaptFile
>& file
);
509 const ResTable
& getIncludedResources() const;
513 inline const Vector
<sp
<AaptDir
> >& resDirs() { return mDirs
; }
514 sp
<AaptDir
> resDir(const String8
& name
);
516 inline sp
<AaptAssets
> getOverlay() { return mOverlay
; }
517 inline void setOverlay(sp
<AaptAssets
>& overlay
) { mOverlay
= overlay
; }
519 inline KeyedVector
<String8
, sp
<ResourceTypeSet
> >* getResources() { return mRes
; }
521 setResources(KeyedVector
<String8
, sp
<ResourceTypeSet
> >* res
) { delete mRes
; mRes
= res
; }
525 SortedVector
<AaptGroupEntry
> mGroupEntries
;
526 DefaultKeyedVector
<String8
, sp
<AaptSymbols
> > mSymbols
;
527 String8 mSymbolsPrivatePackage
;
529 Vector
<sp
<AaptDir
> > mDirs
;
531 bool mHaveIncludedAssets
;
532 AssetManager mIncludedAssets
;
534 sp
<AaptAssets
> mOverlay
;
535 KeyedVector
<String8
, sp
<ResourceTypeSet
> >* mRes
;
538 #endif // __AAPT_ASSETS_H