blob: a0db41baecb42e4f2bb66b54d5a2258d833dfa8b [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
Adam Lesinskice5e56e2016-10-21 17:56:45 -070021#include "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
Ryan Mitchell4382e442021-07-14 12:53:01 -070031static std::optional<std::string> ExtractJavaIdentifier(IDiagnostics* diag, const Source& source,
32 const std::string& value) {
Pirama Arumuga Nainar9ba5cb42018-09-24 15:20:15 -070033 std::string result = value;
Adam Lesinski09f4d702017-08-08 10:39:55 -070034 size_t pos = value.rfind('.');
35 if (pos != std::string::npos) {
36 result = result.substr(pos + 1);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070037 }
Adam Lesinskica5638f2015-10-21 14:42:43 -070038
Izabela Orlowskad31bc122018-02-12 11:03:42 +000039 // Normalize only the java identifier, leave the original value unchanged.
Chih-Hung Hsiehf2ef6572020-02-11 14:27:11 -080040 if (result.find('-') != std::string::npos) {
Izabela Orlowskad31bc122018-02-12 11:03:42 +000041 result = JavaClassGenerator::TransformToFieldName(result);
42 }
43
Adam Lesinskice5e56e2016-10-21 17:56:45 -070044 if (result.empty()) {
45 diag->Error(DiagMessage(source) << "empty symbol");
46 return {};
47 }
Adam Lesinskica5638f2015-10-21 14:42:43 -070048
Adam Lesinski09f4d702017-08-08 10:39:55 -070049 if (!IsJavaIdentifier(result)) {
50 diag->Error(DiagMessage(source) << "invalid Java identifier '" << result << "'");
Adam Lesinskice5e56e2016-10-21 17:56:45 -070051 return {};
52 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070053 return result;
Adam Lesinskica5638f2015-10-21 14:42:43 -070054}
55
Adam Lesinski09f4d702017-08-08 10:39:55 -070056static bool WriteSymbol(const Source& source, IDiagnostics* diag, xml::Element* el,
57 ClassDefinition* class_def) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070058 xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "name");
59 if (!attr) {
Adam Lesinski09f4d702017-08-08 10:39:55 -070060 diag->Error(DiagMessage(source) << "<" << el->name << "> must define 'android:name'");
Adam Lesinskice5e56e2016-10-21 17:56:45 -070061 return false;
62 }
Adam Lesinskica5638f2015-10-21 14:42:43 -070063
Ryan Mitchell4382e442021-07-14 12:53:01 -070064 std::optional<std::string> result =
Adam Lesinski09f4d702017-08-08 10:39:55 -070065 ExtractJavaIdentifier(diag, source.WithLine(el->line_number), attr->value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070066 if (!result) {
67 return false;
68 }
Adam Lesinskica5638f2015-10-21 14:42:43 -070069
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070 std::unique_ptr<StringMember> string_member =
71 util::make_unique<StringMember>(result.value(), attr->value);
72 string_member->GetCommentBuilder()->AppendComment(el->comment);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070073
Adam Lesinskif852dd02017-08-18 19:49:58 -070074 if (class_def->AddMember(std::move(string_member)) == ClassDefinition::Result::kOverridden) {
75 diag->Warn(DiagMessage(source.WithLine(el->line_number))
76 << "duplicate definitions of '" << result.value() << "', overriding previous");
77 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078 return true;
Adam Lesinskica5638f2015-10-21 14:42:43 -070079}
80
Adam Lesinski09f4d702017-08-08 10:39:55 -070081std::unique_ptr<ClassDefinition> GenerateManifestClass(IDiagnostics* diag, xml::XmlResource* res) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070082 xml::Element* el = xml::FindRootElement(res->root.get());
83 if (!el) {
84 diag->Error(DiagMessage(res->file.source) << "no root tag defined");
85 return {};
86 }
87
88 if (el->name != "manifest" && !el->namespace_uri.empty()) {
Adam Lesinski09f4d702017-08-08 10:39:55 -070089 diag->Error(DiagMessage(res->file.source) << "no <manifest> root tag defined");
Adam Lesinskice5e56e2016-10-21 17:56:45 -070090 return {};
91 }
92
93 std::unique_ptr<ClassDefinition> permission_class =
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080094 util::make_unique<ClassDefinition>("permission", ClassQualifier::kStatic, false);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070095 std::unique_ptr<ClassDefinition> permission_group_class =
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080096 util::make_unique<ClassDefinition>("permission_group", ClassQualifier::kStatic, false);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070097
98 bool error = false;
99 std::vector<xml::Element*> children = el->GetChildElements();
100 for (xml::Element* child_el : children) {
101 if (child_el->namespace_uri.empty()) {
102 if (child_el->name == "permission") {
Adam Lesinski09f4d702017-08-08 10:39:55 -0700103 error |= !WriteSymbol(res->file.source, diag, child_el, permission_class.get());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700104 } else if (child_el->name == "permission-group") {
Adam Lesinski09f4d702017-08-08 10:39:55 -0700105 error |= !WriteSymbol(res->file.source, diag, child_el, permission_group_class.get());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700106 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700107 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700108 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700109
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700110 if (error) {
111 return {};
112 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700113
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700114 std::unique_ptr<ClassDefinition> manifest_class =
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800115 util::make_unique<ClassDefinition>("Manifest", ClassQualifier::kNone, false);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700116 manifest_class->AddMember(std::move(permission_class));
117 manifest_class->AddMember(std::move(permission_group_class));
118 return manifest_class;
Adam Lesinskica5638f2015-10-21 14:42:43 -0700119}
120
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700121} // namespace aapt