BaseXMLParser.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. #ifndef ROCKETCOREBASEXMLPARSER_H
  28. #define ROCKETCOREBASEXMLPARSER_H
  29. #include "Header.h"
  30. #include "Types.h"
  31. #include "Dictionary.h"
  32. #include <set>
  33. namespace Rocket {
  34. namespace Core {
  35. class Stream;
  36. typedef Dictionary XMLAttributes;
  37. /**
  38. @author Peter Curry
  39. */
  40. class ROCKETCORE_API BaseXMLParser
  41. {
  42. public:
  43. BaseXMLParser();
  44. virtual ~BaseXMLParser();
  45. /// Registers a tag as containing general character data. This will mean the contents of the tag will be parsed
  46. /// similarly to a CDATA tag (ie, no other markup will be recognised until the section's closing tag is found).
  47. /// @param[in] tag The tag to register as containing generic character data.
  48. void RegisterCDATATag(const String& tag);
  49. /// Parses the given stream as an XML file, and calls the handlers when
  50. /// interesting phenomena are encountered.
  51. void Parse(Stream* stream);
  52. /// Get the line number in the stream.
  53. /// @return The line currently being processed in the XML stream.
  54. int GetLineNumber();
  55. /// Called when the parser finds the beginning of an element tag.
  56. virtual void HandleElementStart(const String& name, const XMLAttributes& attributes);
  57. /// Called when the parser finds the end of an element tag.
  58. virtual void HandleElementEnd(const String& name);
  59. /// Called when the parser encounters data.
  60. virtual void HandleData(const String& data);
  61. protected:
  62. // The stream we're reading the XML from.
  63. Stream* xml_source;
  64. private:
  65. void ReadHeader();
  66. void ReadBody();
  67. bool ReadOpenTag();
  68. bool ReadCloseTag();
  69. bool ReadAttributes(XMLAttributes& attributes);
  70. bool ReadCDATA(const char* terminator = NULL);
  71. // Reads from the stream until a complete word is found.
  72. // @param[out] word Word thats been found
  73. // @param[in] terminators List of characters that terminate the search
  74. bool FindWord(String& word, const char* terminators = NULL);
  75. // Reads from the stream until the given character set is found. All
  76. // intervening characters will be returned in data.
  77. bool FindString(const unsigned char* string, String& data);
  78. // Returns true if the next sequence of characters in the stream
  79. // matches the given string. If consume is set and this returns true,
  80. // the characters will be consumed.
  81. bool PeekString(const unsigned char* string, bool consume = true);
  82. // Fill the buffer as much as possible, without removing any content that is still pending
  83. bool FillBuffer();
  84. unsigned char* read;
  85. unsigned char* buffer;
  86. int buffer_size;
  87. int buffer_used;
  88. int line_number;
  89. int open_tag_depth;
  90. // The element attributes being read.
  91. XMLAttributes attributes;
  92. // The loose data being read.
  93. String data;
  94. std::set< String > cdata_tags;
  95. };
  96. }
  97. }
  98. #endif