blob: c4b36176aa71aa14d9fcd3c1695fa19c81a9f551 [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"
Adam Lesinski09f4d702017-08-08 10:39:55 -070024#include "text/Unicode.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070025#include "util/Maybe.h"
26#include "xml/XmlDom.h"
27
Adam Lesinski09f4d702017-08-08 10:39:55 -070028using ::android::StringPiece;
29using ::aapt::text::IsJavaIdentifier;
Adam Lesinskid5083f62017-01-16 15:07:21 -080030
Adam Lesinskica5638f2015-10-21 14:42:43 -070031namespace aapt {
32
Adam Lesinski09f4d702017-08-08 10:39:55 -070033static Maybe<StringPiece> ExtractJavaIdentifier(IDiagnostics* diag, const Source& source,
34 const std::string& value) {
35 StringPiece result = value;
36 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
Adam Lesinskice5e56e2016-10-21 17:56:45 -070041 if (result.empty()) {
42 diag->Error(DiagMessage(source) << "empty symbol");
43 return {};
44 }
Adam Lesinskica5638f2015-10-21 14:42:43 -070045
Adam Lesinski09f4d702017-08-08 10:39:55 -070046 if (!IsJavaIdentifier(result)) {
47 diag->Error(DiagMessage(source) << "invalid Java identifier '" << result << "'");
Adam Lesinskice5e56e2016-10-21 17:56:45 -070048 return {};
49 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070050 return result;
Adam Lesinskica5638f2015-10-21 14:42:43 -070051}
52
Adam Lesinski09f4d702017-08-08 10:39:55 -070053static bool WriteSymbol(const Source& source, IDiagnostics* diag, xml::Element* el,
54 ClassDefinition* class_def) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070055 xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "name");
56 if (!attr) {
Adam Lesinski09f4d702017-08-08 10:39:55 -070057 diag->Error(DiagMessage(source) << "<" << el->name << "> must define 'android:name'");
Adam Lesinskice5e56e2016-10-21 17:56:45 -070058 return false;
59 }
Adam Lesinskica5638f2015-10-21 14:42:43 -070060
Adam Lesinski09f4d702017-08-08 10:39:55 -070061 Maybe<StringPiece> result =
62 ExtractJavaIdentifier(diag, source.WithLine(el->line_number), attr->value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070063 if (!result) {
64 return false;
65 }
Adam Lesinskica5638f2015-10-21 14:42:43 -070066
Adam Lesinskice5e56e2016-10-21 17:56:45 -070067 std::unique_ptr<StringMember> string_member =
68 util::make_unique<StringMember>(result.value(), attr->value);
69 string_member->GetCommentBuilder()->AppendComment(el->comment);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070070
Adam Lesinskif852dd02017-08-18 19:49:58 -070071 if (class_def->AddMember(std::move(string_member)) == ClassDefinition::Result::kOverridden) {
72 diag->Warn(DiagMessage(source.WithLine(el->line_number))
73 << "duplicate definitions of '" << result.value() << "', overriding previous");
74 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070075 return true;
Adam Lesinskica5638f2015-10-21 14:42:43 -070076}
77
Adam Lesinski09f4d702017-08-08 10:39:55 -070078std::unique_ptr<ClassDefinition> GenerateManifestClass(IDiagnostics* diag, xml::XmlResource* res) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079 xml::Element* el = xml::FindRootElement(res->root.get());
80 if (!el) {
81 diag->Error(DiagMessage(res->file.source) << "no root tag defined");
82 return {};
83 }
84
85 if (el->name != "manifest" && !el->namespace_uri.empty()) {
Adam Lesinski09f4d702017-08-08 10:39:55 -070086 diag->Error(DiagMessage(res->file.source) << "no <manifest> root tag defined");
Adam Lesinskice5e56e2016-10-21 17:56:45 -070087 return {};
88 }
89
90 std::unique_ptr<ClassDefinition> permission_class =
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080091 util::make_unique<ClassDefinition>("permission", ClassQualifier::kStatic, false);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070092 std::unique_ptr<ClassDefinition> permission_group_class =
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080093 util::make_unique<ClassDefinition>("permission_group", ClassQualifier::kStatic, false);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070094
95 bool error = false;
96 std::vector<xml::Element*> children = el->GetChildElements();
97 for (xml::Element* child_el : children) {
98 if (child_el->namespace_uri.empty()) {
99 if (child_el->name == "permission") {
Adam Lesinski09f4d702017-08-08 10:39:55 -0700100 error |= !WriteSymbol(res->file.source, diag, child_el, permission_class.get());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700101 } else if (child_el->name == "permission-group") {
Adam Lesinski09f4d702017-08-08 10:39:55 -0700102 error |= !WriteSymbol(res->file.source, diag, child_el, permission_group_class.get());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700103 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700104 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700105 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700106
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700107 if (error) {
108 return {};
109 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700110
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700111 std::unique_ptr<ClassDefinition> manifest_class =
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800112 util::make_unique<ClassDefinition>("Manifest", ClassQualifier::kNone, false);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700113 manifest_class->AddMember(std::move(permission_class));
114 manifest_class->AddMember(std::move(permission_group_class));
115 return manifest_class;
Adam Lesinskica5638f2015-10-21 14:42:43 -0700116}
117
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700118} // namespace aapt