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" |
| 24 | #include "util/Maybe.h" |
| 25 | #include "xml/XmlDom.h" |
| 26 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 27 | using android::StringPiece; |
| 28 | |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 29 | namespace aapt { |
| 30 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 31 | static Maybe<StringPiece> ExtractJavaIdentifier(IDiagnostics* diag, |
| 32 | const Source& source, |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 33 | const StringPiece& value) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 34 | const StringPiece sep = "."; |
| 35 | auto iter = std::find_end(value.begin(), value.end(), sep.begin(), sep.end()); |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 36 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 37 | StringPiece result; |
| 38 | if (iter != value.end()) { |
| 39 | result.assign(iter + sep.size(), value.end() - (iter + sep.size())); |
| 40 | } else { |
| 41 | result = value; |
| 42 | } |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 43 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 44 | if (result.empty()) { |
| 45 | diag->Error(DiagMessage(source) << "empty symbol"); |
| 46 | return {}; |
| 47 | } |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 48 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 49 | iter = util::FindNonAlphaNumericAndNotInSet(result, "_"); |
| 50 | if (iter != result.end()) { |
| 51 | diag->Error(DiagMessage(source) << "invalid character '" |
| 52 | << StringPiece(iter, 1) << "' in '" |
| 53 | << result << "'"); |
| 54 | return {}; |
| 55 | } |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 56 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 57 | if (*result.begin() >= '0' && *result.begin() <= '9') { |
| 58 | diag->Error(DiagMessage(source) << "symbol can not start with a digit"); |
| 59 | return {}; |
| 60 | } |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 61 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 62 | return result; |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 65 | static bool WriteSymbol(const Source& source, IDiagnostics* diag, |
| 66 | xml::Element* el, ClassDefinition* class_def) { |
| 67 | xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "name"); |
| 68 | if (!attr) { |
| 69 | diag->Error(DiagMessage(source) << "<" << el->name |
| 70 | << "> must define 'android:name'"); |
| 71 | return false; |
| 72 | } |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 73 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 74 | Maybe<StringPiece> result = ExtractJavaIdentifier( |
| 75 | diag, source.WithLine(el->line_number), attr->value); |
| 76 | if (!result) { |
| 77 | return false; |
| 78 | } |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 79 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 80 | std::unique_ptr<StringMember> string_member = |
| 81 | util::make_unique<StringMember>(result.value(), attr->value); |
| 82 | string_member->GetCommentBuilder()->AppendComment(el->comment); |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 83 | |
Adam Lesinski | 6e241e7 | 2017-08-18 19:49:58 -0700 | [diff] [blame] | 84 | if (class_def->AddMember(std::move(string_member)) == ClassDefinition::Result::kOverridden) { |
| 85 | diag->Warn(DiagMessage(source.WithLine(el->line_number)) |
| 86 | << "duplicate definitions of '" << result.value() << "', overriding previous"); |
| 87 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 88 | return true; |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 91 | std::unique_ptr<ClassDefinition> GenerateManifestClass(IDiagnostics* diag, |
| 92 | xml::XmlResource* res) { |
| 93 | xml::Element* el = xml::FindRootElement(res->root.get()); |
| 94 | if (!el) { |
| 95 | diag->Error(DiagMessage(res->file.source) << "no root tag defined"); |
| 96 | return {}; |
| 97 | } |
| 98 | |
| 99 | if (el->name != "manifest" && !el->namespace_uri.empty()) { |
| 100 | diag->Error(DiagMessage(res->file.source) |
| 101 | << "no <manifest> root tag defined"); |
| 102 | return {}; |
| 103 | } |
| 104 | |
| 105 | std::unique_ptr<ClassDefinition> permission_class = |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 106 | util::make_unique<ClassDefinition>("permission", ClassQualifier::kStatic, false); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 107 | std::unique_ptr<ClassDefinition> permission_group_class = |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 108 | util::make_unique<ClassDefinition>("permission_group", ClassQualifier::kStatic, false); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 109 | |
| 110 | bool error = false; |
| 111 | std::vector<xml::Element*> children = el->GetChildElements(); |
| 112 | for (xml::Element* child_el : children) { |
| 113 | if (child_el->namespace_uri.empty()) { |
| 114 | if (child_el->name == "permission") { |
| 115 | error |= !WriteSymbol(res->file.source, diag, child_el, |
| 116 | permission_class.get()); |
| 117 | } else if (child_el->name == "permission-group") { |
| 118 | error |= !WriteSymbol(res->file.source, diag, child_el, |
| 119 | permission_group_class.get()); |
| 120 | } |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 121 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 122 | } |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 123 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 124 | if (error) { |
| 125 | return {}; |
| 126 | } |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 127 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 128 | std::unique_ptr<ClassDefinition> manifest_class = |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 129 | util::make_unique<ClassDefinition>("Manifest", ClassQualifier::kNone, false); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 130 | manifest_class->AddMember(std::move(permission_class)); |
| 131 | manifest_class->AddMember(std::move(permission_group_class)); |
| 132 | return manifest_class; |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 135 | } // namespace aapt |