blob: fab17c949dd80db1730d8bb1e0bc496b7d49c796 [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
Adam Lesinski63699b12017-05-08 18:36:33 -070024static bool wrapper_one(XmlNodeAction::ActionFunc& f, Element* el, SourcePathDiagnostics*) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070025 return f(el);
Adam Lesinskicc5609d2016-04-05 12:41:07 -070026}
27
Adam Lesinskice5e56e2016-10-21 17:56:45 -070028static bool wrapper_two(XmlNodeAction::ActionFuncWithDiag& f, Element* el,
29 SourcePathDiagnostics* diag) {
30 return f(el, diag);
Adam Lesinskicc5609d2016-04-05 12:41:07 -070031}
32
Adam Lesinskice5e56e2016-10-21 17:56:45 -070033void XmlNodeAction::Action(XmlNodeAction::ActionFunc f) {
34 actions_.emplace_back(std::bind(
35 wrapper_one, std::move(f), std::placeholders::_1, std::placeholders::_2));
Adam Lesinskicc5609d2016-04-05 12:41:07 -070036}
37
Adam Lesinskice5e56e2016-10-21 17:56:45 -070038void XmlNodeAction::Action(XmlNodeAction::ActionFuncWithDiag f) {
39 actions_.emplace_back(std::bind(
40 wrapper_two, std::move(f), std::placeholders::_1, std::placeholders::_2));
Adam Lesinskicc5609d2016-04-05 12:41:07 -070041}
42
Adam Lesinskice5e56e2016-10-21 17:56:45 -070043static void PrintElementToDiagMessage(const Element* el, DiagMessage* msg) {
44 *msg << "<";
45 if (!el->namespace_uri.empty()) {
46 *msg << el->namespace_uri << ":";
47 }
48 *msg << el->name << ">";
Adam Lesinskicc5609d2016-04-05 12:41:07 -070049}
50
Adam Lesinskied37f482017-11-02 12:07:08 -070051bool XmlNodeAction::Execute(XmlActionExecutorPolicy policy, std::vector<StringPiece>* bread_crumb,
52 SourcePathDiagnostics* diag, Element* el) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070053 bool error = false;
54 for (const ActionFuncWithDiag& action : actions_) {
55 error |= !action(el, diag);
56 }
57
58 for (Element* child_el : el->GetChildElements()) {
59 if (child_el->namespace_uri.empty()) {
Adam Lesinski63699b12017-05-08 18:36:33 -070060 std::map<std::string, XmlNodeAction>::const_iterator iter = map_.find(child_el->name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070061 if (iter != map_.end()) {
Adam Lesinskied37f482017-11-02 12:07:08 -070062 // Use the iterator's copy of the element name, because the element may be modified.
63 bread_crumb->push_back(iter->first);
64 error |= !iter->second.Execute(policy, bread_crumb, diag, child_el);
65 bread_crumb->pop_back();
Adam Lesinskice5e56e2016-10-21 17:56:45 -070066 continue;
67 }
Adam Lesinskicc5609d2016-04-05 12:41:07 -070068
Izabela Orlowskaad9e1322017-12-19 16:22:42 +000069 if (policy != XmlActionExecutorPolicy::kNone) {
Adam Lesinski63699b12017-05-08 18:36:33 -070070 DiagMessage error_msg(child_el->line_number);
Adam Lesinskied37f482017-11-02 12:07:08 -070071 error_msg << "unexpected element ";
Adam Lesinski63699b12017-05-08 18:36:33 -070072 PrintElementToDiagMessage(child_el, &error_msg);
Adam Lesinskied37f482017-11-02 12:07:08 -070073 error_msg << " found in ";
74 for (const StringPiece& element : *bread_crumb) {
75 error_msg << "<" << element << ">";
76 }
Ryan Mitchell4ea90752020-07-31 08:21:43 -070077 if (policy == XmlActionExecutorPolicy::kAllowListWarning) {
Izabela Orlowskaad9e1322017-12-19 16:22:42 +000078 // Treat the error only as a warning.
79 diag->Warn(error_msg);
80 } else {
Ryan Mitchell4ea90752020-07-31 08:21:43 -070081 // Policy is XmlActionExecutorPolicy::kAllowList, we should fail.
Izabela Orlowskaad9e1322017-12-19 16:22:42 +000082 diag->Error(error_msg);
83 error = true;
84 }
Adam Lesinski63699b12017-05-08 18:36:33 -070085 }
Adam Lesinskicc5609d2016-04-05 12:41:07 -070086 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070087 }
88 return !error;
Adam Lesinskicc5609d2016-04-05 12:41:07 -070089}
90
Adam Lesinski63699b12017-05-08 18:36:33 -070091bool XmlActionExecutor::Execute(XmlActionExecutorPolicy policy, IDiagnostics* diag,
92 XmlResource* doc) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070093 SourcePathDiagnostics source_diag(doc->file.source, diag);
Adam Lesinskicc5609d2016-04-05 12:41:07 -070094
Adam Lesinski6b372992017-08-09 10:54:23 -070095 Element* el = doc->root.get();
Adam Lesinskice5e56e2016-10-21 17:56:45 -070096 if (!el) {
Ryan Mitchell4ea90752020-07-31 08:21:43 -070097 if (policy == XmlActionExecutorPolicy::kAllowList) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070098 source_diag.Error(DiagMessage() << "no root XML tag found");
99 return false;
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700100 }
101 return true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700102 }
103
104 if (el->namespace_uri.empty()) {
Adam Lesinski63699b12017-05-08 18:36:33 -0700105 std::map<std::string, XmlNodeAction>::const_iterator iter = map_.find(el->name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700106 if (iter != map_.end()) {
Adam Lesinskied37f482017-11-02 12:07:08 -0700107 std::vector<StringPiece> bread_crumb;
108 bread_crumb.push_back(iter->first);
109 return iter->second.Execute(policy, &bread_crumb, &source_diag, el);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700110 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700111
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700112 if (policy == XmlActionExecutorPolicy::kAllowList) {
Adam Lesinski63699b12017-05-08 18:36:33 -0700113 DiagMessage error_msg(el->line_number);
Adam Lesinskied37f482017-11-02 12:07:08 -0700114 error_msg << "unexpected root element ";
Adam Lesinski63699b12017-05-08 18:36:33 -0700115 PrintElementToDiagMessage(el, &error_msg);
Adam Lesinski63699b12017-05-08 18:36:33 -0700116 source_diag.Error(error_msg);
117 return false;
118 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700119 }
120 return true;
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700121}
122
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700123} // namespace xml
124} // namespace aapt