Debian Codegraph @

tools/aapt2/java/ManifestClassGenerator.cpp

raw symbols SCIP
1 /*
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
17 #include "java/ManifestClassGenerator.h"
18
19 #include <algorithm>
20
21 #include "Source.h"
22 #include "java/ClassDefinition.h"
23 #include "java/JavaClassGenerator.h"
24 #include "text/Unicode.h"
25 #include "xml/XmlDom.h"
26
27 using ::aapt::text::IsJavaIdentifier;
28
29 namespace aapt {
30
31 static std::optional<std::string> ExtractJavaIdentifier(IDiagnostics* diag, const Source& source,
32 const std::string& value) {
33 std::string result = value;
34 size_t pos = value.rfind('.');
35 if (pos != std::string::npos) {
36 result = result.substr(pos + 1);
37 }
38
39 // Normalize only the java identifier, leave the original value unchanged.
40 if (result.find('-') != std::string::npos) {
41 result = JavaClassGenerator::TransformToFieldName(result);
42 }
43
44 if (result.empty()) {
45 diag->Error(DiagMessage(source) << "empty symbol");
46 return {};
47 }
48
49 if (!IsJavaIdentifier(result)) {
50 diag->Error(DiagMessage(source) << "invalid Java identifier '" << result << "'");
51 return {};
52 }
53 return result;
54 }
55
56 static bool WriteSymbol(const Source& source, IDiagnostics* diag, xml::Element* el,
57 ClassDefinition* class_def) {
58 xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "name");
59 if (!attr) {
60 diag->Error(DiagMessage(source) << "<" << el->name << "> must define 'android:name'");
61 return false;
62 }
63
64 std::optional<std::string> result =
65 ExtractJavaIdentifier(diag, source.WithLine(el->line_number), attr->value);
66 if (!result) {
67 return false;
68 }
69
70 std::unique_ptr<StringMember> string_member =
71 util::make_unique<StringMember>(result.value(), attr->value);
72 string_member->GetCommentBuilder()->AppendComment(el->comment);
73
74 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 }
78 return true;
79 }
80
81 std::unique_ptr<ClassDefinition> GenerateManifestClass(IDiagnostics* diag, xml::XmlResource* res) {
82 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()) {
89 diag->Error(DiagMessage(res->file.source) << "no <manifest> root tag defined");
90 return {};
91 }
92
93 std::unique_ptr<ClassDefinition> permission_class =
94 util::make_unique<ClassDefinition>("permission", ClassQualifier::kStatic, false);
95 std::unique_ptr<ClassDefinition> permission_group_class =
96 util::make_unique<ClassDefinition>("permission_group", ClassQualifier::kStatic, false);
97
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") {
103 error |= !WriteSymbol(res->file.source, diag, child_el, permission_class.get());
104 } else if (child_el->name == "permission-group") {
105 error |= !WriteSymbol(res->file.source, diag, child_el, permission_group_class.get());
106 }
107 }
108 }
109
110 if (error) {
111 return {};
112 }
113
114 std::unique_ptr<ClassDefinition> manifest_class =
115 util::make_unique<ClassDefinition>("Manifest", ClassQualifier::kNone, false);
116 manifest_class->AddMember(std::move(permission_class));
117 manifest_class->AddMember(std::move(permission_group_class));
118 return manifest_class;
119 }
120
121 } // namespace aapt