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
);
44 * This structure contains a specific variation of a single file out
45 * of all the variations it can have that we can have.
51 AaptGroupEntry(const String8
& _locale
, const String8
& _vendor
)
52 : locale(_locale
), vendor(_vendor
) { }
67 bool initFromDirName(const char* dir
, String8
* resType
);
69 static status_t
parseNamePart(const String8
& part
, int* axis
, uint32_t* value
);
71 static bool getMccName(const char* name
, ResTable_config
* out
= NULL
);
72 static bool getMncName(const char* name
, ResTable_config
* out
= NULL
);
73 static bool getLocaleName(const char* name
, ResTable_config
* out
= NULL
);
74 static bool getOrientationName(const char* name
, ResTable_config
* out
= NULL
);
75 static bool getDensityName(const char* name
, ResTable_config
* out
= NULL
);
76 static bool getTouchscreenName(const char* name
, ResTable_config
* out
= NULL
);
77 static bool getKeysHiddenName(const char* name
, ResTable_config
* out
= NULL
);
78 static bool getKeyboardName(const char* name
, ResTable_config
* out
= NULL
);
79 static bool getNavigationName(const char* name
, ResTable_config
* out
= NULL
);
80 static bool getScreenSizeName(const char* name
, ResTable_config
* out
= NULL
);
81 static bool getVersionName(const char* name
, ResTable_config
* out
= NULL
);
83 int compare(const AaptGroupEntry
& o
) const;
85 ResTable_config
toParams() const;
87 inline bool operator<(const AaptGroupEntry
& o
) const { return compare(o
) < 0; }
88 inline bool operator<=(const AaptGroupEntry
& o
) const { return compare(o
) <= 0; }
89 inline bool operator==(const AaptGroupEntry
& o
) const { return compare(o
) == 0; }
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; }
94 String8
toString() const;
95 String8
toDirName(const String8
& resType
) const;
98 inline int compare_type(const AaptGroupEntry
& lhs
, const AaptGroupEntry
& rhs
)
100 return lhs
.compare(rhs
);
103 inline int strictly_order_type(const AaptGroupEntry
& lhs
, const AaptGroupEntry
& rhs
)
105 return compare_type(lhs
, rhs
) < 0;
111 * A single asset file we know about.
113 class AaptFile
: public RefBase
116 AaptFile(const String8
& sourceFile
, const AaptGroupEntry
& groupEntry
,
117 const String8
& resType
)
118 : mGroupEntry(groupEntry
)
119 , mResourceType(resType
)
120 , mSourceFile(sourceFile
)
124 , mCompression(ZipEntry::kCompressStored
)
126 //printf("new AaptFile created %s\n", (const char*)sourceFile);
128 virtual ~AaptFile() { }
130 const String8
& getPath() const { return mPath
; }
131 const AaptGroupEntry
& getGroupEntry() const { return mGroupEntry
; }
133 // Data API. If there is data attached to the file,
134 // getSourceFile() is not used.
135 bool hasData() const { return mData
!= NULL
; }
136 const void* getData() const { return mData
; }
137 size_t getSize() const { return mDataSize
; }
138 void* editData(size_t size
);
139 void* editData(size_t* outSize
= NULL
);
140 void* padData(size_t wordSize
);
141 status_t
writeData(const void* data
, size_t size
);
144 const String8
& getResourceType() const { return mResourceType
; }
146 // File API. If the file does not hold raw data, this is
147 // a full path to a file on the filesystem that holds its data.
148 const String8
& getSourceFile() const { return mSourceFile
; }
150 String8
getPrintableSource() const;
152 // Desired compression method, as per utils/ZipEntry.h. For example,
153 // no compression is ZipEntry::kCompressStored.
154 int getCompressionMethod() const { return mCompression
; }
155 void setCompressionMethod(int c
) { mCompression
= c
; }
157 friend class AaptGroup
;
160 AaptGroupEntry mGroupEntry
;
161 String8 mResourceType
;
170 * A group of related files (the same file, with different
171 * vendor/locale variations).
173 class AaptGroup
: public RefBase
176 AaptGroup(const String8
& leaf
, const String8
& path
)
177 : mLeaf(leaf
), mPath(path
) { }
178 virtual ~AaptGroup() { }
180 const String8
& getLeaf() const { return mLeaf
; }
182 // Returns the relative path after the AaptGroupEntry dirs.
183 const String8
& getPath() const { return mPath
; }
185 const DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> >& getFiles() const
188 status_t
addFile(const sp
<AaptFile
>& file
);
189 void removeFile(size_t index
);
193 String8
getPrintableSource() const;
199 DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> > mFiles
;
203 * A single directory of assets, which can contain for files and other
206 class AaptDir
: public RefBase
209 AaptDir(const String8
& leaf
, const String8
& path
)
210 : mLeaf(leaf
), mPath(path
) { }
211 virtual ~AaptDir() { }
213 const String8
& getLeaf() const { return mLeaf
; }
215 const String8
& getPath() const { return mPath
; }
217 const DefaultKeyedVector
<String8
, sp
<AaptGroup
> >& getFiles() const { return mFiles
; }
218 const DefaultKeyedVector
<String8
, sp
<AaptDir
> >& getDirs() const { return mDirs
; }
220 status_t
addFile(const String8
& name
, const sp
<AaptGroup
>& file
);
221 status_t
addDir(const String8
& name
, const sp
<AaptDir
>& dir
);
223 sp
<AaptDir
> makeDir(const String8
& name
);
225 void removeFile(const String8
& name
);
226 void removeDir(const String8
& name
);
228 status_t
renameFile(const sp
<AaptFile
>& file
, const String8
& newName
);
230 status_t
addLeafFile(const String8
& leafName
,
231 const sp
<AaptFile
>& file
);
233 virtual ssize_t
slurpFullTree(Bundle
* bundle
,
234 const String8
& srcDir
,
235 const AaptGroupEntry
& kind
,
236 const String8
& resType
);
239 * Perform some sanity checks on the names of files and directories here.
241 * - Check for illegal chars in filenames.
242 * - Check filename length.
243 * - Check for presence of ".gz" and non-".gz" copies of same file.
244 * - Check for multiple files whose names match in a case-insensitive
245 * fashion (problematic for some systems).
247 * Comparing names against all other names is O(n^2). We could speed
248 * it up some by sorting the entries and being smarter about what we
249 * compare against, but I'm not expecting to have enough files in a
250 * single directory to make a noticeable difference in speed.
252 * Note that sorting here is not enough to guarantee that the package
253 * contents are sorted -- subsequent updates can rearrange things.
255 status_t
validate() const;
259 String8
getPrintableSource() const;
265 DefaultKeyedVector
<String8
, sp
<AaptGroup
> > mFiles
;
266 DefaultKeyedVector
<String8
, sp
<AaptDir
> > mDirs
;
270 * All information we know about a particular symbol.
272 class AaptSymbolEntry
276 : isPublic(false), typeCode(TYPE_UNKNOWN
)
279 AaptSymbolEntry(const String8
& _name
)
280 : name(_name
), isPublic(false), typeCode(TYPE_UNKNOWN
)
283 AaptSymbolEntry(const AaptSymbolEntry
& o
)
284 : name(o
.name
), sourcePos(o
.sourcePos
), isPublic(o
.isPublic
)
285 , comment(o
.comment
), typeComment(o
.typeComment
)
286 , typeCode(o
.typeCode
), int32Val(o
.int32Val
), stringVal(o
.stringVal
)
289 AaptSymbolEntry
operator=(const AaptSymbolEntry
& o
)
291 sourcePos
= o
.sourcePos
;
292 isPublic
= o
.isPublic
;
294 typeComment
= o
.typeComment
;
295 typeCode
= o
.typeCode
;
296 int32Val
= o
.int32Val
;
297 stringVal
= o
.stringVal
;
307 String16 typeComment
;
317 // Value. May be one of these.
323 * A group of related symbols (such as indices into a string block)
324 * that have been generated from the assets.
326 class AaptSymbols
: public RefBase
330 virtual ~AaptSymbols() { }
332 status_t
addSymbol(const String8
& name
, int32_t value
, const SourcePos
& pos
) {
333 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
336 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
337 sym
.typeCode
= AaptSymbolEntry::TYPE_INT32
;
338 sym
.int32Val
= value
;
342 status_t
addStringSymbol(const String8
& name
, const String8
& value
,
343 const SourcePos
& pos
) {
344 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
347 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
348 sym
.typeCode
= AaptSymbolEntry::TYPE_STRING
;
349 sym
.stringVal
= value
;
353 status_t
makeSymbolPublic(const String8
& name
, const SourcePos
& pos
) {
354 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
357 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
362 void appendComment(const String8
& name
, const String16
& comment
, const SourcePos
& pos
) {
363 if (comment
.size() <= 0) {
366 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
367 if (sym
.comment
.size() == 0) {
368 sym
.comment
= comment
;
370 sym
.comment
.append(String16("\n"));
371 sym
.comment
.append(comment
);
375 void appendTypeComment(const String8
& name
, const String16
& comment
) {
376 if (comment
.size() <= 0) {
379 AaptSymbolEntry
& sym
= edit_symbol(name
, NULL
);
380 if (sym
.typeComment
.size() == 0) {
381 sym
.typeComment
= comment
;
383 sym
.typeComment
.append(String16("\n"));
384 sym
.typeComment
.append(comment
);
388 sp
<AaptSymbols
> addNestedSymbol(const String8
& name
, const SourcePos
& pos
) {
389 if (!check_valid_symbol_name(name
, pos
, "nested symbol")) {
393 sp
<AaptSymbols
> sym
= mNestedSymbols
.valueFor(name
);
395 sym
= new AaptSymbols();
396 mNestedSymbols
.add(name
, sym
);
402 const KeyedVector
<String8
, AaptSymbolEntry
>& getSymbols() const
404 const DefaultKeyedVector
<String8
, sp
<AaptSymbols
> >& getNestedSymbols() const
405 { return mNestedSymbols
; }
407 const String16
& getComment(const String8
& name
) const
408 { return get_symbol(name
).comment
; }
409 const String16
& getTypeComment(const String8
& name
) const
410 { return get_symbol(name
).typeComment
; }
413 bool check_valid_symbol_name(const String8
& symbol
, const SourcePos
& pos
, const char* label
) {
414 if (valid_symbol_name(symbol
)) {
417 pos
.error("invalid %s: '%s'\n", label
, symbol
.string());
420 AaptSymbolEntry
& edit_symbol(const String8
& symbol
, const SourcePos
* pos
) {
421 ssize_t i
= mSymbols
.indexOfKey(symbol
);
423 i
= mSymbols
.add(symbol
, AaptSymbolEntry(symbol
));
425 AaptSymbolEntry
& sym
= mSymbols
.editValueAt(i
);
426 if (pos
!= NULL
&& sym
.sourcePos
.line
< 0) {
427 sym
.sourcePos
= *pos
;
431 const AaptSymbolEntry
& get_symbol(const String8
& symbol
) const {
432 ssize_t i
= mSymbols
.indexOfKey(symbol
);
434 return mSymbols
.valueAt(i
);
439 KeyedVector
<String8
, AaptSymbolEntry
> mSymbols
;
440 DefaultKeyedVector
<String8
, sp
<AaptSymbols
> > mNestedSymbols
;
441 AaptSymbolEntry mDefSymbol
;
445 * Asset hierarchy being operated on.
447 class AaptAssets
: public AaptDir
450 AaptAssets() : AaptDir(String8(), String8()), mHaveIncludedAssets(false) { }
451 virtual ~AaptAssets() { }
453 const String8
& getPackage() const { return mPackage
; }
454 void setPackage(const String8
& package
) { mPackage
= package
; mSymbolsPrivatePackage
= package
; }
456 const SortedVector
<AaptGroupEntry
>& getGroupEntries() const { return mGroupEntries
; }
458 sp
<AaptFile
> addFile(const String8
& filePath
,
459 const AaptGroupEntry
& entry
,
460 const String8
& srcDir
,
461 sp
<AaptGroup
>* outGroup
,
462 const String8
& resType
);
464 void addResource(const String8
& leafName
,
466 const sp
<AaptFile
>& file
,
467 const String8
& resType
);
469 ssize_t
slurpFromArgs(Bundle
* bundle
);
471 virtual ssize_t
slurpFullTree(Bundle
* bundle
,
472 const String8
& srcDir
,
473 const AaptGroupEntry
& kind
,
474 const String8
& resType
);
476 ssize_t
slurpResourceTree(Bundle
* bundle
, const String8
& srcDir
);
477 ssize_t
slurpResourceZip(Bundle
* bundle
, const char* filename
);
479 sp
<AaptSymbols
> getSymbolsFor(const String8
& name
);
481 const DefaultKeyedVector
<String8
, sp
<AaptSymbols
> >& getSymbols() const { return mSymbols
; }
483 String8
getSymbolsPrivatePackage() const { return mSymbolsPrivatePackage
; }
484 void setSymbolsPrivatePackage(const String8
& pkg
) { mSymbolsPrivatePackage
= pkg
; }
486 status_t
buildIncludedResources(Bundle
* bundle
);
487 status_t
addIncludedResources(const sp
<AaptFile
>& file
);
488 const ResTable
& getIncludedResources() const;
492 inline const Vector
<sp
<AaptDir
> >& resDirs() { return mDirs
; }
496 SortedVector
<AaptGroupEntry
> mGroupEntries
;
497 DefaultKeyedVector
<String8
, sp
<AaptSymbols
> > mSymbols
;
498 String8 mSymbolsPrivatePackage
;
500 Vector
<sp
<AaptDir
> > mDirs
;
502 bool mHaveIncludedAssets
;
503 AssetManager mIncludedAssets
;
506 #endif // __AAPT_ASSETS_H