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 "compile/XmlIdCollector.h"
18
19 #include <algorithm>
20 #include <vector>
21
22 #include "ResourceUtils.h"
23 #include "ResourceValues.h"
24 #include "xml/XmlDom.h"
25
26 namespace aapt {
27
28 namespace {
29
cmp_name(const SourcedResourceName & a,const ResourceNameRef & b)30 static bool cmp_name(const SourcedResourceName& a, const ResourceNameRef& b) {
31 return a.name < b;
32 }
33
34 struct IdCollector : public xml::Visitor {
35 public:
36 using xml::Visitor::Visit;
37
IdCollectoraapt::__anonef67dd8e0111::IdCollector38 explicit IdCollector(std::vector<SourcedResourceName>* out_symbols)
39 : out_symbols_(out_symbols) {}
40
Visitaapt::__anonef67dd8e0111::IdCollector41 void Visit(xml::Element* element) override {
42 for (xml::Attribute& attr : element->attributes) {
43 ResourceNameRef name;
44 bool create = false;
45 if (ResourceUtils::ParseReference(attr.value, &name, &create, nullptr)) {
46 if (create && name.type == ResourceType::kId) {
47 auto iter = std::lower_bound(out_symbols_->begin(),
48 out_symbols_->end(), name, cmp_name);
49 if (iter == out_symbols_->end() || iter->name != name) {
50 out_symbols_->insert(iter,
51 SourcedResourceName{name.ToResourceName(),
52 element->line_number});
53 }
54 }
55 }
56 }
57
58 xml::Visitor::Visit(element);
59 }
60
61 private:
62 std::vector<SourcedResourceName>* out_symbols_;
63 };
64
65 } // namespace
66
Consume(IAaptContext * context,xml::XmlResource * xmlRes)67 bool XmlIdCollector::Consume(IAaptContext* context, xml::XmlResource* xmlRes) {
68 xmlRes->file.exported_symbols.clear();
69 IdCollector collector(&xmlRes->file.exported_symbols);
70 xmlRes->root->Accept(&collector);
71 return true;
72 }
73
74 } // namespace aapt
75