blob: be67c9c8c03c9e694f071d4e8348093f11095cad [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 ::android::StringPiece;
30using ::aapt::text::IsJavaIdentifier;
Adam Lesinskid5083f62017-01-16 15:07:21 -080031
Adam Lesinskica5638f2015-10-21 14:42:43 -070032namespace aapt {
33
Adam Lesinski09f4d702017-08-08 10:39:55 -070034static Maybe<StringPiece> ExtractJavaIdentifier(IDiagnostics* diag, const Source& source,
35 const std::string& value) {
36 StringPiece result = value;
37 size_t pos = value.rfind('.');
38 if (pos != std::string::npos) {
39 result = result.substr(pos + 1);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070040 }
Adam Lesinskica5638f2015-10-21 14:42:43 -070041
Izabela Orlowskad31bc122018-02-12 11:03:42 +000042 // Normalize only the java identifier, leave the original value unchanged.
43 if (result.contains("-")) {
44 result = JavaClassGenerator::TransformToFieldName(result);
45 }
46
Adam Lesinskice5e56e2016-10-21 17:56:45 -070047 if (result.empty()) {
48 diag->Error(DiagMessage(source) << "empty symbol");
49 return {};
50 }
Adam Lesinskica5638f2015-10-21 14:42:43 -070051
Adam Lesinski09f4d702017-08-08 10:39:55 -070052 if (!IsJavaIdentifier(result)) {
53 diag->Error(DiagMessage(source) << "invalid Java identifier '" << result << "'");
Adam Lesinskice5e56e2016-10-21 17:56:45 -070054 return {};
55 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070056 return result;
Adam Lesinskica5638f2015-10-21 14:42:43 -070057}
58
Adam Lesinski09f4d702017-08-08 10:39:55 -070059static bool WriteSymbol(const Source& source, IDiagnostics* diag, xml::Element* el,
60 ClassDefinition* class_def) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070061 xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "name");
62 if (!attr) {
Adam Lesinski09f4d702017-08-08 10:39:55 -070063 diag->Error(DiagMessage(source) << "<" << el->name << "> must define 'android:name'");
Adam Lesinskice5e56e2016-10-21 17:56:45 -070064 return false;
65 }
Adam Lesinskica5638f2015-10-21 14:42:43 -070066
Adam Lesinski09f4d702017-08-08 10:39:55 -070067 Maybe<StringPiece> result =
68 ExtractJavaIdentifier(diag, source.WithLine(el->line_number), attr->value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070069 if (!result) {
70 return false;
71 }
Adam Lesinskica5638f2015-10-21 14:42:43 -070072
Adam Lesinskice5e56e2016-10-21 17:56:45 -070073 std::unique_ptr<StringMember> string_member =
74 util::make_unique<StringMember>(result.value(), attr->value);
75 string_member->GetCommentBuilder()->AppendComment(el->comment);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070076
Adam Lesinskif852dd02017-08-18 19:49:58 -070077 if (class_def->AddMember(std::move(string_member)) == ClassDefinition::Result::kOverridden) {
78 diag->Warn(DiagMessage(source.WithLine(el->line_number))
79 << "duplicate definitions of '" << result.value() << "', overriding previous");
80 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070081 return true;
Adam Lesinskica5638f2015-10-21 14:42:43 -070082}
83
Adam Lesinski09f4d702017-08-08 10:39:55 -070084std::unique_ptr<ClassDefinition> GenerateManifestClass(IDiagnostics* diag, xml::XmlResource* res) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070085 xml::Element* el = xml::FindRootElement(res->root.get());
86 if (!el) {
87 diag->Error(DiagMessage(res->file.source) << "no root tag defined");
88 return {};
89 }
90
91 if (el->name != "manifest" && !el->namespace_uri.empty()) {
Adam Lesinski09f4d702017-08-08 10:39:55 -070092 diag->Error(DiagMessage(res->file.source) << "no <manifest> root tag defined");
Adam Lesinskice5e56e2016-10-21 17:56:45 -070093 return {};
94 }
95
96 std::unique_ptr<ClassDefinition> permission_class =
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080097 util::make_unique<ClassDefinition>("permission", ClassQualifier::kStatic, false);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070098 std::unique_ptr<ClassDefinition> permission_group_class =
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080099 util::make_unique<ClassDefinition>("permission_group", ClassQualifier::kStatic, false);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700100
101 bool error = false;
102 std::vector<xml::Element*> children = el->GetChildElements();
103 for (xml::Element* child_el : children) {
104 if (child_el->namespace_uri.empty()) {
105 if (child_el->name == "permission") {
Adam Lesinski09f4d702017-08-08 10:39:55 -0700106 error |= !WriteSymbol(res->file.source, diag, child_el, permission_class.get());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700107 } else if (child_el->name == "permission-group") {
Adam Lesinski09f4d702017-08-08 10:39:55 -0700108 error |= !WriteSymbol(res->file.source, diag, child_el, permission_group_class.get());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700109 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700110 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700111 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700112
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700113 if (error) {
114 return {};
115 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700116
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700117 std::unique_ptr<ClassDefinition> manifest_class =
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800118 util::make_unique<ClassDefinition>("Manifest", ClassQualifier::kNone, false);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700119 manifest_class->AddMember(std::move(permission_class));
120 manifest_class->AddMember(std::move(permission_group_class));
121 return manifest_class;
Adam Lesinskica5638f2015-10-21 14:42:43 -0700122}
123
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700124} // namespace aapt