This repository has been archived by the owner on Dec 15, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
ResourceTable.cpp
5218 lines (4670 loc) · 203 KB
/
ResourceTable.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Copyright 2006 The Android Open Source Project
//
// Build resource files from raw assets.
//
#include "ResourceTable.h"
#include "AoptUtil.h"
#include "XMLNode.h"
#include "ResourceFilter.h"
#include "ResourceIdCache.h"
#include "SdkConstants.h"
#include <algorithm>
#include <androidfw/ResourceTypes.h>
#include <utils/ByteOrder.h>
#include <utils/TypeHelpers.h>
#include <stdarg.h>
// SSIZE: mingw does not have signed size_t == ssize_t.
// STATUST: mingw does seem to redefine UNKNOWN_ERROR from our enum value, so a cast is necessary.
#if !defined(_WIN32)
# define SSIZE(x) x
# define STATUST(x) x
#else
# define SSIZE(x) (signed size_t)x
# define STATUST(x) (status_t)x
#endif
// Set to true for noisy debug output.
static const bool kIsDebug = false;
#if PRINT_STRING_METRICS
static const bool kPrintStringMetrics = true;
#else
static const bool kPrintStringMetrics = false;
#endif
static const char* kAttrPrivateType = "^attr-private";
status_t compileXmlFile(const Bundle* bundle,
const sp<AoptAssets>& assets,
const String16& resourceName,
const sp<AoptFile>& target,
ResourceTable* table,
int options)
{
sp<XMLNode> root = XMLNode::parse(target);
if (root == NULL) {
return UNKNOWN_ERROR;
}
return compileXmlFile(bundle, assets, resourceName, root, target, table, options);
}
status_t compileXmlFile(const Bundle* bundle,
const sp<AoptAssets>& assets,
const String16& resourceName,
const sp<AoptFile>& target,
const sp<AoptFile>& outTarget,
ResourceTable* table,
int options)
{
sp<XMLNode> root = XMLNode::parse(target);
if (root == NULL) {
return UNKNOWN_ERROR;
}
return compileXmlFile(bundle, assets, resourceName, root, outTarget, table, options);
}
status_t compileXmlFile(const Bundle* bundle,
const sp<AoptAssets>& assets,
const String16& resourceName,
const sp<XMLNode>& root,
const sp<AoptFile>& target,
ResourceTable* table,
int options)
{
if ((options&XML_COMPILE_STRIP_WHITESPACE) != 0) {
root->removeWhitespace(true, NULL);
} else if ((options&XML_COMPILE_COMPACT_WHITESPACE) != 0) {
root->removeWhitespace(false, NULL);
}
if ((options&XML_COMPILE_UTF8) != 0) {
root->setUTF8(true);
}
if (table->processBundleFormat(bundle, resourceName, target, root) != NO_ERROR) {
return UNKNOWN_ERROR;
}
bool hasErrors = false;
if ((options&XML_COMPILE_ASSIGN_ATTRIBUTE_IDS) != 0) {
status_t err = root->assignResourceIds(assets, table);
if (err != NO_ERROR) {
hasErrors = true;
}
}
if ((options&XML_COMPILE_PARSE_VALUES) != 0) {
status_t err = root->parseValues(assets, table);
if (err != NO_ERROR) {
hasErrors = true;
}
}
if (hasErrors) {
return UNKNOWN_ERROR;
}
if (table->modifyForCompat(bundle, resourceName, target, root) != NO_ERROR) {
return UNKNOWN_ERROR;
}
if (kIsDebug) {
printf("Input XML Resource:\n");
root->print();
}
status_t err = root->flatten(target,
(options&XML_COMPILE_STRIP_COMMENTS) != 0,
(options&XML_COMPILE_STRIP_RAW_VALUES) != 0);
if (err != NO_ERROR) {
return err;
}
if (kIsDebug) {
printf("Output XML Resource:\n");
ResXMLTree tree;
tree.setTo(target->getData(), target->getSize());
printXMLBlock(&tree);
}
target->setCompressionMethod(ZipEntry::kCompressDeflated);
return err;
}
struct flag_entry
{
const char16_t* name;
size_t nameLen;
uint32_t value;
const char* description;
};
static const char16_t referenceArray[] =
{ 'r', 'e', 'f', 'e', 'r', 'e', 'n', 'c', 'e' };
static const char16_t stringArray[] =
{ 's', 't', 'r', 'i', 'n', 'g' };
static const char16_t integerArray[] =
{ 'i', 'n', 't', 'e', 'g', 'e', 'r' };
static const char16_t booleanArray[] =
{ 'b', 'o', 'o', 'l', 'e', 'a', 'n' };
static const char16_t colorArray[] =
{ 'c', 'o', 'l', 'o', 'r' };
static const char16_t floatArray[] =
{ 'f', 'l', 'o', 'a', 't' };
static const char16_t dimensionArray[] =
{ 'd', 'i', 'm', 'e', 'n', 's', 'i', 'o', 'n' };
static const char16_t fractionArray[] =
{ 'f', 'r', 'a', 'c', 't', 'i', 'o', 'n' };
static const char16_t enumArray[] =
{ 'e', 'n', 'u', 'm' };
static const char16_t flagsArray[] =
{ 'f', 'l', 'a', 'g', 's' };
static const flag_entry gFormatFlags[] = {
{ referenceArray, sizeof(referenceArray)/2, ResTable_map::TYPE_REFERENCE,
"a reference to another resource, in the form \"<code>@[+][<i>package</i>:]<i>type</i>:<i>name</i></code>\"\n"
"or to a theme attribute in the form \"<code>?[<i>package</i>:][<i>type</i>:]<i>name</i></code>\"."},
{ stringArray, sizeof(stringArray)/2, ResTable_map::TYPE_STRING,
"a string value, using '\\\\;' to escape characters such as '\\\\n' or '\\\\uxxxx' for a unicode character." },
{ integerArray, sizeof(integerArray)/2, ResTable_map::TYPE_INTEGER,
"an integer value, such as \"<code>100</code>\"." },
{ booleanArray, sizeof(booleanArray)/2, ResTable_map::TYPE_BOOLEAN,
"a boolean value, either \"<code>true</code>\" or \"<code>false</code>\"." },
{ colorArray, sizeof(colorArray)/2, ResTable_map::TYPE_COLOR,
"a color value, in the form of \"<code>#<i>rgb</i></code>\", \"<code>#<i>argb</i></code>\",\n"
"\"<code>#<i>rrggbb</i></code>\", or \"<code>#<i>aarrggbb</i></code>\"." },
{ floatArray, sizeof(floatArray)/2, ResTable_map::TYPE_FLOAT,
"a floating point value, such as \"<code>1.2</code>\"."},
{ dimensionArray, sizeof(dimensionArray)/2, ResTable_map::TYPE_DIMENSION,
"a dimension value, which is a floating point number appended with a unit such as \"<code>14.5sp</code>\".\n"
"Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size),\n"
"in (inches), mm (millimeters)." },
{ fractionArray, sizeof(fractionArray)/2, ResTable_map::TYPE_FRACTION,
"a fractional value, which is a floating point number appended with either % or %p, such as \"<code>14.5%</code>\".\n"
"The % suffix always means a percentage of the base size; the optional %p suffix provides a size relative to\n"
"some parent container." },
{ enumArray, sizeof(enumArray)/2, ResTable_map::TYPE_ENUM, NULL },
{ flagsArray, sizeof(flagsArray)/2, ResTable_map::TYPE_FLAGS, NULL },
{ NULL, 0, 0, NULL }
};
static const char16_t suggestedArray[] = { 's', 'u', 'g', 'g', 'e', 's', 't', 'e', 'd' };
static const flag_entry l10nRequiredFlags[] = {
{ suggestedArray, sizeof(suggestedArray)/2, ResTable_map::L10N_SUGGESTED, NULL },
{ NULL, 0, 0, NULL }
};
static const char16_t nulStr[] = { 0 };
static uint32_t parse_flags(const char16_t* str, size_t len,
const flag_entry* flags, bool* outError = NULL)
{
while (len > 0 && isspace(*str)) {
str++;
len--;
}
while (len > 0 && isspace(str[len-1])) {
len--;
}
const char16_t* const end = str + len;
uint32_t value = 0;
while (str < end) {
const char16_t* div = str;
while (div < end && *div != '|') {
div++;
}
const flag_entry* cur = flags;
while (cur->name) {
if (strzcmp16(cur->name, cur->nameLen, str, div-str) == 0) {
value |= cur->value;
break;
}
cur++;
}
if (!cur->name) {
if (outError) *outError = true;
return 0;
}
str = div < end ? div+1 : div;
}
if (outError) *outError = false;
return value;
}
static String16 mayOrMust(int type, int flags)
{
if ((type&(~flags)) == 0) {
return String16("<p>Must");
}
return String16("<p>May");
}
static void appendTypeInfo(ResourceTable* outTable, const String16& pkg,
const String16& typeName, const String16& ident, int type,
const flag_entry* flags)
{
bool hadType = false;
while (flags->name) {
if ((type&flags->value) != 0 && flags->description != NULL) {
String16 fullMsg(mayOrMust(type, flags->value));
fullMsg.append(String16(" be "));
fullMsg.append(String16(flags->description));
outTable->appendTypeComment(pkg, typeName, ident, fullMsg);
hadType = true;
}
flags++;
}
if (hadType && (type&ResTable_map::TYPE_REFERENCE) == 0) {
outTable->appendTypeComment(pkg, typeName, ident,
String16("<p>This may also be a reference to a resource (in the form\n"
"\"<code>@[<i>package</i>:]<i>type</i>:<i>name</i></code>\") or\n"
"theme attribute (in the form\n"
"\"<code>?[<i>package</i>:][<i>type</i>:]<i>name</i></code>\")\n"
"containing a value of this type."));
}
}
struct PendingAttribute
{
const String16 myPackage;
const SourcePos sourcePos;
const bool appendComment;
int32_t type;
String16 ident;
String16 comment;
bool hasErrors;
bool added;
PendingAttribute(String16 _package, const sp<AoptFile>& in,
ResXMLTree& block, bool _appendComment)
: myPackage(_package)
, sourcePos(in->getPrintableSource(), block.getLineNumber())
, appendComment(_appendComment)
, type(ResTable_map::TYPE_ANY)
, hasErrors(false)
, added(false)
{
}
status_t createIfNeeded(ResourceTable* outTable)
{
if (added || hasErrors) {
return NO_ERROR;
}
added = true;
if (!outTable->makeAttribute(myPackage, ident, sourcePos, type, comment, appendComment)) {
hasErrors = true;
return UNKNOWN_ERROR;
}
return NO_ERROR;
}
};
static status_t compileAttribute(const sp<AoptFile>& in,
ResXMLTree& block,
const String16& myPackage,
ResourceTable* outTable,
String16* outIdent = NULL,
bool inStyleable = false)
{
PendingAttribute attr(myPackage, in, block, inStyleable);
const String16 attr16("attr");
const String16 id16("id");
// Attribute type constants.
const String16 enum16("enum");
const String16 flag16("flag");
ResXMLTree::event_code_t code;
size_t len;
status_t err;
ssize_t identIdx = block.indexOfAttribute(NULL, "name");
if (identIdx >= 0) {
attr.ident = String16(block.getAttributeStringValue(identIdx, &len));
if (outIdent) {
*outIdent = attr.ident;
}
} else {
attr.sourcePos.error("A 'name' attribute is required for <attr>\n");
attr.hasErrors = true;
}
attr.comment = String16(
block.getComment(&len) ? block.getComment(&len) : nulStr);
ssize_t typeIdx = block.indexOfAttribute(NULL, "format");
if (typeIdx >= 0) {
String16 typeStr = String16(block.getAttributeStringValue(typeIdx, &len));
attr.type = parse_flags(typeStr.string(), typeStr.size(), gFormatFlags);
if (attr.type == 0) {
attr.sourcePos.error("Tag <attr> 'format' attribute value \"%s\" not valid\n",
String8(typeStr).string());
attr.hasErrors = true;
}
attr.createIfNeeded(outTable);
} else if (!inStyleable) {
// Attribute definitions outside of styleables always define the
// attribute as a generic value.
attr.createIfNeeded(outTable);
}
//printf("Attribute %s: type=0x%08x\n", String8(attr.ident).string(), attr.type);
ssize_t minIdx = block.indexOfAttribute(NULL, "min");
if (minIdx >= 0) {
String16 val = String16(block.getAttributeStringValue(minIdx, &len));
if (!ResTable::stringToInt(val.string(), val.size(), NULL)) {
attr.sourcePos.error("Tag <attr> 'min' attribute must be a number, not \"%s\"\n",
String8(val).string());
attr.hasErrors = true;
}
attr.createIfNeeded(outTable);
if (!attr.hasErrors) {
err = outTable->addBag(attr.sourcePos, myPackage, attr16, attr.ident,
String16(""), String16("^min"), String16(val), NULL, NULL);
if (err != NO_ERROR) {
attr.hasErrors = true;
}
}
}
ssize_t maxIdx = block.indexOfAttribute(NULL, "max");
if (maxIdx >= 0) {
String16 val = String16(block.getAttributeStringValue(maxIdx, &len));
if (!ResTable::stringToInt(val.string(), val.size(), NULL)) {
attr.sourcePos.error("Tag <attr> 'max' attribute must be a number, not \"%s\"\n",
String8(val).string());
attr.hasErrors = true;
}
attr.createIfNeeded(outTable);
if (!attr.hasErrors) {
err = outTable->addBag(attr.sourcePos, myPackage, attr16, attr.ident,
String16(""), String16("^max"), String16(val), NULL, NULL);
attr.hasErrors = true;
}
}
if ((minIdx >= 0 || maxIdx >= 0) && (attr.type&ResTable_map::TYPE_INTEGER) == 0) {
attr.sourcePos.error("Tag <attr> must have format=integer attribute if using max or min\n");
attr.hasErrors = true;
}
ssize_t l10nIdx = block.indexOfAttribute(NULL, "localization");
if (l10nIdx >= 0) {
const char16_t* str = block.getAttributeStringValue(l10nIdx, &len);
bool error;
uint32_t l10n_required = parse_flags(str, len, l10nRequiredFlags, &error);
if (error) {
attr.sourcePos.error("Tag <attr> 'localization' attribute value \"%s\" not valid\n",
String8(str).string());
attr.hasErrors = true;
}
attr.createIfNeeded(outTable);
if (!attr.hasErrors) {
char buf[11];
sprintf(buf, "%d", l10n_required);
err = outTable->addBag(attr.sourcePos, myPackage, attr16, attr.ident,
String16(""), String16("^l10n"), String16(buf), NULL, NULL);
if (err != NO_ERROR) {
attr.hasErrors = true;
}
}
}
String16 enumOrFlagsComment;
while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) {
if (code == ResXMLTree::START_TAG) {
uint32_t localType = 0;
if (strcmp16(block.getElementName(&len), enum16.string()) == 0) {
localType = ResTable_map::TYPE_ENUM;
} else if (strcmp16(block.getElementName(&len), flag16.string()) == 0) {
localType = ResTable_map::TYPE_FLAGS;
} else {
SourcePos(in->getPrintableSource(), block.getLineNumber())
.error("Tag <%s> can not appear inside <attr>, only <enum> or <flag>\n",
String8(block.getElementName(&len)).string());
return UNKNOWN_ERROR;
}
attr.createIfNeeded(outTable);
if (attr.type == ResTable_map::TYPE_ANY) {
// No type was explicitly stated, so supplying enum tags
// implicitly creates an enum or flag.
attr.type = 0;
}
if ((attr.type&(ResTable_map::TYPE_ENUM|ResTable_map::TYPE_FLAGS)) == 0) {
// Wasn't originally specified as an enum, so update its type.
attr.type |= localType;
if (!attr.hasErrors) {
char numberStr[16];
sprintf(numberStr, "%d", attr.type);
err = outTable->addBag(SourcePos(in->getPrintableSource(), block.getLineNumber()),
myPackage, attr16, attr.ident, String16(""),
String16("^type"), String16(numberStr), NULL, NULL, true);
if (err != NO_ERROR) {
attr.hasErrors = true;
}
}
} else if ((uint32_t)(attr.type&(ResTable_map::TYPE_ENUM|ResTable_map::TYPE_FLAGS)) != localType) {
if (localType == ResTable_map::TYPE_ENUM) {
SourcePos(in->getPrintableSource(), block.getLineNumber())
.error("<enum> attribute can not be used inside a flags format\n");
attr.hasErrors = true;
} else {
SourcePos(in->getPrintableSource(), block.getLineNumber())
.error("<flag> attribute can not be used inside a enum format\n");
attr.hasErrors = true;
}
}
String16 itemIdent;
ssize_t itemIdentIdx = block.indexOfAttribute(NULL, "name");
if (itemIdentIdx >= 0) {
itemIdent = String16(block.getAttributeStringValue(itemIdentIdx, &len));
} else {
SourcePos(in->getPrintableSource(), block.getLineNumber())
.error("A 'name' attribute is required for <enum> or <flag>\n");
attr.hasErrors = true;
}
String16 value;
ssize_t valueIdx = block.indexOfAttribute(NULL, "value");
if (valueIdx >= 0) {
value = String16(block.getAttributeStringValue(valueIdx, &len));
} else {
SourcePos(in->getPrintableSource(), block.getLineNumber())
.error("A 'value' attribute is required for <enum> or <flag>\n");
attr.hasErrors = true;
}
if (!attr.hasErrors && !ResTable::stringToInt(value.string(), value.size(), NULL)) {
SourcePos(in->getPrintableSource(), block.getLineNumber())
.error("Tag <enum> or <flag> 'value' attribute must be a number,"
" not \"%s\"\n",
String8(value).string());
attr.hasErrors = true;
}
if (!attr.hasErrors) {
if (enumOrFlagsComment.size() == 0) {
enumOrFlagsComment.append(mayOrMust(attr.type,
ResTable_map::TYPE_ENUM|ResTable_map::TYPE_FLAGS));
enumOrFlagsComment.append((attr.type&ResTable_map::TYPE_ENUM)
? String16(" be one of the following constant values.")
: String16(" be one or more (separated by '|') of the following constant values."));
enumOrFlagsComment.append(String16("</p>\n<table>\n"
"<colgroup align=\"left\" />\n"
"<colgroup align=\"left\" />\n"
"<colgroup align=\"left\" />\n"
"<tr><th>Constant</th><th>Value</th><th>Description</th></tr>"));
}
enumOrFlagsComment.append(String16("\n<tr><td><code>"));
enumOrFlagsComment.append(itemIdent);
enumOrFlagsComment.append(String16("</code></td><td>"));
enumOrFlagsComment.append(value);
enumOrFlagsComment.append(String16("</td><td>"));
if (block.getComment(&len)) {
enumOrFlagsComment.append(String16(block.getComment(&len)));
}
enumOrFlagsComment.append(String16("</td></tr>"));
err = outTable->addBag(SourcePos(in->getPrintableSource(), block.getLineNumber()),
myPackage,
attr16, attr.ident, String16(""),
itemIdent, value, NULL, NULL, false, true);
if (err != NO_ERROR) {
attr.hasErrors = true;
}
}
} else if (code == ResXMLTree::END_TAG) {
if (strcmp16(block.getElementName(&len), attr16.string()) == 0) {
break;
}
if ((attr.type&ResTable_map::TYPE_ENUM) != 0) {
if (strcmp16(block.getElementName(&len), enum16.string()) != 0) {
SourcePos(in->getPrintableSource(), block.getLineNumber())
.error("Found tag </%s> where </enum> is expected\n",
String8(block.getElementName(&len)).string());
return UNKNOWN_ERROR;
}
} else {
if (strcmp16(block.getElementName(&len), flag16.string()) != 0) {
SourcePos(in->getPrintableSource(), block.getLineNumber())
.error("Found tag </%s> where </flag> is expected\n",
String8(block.getElementName(&len)).string());
return UNKNOWN_ERROR;
}
}
}
}
if (!attr.hasErrors && attr.added) {
appendTypeInfo(outTable, myPackage, attr16, attr.ident, attr.type, gFormatFlags);
}
if (!attr.hasErrors && enumOrFlagsComment.size() > 0) {
enumOrFlagsComment.append(String16("\n</table>"));
outTable->appendTypeComment(myPackage, attr16, attr.ident, enumOrFlagsComment);
}
return NO_ERROR;
}
bool localeIsDefined(const ResTable_config& config)
{
return config.locale == 0;
}
status_t parseAndAddBag(Bundle* bundle,
const sp<AoptFile>& in,
ResXMLTree* block,
const ResTable_config& config,
const String16& myPackage,
const String16& curType,
const String16& ident,
const String16& parentIdent,
const String16& itemIdent,
int32_t curFormat,
bool isFormatted,
const String16& /* product */,
PseudolocalizationMethod pseudolocalize,
const bool overwrite,
ResourceTable* outTable)
{
status_t err;
const String16 item16("item");
String16 str;
Vector<StringPool::entry_style_span> spans;
err = parseStyledString(bundle, in->getPrintableSource().string(),
block, item16, &str, &spans, isFormatted,
pseudolocalize);
if (err != NO_ERROR) {
return err;
}
if (kIsDebug) {
printf("Adding resource bag entry l=%c%c c=%c%c orien=%d d=%d "
" pid=%s, bag=%s, id=%s: %s\n",
config.language[0], config.language[1],
config.country[0], config.country[1],
config.orientation, config.density,
String8(parentIdent).string(),
String8(ident).string(),
String8(itemIdent).string(),
String8(str).string());
}
err = outTable->addBag(SourcePos(in->getPrintableSource(), block->getLineNumber()),
myPackage, curType, ident, parentIdent, itemIdent, str,
&spans, &config, overwrite, false, curFormat);
return err;
}
/*
* Returns true if needle is one of the elements in the comma-separated list
* haystack, false otherwise.
*/
bool isInProductList(const String16& needle, const String16& haystack) {
const char16_t *needle2 = needle.string();
const char16_t *haystack2 = haystack.string();
size_t needlesize = needle.size();
while (*haystack2 != '\0') {
if (strncmp16(haystack2, needle2, needlesize) == 0) {
if (haystack2[needlesize] == '\0' || haystack2[needlesize] == ',') {
return true;
}
}
while (*haystack2 != '\0' && *haystack2 != ',') {
haystack2++;
}
if (*haystack2 == ',') {
haystack2++;
}
}
return false;
}
/*
* A simple container that holds a resource type and name. It is ordered first by type then
* by name.
*/
struct type_ident_pair_t {
String16 type;
String16 ident;
type_ident_pair_t() { };
type_ident_pair_t(const String16& t, const String16& i) : type(t), ident(i) { }
type_ident_pair_t(const type_ident_pair_t& o) : type(o.type), ident(o.ident) { }
inline bool operator < (const type_ident_pair_t& o) const {
int cmp = compare_type(type, o.type);
if (cmp < 0) {
return true;
} else if (cmp > 0) {
return false;
} else {
return strictly_order_type(ident, o.ident);
}
}
};
status_t parseAndAddEntry(Bundle* bundle,
const sp<AoptFile>& in,
ResXMLTree* block,
const ResTable_config& config,
const String16& myPackage,
const String16& curType,
const String16& ident,
const String16& curTag,
bool curIsStyled,
int32_t curFormat,
bool isFormatted,
const String16& product,
PseudolocalizationMethod pseudolocalize,
const bool overwrite,
KeyedVector<type_ident_pair_t, bool>* skippedResourceNames,
ResourceTable* outTable)
{
status_t err;
String16 str;
Vector<StringPool::entry_style_span> spans;
err = parseStyledString(bundle, in->getPrintableSource().string(), block,
curTag, &str, curIsStyled ? &spans : NULL,
isFormatted, pseudolocalize);
if (err < NO_ERROR) {
return err;
}
/*
* If a product type was specified on the command line
* and also in the string, and the two are not the same,
* return without adding the string.
*/
const char *bundleProduct = bundle->getProduct();
if (bundleProduct == NULL) {
bundleProduct = "";
}
if (product.size() != 0) {
/*
* If the command-line-specified product is empty, only "default"
* matches. Other variants are skipped. This is so generation
* of the R.java file when the product is not known is predictable.
*/
if (bundleProduct[0] == '\0') {
if (strcmp16(String16("default").string(), product.string()) != 0) {
/*
* This string has a product other than 'default'. Do not add it,
* but record it so that if we do not see the same string with
* product 'default' or no product, then report an error.
*/
skippedResourceNames->replaceValueFor(
type_ident_pair_t(curType, ident), true);
return NO_ERROR;
}
} else {
/*
* The command-line product is not empty.
* If the product for this string is on the command-line list,
* it matches. "default" also matches, but only if nothing
* else has matched already.
*/
if (isInProductList(product, String16(bundleProduct))) {
;
} else if (strcmp16(String16("default").string(), product.string()) == 0 &&
!outTable->hasBagOrEntry(myPackage, curType, ident, config)) {
;
} else {
return NO_ERROR;
}
}
}
if (kIsDebug) {
printf("Adding resource entry l=%c%c c=%c%c orien=%d d=%d id=%s: %s\n",
config.language[0], config.language[1],
config.country[0], config.country[1],
config.orientation, config.density,
String8(ident).string(), String8(str).string());
}
err = outTable->addEntry(SourcePos(in->getPrintableSource(), block->getLineNumber()),
myPackage, curType, ident, str, &spans, &config,
false, curFormat, overwrite);
return err;
}
status_t compileResourceFile(Bundle* bundle,
const sp<AoptAssets>& assets,
const sp<AoptFile>& in,
const ResTable_config& defParams,
const bool overwrite,
ResourceTable* outTable)
{
ResXMLTree block;
status_t err = parseXMLResource(in, &block, false, true);
if (err != NO_ERROR) {
return err;
}
// Top-level tag.
const String16 resources16("resources");
// Identifier declaration tags.
const String16 declare_styleable16("declare-styleable");
const String16 attr16("attr");
// Data creation organizational tags.
const String16 string16("string");
const String16 drawable16("drawable");
const String16 color16("color");
const String16 bool16("bool");
const String16 integer16("integer");
const String16 dimen16("dimen");
const String16 fraction16("fraction");
const String16 style16("style");
const String16 plurals16("plurals");
const String16 array16("array");
const String16 string_array16("string-array");
const String16 integer_array16("integer-array");
const String16 public16("public");
const String16 overlay16("overlay");
const String16 public_padding16("public-padding");
const String16 private_symbols16("private-symbols");
const String16 java_symbol16("java-symbol");
const String16 add_resource16("add-resource");
const String16 skip16("skip");
const String16 eat_comment16("eat-comment");
// Data creation tags.
const String16 bag16("bag");
const String16 item16("item");
// Attribute type constants.
const String16 enum16("enum");
// plural values
const String16 other16("other");
const String16 quantityOther16("^other");
const String16 zero16("zero");
const String16 quantityZero16("^zero");
const String16 one16("one");
const String16 quantityOne16("^one");
const String16 two16("two");
const String16 quantityTwo16("^two");
const String16 few16("few");
const String16 quantityFew16("^few");
const String16 many16("many");
const String16 quantityMany16("^many");
// useful attribute names and special values
const String16 name16("name");
const String16 translatable16("translatable");
const String16 formatted16("formatted");
const String16 false16("false");
const String16 myPackage(assets->getPackage());
bool hasErrors = false;
bool fileIsTranslatable = true;
if (strstr(in->getPrintableSource().string(), "donottranslate") != NULL) {
fileIsTranslatable = false;
}
DefaultKeyedVector<String16, uint32_t> nextPublicId(0);
// Stores the resource names that were skipped. Typically this happens when
// AOPT is invoked without a product specified and a resource has no
// 'default' product attribute.
KeyedVector<type_ident_pair_t, bool> skippedResourceNames;
ResXMLTree::event_code_t code;
do {
code = block.next();
} while (code == ResXMLTree::START_NAMESPACE);
size_t len;
if (code != ResXMLTree::START_TAG) {
SourcePos(in->getPrintableSource(), block.getLineNumber()).error(
"No start tag found\n");
return UNKNOWN_ERROR;
}
if (strcmp16(block.getElementName(&len), resources16.string()) != 0) {
SourcePos(in->getPrintableSource(), block.getLineNumber()).error(
"Invalid start tag %s\n", String8(block.getElementName(&len)).string());
return UNKNOWN_ERROR;
}
ResTable_config curParams(defParams);
ResTable_config pseudoParams(curParams);
pseudoParams.language[0] = 'e';
pseudoParams.language[1] = 'n';
pseudoParams.country[0] = 'X';
pseudoParams.country[1] = 'A';
ResTable_config pseudoBidiParams(curParams);
pseudoBidiParams.language[0] = 'a';
pseudoBidiParams.language[1] = 'r';
pseudoBidiParams.country[0] = 'X';
pseudoBidiParams.country[1] = 'B';
// We should skip resources for pseudolocales if they were
// already added automatically. This is a fix for a transition period when
// manually pseudolocalized resources may be expected.
// TODO: remove this check after next SDK version release.
if ((bundle->getPseudolocalize() & PSEUDO_ACCENTED &&
curParams.locale == pseudoParams.locale) ||
(bundle->getPseudolocalize() & PSEUDO_BIDI &&
curParams.locale == pseudoBidiParams.locale)) {
SourcePos(in->getPrintableSource(), 0).warning(
"Resource file %s is skipped as pseudolocalization"
" was done automatically.",
in->getPrintableSource().string());
return NO_ERROR;
}
while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) {
if (code == ResXMLTree::START_TAG) {
const String16* curTag = NULL;
String16 curType;
String16 curName;
int32_t curFormat = ResTable_map::TYPE_ANY;
bool curIsBag = false;
bool curIsBagReplaceOnOverwrite = false;
bool curIsStyled = false;
bool curIsPseudolocalizable = false;
bool curIsFormatted = fileIsTranslatable;
bool localHasErrors = false;
if (strcmp16(block.getElementName(&len), skip16.string()) == 0) {
while ((code=block.next()) != ResXMLTree::END_DOCUMENT
&& code != ResXMLTree::BAD_DOCUMENT) {
if (code == ResXMLTree::END_TAG) {
if (strcmp16(block.getElementName(&len), skip16.string()) == 0) {
break;
}
}
}
continue;
} else if (strcmp16(block.getElementName(&len), eat_comment16.string()) == 0) {
while ((code=block.next()) != ResXMLTree::END_DOCUMENT
&& code != ResXMLTree::BAD_DOCUMENT) {
if (code == ResXMLTree::END_TAG) {
if (strcmp16(block.getElementName(&len), eat_comment16.string()) == 0) {
break;
}
}
}
continue;
} else if (strcmp16(block.getElementName(&len), public16.string()) == 0) {
SourcePos srcPos(in->getPrintableSource(), block.getLineNumber());
String16 type;
ssize_t typeIdx = block.indexOfAttribute(NULL, "type");
if (typeIdx < 0) {
srcPos.error("A 'type' attribute is required for <public>\n");
hasErrors = localHasErrors = true;
}
type = String16(block.getAttributeStringValue(typeIdx, &len));
String16 name;
ssize_t nameIdx = block.indexOfAttribute(NULL, "name");
if (nameIdx < 0) {
srcPos.error("A 'name' attribute is required for <public>\n");
hasErrors = localHasErrors = true;
}
name = String16(block.getAttributeStringValue(nameIdx, &len));
uint32_t ident = 0;
ssize_t identIdx = block.indexOfAttribute(NULL, "id");
if (identIdx >= 0) {
const char16_t* identStr = block.getAttributeStringValue(identIdx, &len);
Res_value identValue;
if (!ResTable::stringToInt(identStr, len, &identValue)) {
srcPos.error("Given 'id' attribute is not an integer: %s\n",
String8(block.getAttributeStringValue(identIdx, &len)).string());
hasErrors = localHasErrors = true;
} else {
ident = identValue.data;
nextPublicId.replaceValueFor(type, ident+1);
}
} else if (nextPublicId.indexOfKey(type) < 0) {
srcPos.error("No 'id' attribute supplied <public>,"
" and no previous id defined in this file.\n");
hasErrors = localHasErrors = true;
} else if (!localHasErrors) {
ident = nextPublicId.valueFor(type);
nextPublicId.replaceValueFor(type, ident+1);
}
if (!localHasErrors) {
err = outTable->addPublic(srcPos, myPackage, type, name, ident);
if (err < NO_ERROR) {
hasErrors = localHasErrors = true;
}
}
if (!localHasErrors) {
sp<AoptSymbols> symbols = assets->getSymbolsFor(String8("R"));
if (symbols != NULL) {
symbols = symbols->addNestedSymbol(String8(type), srcPos);
}
if (symbols != NULL) {
symbols->makeSymbolPublic(String8(name), srcPos);
String16 comment(
block.getComment(&len) ? block.getComment(&len) : nulStr);
symbols->appendComment(String8(name), comment, srcPos);
} else {
srcPos.error("Unable to create symbols!\n");
hasErrors = localHasErrors = true;
}
}
while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) {
if (code == ResXMLTree::END_TAG) {
if (strcmp16(block.getElementName(&len), public16.string()) == 0) {