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" |
| 18 | #include "Resource.h" |
| 19 | #include "ResourceTable.h" |
| 20 | #include "ResourceValues.h" |
| 21 | #include "StringPiece.h" |
| 22 | |
| 23 | #include <ostream> |
| 24 | #include <set> |
| 25 | #include <sstream> |
| 26 | #include <tuple> |
| 27 | |
| 28 | namespace aapt { |
| 29 | |
| 30 | // The number of attributes to emit per line in a Styleable array. |
| 31 | constexpr size_t kAttribsPerLine = 4; |
| 32 | |
| 33 | JavaClassGenerator::JavaClassGenerator(std::shared_ptr<const ResourceTable> table, |
| 34 | Options options) : |
| 35 | mTable(table), mOptions(options) { |
| 36 | } |
| 37 | |
| 38 | static void generateHeader(std::ostream& out, const StringPiece16& package) { |
| 39 | out << "/* AUTO-GENERATED FILE. DO NOT MODIFY.\n" |
| 40 | " *\n" |
| 41 | " * This class was automatically generated by the\n" |
| 42 | " * aapt tool from the resource data it found. It\n" |
| 43 | " * should not be modified by hand.\n" |
| 44 | " */\n\n"; |
| 45 | out << "package " << package << ";" |
| 46 | << std::endl |
| 47 | << std::endl; |
| 48 | } |
| 49 | |
| 50 | static const std::set<StringPiece16> sJavaIdentifiers = { |
| 51 | u"abstract", u"assert", u"boolean", u"break", u"byte", |
| 52 | u"case", u"catch", u"char", u"class", u"const", u"continue", |
| 53 | u"default", u"do", u"double", u"else", u"enum", u"extends", |
| 54 | u"final", u"finally", u"float", u"for", u"goto", u"if", |
| 55 | u"implements", u"import", u"instanceof", u"int", u"interface", |
| 56 | u"long", u"native", u"new", u"package", u"private", u"protected", |
| 57 | u"public", u"return", u"short", u"static", u"strictfp", u"super", |
| 58 | u"switch", u"synchronized", u"this", u"throw", u"throws", |
| 59 | u"transient", u"try", u"void", u"volatile", u"while", u"true", |
| 60 | u"false", u"null" |
| 61 | }; |
| 62 | |
| 63 | static bool isValidSymbol(const StringPiece16& symbol) { |
| 64 | return sJavaIdentifiers.find(symbol) == sJavaIdentifiers.end(); |
| 65 | } |
| 66 | |
| 67 | /* |
| 68 | * Java symbols can not contain . or -, but those are valid in a resource name. |
| 69 | * Replace those with '_'. |
| 70 | */ |
| 71 | static std::u16string transform(const StringPiece16& symbol) { |
| 72 | std::u16string output = symbol.toString(); |
| 73 | for (char16_t& c : output) { |
| 74 | if (c == u'.' || c == u'-') { |
| 75 | c = u'_'; |
| 76 | } |
| 77 | } |
| 78 | return output; |
| 79 | } |
| 80 | |
| 81 | bool JavaClassGenerator::generateType(std::ostream& out, const ResourceTableType& type, |
| 82 | size_t packageId) { |
| 83 | const StringPiece finalModifier = mOptions.useFinal ? " final" : ""; |
| 84 | |
| 85 | for (const auto& entry : type.entries) { |
| 86 | ResourceId id = { packageId, type.typeId, entry->entryId }; |
| 87 | assert(id.isValid()); |
| 88 | |
| 89 | if (!isValidSymbol(entry->name)) { |
| 90 | mError = (std::stringstream() |
| 91 | << "invalid symbol name '" |
| 92 | << StringPiece16(entry->name) |
| 93 | << "'").str(); |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | out << " " |
| 98 | << "public static" << finalModifier |
| 99 | << " int " << transform(entry->name) << " = " << id << ";" << std::endl; |
| 100 | } |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | struct GenArgs : ValueVisitorArgs { |
| 105 | GenArgs(std::ostream& o, const ResourceEntry& e) : out(o), entry(e) { |
| 106 | } |
| 107 | |
| 108 | std::ostream& out; |
| 109 | const ResourceEntry& entry; |
| 110 | }; |
| 111 | |
| 112 | void JavaClassGenerator::visit(const Styleable& styleable, ValueVisitorArgs& a) { |
| 113 | const StringPiece finalModifier = mOptions.useFinal ? " final" : ""; |
| 114 | std::ostream& out = static_cast<GenArgs&>(a).out; |
| 115 | const ResourceEntry& entry = static_cast<GenArgs&>(a).entry; |
| 116 | |
| 117 | // This must be sorted by resource ID. |
| 118 | std::vector<std::pair<ResourceId, StringPiece16>> sortedAttributes; |
| 119 | sortedAttributes.reserve(styleable.entries.size()); |
| 120 | for (const auto& attr : styleable.entries) { |
| 121 | assert(attr.id.isValid() && "no ID set for Styleable entry"); |
| 122 | assert(attr.name.isValid() && "no name set for Styleable entry"); |
| 123 | sortedAttributes.emplace_back(attr.id, attr.name.entry); |
| 124 | } |
| 125 | std::sort(sortedAttributes.begin(), sortedAttributes.end()); |
| 126 | |
| 127 | // First we emit the array containing the IDs of each attribute. |
| 128 | out << " " |
| 129 | << "public static final int[] " << transform(entry.name) << " = {"; |
| 130 | |
| 131 | const size_t attrCount = sortedAttributes.size(); |
| 132 | for (size_t i = 0; i < attrCount; i++) { |
| 133 | if (i % kAttribsPerLine == 0) { |
| 134 | out << std::endl << " "; |
| 135 | } |
| 136 | |
| 137 | out << sortedAttributes[i].first; |
| 138 | if (i != attrCount - 1) { |
| 139 | out << ", "; |
| 140 | } |
| 141 | } |
| 142 | out << std::endl << " };" << std::endl; |
| 143 | |
| 144 | // Now we emit the indices into the array. |
| 145 | for (size_t i = 0; i < attrCount; i++) { |
| 146 | out << " " |
| 147 | << "public static" << finalModifier |
| 148 | << " int " << transform(entry.name) << "_" << transform(sortedAttributes[i].second) |
| 149 | << " = " << i << ";" << std::endl; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | bool JavaClassGenerator::generate(std::ostream& out) { |
| 154 | const size_t packageId = mTable->getPackageId(); |
| 155 | |
| 156 | generateHeader(out, mTable->getPackage()); |
| 157 | |
| 158 | out << "public final class R {" << std::endl; |
| 159 | |
| 160 | for (const auto& type : *mTable) { |
| 161 | out << " public static final class " << type->type << " {" << std::endl; |
| 162 | bool result; |
| 163 | if (type->type == ResourceType::kStyleable) { |
| 164 | for (const auto& entry : type->entries) { |
| 165 | assert(!entry->values.empty()); |
| 166 | if (!isValidSymbol(entry->name)) { |
| 167 | mError = (std::stringstream() |
| 168 | << "invalid symbol name '" |
| 169 | << StringPiece16(entry->name) |
| 170 | << "'").str(); |
| 171 | return false; |
| 172 | } |
| 173 | entry->values.front().value->accept(*this, GenArgs{ out, *entry }); |
| 174 | } |
| 175 | } else { |
| 176 | result = generateType(out, *type, packageId); |
| 177 | } |
| 178 | |
| 179 | if (!result) { |
| 180 | return false; |
| 181 | } |
| 182 | out << " }" << std::endl; |
| 183 | } |
| 184 | |
| 185 | out << "}" << std::endl; |
| 186 | return true; |
| 187 | } |
| 188 | |
| 189 | } // namespace aapt |