• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 AAPT2_FILTER_H
18 #define AAPT2_FILTER_H
19 
20 #include <string>
21 #include <string_view>
22 #include <vector>
23 
24 #include "util/Util.h"
25 
26 namespace aapt {
27 
28 /** A filter to be applied to a path segment. */
29 class IPathFilter {
30  public:
31   virtual ~IPathFilter() = default;
32 
33   /** Returns true if the path should be kept. */
34   virtual bool Keep(std::string_view path) = 0;
35 };
36 
37 /**
38  * Path filter that keeps anything that matches the provided prefix.
39  */
40 class PrefixFilter : public IPathFilter {
41  public:
PrefixFilter(std::string prefix)42   explicit PrefixFilter(std::string prefix) : prefix_(std::move(prefix)) {
43   }
44 
45   /** Returns true if the provided path matches the prefix. */
Keep(std::string_view path)46   bool Keep(std::string_view path) override {
47     return util::StartsWith(path, prefix_);
48   }
49 
50  private:
51   const std::string prefix_;
52 };
53 
54 /** Applies a set of IPathFilters to a path and returns true iif all filters keep the path. */
55 class FilterChain : public IPathFilter {
56  public:
57   /** Adds a filter to the list to be applied to each path. */
AddFilter(std::unique_ptr<IPathFilter> filter)58   void AddFilter(std::unique_ptr<IPathFilter> filter) {
59     filters_.push_back(std::move(filter));
60   }
61 
62   /** Returns true if all filters keep the path. */
Keep(std::string_view path)63   bool Keep(std::string_view path) override {
64     for (auto& filter : filters_) {
65       if (!filter->Keep(path)) {
66         return false;
67       }
68     }
69     return true;
70   }
71 
72  private:
73   std::vector<std::unique_ptr<IPathFilter>> filters_;
74 };
75 
76 }  // namespace aapt
77 
78 #endif  // AAPT2_FILTER_H
79