Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -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 | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 17 | #include "XmlDom.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 18 | |
| 19 | #include <expat.h> |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 20 | |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 21 | #include <memory> |
| 22 | #include <stack> |
| 23 | #include <string> |
| 24 | #include <tuple> |
| 25 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 26 | #include "android-base/logging.h" |
| 27 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 28 | #include "ResourceUtils.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 29 | #include "XmlPullParser.h" |
| 30 | #include "util/Util.h" |
| 31 | |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 32 | using ::aapt::io::InputStream; |
| 33 | using ::android::StringPiece; |
| 34 | using ::android::StringPiece16; |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 35 | |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 36 | namespace aapt { |
| 37 | namespace xml { |
| 38 | |
| 39 | constexpr char kXmlNamespaceSep = 1; |
| 40 | |
| 41 | struct Stack { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 42 | std::unique_ptr<xml::Element> root; |
| 43 | std::stack<xml::Element*> node_stack; |
| 44 | std::unique_ptr<xml::Element> pending_element; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 45 | std::string pending_comment; |
Adam Lesinski | ac6edc5 | 2017-03-02 19:31:28 -0800 | [diff] [blame] | 46 | std::unique_ptr<xml::Text> last_text_node; |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 47 | }; |
| 48 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 49 | // Extracts the namespace and name of an expanded element or attribute name. |
| 50 | static void SplitName(const char* name, std::string* out_ns, std::string* out_name) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 51 | const char* p = name; |
| 52 | while (*p != 0 && *p != kXmlNamespaceSep) { |
| 53 | p++; |
| 54 | } |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 55 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 56 | if (*p == 0) { |
| 57 | out_ns->clear(); |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 58 | out_name->assign(name); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 59 | } else { |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 60 | out_ns->assign(name, (p - name)); |
| 61 | out_name->assign(p + 1); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 62 | } |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Adam Lesinski | ac6edc5 | 2017-03-02 19:31:28 -0800 | [diff] [blame] | 65 | static void FinishPendingText(Stack* stack) { |
| 66 | if (stack->last_text_node != nullptr) { |
Adam Lesinski | 48448e8 | 2017-04-26 15:13:52 -0700 | [diff] [blame] | 67 | if (!stack->last_text_node->text.empty()) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 68 | CHECK(!stack->node_stack.empty()); |
Adam Lesinski | ac6edc5 | 2017-03-02 19:31:28 -0800 | [diff] [blame] | 69 | stack->node_stack.top()->AppendChild(std::move(stack->last_text_node)); |
| 70 | } else { |
| 71 | // Drop an empty text node. |
Adam Lesinski | ac6edc5 | 2017-03-02 19:31:28 -0800 | [diff] [blame] | 72 | } |
Adam Lesinski | 48448e8 | 2017-04-26 15:13:52 -0700 | [diff] [blame] | 73 | stack->last_text_node = nullptr; |
Adam Lesinski | ac6edc5 | 2017-03-02 19:31:28 -0800 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 77 | static void XMLCALL StartNamespaceHandler(void* user_data, const char* prefix, const char* uri) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 78 | XML_Parser parser = reinterpret_cast<XML_Parser>(user_data); |
| 79 | Stack* stack = reinterpret_cast<Stack*>(XML_GetUserData(parser)); |
Adam Lesinski | ac6edc5 | 2017-03-02 19:31:28 -0800 | [diff] [blame] | 80 | FinishPendingText(stack); |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 81 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 82 | NamespaceDecl decl; |
| 83 | decl.line_number = XML_GetCurrentLineNumber(parser); |
| 84 | decl.column_number = XML_GetCurrentColumnNumber(parser); |
| 85 | decl.prefix = prefix ? prefix : ""; |
| 86 | decl.uri = uri ? uri : ""; |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 87 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 88 | if (stack->pending_element == nullptr) { |
| 89 | stack->pending_element = util::make_unique<Element>(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 90 | } |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 91 | stack->pending_element->namespace_decls.push_back(std::move(decl)); |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 94 | static void XMLCALL EndNamespaceHandler(void* user_data, const char* /*prefix*/) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 95 | XML_Parser parser = reinterpret_cast<XML_Parser>(user_data); |
| 96 | Stack* stack = reinterpret_cast<Stack*>(XML_GetUserData(parser)); |
Adam Lesinski | ac6edc5 | 2017-03-02 19:31:28 -0800 | [diff] [blame] | 97 | FinishPendingText(stack); |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 100 | static bool less_attribute(const Attribute& lhs, const Attribute& rhs) { |
| 101 | return std::tie(lhs.namespace_uri, lhs.name, lhs.value) < |
| 102 | std::tie(rhs.namespace_uri, rhs.name, rhs.value); |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 105 | static void XMLCALL StartElementHandler(void* user_data, const char* name, const char** attrs) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 106 | XML_Parser parser = reinterpret_cast<XML_Parser>(user_data); |
| 107 | Stack* stack = reinterpret_cast<Stack*>(XML_GetUserData(parser)); |
Adam Lesinski | ac6edc5 | 2017-03-02 19:31:28 -0800 | [diff] [blame] | 108 | FinishPendingText(stack); |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 109 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 110 | std::unique_ptr<Element> el; |
| 111 | if (stack->pending_element != nullptr) { |
| 112 | el = std::move(stack->pending_element); |
| 113 | } else { |
| 114 | el = util::make_unique<Element>(); |
| 115 | } |
| 116 | |
| 117 | el->line_number = XML_GetCurrentLineNumber(parser); |
| 118 | el->column_number = XML_GetCurrentColumnNumber(parser); |
| 119 | el->comment = std::move(stack->pending_comment); |
| 120 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 121 | SplitName(name, &el->namespace_uri, &el->name); |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 122 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 123 | while (*attrs) { |
| 124 | Attribute attribute; |
| 125 | SplitName(*attrs++, &attribute.namespace_uri, &attribute.name); |
Adam Lesinski | 48448e8 | 2017-04-26 15:13:52 -0700 | [diff] [blame] | 126 | attribute.value = *attrs++; |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 127 | el->attributes.push_back(std::move(attribute)); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 128 | } |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 129 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 130 | // Sort the attributes. |
| 131 | std::sort(el->attributes.begin(), el->attributes.end(), less_attribute); |
| 132 | |
| 133 | // Add to the stack. |
| 134 | Element* this_el = el.get(); |
| 135 | if (!stack->node_stack.empty()) { |
| 136 | stack->node_stack.top()->AppendChild(std::move(el)); |
| 137 | } else { |
| 138 | stack->root = std::move(el); |
| 139 | } |
| 140 | stack->node_stack.push(this_el); |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 143 | static void XMLCALL EndElementHandler(void* user_data, const char* name) { |
| 144 | XML_Parser parser = reinterpret_cast<XML_Parser>(user_data); |
| 145 | Stack* stack = reinterpret_cast<Stack*>(XML_GetUserData(parser)); |
Adam Lesinski | ac6edc5 | 2017-03-02 19:31:28 -0800 | [diff] [blame] | 146 | FinishPendingText(stack); |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 147 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 148 | CHECK(!stack->node_stack.empty()); |
| 149 | // stack->nodeStack.top()->comment = std::move(stack->pendingComment); |
| 150 | stack->node_stack.pop(); |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Adam Lesinski | ac6edc5 | 2017-03-02 19:31:28 -0800 | [diff] [blame] | 153 | static void XMLCALL CharacterDataHandler(void* user_data, const char* s, int len) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 154 | XML_Parser parser = reinterpret_cast<XML_Parser>(user_data); |
| 155 | Stack* stack = reinterpret_cast<Stack*>(XML_GetUserData(parser)); |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 156 | |
Adam Lesinski | ac6edc5 | 2017-03-02 19:31:28 -0800 | [diff] [blame] | 157 | const StringPiece str(s, len); |
| 158 | if (str.empty()) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 159 | return; |
| 160 | } |
| 161 | |
| 162 | // See if we can just append the text to a previous text node. |
Adam Lesinski | ac6edc5 | 2017-03-02 19:31:28 -0800 | [diff] [blame] | 163 | if (stack->last_text_node != nullptr) { |
Adam Lesinski | 48448e8 | 2017-04-26 15:13:52 -0700 | [diff] [blame] | 164 | stack->last_text_node->text.append(str.data(), str.size()); |
Adam Lesinski | ac6edc5 | 2017-03-02 19:31:28 -0800 | [diff] [blame] | 165 | return; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 166 | } |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 167 | |
Adam Lesinski | ac6edc5 | 2017-03-02 19:31:28 -0800 | [diff] [blame] | 168 | stack->last_text_node = util::make_unique<Text>(); |
| 169 | stack->last_text_node->line_number = XML_GetCurrentLineNumber(parser); |
| 170 | stack->last_text_node->column_number = XML_GetCurrentColumnNumber(parser); |
Adam Lesinski | 48448e8 | 2017-04-26 15:13:52 -0700 | [diff] [blame] | 171 | stack->last_text_node->text = str.to_string(); |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 174 | static void XMLCALL CommentDataHandler(void* user_data, const char* comment) { |
| 175 | XML_Parser parser = reinterpret_cast<XML_Parser>(user_data); |
| 176 | Stack* stack = reinterpret_cast<Stack*>(XML_GetUserData(parser)); |
Adam Lesinski | ac6edc5 | 2017-03-02 19:31:28 -0800 | [diff] [blame] | 177 | FinishPendingText(stack); |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 178 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 179 | if (!stack->pending_comment.empty()) { |
| 180 | stack->pending_comment += '\n'; |
| 181 | } |
| 182 | stack->pending_comment += comment; |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 185 | std::unique_ptr<XmlResource> Inflate(InputStream* in, IDiagnostics* diag, const Source& source) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 186 | Stack stack; |
Adam Lesinski | 803c7c8 | 2016-04-06 16:09:43 -0700 | [diff] [blame] | 187 | |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 188 | std::unique_ptr<std::remove_pointer<XML_Parser>::type, decltype(XML_ParserFree)*> parser = { |
| 189 | XML_ParserCreateNS(nullptr, kXmlNamespaceSep), XML_ParserFree}; |
| 190 | XML_SetUserData(parser.get(), &stack); |
| 191 | XML_UseParserAsHandlerArg(parser.get()); |
| 192 | XML_SetElementHandler(parser.get(), StartElementHandler, EndElementHandler); |
| 193 | XML_SetNamespaceDeclHandler(parser.get(), StartNamespaceHandler, EndNamespaceHandler); |
| 194 | XML_SetCharacterDataHandler(parser.get(), CharacterDataHandler); |
| 195 | XML_SetCommentHandler(parser.get(), CommentDataHandler); |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 196 | |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 197 | const char* buffer = nullptr; |
| 198 | size_t buffer_size = 0; |
| 199 | while (in->Next(reinterpret_cast<const void**>(&buffer), &buffer_size)) { |
| 200 | if (XML_Parse(parser.get(), buffer, buffer_size, false) == XML_STATUS_ERROR) { |
| 201 | diag->Error(DiagMessage(source.WithLine(XML_GetCurrentLineNumber(parser.get()))) |
| 202 | << XML_ErrorString(XML_GetErrorCode(parser.get()))); |
| 203 | return {}; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 204 | } |
| 205 | } |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 206 | |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 207 | if (in->HadError()) { |
| 208 | diag->Error(DiagMessage(source) << in->GetError()); |
| 209 | return {}; |
| 210 | } else { |
| 211 | // Finish off the parsing. |
| 212 | if (XML_Parse(parser.get(), nullptr, 0u, true) == XML_STATUS_ERROR) { |
| 213 | diag->Error(DiagMessage(source.WithLine(XML_GetCurrentLineNumber(parser.get()))) |
| 214 | << XML_ErrorString(XML_GetErrorCode(parser.get()))); |
| 215 | return {}; |
| 216 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 217 | } |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 218 | return util::make_unique<XmlResource>(ResourceFile{{}, {}, ResourceFile::Type::kUnknown, source}, |
| 219 | StringPool{}, std::move(stack.root)); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 220 | } |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 221 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 222 | static void CopyAttributes(Element* el, android::ResXMLParser* parser, StringPool* out_pool) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 223 | const size_t attr_count = parser->getAttributeCount(); |
| 224 | if (attr_count > 0) { |
| 225 | el->attributes.reserve(attr_count); |
| 226 | for (size_t i = 0; i < attr_count; i++) { |
| 227 | Attribute attr; |
| 228 | size_t len; |
| 229 | const char16_t* str16 = parser->getAttributeNamespace(i, &len); |
| 230 | if (str16) { |
| 231 | attr.namespace_uri = util::Utf16ToUtf8(StringPiece16(str16, len)); |
| 232 | } |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 233 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 234 | str16 = parser->getAttributeName(i, &len); |
| 235 | if (str16) { |
| 236 | attr.name = util::Utf16ToUtf8(StringPiece16(str16, len)); |
| 237 | } |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 238 | |
Shane Farmer | 4b8ca8b | 2017-09-08 12:17:05 -0700 | [diff] [blame] | 239 | uint32_t res_id = parser->getAttributeNameResID(i); |
| 240 | if (res_id > 0) { |
| 241 | attr.compiled_attribute = AaptAttribute(::aapt::Attribute(), {res_id}); |
| 242 | } |
| 243 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 244 | str16 = parser->getAttributeStringValue(i, &len); |
| 245 | if (str16) { |
| 246 | attr.value = util::Utf16ToUtf8(StringPiece16(str16, len)); |
| 247 | } |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 248 | |
Adam Lesinski | bbf4297 | 2018-02-14 13:36:09 -0800 | [diff] [blame] | 249 | android::Res_value res_value; |
| 250 | if (parser->getAttributeValue(i, &res_value) > 0) { |
Adam Lesinski | e1094a2 | 2018-02-22 17:27:17 -0800 | [diff] [blame] | 251 | // Only compile the value if it is not a string, or it is a string that differs from |
| 252 | // the raw attribute value. |
| 253 | int32_t raw_value_idx = parser->getAttributeValueStringID(i); |
| 254 | if (res_value.dataType != android::Res_value::TYPE_STRING || raw_value_idx < 0 || |
| 255 | static_cast<uint32_t>(raw_value_idx) != res_value.data) { |
| 256 | attr.compiled_value = ResourceUtils::ParseBinaryResValue( |
| 257 | ResourceType::kAnim, {}, parser->getStrings(), res_value, out_pool); |
| 258 | } |
Adam Lesinski | bbf4297 | 2018-02-14 13:36:09 -0800 | [diff] [blame] | 259 | } |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 260 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 261 | el->attributes.push_back(std::move(attr)); |
| 262 | } |
| 263 | } |
| 264 | } |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 265 | |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 266 | std::unique_ptr<XmlResource> Inflate(const void* data, size_t len, std::string* out_error) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 267 | // We import the android namespace because on Windows NO_ERROR is a macro, not |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 268 | // an enum, which causes errors when qualifying it with android:: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 269 | using namespace android; |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 270 | |
Adam Lesinski | e1094a2 | 2018-02-22 17:27:17 -0800 | [diff] [blame] | 271 | std::unique_ptr<XmlResource> xml_resource = util::make_unique<XmlResource>(); |
| 272 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 273 | std::stack<Element*> node_stack; |
| 274 | std::unique_ptr<Element> pending_element; |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 275 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 276 | ResXMLTree tree; |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 277 | if (tree.setTo(data, len) != NO_ERROR) { |
| 278 | if (out_error != nullptr) { |
| 279 | *out_error = "failed to initialize ResXMLTree"; |
| 280 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 281 | return {}; |
| 282 | } |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 283 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 284 | ResXMLParser::event_code_t code; |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 285 | while ((code = tree.next()) != ResXMLParser::BAD_DOCUMENT && code != ResXMLParser::END_DOCUMENT) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 286 | std::unique_ptr<Node> new_node; |
| 287 | switch (code) { |
| 288 | case ResXMLParser::START_NAMESPACE: { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 289 | NamespaceDecl decl; |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 290 | decl.line_number = tree.getLineNumber(); |
| 291 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 292 | size_t len; |
| 293 | const char16_t* str16 = tree.getNamespacePrefix(&len); |
| 294 | if (str16) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 295 | decl.prefix = util::Utf16ToUtf8(StringPiece16(str16, len)); |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 298 | str16 = tree.getNamespaceUri(&len); |
| 299 | if (str16) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 300 | decl.uri = util::Utf16ToUtf8(StringPiece16(str16, len)); |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 301 | } |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 302 | |
| 303 | if (pending_element == nullptr) { |
| 304 | pending_element = util::make_unique<Element>(); |
| 305 | } |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 306 | pending_element->namespace_decls.push_back(std::move(decl)); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 307 | break; |
| 308 | } |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 309 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 310 | case ResXMLParser::START_TAG: { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 311 | std::unique_ptr<Element> el; |
| 312 | if (pending_element != nullptr) { |
| 313 | el = std::move(pending_element); |
| 314 | } else { |
| 315 | el = util::make_unique<Element>(); |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 316 | } |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 317 | el->line_number = tree.getLineNumber(); |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 318 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 319 | size_t len; |
| 320 | const char16_t* str16 = tree.getElementNamespace(&len); |
| 321 | if (str16) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 322 | el->namespace_uri = util::Utf16ToUtf8(StringPiece16(str16, len)); |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 323 | } |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 324 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 325 | str16 = tree.getElementName(&len); |
| 326 | if (str16) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 327 | el->name = util::Utf16ToUtf8(StringPiece16(str16, len)); |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 328 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 329 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 330 | Element* this_el = el.get(); |
Adam Lesinski | e1094a2 | 2018-02-22 17:27:17 -0800 | [diff] [blame] | 331 | CopyAttributes(el.get(), &tree, &xml_resource->string_pool); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 332 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 333 | if (!node_stack.empty()) { |
| 334 | node_stack.top()->AppendChild(std::move(el)); |
| 335 | } else { |
Adam Lesinski | e1094a2 | 2018-02-22 17:27:17 -0800 | [diff] [blame] | 336 | xml_resource->root = std::move(el); |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 337 | } |
| 338 | node_stack.push(this_el); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 339 | break; |
| 340 | } |
| 341 | |
| 342 | case ResXMLParser::TEXT: { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 343 | std::unique_ptr<Text> text = util::make_unique<Text>(); |
| 344 | text->line_number = tree.getLineNumber(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 345 | size_t len; |
| 346 | const char16_t* str16 = tree.getText(&len); |
| 347 | if (str16) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 348 | text->text = util::Utf16ToUtf8(StringPiece16(str16, len)); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 349 | } |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 350 | CHECK(!node_stack.empty()); |
| 351 | node_stack.top()->AppendChild(std::move(text)); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 352 | break; |
| 353 | } |
| 354 | |
| 355 | case ResXMLParser::END_NAMESPACE: |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 356 | break; |
| 357 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 358 | case ResXMLParser::END_TAG: |
| 359 | CHECK(!node_stack.empty()); |
| 360 | node_stack.pop(); |
| 361 | break; |
| 362 | |
| 363 | default: |
| 364 | LOG(FATAL) << "unhandled XML chunk type"; |
| 365 | break; |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 366 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 367 | } |
Adam Lesinski | e1094a2 | 2018-02-22 17:27:17 -0800 | [diff] [blame] | 368 | return xml_resource; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 371 | std::unique_ptr<XmlResource> XmlResource::Clone() const { |
| 372 | std::unique_ptr<XmlResource> cloned = util::make_unique<XmlResource>(file); |
| 373 | if (root != nullptr) { |
| 374 | cloned->root = root->CloneElement([&](const xml::Element& src, xml::Element* dst) { |
| 375 | dst->attributes.reserve(src.attributes.size()); |
| 376 | for (const xml::Attribute& attr : src.attributes) { |
| 377 | xml::Attribute cloned_attr; |
| 378 | cloned_attr.name = attr.name; |
| 379 | cloned_attr.namespace_uri = attr.namespace_uri; |
| 380 | cloned_attr.value = attr.value; |
| 381 | cloned_attr.compiled_attribute = attr.compiled_attribute; |
| 382 | if (attr.compiled_value != nullptr) { |
| 383 | cloned_attr.compiled_value.reset(attr.compiled_value->Clone(&cloned->string_pool)); |
| 384 | } |
| 385 | dst->attributes.push_back(std::move(cloned_attr)); |
| 386 | } |
| 387 | }); |
| 388 | } |
| 389 | return cloned; |
| 390 | } |
| 391 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 392 | Element* FindRootElement(Node* node) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 393 | if (node == nullptr) { |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 394 | return nullptr; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 395 | } |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 396 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 397 | while (node->parent != nullptr) { |
| 398 | node = node->parent; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 399 | } |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 400 | return NodeCast<Element>(node); |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 401 | } |
| 402 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 403 | void Element::AppendChild(std::unique_ptr<Node> child) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 404 | child->parent = this; |
| 405 | children.push_back(std::move(child)); |
| 406 | } |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 407 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 408 | void Element::InsertChild(size_t index, std::unique_ptr<Node> child) { |
Adam Lesinski | e343eb1 | 2016-10-27 16:31:58 -0700 | [diff] [blame] | 409 | child->parent = this; |
| 410 | children.insert(children.begin() + index, std::move(child)); |
| 411 | } |
| 412 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 413 | Attribute* Element::FindAttribute(const StringPiece& ns, const StringPiece& name) { |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 414 | return const_cast<Attribute*>(static_cast<const Element*>(this)->FindAttribute(ns, name)); |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 417 | const Attribute* Element::FindAttribute(const StringPiece& ns, const StringPiece& name) const { |
| 418 | for (const auto& attr : attributes) { |
| 419 | if (ns == attr.namespace_uri && name == attr.name) { |
| 420 | return &attr; |
| 421 | } |
| 422 | } |
| 423 | return nullptr; |
| 424 | } |
| 425 | |
Colin Cross | dcd58c4 | 2018-05-25 22:46:35 -0700 | [diff] [blame] | 426 | void Element::RemoveAttribute(const StringPiece& ns, const StringPiece& name) { |
| 427 | auto new_attr_end = std::remove_if(attributes.begin(), attributes.end(), |
| 428 | [&](const Attribute& attr) -> bool { |
| 429 | return ns == attr.namespace_uri && name == attr.name; |
| 430 | }); |
| 431 | |
| 432 | attributes.erase(new_attr_end, attributes.end()); |
| 433 | } |
| 434 | |
Adam Lesinski | c628437 | 2017-12-04 13:46:23 -0800 | [diff] [blame] | 435 | Attribute* Element::FindOrCreateAttribute(const StringPiece& ns, const StringPiece& name) { |
| 436 | Attribute* attr = FindAttribute(ns, name); |
| 437 | if (attr == nullptr) { |
| 438 | attributes.push_back(Attribute{ns.to_string(), name.to_string()}); |
| 439 | attr = &attributes.back(); |
| 440 | } |
| 441 | return attr; |
| 442 | } |
| 443 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 444 | Element* Element::FindChild(const StringPiece& ns, const StringPiece& name) { |
| 445 | return FindChildWithAttribute(ns, name, {}, {}, {}); |
| 446 | } |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 447 | |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 448 | const Element* Element::FindChild(const StringPiece& ns, const StringPiece& name) const { |
| 449 | return FindChildWithAttribute(ns, name, {}, {}, {}); |
| 450 | } |
| 451 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 452 | Element* Element::FindChildWithAttribute(const StringPiece& ns, const StringPiece& name, |
| 453 | const StringPiece& attr_ns, const StringPiece& attr_name, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 454 | const StringPiece& attr_value) { |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 455 | return const_cast<Element*>(static_cast<const Element*>(this)->FindChildWithAttribute( |
| 456 | ns, name, attr_ns, attr_name, attr_value)); |
| 457 | } |
| 458 | |
| 459 | const Element* Element::FindChildWithAttribute(const StringPiece& ns, const StringPiece& name, |
| 460 | const StringPiece& attr_ns, |
| 461 | const StringPiece& attr_name, |
| 462 | const StringPiece& attr_value) const { |
| 463 | for (const auto& child : children) { |
| 464 | if (const Element* el = NodeCast<Element>(child.get())) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 465 | if (ns == el->namespace_uri && name == el->name) { |
| 466 | if (attr_ns.empty() && attr_name.empty()) { |
| 467 | return el; |
| 468 | } |
| 469 | |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 470 | const Attribute* attr = el->FindAttribute(attr_ns, attr_name); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 471 | if (attr && attr_value == attr->value) { |
| 472 | return el; |
| 473 | } |
| 474 | } |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 475 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 476 | } |
| 477 | return nullptr; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 478 | } |
| 479 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 480 | std::vector<Element*> Element::GetChildElements() { |
| 481 | std::vector<Element*> elements; |
| 482 | for (auto& child_node : children) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 483 | if (Element* child = NodeCast<Element>(child_node.get())) { |
| 484 | elements.push_back(child); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 485 | } |
| 486 | } |
| 487 | return elements; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 488 | } |
| 489 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 490 | std::unique_ptr<Node> Element::Clone(const ElementCloneFunc& el_cloner) const { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 491 | auto el = util::make_unique<Element>(); |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 492 | el->namespace_decls = namespace_decls; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 493 | el->comment = comment; |
| 494 | el->line_number = line_number; |
| 495 | el->column_number = column_number; |
| 496 | el->name = name; |
| 497 | el->namespace_uri = namespace_uri; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 498 | el->attributes.reserve(attributes.size()); |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 499 | el_cloner(*this, el.get()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 500 | el->children.reserve(children.size()); |
| 501 | for (const std::unique_ptr<xml::Node>& child : children) { |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 502 | el->AppendChild(child->Clone(el_cloner)); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 503 | } |
| 504 | return std::move(el); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 505 | } |
| 506 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 507 | std::unique_ptr<Element> Element::CloneElement(const ElementCloneFunc& el_cloner) const { |
| 508 | return std::unique_ptr<Element>(static_cast<Element*>(Clone(el_cloner).release())); |
| 509 | } |
| 510 | |
| 511 | void Element::Accept(Visitor* visitor) { |
| 512 | visitor->BeforeVisitElement(this); |
| 513 | visitor->Visit(this); |
| 514 | visitor->AfterVisitElement(this); |
| 515 | } |
| 516 | |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 517 | void Element::Accept(ConstVisitor* visitor) const { |
| 518 | visitor->BeforeVisitElement(this); |
| 519 | visitor->Visit(this); |
| 520 | visitor->AfterVisitElement(this); |
| 521 | } |
| 522 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 523 | std::unique_ptr<Node> Text::Clone(const ElementCloneFunc&) const { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 524 | auto t = util::make_unique<Text>(); |
| 525 | t->comment = comment; |
| 526 | t->line_number = line_number; |
| 527 | t->column_number = column_number; |
| 528 | t->text = text; |
| 529 | return std::move(t); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 530 | } |
| 531 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 532 | void Text::Accept(Visitor* visitor) { |
| 533 | visitor->Visit(this); |
| 534 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 535 | |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 536 | void Text::Accept(ConstVisitor* visitor) const { |
| 537 | visitor->Visit(this); |
| 538 | } |
| 539 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 540 | void PackageAwareVisitor::BeforeVisitElement(Element* el) { |
| 541 | std::vector<PackageDecl> decls; |
| 542 | for (const NamespaceDecl& decl : el->namespace_decls) { |
| 543 | if (Maybe<ExtractedPackage> maybe_package = ExtractPackageFromNamespace(decl.uri)) { |
| 544 | decls.push_back(PackageDecl{decl.prefix, std::move(maybe_package.value())}); |
| 545 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 546 | } |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 547 | package_decls_.push_back(std::move(decls)); |
| 548 | } |
| 549 | |
| 550 | void PackageAwareVisitor::AfterVisitElement(Element* el) { |
| 551 | package_decls_.pop_back(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 552 | } |
| 553 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 554 | Maybe<ExtractedPackage> PackageAwareVisitor::TransformPackageAlias(const StringPiece& alias) const { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 555 | if (alias.empty()) { |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 556 | return ExtractedPackage{{}, false /*private*/}; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | const auto rend = package_decls_.rend(); |
| 560 | for (auto iter = package_decls_.rbegin(); iter != rend; ++iter) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 561 | const std::vector<PackageDecl>& decls = *iter; |
| 562 | const auto rend2 = decls.rend(); |
| 563 | for (auto iter2 = decls.rbegin(); iter2 != rend2; ++iter2) { |
| 564 | const PackageDecl& decl = *iter2; |
| 565 | if (alias == decl.prefix) { |
| 566 | if (decl.package.package.empty()) { |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 567 | return ExtractedPackage{{}, decl.package.private_namespace}; |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 568 | } |
| 569 | return decl.package; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 570 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 571 | } |
| 572 | } |
| 573 | return {}; |
| 574 | } |
| 575 | |
| 576 | } // namespace xml |
| 577 | } // namespace aapt |