Home | History | Annotate | Download | only in link
      1 /*
      2  * Copyright (C) 2017 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 #ifndef AAPT_LINKER_XMLCOMPATVERSIONER_H
     18 #define AAPT_LINKER_XMLCOMPATVERSIONER_H
     19 
     20 #include <set>
     21 #include <unordered_map>
     22 #include <vector>
     23 
     24 #include "android-base/macros.h"
     25 
     26 #include "Resource.h"
     27 #include "SdkConstants.h"
     28 #include "process/IResourceTableConsumer.h"
     29 #include "util/Util.h"
     30 #include "xml/XmlDom.h"
     31 
     32 namespace aapt {
     33 
     34 class IDegradeRule;
     35 
     36 struct DegradeResult {
     37   xml::Attribute attr;
     38   ApiVersion attr_api_version;
     39 };
     40 
     41 class IDegradeRule {
     42  public:
     43   IDegradeRule() = default;
     44   virtual ~IDegradeRule() = default;
     45 
     46   virtual std::vector<DegradeResult> Degrade(const xml::Element& src_el,
     47                                              const xml::Attribute& src_attr,
     48                                              StringPool* out_string_pool) const = 0;
     49 
     50  private:
     51   DISALLOW_COPY_AND_ASSIGN(IDegradeRule);
     52 };
     53 
     54 class XmlCompatVersioner {
     55  public:
     56   using Rules = std::unordered_map<ResourceId, std::unique_ptr<IDegradeRule>>;
     57 
     58   XmlCompatVersioner(const Rules* rules);
     59 
     60   std::vector<std::unique_ptr<xml::XmlResource>> Process(IAaptContext* context,
     61                                                          xml::XmlResource* doc,
     62                                                          util::Range<ApiVersion> api_range);
     63 
     64  private:
     65   DISALLOW_COPY_AND_ASSIGN(XmlCompatVersioner);
     66 
     67   std::unique_ptr<xml::XmlResource> ProcessDoc(ApiVersion target_api, ApiVersion max_api,
     68                                                xml::XmlResource* doc,
     69                                                std::set<ApiVersion>* out_apis_referenced);
     70   void ProcessRule(const xml::Element& src_el, const xml::Attribute& src_attr,
     71                    const ApiVersion& src_attr_version, const IDegradeRule* rule,
     72                    const util::Range<ApiVersion>& api_range, bool generated, xml::Element* dst_el,
     73                    std::set<ApiVersion>* out_apis_referenced, StringPool* out_string_pool);
     74 
     75   const Rules* rules_;
     76 };
     77 
     78 struct ReplacementAttr {
     79   std::string name;
     80   ResourceId id;
     81   Attribute attr;
     82 };
     83 
     84 class DegradeToManyRule : public IDegradeRule {
     85  public:
     86   DegradeToManyRule(std::vector<ReplacementAttr> attrs);
     87   virtual ~DegradeToManyRule() = default;
     88 
     89   std::vector<DegradeResult> Degrade(const xml::Element& src_el, const xml::Attribute& src_attr,
     90                                      StringPool* out_string_pool) const override;
     91 
     92  private:
     93   DISALLOW_COPY_AND_ASSIGN(DegradeToManyRule);
     94 
     95   std::vector<ReplacementAttr> attrs_;
     96 };
     97 
     98 }  // namespace aapt
     99 
    100 #endif  // AAPT_LINKER_XMLCOMPATVERSIONER_H
    101