blob: 602a902bfc3e842237fc62632e484e1dda0e9c0c [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
Adam Lesinski63699b12017-05-08 18:36:33 -070069 if (policy == XmlActionExecutorPolicy::kWhitelist) {
70 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 }
Adam Lesinski63699b12017-05-08 18:36:33 -070077 diag->Error(error_msg);
78 error = true;
79 }
Adam Lesinskicc5609d2016-04-05 12:41:07 -070080 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070081 }
82 return !error;
Adam Lesinskicc5609d2016-04-05 12:41:07 -070083}
84
Adam Lesinski63699b12017-05-08 18:36:33 -070085bool XmlActionExecutor::Execute(XmlActionExecutorPolicy policy, IDiagnostics* diag,
86 XmlResource* doc) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070087 SourcePathDiagnostics source_diag(doc->file.source, diag);
Adam Lesinskicc5609d2016-04-05 12:41:07 -070088
Adam Lesinski6b372992017-08-09 10:54:23 -070089 Element* el = doc->root.get();
Adam Lesinskice5e56e2016-10-21 17:56:45 -070090 if (!el) {
91 if (policy == XmlActionExecutorPolicy::kWhitelist) {
92 source_diag.Error(DiagMessage() << "no root XML tag found");
93 return false;
Adam Lesinskicc5609d2016-04-05 12:41:07 -070094 }
95 return true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070096 }
97
98 if (el->namespace_uri.empty()) {
Adam Lesinski63699b12017-05-08 18:36:33 -070099 std::map<std::string, XmlNodeAction>::const_iterator iter = map_.find(el->name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700100 if (iter != map_.end()) {
Adam Lesinskied37f482017-11-02 12:07:08 -0700101 std::vector<StringPiece> bread_crumb;
102 bread_crumb.push_back(iter->first);
103 return iter->second.Execute(policy, &bread_crumb, &source_diag, el);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700104 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700105
Adam Lesinski63699b12017-05-08 18:36:33 -0700106 if (policy == XmlActionExecutorPolicy::kWhitelist) {
107 DiagMessage error_msg(el->line_number);
Adam Lesinskied37f482017-11-02 12:07:08 -0700108 error_msg << "unexpected root element ";
Adam Lesinski63699b12017-05-08 18:36:33 -0700109 PrintElementToDiagMessage(el, &error_msg);
Adam Lesinski63699b12017-05-08 18:36:33 -0700110 source_diag.Error(error_msg);
111 return false;
112 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700113 }
114 return true;
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700115}
116
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700117} // namespace xml
118} // namespace aapt