XMLParser.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 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 "DocumentHeader.h"
  29. #include "../../Include/RmlUi/Core/Log.h"
  30. #include "../../Include/RmlUi/Core/Profiling.h"
  31. #include "../../Include/RmlUi/Core/Stream.h"
  32. #include "../../Include/RmlUi/Core/Types.h"
  33. #include "../../Include/RmlUi/Core/XMLNodeHandler.h"
  34. #include "../../Include/RmlUi/Core/URL.h"
  35. #include "../../Include/RmlUi/Core/XMLParser.h"
  36. namespace Rml {
  37. namespace Core {
  38. using NodeHandlers = UnorderedMap< String, SharedPtr<XMLNodeHandler> > ;
  39. static NodeHandlers node_handlers;
  40. static SharedPtr<XMLNodeHandler> default_node_handler;
  41. XMLParser::XMLParser(Element* root)
  42. {
  43. RegisterCDATATag("script");
  44. // Add the first frame.
  45. ParseFrame frame;
  46. frame.node_handler = nullptr;
  47. frame.child_handler = nullptr;
  48. frame.element = root;
  49. frame.tag = "";
  50. stack.push(frame);
  51. active_handler = nullptr;
  52. active_data_model = nullptr;
  53. header = new DocumentHeader();
  54. }
  55. XMLParser::~XMLParser()
  56. {
  57. delete header;
  58. }
  59. // Registers a custom node handler to be used to a given tag.
  60. XMLNodeHandler* XMLParser::RegisterNodeHandler(const String& _tag, SharedPtr<XMLNodeHandler> handler)
  61. {
  62. String tag = StringUtilities::ToLower(_tag);
  63. // Check for a default node registration.
  64. if (tag.empty())
  65. {
  66. default_node_handler = std::move(handler);
  67. return default_node_handler.get();
  68. }
  69. XMLNodeHandler* result = handler.get();
  70. node_handlers[tag] = std::move(handler);
  71. return result;
  72. }
  73. // Releases all registered node handlers. This is called internally.
  74. void XMLParser::ReleaseHandlers()
  75. {
  76. default_node_handler.reset();
  77. node_handlers.clear();
  78. }
  79. DocumentHeader* XMLParser::GetDocumentHeader()
  80. {
  81. return header;
  82. }
  83. const URL& XMLParser::GetSourceURL() const
  84. {
  85. return xml_source->GetSourceURL();
  86. }
  87. // Pushes the default element handler onto the parse stack.
  88. void XMLParser::PushDefaultHandler()
  89. {
  90. active_handler = default_node_handler.get();
  91. }
  92. bool XMLParser::PushHandler(const String& tag)
  93. {
  94. NodeHandlers::iterator i = node_handlers.find(StringUtilities::ToLower(tag));
  95. if (i == node_handlers.end())
  96. return false;
  97. active_handler = i->second.get();
  98. return true;
  99. }
  100. /// Access the current parse frame
  101. const XMLParser::ParseFrame* XMLParser::GetParseFrame() const
  102. {
  103. return &stack.top();
  104. }
  105. DataModel* XMLParser::GetDataModel() const
  106. {
  107. return active_data_model;
  108. }
  109. /// Called when the parser finds the beginning of an element tag.
  110. void XMLParser::HandleElementStart(const String& _name, const XMLAttributes& attributes)
  111. {
  112. RMLUI_ZoneScoped;
  113. const String name = StringUtilities::ToLower(_name);
  114. // Check for a specific handler that will override the child handler.
  115. NodeHandlers::iterator itr = node_handlers.find(name);
  116. if (itr != node_handlers.end())
  117. active_handler = itr->second.get();
  118. // Store the current active handler, so we can use it through this function (as active handler may change)
  119. XMLNodeHandler* node_handler = active_handler;
  120. Element* element = nullptr;
  121. // Get the handler to handle the open tag
  122. if (node_handler)
  123. {
  124. element = node_handler->ElementStart(this, name, attributes);
  125. }
  126. static const String data_model = "data-model";
  127. auto it = attributes.find(data_model);
  128. if (element && it != attributes.end())
  129. {
  130. String data_model = it->second.Get<String>();
  131. active_data_model = nullptr;
  132. if (auto context = element->GetContext())
  133. active_data_model = context->GetDataModel( data_model );
  134. if(!active_data_model)
  135. Log::Message(Log::LT_WARNING, "Could not locate data model '%s'.", data_model.c_str());
  136. }
  137. // Push onto the stack
  138. ParseFrame frame;
  139. frame.node_handler = node_handler;
  140. frame.child_handler = active_handler;
  141. frame.element = (element ? element : stack.top().element);
  142. frame.tag = name;
  143. frame.data_model = active_data_model;
  144. stack.push(frame);
  145. }
  146. /// Called when the parser finds the end of an element tag.
  147. void XMLParser::HandleElementEnd(const String& _name)
  148. {
  149. RMLUI_ZoneScoped;
  150. String name = StringUtilities::ToLower(_name);
  151. // Copy the top of the stack
  152. ParseFrame frame = stack.top();
  153. // Pop the frame
  154. stack.pop();
  155. // Restore active handler to the previous frame's child handler
  156. active_handler = stack.top().child_handler;
  157. // Restore the active data model to the current frame's model
  158. active_data_model = stack.top().data_model;
  159. // Check frame names
  160. if (name != frame.tag)
  161. {
  162. Log::Message(Log::LT_ERROR, "Closing tag '%s' mismatched on %s:%d was expecting '%s'.", name.c_str(), GetSourceURL().GetURL().c_str(), GetLineNumber(), frame.tag.c_str());
  163. }
  164. // Call element end handler
  165. if (frame.node_handler)
  166. {
  167. frame.node_handler->ElementEnd(this, name);
  168. }
  169. }
  170. /// Called when the parser encounters data.
  171. void XMLParser::HandleData(const String& data)
  172. {
  173. RMLUI_ZoneScoped;
  174. if (stack.top().node_handler)
  175. stack.top().node_handler->ElementData(this, data);
  176. }
  177. }
  178. }