XMLParser.cpp 5.4 KB

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