1 /* 2 * libjingle 3 * Copyright 2004--2005, Google Inc. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 3. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #ifndef _xmppstanzaparser_h_ 29 #define _xmppstanzaparser_h_ 30 31 #include "talk/xmllite/xmlparser.h" 32 #include "talk/xmllite/xmlbuilder.h" 33 34 35 namespace buzz { 36 37 class XmlElement; 38 39 class XmppStanzaParseHandler { 40 public: ~XmppStanzaParseHandler()41 virtual ~XmppStanzaParseHandler() {} 42 virtual void StartStream(const XmlElement * pelStream) = 0; 43 virtual void Stanza(const XmlElement * pelStanza) = 0; 44 virtual void EndStream() = 0; 45 virtual void XmlError() = 0; 46 }; 47 48 class XmppStanzaParser { 49 public: 50 XmppStanzaParser(XmppStanzaParseHandler *psph); Parse(const char * data,size_t len,bool isFinal)51 bool Parse(const char * data, size_t len, bool isFinal) 52 { return parser_.Parse(data, len, isFinal); } 53 void Reset(); 54 55 private: 56 class ParseHandler : public XmlParseHandler { 57 public: ParseHandler(XmppStanzaParser * outer)58 ParseHandler(XmppStanzaParser * outer) : outer_(outer) {} StartElement(XmlParseContext * pctx,const char * name,const char ** atts)59 virtual void StartElement(XmlParseContext * pctx, 60 const char * name, const char ** atts) 61 { outer_->IncomingStartElement(pctx, name, atts); } EndElement(XmlParseContext * pctx,const char * name)62 virtual void EndElement(XmlParseContext * pctx, 63 const char * name) 64 { outer_->IncomingEndElement(pctx, name); } CharacterData(XmlParseContext * pctx,const char * text,int len)65 virtual void CharacterData(XmlParseContext * pctx, 66 const char * text, int len) 67 { outer_->IncomingCharacterData(pctx, text, len); } Error(XmlParseContext * pctx,XML_Error errCode)68 virtual void Error(XmlParseContext * pctx, 69 XML_Error errCode) 70 { outer_->IncomingError(pctx, errCode); } 71 private: 72 XmppStanzaParser * const outer_; 73 }; 74 75 friend class ParseHandler; 76 77 void IncomingStartElement(XmlParseContext * pctx, 78 const char * name, const char ** atts); 79 void IncomingEndElement(XmlParseContext * pctx, 80 const char * name); 81 void IncomingCharacterData(XmlParseContext * pctx, 82 const char * text, int len); 83 void IncomingError(XmlParseContext * pctx, 84 XML_Error errCode); 85 86 XmppStanzaParseHandler * psph_; 87 ParseHandler innerHandler_; 88 XmlParser parser_; 89 int depth_; 90 XmlBuilder builder_; 91 92 }; 93 94 95 } 96 97 #endif 98