• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "link/Linkers.h"
18 
19 #include "ResourceTable.h"
20 
21 namespace aapt {
22 
SelectProductToKeep(const ResourceNameRef & name,const ResourceConfigValueIter begin,const ResourceConfigValueIter end,IDiagnostics * diag)23 ProductFilter::ResourceConfigValueIter ProductFilter::SelectProductToKeep(
24     const ResourceNameRef& name, const ResourceConfigValueIter begin,
25     const ResourceConfigValueIter end, IDiagnostics* diag) {
26   ResourceConfigValueIter default_product_iter = end;
27   ResourceConfigValueIter selected_product_iter = end;
28 
29   for (ResourceConfigValueIter iter = begin; iter != end; ++iter) {
30     ResourceConfigValue* config_value = iter->get();
31     if (products_.find(config_value->product) != products_.end()) {
32       if (selected_product_iter != end) {
33         // We have two possible values for this product!
34         diag->Error(DiagMessage(config_value->value->GetSource())
35                     << "selection of product '" << config_value->product
36                     << "' for resource " << name << " is ambiguous");
37 
38         ResourceConfigValue* previously_selected_config_value =
39             selected_product_iter->get();
40         diag->Note(
41             DiagMessage(previously_selected_config_value->value->GetSource())
42             << "product '" << previously_selected_config_value->product
43             << "' is also a candidate");
44         return end;
45       }
46 
47       // Select this product.
48       selected_product_iter = iter;
49     }
50 
51     if (config_value->product.empty() || config_value->product == "default") {
52       if (default_product_iter != end) {
53         // We have two possible default values.
54         diag->Error(DiagMessage(config_value->value->GetSource())
55                     << "multiple default products defined for resource "
56                     << name);
57 
58         ResourceConfigValue* previously_default_config_value =
59             default_product_iter->get();
60         diag->Note(
61             DiagMessage(previously_default_config_value->value->GetSource())
62             << "default product also defined here");
63         return end;
64       }
65 
66       // Mark the default.
67       default_product_iter = iter;
68     }
69   }
70 
71   if (default_product_iter == end) {
72     diag->Error(DiagMessage() << "no default product defined for resource "
73                               << name);
74     return end;
75   }
76 
77   if (selected_product_iter == end) {
78     selected_product_iter = default_product_iter;
79   }
80   return selected_product_iter;
81 }
82 
Consume(IAaptContext * context,ResourceTable * table)83 bool ProductFilter::Consume(IAaptContext* context, ResourceTable* table) {
84   bool error = false;
85   for (auto& pkg : table->packages) {
86     for (auto& type : pkg->types) {
87       for (auto& entry : type->entries) {
88         std::vector<std::unique_ptr<ResourceConfigValue>> new_values;
89 
90         ResourceConfigValueIter iter = entry->values.begin();
91         ResourceConfigValueIter start_range_iter = iter;
92         while (iter != entry->values.end()) {
93           ++iter;
94           if (iter == entry->values.end() ||
95               (*iter)->config != (*start_range_iter)->config) {
96             // End of the array, or we saw a different config,
97             // so this must be the end of a range of products.
98             // Select the product to keep from the set of products defined.
99             ResourceNameRef name(pkg->name, type->type, entry->name);
100             auto value_to_keep = SelectProductToKeep(
101                 name, start_range_iter, iter, context->GetDiagnostics());
102             if (value_to_keep == iter) {
103               // An error occurred, we could not pick a product.
104               error = true;
105             } else {
106               // We selected a product to keep. Move it to the new array.
107               new_values.push_back(std::move(*value_to_keep));
108             }
109 
110             // Start the next range of products.
111             start_range_iter = iter;
112           }
113         }
114 
115         // Now move the new values in to place.
116         entry->values = std::move(new_values);
117       }
118     }
119   }
120   return !error;
121 }
122 
123 }  // namespace aapt
124