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/AnnotationProcessor.h"
23 #include "java/ClassDefinition.h"
24 #include "java/JavaClassGenerator.h"
25 #include "text/Unicode.h"
26 #include "util/Maybe.h"
27 #include "xml/XmlDom.h"
28 
29 using ::android::StringPiece;
30 using ::aapt::text::IsJavaIdentifier;
31 
32 namespace aapt {
33 
ExtractJavaIdentifier(IDiagnostics * diag,const Source & source,const std::string & value)34 static 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);
40   }
41 
42   // Normalize only the java identifier, leave the original value unchanged.
43   if (result.contains("-")) {
44     result = JavaClassGenerator::TransformToFieldName(result);
45   }
46 
47   if (result.empty()) {
48     diag->Error(DiagMessage(source) << "empty symbol");
49     return {};
50   }
51 
52   if (!IsJavaIdentifier(result)) {
53     diag->Error(DiagMessage(source) << "invalid Java identifier '" << result << "'");
54     return {};
55   }
56   return result;
57 }
58 
WriteSymbol(const Source & source,IDiagnostics * diag,xml::Element * el,ClassDefinition * class_def)59 static bool WriteSymbol(const Source& source, IDiagnostics* diag, xml::Element* el,
60                         ClassDefinition* class_def) {
61   xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "name");
62   if (!attr) {
63     diag->Error(DiagMessage(source) << "<" << el->name << "> must define 'android:name'");
64     return false;
65   }
66 
67   Maybe<StringPiece> result =
68       ExtractJavaIdentifier(diag, source.WithLine(el->line_number), attr->value);
69   if (!result) {
70     return false;
71   }
72 
73   std::unique_ptr<StringMember> string_member =
74       util::make_unique<StringMember>(result.value(), attr->value);
75   string_member->GetCommentBuilder()->AppendComment(el->comment);
76 
77   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   }
81   return true;
82 }
83 
GenerateManifestClass(IDiagnostics * diag,xml::XmlResource * res)84 std::unique_ptr<ClassDefinition> GenerateManifestClass(IDiagnostics* diag, xml::XmlResource* res) {
85   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()) {
92     diag->Error(DiagMessage(res->file.source) << "no <manifest> root tag defined");
93     return {};
94   }
95 
96   std::unique_ptr<ClassDefinition> permission_class =
97       util::make_unique<ClassDefinition>("permission", ClassQualifier::kStatic, false);
98   std::unique_ptr<ClassDefinition> permission_group_class =
99       util::make_unique<ClassDefinition>("permission_group", ClassQualifier::kStatic, false);
100 
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") {
106         error |= !WriteSymbol(res->file.source, diag, child_el, permission_class.get());
107       } else if (child_el->name == "permission-group") {
108         error |= !WriteSymbol(res->file.source, diag, child_el, permission_group_class.get());
109       }
110     }
111   }
112 
113   if (error) {
114     return {};
115   }
116 
117   std::unique_ptr<ClassDefinition> manifest_class =
118       util::make_unique<ClassDefinition>("Manifest", ClassQualifier::kNone, false);
119   manifest_class->AddMember(std::move(permission_class));
120   manifest_class->AddMember(std::move(permission_group_class));
121   return manifest_class;
122 }
123 
124 }  // namespace aapt
125