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::kMenu:
82 return "menu";
83 case ResourceType::kMipmap:
84 return "mipmap";
85 case ResourceType::kNavigation:
86 return "navigation";
87 case ResourceType::kPlurals:
88 return "plurals";
89 case ResourceType::kRaw:
90 return "raw";
91 case ResourceType::kString:
92 return "string";
93 case ResourceType::kStyle:
94 return "style";
95 case ResourceType::kStyleable:
96 return "styleable";
97 case ResourceType::kTransition:
98 return "transition";
99 case ResourceType::kUnknown:
100 return "unknown";
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 {"menu", ResourceType::kMenu},
125 {"mipmap", ResourceType::kMipmap},
126 {"navigation", ResourceType::kNavigation},
127 {"plurals", ResourceType::kPlurals},
128 {"raw", ResourceType::kRaw},
129 {"string", ResourceType::kString},
130 {"style", ResourceType::kStyle},
131 {"styleable", ResourceType::kStyleable},
132 {"transition", ResourceType::kTransition},
133 {"xml", ResourceType::kXml},
134 };
135
ParseResourceType(const StringPiece & str)136 const ResourceType* ParseResourceType(const StringPiece& str) {
137 auto iter = sResourceTypeMap.find(str);
138 if (iter == std::end(sResourceTypeMap)) {
139 return nullptr;
140 }
141 return &iter->second;
142 }
143
operator <(const ResourceKey & a,const ResourceKey & b)144 bool operator<(const ResourceKey& a, const ResourceKey& b) {
145 return std::tie(a.name, a.config) < std::tie(b.name, b.config);
146 }
147
operator <(const ResourceKeyRef & a,const ResourceKeyRef & b)148 bool operator<(const ResourceKeyRef& a, const ResourceKeyRef& b) {
149 return std::tie(a.name, a.config) < std::tie(b.name, b.config);
150 }
151
152 } // namespace aapt
153