Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [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 | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 17 | #include "java/ManifestClassGenerator.h" |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 18 | |
| 19 | #include <algorithm> |
| 20 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame^] | 21 | #include "androidfw/Source.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 22 | #include "java/ClassDefinition.h" |
Izabela Orlowska | d31bc12 | 2018-02-12 11:03:42 +0000 | [diff] [blame] | 23 | #include "java/JavaClassGenerator.h" |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 24 | #include "text/Unicode.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 25 | #include "xml/XmlDom.h" |
| 26 | |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 27 | using ::aapt::text::IsJavaIdentifier; |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 28 | |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 29 | namespace aapt { |
| 30 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame^] | 31 | static std::optional<std::string> ExtractJavaIdentifier(android::IDiagnostics* diag, |
| 32 | const android::Source& source, |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 33 | const std::string& value) { |
Pirama Arumuga Nainar | 9ba5cb4 | 2018-09-24 15:20:15 -0700 | [diff] [blame] | 34 | std::string result = value; |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 35 | size_t pos = value.rfind('.'); |
| 36 | if (pos != std::string::npos) { |
| 37 | result = result.substr(pos + 1); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 38 | } |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 39 | |
Izabela Orlowska | d31bc12 | 2018-02-12 11:03:42 +0000 | [diff] [blame] | 40 | // Normalize only the java identifier, leave the original value unchanged. |
Chih-Hung Hsieh | f2ef657 | 2020-02-11 14:27:11 -0800 | [diff] [blame] | 41 | if (result.find('-') != std::string::npos) { |
Izabela Orlowska | d31bc12 | 2018-02-12 11:03:42 +0000 | [diff] [blame] | 42 | result = JavaClassGenerator::TransformToFieldName(result); |
| 43 | } |
| 44 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 45 | if (result.empty()) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame^] | 46 | diag->Error(android::DiagMessage(source) << "empty symbol"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 47 | return {}; |
| 48 | } |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 49 | |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 50 | if (!IsJavaIdentifier(result)) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame^] | 51 | diag->Error(android::DiagMessage(source) << "invalid Java identifier '" << result << "'"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 52 | return {}; |
| 53 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 54 | return result; |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame^] | 57 | static bool WriteSymbol(const android::Source& source, android::IDiagnostics* diag, |
| 58 | xml::Element* el, ClassDefinition* class_def) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 59 | xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "name"); |
| 60 | if (!attr) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame^] | 61 | diag->Error(android::DiagMessage(source) << "<" << el->name << "> must define 'android:name'"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 62 | return false; |
| 63 | } |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 64 | |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 65 | std::optional<std::string> result = |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 66 | ExtractJavaIdentifier(diag, source.WithLine(el->line_number), attr->value); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 67 | if (!result) { |
| 68 | return false; |
| 69 | } |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 70 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 71 | std::unique_ptr<StringMember> string_member = |
| 72 | util::make_unique<StringMember>(result.value(), attr->value); |
| 73 | string_member->GetCommentBuilder()->AppendComment(el->comment); |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 74 | |
Adam Lesinski | f852dd0 | 2017-08-18 19:49:58 -0700 | [diff] [blame] | 75 | if (class_def->AddMember(std::move(string_member)) == ClassDefinition::Result::kOverridden) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame^] | 76 | diag->Warn(android::DiagMessage(source.WithLine(el->line_number)) |
Adam Lesinski | f852dd0 | 2017-08-18 19:49:58 -0700 | [diff] [blame] | 77 | << "duplicate definitions of '" << result.value() << "', overriding previous"); |
| 78 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 79 | return true; |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame^] | 82 | std::unique_ptr<ClassDefinition> GenerateManifestClass(android::IDiagnostics* diag, |
| 83 | xml::XmlResource* res) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 84 | xml::Element* el = xml::FindRootElement(res->root.get()); |
| 85 | if (!el) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame^] | 86 | diag->Error(android::DiagMessage(res->file.source) << "no root tag defined"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 87 | return {}; |
| 88 | } |
| 89 | |
| 90 | if (el->name != "manifest" && !el->namespace_uri.empty()) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame^] | 91 | diag->Error(android::DiagMessage(res->file.source) << "no <manifest> root tag defined"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 92 | return {}; |
| 93 | } |
| 94 | |
| 95 | std::unique_ptr<ClassDefinition> permission_class = |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 96 | util::make_unique<ClassDefinition>("permission", ClassQualifier::kStatic, false); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 97 | std::unique_ptr<ClassDefinition> permission_group_class = |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 98 | util::make_unique<ClassDefinition>("permission_group", ClassQualifier::kStatic, false); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 99 | |
| 100 | bool error = false; |
| 101 | std::vector<xml::Element*> children = el->GetChildElements(); |
| 102 | for (xml::Element* child_el : children) { |
| 103 | if (child_el->namespace_uri.empty()) { |
| 104 | if (child_el->name == "permission") { |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 105 | error |= !WriteSymbol(res->file.source, diag, child_el, permission_class.get()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 106 | } else if (child_el->name == "permission-group") { |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 107 | error |= !WriteSymbol(res->file.source, diag, child_el, permission_group_class.get()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 108 | } |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 109 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 110 | } |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 111 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 112 | if (error) { |
| 113 | return {}; |
| 114 | } |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 115 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 116 | std::unique_ptr<ClassDefinition> manifest_class = |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 117 | util::make_unique<ClassDefinition>("Manifest", ClassQualifier::kNone, false); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 118 | manifest_class->AddMember(std::move(permission_class)); |
| 119 | manifest_class->AddMember(std::move(permission_group_class)); |
| 120 | return manifest_class; |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 123 | } // namespace aapt |