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