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 | |
| 19 | namespace aapt { |
| 20 | namespace xml { |
| 21 | |
Adam Lesinski | 63699b1 | 2017-05-08 18:36:33 -0700 | [diff] [blame] | 22 | static bool wrapper_one(XmlNodeAction::ActionFunc& f, Element* el, SourcePathDiagnostics*) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 23 | return f(el); |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 24 | } |
| 25 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 26 | static bool wrapper_two(XmlNodeAction::ActionFuncWithDiag& f, Element* el, |
| 27 | SourcePathDiagnostics* diag) { |
| 28 | return f(el, diag); |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 29 | } |
| 30 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 31 | void XmlNodeAction::Action(XmlNodeAction::ActionFunc f) { |
| 32 | actions_.emplace_back(std::bind( |
| 33 | wrapper_one, std::move(f), std::placeholders::_1, std::placeholders::_2)); |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 36 | void XmlNodeAction::Action(XmlNodeAction::ActionFuncWithDiag f) { |
| 37 | actions_.emplace_back(std::bind( |
| 38 | wrapper_two, std::move(f), std::placeholders::_1, std::placeholders::_2)); |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 41 | static void PrintElementToDiagMessage(const Element* el, DiagMessage* msg) { |
| 42 | *msg << "<"; |
| 43 | if (!el->namespace_uri.empty()) { |
| 44 | *msg << el->namespace_uri << ":"; |
| 45 | } |
| 46 | *msg << el->name << ">"; |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Adam Lesinski | 63699b1 | 2017-05-08 18:36:33 -0700 | [diff] [blame] | 49 | bool XmlNodeAction::Execute(XmlActionExecutorPolicy policy, SourcePathDiagnostics* diag, |
| 50 | Element* el) const { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 51 | bool error = false; |
| 52 | for (const ActionFuncWithDiag& action : actions_) { |
| 53 | error |= !action(el, diag); |
| 54 | } |
| 55 | |
| 56 | for (Element* child_el : el->GetChildElements()) { |
| 57 | if (child_el->namespace_uri.empty()) { |
Adam Lesinski | 63699b1 | 2017-05-08 18:36:33 -0700 | [diff] [blame] | 58 | 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] | 59 | if (iter != map_.end()) { |
| 60 | error |= !iter->second.Execute(policy, diag, child_el); |
| 61 | continue; |
| 62 | } |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 63 | |
Adam Lesinski | 63699b1 | 2017-05-08 18:36:33 -0700 | [diff] [blame] | 64 | if (policy == XmlActionExecutorPolicy::kWhitelist) { |
| 65 | DiagMessage error_msg(child_el->line_number); |
| 66 | error_msg << "unknown element "; |
| 67 | PrintElementToDiagMessage(child_el, &error_msg); |
| 68 | error_msg << " found"; |
| 69 | diag->Error(error_msg); |
| 70 | error = true; |
| 71 | } |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 72 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 73 | } |
| 74 | return !error; |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Adam Lesinski | 63699b1 | 2017-05-08 18:36:33 -0700 | [diff] [blame] | 77 | bool XmlActionExecutor::Execute(XmlActionExecutorPolicy policy, IDiagnostics* diag, |
| 78 | XmlResource* doc) const { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 79 | SourcePathDiagnostics source_diag(doc->file.source, diag); |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 80 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 81 | Element* el = FindRootElement(doc); |
| 82 | if (!el) { |
| 83 | if (policy == XmlActionExecutorPolicy::kWhitelist) { |
| 84 | source_diag.Error(DiagMessage() << "no root XML tag found"); |
| 85 | return false; |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 86 | } |
| 87 | return true; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | if (el->namespace_uri.empty()) { |
Adam Lesinski | 63699b1 | 2017-05-08 18:36:33 -0700 | [diff] [blame] | 91 | std::map<std::string, XmlNodeAction>::const_iterator iter = map_.find(el->name); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 92 | if (iter != map_.end()) { |
| 93 | return iter->second.Execute(policy, &source_diag, el); |
| 94 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 95 | |
Adam Lesinski | 63699b1 | 2017-05-08 18:36:33 -0700 | [diff] [blame] | 96 | if (policy == XmlActionExecutorPolicy::kWhitelist) { |
| 97 | DiagMessage error_msg(el->line_number); |
| 98 | error_msg << "unknown element "; |
| 99 | PrintElementToDiagMessage(el, &error_msg); |
| 100 | error_msg << " found"; |
| 101 | source_diag.Error(error_msg); |
| 102 | return false; |
| 103 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 104 | } |
| 105 | return true; |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 108 | } // namespace xml |
| 109 | } // namespace aapt |