blob: ea42d26358a875f821ef6852eae72a8fd0fd7683 [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
Adam Lesinskied37f482017-11-02 12:07:08 -070019using ::android::StringPiece;
20
Adam Lesinskicc5609d2016-04-05 12:41:07 -070021namespace aapt {
22namespace xml {
23
Rhed Jao2c434422020-11-12 10:48:03 +080024static bool wrapper_one(const XmlNodeAction::ActionFunc& f, Element* el,
25 const XmlActionExecutorPolicy& policy, SourcePathDiagnostics*) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070026 return f(el);
Adam Lesinskicc5609d2016-04-05 12:41:07 -070027}
28
Rhed Jao2c434422020-11-12 10:48:03 +080029static bool wrapper_two(const XmlNodeAction::ActionFuncWithDiag& f, Element* el,
30 const XmlActionExecutorPolicy& policy, SourcePathDiagnostics* diag) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070031 return f(el, diag);
Adam Lesinskicc5609d2016-04-05 12:41:07 -070032}
33
Rhed Jao2c434422020-11-12 10:48:03 +080034static bool wrapper_three(const XmlNodeAction::ActionFuncWithPolicyAndDiag& f, Element* el,
35 const XmlActionExecutorPolicy& policy, SourcePathDiagnostics* diag) {
36 return f(el, policy, diag);
37}
38
Adam Lesinskice5e56e2016-10-21 17:56:45 -070039void XmlNodeAction::Action(XmlNodeAction::ActionFunc f) {
Rhed Jao2c434422020-11-12 10:48:03 +080040 actions_.emplace_back(std::bind(wrapper_one, std::move(f), std::placeholders::_1,
41 std::placeholders::_2, std::placeholders::_3));
Adam Lesinskicc5609d2016-04-05 12:41:07 -070042}
43
Adam Lesinskice5e56e2016-10-21 17:56:45 -070044void XmlNodeAction::Action(XmlNodeAction::ActionFuncWithDiag f) {
Rhed Jao2c434422020-11-12 10:48:03 +080045 actions_.emplace_back(std::bind(wrapper_two, std::move(f), std::placeholders::_1,
46 std::placeholders::_2, std::placeholders::_3));
47}
48
49void XmlNodeAction::Action(XmlNodeAction::ActionFuncWithPolicyAndDiag f) {
50 actions_.emplace_back(std::bind(wrapper_three, std::move(f), std::placeholders::_1,
51 std::placeholders::_2, std::placeholders::_3));
Adam Lesinskicc5609d2016-04-05 12:41:07 -070052}
53
Adam Lesinskice5e56e2016-10-21 17:56:45 -070054static void PrintElementToDiagMessage(const Element* el, DiagMessage* msg) {
55 *msg << "<";
56 if (!el->namespace_uri.empty()) {
57 *msg << el->namespace_uri << ":";
58 }
59 *msg << el->name << ">";
Adam Lesinskicc5609d2016-04-05 12:41:07 -070060}
61
Adam Lesinskied37f482017-11-02 12:07:08 -070062bool XmlNodeAction::Execute(XmlActionExecutorPolicy policy, std::vector<StringPiece>* bread_crumb,
63 SourcePathDiagnostics* diag, Element* el) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070064 bool error = false;
Rhed Jao2c434422020-11-12 10:48:03 +080065 for (const ActionFuncWithPolicyAndDiag& action : actions_) {
66 error |= !action(el, policy, diag);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070067 }
68
69 for (Element* child_el : el->GetChildElements()) {
70 if (child_el->namespace_uri.empty()) {
Adam Lesinski63699b12017-05-08 18:36:33 -070071 std::map<std::string, XmlNodeAction>::const_iterator iter = map_.find(child_el->name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072 if (iter != map_.end()) {
Adam Lesinskied37f482017-11-02 12:07:08 -070073 // Use the iterator's copy of the element name, because the element may be modified.
74 bread_crumb->push_back(iter->first);
75 error |= !iter->second.Execute(policy, bread_crumb, diag, child_el);
76 bread_crumb->pop_back();
Adam Lesinskice5e56e2016-10-21 17:56:45 -070077 continue;
78 }
Adam Lesinskicc5609d2016-04-05 12:41:07 -070079
Izabela Orlowskaad9e1322017-12-19 16:22:42 +000080 if (policy != XmlActionExecutorPolicy::kNone) {
Adam Lesinski63699b12017-05-08 18:36:33 -070081 DiagMessage error_msg(child_el->line_number);
Adam Lesinskied37f482017-11-02 12:07:08 -070082 error_msg << "unexpected element ";
Adam Lesinski63699b12017-05-08 18:36:33 -070083 PrintElementToDiagMessage(child_el, &error_msg);
Adam Lesinskied37f482017-11-02 12:07:08 -070084 error_msg << " found in ";
85 for (const StringPiece& element : *bread_crumb) {
86 error_msg << "<" << element << ">";
87 }
Ryan Mitchell4ea90752020-07-31 08:21:43 -070088 if (policy == XmlActionExecutorPolicy::kAllowListWarning) {
Izabela Orlowskaad9e1322017-12-19 16:22:42 +000089 // Treat the error only as a warning.
90 diag->Warn(error_msg);
91 } else {
Ryan Mitchell4ea90752020-07-31 08:21:43 -070092 // Policy is XmlActionExecutorPolicy::kAllowList, we should fail.
Izabela Orlowskaad9e1322017-12-19 16:22:42 +000093 diag->Error(error_msg);
94 error = true;
95 }
Adam Lesinski63699b12017-05-08 18:36:33 -070096 }
Adam Lesinskicc5609d2016-04-05 12:41:07 -070097 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070098 }
99 return !error;
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700100}
101
Adam Lesinski63699b12017-05-08 18:36:33 -0700102bool XmlActionExecutor::Execute(XmlActionExecutorPolicy policy, IDiagnostics* diag,
103 XmlResource* doc) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700104 SourcePathDiagnostics source_diag(doc->file.source, diag);
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700105
Adam Lesinski6b372992017-08-09 10:54:23 -0700106 Element* el = doc->root.get();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700107 if (!el) {
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700108 if (policy == XmlActionExecutorPolicy::kAllowList) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700109 source_diag.Error(DiagMessage() << "no root XML tag found");
110 return false;
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700111 }
112 return true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700113 }
114
115 if (el->namespace_uri.empty()) {
Adam Lesinski63699b12017-05-08 18:36:33 -0700116 std::map<std::string, XmlNodeAction>::const_iterator iter = map_.find(el->name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700117 if (iter != map_.end()) {
Adam Lesinskied37f482017-11-02 12:07:08 -0700118 std::vector<StringPiece> bread_crumb;
119 bread_crumb.push_back(iter->first);
120 return iter->second.Execute(policy, &bread_crumb, &source_diag, el);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700121 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700122
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700123 if (policy == XmlActionExecutorPolicy::kAllowList) {
Adam Lesinski63699b12017-05-08 18:36:33 -0700124 DiagMessage error_msg(el->line_number);
Adam Lesinskied37f482017-11-02 12:07:08 -0700125 error_msg << "unexpected root element ";
Adam Lesinski63699b12017-05-08 18:36:33 -0700126 PrintElementToDiagMessage(el, &error_msg);
Adam Lesinski63699b12017-05-08 18:36:33 -0700127 source_diag.Error(error_msg);
128 return false;
129 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700130 }
131 return true;
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700132}
133
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700134} // namespace xml
135} // namespace aapt