XMLParser.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "../../Include/RmlUi/Core/XMLParser.h"
  29. #include "../../Include/RmlUi/Core/Element.h"
  30. #include "../../Include/RmlUi/Core/Factory.h"
  31. #include "../../Include/RmlUi/Core/Log.h"
  32. #include "../../Include/RmlUi/Core/Profiling.h"
  33. #include "../../Include/RmlUi/Core/Stream.h"
  34. #include "../../Include/RmlUi/Core/Types.h"
  35. #include "../../Include/RmlUi/Core/URL.h"
  36. #include "../../Include/RmlUi/Core/XMLNodeHandler.h"
  37. #include "ControlledLifetimeResource.h"
  38. #include "DocumentHeader.h"
  39. namespace Rml {
  40. struct XmlParserData {
  41. UnorderedMap<String, SharedPtr<XMLNodeHandler>> node_handlers;
  42. UnorderedSet<String> cdata_tags;
  43. SharedPtr<XMLNodeHandler> default_node_handler;
  44. };
  45. static ControlledLifetimeResource<XmlParserData> xml_parser_data;
  46. XMLParser::XMLParser(Element* root)
  47. {
  48. for (const String& cdata_tag : xml_parser_data->cdata_tags)
  49. RegisterCDATATag(cdata_tag);
  50. for (const String& name : Factory::GetStructuralDataViewAttributeNames())
  51. RegisterInnerXMLAttribute(name);
  52. // Add the first frame.
  53. ParseFrame frame;
  54. frame.element = root;
  55. if (root != nullptr)
  56. {
  57. frame.tag = root->GetTagName();
  58. auto itr = xml_parser_data->node_handlers.find(root->GetTagName());
  59. if (itr != xml_parser_data->node_handlers.end())
  60. {
  61. frame.node_handler = itr->second.get();
  62. }
  63. else
  64. {
  65. frame.node_handler = xml_parser_data->default_node_handler.get();
  66. }
  67. }
  68. stack.push(frame);
  69. active_handler = nullptr;
  70. header = MakeUnique<DocumentHeader>();
  71. }
  72. XMLParser::~XMLParser() {}
  73. void XMLParser::RegisterPersistentCDATATag(const String& _tag)
  74. {
  75. if (!xml_parser_data)
  76. xml_parser_data.Initialize();
  77. String tag = StringUtilities::ToLower(_tag);
  78. if (tag.empty())
  79. return;
  80. xml_parser_data->cdata_tags.insert(tag);
  81. }
  82. XMLNodeHandler* XMLParser::RegisterNodeHandler(const String& _tag, SharedPtr<XMLNodeHandler> handler)
  83. {
  84. if (!xml_parser_data)
  85. xml_parser_data.Initialize();
  86. String tag = StringUtilities::ToLower(_tag);
  87. // Check for a default node registration.
  88. if (tag.empty())
  89. {
  90. xml_parser_data->default_node_handler = std::move(handler);
  91. return xml_parser_data->default_node_handler.get();
  92. }
  93. XMLNodeHandler* result = handler.get();
  94. xml_parser_data->node_handlers[tag] = std::move(handler);
  95. return result;
  96. }
  97. XMLNodeHandler* XMLParser::GetNodeHandler(const String& tag)
  98. {
  99. auto it = xml_parser_data->node_handlers.find(tag);
  100. if (it != xml_parser_data->node_handlers.end())
  101. return it->second.get();
  102. return nullptr;
  103. }
  104. void XMLParser::ReleaseHandlers()
  105. {
  106. xml_parser_data.Shutdown();
  107. }
  108. DocumentHeader* XMLParser::GetDocumentHeader()
  109. {
  110. return header.get();
  111. }
  112. void XMLParser::PushDefaultHandler()
  113. {
  114. active_handler = xml_parser_data->default_node_handler.get();
  115. }
  116. bool XMLParser::PushHandler(const String& tag)
  117. {
  118. auto it = xml_parser_data->node_handlers.find(StringUtilities::ToLower(tag));
  119. if (it == xml_parser_data->node_handlers.end())
  120. return false;
  121. active_handler = it->second.get();
  122. return true;
  123. }
  124. const XMLParser::ParseFrame* XMLParser::GetParseFrame() const
  125. {
  126. return &stack.top();
  127. }
  128. const URL& XMLParser::GetSourceURL() const
  129. {
  130. RMLUI_ASSERT(GetSourceURLPtr());
  131. return *GetSourceURLPtr();
  132. }
  133. void XMLParser::HandleElementStart(const String& _name, const XMLAttributes& attributes)
  134. {
  135. RMLUI_ZoneScoped;
  136. const String name = StringUtilities::ToLower(_name);
  137. // Check for a specific handler that will override the child handler.
  138. auto itr = xml_parser_data->node_handlers.find(name);
  139. if (itr != xml_parser_data->node_handlers.end())
  140. active_handler = itr->second.get();
  141. // Store the current active handler, so we can use it through this function (as active handler may change)
  142. XMLNodeHandler* node_handler = active_handler;
  143. Element* element = nullptr;
  144. // Get the handler to handle the open tag
  145. if (node_handler)
  146. {
  147. element = node_handler->ElementStart(this, name, attributes);
  148. }
  149. // Push onto the stack
  150. ParseFrame frame;
  151. frame.node_handler = node_handler;
  152. frame.child_handler = active_handler;
  153. frame.element = (element ? element : stack.top().element);
  154. frame.tag = name;
  155. stack.push(frame);
  156. }
  157. void XMLParser::HandleElementEnd(const String& _name)
  158. {
  159. RMLUI_ZoneScoped;
  160. String name = StringUtilities::ToLower(_name);
  161. // Copy the top of the stack
  162. ParseFrame frame = stack.top();
  163. // Pop the frame
  164. stack.pop();
  165. // Restore active handler to the previous frame's child handler
  166. active_handler = stack.top().child_handler;
  167. // Check frame names
  168. if (name != frame.tag)
  169. {
  170. Log::Message(Log::LT_ERROR, "Closing tag '%s' mismatched on %s:%d was expecting '%s'.", name.c_str(), GetSourceURL().GetURL().c_str(),
  171. GetLineNumber(), frame.tag.c_str());
  172. }
  173. // Call element end handler
  174. if (frame.node_handler)
  175. {
  176. frame.node_handler->ElementEnd(this, name);
  177. }
  178. }
  179. void XMLParser::HandleData(const String& data, XMLDataType type)
  180. {
  181. RMLUI_ZoneScoped;
  182. if (stack.top().node_handler)
  183. stack.top().node_handler->ElementData(this, data, type);
  184. }
  185. } // namespace Rml