Home | History | Annotate | Download | only in xml
      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 namespace aapt {
     20 namespace xml {
     21 
     22 static bool wrapper_one(XmlNodeAction::ActionFunc& f, Element* el, SourcePathDiagnostics*) {
     23   return f(el);
     24 }
     25 
     26 static bool wrapper_two(XmlNodeAction::ActionFuncWithDiag& f, Element* el,
     27                         SourcePathDiagnostics* diag) {
     28   return f(el, diag);
     29 }
     30 
     31 void XmlNodeAction::Action(XmlNodeAction::ActionFunc f) {
     32   actions_.emplace_back(std::bind(
     33       wrapper_one, std::move(f), std::placeholders::_1, std::placeholders::_2));
     34 }
     35 
     36 void XmlNodeAction::Action(XmlNodeAction::ActionFuncWithDiag f) {
     37   actions_.emplace_back(std::bind(
     38       wrapper_two, std::move(f), std::placeholders::_1, std::placeholders::_2));
     39 }
     40 
     41 static void PrintElementToDiagMessage(const Element* el, DiagMessage* msg) {
     42   *msg << "<";
     43   if (!el->namespace_uri.empty()) {
     44     *msg << el->namespace_uri << ":";
     45   }
     46   *msg << el->name << ">";
     47 }
     48 
     49 bool XmlNodeAction::Execute(XmlActionExecutorPolicy policy, SourcePathDiagnostics* diag,
     50                             Element* el) const {
     51   bool error = false;
     52   for (const ActionFuncWithDiag& action : actions_) {
     53     error |= !action(el, diag);
     54   }
     55 
     56   for (Element* child_el : el->GetChildElements()) {
     57     if (child_el->namespace_uri.empty()) {
     58       std::map<std::string, XmlNodeAction>::const_iterator iter = map_.find(child_el->name);
     59       if (iter != map_.end()) {
     60         error |= !iter->second.Execute(policy, diag, child_el);
     61         continue;
     62       }
     63 
     64       if (policy == XmlActionExecutorPolicy::kWhitelist) {
     65         DiagMessage error_msg(child_el->line_number);
     66         error_msg << "unknown element ";
     67         PrintElementToDiagMessage(child_el, &error_msg);
     68         error_msg << " found";
     69         diag->Error(error_msg);
     70         error = true;
     71       }
     72     }
     73   }
     74   return !error;
     75 }
     76 
     77 bool XmlActionExecutor::Execute(XmlActionExecutorPolicy policy, IDiagnostics* diag,
     78                                 XmlResource* doc) const {
     79   SourcePathDiagnostics source_diag(doc->file.source, diag);
     80 
     81   Element* el = FindRootElement(doc);
     82   if (!el) {
     83     if (policy == XmlActionExecutorPolicy::kWhitelist) {
     84       source_diag.Error(DiagMessage() << "no root XML tag found");
     85       return false;
     86     }
     87     return true;
     88   }
     89 
     90   if (el->namespace_uri.empty()) {
     91     std::map<std::string, XmlNodeAction>::const_iterator iter = map_.find(el->name);
     92     if (iter != map_.end()) {
     93       return iter->second.Execute(policy, &source_diag, el);
     94     }
     95 
     96     if (policy == XmlActionExecutorPolicy::kWhitelist) {
     97       DiagMessage error_msg(el->line_number);
     98       error_msg << "unknown element ";
     99       PrintElementToDiagMessage(el, &error_msg);
    100       error_msg << " found";
    101       source_diag.Error(error_msg);
    102       return false;
    103     }
    104   }
    105   return true;
    106 }
    107 
    108 }  // namespace xml
    109 }  // namespace aapt
    110