Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 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 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 17 | #include "compile/XmlIdCollector.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 18 | |
| 19 | #include <algorithm> |
| 20 | #include <vector> |
| 21 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 22 | #include "ResourceUtils.h" |
| 23 | #include "ResourceValues.h" |
Ryan Mitchell | 7984854 | 2018-09-11 10:41:09 -0700 | [diff] [blame] | 24 | #include "text/Unicode.h" |
Fabien Sanglard | 2d34e76 | 2019-02-21 15:13:29 -0800 | [diff] [blame] | 25 | #include "trace/TraceBuffer.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 26 | #include "xml/XmlDom.h" |
| 27 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 28 | namespace aapt { |
| 29 | |
| 30 | namespace { |
| 31 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 32 | static bool cmp_name(const SourcedResourceName& a, const ResourceNameRef& b) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 33 | return a.name < b; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | struct IdCollector : public xml::Visitor { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 37 | public: |
| 38 | using xml::Visitor::Visit; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 39 | |
Ryan Mitchell | 7984854 | 2018-09-11 10:41:09 -0700 | [diff] [blame] | 40 | explicit IdCollector(std::vector<SourcedResourceName>* out_symbols, |
| 41 | SourcePathDiagnostics* source_diag) : out_symbols_(out_symbols), |
| 42 | source_diag_(source_diag) {} |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 43 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 44 | void Visit(xml::Element* element) override { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 45 | for (xml::Attribute& attr : element->attributes) { |
| 46 | ResourceNameRef name; |
| 47 | bool create = false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 48 | if (ResourceUtils::ParseReference(attr.value, &name, &create, nullptr)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 49 | if (create && name.type == ResourceType::kId) { |
Ryan Mitchell | 7984854 | 2018-09-11 10:41:09 -0700 | [diff] [blame] | 50 | 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 Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 60 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 61 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 62 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 63 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 64 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 65 | xml::Visitor::Visit(element); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 66 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 67 | |
| 68 | private: |
| 69 | std::vector<SourcedResourceName>* out_symbols_; |
Ryan Mitchell | 7984854 | 2018-09-11 10:41:09 -0700 | [diff] [blame] | 70 | SourcePathDiagnostics* source_diag_; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 71 | }; |
| 72 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 73 | } // namespace |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 74 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 75 | bool XmlIdCollector::Consume(IAaptContext* context, xml::XmlResource* xmlRes) { |
Fabien Sanglard | 2d34e76 | 2019-02-21 15:13:29 -0800 | [diff] [blame] | 76 | TRACE_CALL(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 77 | xmlRes->file.exported_symbols.clear(); |
Ryan Mitchell | 7984854 | 2018-09-11 10:41:09 -0700 | [diff] [blame] | 78 | SourcePathDiagnostics source_diag(xmlRes->file.source, context->GetDiagnostics()); |
| 79 | IdCollector collector(&xmlRes->file.exported_symbols, &source_diag); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 80 | xmlRes->root->Accept(&collector); |
Ryan Mitchell | 7984854 | 2018-09-11 10:41:09 -0700 | [diff] [blame] | 81 | return !source_diag.HadError(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 84 | } // namespace aapt |