XMLParser.cpp 5.1 KB

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