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" |
| 22 | #include "StringPiece.h" |
| 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 | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 35 | JavaClassGenerator::JavaClassGenerator(const std::shared_ptr<const ResourceTable>& table, |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 36 | Options options) : |
| 37 | mTable(table), mOptions(options) { |
| 38 | } |
| 39 | |
| 40 | static void generateHeader(std::ostream& out, const StringPiece16& package) { |
| 41 | out << "/* AUTO-GENERATED FILE. DO NOT MODIFY.\n" |
| 42 | " *\n" |
| 43 | " * This class was automatically generated by the\n" |
| 44 | " * aapt tool from the resource data it found. It\n" |
| 45 | " * should not be modified by hand.\n" |
| 46 | " */\n\n"; |
| 47 | out << "package " << package << ";" |
| 48 | << std::endl |
| 49 | << std::endl; |
| 50 | } |
| 51 | |
| 52 | static 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 | |
| 65 | static 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 | */ |
| 73 | static 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 Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 83 | struct GenArgs : ValueVisitorArgs { |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 84 | GenArgs(std::ostream* o, const std::u16string* p, std::u16string* e) : |
| 85 | out(o), package(p), entryName(e) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 88 | std::ostream* out; |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 89 | const std::u16string* package; |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 90 | std::u16string* entryName; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | void JavaClassGenerator::visit(const Styleable& styleable, ValueVisitorArgs& a) { |
| 94 | const StringPiece finalModifier = mOptions.useFinal ? " final" : ""; |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 95 | std::ostream* out = static_cast<GenArgs&>(a).out; |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 96 | const std::u16string* package = static_cast<GenArgs&>(a).package; |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 97 | std::u16string* entryName = static_cast<GenArgs&>(a).entryName; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 98 | |
| 99 | // This must be sorted by resource ID. |
Adam Lesinski | 838a687 | 2015-05-01 13:14:05 -0700 | [diff] [blame] | 100 | std::vector<std::pair<ResourceId, ResourceNameRef>> sortedAttributes; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 101 | sortedAttributes.reserve(styleable.entries.size()); |
| 102 | for (const auto& attr : styleable.entries) { |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 103 | // If we are not encoding final attributes, the styleable entry may have no ID |
| 104 | // if we are building a static library. |
| 105 | assert((!mOptions.useFinal || attr.id.isValid()) && "no ID set for Styleable entry"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 106 | assert(attr.name.isValid() && "no name set for Styleable entry"); |
Adam Lesinski | 838a687 | 2015-05-01 13:14:05 -0700 | [diff] [blame] | 107 | sortedAttributes.emplace_back(attr.id, attr.name); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 108 | } |
| 109 | std::sort(sortedAttributes.begin(), sortedAttributes.end()); |
| 110 | |
| 111 | // First we emit the array containing the IDs of each attribute. |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 112 | *out << " " |
| 113 | << "public static final int[] " << transform(*entryName) << " = {"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 114 | |
| 115 | const size_t attrCount = sortedAttributes.size(); |
| 116 | for (size_t i = 0; i < attrCount; i++) { |
| 117 | if (i % kAttribsPerLine == 0) { |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 118 | *out << std::endl << " "; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 121 | *out << sortedAttributes[i].first; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 122 | if (i != attrCount - 1) { |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 123 | *out << ", "; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 124 | } |
| 125 | } |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 126 | *out << std::endl << " };" << std::endl; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 127 | |
| 128 | // Now we emit the indices into the array. |
| 129 | for (size_t i = 0; i < attrCount; i++) { |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 130 | *out << " " |
| 131 | << "public static" << finalModifier |
Adam Lesinski | 838a687 | 2015-05-01 13:14:05 -0700 | [diff] [blame] | 132 | << " int " << transform(*entryName); |
| 133 | |
| 134 | // We may reference IDs from other packages, so prefix the entry name with |
| 135 | // the package. |
| 136 | const ResourceNameRef& itemName = sortedAttributes[i].second; |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 137 | if (itemName.package != *package) { |
Adam Lesinski | 838a687 | 2015-05-01 13:14:05 -0700 | [diff] [blame] | 138 | *out << "_" << transform(itemName.package); |
| 139 | } |
| 140 | *out << "_" << transform(itemName.entry) << " = " << i << ";" << std::endl; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 144 | bool JavaClassGenerator::generateType(const std::u16string& package, size_t packageId, |
| 145 | const ResourceTableType& type, std::ostream& out) { |
| 146 | const StringPiece finalModifier = mOptions.useFinal ? " final" : ""; |
| 147 | |
| 148 | std::u16string unmangledPackage; |
| 149 | std::u16string unmangledName; |
| 150 | for (const auto& entry : type.entries) { |
| 151 | ResourceId id = { packageId, type.typeId, entry->entryId }; |
| 152 | assert(id.isValid()); |
| 153 | |
| 154 | unmangledName = entry->name; |
| 155 | if (NameMangler::unmangle(&unmangledName, &unmangledPackage)) { |
| 156 | // The entry name was mangled, and we successfully unmangled it. |
| 157 | // Check that we want to emit this symbol. |
| 158 | if (package != unmangledPackage) { |
| 159 | // Skip the entry if it doesn't belong to the package we're writing. |
| 160 | continue; |
| 161 | } |
| 162 | } else { |
| 163 | if (package != mTable->getPackage()) { |
| 164 | // We are processing a mangled package name, |
| 165 | // but this is a non-mangled resource. |
| 166 | continue; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | if (!isValidSymbol(unmangledName)) { |
| 171 | ResourceNameRef resourceName = { package, type.type, unmangledName }; |
| 172 | std::stringstream err; |
| 173 | err << "invalid symbol name '" << resourceName << "'"; |
| 174 | mError = err.str(); |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | if (type.type == ResourceType::kStyleable) { |
| 179 | assert(!entry->values.empty()); |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 180 | entry->values.front().value->accept(*this, GenArgs{ &out, &package, &unmangledName }); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 181 | } else { |
| 182 | out << " " << "public static" << finalModifier |
| 183 | << " int " << transform(unmangledName) << " = " << id << ";" << std::endl; |
| 184 | } |
| 185 | } |
| 186 | return true; |
| 187 | } |
| 188 | |
| 189 | bool JavaClassGenerator::generate(const std::u16string& package, std::ostream& out) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 190 | const size_t packageId = mTable->getPackageId(); |
| 191 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 192 | generateHeader(out, package); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 193 | |
| 194 | out << "public final class R {" << std::endl; |
| 195 | |
| 196 | for (const auto& type : *mTable) { |
| 197 | out << " public static final class " << type->type << " {" << std::endl; |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 198 | if (!generateType(package, packageId, *type, out)) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 199 | return false; |
| 200 | } |
| 201 | out << " }" << std::endl; |
| 202 | } |
| 203 | |
| 204 | out << "}" << std::endl; |
| 205 | return true; |
| 206 | } |
| 207 | |
| 208 | } // namespace aapt |