blob: 84a412545dd8a112cc2a9057d5cbe1bf432f9289 [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
17#include "JavaClassGenerator.h"
Adam Lesinski769de982015-04-10 19:43:55 -070018#include "NameMangler.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080019#include "Resource.h"
20#include "ResourceTable.h"
21#include "ResourceValues.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070022#include "util/StringPiece.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080023
Adam Lesinskica2fc352015-04-03 12:08:26 -070024#include <algorithm>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080025#include <ostream>
26#include <set>
27#include <sstream>
28#include <tuple>
29
30namespace aapt {
31
32// The number of attributes to emit per line in a Styleable array.
33constexpr size_t kAttribsPerLine = 4;
34
Adam Lesinski1ab598f2015-08-14 14:26:04 -070035JavaClassGenerator::JavaClassGenerator(ResourceTable* table, JavaClassGeneratorOptions options) :
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080036 mTable(table), mOptions(options) {
37}
38
Adam Lesinski1ab598f2015-08-14 14:26:04 -070039static void generateHeader(const StringPiece16& packageNameToGenerate, std::ostream* out) {
40 *out << "/* AUTO-GENERATED FILE. DO NOT MODIFY.\n"
41 " *\n"
42 " * This class was automatically generated by the\n"
43 " * aapt tool from the resource data it found. It\n"
44 " * should not be modified by hand.\n"
45 " */\n\n"
46 "package " << packageNameToGenerate << ";\n\n";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080047}
48
49static const std::set<StringPiece16> sJavaIdentifiers = {
50 u"abstract", u"assert", u"boolean", u"break", u"byte",
51 u"case", u"catch", u"char", u"class", u"const", u"continue",
52 u"default", u"do", u"double", u"else", u"enum", u"extends",
53 u"final", u"finally", u"float", u"for", u"goto", u"if",
54 u"implements", u"import", u"instanceof", u"int", u"interface",
55 u"long", u"native", u"new", u"package", u"private", u"protected",
56 u"public", u"return", u"short", u"static", u"strictfp", u"super",
57 u"switch", u"synchronized", u"this", u"throw", u"throws",
58 u"transient", u"try", u"void", u"volatile", u"while", u"true",
59 u"false", u"null"
60};
61
62static bool isValidSymbol(const StringPiece16& symbol) {
63 return sJavaIdentifiers.find(symbol) == sJavaIdentifiers.end();
64}
65
66/*
67 * Java symbols can not contain . or -, but those are valid in a resource name.
68 * Replace those with '_'.
69 */
70static std::u16string transform(const StringPiece16& symbol) {
71 std::u16string output = symbol.toString();
72 for (char16_t& c : output) {
73 if (c == u'.' || c == u'-') {
74 c = u'_';
75 }
76 }
77 return output;
78}
79
Adam Lesinski1ab598f2015-08-14 14:26:04 -070080void JavaClassGenerator::generateStyleable(const StringPiece16& packageNameToGenerate,
81 const std::u16string& entryName,
82 const Styleable* styleable,
83 std::ostream* out) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080084 const StringPiece finalModifier = mOptions.useFinal ? " final" : "";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080085
86 // This must be sorted by resource ID.
Adam Lesinski838a6872015-05-01 13:14:05 -070087 std::vector<std::pair<ResourceId, ResourceNameRef>> sortedAttributes;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070088 sortedAttributes.reserve(styleable->entries.size());
89 for (const auto& attr : styleable->entries) {
Adam Lesinski330edcd2015-05-04 17:40:56 -070090 // If we are not encoding final attributes, the styleable entry may have no ID
91 // if we are building a static library.
Adam Lesinski1ab598f2015-08-14 14:26:04 -070092 assert((!mOptions.useFinal || attr.id) && "no ID set for Styleable entry");
93 assert(attr.name && "no name set for Styleable entry");
94 sortedAttributes.emplace_back(attr.id ? attr.id.value() : ResourceId(0), attr.name.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080095 }
96 std::sort(sortedAttributes.begin(), sortedAttributes.end());
97
98 // First we emit the array containing the IDs of each attribute.
Adam Lesinski769de982015-04-10 19:43:55 -070099 *out << " "
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700100 << "public static final int[] " << transform(entryName) << " = {";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800101
102 const size_t attrCount = sortedAttributes.size();
103 for (size_t i = 0; i < attrCount; i++) {
104 if (i % kAttribsPerLine == 0) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700105 *out << "\n ";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800106 }
107
Adam Lesinski769de982015-04-10 19:43:55 -0700108 *out << sortedAttributes[i].first;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800109 if (i != attrCount - 1) {
Adam Lesinski769de982015-04-10 19:43:55 -0700110 *out << ", ";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800111 }
112 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700113 *out << "\n };\n";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800114
115 // Now we emit the indices into the array.
116 for (size_t i = 0; i < attrCount; i++) {
Adam Lesinski769de982015-04-10 19:43:55 -0700117 *out << " "
118 << "public static" << finalModifier
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700119 << " int " << transform(entryName);
Adam Lesinski838a6872015-05-01 13:14:05 -0700120
121 // We may reference IDs from other packages, so prefix the entry name with
122 // the package.
123 const ResourceNameRef& itemName = sortedAttributes[i].second;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700124 if (packageNameToGenerate != itemName.package) {
Adam Lesinski838a6872015-05-01 13:14:05 -0700125 *out << "_" << transform(itemName.package);
126 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700127 *out << "_" << transform(itemName.entry) << " = " << i << ";\n";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800128 }
129}
130
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700131bool JavaClassGenerator::generateType(const StringPiece16& packageNameToGenerate,
132 const ResourceTablePackage* package,
133 const ResourceTableType* type,
134 std::ostream* out) {
Adam Lesinski769de982015-04-10 19:43:55 -0700135 const StringPiece finalModifier = mOptions.useFinal ? " final" : "";
136
137 std::u16string unmangledPackage;
138 std::u16string unmangledName;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700139 for (const auto& entry : type->entries) {
140 ResourceId id = { package->id.value(), type->id.value(), entry->id.value() };
Adam Lesinski769de982015-04-10 19:43:55 -0700141 assert(id.isValid());
142
143 unmangledName = entry->name;
144 if (NameMangler::unmangle(&unmangledName, &unmangledPackage)) {
145 // The entry name was mangled, and we successfully unmangled it.
146 // Check that we want to emit this symbol.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700147 if (package->name != unmangledPackage) {
Adam Lesinski769de982015-04-10 19:43:55 -0700148 // Skip the entry if it doesn't belong to the package we're writing.
149 continue;
150 }
151 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700152 if (packageNameToGenerate != package->name) {
Adam Lesinski769de982015-04-10 19:43:55 -0700153 // We are processing a mangled package name,
154 // but this is a non-mangled resource.
155 continue;
156 }
157 }
158
159 if (!isValidSymbol(unmangledName)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700160 ResourceNameRef resourceName = { packageNameToGenerate, type->type, unmangledName };
Adam Lesinski769de982015-04-10 19:43:55 -0700161 std::stringstream err;
162 err << "invalid symbol name '" << resourceName << "'";
163 mError = err.str();
164 return false;
165 }
166
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700167 if (type->type == ResourceType::kStyleable) {
Adam Lesinski769de982015-04-10 19:43:55 -0700168 assert(!entry->values.empty());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700169 generateStyleable(packageNameToGenerate, unmangledName, static_cast<const Styleable*>(
170 entry->values.front().value.get()), out);
Adam Lesinski769de982015-04-10 19:43:55 -0700171 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700172 *out << " " << "public static" << finalModifier
173 << " int " << transform(unmangledName) << " = " << id << ";\n";
Adam Lesinski769de982015-04-10 19:43:55 -0700174 }
175 }
176 return true;
177}
178
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700179bool JavaClassGenerator::generate(const StringPiece16& packageNameToGenerate, std::ostream* out) {
180 generateHeader(packageNameToGenerate, out);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800181
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700182 *out << "public final class R {\n";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800183
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700184 for (const auto& package : mTable->packages) {
185 for (const auto& type : package->types) {
186 *out << " public static final class " << type->type << " {\n";
187 if (!generateType(packageNameToGenerate, package.get(), type.get(), out)) {
188 return false;
189 }
190 *out << " }\n";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800191 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800192 }
193
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700194 *out << "}\n";
195 out->flush();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800196 return true;
197}
198
199} // namespace aapt