Template.cpp 3.9 KB

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