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 "androidfw/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 
ExtractJavaIdentifier(android::IDiagnostics * diag,const android::Source & source,const std::string & value)31 static std::optional<std::string> ExtractJavaIdentifier(android::IDiagnostics* diag,
32                                                         const android::Source& source,
33                                                         const std::string& value) {
34   std::string result = value;
35   size_t pos = value.rfind('.');
36   if (pos != std::string::npos) {
37     result = result.substr(pos + 1);
38   }
39 
40   // Normalize only the java identifier, leave the original value unchanged.
41   if (result.find('-') != std::string::npos) {
42     result = JavaClassGenerator::TransformToFieldName(result);
43   }
44 
45   if (result.empty()) {
46     diag->Error(android::DiagMessage(source) << "empty symbol");
47     return {};
48   }
49 
50   if (!IsJavaIdentifier(result)) {
51     diag->Error(android::DiagMessage(source) << "invalid Java identifier '" << result << "'");
52     return {};
53   }
54   return result;
55 }
56 
WriteSymbol(const android::Source & source,android::IDiagnostics * diag,xml::Element * el,ClassDefinition * class_def)57 static bool WriteSymbol(const android::Source& source, android::IDiagnostics* diag,
58                         xml::Element* el, ClassDefinition* class_def) {
59   xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "name");
60   if (!attr) {
61     diag->Error(android::DiagMessage(source) << "<" << el->name << "> must define 'android:name'");
62     return false;
63   }
64 
65   std::optional<std::string> result =
66       ExtractJavaIdentifier(diag, source.WithLine(el->line_number), attr->value);
67   if (!result) {
68     return false;
69   }
70 
71   std::unique_ptr<StringMember> string_member =
72       util::make_unique<StringMember>(result.value(), attr->value);
73   string_member->GetCommentBuilder()->AppendComment(el->comment);
74 
75   if (class_def->AddMember(std::move(string_member)) == ClassDefinition::Result::kOverridden) {
76     diag->Warn(android::DiagMessage(source.WithLine(el->line_number))
77                << "duplicate definitions of '" << result.value() << "', overriding previous");
78   }
79   return true;
80 }
81 
GenerateManifestClass(android::IDiagnostics * diag,xml::XmlResource * res)82 std::unique_ptr<ClassDefinition> GenerateManifestClass(android::IDiagnostics* diag,
83                                                        xml::XmlResource* res) {
84   xml::Element* el = xml::FindRootElement(res->root.get());
85   if (!el) {
86     diag->Error(android::DiagMessage(res->file.source) << "no root tag defined");
87     return {};
88   }
89 
90   if (el->name != "manifest" && !el->namespace_uri.empty()) {
91     diag->Error(android::DiagMessage(res->file.source) << "no <manifest> root tag defined");
92     return {};
93   }
94 
95   std::unique_ptr<ClassDefinition> permission_class =
96       util::make_unique<ClassDefinition>("permission", ClassQualifier::kStatic, false);
97   std::unique_ptr<ClassDefinition> permission_group_class =
98       util::make_unique<ClassDefinition>("permission_group", ClassQualifier::kStatic, false);
99 
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") {
105         error |= !WriteSymbol(res->file.source, diag, child_el, permission_class.get());
106       } else if (child_el->name == "permission-group") {
107         error |= !WriteSymbol(res->file.source, diag, child_el, permission_group_class.get());
108       }
109     }
110   }
111 
112   if (error) {
113     return {};
114   }
115 
116   std::unique_ptr<ClassDefinition> manifest_class =
117       util::make_unique<ClassDefinition>("Manifest", ClassQualifier::kNone, false);
118   manifest_class->AddMember(std::move(permission_class));
119   manifest_class->AddMember(std::move(permission_group_class));
120   return manifest_class;
121 }
122 
123 }  // namespace aapt
124