blob: 65b63b7c0c6115dc3771bc2779d6898de2e74a00 [file] [log] [blame]
Adam Lesinskica5638f2015-10-21 14:42:43 -07001/*
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 Lesinskica5638f2015-10-21 14:42:43 -070017#include "java/ManifestClassGenerator.h"
Adam Lesinskica5638f2015-10-21 14:42:43 -070018
19#include <algorithm>
20
Jeremy Meyer56f36e82022-05-20 20:35:42 +000021#include "androidfw/Source.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070022#include "java/ClassDefinition.h"
Izabela Orlowskad31bc122018-02-12 11:03:42 +000023#include "java/JavaClassGenerator.h"
Adam Lesinski09f4d702017-08-08 10:39:55 -070024#include "text/Unicode.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070025#include "xml/XmlDom.h"
26
Adam Lesinski09f4d702017-08-08 10:39:55 -070027using ::aapt::text::IsJavaIdentifier;
Adam Lesinskid5083f62017-01-16 15:07:21 -080028
Adam Lesinskica5638f2015-10-21 14:42:43 -070029namespace aapt {
30
Jeremy Meyer56f36e82022-05-20 20:35:42 +000031static std::optional<std::string> ExtractJavaIdentifier(android::IDiagnostics* diag,
32 const android::Source& source,
Ryan Mitchell4382e442021-07-14 12:53:01 -070033 const std::string& value) {
Pirama Arumuga Nainar9ba5cb42018-09-24 15:20:15 -070034 std::string result = value;
Adam Lesinski09f4d702017-08-08 10:39:55 -070035 size_t pos = value.rfind('.');
36 if (pos != std::string::npos) {
37 result = result.substr(pos + 1);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070038 }
Adam Lesinskica5638f2015-10-21 14:42:43 -070039
Izabela Orlowskad31bc122018-02-12 11:03:42 +000040 // Normalize only the java identifier, leave the original value unchanged.
Chih-Hung Hsiehf2ef6572020-02-11 14:27:11 -080041 if (result.find('-') != std::string::npos) {
Izabela Orlowskad31bc122018-02-12 11:03:42 +000042 result = JavaClassGenerator::TransformToFieldName(result);
43 }
44
Adam Lesinskice5e56e2016-10-21 17:56:45 -070045 if (result.empty()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +000046 diag->Error(android::DiagMessage(source) << "empty symbol");
Adam Lesinskice5e56e2016-10-21 17:56:45 -070047 return {};
48 }
Adam Lesinskica5638f2015-10-21 14:42:43 -070049
Adam Lesinski09f4d702017-08-08 10:39:55 -070050 if (!IsJavaIdentifier(result)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +000051 diag->Error(android::DiagMessage(source) << "invalid Java identifier '" << result << "'");
Adam Lesinskice5e56e2016-10-21 17:56:45 -070052 return {};
53 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070054 return result;
Adam Lesinskica5638f2015-10-21 14:42:43 -070055}
56
Jeremy Meyer56f36e82022-05-20 20:35:42 +000057static bool WriteSymbol(const android::Source& source, android::IDiagnostics* diag,
58 xml::Element* el, ClassDefinition* class_def) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059 xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "name");
60 if (!attr) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +000061 diag->Error(android::DiagMessage(source) << "<" << el->name << "> must define 'android:name'");
Adam Lesinskice5e56e2016-10-21 17:56:45 -070062 return false;
63 }
Adam Lesinskica5638f2015-10-21 14:42:43 -070064
Ryan Mitchell4382e442021-07-14 12:53:01 -070065 std::optional<std::string> result =
Adam Lesinski09f4d702017-08-08 10:39:55 -070066 ExtractJavaIdentifier(diag, source.WithLine(el->line_number), attr->value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070067 if (!result) {
68 return false;
69 }
Adam Lesinskica5638f2015-10-21 14:42:43 -070070
Adam Lesinskice5e56e2016-10-21 17:56:45 -070071 std::unique_ptr<StringMember> string_member =
72 util::make_unique<StringMember>(result.value(), attr->value);
73 string_member->GetCommentBuilder()->AppendComment(el->comment);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070074
Adam Lesinskif852dd02017-08-18 19:49:58 -070075 if (class_def->AddMember(std::move(string_member)) == ClassDefinition::Result::kOverridden) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +000076 diag->Warn(android::DiagMessage(source.WithLine(el->line_number))
Adam Lesinskif852dd02017-08-18 19:49:58 -070077 << "duplicate definitions of '" << result.value() << "', overriding previous");
78 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079 return true;
Adam Lesinskica5638f2015-10-21 14:42:43 -070080}
81
Jeremy Meyer56f36e82022-05-20 20:35:42 +000082std::unique_ptr<ClassDefinition> GenerateManifestClass(android::IDiagnostics* diag,
83 xml::XmlResource* res) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070084 xml::Element* el = xml::FindRootElement(res->root.get());
85 if (!el) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +000086 diag->Error(android::DiagMessage(res->file.source) << "no root tag defined");
Adam Lesinskice5e56e2016-10-21 17:56:45 -070087 return {};
88 }
89
90 if (el->name != "manifest" && !el->namespace_uri.empty()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +000091 diag->Error(android::DiagMessage(res->file.source) << "no <manifest> root tag defined");
Adam Lesinskice5e56e2016-10-21 17:56:45 -070092 return {};
93 }
94
95 std::unique_ptr<ClassDefinition> permission_class =
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080096 util::make_unique<ClassDefinition>("permission", ClassQualifier::kStatic, false);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070097 std::unique_ptr<ClassDefinition> permission_group_class =
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080098 util::make_unique<ClassDefinition>("permission_group", ClassQualifier::kStatic, false);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070099
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 Lesinski09f4d702017-08-08 10:39:55 -0700105 error |= !WriteSymbol(res->file.source, diag, child_el, permission_class.get());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700106 } else if (child_el->name == "permission-group") {
Adam Lesinski09f4d702017-08-08 10:39:55 -0700107 error |= !WriteSymbol(res->file.source, diag, child_el, permission_group_class.get());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700108 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700109 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700110 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700111
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700112 if (error) {
113 return {};
114 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700115
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700116 std::unique_ptr<ClassDefinition> manifest_class =
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800117 util::make_unique<ClassDefinition>("Manifest", ClassQualifier::kNone, false);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700118 manifest_class->AddMember(std::move(permission_class));
119 manifest_class->AddMember(std::move(permission_group_class));
120 return manifest_class;
Adam Lesinskica5638f2015-10-21 14:42:43 -0700121}
122
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700123} // namespace aapt