1 /*
2  * Copyright (C) 2015 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 "Resource.h"
18 
19 #include <map>
20 #include <sstream>
21 #include <string>
22 
23 #include "android-base/stringprintf.h"
24 
25 using ::android::StringPiece;
26 using ::android::base::StringPrintf;
27 
28 namespace aapt {
29 
to_string() const30 std::string ResourceId::to_string() const {
31   return StringPrintf("0x%08x", id);
32 }
33 
to_string() const34 std::string ResourceName::to_string() const {
35   return ResourceNameRef(*this).to_string();
36 }
37 
to_string() const38 std::string ResourceNameRef::to_string() const {
39   std::ostringstream str_stream;
40   if (!package.empty()) {
41     str_stream << package << ":";
42   }
43   str_stream << type << "/" << entry;
44   return str_stream.str();
45 }
46 
to_string(ResourceType type)47 StringPiece to_string(ResourceType type) {
48   switch (type) {
49     case ResourceType::kAnim:
50       return "anim";
51     case ResourceType::kAnimator:
52       return "animator";
53     case ResourceType::kArray:
54       return "array";
55     case ResourceType::kAttr:
56       return "attr";
57     case ResourceType::kAttrPrivate:
58       return "^attr-private";
59     case ResourceType::kBool:
60       return "bool";
61     case ResourceType::kColor:
62       return "color";
63     case ResourceType::kConfigVarying:
64       return "configVarying";
65     case ResourceType::kDimen:
66       return "dimen";
67     case ResourceType::kDrawable:
68       return "drawable";
69     case ResourceType::kFont:
70       return "font";
71     case ResourceType::kFraction:
72       return "fraction";
73     case ResourceType::kId:
74       return "id";
75     case ResourceType::kInteger:
76       return "integer";
77     case ResourceType::kInterpolator:
78       return "interpolator";
79     case ResourceType::kLayout:
80       return "layout";
81     case ResourceType::kMacro:
82       return "macro";
83     case ResourceType::kMenu:
84       return "menu";
85     case ResourceType::kMipmap:
86       return "mipmap";
87     case ResourceType::kNavigation:
88       return "navigation";
89     case ResourceType::kPlurals:
90       return "plurals";
91     case ResourceType::kRaw:
92       return "raw";
93     case ResourceType::kString:
94       return "string";
95     case ResourceType::kStyle:
96       return "style";
97     case ResourceType::kStyleable:
98       return "styleable";
99     case ResourceType::kTransition:
100       return "transition";
101     case ResourceType::kXml:
102       return "xml";
103   }
104   return {};
105 }
106 
107 static const std::map<StringPiece, ResourceType> sResourceTypeMap{
108     {"anim", ResourceType::kAnim},
109     {"animator", ResourceType::kAnimator},
110     {"array", ResourceType::kArray},
111     {"attr", ResourceType::kAttr},
112     {"^attr-private", ResourceType::kAttrPrivate},
113     {"bool", ResourceType::kBool},
114     {"color", ResourceType::kColor},
115     {"configVarying", ResourceType::kConfigVarying},
116     {"dimen", ResourceType::kDimen},
117     {"drawable", ResourceType::kDrawable},
118     {"font", ResourceType::kFont},
119     {"fraction", ResourceType::kFraction},
120     {"id", ResourceType::kId},
121     {"integer", ResourceType::kInteger},
122     {"interpolator", ResourceType::kInterpolator},
123     {"layout", ResourceType::kLayout},
124     {"macro", ResourceType::kMacro},
125     {"menu", ResourceType::kMenu},
126     {"mipmap", ResourceType::kMipmap},
127     {"navigation", ResourceType::kNavigation},
128     {"plurals", ResourceType::kPlurals},
129     {"raw", ResourceType::kRaw},
130     {"string", ResourceType::kString},
131     {"style", ResourceType::kStyle},
132     {"styleable", ResourceType::kStyleable},
133     {"transition", ResourceType::kTransition},
134     {"xml", ResourceType::kXml},
135 };
136 
ResourceNamedTypeWithDefaultName(ResourceType t)137 ResourceNamedTypeRef ResourceNamedTypeWithDefaultName(ResourceType t) {
138   return {to_string(t), t};
139 }
140 
ParseResourceNamedType(android::StringPiece s)141 std::optional<ResourceNamedTypeRef> ParseResourceNamedType(android::StringPiece s) {
142   auto dot = std::find(s.begin(), s.end(), '.');
143   const ResourceType* parsedType;
144   if (dot != s.end() && dot != std::prev(s.end())) {
145     parsedType = ParseResourceType(android::StringPiece(s.begin(), dot - s.begin()));
146   } else {
147     parsedType = ParseResourceType(s);
148   }
149   if (parsedType == nullptr) {
150     return std::nullopt;
151   }
152   return ResourceNamedTypeRef(s, *parsedType);
153 }
154 
ParseResourceType(StringPiece str)155 const ResourceType* ParseResourceType(StringPiece str) {
156   auto iter = sResourceTypeMap.find(str);
157   if (iter == std::end(sResourceTypeMap)) {
158     return nullptr;
159   }
160   return &iter->second;
161 }
162 
operator <(const ResourceKey & a,const ResourceKey & b)163 bool operator<(const ResourceKey& a, const ResourceKey& b) {
164   return std::tie(a.name, a.config) < std::tie(b.name, b.config);
165 }
166 
operator <(const ResourceKeyRef & a,const ResourceKeyRef & b)167 bool operator<(const ResourceKeyRef& a, const ResourceKeyRef& b) {
168   return std::tie(a.name, a.config) < std::tie(b.name, b.config);
169 }
170 
171 }  // namespace aapt
172