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
ParseResourceType(const StringPiece & str)137 const ResourceType* ParseResourceType(const StringPiece& str) {
138 auto iter = sResourceTypeMap.find(str);
139 if (iter == std::end(sResourceTypeMap)) {
140 return nullptr;
141 }
142 return &iter->second;
143 }
144
operator <(const ResourceKey & a,const ResourceKey & b)145 bool operator<(const ResourceKey& a, const ResourceKey& b) {
146 return std::tie(a.name, a.config) < std::tie(b.name, b.config);
147 }
148
operator <(const ResourceKeyRef & a,const ResourceKeyRef & b)149 bool operator<(const ResourceKeyRef& a, const ResourceKeyRef& b) {
150 return std::tie(a.name, a.config) < std::tie(b.name, b.config);
151 }
152
153 } // namespace aapt
154