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 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 17 | #include "NameMangler.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 18 | #include "Resource.h" |
| 19 | #include "ResourceTable.h" |
| 20 | #include "ResourceValues.h" |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 21 | #include "ValueVisitor.h" |
| 22 | |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 23 | #include "java/AnnotationProcessor.h" |
| 24 | #include "java/JavaClassGenerator.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 25 | #include "util/StringPiece.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 26 | |
Adam Lesinski | ca2fc35 | 2015-04-03 12:08:26 -0700 | [diff] [blame] | 27 | #include <algorithm> |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 28 | #include <ostream> |
| 29 | #include <set> |
| 30 | #include <sstream> |
| 31 | #include <tuple> |
| 32 | |
| 33 | namespace aapt { |
| 34 | |
| 35 | // The number of attributes to emit per line in a Styleable array. |
| 36 | constexpr size_t kAttribsPerLine = 4; |
| 37 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 38 | JavaClassGenerator::JavaClassGenerator(ResourceTable* table, JavaClassGeneratorOptions options) : |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 39 | mTable(table), mOptions(options) { |
| 40 | } |
| 41 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 42 | static void generateHeader(const StringPiece16& packageNameToGenerate, std::ostream* out) { |
| 43 | *out << "/* AUTO-GENERATED FILE. DO NOT MODIFY.\n" |
| 44 | " *\n" |
| 45 | " * This class was automatically generated by the\n" |
| 46 | " * aapt tool from the resource data it found. It\n" |
| 47 | " * should not be modified by hand.\n" |
| 48 | " */\n\n" |
| 49 | "package " << packageNameToGenerate << ";\n\n"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 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 | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 83 | bool JavaClassGenerator::skipSymbol(SymbolState state) { |
| 84 | switch (mOptions.types) { |
| 85 | case JavaClassGeneratorOptions::SymbolTypes::kAll: |
| 86 | return false; |
| 87 | case JavaClassGeneratorOptions::SymbolTypes::kPublicPrivate: |
| 88 | return state == SymbolState::kUndefined; |
| 89 | case JavaClassGeneratorOptions::SymbolTypes::kPublic: |
| 90 | return state != SymbolState::kPublic; |
| 91 | } |
| 92 | return true; |
| 93 | } |
| 94 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 95 | void JavaClassGenerator::generateStyleable(const StringPiece16& packageNameToGenerate, |
| 96 | const std::u16string& entryName, |
| 97 | const Styleable* styleable, |
| 98 | std::ostream* out) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 99 | const StringPiece finalModifier = mOptions.useFinal ? " final" : ""; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 100 | |
| 101 | // This must be sorted by resource ID. |
Adam Lesinski | 838a687 | 2015-05-01 13:14:05 -0700 | [diff] [blame] | 102 | std::vector<std::pair<ResourceId, ResourceNameRef>> sortedAttributes; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 103 | sortedAttributes.reserve(styleable->entries.size()); |
| 104 | for (const auto& attr : styleable->entries) { |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 105 | // If we are not encoding final attributes, the styleable entry may have no ID |
| 106 | // if we are building a static library. |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 107 | assert((!mOptions.useFinal || attr.id) && "no ID set for Styleable entry"); |
| 108 | assert(attr.name && "no name set for Styleable entry"); |
| 109 | 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] | 110 | } |
| 111 | std::sort(sortedAttributes.begin(), sortedAttributes.end()); |
| 112 | |
| 113 | // First we emit the array containing the IDs of each attribute. |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 114 | *out << " " |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 115 | << "public static final int[] " << transform(entryName) << " = {"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 116 | |
| 117 | const size_t attrCount = sortedAttributes.size(); |
| 118 | for (size_t i = 0; i < attrCount; i++) { |
| 119 | if (i % kAttribsPerLine == 0) { |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 120 | *out << "\n "; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 123 | *out << sortedAttributes[i].first; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 124 | if (i != attrCount - 1) { |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 125 | *out << ", "; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 126 | } |
| 127 | } |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 128 | *out << "\n };\n"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 129 | |
| 130 | // Now we emit the indices into the array. |
| 131 | for (size_t i = 0; i < attrCount; i++) { |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 132 | *out << " " |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 133 | << "public static" << finalModifier |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 134 | << " int " << transform(entryName); |
Adam Lesinski | 838a687 | 2015-05-01 13:14:05 -0700 | [diff] [blame] | 135 | |
| 136 | // We may reference IDs from other packages, so prefix the entry name with |
| 137 | // the package. |
| 138 | const ResourceNameRef& itemName = sortedAttributes[i].second; |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 139 | if (!itemName.package.empty() && packageNameToGenerate != itemName.package) { |
Adam Lesinski | 838a687 | 2015-05-01 13:14:05 -0700 | [diff] [blame] | 140 | *out << "_" << transform(itemName.package); |
| 141 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 142 | *out << "_" << transform(itemName.entry) << " = " << i << ";\n"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 146 | static void addAttributeFormatDoc(AnnotationProcessor* processor, Attribute* attr) { |
| 147 | const uint32_t typeMask = attr->typeMask; |
| 148 | if (typeMask & android::ResTable_map::TYPE_REFERENCE) { |
| 149 | processor->appendComment( |
| 150 | "<p>May be a reference to another resource, in the form\n" |
| 151 | "\"<code>@[+][<i>package</i>:]<i>type</i>/<i>name</i></code>\" or a theme\n" |
| 152 | "attribute in the form\n" |
| 153 | "\"<code>?[<i>package</i>:]<i>type</i>/<i>name</i></code>\"."); |
| 154 | } |
| 155 | |
| 156 | if (typeMask & android::ResTable_map::TYPE_STRING) { |
| 157 | processor->appendComment( |
| 158 | "<p>May be a string value, using '\\;' to escape characters such as\n" |
| 159 | "'\\n' or '\\uxxxx' for a unicode character;"); |
| 160 | } |
| 161 | |
| 162 | if (typeMask & android::ResTable_map::TYPE_INTEGER) { |
| 163 | processor->appendComment("<p>May be an integer value, such as \"<code>100</code>\"."); |
| 164 | } |
| 165 | |
| 166 | if (typeMask & android::ResTable_map::TYPE_BOOLEAN) { |
| 167 | processor->appendComment( |
| 168 | "<p>May be a boolean value, such as \"<code>true</code>\" or\n" |
| 169 | "\"<code>false</code>\"."); |
| 170 | } |
| 171 | |
| 172 | if (typeMask & android::ResTable_map::TYPE_COLOR) { |
| 173 | processor->appendComment( |
| 174 | "<p>May be a color value, in the form of \"<code>#<i>rgb</i></code>\",\n" |
| 175 | "\"<code>#<i>argb</i></code>\", \"<code>#<i>rrggbb</i></code\", or \n" |
| 176 | "\"<code>#<i>aarrggbb</i></code>\"."); |
| 177 | } |
| 178 | |
| 179 | if (typeMask & android::ResTable_map::TYPE_FLOAT) { |
| 180 | processor->appendComment( |
| 181 | "<p>May be a floating point value, such as \"<code>1.2</code>\"."); |
| 182 | } |
| 183 | |
| 184 | if (typeMask & android::ResTable_map::TYPE_DIMENSION) { |
| 185 | processor->appendComment( |
| 186 | "<p>May be a dimension value, which is a floating point number appended with a\n" |
| 187 | "unit such as \"<code>14.5sp</code>\".\n" |
| 188 | "Available units are: px (pixels), dp (density-independent pixels),\n" |
| 189 | "sp (scaled pixels based on preferred font size), in (inches), and\n" |
| 190 | "mm (millimeters)."); |
| 191 | } |
| 192 | |
| 193 | if (typeMask & android::ResTable_map::TYPE_FRACTION) { |
| 194 | processor->appendComment( |
| 195 | "<p>May be a fractional value, which is a floating point number appended with\n" |
| 196 | "either % or %p, such as \"<code>14.5%</code>\".\n" |
| 197 | "The % suffix always means a percentage of the base size;\n" |
| 198 | "the optional %p suffix provides a size relative to some parent container."); |
| 199 | } |
| 200 | |
| 201 | if (typeMask & (android::ResTable_map::TYPE_FLAGS | android::ResTable_map::TYPE_ENUM)) { |
| 202 | if (typeMask & android::ResTable_map::TYPE_FLAGS) { |
| 203 | processor->appendComment( |
| 204 | "<p>Must be one or more (separated by '|') of the following " |
| 205 | "constant values.</p>"); |
| 206 | } else { |
| 207 | processor->appendComment("<p>Must be one of the following constant values.</p>"); |
| 208 | } |
| 209 | |
| 210 | processor->appendComment("<table>\n<colgroup align=\"left\" />\n" |
| 211 | "<colgroup align=\"left\" />\n" |
| 212 | "<colgroup align=\"left\" />\n" |
| 213 | "<tr><th>Constant</th><th>Value</th><th>Description</th></tr>\n"); |
| 214 | for (const Attribute::Symbol& symbol : attr->symbols) { |
| 215 | std::stringstream line; |
| 216 | line << "<tr><td>" << symbol.symbol.name.value().entry << "</td>" |
| 217 | << "<td>" << std::hex << symbol.value << std::dec << "</td>" |
| 218 | << "<td>" << util::trimWhitespace(symbol.symbol.getComment()) << "</td></tr>"; |
| 219 | processor->appendComment(line.str()); |
| 220 | } |
| 221 | processor->appendComment("</table>"); |
| 222 | } |
| 223 | } |
| 224 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 225 | bool JavaClassGenerator::generateType(const StringPiece16& packageNameToGenerate, |
| 226 | const ResourceTablePackage* package, |
| 227 | const ResourceTableType* type, |
| 228 | std::ostream* out) { |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 229 | const StringPiece finalModifier = mOptions.useFinal ? " final" : ""; |
| 230 | |
| 231 | std::u16string unmangledPackage; |
| 232 | std::u16string unmangledName; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 233 | for (const auto& entry : type->entries) { |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 234 | if (skipSymbol(entry->symbolStatus.state)) { |
| 235 | continue; |
| 236 | } |
| 237 | |
| 238 | ResourceId id(package->id.value(), type->id.value(), entry->id.value()); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 239 | assert(id.isValid()); |
| 240 | |
| 241 | unmangledName = entry->name; |
| 242 | if (NameMangler::unmangle(&unmangledName, &unmangledPackage)) { |
| 243 | // The entry name was mangled, and we successfully unmangled it. |
| 244 | // Check that we want to emit this symbol. |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 245 | if (package->name != unmangledPackage) { |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 246 | // Skip the entry if it doesn't belong to the package we're writing. |
| 247 | continue; |
| 248 | } |
| 249 | } else { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 250 | if (packageNameToGenerate != package->name) { |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 251 | // We are processing a mangled package name, |
| 252 | // but this is a non-mangled resource. |
| 253 | continue; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | if (!isValidSymbol(unmangledName)) { |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 258 | ResourceNameRef resourceName(packageNameToGenerate, type->type, unmangledName); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 259 | std::stringstream err; |
| 260 | err << "invalid symbol name '" << resourceName << "'"; |
| 261 | mError = err.str(); |
| 262 | return false; |
| 263 | } |
| 264 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 265 | if (type->type == ResourceType::kStyleable) { |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 266 | assert(!entry->values.empty()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 267 | generateStyleable(packageNameToGenerate, unmangledName, static_cast<const Styleable*>( |
| 268 | entry->values.front().value.get()), out); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 269 | } else { |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 270 | AnnotationProcessor processor(" "); |
| 271 | if (entry->symbolStatus.state != SymbolState::kUndefined) { |
| 272 | processor.appendComment(entry->symbolStatus.comment); |
| 273 | } |
| 274 | |
| 275 | for (const auto& configValue : entry->values) { |
| 276 | processor.appendComment(configValue.value->getComment()); |
| 277 | } |
| 278 | |
| 279 | if (!entry->values.empty()) { |
| 280 | if (Attribute* attr = valueCast<Attribute>(entry->values.front().value.get())) { |
| 281 | // We list out the available values for the given attribute. |
| 282 | addAttributeFormatDoc(&processor, attr); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | std::string comment = processor.buildComment(); |
| 287 | if (!comment.empty()) { |
| 288 | *out << comment << "\n"; |
| 289 | } |
| 290 | |
| 291 | std::string annotations = processor.buildAnnotations(); |
| 292 | if (!annotations.empty()) { |
| 293 | *out << annotations << "\n"; |
| 294 | } |
| 295 | |
| 296 | *out << " " << "public static" << finalModifier |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 297 | << " int " << transform(unmangledName) << " = " << id << ";\n"; |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | return true; |
| 301 | } |
| 302 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 303 | bool JavaClassGenerator::generate(const StringPiece16& packageNameToGenerate, std::ostream* out) { |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 304 | return generate(packageNameToGenerate, packageNameToGenerate, out); |
| 305 | } |
| 306 | |
| 307 | bool JavaClassGenerator::generate(const StringPiece16& packageNameToGenerate, |
| 308 | const StringPiece16& outPackageName, std::ostream* out) { |
| 309 | generateHeader(outPackageName, out); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 310 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 311 | *out << "public final class R {\n"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 312 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 313 | for (const auto& package : mTable->packages) { |
| 314 | for (const auto& type : package->types) { |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 315 | StringPiece16 typeStr; |
| 316 | if (type->type == ResourceType::kAttrPrivate) { |
| 317 | typeStr = toString(ResourceType::kAttr); |
| 318 | } else { |
| 319 | typeStr = toString(type->type); |
| 320 | } |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 321 | *out << " public static final class " << typeStr << " {\n"; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 322 | if (!generateType(packageNameToGenerate, package.get(), type.get(), out)) { |
| 323 | return false; |
| 324 | } |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 325 | *out << " }\n"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 326 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 327 | } |
| 328 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 329 | *out << "}\n"; |
| 330 | out->flush(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 331 | return true; |
| 332 | } |
| 333 | |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 334 | |
| 335 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 336 | } // namespace aapt |