1 /*
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 
19 using ::android::StringPiece;
20 
21 namespace aapt {
22 namespace xml {
23 
wrapper_one(const XmlNodeAction::ActionFunc & f,Element * el,const XmlActionExecutorPolicy & policy,SourcePathDiagnostics *)24 static bool wrapper_one(const XmlNodeAction::ActionFunc& f, Element* el,
25                         const XmlActionExecutorPolicy& policy, SourcePathDiagnostics*) {
26   return f(el);
27 }
28 
wrapper_two(const XmlNodeAction::ActionFuncWithDiag & f,Element * el,const XmlActionExecutorPolicy & policy,SourcePathDiagnostics * diag)29 static bool wrapper_two(const XmlNodeAction::ActionFuncWithDiag& f, Element* el,
30                         const XmlActionExecutorPolicy& policy, SourcePathDiagnostics* diag) {
31   return f(el, diag);
32 }
33 
wrapper_three(const XmlNodeAction::ActionFuncWithPolicyAndDiag & f,Element * el,const XmlActionExecutorPolicy & policy,SourcePathDiagnostics * diag)34 static bool wrapper_three(const XmlNodeAction::ActionFuncWithPolicyAndDiag& f, Element* el,
35                           const XmlActionExecutorPolicy& policy, SourcePathDiagnostics* diag) {
36   return f(el, policy, diag);
37 }
38 
Action(XmlNodeAction::ActionFunc f)39 void XmlNodeAction::Action(XmlNodeAction::ActionFunc f) {
40   actions_.emplace_back(std::bind(wrapper_one, std::move(f), std::placeholders::_1,
41                                   std::placeholders::_2, std::placeholders::_3));
42 }
43 
Action(XmlNodeAction::ActionFuncWithDiag f)44 void XmlNodeAction::Action(XmlNodeAction::ActionFuncWithDiag f) {
45   actions_.emplace_back(std::bind(wrapper_two, std::move(f), std::placeholders::_1,
46                                   std::placeholders::_2, std::placeholders::_3));
47 }
48 
Action(XmlNodeAction::ActionFuncWithPolicyAndDiag f)49 void 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));
52 }
53 
PrintElementToDiagMessage(const Element * el,DiagMessage * msg)54 static 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 << ">";
60 }
61 
Execute(XmlActionExecutorPolicy policy,std::vector<StringPiece> * bread_crumb,SourcePathDiagnostics * diag,Element * el) const62 bool XmlNodeAction::Execute(XmlActionExecutorPolicy policy, std::vector<StringPiece>* bread_crumb,
63                             SourcePathDiagnostics* diag, Element* el) const {
64   bool error = false;
65   for (const ActionFuncWithPolicyAndDiag& action : actions_) {
66     error |= !action(el, policy, diag);
67   }
68 
69   for (Element* child_el : el->GetChildElements()) {
70     if (child_el->namespace_uri.empty()) {
71       std::map<std::string, XmlNodeAction>::const_iterator iter = map_.find(child_el->name);
72       if (iter != map_.end()) {
73         // 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();
77         continue;
78       }
79 
80       if (policy != XmlActionExecutorPolicy::kNone) {
81         DiagMessage error_msg(child_el->line_number);
82         error_msg << "unexpected element ";
83         PrintElementToDiagMessage(child_el, &error_msg);
84         error_msg << " found in ";
85         for (const StringPiece& element : *bread_crumb) {
86           error_msg << "<" << element << ">";
87         }
88         if (policy == XmlActionExecutorPolicy::kAllowListWarning) {
89           // Treat the error only as a warning.
90           diag->Warn(error_msg);
91         } else {
92           // Policy is XmlActionExecutorPolicy::kAllowList, we should fail.
93           diag->Error(error_msg);
94           error = true;
95         }
96       }
97     }
98   }
99   return !error;
100 }
101 
Execute(XmlActionExecutorPolicy policy,IDiagnostics * diag,XmlResource * doc) const102 bool XmlActionExecutor::Execute(XmlActionExecutorPolicy policy, IDiagnostics* diag,
103                                 XmlResource* doc) const {
104   SourcePathDiagnostics source_diag(doc->file.source, diag);
105 
106   Element* el = doc->root.get();
107   if (!el) {
108     if (policy == XmlActionExecutorPolicy::kAllowList) {
109       source_diag.Error(DiagMessage() << "no root XML tag found");
110       return false;
111     }
112     return true;
113   }
114 
115   if (el->namespace_uri.empty()) {
116     std::map<std::string, XmlNodeAction>::const_iterator iter = map_.find(el->name);
117     if (iter != map_.end()) {
118       std::vector<StringPiece> bread_crumb;
119       bread_crumb.push_back(iter->first);
120       return iter->second.Execute(policy, &bread_crumb, &source_diag, el);
121     }
122 
123     if (policy == XmlActionExecutorPolicy::kAllowList) {
124       DiagMessage error_msg(el->line_number);
125       error_msg << "unexpected root element ";
126       PrintElementToDiagMessage(el, &error_msg);
127       source_diag.Error(error_msg);
128       return false;
129     }
130   }
131   return true;
132 }
133 
134 }  // namespace xml
135 }  // namespace aapt
136