Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 "xml/XmlActionExecutor.h" |
| 18 | |
Adam Lesinski | ed37f48 | 2017-11-02 12:07:08 -0700 | [diff] [blame] | 19 | using ::android::StringPiece; |
| 20 | |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 21 | namespace aapt { |
| 22 | namespace xml { |
| 23 | |
Rhed Jao | 2c43442 | 2020-11-12 10:48:03 +0800 | [diff] [blame] | 24 | static bool wrapper_one(const XmlNodeAction::ActionFunc& f, Element* el, |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame^] | 25 | const XmlActionExecutorPolicy& policy, android::SourcePathDiagnostics*) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 26 | return f(el); |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 27 | } |
| 28 | |
Rhed Jao | 2c43442 | 2020-11-12 10:48:03 +0800 | [diff] [blame] | 29 | static bool wrapper_two(const XmlNodeAction::ActionFuncWithDiag& f, Element* el, |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame^] | 30 | const XmlActionExecutorPolicy& policy, |
| 31 | android::SourcePathDiagnostics* diag) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 32 | return f(el, diag); |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 33 | } |
| 34 | |
Rhed Jao | 2c43442 | 2020-11-12 10:48:03 +0800 | [diff] [blame] | 35 | static bool wrapper_three(const XmlNodeAction::ActionFuncWithPolicyAndDiag& f, Element* el, |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame^] | 36 | const XmlActionExecutorPolicy& policy, |
| 37 | android::SourcePathDiagnostics* diag) { |
Rhed Jao | 2c43442 | 2020-11-12 10:48:03 +0800 | [diff] [blame] | 38 | return f(el, policy, diag); |
| 39 | } |
| 40 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 41 | void XmlNodeAction::Action(XmlNodeAction::ActionFunc f) { |
Rhed Jao | 2c43442 | 2020-11-12 10:48:03 +0800 | [diff] [blame] | 42 | actions_.emplace_back(std::bind(wrapper_one, std::move(f), std::placeholders::_1, |
| 43 | std::placeholders::_2, std::placeholders::_3)); |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 46 | void XmlNodeAction::Action(XmlNodeAction::ActionFuncWithDiag f) { |
Rhed Jao | 2c43442 | 2020-11-12 10:48:03 +0800 | [diff] [blame] | 47 | actions_.emplace_back(std::bind(wrapper_two, std::move(f), std::placeholders::_1, |
| 48 | std::placeholders::_2, std::placeholders::_3)); |
| 49 | } |
| 50 | |
| 51 | void XmlNodeAction::Action(XmlNodeAction::ActionFuncWithPolicyAndDiag f) { |
| 52 | actions_.emplace_back(std::bind(wrapper_three, std::move(f), std::placeholders::_1, |
| 53 | std::placeholders::_2, std::placeholders::_3)); |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame^] | 56 | static void PrintElementToDiagMessage(const Element* el, android::DiagMessage* msg) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 57 | *msg << "<"; |
| 58 | if (!el->namespace_uri.empty()) { |
| 59 | *msg << el->namespace_uri << ":"; |
| 60 | } |
| 61 | *msg << el->name << ">"; |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Adam Lesinski | ed37f48 | 2017-11-02 12:07:08 -0700 | [diff] [blame] | 64 | bool XmlNodeAction::Execute(XmlActionExecutorPolicy policy, std::vector<StringPiece>* bread_crumb, |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame^] | 65 | android::SourcePathDiagnostics* diag, Element* el) const { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 66 | bool error = false; |
Rhed Jao | 2c43442 | 2020-11-12 10:48:03 +0800 | [diff] [blame] | 67 | for (const ActionFuncWithPolicyAndDiag& action : actions_) { |
| 68 | error |= !action(el, policy, diag); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | for (Element* child_el : el->GetChildElements()) { |
| 72 | if (child_el->namespace_uri.empty()) { |
Adam Lesinski | 63699b1 | 2017-05-08 18:36:33 -0700 | [diff] [blame] | 73 | std::map<std::string, XmlNodeAction>::const_iterator iter = map_.find(child_el->name); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 74 | if (iter != map_.end()) { |
Adam Lesinski | ed37f48 | 2017-11-02 12:07:08 -0700 | [diff] [blame] | 75 | // Use the iterator's copy of the element name, because the element may be modified. |
| 76 | bread_crumb->push_back(iter->first); |
| 77 | error |= !iter->second.Execute(policy, bread_crumb, diag, child_el); |
| 78 | bread_crumb->pop_back(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 79 | continue; |
| 80 | } |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 81 | |
Izabela Orlowska | ad9e132 | 2017-12-19 16:22:42 +0000 | [diff] [blame] | 82 | if (policy != XmlActionExecutorPolicy::kNone) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame^] | 83 | android::DiagMessage error_msg(child_el->line_number); |
Adam Lesinski | ed37f48 | 2017-11-02 12:07:08 -0700 | [diff] [blame] | 84 | error_msg << "unexpected element "; |
Adam Lesinski | 63699b1 | 2017-05-08 18:36:33 -0700 | [diff] [blame] | 85 | PrintElementToDiagMessage(child_el, &error_msg); |
Adam Lesinski | ed37f48 | 2017-11-02 12:07:08 -0700 | [diff] [blame] | 86 | error_msg << " found in "; |
| 87 | for (const StringPiece& element : *bread_crumb) { |
| 88 | error_msg << "<" << element << ">"; |
| 89 | } |
Ryan Mitchell | 4ea9075 | 2020-07-31 08:21:43 -0700 | [diff] [blame] | 90 | if (policy == XmlActionExecutorPolicy::kAllowListWarning) { |
Izabela Orlowska | ad9e132 | 2017-12-19 16:22:42 +0000 | [diff] [blame] | 91 | // Treat the error only as a warning. |
| 92 | diag->Warn(error_msg); |
| 93 | } else { |
Ryan Mitchell | 4ea9075 | 2020-07-31 08:21:43 -0700 | [diff] [blame] | 94 | // Policy is XmlActionExecutorPolicy::kAllowList, we should fail. |
Izabela Orlowska | ad9e132 | 2017-12-19 16:22:42 +0000 | [diff] [blame] | 95 | diag->Error(error_msg); |
| 96 | error = true; |
| 97 | } |
Adam Lesinski | 63699b1 | 2017-05-08 18:36:33 -0700 | [diff] [blame] | 98 | } |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 99 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 100 | } |
| 101 | return !error; |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame^] | 104 | bool XmlActionExecutor::Execute(XmlActionExecutorPolicy policy, android::IDiagnostics* diag, |
Adam Lesinski | 63699b1 | 2017-05-08 18:36:33 -0700 | [diff] [blame] | 105 | XmlResource* doc) const { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame^] | 106 | android::SourcePathDiagnostics source_diag(doc->file.source, diag); |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 107 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 108 | Element* el = doc->root.get(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 109 | if (!el) { |
Ryan Mitchell | 4ea9075 | 2020-07-31 08:21:43 -0700 | [diff] [blame] | 110 | if (policy == XmlActionExecutorPolicy::kAllowList) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame^] | 111 | source_diag.Error(android::DiagMessage() << "no root XML tag found"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 112 | return false; |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 113 | } |
| 114 | return true; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | if (el->namespace_uri.empty()) { |
Adam Lesinski | 63699b1 | 2017-05-08 18:36:33 -0700 | [diff] [blame] | 118 | std::map<std::string, XmlNodeAction>::const_iterator iter = map_.find(el->name); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 119 | if (iter != map_.end()) { |
Adam Lesinski | ed37f48 | 2017-11-02 12:07:08 -0700 | [diff] [blame] | 120 | std::vector<StringPiece> bread_crumb; |
| 121 | bread_crumb.push_back(iter->first); |
| 122 | return iter->second.Execute(policy, &bread_crumb, &source_diag, el); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 123 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 124 | |
Ryan Mitchell | 4ea9075 | 2020-07-31 08:21:43 -0700 | [diff] [blame] | 125 | if (policy == XmlActionExecutorPolicy::kAllowList) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame^] | 126 | android::DiagMessage error_msg(el->line_number); |
Adam Lesinski | ed37f48 | 2017-11-02 12:07:08 -0700 | [diff] [blame] | 127 | error_msg << "unexpected root element "; |
Adam Lesinski | 63699b1 | 2017-05-08 18:36:33 -0700 | [diff] [blame] | 128 | PrintElementToDiagMessage(el, &error_msg); |
Adam Lesinski | 63699b1 | 2017-05-08 18:36:33 -0700 | [diff] [blame] | 129 | source_diag.Error(error_msg); |
| 130 | return false; |
| 131 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 132 | } |
| 133 | return true; |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 136 | } // namespace xml |
| 137 | } // namespace aapt |