Template.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include "Template.h"
  29. #include "XMLParseTools.h"
  30. #include "../../Include/Rocket/Core/ElementUtilities.h"
  31. #include "../../Include/Rocket/Core/XMLParser.h"
  32. namespace Rocket {
  33. namespace Core {
  34. Template::Template()
  35. {
  36. body = NULL;
  37. }
  38. Template::~Template()
  39. {
  40. if (body)
  41. body->RemoveReference();
  42. }
  43. const String& Template::GetName() const
  44. {
  45. return name;
  46. }
  47. bool Template::Load(Stream* stream)
  48. {
  49. // Load the entire template into memory so we can pull out
  50. // the header and body tags
  51. String buffer;
  52. stream->Read(buffer, stream->Length());
  53. // Pull out the header
  54. const char* head_start = XMLParseTools::FindTag("head", buffer.CString());
  55. if (!head_start)
  56. return false;
  57. const char* head_end = XMLParseTools::FindTag("head", head_start, true);
  58. if (!head_end)
  59. return false;
  60. // Advance to the end of the tag
  61. head_end = strchr(head_end, '>') + 1;
  62. // Pull out the body
  63. const char* body_start = XMLParseTools::FindTag("body", head_end);
  64. if (!body_start)
  65. return false;
  66. const char* body_end = XMLParseTools::FindTag("body", body_start, true);
  67. if (!body_end)
  68. return false;
  69. // Advance to the end of the tag
  70. body_end = strchr(body_end, '>') + 1;
  71. // Find the RML tag, skip over it and read the attributes,
  72. // storing the ones we're interested in.
  73. String attribute_name;
  74. String attribute_value;
  75. const char* ptr = XMLParseTools::FindTag("template", buffer.CString());
  76. if (!ptr)
  77. return false;
  78. while (XMLParseTools::ReadAttribute(++ptr, attribute_name, attribute_value))
  79. {
  80. if (attribute_name == "name")
  81. name = attribute_value;
  82. if (attribute_name == "content")
  83. content = attribute_value;
  84. }
  85. // Create a stream around the header, parse it and store it
  86. StreamMemory* header_stream = new StreamMemory((const byte*) head_start,head_end - head_start);
  87. header_stream->SetSourceURL(stream->GetSourceURL());
  88. XMLParser parser(NULL);
  89. parser.Parse(header_stream);
  90. header_stream->RemoveReference();
  91. header = *parser.GetDocumentHeader();
  92. // Store the body in stream form
  93. body = new StreamMemory(body_end - body_start);
  94. body->SetSourceURL(stream->GetSourceURL());
  95. body->PushBack(body_start, body_end - body_start);
  96. return true;
  97. }
  98. Element* Template::ParseTemplate(Element* element)
  99. {
  100. body->Seek(0, SEEK_SET);
  101. XMLParser parser(element);
  102. parser.Parse(body);
  103. // If theres an inject attribute on the template,
  104. // attempt to find the required element
  105. if (!content.Empty())
  106. {
  107. Element* content_element = ElementUtilities::GetElementById(element, content);
  108. if (content_element)
  109. element = content_element;
  110. }
  111. return element;
  112. }
  113. const DocumentHeader* Template::GetHeader()
  114. {
  115. return &header;
  116. }
  117. }
  118. }