• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "text/Unicode.h"
25 #include "trace/TraceBuffer.h"
26 #include "xml/XmlDom.h"
27 
28 namespace aapt {
29 
30 namespace {
31 
cmp_name(const SourcedResourceName & a,const ResourceNameRef & b)32 static bool cmp_name(const SourcedResourceName& a, const ResourceNameRef& b) {
33   return a.name < b;
34 }
35 
36 struct IdCollector : public xml::Visitor {
37  public:
38   using xml::Visitor::Visit;
39 
IdCollectoraapt::__anon8a4f79220111::IdCollector40   explicit IdCollector(std::vector<SourcedResourceName>* out_symbols,
41                        android::SourcePathDiagnostics* source_diag)
42       : out_symbols_(out_symbols), source_diag_(source_diag) {
43   }
44 
Visitaapt::__anon8a4f79220111::IdCollector45   void Visit(xml::Element* element) override {
46     for (xml::Attribute& attr : element->attributes) {
47       ResourceNameRef name;
48       bool create = false;
49       if (ResourceUtils::ParseReference(attr.value, &name, &create, nullptr)) {
50         if (create && name.type.type == ResourceType::kId) {
51           if (!text::IsValidResourceEntryName(name.entry)) {
52             source_diag_->Error(android::DiagMessage(element->line_number)
53                                 << "id '" << name << "' has an invalid entry name");
54           } 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             }
61           }
62         }
63       }
64     }
65 
66     xml::Visitor::Visit(element);
67   }
68 
69  private:
70   std::vector<SourcedResourceName>* out_symbols_;
71   android::SourcePathDiagnostics* source_diag_;
72 };
73 
74 }  // namespace
75 
Consume(IAaptContext * context,xml::XmlResource * xmlRes)76 bool XmlIdCollector::Consume(IAaptContext* context, xml::XmlResource* xmlRes) {
77   TRACE_CALL();
78   xmlRes->file.exported_symbols.clear();
79   android::SourcePathDiagnostics source_diag(xmlRes->file.source, context->GetDiagnostics());
80   IdCollector collector(&xmlRes->file.exported_symbols, &source_diag);
81   xmlRes->root->Accept(&collector);
82   return !source_diag.HadError();
83 }
84 
85 }  // namespace aapt
86