1 /*
2  *  Copyright 2004 The WebRTC Project Authors. All rights reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef WEBRTC_LIBJINGLE_XMPP_XMPPSTANZAPARSER_H_
12 #define WEBRTC_LIBJINGLE_XMPP_XMPPSTANZAPARSER_H_
13 
14 #include "webrtc/libjingle/xmllite/xmlbuilder.h"
15 #include "webrtc/libjingle/xmllite/xmlparser.h"
16 
17 
18 namespace buzz {
19 
20 class XmlElement;
21 
22 class XmppStanzaParseHandler {
23 public:
~XmppStanzaParseHandler()24   virtual ~XmppStanzaParseHandler() {}
25   virtual void StartStream(const XmlElement * pelStream) = 0;
26   virtual void Stanza(const XmlElement * pelStanza) = 0;
27   virtual void EndStream() = 0;
28   virtual void XmlError() = 0;
29 };
30 
31 class XmppStanzaParser {
32 public:
33   XmppStanzaParser(XmppStanzaParseHandler *psph);
Parse(const char * data,size_t len,bool isFinal)34   bool Parse(const char * data, size_t len, bool isFinal)
35     { return parser_.Parse(data, len, isFinal); }
36   void Reset();
37 
38 private:
39   class ParseHandler : public XmlParseHandler {
40   public:
ParseHandler(XmppStanzaParser * outer)41     ParseHandler(XmppStanzaParser * outer) : outer_(outer) {}
StartElement(XmlParseContext * pctx,const char * name,const char ** atts)42     virtual void StartElement(XmlParseContext * pctx,
43                const char * name, const char ** atts)
44       { outer_->IncomingStartElement(pctx, name, atts); }
EndElement(XmlParseContext * pctx,const char * name)45     virtual void EndElement(XmlParseContext * pctx,
46                const char * name)
47       { outer_->IncomingEndElement(pctx, name); }
CharacterData(XmlParseContext * pctx,const char * text,int len)48     virtual void CharacterData(XmlParseContext * pctx,
49                const char * text, int len)
50       { outer_->IncomingCharacterData(pctx, text, len); }
Error(XmlParseContext * pctx,XML_Error errCode)51     virtual void Error(XmlParseContext * pctx,
52                XML_Error errCode)
53       { outer_->IncomingError(pctx, errCode); }
54   private:
55     XmppStanzaParser * const outer_;
56   };
57 
58   friend class ParseHandler;
59 
60   void IncomingStartElement(XmlParseContext * pctx,
61                const char * name, const char ** atts);
62   void IncomingEndElement(XmlParseContext * pctx,
63                const char * name);
64   void IncomingCharacterData(XmlParseContext * pctx,
65                const char * text, int len);
66   void IncomingError(XmlParseContext * pctx,
67                XML_Error errCode);
68 
69   XmppStanzaParseHandler * psph_;
70   ParseHandler innerHandler_;
71   XmlParser parser_;
72   int depth_;
73   XmlBuilder builder_;
74 
75  };
76 
77 
78 }
79 
80 #endif  // WEBRTC_LIBJINGLE_XMPP_XMPPSTANZAPARSER_H_
81