blob: dfd2ef6f53725bd3b10a201e37620d96380db2fe [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Adam Lesinski769de982015-04-10 19:43:55 -070017#include "NameMangler.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080018#include "Resource.h"
19#include "ResourceTable.h"
20#include "ResourceValues.h"
Adam Lesinski3b4cd942015-10-30 16:31:42 -070021#include "ValueVisitor.h"
22
Adam Lesinskica5638f2015-10-21 14:42:43 -070023#include "java/AnnotationProcessor.h"
24#include "java/JavaClassGenerator.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070025#include "util/StringPiece.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080026
Adam Lesinskica2fc352015-04-03 12:08:26 -070027#include <algorithm>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080028#include <ostream>
29#include <set>
30#include <sstream>
31#include <tuple>
32
33namespace aapt {
34
35// The number of attributes to emit per line in a Styleable array.
36constexpr size_t kAttribsPerLine = 4;
37
Adam Lesinski1ab598f2015-08-14 14:26:04 -070038JavaClassGenerator::JavaClassGenerator(ResourceTable* table, JavaClassGeneratorOptions options) :
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080039 mTable(table), mOptions(options) {
40}
41
Adam Lesinski1ab598f2015-08-14 14:26:04 -070042static void generateHeader(const StringPiece16& packageNameToGenerate, std::ostream* out) {
43 *out << "/* AUTO-GENERATED FILE. DO NOT MODIFY.\n"
44 " *\n"
45 " * This class was automatically generated by the\n"
46 " * aapt tool from the resource data it found. It\n"
47 " * should not be modified by hand.\n"
48 " */\n\n"
49 "package " << packageNameToGenerate << ";\n\n";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080050}
51
52static const std::set<StringPiece16> sJavaIdentifiers = {
53 u"abstract", u"assert", u"boolean", u"break", u"byte",
54 u"case", u"catch", u"char", u"class", u"const", u"continue",
55 u"default", u"do", u"double", u"else", u"enum", u"extends",
56 u"final", u"finally", u"float", u"for", u"goto", u"if",
57 u"implements", u"import", u"instanceof", u"int", u"interface",
58 u"long", u"native", u"new", u"package", u"private", u"protected",
59 u"public", u"return", u"short", u"static", u"strictfp", u"super",
60 u"switch", u"synchronized", u"this", u"throw", u"throws",
61 u"transient", u"try", u"void", u"volatile", u"while", u"true",
62 u"false", u"null"
63};
64
65static bool isValidSymbol(const StringPiece16& symbol) {
66 return sJavaIdentifiers.find(symbol) == sJavaIdentifiers.end();
67}
68
69/*
70 * Java symbols can not contain . or -, but those are valid in a resource name.
71 * Replace those with '_'.
72 */
73static std::u16string transform(const StringPiece16& symbol) {
74 std::u16string output = symbol.toString();
75 for (char16_t& c : output) {
76 if (c == u'.' || c == u'-') {
77 c = u'_';
78 }
79 }
80 return output;
81}
82
Adam Lesinski9e10ac72015-10-16 14:37:48 -070083bool JavaClassGenerator::skipSymbol(SymbolState state) {
84 switch (mOptions.types) {
85 case JavaClassGeneratorOptions::SymbolTypes::kAll:
86 return false;
87 case JavaClassGeneratorOptions::SymbolTypes::kPublicPrivate:
88 return state == SymbolState::kUndefined;
89 case JavaClassGeneratorOptions::SymbolTypes::kPublic:
90 return state != SymbolState::kPublic;
91 }
92 return true;
93}
94
Adam Lesinski1ab598f2015-08-14 14:26:04 -070095void JavaClassGenerator::generateStyleable(const StringPiece16& packageNameToGenerate,
96 const std::u16string& entryName,
97 const Styleable* styleable,
98 std::ostream* out) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080099 const StringPiece finalModifier = mOptions.useFinal ? " final" : "";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800100
101 // This must be sorted by resource ID.
Adam Lesinski838a6872015-05-01 13:14:05 -0700102 std::vector<std::pair<ResourceId, ResourceNameRef>> sortedAttributes;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700103 sortedAttributes.reserve(styleable->entries.size());
104 for (const auto& attr : styleable->entries) {
Adam Lesinski330edcd2015-05-04 17:40:56 -0700105 // If we are not encoding final attributes, the styleable entry may have no ID
106 // if we are building a static library.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700107 assert((!mOptions.useFinal || attr.id) && "no ID set for Styleable entry");
108 assert(attr.name && "no name set for Styleable entry");
109 sortedAttributes.emplace_back(attr.id ? attr.id.value() : ResourceId(0), attr.name.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800110 }
111 std::sort(sortedAttributes.begin(), sortedAttributes.end());
112
113 // First we emit the array containing the IDs of each attribute.
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700114 *out << " "
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700115 << "public static final int[] " << transform(entryName) << " = {";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800116
117 const size_t attrCount = sortedAttributes.size();
118 for (size_t i = 0; i < attrCount; i++) {
119 if (i % kAttribsPerLine == 0) {
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700120 *out << "\n ";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800121 }
122
Adam Lesinski769de982015-04-10 19:43:55 -0700123 *out << sortedAttributes[i].first;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800124 if (i != attrCount - 1) {
Adam Lesinski769de982015-04-10 19:43:55 -0700125 *out << ", ";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800126 }
127 }
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700128 *out << "\n };\n";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800129
130 // Now we emit the indices into the array.
131 for (size_t i = 0; i < attrCount; i++) {
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700132 *out << " "
Adam Lesinski769de982015-04-10 19:43:55 -0700133 << "public static" << finalModifier
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700134 << " int " << transform(entryName);
Adam Lesinski838a6872015-05-01 13:14:05 -0700135
136 // We may reference IDs from other packages, so prefix the entry name with
137 // the package.
138 const ResourceNameRef& itemName = sortedAttributes[i].second;
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700139 if (!itemName.package.empty() && packageNameToGenerate != itemName.package) {
Adam Lesinski838a6872015-05-01 13:14:05 -0700140 *out << "_" << transform(itemName.package);
141 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700142 *out << "_" << transform(itemName.entry) << " = " << i << ";\n";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800143 }
144}
145
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700146static void addAttributeFormatDoc(AnnotationProcessor* processor, Attribute* attr) {
147 const uint32_t typeMask = attr->typeMask;
148 if (typeMask & android::ResTable_map::TYPE_REFERENCE) {
149 processor->appendComment(
150 "<p>May be a reference to another resource, in the form\n"
151 "\"<code>@[+][<i>package</i>:]<i>type</i>/<i>name</i></code>\" or a theme\n"
152 "attribute in the form\n"
153 "\"<code>?[<i>package</i>:]<i>type</i>/<i>name</i></code>\".");
154 }
155
156 if (typeMask & android::ResTable_map::TYPE_STRING) {
157 processor->appendComment(
158 "<p>May be a string value, using '\\;' to escape characters such as\n"
159 "'\\n' or '\\uxxxx' for a unicode character;");
160 }
161
162 if (typeMask & android::ResTable_map::TYPE_INTEGER) {
163 processor->appendComment("<p>May be an integer value, such as \"<code>100</code>\".");
164 }
165
166 if (typeMask & android::ResTable_map::TYPE_BOOLEAN) {
167 processor->appendComment(
168 "<p>May be a boolean value, such as \"<code>true</code>\" or\n"
169 "\"<code>false</code>\".");
170 }
171
172 if (typeMask & android::ResTable_map::TYPE_COLOR) {
173 processor->appendComment(
174 "<p>May be a color value, in the form of \"<code>#<i>rgb</i></code>\",\n"
175 "\"<code>#<i>argb</i></code>\", \"<code>#<i>rrggbb</i></code\", or \n"
176 "\"<code>#<i>aarrggbb</i></code>\".");
177 }
178
179 if (typeMask & android::ResTable_map::TYPE_FLOAT) {
180 processor->appendComment(
181 "<p>May be a floating point value, such as \"<code>1.2</code>\".");
182 }
183
184 if (typeMask & android::ResTable_map::TYPE_DIMENSION) {
185 processor->appendComment(
186 "<p>May be a dimension value, which is a floating point number appended with a\n"
187 "unit such as \"<code>14.5sp</code>\".\n"
188 "Available units are: px (pixels), dp (density-independent pixels),\n"
189 "sp (scaled pixels based on preferred font size), in (inches), and\n"
190 "mm (millimeters).");
191 }
192
193 if (typeMask & android::ResTable_map::TYPE_FRACTION) {
194 processor->appendComment(
195 "<p>May be a fractional value, which is a floating point number appended with\n"
196 "either % or %p, such as \"<code>14.5%</code>\".\n"
197 "The % suffix always means a percentage of the base size;\n"
198 "the optional %p suffix provides a size relative to some parent container.");
199 }
200
201 if (typeMask & (android::ResTable_map::TYPE_FLAGS | android::ResTable_map::TYPE_ENUM)) {
202 if (typeMask & android::ResTable_map::TYPE_FLAGS) {
203 processor->appendComment(
204 "<p>Must be one or more (separated by '|') of the following "
205 "constant values.</p>");
206 } else {
207 processor->appendComment("<p>Must be one of the following constant values.</p>");
208 }
209
210 processor->appendComment("<table>\n<colgroup align=\"left\" />\n"
211 "<colgroup align=\"left\" />\n"
212 "<colgroup align=\"left\" />\n"
213 "<tr><th>Constant</th><th>Value</th><th>Description</th></tr>\n");
214 for (const Attribute::Symbol& symbol : attr->symbols) {
215 std::stringstream line;
216 line << "<tr><td>" << symbol.symbol.name.value().entry << "</td>"
217 << "<td>" << std::hex << symbol.value << std::dec << "</td>"
218 << "<td>" << util::trimWhitespace(symbol.symbol.getComment()) << "</td></tr>";
219 processor->appendComment(line.str());
220 }
221 processor->appendComment("</table>");
222 }
223}
224
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700225bool JavaClassGenerator::generateType(const StringPiece16& packageNameToGenerate,
226 const ResourceTablePackage* package,
227 const ResourceTableType* type,
228 std::ostream* out) {
Adam Lesinski769de982015-04-10 19:43:55 -0700229 const StringPiece finalModifier = mOptions.useFinal ? " final" : "";
230
231 std::u16string unmangledPackage;
232 std::u16string unmangledName;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700233 for (const auto& entry : type->entries) {
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700234 if (skipSymbol(entry->symbolStatus.state)) {
235 continue;
236 }
237
238 ResourceId id(package->id.value(), type->id.value(), entry->id.value());
Adam Lesinski769de982015-04-10 19:43:55 -0700239 assert(id.isValid());
240
241 unmangledName = entry->name;
242 if (NameMangler::unmangle(&unmangledName, &unmangledPackage)) {
243 // The entry name was mangled, and we successfully unmangled it.
244 // Check that we want to emit this symbol.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700245 if (package->name != unmangledPackage) {
Adam Lesinski769de982015-04-10 19:43:55 -0700246 // Skip the entry if it doesn't belong to the package we're writing.
247 continue;
248 }
249 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700250 if (packageNameToGenerate != package->name) {
Adam Lesinski769de982015-04-10 19:43:55 -0700251 // We are processing a mangled package name,
252 // but this is a non-mangled resource.
253 continue;
254 }
255 }
256
257 if (!isValidSymbol(unmangledName)) {
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700258 ResourceNameRef resourceName(packageNameToGenerate, type->type, unmangledName);
Adam Lesinski769de982015-04-10 19:43:55 -0700259 std::stringstream err;
260 err << "invalid symbol name '" << resourceName << "'";
261 mError = err.str();
262 return false;
263 }
264
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700265 if (type->type == ResourceType::kStyleable) {
Adam Lesinski769de982015-04-10 19:43:55 -0700266 assert(!entry->values.empty());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700267 generateStyleable(packageNameToGenerate, unmangledName, static_cast<const Styleable*>(
268 entry->values.front().value.get()), out);
Adam Lesinski769de982015-04-10 19:43:55 -0700269 } else {
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700270 AnnotationProcessor processor(" ");
271 if (entry->symbolStatus.state != SymbolState::kUndefined) {
272 processor.appendComment(entry->symbolStatus.comment);
273 }
274
275 for (const auto& configValue : entry->values) {
276 processor.appendComment(configValue.value->getComment());
277 }
278
279 if (!entry->values.empty()) {
280 if (Attribute* attr = valueCast<Attribute>(entry->values.front().value.get())) {
281 // We list out the available values for the given attribute.
282 addAttributeFormatDoc(&processor, attr);
283 }
284 }
285
286 std::string comment = processor.buildComment();
287 if (!comment.empty()) {
288 *out << comment << "\n";
289 }
290
291 std::string annotations = processor.buildAnnotations();
292 if (!annotations.empty()) {
293 *out << annotations << "\n";
294 }
295
296 *out << " " << "public static" << finalModifier
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700297 << " int " << transform(unmangledName) << " = " << id << ";\n";
Adam Lesinski769de982015-04-10 19:43:55 -0700298 }
299 }
300 return true;
301}
302
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700303bool JavaClassGenerator::generate(const StringPiece16& packageNameToGenerate, std::ostream* out) {
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700304 return generate(packageNameToGenerate, packageNameToGenerate, out);
305}
306
307bool JavaClassGenerator::generate(const StringPiece16& packageNameToGenerate,
308 const StringPiece16& outPackageName, std::ostream* out) {
309 generateHeader(outPackageName, out);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800310
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700311 *out << "public final class R {\n";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800312
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700313 for (const auto& package : mTable->packages) {
314 for (const auto& type : package->types) {
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700315 StringPiece16 typeStr;
316 if (type->type == ResourceType::kAttrPrivate) {
317 typeStr = toString(ResourceType::kAttr);
318 } else {
319 typeStr = toString(type->type);
320 }
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700321 *out << " public static final class " << typeStr << " {\n";
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700322 if (!generateType(packageNameToGenerate, package.get(), type.get(), out)) {
323 return false;
324 }
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700325 *out << " }\n";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800326 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800327 }
328
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700329 *out << "}\n";
330 out->flush();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800331 return true;
332}
333
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700334
335
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800336} // namespace aapt