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