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