blob: 352a933616333b897121423efbdcf00859b36dce [file] [log] [blame]
Adam Lesinskicc5609d2016-04-05 12:41:07 -07001/*
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
19namespace aapt {
20namespace xml {
21
Adam Lesinski63699b12017-05-08 18:36:33 -070022static bool wrapper_one(XmlNodeAction::ActionFunc& f, Element* el, SourcePathDiagnostics*) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070023 return f(el);
Adam Lesinskicc5609d2016-04-05 12:41:07 -070024}
25
Adam Lesinskice5e56e2016-10-21 17:56:45 -070026static bool wrapper_two(XmlNodeAction::ActionFuncWithDiag& f, Element* el,
27 SourcePathDiagnostics* diag) {
28 return f(el, diag);
Adam Lesinskicc5609d2016-04-05 12:41:07 -070029}
30
Adam Lesinskice5e56e2016-10-21 17:56:45 -070031void XmlNodeAction::Action(XmlNodeAction::ActionFunc f) {
32 actions_.emplace_back(std::bind(
33 wrapper_one, std::move(f), std::placeholders::_1, std::placeholders::_2));
Adam Lesinskicc5609d2016-04-05 12:41:07 -070034}
35
Adam Lesinskice5e56e2016-10-21 17:56:45 -070036void XmlNodeAction::Action(XmlNodeAction::ActionFuncWithDiag f) {
37 actions_.emplace_back(std::bind(
38 wrapper_two, std::move(f), std::placeholders::_1, std::placeholders::_2));
Adam Lesinskicc5609d2016-04-05 12:41:07 -070039}
40
Adam Lesinskice5e56e2016-10-21 17:56:45 -070041static 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 Lesinskicc5609d2016-04-05 12:41:07 -070047}
48
Adam Lesinski63699b12017-05-08 18:36:33 -070049bool XmlNodeAction::Execute(XmlActionExecutorPolicy policy, SourcePathDiagnostics* diag,
50 Element* el) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070051 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 Lesinski63699b12017-05-08 18:36:33 -070058 std::map<std::string, XmlNodeAction>::const_iterator iter = map_.find(child_el->name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059 if (iter != map_.end()) {
60 error |= !iter->second.Execute(policy, diag, child_el);
61 continue;
62 }
Adam Lesinskicc5609d2016-04-05 12:41:07 -070063
Adam Lesinski63699b12017-05-08 18:36:33 -070064 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 Lesinskicc5609d2016-04-05 12:41:07 -070072 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070073 }
74 return !error;
Adam Lesinskicc5609d2016-04-05 12:41:07 -070075}
76
Adam Lesinski63699b12017-05-08 18:36:33 -070077bool XmlActionExecutor::Execute(XmlActionExecutorPolicy policy, IDiagnostics* diag,
78 XmlResource* doc) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079 SourcePathDiagnostics source_diag(doc->file.source, diag);
Adam Lesinskicc5609d2016-04-05 12:41:07 -070080
Adam Lesinskice5e56e2016-10-21 17:56:45 -070081 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 Lesinskicc5609d2016-04-05 12:41:07 -070086 }
87 return true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070088 }
89
90 if (el->namespace_uri.empty()) {
Adam Lesinski63699b12017-05-08 18:36:33 -070091 std::map<std::string, XmlNodeAction>::const_iterator iter = map_.find(el->name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070092 if (iter != map_.end()) {
93 return iter->second.Execute(policy, &source_diag, el);
94 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070095
Adam Lesinski63699b12017-05-08 18:36:33 -070096 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 Lesinskice5e56e2016-10-21 17:56:45 -0700104 }
105 return true;
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700106}
107
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700108} // namespace xml
109} // namespace aapt