blob: 50541152f80285dd60976f6d9339582e779cd752 [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,
41 SourcePathDiagnostics* source_diag) : out_symbols_(out_symbols),
42 source_diag_(source_diag) {}
Adam Lesinski1ab598f2015-08-14 14:26:04 -070043
Adam Lesinskice5e56e2016-10-21 17:56:45 -070044 void Visit(xml::Element* element) override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070045 for (xml::Attribute& attr : element->attributes) {
46 ResourceNameRef name;
47 bool create = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070048 if (ResourceUtils::ParseReference(attr.value, &name, &create, nullptr)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070049 if (create && name.type == ResourceType::kId) {
Ryan Mitchell79848542018-09-11 10:41:09 -070050 if (!text::IsValidResourceEntryName(name.entry)) {
51 source_diag_->Error(DiagMessage(element->line_number)
52 << "id '" << name << "' has an invalid entry name");
53 } else {
54 auto iter = std::lower_bound(out_symbols_->begin(),
55 out_symbols_->end(), name, cmp_name);
56 if (iter == out_symbols_->end() || iter->name != name) {
57 out_symbols_->insert(iter, SourcedResourceName{name.ToResourceName(),
58 element->line_number});
59 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070060 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070061 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070063 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070064
Adam Lesinskice5e56e2016-10-21 17:56:45 -070065 xml::Visitor::Visit(element);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070066 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070067
68 private:
69 std::vector<SourcedResourceName>* out_symbols_;
Ryan Mitchell79848542018-09-11 10:41:09 -070070 SourcePathDiagnostics* source_diag_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070071};
72
Adam Lesinskicacb28f2016-10-19 12:18:14 -070073} // namespace
Adam Lesinski1ab598f2015-08-14 14:26:04 -070074
Adam Lesinskice5e56e2016-10-21 17:56:45 -070075bool XmlIdCollector::Consume(IAaptContext* context, xml::XmlResource* xmlRes) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -080076 TRACE_CALL();
Adam Lesinskice5e56e2016-10-21 17:56:45 -070077 xmlRes->file.exported_symbols.clear();
Ryan Mitchell79848542018-09-11 10:41:09 -070078 SourcePathDiagnostics source_diag(xmlRes->file.source, context->GetDiagnostics());
79 IdCollector collector(&xmlRes->file.exported_symbols, &source_diag);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070080 xmlRes->root->Accept(&collector);
Ryan Mitchell79848542018-09-11 10:41:09 -070081 return !source_diag.HadError();
Adam Lesinski1ab598f2015-08-14 14:26:04 -070082}
83
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084} // namespace aapt