Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1 | /* |
| 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 Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 18 | #include "NameMangler.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 19 | #include "Resource.h" |
| 20 | #include "ResourceTable.h" |
| 21 | #include "ResourceValues.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 22 | #include "util/StringPiece.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 23 | |
Adam Lesinski | ca2fc35 | 2015-04-03 12:08:26 -0700 | [diff] [blame] | 24 | #include <algorithm> |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 25 | #include <ostream> |
| 26 | #include <set> |
| 27 | #include <sstream> |
| 28 | #include <tuple> |
| 29 | |
| 30 | namespace aapt { |
| 31 | |
| 32 | // The number of attributes to emit per line in a Styleable array. |
| 33 | constexpr size_t kAttribsPerLine = 4; |
| 34 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 35 | JavaClassGenerator::JavaClassGenerator(ResourceTable* table, JavaClassGeneratorOptions options) : |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 36 | mTable(table), mOptions(options) { |
| 37 | } |
| 38 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 39 | static 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 Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | static 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 | |
| 62 | static 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 | */ |
| 70 | static 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 80 | void JavaClassGenerator::generateStyleable(const StringPiece16& packageNameToGenerate, |
| 81 | const std::u16string& entryName, |
| 82 | const Styleable* styleable, |
| 83 | std::ostream* out) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 84 | const StringPiece finalModifier = mOptions.useFinal ? " final" : ""; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 85 | |
| 86 | // This must be sorted by resource ID. |
Adam Lesinski | 838a687 | 2015-05-01 13:14:05 -0700 | [diff] [blame] | 87 | std::vector<std::pair<ResourceId, ResourceNameRef>> sortedAttributes; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 88 | sortedAttributes.reserve(styleable->entries.size()); |
| 89 | for (const auto& attr : styleable->entries) { |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 90 | // If we are not encoding final attributes, the styleable entry may have no ID |
| 91 | // if we are building a static library. |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 92 | 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 Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 95 | } |
| 96 | std::sort(sortedAttributes.begin(), sortedAttributes.end()); |
| 97 | |
| 98 | // First we emit the array containing the IDs of each attribute. |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 99 | *out << " " |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 100 | << "public static final int[] " << transform(entryName) << " = {"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 101 | |
| 102 | const size_t attrCount = sortedAttributes.size(); |
| 103 | for (size_t i = 0; i < attrCount; i++) { |
| 104 | if (i % kAttribsPerLine == 0) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 105 | *out << "\n "; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 106 | } |
| 107 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 108 | *out << sortedAttributes[i].first; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 109 | if (i != attrCount - 1) { |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 110 | *out << ", "; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 111 | } |
| 112 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 113 | *out << "\n };\n"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 114 | |
| 115 | // Now we emit the indices into the array. |
| 116 | for (size_t i = 0; i < attrCount; i++) { |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 117 | *out << " " |
| 118 | << "public static" << finalModifier |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 119 | << " int " << transform(entryName); |
Adam Lesinski | 838a687 | 2015-05-01 13:14:05 -0700 | [diff] [blame] | 120 | |
| 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 124 | if (packageNameToGenerate != itemName.package) { |
Adam Lesinski | 838a687 | 2015-05-01 13:14:05 -0700 | [diff] [blame] | 125 | *out << "_" << transform(itemName.package); |
| 126 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 127 | *out << "_" << transform(itemName.entry) << " = " << i << ";\n"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 131 | bool JavaClassGenerator::generateType(const StringPiece16& packageNameToGenerate, |
| 132 | const ResourceTablePackage* package, |
| 133 | const ResourceTableType* type, |
| 134 | std::ostream* out) { |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 135 | const StringPiece finalModifier = mOptions.useFinal ? " final" : ""; |
| 136 | |
| 137 | std::u16string unmangledPackage; |
| 138 | std::u16string unmangledName; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 139 | for (const auto& entry : type->entries) { |
| 140 | ResourceId id = { package->id.value(), type->id.value(), entry->id.value() }; |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 141 | 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 147 | if (package->name != unmangledPackage) { |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 148 | // Skip the entry if it doesn't belong to the package we're writing. |
| 149 | continue; |
| 150 | } |
| 151 | } else { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 152 | if (packageNameToGenerate != package->name) { |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 153 | // 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 160 | ResourceNameRef resourceName = { packageNameToGenerate, type->type, unmangledName }; |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 161 | std::stringstream err; |
| 162 | err << "invalid symbol name '" << resourceName << "'"; |
| 163 | mError = err.str(); |
| 164 | return false; |
| 165 | } |
| 166 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 167 | if (type->type == ResourceType::kStyleable) { |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 168 | assert(!entry->values.empty()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 169 | generateStyleable(packageNameToGenerate, unmangledName, static_cast<const Styleable*>( |
| 170 | entry->values.front().value.get()), out); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 171 | } else { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 172 | *out << " " << "public static" << finalModifier |
| 173 | << " int " << transform(unmangledName) << " = " << id << ";\n"; |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | return true; |
| 177 | } |
| 178 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 179 | bool JavaClassGenerator::generate(const StringPiece16& packageNameToGenerate, std::ostream* out) { |
| 180 | generateHeader(packageNameToGenerate, out); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 181 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 182 | *out << "public final class R {\n"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 183 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 184 | 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 Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 191 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 194 | *out << "}\n"; |
| 195 | out->flush(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 196 | return true; |
| 197 | } |
| 198 | |
| 199 | } // namespace aapt |