blob: 4ef32c92dd848f41ae5b007c737a255e6f80935e [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 Lesinskice5e56e2016-10-21 17:56:45 -070071 class_def->AddMember(std::move(string_member));
72 return true;
Adam Lesinskica5638f2015-10-21 14:42:43 -070073}
74
Adam Lesinski09f4d702017-08-08 10:39:55 -070075std::unique_ptr<ClassDefinition> GenerateManifestClass(IDiagnostics* diag, xml::XmlResource* res) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076 xml::Element* el = xml::FindRootElement(res->root.get());
77 if (!el) {
78 diag->Error(DiagMessage(res->file.source) << "no root tag defined");
79 return {};
80 }
81
82 if (el->name != "manifest" && !el->namespace_uri.empty()) {
Adam Lesinski09f4d702017-08-08 10:39:55 -070083 diag->Error(DiagMessage(res->file.source) << "no <manifest> root tag defined");
Adam Lesinskice5e56e2016-10-21 17:56:45 -070084 return {};
85 }
86
87 std::unique_ptr<ClassDefinition> permission_class =
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080088 util::make_unique<ClassDefinition>("permission", ClassQualifier::kStatic, false);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070089 std::unique_ptr<ClassDefinition> permission_group_class =
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080090 util::make_unique<ClassDefinition>("permission_group", ClassQualifier::kStatic, false);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070091
92 bool error = false;
93 std::vector<xml::Element*> children = el->GetChildElements();
94 for (xml::Element* child_el : children) {
95 if (child_el->namespace_uri.empty()) {
96 if (child_el->name == "permission") {
Adam Lesinski09f4d702017-08-08 10:39:55 -070097 error |= !WriteSymbol(res->file.source, diag, child_el, permission_class.get());
Adam Lesinskice5e56e2016-10-21 17:56:45 -070098 } else if (child_el->name == "permission-group") {
Adam Lesinski09f4d702017-08-08 10:39:55 -070099 error |= !WriteSymbol(res->file.source, diag, child_el, permission_group_class.get());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700100 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700101 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700102 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700103
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700104 if (error) {
105 return {};
106 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700107
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700108 std::unique_ptr<ClassDefinition> manifest_class =
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800109 util::make_unique<ClassDefinition>("Manifest", ClassQualifier::kNone, false);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700110 manifest_class->AddMember(std::move(permission_class));
111 manifest_class->AddMember(std::move(permission_group_class));
112 return manifest_class;
Adam Lesinskica5638f2015-10-21 14:42:43 -0700113}
114
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700115} // namespace aapt