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 | |
| 17 | #include "ResourceUtils.h" |
| 18 | #include "ResourceValues.h" |
| 19 | #include "XmlDom.h" |
| 20 | |
| 21 | #include "compile/XmlIdCollector.h" |
| 22 | |
| 23 | #include <algorithm> |
| 24 | #include <vector> |
| 25 | |
| 26 | namespace aapt { |
| 27 | |
| 28 | namespace { |
| 29 | |
| 30 | static bool cmpName(const SourcedResourceName& a, const ResourceNameRef& b) { |
| 31 | return a.name < b; |
| 32 | } |
| 33 | |
| 34 | struct IdCollector : public xml::Visitor { |
| 35 | using xml::Visitor::visit; |
| 36 | |
| 37 | std::vector<SourcedResourceName>* mOutSymbols; |
| 38 | |
| 39 | IdCollector(std::vector<SourcedResourceName>* outSymbols) : mOutSymbols(outSymbols) { |
| 40 | } |
| 41 | |
| 42 | void visit(xml::Element* element) override { |
| 43 | for (xml::Attribute& attr : element->attributes) { |
| 44 | ResourceNameRef name; |
| 45 | bool create = false; |
| 46 | if (ResourceUtils::tryParseReference(attr.value, &name, &create, nullptr)) { |
| 47 | if (create && name.type == ResourceType::kId) { |
| 48 | auto iter = std::lower_bound(mOutSymbols->begin(), mOutSymbols->end(), |
| 49 | name, cmpName); |
| 50 | if (iter == mOutSymbols->end() || iter->name != name) { |
| 51 | mOutSymbols->insert(iter, SourcedResourceName{ name.toResourceName(), |
| 52 | element->lineNumber }); |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | xml::Visitor::visit(element); |
| 59 | } |
| 60 | }; |
| 61 | |
| 62 | } // namespace |
| 63 | |
| 64 | bool XmlIdCollector::consume(IAaptContext* context, XmlResource* xmlRes) { |
| 65 | xmlRes->file.exportedSymbols.clear(); |
| 66 | IdCollector collector(&xmlRes->file.exportedSymbols); |
| 67 | xmlRes->root->accept(&collector); |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | } // namespace aapt |