blob: 90a8cd975d7358e9b375f4eeba45cbe937a7f721 [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
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#ifndef AAPT_RESOLVER_H
18#define AAPT_RESOLVER_H
19
20#include "Maybe.h"
21#include "Resource.h"
22#include "ResourceTable.h"
23#include "ResourceValues.h"
24
25#include <androidfw/AssetManager.h>
26#include <androidfw/ResourceTypes.h>
27#include <memory>
28#include <vector>
29
30namespace aapt {
31
32/**
33 * Resolves symbolic references (package:type/entry) into resource IDs/objects.
34 * Encapsulates the search of library sources as well as the local ResourceTable.
35 */
36class Resolver {
37public:
38 /**
39 * Creates a resolver with a local ResourceTable and an AssetManager
40 * loaded with library packages.
41 */
42 Resolver(std::shared_ptr<const ResourceTable> table,
43 std::shared_ptr<const android::AssetManager> sources);
44
45 Resolver(const Resolver&) = delete; // Not copyable.
46
47 /**
48 * Holds the result of a resource name lookup.
49 */
50 struct Entry {
51 /**
52 * The ID of the resource. ResourceId::isValid() may
53 * return false if the resource has not been assigned
54 * an ID.
55 */
56 ResourceId id;
57
58 /**
59 * If the resource is an attribute, this will point
60 * to a valid Attribute object, or else it will be
61 * nullptr.
62 */
63 const Attribute* attr;
64 };
65
66 /**
67 * Return the package to use when none is specified. This
68 * is the package name of the app being built.
69 */
70 const std::u16string& getDefaultPackage() const;
71
72 /**
73 * Returns a ResourceID if the name is found. The ResourceID
74 * may not be valid if the resource was not assigned an ID.
75 */
76 Maybe<ResourceId> findId(const ResourceName& name);
77
78 /**
79 * Returns an Entry if the name is found. Entry::attr
80 * may be nullptr if the resource is not an attribute.
81 */
82 Maybe<Entry> findAttribute(const ResourceName& name);
83
84 const android::ResTable& getResTable() const;
85
86private:
87 struct CacheEntry {
88 ResourceId id;
89 std::unique_ptr<Attribute> attr;
90 };
91
92 const CacheEntry* buildCacheEntry(const ResourceName& name);
93
94 std::shared_ptr<const ResourceTable> mTable;
95 std::shared_ptr<const android::AssetManager> mSources;
96 std::map<ResourceName, CacheEntry> mCache;
97};
98
99inline const std::u16string& Resolver::getDefaultPackage() const {
100 return mTable->getPackage();
101}
102
103inline const android::ResTable& Resolver::getResTable() const {
104 return mSources->getResources(false);
105}
106
107} // namespace aapt
108
109#endif // AAPT_RESOLVER_H