XMLParseTools.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 "XMLParseTools.h"
  29. #include "../../Include/Rocket/Core/StreamMemory.h"
  30. #include "Template.h"
  31. #include "TemplateCache.h"
  32. #include "../../Include/Rocket/Core.h"
  33. #include <ctype.h>
  34. namespace Rocket {
  35. namespace Core {
  36. // Searchs a string for the specified tag
  37. // NOTE: tag *MUST* be in lowercase
  38. const char* XMLParseTools::FindTag(const char* tag, const char* string, bool closing_tag)
  39. {
  40. int length = (int)strlen(tag);
  41. const char* ptr = string;
  42. bool found_closing = false;
  43. while (*ptr)
  44. {
  45. // Check if the first character matches
  46. if (tolower((*ptr)) == tag[0])
  47. {
  48. // If it does, check the whole word
  49. if (strncasecmp(ptr,tag,length) == 0)
  50. {
  51. // Check for opening <, loop back in the string skipping white space and forward slashes if
  52. // we're looking for the closing tag
  53. const char* tag_start = ptr - 1;
  54. while (tag_start > string && (StringUtilities::IsWhitespace(*tag_start) || *tag_start == '/'))
  55. {
  56. if (*tag_start == '/')
  57. found_closing = true;
  58. tag_start--;
  59. }
  60. // If the character we're looking at is a <, and found closing matches closing tag,
  61. // its the tag we're looking for
  62. if (*tag_start == '<' && found_closing == closing_tag)
  63. return tag_start;
  64. // Otherwise, keep looking
  65. }
  66. }
  67. ptr++;
  68. }
  69. return NULL;
  70. }
  71. bool XMLParseTools::ReadAttribute(const char* &string, String& name, String& value)
  72. {
  73. const char* ptr = string;
  74. name = "";
  75. value = "";
  76. // Skip whitespace
  77. while (StringUtilities::IsWhitespace(*ptr))
  78. ptr++;
  79. // Look for the end of the attribute name
  80. bool found_whitespace = false;
  81. while (*ptr != '=' && *ptr != '>' && (!found_whitespace || StringUtilities::IsWhitespace(*ptr)))
  82. {
  83. if (StringUtilities::IsWhitespace(*ptr))
  84. found_whitespace = true;
  85. else
  86. name += *ptr;
  87. ptr++;
  88. }
  89. if (*ptr == '>')
  90. return false;
  91. // If we stopped on an equals, parse the value
  92. if (*ptr == '=')
  93. {
  94. // Skip over white space, ='s and quotes
  95. bool quoted = false;
  96. while (StringUtilities::IsWhitespace(*ptr) || *ptr == '\'' || *ptr == '"' || *ptr == '=')
  97. {
  98. if (*ptr == '\'' || *ptr == '"')
  99. quoted = true;
  100. ptr++;
  101. }
  102. if (*ptr == '>')
  103. return false;
  104. // Store the value
  105. while (*ptr != '\'' && *ptr != '"' && *ptr != '>' && (*ptr != ' ' || quoted))
  106. {
  107. value += *ptr++;
  108. }
  109. if (*ptr == '>')
  110. return false;
  111. // Advance passed the quote
  112. if (quoted)
  113. ptr++;
  114. }
  115. else
  116. {
  117. ptr--;
  118. }
  119. // Update the string pointer
  120. string = ptr;
  121. return true;
  122. }
  123. Element* XMLParseTools::ParseTemplate(Element* element, const String& template_name)
  124. {
  125. // Load the template, and parse it
  126. Template* parse_template = TemplateCache::GetTemplate(template_name);
  127. if (!parse_template)
  128. {
  129. Log::ParseError(element->GetOwnerDocument()->GetSourceURL(), -1, "Failed to find template '%s'.", template_name.CString());
  130. return element;
  131. }
  132. return parse_template->ParseTemplate(element);
  133. }
  134. }
  135. }