blob: 0075a922d6760a5905b7440e65896748a26031cd [file] [log] [blame]
Mårten Kongstad02751232018-04-27 13:16:32 +02001/*
2 * Copyright (C) 2018 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 <map>
18#include <memory>
19#include <string>
20#include <utility>
21
22#include "idmap2/Xml.h"
23
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010024namespace android::idmap2 {
Mårten Kongstad02751232018-04-27 13:16:32 +020025
26std::unique_ptr<const Xml> Xml::Create(const uint8_t* data, size_t size, bool copyData) {
27 std::unique_ptr<Xml> xml(new Xml());
28 if (xml->xml_.setTo(data, size, copyData) != NO_ERROR) {
29 return nullptr;
30 }
31 return xml;
32}
33
34std::unique_ptr<std::map<std::string, std::string>> Xml::FindTag(const std::string& name) const {
35 const String16 tag_to_find(name.c_str(), name.size());
36 xml_.restart();
37 ResXMLParser::event_code_t type;
38 do {
39 type = xml_.next();
40 if (type == ResXMLParser::START_TAG) {
41 size_t len;
42 const String16 tag(xml_.getElementName(&len));
43 if (tag == tag_to_find) {
44 std::unique_ptr<std::map<std::string, std::string>> map(
45 new std::map<std::string, std::string>());
46 for (size_t i = 0; i < xml_.getAttributeCount(); i++) {
47 const String16 key16(xml_.getAttributeName(i, &len));
48 std::string key = String8(key16).c_str();
49
50 std::string value;
51 switch (xml_.getAttributeDataType(i)) {
52 case Res_value::TYPE_STRING: {
53 const String16 value16(xml_.getAttributeStringValue(i, &len));
54 value = String8(value16).c_str();
55 } break;
56 case Res_value::TYPE_INT_DEC:
57 case Res_value::TYPE_INT_HEX:
58 case Res_value::TYPE_INT_BOOLEAN: {
59 Res_value resValue;
60 xml_.getAttributeValue(i, &resValue);
61 value = std::to_string(resValue.data);
62 } break;
63 default:
64 return nullptr;
65 }
66
67 map->emplace(std::make_pair(key, value));
68 }
69 return map;
70 }
71 }
72 } while (type != ResXMLParser::BAD_DOCUMENT && type != ResXMLParser::END_DOCUMENT);
73 return nullptr;
74}
75
76Xml::~Xml() {
77 xml_.uninit();
78}
79
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010080} // namespace android::idmap2