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 | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame^] | 80 | bool JavaClassGenerator::skipSymbol(SymbolState state) { |
| 81 | switch (mOptions.types) { |
| 82 | case JavaClassGeneratorOptions::SymbolTypes::kAll: |
| 83 | return false; |
| 84 | case JavaClassGeneratorOptions::SymbolTypes::kPublicPrivate: |
| 85 | return state == SymbolState::kUndefined; |
| 86 | case JavaClassGeneratorOptions::SymbolTypes::kPublic: |
| 87 | return state != SymbolState::kPublic; |
| 88 | } |
| 89 | return true; |
| 90 | } |
| 91 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 92 | void JavaClassGenerator::generateStyleable(const StringPiece16& packageNameToGenerate, |
| 93 | const std::u16string& entryName, |
| 94 | const Styleable* styleable, |
| 95 | std::ostream* out) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 96 | const StringPiece finalModifier = mOptions.useFinal ? " final" : ""; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 97 | |
| 98 | // This must be sorted by resource ID. |
Adam Lesinski | 838a687 | 2015-05-01 13:14:05 -0700 | [diff] [blame] | 99 | std::vector<std::pair<ResourceId, ResourceNameRef>> sortedAttributes; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 100 | sortedAttributes.reserve(styleable->entries.size()); |
| 101 | for (const auto& attr : styleable->entries) { |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 102 | // If we are not encoding final attributes, the styleable entry may have no ID |
| 103 | // if we are building a static library. |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 104 | assert((!mOptions.useFinal || attr.id) && "no ID set for Styleable entry"); |
| 105 | assert(attr.name && "no name set for Styleable entry"); |
| 106 | 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] | 107 | } |
| 108 | std::sort(sortedAttributes.begin(), sortedAttributes.end()); |
| 109 | |
| 110 | // First we emit the array containing the IDs of each attribute. |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 111 | *out << " " |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 112 | << "public static final int[] " << transform(entryName) << " = {"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 113 | |
| 114 | const size_t attrCount = sortedAttributes.size(); |
| 115 | for (size_t i = 0; i < attrCount; i++) { |
| 116 | if (i % kAttribsPerLine == 0) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 117 | *out << "\n "; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 120 | *out << sortedAttributes[i].first; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 121 | if (i != attrCount - 1) { |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 122 | *out << ", "; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 123 | } |
| 124 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 125 | *out << "\n };\n"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 126 | |
| 127 | // Now we emit the indices into the array. |
| 128 | for (size_t i = 0; i < attrCount; i++) { |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 129 | *out << " " |
| 130 | << "public static" << finalModifier |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 131 | << " int " << transform(entryName); |
Adam Lesinski | 838a687 | 2015-05-01 13:14:05 -0700 | [diff] [blame] | 132 | |
| 133 | // We may reference IDs from other packages, so prefix the entry name with |
| 134 | // the package. |
| 135 | const ResourceNameRef& itemName = sortedAttributes[i].second; |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame^] | 136 | if (!itemName.package.empty() && packageNameToGenerate != itemName.package) { |
Adam Lesinski | 838a687 | 2015-05-01 13:14:05 -0700 | [diff] [blame] | 137 | *out << "_" << transform(itemName.package); |
| 138 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 139 | *out << "_" << transform(itemName.entry) << " = " << i << ";\n"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 143 | bool JavaClassGenerator::generateType(const StringPiece16& packageNameToGenerate, |
| 144 | const ResourceTablePackage* package, |
| 145 | const ResourceTableType* type, |
| 146 | std::ostream* out) { |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 147 | const StringPiece finalModifier = mOptions.useFinal ? " final" : ""; |
| 148 | |
| 149 | std::u16string unmangledPackage; |
| 150 | std::u16string unmangledName; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 151 | for (const auto& entry : type->entries) { |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame^] | 152 | if (skipSymbol(entry->symbolStatus.state)) { |
| 153 | continue; |
| 154 | } |
| 155 | |
| 156 | ResourceId id(package->id.value(), type->id.value(), entry->id.value()); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 157 | assert(id.isValid()); |
| 158 | |
| 159 | unmangledName = entry->name; |
| 160 | if (NameMangler::unmangle(&unmangledName, &unmangledPackage)) { |
| 161 | // The entry name was mangled, and we successfully unmangled it. |
| 162 | // Check that we want to emit this symbol. |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 163 | if (package->name != unmangledPackage) { |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 164 | // Skip the entry if it doesn't belong to the package we're writing. |
| 165 | continue; |
| 166 | } |
| 167 | } else { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 168 | if (packageNameToGenerate != package->name) { |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 169 | // We are processing a mangled package name, |
| 170 | // but this is a non-mangled resource. |
| 171 | continue; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | if (!isValidSymbol(unmangledName)) { |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame^] | 176 | ResourceNameRef resourceName(packageNameToGenerate, type->type, unmangledName); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 177 | std::stringstream err; |
| 178 | err << "invalid symbol name '" << resourceName << "'"; |
| 179 | mError = err.str(); |
| 180 | return false; |
| 181 | } |
| 182 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 183 | if (type->type == ResourceType::kStyleable) { |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 184 | assert(!entry->values.empty()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 185 | generateStyleable(packageNameToGenerate, unmangledName, static_cast<const Styleable*>( |
| 186 | entry->values.front().value.get()), out); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 187 | } else { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 188 | *out << " " << "public static" << finalModifier |
| 189 | << " int " << transform(unmangledName) << " = " << id << ";\n"; |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | return true; |
| 193 | } |
| 194 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 195 | bool JavaClassGenerator::generate(const StringPiece16& packageNameToGenerate, std::ostream* out) { |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame^] | 196 | return generate(packageNameToGenerate, packageNameToGenerate, out); |
| 197 | } |
| 198 | |
| 199 | bool JavaClassGenerator::generate(const StringPiece16& packageNameToGenerate, |
| 200 | const StringPiece16& outPackageName, std::ostream* out) { |
| 201 | generateHeader(outPackageName, out); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 202 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 203 | *out << "public final class R {\n"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 204 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 205 | for (const auto& package : mTable->packages) { |
| 206 | for (const auto& type : package->types) { |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame^] | 207 | StringPiece16 typeStr; |
| 208 | if (type->type == ResourceType::kAttrPrivate) { |
| 209 | typeStr = toString(ResourceType::kAttr); |
| 210 | } else { |
| 211 | typeStr = toString(type->type); |
| 212 | } |
| 213 | *out << " public static final class " << typeStr << " {\n"; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 214 | if (!generateType(packageNameToGenerate, package.get(), type.get(), out)) { |
| 215 | return false; |
| 216 | } |
| 217 | *out << " }\n"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 218 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 219 | } |
| 220 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 221 | *out << "}\n"; |
| 222 | out->flush(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 223 | return true; |
| 224 | } |
| 225 | |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame^] | 226 | |
| 227 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 228 | } // namespace aapt |