blob: 9bdbd22b5697c542a42f8f4cd76f04296a6d8f7f [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,
Jeremy Meyer56f36e82022-05-20 20:35:42 +000025 const XmlActionExecutorPolicy& policy, android::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,
Jeremy Meyer56f36e82022-05-20 20:35:42 +000030 const XmlActionExecutorPolicy& policy,
31 android::SourcePathDiagnostics* diag) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070032 return f(el, diag);
Adam Lesinskicc5609d2016-04-05 12:41:07 -070033}
34
Rhed Jao2c434422020-11-12 10:48:03 +080035static bool wrapper_three(const XmlNodeAction::ActionFuncWithPolicyAndDiag& f, Element* el,
Jeremy Meyer56f36e82022-05-20 20:35:42 +000036 const XmlActionExecutorPolicy& policy,
37 android::SourcePathDiagnostics* diag) {
Rhed Jao2c434422020-11-12 10:48:03 +080038 return f(el, policy, diag);
39}
40
Adam Lesinskice5e56e2016-10-21 17:56:45 -070041void XmlNodeAction::Action(XmlNodeAction::ActionFunc f) {
Rhed Jao2c434422020-11-12 10:48:03 +080042 actions_.emplace_back(std::bind(wrapper_one, std::move(f), std::placeholders::_1,
43 std::placeholders::_2, std::placeholders::_3));
Adam Lesinskicc5609d2016-04-05 12:41:07 -070044}
45
Adam Lesinskice5e56e2016-10-21 17:56:45 -070046void XmlNodeAction::Action(XmlNodeAction::ActionFuncWithDiag f) {
Rhed Jao2c434422020-11-12 10:48:03 +080047 actions_.emplace_back(std::bind(wrapper_two, std::move(f), std::placeholders::_1,
48 std::placeholders::_2, std::placeholders::_3));
49}
50
51void XmlNodeAction::Action(XmlNodeAction::ActionFuncWithPolicyAndDiag f) {
52 actions_.emplace_back(std::bind(wrapper_three, std::move(f), std::placeholders::_1,
53 std::placeholders::_2, std::placeholders::_3));
Adam Lesinskicc5609d2016-04-05 12:41:07 -070054}
55
Jeremy Meyer56f36e82022-05-20 20:35:42 +000056static void PrintElementToDiagMessage(const Element* el, android::DiagMessage* msg) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070057 *msg << "<";
58 if (!el->namespace_uri.empty()) {
59 *msg << el->namespace_uri << ":";
60 }
61 *msg << el->name << ">";
Adam Lesinskicc5609d2016-04-05 12:41:07 -070062}
63
Adam Lesinskied37f482017-11-02 12:07:08 -070064bool XmlNodeAction::Execute(XmlActionExecutorPolicy policy, std::vector<StringPiece>* bread_crumb,
Jeremy Meyer56f36e82022-05-20 20:35:42 +000065 android::SourcePathDiagnostics* diag, Element* el) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070066 bool error = false;
Rhed Jao2c434422020-11-12 10:48:03 +080067 for (const ActionFuncWithPolicyAndDiag& action : actions_) {
68 error |= !action(el, policy, diag);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070069 }
70
71 for (Element* child_el : el->GetChildElements()) {
72 if (child_el->namespace_uri.empty()) {
Adam Lesinski63699b12017-05-08 18:36:33 -070073 std::map<std::string, XmlNodeAction>::const_iterator iter = map_.find(child_el->name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070074 if (iter != map_.end()) {
Adam Lesinskied37f482017-11-02 12:07:08 -070075 // Use the iterator's copy of the element name, because the element may be modified.
76 bread_crumb->push_back(iter->first);
77 error |= !iter->second.Execute(policy, bread_crumb, diag, child_el);
78 bread_crumb->pop_back();
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079 continue;
80 }
Adam Lesinskicc5609d2016-04-05 12:41:07 -070081
Izabela Orlowskaad9e1322017-12-19 16:22:42 +000082 if (policy != XmlActionExecutorPolicy::kNone) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +000083 android::DiagMessage error_msg(child_el->line_number);
Adam Lesinskied37f482017-11-02 12:07:08 -070084 error_msg << "unexpected element ";
Adam Lesinski63699b12017-05-08 18:36:33 -070085 PrintElementToDiagMessage(child_el, &error_msg);
Adam Lesinskied37f482017-11-02 12:07:08 -070086 error_msg << " found in ";
87 for (const StringPiece& element : *bread_crumb) {
88 error_msg << "<" << element << ">";
89 }
Ryan Mitchell4ea90752020-07-31 08:21:43 -070090 if (policy == XmlActionExecutorPolicy::kAllowListWarning) {
Izabela Orlowskaad9e1322017-12-19 16:22:42 +000091 // Treat the error only as a warning.
92 diag->Warn(error_msg);
93 } else {
Ryan Mitchell4ea90752020-07-31 08:21:43 -070094 // Policy is XmlActionExecutorPolicy::kAllowList, we should fail.
Izabela Orlowskaad9e1322017-12-19 16:22:42 +000095 diag->Error(error_msg);
96 error = true;
97 }
Adam Lesinski63699b12017-05-08 18:36:33 -070098 }
Adam Lesinskicc5609d2016-04-05 12:41:07 -070099 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700100 }
101 return !error;
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700102}
103
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000104bool XmlActionExecutor::Execute(XmlActionExecutorPolicy policy, android::IDiagnostics* diag,
Adam Lesinski63699b12017-05-08 18:36:33 -0700105 XmlResource* doc) const {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000106 android::SourcePathDiagnostics source_diag(doc->file.source, diag);
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700107
Adam Lesinski6b372992017-08-09 10:54:23 -0700108 Element* el = doc->root.get();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700109 if (!el) {
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700110 if (policy == XmlActionExecutorPolicy::kAllowList) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000111 source_diag.Error(android::DiagMessage() << "no root XML tag found");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700112 return false;
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700113 }
114 return true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700115 }
116
117 if (el->namespace_uri.empty()) {
Adam Lesinski63699b12017-05-08 18:36:33 -0700118 std::map<std::string, XmlNodeAction>::const_iterator iter = map_.find(el->name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700119 if (iter != map_.end()) {
Adam Lesinskied37f482017-11-02 12:07:08 -0700120 std::vector<StringPiece> bread_crumb;
121 bread_crumb.push_back(iter->first);
122 return iter->second.Execute(policy, &bread_crumb, &source_diag, el);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700123 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700124
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700125 if (policy == XmlActionExecutorPolicy::kAllowList) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000126 android::DiagMessage error_msg(el->line_number);
Adam Lesinskied37f482017-11-02 12:07:08 -0700127 error_msg << "unexpected root element ";
Adam Lesinski63699b12017-05-08 18:36:33 -0700128 PrintElementToDiagMessage(el, &error_msg);
Adam Lesinski63699b12017-05-08 18:36:33 -0700129 source_diag.Error(error_msg);
130 return false;
131 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700132 }
133 return true;
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700134}
135
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700136} // namespace xml
137} // namespace aapt