blob: d61a15af0d8531564d35973a6366387e652b564c [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
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
Adam Lesinskicacb28f2016-10-19 12:18:14 -070017#include "compile/XmlIdCollector.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070018
19#include <algorithm>
20#include <vector>
21
Adam Lesinskice5e56e2016-10-21 17:56:45 -070022#include "ResourceUtils.h"
23#include "ResourceValues.h"
24#include "xml/XmlDom.h"
25
Adam Lesinski1ab598f2015-08-14 14:26:04 -070026namespace aapt {
27
28namespace {
29
Adam Lesinskice5e56e2016-10-21 17:56:45 -070030static bool cmp_name(const SourcedResourceName& a, const ResourceNameRef& b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070031 return a.name < b;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070032}
33
34struct IdCollector : public xml::Visitor {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070035 public:
36 using xml::Visitor::Visit;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070037
Adam Lesinskice5e56e2016-10-21 17:56:45 -070038 explicit IdCollector(std::vector<SourcedResourceName>* out_symbols)
39 : out_symbols_(out_symbols) {}
Adam Lesinski1ab598f2015-08-14 14:26:04 -070040
Adam Lesinskice5e56e2016-10-21 17:56:45 -070041 void Visit(xml::Element* element) override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070042 for (xml::Attribute& attr : element->attributes) {
43 ResourceNameRef name;
44 bool create = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070045 if (ResourceUtils::ParseReference(attr.value, &name, &create, nullptr)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046 if (create && name.type == ResourceType::kId) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070047 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});
Adam Lesinskicacb28f2016-10-19 12:18:14 -070053 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070054 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070055 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070056 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070057
Adam Lesinskice5e56e2016-10-21 17:56:45 -070058 xml::Visitor::Visit(element);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070059 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070060
61 private:
62 std::vector<SourcedResourceName>* out_symbols_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070063};
64
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065} // namespace
Adam Lesinski1ab598f2015-08-14 14:26:04 -070066
Adam Lesinskice5e56e2016-10-21 17:56:45 -070067bool 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);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070072}
73
Adam Lesinskicacb28f2016-10-19 12:18:14 -070074} // namespace aapt