blob: 09ea03b23c9a1ce6aeaf3b159f89e3c029e41884 [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"
22#include "java/AnnotationProcessor.h"
23#include "java/ClassDefinition.h"
Izabela Orlowskad31bc122018-02-12 11:03:42 +000024#include "java/JavaClassGenerator.h"
Adam Lesinski09f4d702017-08-08 10:39:55 -070025#include "text/Unicode.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070026#include "util/Maybe.h"
27#include "xml/XmlDom.h"
28
Adam Lesinski09f4d702017-08-08 10:39:55 -070029using ::aapt::text::IsJavaIdentifier;
Adam Lesinskid5083f62017-01-16 15:07:21 -080030
Adam Lesinskica5638f2015-10-21 14:42:43 -070031namespace aapt {
32
Pirama Arumuga Nainar9ba5cb42018-09-24 15:20:15 -070033static Maybe<std::string> ExtractJavaIdentifier(IDiagnostics* diag, const Source& source,
Adam Lesinski09f4d702017-08-08 10:39:55 -070034 const std::string& value) {
Pirama Arumuga Nainar9ba5cb42018-09-24 15:20:15 -070035 std::string result = value;
Adam Lesinski09f4d702017-08-08 10:39:55 -070036 size_t pos = value.rfind('.');
37 if (pos != std::string::npos) {
38 result = result.substr(pos + 1);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070039 }
Adam Lesinskica5638f2015-10-21 14:42:43 -070040
Izabela Orlowskad31bc122018-02-12 11:03:42 +000041 // Normalize only the java identifier, leave the original value unchanged.
Chih-Hung Hsiehf2ef6572020-02-11 14:27:11 -080042 if (result.find('-') != std::string::npos) {
Izabela Orlowskad31bc122018-02-12 11:03:42 +000043 result = JavaClassGenerator::TransformToFieldName(result);
44 }
45
Adam Lesinskice5e56e2016-10-21 17:56:45 -070046 if (result.empty()) {
47 diag->Error(DiagMessage(source) << "empty symbol");
48 return {};
49 }
Adam Lesinskica5638f2015-10-21 14:42:43 -070050
Adam Lesinski09f4d702017-08-08 10:39:55 -070051 if (!IsJavaIdentifier(result)) {
52 diag->Error(DiagMessage(source) << "invalid Java identifier '" << result << "'");
Adam Lesinskice5e56e2016-10-21 17:56:45 -070053 return {};
54 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070055 return result;
Adam Lesinskica5638f2015-10-21 14:42:43 -070056}
57
Adam Lesinski09f4d702017-08-08 10:39:55 -070058static bool WriteSymbol(const Source& source, IDiagnostics* diag, xml::Element* el,
59 ClassDefinition* class_def) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070060 xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "name");
61 if (!attr) {
Adam Lesinski09f4d702017-08-08 10:39:55 -070062 diag->Error(DiagMessage(source) << "<" << el->name << "> must define 'android:name'");
Adam Lesinskice5e56e2016-10-21 17:56:45 -070063 return false;
64 }
Adam Lesinskica5638f2015-10-21 14:42:43 -070065
Pirama Arumuga Nainar9ba5cb42018-09-24 15:20:15 -070066 Maybe<std::string> result =
Adam Lesinski09f4d702017-08-08 10:39:55 -070067 ExtractJavaIdentifier(diag, source.WithLine(el->line_number), attr->value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070068 if (!result) {
69 return false;
70 }
Adam Lesinskica5638f2015-10-21 14:42:43 -070071
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072 std::unique_ptr<StringMember> string_member =
73 util::make_unique<StringMember>(result.value(), attr->value);
74 string_member->GetCommentBuilder()->AppendComment(el->comment);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070075
Adam Lesinskif852dd02017-08-18 19:49:58 -070076 if (class_def->AddMember(std::move(string_member)) == ClassDefinition::Result::kOverridden) {
77 diag->Warn(DiagMessage(source.WithLine(el->line_number))
78 << "duplicate definitions of '" << result.value() << "', overriding previous");
79 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070080 return true;
Adam Lesinskica5638f2015-10-21 14:42:43 -070081}
82
Adam Lesinski09f4d702017-08-08 10:39:55 -070083std::unique_ptr<ClassDefinition> GenerateManifestClass(IDiagnostics* diag, xml::XmlResource* res) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070084 xml::Element* el = xml::FindRootElement(res->root.get());
85 if (!el) {
86 diag->Error(DiagMessage(res->file.source) << "no root tag defined");
87 return {};
88 }
89
90 if (el->name != "manifest" && !el->namespace_uri.empty()) {
Adam Lesinski09f4d702017-08-08 10:39:55 -070091 diag->Error(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