Template.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 "Template.h"
  29. #include "../../Include/RmlUi/Core/ElementUtilities.h"
  30. #include "../../Include/RmlUi/Core/XMLParser.h"
  31. #include "XMLParseTools.h"
  32. #include <string.h>
  33. namespace Rml {
  34. Template::Template() {}
  35. Template::~Template() {}
  36. const String& Template::GetName() const
  37. {
  38. return name;
  39. }
  40. bool Template::Load(Stream* stream)
  41. {
  42. // Load the entire template into memory so we can pull out
  43. // the header and body tags
  44. String buffer;
  45. stream->Read(buffer, stream->Length());
  46. // Pull out the header
  47. const char* head_start = XMLParseTools::FindTag("head", buffer.c_str());
  48. if (!head_start)
  49. return false;
  50. const char* head_end = XMLParseTools::FindTag("head", head_start, true);
  51. if (!head_end)
  52. return false;
  53. // Advance to the end of the tag
  54. head_end = strchr(head_end, '>') + 1;
  55. // Pull out the body
  56. const char* body_start = XMLParseTools::FindTag("body", head_end);
  57. if (!body_start)
  58. return false;
  59. const char* body_end = XMLParseTools::FindTag("body", body_start, true);
  60. if (!body_end)
  61. return false;
  62. // Advance to the end of the tag
  63. body_end = strchr(body_end, '>') + 1;
  64. // Find the RML tag, skip over it and read the attributes,
  65. // storing the ones we're interested in.
  66. String attribute_name;
  67. String attribute_value;
  68. const char* ptr = XMLParseTools::FindTag("template", buffer.c_str());
  69. if (!ptr)
  70. return false;
  71. while (XMLParseTools::ReadAttribute(++ptr, attribute_name, attribute_value))
  72. {
  73. if (attribute_name == "name")
  74. name = attribute_value;
  75. if (attribute_name == "content")
  76. content = attribute_value;
  77. }
  78. // Create a stream around the header, parse it and store it
  79. auto header_stream = MakeUnique<StreamMemory>((const byte*)head_start, head_end - head_start);
  80. header_stream->SetSourceURL(stream->GetSourceURL());
  81. XMLParser parser(nullptr);
  82. parser.Parse(header_stream.get());
  83. header_stream.reset();
  84. header = *parser.GetDocumentHeader();
  85. // Store the body in stream form
  86. body = MakeUnique<StreamMemory>(body_end - body_start);
  87. body->SetSourceURL(stream->GetSourceURL());
  88. body->PushBack(body_start, body_end - body_start);
  89. return true;
  90. }
  91. Element* Template::ParseTemplate(Element* element)
  92. {
  93. body->Seek(0, SEEK_SET);
  94. XMLParser parser(element);
  95. parser.Parse(body.get());
  96. // If theres an inject attribute on the template,
  97. // attempt to find the required element
  98. if (!content.empty())
  99. {
  100. Element* content_element = ElementUtilities::GetElementById(element, content);
  101. if (content_element)
  102. element = content_element;
  103. }
  104. return element;
  105. }
  106. const DocumentHeader* Template::GetHeader()
  107. {
  108. return &header;
  109. }
  110. } // namespace Rml