Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 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 | |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 17 | #include "java/ManifestClassGenerator.h" |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 18 | |
| 19 | #include <algorithm> |
| 20 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 21 | #include "Source.h" |
| 22 | #include "java/AnnotationProcessor.h" |
| 23 | #include "java/ClassDefinition.h" |
Izabela Orlowska | d31bc12 | 2018-02-12 11:03:42 +0000 | [diff] [blame] | 24 | #include "java/JavaClassGenerator.h" |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 25 | #include "text/Unicode.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 26 | #include "util/Maybe.h" |
| 27 | #include "xml/XmlDom.h" |
| 28 | |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 29 | using ::android::StringPiece; |
| 30 | using ::aapt::text::IsJavaIdentifier; |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 31 | |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 32 | namespace aapt { |
| 33 | |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 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); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 40 | } |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 41 | |
Izabela Orlowska | d31bc12 | 2018-02-12 11:03:42 +0000 | [diff] [blame] | 42 | // Normalize only the java identifier, leave the original value unchanged. |
| 43 | if (result.contains("-")) { |
| 44 | result = JavaClassGenerator::TransformToFieldName(result); |
| 45 | } |
| 46 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 47 | if (result.empty()) { |
| 48 | diag->Error(DiagMessage(source) << "empty symbol"); |
| 49 | return {}; |
| 50 | } |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 51 | |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 52 | if (!IsJavaIdentifier(result)) { |
| 53 | diag->Error(DiagMessage(source) << "invalid Java identifier '" << result << "'"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 54 | return {}; |
| 55 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 56 | return result; |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 59 | static bool WriteSymbol(const Source& source, IDiagnostics* diag, xml::Element* el, |
| 60 | ClassDefinition* class_def) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 61 | xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "name"); |
| 62 | if (!attr) { |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 63 | diag->Error(DiagMessage(source) << "<" << el->name << "> must define 'android:name'"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 64 | return false; |
| 65 | } |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 66 | |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 67 | Maybe<StringPiece> result = |
| 68 | ExtractJavaIdentifier(diag, source.WithLine(el->line_number), attr->value); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 69 | if (!result) { |
| 70 | return false; |
| 71 | } |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 72 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 73 | std::unique_ptr<StringMember> string_member = |
| 74 | util::make_unique<StringMember>(result.value(), attr->value); |
| 75 | string_member->GetCommentBuilder()->AppendComment(el->comment); |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 76 | |
Adam Lesinski | f852dd0 | 2017-08-18 19:49:58 -0700 | [diff] [blame] | 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 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 81 | return true; |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 84 | std::unique_ptr<ClassDefinition> GenerateManifestClass(IDiagnostics* diag, xml::XmlResource* res) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 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()) { |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 92 | diag->Error(DiagMessage(res->file.source) << "no <manifest> root tag defined"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 93 | return {}; |
| 94 | } |
| 95 | |
| 96 | std::unique_ptr<ClassDefinition> permission_class = |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 97 | util::make_unique<ClassDefinition>("permission", ClassQualifier::kStatic, false); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 98 | std::unique_ptr<ClassDefinition> permission_group_class = |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 99 | util::make_unique<ClassDefinition>("permission_group", ClassQualifier::kStatic, false); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 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") { |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 106 | error |= !WriteSymbol(res->file.source, diag, child_el, permission_class.get()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 107 | } else if (child_el->name == "permission-group") { |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 108 | error |= !WriteSymbol(res->file.source, diag, child_el, permission_group_class.get()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 109 | } |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 110 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 111 | } |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 112 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 113 | if (error) { |
| 114 | return {}; |
| 115 | } |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 116 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 117 | std::unique_ptr<ClassDefinition> manifest_class = |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 118 | util::make_unique<ClassDefinition>("Manifest", ClassQualifier::kNone, false); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 119 | manifest_class->AddMember(std::move(permission_class)); |
| 120 | manifest_class->AddMember(std::move(permission_group_class)); |
| 121 | return manifest_class; |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 124 | } // namespace aapt |