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" |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 24 | #include "text/Unicode.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 25 | #include "util/Maybe.h" |
| 26 | #include "xml/XmlDom.h" |
| 27 | |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 28 | using ::android::StringPiece; |
| 29 | using ::aapt::text::IsJavaIdentifier; |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 30 | |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 31 | namespace aapt { |
| 32 | |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 33 | static 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 39 | } |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 40 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 41 | if (result.empty()) { |
| 42 | diag->Error(DiagMessage(source) << "empty symbol"); |
| 43 | return {}; |
| 44 | } |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 45 | |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 46 | if (!IsJavaIdentifier(result)) { |
| 47 | diag->Error(DiagMessage(source) << "invalid Java identifier '" << result << "'"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 48 | return {}; |
| 49 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 50 | return result; |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 53 | static bool WriteSymbol(const Source& source, IDiagnostics* diag, xml::Element* el, |
| 54 | ClassDefinition* class_def) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 55 | xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "name"); |
| 56 | if (!attr) { |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 57 | diag->Error(DiagMessage(source) << "<" << el->name << "> must define 'android:name'"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 58 | return false; |
| 59 | } |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 60 | |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 61 | Maybe<StringPiece> result = |
| 62 | ExtractJavaIdentifier(diag, source.WithLine(el->line_number), attr->value); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 63 | if (!result) { |
| 64 | return false; |
| 65 | } |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 66 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 67 | std::unique_ptr<StringMember> string_member = |
| 68 | util::make_unique<StringMember>(result.value(), attr->value); |
| 69 | string_member->GetCommentBuilder()->AppendComment(el->comment); |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 70 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 71 | class_def->AddMember(std::move(string_member)); |
| 72 | return true; |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 75 | std::unique_ptr<ClassDefinition> GenerateManifestClass(IDiagnostics* diag, xml::XmlResource* res) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 76 | 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 Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 83 | diag->Error(DiagMessage(res->file.source) << "no <manifest> root tag defined"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 84 | return {}; |
| 85 | } |
| 86 | |
| 87 | std::unique_ptr<ClassDefinition> permission_class = |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 88 | util::make_unique<ClassDefinition>("permission", ClassQualifier::kStatic, false); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 89 | std::unique_ptr<ClassDefinition> permission_group_class = |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 90 | util::make_unique<ClassDefinition>("permission_group", ClassQualifier::kStatic, false); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 91 | |
| 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 Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 97 | error |= !WriteSymbol(res->file.source, diag, child_el, permission_class.get()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 98 | } else if (child_el->name == "permission-group") { |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 99 | error |= !WriteSymbol(res->file.source, diag, child_el, permission_group_class.get()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 100 | } |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 101 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 102 | } |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 103 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 104 | if (error) { |
| 105 | return {}; |
| 106 | } |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 107 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 108 | std::unique_ptr<ClassDefinition> manifest_class = |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 109 | util::make_unique<ClassDefinition>("Manifest", ClassQualifier::kNone, false); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 110 | manifest_class->AddMember(std::move(permission_class)); |
| 111 | manifest_class->AddMember(std::move(permission_group_class)); |
| 112 | return manifest_class; |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 115 | } // namespace aapt |