Template.cpp 3.9 KB

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