blob: 68cb36a078dd81558df18e867efc1df677f349d2 [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"
Ryan Mitchell79848542018-09-11 10:41:09 -070024#include "text/Unicode.h"
Fabien Sanglard2d34e762019-02-21 15:13:29 -080025#include "trace/TraceBuffer.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070026#include "xml/XmlDom.h"
27
Adam Lesinski1ab598f2015-08-14 14:26:04 -070028namespace aapt {
29
30namespace {
31
Adam Lesinskice5e56e2016-10-21 17:56:45 -070032static bool cmp_name(const SourcedResourceName& a, const ResourceNameRef& b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070033 return a.name < b;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070034}
35
36struct IdCollector : public xml::Visitor {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070037 public:
38 using xml::Visitor::Visit;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070039
Ryan Mitchell79848542018-09-11 10:41:09 -070040 explicit IdCollector(std::vector<SourcedResourceName>* out_symbols,
Jeremy Meyer56f36e82022-05-20 20:35:42 +000041 android::SourcePathDiagnostics* source_diag)
42 : out_symbols_(out_symbols), source_diag_(source_diag) {
43 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070044
Adam Lesinskice5e56e2016-10-21 17:56:45 -070045 void Visit(xml::Element* element) override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046 for (xml::Attribute& attr : element->attributes) {
47 ResourceNameRef name;
48 bool create = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070049 if (ResourceUtils::ParseReference(attr.value, &name, &create, nullptr)) {
Iurii Makhnocff10ce2022-02-15 19:33:50 +000050 if (create && name.type.type == ResourceType::kId) {
Ryan Mitchell79848542018-09-11 10:41:09 -070051 if (!text::IsValidResourceEntryName(name.entry)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +000052 source_diag_->Error(android::DiagMessage(element->line_number)
53 << "id '" << name << "' has an invalid entry name");
Ryan Mitchell79848542018-09-11 10:41:09 -070054 } else {
55 auto iter = std::lower_bound(out_symbols_->begin(),
56 out_symbols_->end(), name, cmp_name);
57 if (iter == out_symbols_->end() || iter->name != name) {
58 out_symbols_->insert(iter, SourcedResourceName{name.ToResourceName(),
59 element->line_number});
60 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070062 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070064 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065
Adam Lesinskice5e56e2016-10-21 17:56:45 -070066 xml::Visitor::Visit(element);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070068
69 private:
70 std::vector<SourcedResourceName>* out_symbols_;
Jeremy Meyer56f36e82022-05-20 20:35:42 +000071 android::SourcePathDiagnostics* source_diag_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070072};
73
Adam Lesinskicacb28f2016-10-19 12:18:14 -070074} // namespace
Adam Lesinski1ab598f2015-08-14 14:26:04 -070075
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076bool XmlIdCollector::Consume(IAaptContext* context, xml::XmlResource* xmlRes) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -080077 TRACE_CALL();
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078 xmlRes->file.exported_symbols.clear();
Jeremy Meyer56f36e82022-05-20 20:35:42 +000079 android::SourcePathDiagnostics source_diag(xmlRes->file.source, context->GetDiagnostics());
Ryan Mitchell79848542018-09-11 10:41:09 -070080 IdCollector collector(&xmlRes->file.exported_symbols, &source_diag);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070081 xmlRes->root->Accept(&collector);
Ryan Mitchell79848542018-09-11 10:41:09 -070082 return !source_diag.HadError();
Adam Lesinski1ab598f2015-08-14 14:26:04 -070083}
84
Adam Lesinskicacb28f2016-10-19 12:18:14 -070085} // namespace aapt