blob: 5543722ce4fe36b005925d51f00c45785ce1584d [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
24namespace android {
25namespace idmap2 {
26
27std::unique_ptr<const Xml> Xml::Create(const uint8_t* data, size_t size, bool copyData) {
28 std::unique_ptr<Xml> xml(new Xml());
29 if (xml->xml_.setTo(data, size, copyData) != NO_ERROR) {
30 return nullptr;
31 }
32 return xml;
33}
34
35std::unique_ptr<std::map<std::string, std::string>> Xml::FindTag(const std::string& name) const {
36 const String16 tag_to_find(name.c_str(), name.size());
37 xml_.restart();
38 ResXMLParser::event_code_t type;
39 do {
40 type = xml_.next();
41 if (type == ResXMLParser::START_TAG) {
42 size_t len;
43 const String16 tag(xml_.getElementName(&len));
44 if (tag == tag_to_find) {
45 std::unique_ptr<std::map<std::string, std::string>> map(
46 new std::map<std::string, std::string>());
47 for (size_t i = 0; i < xml_.getAttributeCount(); i++) {
48 const String16 key16(xml_.getAttributeName(i, &len));
49 std::string key = String8(key16).c_str();
50
51 std::string value;
52 switch (xml_.getAttributeDataType(i)) {
53 case Res_value::TYPE_STRING: {
54 const String16 value16(xml_.getAttributeStringValue(i, &len));
55 value = String8(value16).c_str();
56 } break;
57 case Res_value::TYPE_INT_DEC:
58 case Res_value::TYPE_INT_HEX:
59 case Res_value::TYPE_INT_BOOLEAN: {
60 Res_value resValue;
61 xml_.getAttributeValue(i, &resValue);
62 value = std::to_string(resValue.data);
63 } break;
64 default:
65 return nullptr;
66 }
67
68 map->emplace(std::make_pair(key, value));
69 }
70 return map;
71 }
72 }
73 } while (type != ResXMLParser::BAD_DOCUMENT && type != ResXMLParser::END_DOCUMENT);
74 return nullptr;
75}
76
77Xml::~Xml() {
78 xml_.uninit();
79}
80
81} // namespace idmap2
82} // namespace android