blob: 7580b469a98ed65ff0343dea0df65dbc5b2c420b [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 Lesinskice5e56e22016-10-21 17:56:45 -070022static bool wrapper_one(XmlNodeAction::ActionFunc& f, Element* el,
23 SourcePathDiagnostics*) {
24 return f(el);
Adam Lesinskicc5609d2016-04-05 12:41:07 -070025}
26
Adam Lesinskice5e56e22016-10-21 17:56:45 -070027static bool wrapper_two(XmlNodeAction::ActionFuncWithDiag& f, Element* el,
28 SourcePathDiagnostics* diag) {
29 return f(el, diag);
Adam Lesinskicc5609d2016-04-05 12:41:07 -070030}
31
Adam Lesinskice5e56e22016-10-21 17:56:45 -070032void XmlNodeAction::Action(XmlNodeAction::ActionFunc f) {
33 actions_.emplace_back(std::bind(
34 wrapper_one, std::move(f), std::placeholders::_1, std::placeholders::_2));
Adam Lesinskicc5609d2016-04-05 12:41:07 -070035}
36
Adam Lesinskice5e56e22016-10-21 17:56:45 -070037void XmlNodeAction::Action(XmlNodeAction::ActionFuncWithDiag f) {
38 actions_.emplace_back(std::bind(
39 wrapper_two, std::move(f), std::placeholders::_1, std::placeholders::_2));
Adam Lesinskicc5609d2016-04-05 12:41:07 -070040}
41
Adam Lesinskice5e56e22016-10-21 17:56:45 -070042static 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 Lesinskicc5609d2016-04-05 12:41:07 -070048}
49
Adam Lesinskice5e56e22016-10-21 17:56:45 -070050bool 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 Lesinskicc5609d2016-04-05 12:41:07 -070065 }
66
Adam Lesinskice5e56e22016-10-21 17:56:45 -070067 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 Lesinskicc5609d2016-04-05 12:41:07 -070074 }
Adam Lesinskice5e56e22016-10-21 17:56:45 -070075 }
76 return !error;
Adam Lesinskicc5609d2016-04-05 12:41:07 -070077}
78
Adam Lesinskice5e56e22016-10-21 17:56:45 -070079bool XmlActionExecutor::Execute(XmlActionExecutorPolicy policy,
80 IDiagnostics* diag, XmlResource* doc) const {
81 SourcePathDiagnostics source_diag(doc->file.source, diag);
Adam Lesinskicc5609d2016-04-05 12:41:07 -070082
Adam Lesinskice5e56e22016-10-21 17:56:45 -070083 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 Lesinskicc5609d2016-04-05 12:41:07 -070088 }
89 return true;
Adam Lesinskice5e56e22016-10-21 17:56:45 -070090 }
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 Lesinskicc5609d2016-04-05 12:41:07 -0700109}
110
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700111} // namespace xml
112} // namespace aapt