• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 "utils/resources.h"
18 
19 #include "utils/base/logging.h"
20 #include "utils/zlib/buffer_generated.h"
21 
22 namespace libtextclassifier3 {
23 namespace {
isWildcardMatch(const flatbuffers::String * left,const std::string & right)24 bool isWildcardMatch(const flatbuffers::String* left,
25                      const std::string& right) {
26   return (left == nullptr || right.empty());
27 }
28 
isExactMatch(const flatbuffers::String * left,const std::string & right)29 bool isExactMatch(const flatbuffers::String* left, const std::string& right) {
30   if (left == nullptr) {
31     return right.empty();
32   }
33   return left->str() == right;
34 }
35 
36 }  // namespace
37 
LocaleMatch(const Locale & locale,const LanguageTag * entry_locale) const38 int Resources::LocaleMatch(const Locale& locale,
39                            const LanguageTag* entry_locale) const {
40   int match = LOCALE_NO_MATCH;
41   if (isExactMatch(entry_locale->language(), locale.Language())) {
42     match |= LOCALE_LANGUAGE_MATCH;
43   } else if (isWildcardMatch(entry_locale->language(), locale.Language())) {
44     match |= LOCALE_LANGUAGE_WILDCARD_MATCH;
45   }
46 
47   if (isExactMatch(entry_locale->script(), locale.Script())) {
48     match |= LOCALE_SCRIPT_MATCH;
49   } else if (isWildcardMatch(entry_locale->script(), locale.Script())) {
50     match |= LOCALE_SCRIPT_WILDCARD_MATCH;
51   }
52 
53   if (isExactMatch(entry_locale->region(), locale.Region())) {
54     match |= LOCALE_REGION_MATCH;
55   } else if (isWildcardMatch(entry_locale->region(), locale.Region())) {
56     match |= LOCALE_REGION_WILDCARD_MATCH;
57   }
58 
59   return match;
60 }
61 
FindResource(const StringPiece resource_name) const62 const ResourceEntry* Resources::FindResource(
63     const StringPiece resource_name) const {
64   if (resources_ == nullptr || resources_->resource_entry() == nullptr) {
65     TC3_LOG(ERROR) << "No resources defined.";
66     return nullptr;
67   }
68   const ResourceEntry* entry =
69       resources_->resource_entry()->LookupByKey(resource_name.data());
70   if (entry == nullptr) {
71     TC3_LOG(ERROR) << "Resource " << resource_name.ToString() << " not found";
72     return nullptr;
73   }
74   return entry;
75 }
76 
BestResourceForLocales(const ResourceEntry * resource,const std::vector<Locale> & locales) const77 int Resources::BestResourceForLocales(
78     const ResourceEntry* resource, const std::vector<Locale>& locales) const {
79   // Find best match based on locale.
80   int resource_id = -1;
81   int locale_match = LOCALE_NO_MATCH;
82   const auto* resources = resource->resource();
83   for (int user_locale = 0; user_locale < locales.size(); user_locale++) {
84     if (!locales[user_locale].IsValid()) {
85       continue;
86     }
87     for (int i = 0; i < resources->size(); i++) {
88       for (const int locale_id : *resources->Get(i)->locale()) {
89         const int candidate_match = LocaleMatch(
90             locales[user_locale], resources_->locale()->Get(locale_id));
91 
92         // Only consider if at least the language matches.
93         if ((candidate_match & LOCALE_LANGUAGE_MATCH) == 0 &&
94             (candidate_match & LOCALE_LANGUAGE_WILDCARD_MATCH) == 0) {
95           continue;
96         }
97 
98         if (candidate_match > locale_match) {
99           locale_match = candidate_match;
100           resource_id = i;
101         }
102       }
103     }
104 
105     // If the language matches exactly, we are already finished.
106     // We found an exact language match.
107     if (locale_match & LOCALE_LANGUAGE_MATCH) {
108       return resource_id;
109     }
110   }
111   return resource_id;
112 }
113 
GetResourceContent(const std::vector<Locale> & locales,const StringPiece resource_name,std::string * result) const114 bool Resources::GetResourceContent(const std::vector<Locale>& locales,
115                                    const StringPiece resource_name,
116                                    std::string* result) const {
117   const ResourceEntry* entry = FindResource(resource_name);
118   if (entry == nullptr || entry->resource() == nullptr) {
119     return false;
120   }
121 
122   int resource_id = BestResourceForLocales(entry, locales);
123   if (resource_id < 0) {
124     return false;
125   }
126   const auto* resource = entry->resource()->Get(resource_id);
127   if (resource->content() != nullptr) {
128     *result = resource->content()->str();
129     return true;
130   }
131   return false;
132 }
133 
134 }  // namespace libtextclassifier3
135