blob: aa8b1dfe831194d6b240a8928672fb93995f1040 [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#include "ResourceUtils.h"
19#include "ResourceValues.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080020#include "xml/XmlDom.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070021
22#include <algorithm>
23#include <vector>
24
25namespace aapt {
26
27namespace {
28
29static bool cmpName(const SourcedResourceName& a, const ResourceNameRef& b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070030 return a.name < b;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070031}
32
33struct IdCollector : public xml::Visitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070034 using xml::Visitor::visit;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070035
Adam Lesinskicacb28f2016-10-19 12:18:14 -070036 std::vector<SourcedResourceName>* mOutSymbols;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070037
Adam Lesinskicacb28f2016-10-19 12:18:14 -070038 explicit IdCollector(std::vector<SourcedResourceName>* outSymbols)
39 : mOutSymbols(outSymbols) {}
Adam Lesinski1ab598f2015-08-14 14:26:04 -070040
Adam Lesinskicacb28f2016-10-19 12:18:14 -070041 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(mOutSymbols->begin(), mOutSymbols->end(),
48 name, cmpName);
49 if (iter == mOutSymbols->end() || iter->name != name) {
50 mOutSymbols->insert(iter, SourcedResourceName{name.toResourceName(),
51 element->lineNumber});
52 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070053 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070055 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056
57 xml::Visitor::visit(element);
58 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070059};
60
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061} // namespace
Adam Lesinski1ab598f2015-08-14 14:26:04 -070062
Adam Lesinski467f1712015-11-16 17:35:44 -080063bool XmlIdCollector::consume(IAaptContext* context, xml::XmlResource* xmlRes) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070064 xmlRes->file.exportedSymbols.clear();
65 IdCollector collector(&xmlRes->file.exportedSymbols);
66 xmlRes->root->accept(&collector);
67 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070068}
69
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070} // namespace aapt