Template.cpp 3.9 KB

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