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