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>
18 #include <utils/ZipFile.h>
21 #include "SourcePos.h"
23 using namespace android
;
25 bool valid_symbol_name(const String8
& str
);
45 * This structure contains a specific variation of a single file out
46 * of all the variations it can have that we can have.
52 AaptGroupEntry(const String8
& _locale
, const String8
& _vendor
)
53 : locale(_locale
), vendor(_vendor
) { }
69 bool initFromDirName(const char* dir
, String8
* resType
);
71 static status_t
parseNamePart(const String8
& part
, int* axis
, uint32_t* value
);
73 static bool getMccName(const char* name
, ResTable_config
* out
= NULL
);
74 static bool getMncName(const char* name
, ResTable_config
* out
= NULL
);
75 static bool getLocaleName(const char* name
, ResTable_config
* out
= NULL
);
76 static bool getOrientationName(const char* name
, ResTable_config
* out
= NULL
);
77 static bool getDensityName(const char* name
, ResTable_config
* out
= NULL
);
78 static bool getTouchscreenName(const char* name
, ResTable_config
* out
= NULL
);
79 static bool getKeysHiddenName(const char* name
, ResTable_config
* out
= NULL
);
80 static bool getKeyboardName(const char* name
, ResTable_config
* out
= NULL
);
81 static bool getNavigationName(const char* name
, ResTable_config
* out
= NULL
);
82 static bool getScreenSizeName(const char* name
, ResTable_config
* out
= NULL
);
83 static bool getScreenLayoutName(const char* name
, ResTable_config
* out
= NULL
);
84 static bool getVersionName(const char* name
, ResTable_config
* out
= NULL
);
86 int compare(const AaptGroupEntry
& o
) const;
88 ResTable_config
toParams() const;
90 inline bool operator<(const AaptGroupEntry
& o
) const { return compare(o
) < 0; }
91 inline bool operator<=(const AaptGroupEntry
& o
) const { return compare(o
) <= 0; }
92 inline bool operator==(const AaptGroupEntry
& o
) const { return compare(o
) == 0; }
93 inline bool operator!=(const AaptGroupEntry
& o
) const { return compare(o
) != 0; }
94 inline bool operator>=(const AaptGroupEntry
& o
) const { return compare(o
) >= 0; }
95 inline bool operator>(const AaptGroupEntry
& o
) const { return compare(o
) > 0; }
97 String8
toString() const;
98 String8
toDirName(const String8
& resType
) const;
101 inline int compare_type(const AaptGroupEntry
& lhs
, const AaptGroupEntry
& rhs
)
103 return lhs
.compare(rhs
);
106 inline int strictly_order_type(const AaptGroupEntry
& lhs
, const AaptGroupEntry
& rhs
)
108 return compare_type(lhs
, rhs
) < 0;
114 * A single asset file we know about.
116 class AaptFile
: public RefBase
119 AaptFile(const String8
& sourceFile
, const AaptGroupEntry
& groupEntry
,
120 const String8
& resType
)
121 : mGroupEntry(groupEntry
)
122 , mResourceType(resType
)
123 , mSourceFile(sourceFile
)
127 , mCompression(ZipEntry::kCompressStored
)
129 //printf("new AaptFile created %s\n", (const char*)sourceFile);
131 virtual ~AaptFile() { }
133 const String8
& getPath() const { return mPath
; }
134 const AaptGroupEntry
& getGroupEntry() const { return mGroupEntry
; }
136 // Data API. If there is data attached to the file,
137 // getSourceFile() is not used.
138 bool hasData() const { return mData
!= NULL
; }
139 const void* getData() const { return mData
; }
140 size_t getSize() const { return mDataSize
; }
141 void* editData(size_t size
);
142 void* editData(size_t* outSize
= NULL
);
143 void* padData(size_t wordSize
);
144 status_t
writeData(const void* data
, size_t size
);
147 const String8
& getResourceType() const { return mResourceType
; }
149 // File API. If the file does not hold raw data, this is
150 // a full path to a file on the filesystem that holds its data.
151 const String8
& getSourceFile() const { return mSourceFile
; }
153 String8
getPrintableSource() const;
155 // Desired compression method, as per utils/ZipEntry.h. For example,
156 // no compression is ZipEntry::kCompressStored.
157 int getCompressionMethod() const { return mCompression
; }
158 void setCompressionMethod(int c
) { mCompression
= c
; }
160 friend class AaptGroup
;
163 AaptGroupEntry mGroupEntry
;
164 String8 mResourceType
;
173 * A group of related files (the same file, with different
174 * vendor/locale variations).
176 class AaptGroup
: public RefBase
179 AaptGroup(const String8
& leaf
, const String8
& path
)
180 : mLeaf(leaf
), mPath(path
) { }
181 virtual ~AaptGroup() { }
183 const String8
& getLeaf() const { return mLeaf
; }
185 // Returns the relative path after the AaptGroupEntry dirs.
186 const String8
& getPath() const { return mPath
; }
188 const DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> >& getFiles() const
191 status_t
addFile(const sp
<AaptFile
>& file
);
192 void removeFile(size_t index
);
196 String8
getPrintableSource() const;
202 DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> > mFiles
;
206 * A single directory of assets, which can contain for files and other
209 class AaptDir
: public RefBase
212 AaptDir(const String8
& leaf
, const String8
& path
)
213 : mLeaf(leaf
), mPath(path
) { }
214 virtual ~AaptDir() { }
216 const String8
& getLeaf() const { return mLeaf
; }
218 const String8
& getPath() const { return mPath
; }
220 const DefaultKeyedVector
<String8
, sp
<AaptGroup
> >& getFiles() const { return mFiles
; }
221 const DefaultKeyedVector
<String8
, sp
<AaptDir
> >& getDirs() const { return mDirs
; }
223 status_t
addFile(const String8
& name
, const sp
<AaptGroup
>& file
);
224 status_t
addDir(const String8
& name
, const sp
<AaptDir
>& dir
);
226 sp
<AaptDir
> makeDir(const String8
& name
);
228 void removeFile(const String8
& name
);
229 void removeDir(const String8
& name
);
231 status_t
renameFile(const sp
<AaptFile
>& file
, const String8
& newName
);
233 status_t
addLeafFile(const String8
& leafName
,
234 const sp
<AaptFile
>& file
);
236 virtual ssize_t
slurpFullTree(Bundle
* bundle
,
237 const String8
& srcDir
,
238 const AaptGroupEntry
& kind
,
239 const String8
& resType
);
242 * Perform some sanity checks on the names of files and directories here.
244 * - Check for illegal chars in filenames.
245 * - Check filename length.
246 * - Check for presence of ".gz" and non-".gz" copies of same file.
247 * - Check for multiple files whose names match in a case-insensitive
248 * fashion (problematic for some systems).
250 * Comparing names against all other names is O(n^2). We could speed
251 * it up some by sorting the entries and being smarter about what we
252 * compare against, but I'm not expecting to have enough files in a
253 * single directory to make a noticeable difference in speed.
255 * Note that sorting here is not enough to guarantee that the package
256 * contents are sorted -- subsequent updates can rearrange things.
258 status_t
validate() const;
262 String8
getPrintableSource() const;
268 DefaultKeyedVector
<String8
, sp
<AaptGroup
> > mFiles
;
269 DefaultKeyedVector
<String8
, sp
<AaptDir
> > mDirs
;
273 * All information we know about a particular symbol.
275 class AaptSymbolEntry
279 : isPublic(false), typeCode(TYPE_UNKNOWN
)
282 AaptSymbolEntry(const String8
& _name
)
283 : name(_name
), isPublic(false), typeCode(TYPE_UNKNOWN
)
286 AaptSymbolEntry(const AaptSymbolEntry
& o
)
287 : name(o
.name
), sourcePos(o
.sourcePos
), isPublic(o
.isPublic
)
288 , comment(o
.comment
), typeComment(o
.typeComment
)
289 , typeCode(o
.typeCode
), int32Val(o
.int32Val
), stringVal(o
.stringVal
)
292 AaptSymbolEntry
operator=(const AaptSymbolEntry
& o
)
294 sourcePos
= o
.sourcePos
;
295 isPublic
= o
.isPublic
;
297 typeComment
= o
.typeComment
;
298 typeCode
= o
.typeCode
;
299 int32Val
= o
.int32Val
;
300 stringVal
= o
.stringVal
;
310 String16 typeComment
;
320 // Value. May be one of these.
326 * A group of related symbols (such as indices into a string block)
327 * that have been generated from the assets.
329 class AaptSymbols
: public RefBase
333 virtual ~AaptSymbols() { }
335 status_t
addSymbol(const String8
& name
, int32_t value
, const SourcePos
& pos
) {
336 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
339 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
340 sym
.typeCode
= AaptSymbolEntry::TYPE_INT32
;
341 sym
.int32Val
= value
;
345 status_t
addStringSymbol(const String8
& name
, const String8
& value
,
346 const SourcePos
& pos
) {
347 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
350 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
351 sym
.typeCode
= AaptSymbolEntry::TYPE_STRING
;
352 sym
.stringVal
= value
;
356 status_t
makeSymbolPublic(const String8
& name
, const SourcePos
& pos
) {
357 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
360 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
365 void appendComment(const String8
& name
, const String16
& comment
, const SourcePos
& pos
) {
366 if (comment
.size() <= 0) {
369 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
370 if (sym
.comment
.size() == 0) {
371 sym
.comment
= comment
;
373 sym
.comment
.append(String16("\n"));
374 sym
.comment
.append(comment
);
378 void appendTypeComment(const String8
& name
, const String16
& comment
) {
379 if (comment
.size() <= 0) {
382 AaptSymbolEntry
& sym
= edit_symbol(name
, NULL
);
383 if (sym
.typeComment
.size() == 0) {
384 sym
.typeComment
= comment
;
386 sym
.typeComment
.append(String16("\n"));
387 sym
.typeComment
.append(comment
);
391 sp
<AaptSymbols
> addNestedSymbol(const String8
& name
, const SourcePos
& pos
) {
392 if (!check_valid_symbol_name(name
, pos
, "nested symbol")) {
396 sp
<AaptSymbols
> sym
= mNestedSymbols
.valueFor(name
);
398 sym
= new AaptSymbols();
399 mNestedSymbols
.add(name
, sym
);
405 const KeyedVector
<String8
, AaptSymbolEntry
>& getSymbols() const
407 const DefaultKeyedVector
<String8
, sp
<AaptSymbols
> >& getNestedSymbols() const
408 { return mNestedSymbols
; }
410 const String16
& getComment(const String8
& name
) const
411 { return get_symbol(name
).comment
; }
412 const String16
& getTypeComment(const String8
& name
) const
413 { return get_symbol(name
).typeComment
; }
416 bool check_valid_symbol_name(const String8
& symbol
, const SourcePos
& pos
, const char* label
) {
417 if (valid_symbol_name(symbol
)) {
420 pos
.error("invalid %s: '%s'\n", label
, symbol
.string());
423 AaptSymbolEntry
& edit_symbol(const String8
& symbol
, const SourcePos
* pos
) {
424 ssize_t i
= mSymbols
.indexOfKey(symbol
);
426 i
= mSymbols
.add(symbol
, AaptSymbolEntry(symbol
));
428 AaptSymbolEntry
& sym
= mSymbols
.editValueAt(i
);
429 if (pos
!= NULL
&& sym
.sourcePos
.line
< 0) {
430 sym
.sourcePos
= *pos
;
434 const AaptSymbolEntry
& get_symbol(const String8
& symbol
) const {
435 ssize_t i
= mSymbols
.indexOfKey(symbol
);
437 return mSymbols
.valueAt(i
);
442 KeyedVector
<String8
, AaptSymbolEntry
> mSymbols
;
443 DefaultKeyedVector
<String8
, sp
<AaptSymbols
> > mNestedSymbols
;
444 AaptSymbolEntry mDefSymbol
;
447 class ResourceTypeSet
;
450 * Asset hierarchy being operated on.
452 class AaptAssets
: public AaptDir
455 AaptAssets() : AaptDir(String8(), String8()), mHaveIncludedAssets(false) { }
456 virtual ~AaptAssets() { }
458 const String8
& getPackage() const { return mPackage
; }
459 void setPackage(const String8
& package
) { mPackage
= package
; mSymbolsPrivatePackage
= package
; }
461 const SortedVector
<AaptGroupEntry
>& getGroupEntries() const { return mGroupEntries
; }
463 sp
<AaptFile
> addFile(const String8
& filePath
,
464 const AaptGroupEntry
& entry
,
465 const String8
& srcDir
,
466 sp
<AaptGroup
>* outGroup
,
467 const String8
& resType
);
469 void addResource(const String8
& leafName
,
471 const sp
<AaptFile
>& file
,
472 const String8
& resType
);
474 ssize_t
slurpFromArgs(Bundle
* bundle
);
476 virtual ssize_t
slurpFullTree(Bundle
* bundle
,
477 const String8
& srcDir
,
478 const AaptGroupEntry
& kind
,
479 const String8
& resType
);
481 ssize_t
slurpResourceTree(Bundle
* bundle
, const String8
& srcDir
);
482 ssize_t
slurpResourceZip(Bundle
* bundle
, const char* filename
);
484 sp
<AaptSymbols
> getSymbolsFor(const String8
& name
);
486 const DefaultKeyedVector
<String8
, sp
<AaptSymbols
> >& getSymbols() const { return mSymbols
; }
488 String8
getSymbolsPrivatePackage() const { return mSymbolsPrivatePackage
; }
489 void setSymbolsPrivatePackage(const String8
& pkg
) { mSymbolsPrivatePackage
= pkg
; }
491 status_t
buildIncludedResources(Bundle
* bundle
);
492 status_t
addIncludedResources(const sp
<AaptFile
>& file
);
493 const ResTable
& getIncludedResources() const;
497 inline const Vector
<sp
<AaptDir
> >& resDirs() { return mDirs
; }
499 inline sp
<AaptAssets
> getOverlay() { return mOverlay
; }
500 inline void setOverlay(sp
<AaptAssets
>& overlay
) { mOverlay
= overlay
; }
502 inline KeyedVector
<String8
, sp
<ResourceTypeSet
> >* getResources() { return mRes
; }
504 setResources(KeyedVector
<String8
, sp
<ResourceTypeSet
> >* res
) { mRes
= res
; }
508 SortedVector
<AaptGroupEntry
> mGroupEntries
;
509 DefaultKeyedVector
<String8
, sp
<AaptSymbols
> > mSymbols
;
510 String8 mSymbolsPrivatePackage
;
512 Vector
<sp
<AaptDir
> > mDirs
;
514 bool mHaveIncludedAssets
;
515 AssetManager mIncludedAssets
;
517 sp
<AaptAssets
> mOverlay
;
518 KeyedVector
<String8
, sp
<ResourceTypeSet
> >* mRes
;
521 #endif // __AAPT_ASSETS_H