BaseXMLParser.h

00001 /*
00002  * This source file is part of libRocket, the HTML/CSS Interface Middleware
00003  *
00004  * For the latest information, see http://www.librocket.com
00005  *
00006  * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
00007  *
00008  * Permission is hereby granted, free of charge, to any person obtaining a copy
00009  * of this software and associated documentation files (the "Software"), to deal
00010  * in the Software without restriction, including without limitation the rights
00011  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00012  * copies of the Software, and to permit persons to whom the Software is
00013  * furnished to do so, subject to the following conditions:
00014  *
00015  * The above copyright notice and this permission notice shall be included in
00016  * all copies or substantial portions of the Software.
00017  * 
00018  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00019  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00020  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00021  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00022  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00023  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00024  * THE SOFTWARE.
00025  *
00026  */
00027 
00028 #ifndef ROCKETCOREBASEXMLPARSER_H
00029 #define ROCKETCOREBASEXMLPARSER_H
00030 
00031 #include <Rocket/Core/Header.h>
00032 #include <Rocket/Core/Types.h>
00033 #include <Rocket/Core/Dictionary.h>
00034 #include <set>
00035 
00036 namespace Rocket {
00037 namespace Core {
00038 
00039 class Stream;
00040 
00041 typedef Dictionary XMLAttributes;
00042 
00047 class ROCKETCORE_API BaseXMLParser
00048 {
00049         public:
00050                 BaseXMLParser();
00051                 virtual ~BaseXMLParser();
00052 
00056                 void RegisterCDATATag(const String& tag);
00057 
00060                 void Parse(Stream* stream);
00061 
00064                 int GetLineNumber();
00065 
00067                 virtual void HandleElementStart(const String& name, const XMLAttributes& attributes);
00069                 virtual void HandleElementEnd(const String& name);
00071                 virtual void HandleData(const String& data);
00072 
00073         protected:
00074                 // The stream we're reading the XML from.
00075                 Stream* xml_source;
00076 
00077         private:
00078                 void ReadHeader();
00079                 void ReadBody();
00080 
00081                 bool ReadOpenTag();
00082                 bool ReadCloseTag();
00083                 bool ReadAttributes(XMLAttributes& attributes);
00084                 bool ReadCDATA(const char* terminator = NULL);
00085 
00086                 // Reads from the stream until a complete word is found.
00087                 // @param[out] word Word thats been found
00088                 // @param[in] terminators List of characters that terminate the search
00089                 bool FindWord(String& word, const char* terminators = NULL);
00090                 // Reads from the stream until the given character set is found. All
00091                 // intervening characters will be returned in data.
00092                 bool FindString(const unsigned char* string, String& data);
00093                 // Returns true if the next sequence of characters in the stream
00094                 // matches the given string. If consume is set and this returns true,
00095                 // the characters will be consumed.
00096                 bool PeekString(const unsigned char* string, bool consume = true);
00097 
00098                 // Fill the buffer as much as possible, without removing any content that is still pending
00099                 bool FillBuffer();
00100 
00101                 unsigned char* read;
00102                 unsigned char* buffer;
00103                 int buffer_size;
00104                 int buffer_used;
00105                 int line_number;
00106                 int open_tag_depth;
00107 
00108                 // The element attributes being read.
00109                 XMLAttributes attributes;
00110                 // The loose data being read.
00111                 String data;
00112 
00113                 std::set< String > cdata_tags;
00114 };
00115 
00116 }
00117 }
00118 
00119 #endif