2
0

XMLParser.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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/Factory.h"
  30. #include "../../Include/RmlUi/Core/Log.h"
  31. #include "../../Include/RmlUi/Core/Profiling.h"
  32. #include "../../Include/RmlUi/Core/Stream.h"
  33. #include "../../Include/RmlUi/Core/Types.h"
  34. #include "../../Include/RmlUi/Core/URL.h"
  35. #include "../../Include/RmlUi/Core/XMLNodeHandler.h"
  36. #include "ControlledLifetimeResource.h"
  37. #include "DocumentHeader.h"
  38. namespace Rml {
  39. struct XmlParserData {
  40. UnorderedMap<String, SharedPtr<XMLNodeHandler>> node_handlers;
  41. SharedPtr<XMLNodeHandler> default_node_handler;
  42. };
  43. static ControlledLifetimeResource<XmlParserData> xml_parser_data;
  44. XMLParser::XMLParser(Element* root)
  45. {
  46. RegisterCDATATag("script");
  47. RegisterCDATATag("style");
  48. for (const String& name : Factory::GetStructuralDataViewAttributeNames())
  49. RegisterInnerXMLAttribute(name);
  50. // Add the first frame.
  51. ParseFrame frame;
  52. frame.element = root;
  53. stack.push(frame);
  54. active_handler = nullptr;
  55. header = MakeUnique<DocumentHeader>();
  56. }
  57. XMLParser::~XMLParser() {}
  58. XMLNodeHandler* XMLParser::RegisterNodeHandler(const String& _tag, SharedPtr<XMLNodeHandler> handler)
  59. {
  60. if (!xml_parser_data)
  61. xml_parser_data.Initialize();
  62. String tag = StringUtilities::ToLower(_tag);
  63. // Check for a default node registration.
  64. if (tag.empty())
  65. {
  66. xml_parser_data->default_node_handler = std::move(handler);
  67. return xml_parser_data->default_node_handler.get();
  68. }
  69. XMLNodeHandler* result = handler.get();
  70. xml_parser_data->node_handlers[tag] = std::move(handler);
  71. return result;
  72. }
  73. XMLNodeHandler* XMLParser::GetNodeHandler(const String& tag)
  74. {
  75. auto it = xml_parser_data->node_handlers.find(tag);
  76. if (it != xml_parser_data->node_handlers.end())
  77. return it->second.get();
  78. return nullptr;
  79. }
  80. void XMLParser::ReleaseHandlers()
  81. {
  82. xml_parser_data.Shutdown();
  83. }
  84. DocumentHeader* XMLParser::GetDocumentHeader()
  85. {
  86. return header.get();
  87. }
  88. void XMLParser::PushDefaultHandler()
  89. {
  90. active_handler = xml_parser_data->default_node_handler.get();
  91. }
  92. bool XMLParser::PushHandler(const String& tag)
  93. {
  94. auto it = xml_parser_data->node_handlers.find(StringUtilities::ToLower(tag));
  95. if (it == xml_parser_data->node_handlers.end())
  96. return false;
  97. active_handler = it->second.get();
  98. return true;
  99. }
  100. const XMLParser::ParseFrame* XMLParser::GetParseFrame() const
  101. {
  102. return &stack.top();
  103. }
  104. const URL& XMLParser::GetSourceURL() const
  105. {
  106. RMLUI_ASSERT(GetSourceURLPtr());
  107. return *GetSourceURLPtr();
  108. }
  109. void XMLParser::HandleElementStart(const String& _name, const XMLAttributes& attributes)
  110. {
  111. RMLUI_ZoneScoped;
  112. const String name = StringUtilities::ToLower(_name);
  113. // Check for a specific handler that will override the child handler.
  114. auto itr = xml_parser_data->node_handlers.find(name);
  115. if (itr != xml_parser_data->node_handlers.end())
  116. active_handler = itr->second.get();
  117. // Store the current active handler, so we can use it through this function (as active handler may change)
  118. XMLNodeHandler* node_handler = active_handler;
  119. Element* element = nullptr;
  120. // Get the handler to handle the open tag
  121. if (node_handler)
  122. {
  123. element = node_handler->ElementStart(this, name, attributes);
  124. }
  125. // Push onto the stack
  126. ParseFrame frame;
  127. frame.node_handler = node_handler;
  128. frame.child_handler = active_handler;
  129. frame.element = (element ? element : stack.top().element);
  130. frame.tag = name;
  131. stack.push(frame);
  132. }
  133. void XMLParser::HandleElementEnd(const String& _name)
  134. {
  135. RMLUI_ZoneScoped;
  136. String name = StringUtilities::ToLower(_name);
  137. // Copy the top of the stack
  138. ParseFrame frame = stack.top();
  139. // Pop the frame
  140. stack.pop();
  141. // Restore active handler to the previous frame's child handler
  142. active_handler = stack.top().child_handler;
  143. // Check frame names
  144. if (name != frame.tag)
  145. {
  146. Log::Message(Log::LT_ERROR, "Closing tag '%s' mismatched on %s:%d was expecting '%s'.", name.c_str(), GetSourceURL().GetURL().c_str(),
  147. GetLineNumber(), frame.tag.c_str());
  148. }
  149. // Call element end handler
  150. if (frame.node_handler)
  151. {
  152. frame.node_handler->ElementEnd(this, name);
  153. }
  154. }
  155. void XMLParser::HandleData(const String& data, XMLDataType type)
  156. {
  157. RMLUI_ZoneScoped;
  158. if (stack.top().node_handler)
  159. stack.top().node_handler->ElementData(this, data, type);
  160. }
  161. } // namespace Rml