XMLParser.cpp 5.4 KB

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