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 THIRD_PARTY_LIBJINGLE_XMPP_XMLLITE_XMLPARSER_H_
12 #define THIRD_PARTY_LIBJINGLE_XMPP_XMLLITE_XMLPARSER_H_
13 
14 #include <string>
15 
16 #include "third_party/libjingle_xmpp/xmllite/xmlnsstack.h"
17 #ifdef EXPAT_RELATIVE_PATH
18 #include "expat.h"
19 #else
20 #include "third_party/expat/v2_0_1/Source/lib/expat.h"
21 #endif  // EXPAT_RELATIVE_PATH
22 
23 struct XML_ParserStruct;
24 typedef struct XML_ParserStruct* XML_Parser;
25 
26 namespace jingle_xmpp {
27 
28 class XmlParseHandler;
29 class XmlParseContext;
30 class XmlParser;
31 
32 class XmlParseContext {
33 public:
~XmlParseContext()34   virtual ~XmlParseContext() {}
35   virtual QName ResolveQName(const char * qname, bool isAttr) = 0;
36   virtual void RaiseError(XML_Error err) = 0;
37   virtual void GetPosition(unsigned long * line, unsigned long * column,
38                            unsigned long * byte_index) = 0;
39 };
40 
41 class XmlParseHandler {
42 public:
~XmlParseHandler()43   virtual ~XmlParseHandler() {}
44   virtual void StartElement(XmlParseContext * pctx,
45                const char * name, const char ** atts) = 0;
46   virtual void EndElement(XmlParseContext * pctx,
47                const char * name) = 0;
48   virtual void CharacterData(XmlParseContext * pctx,
49                const char * text, int len) = 0;
50   virtual void Error(XmlParseContext * pctx,
51                XML_Error errorCode) = 0;
52 };
53 
54 class XmlParser {
55 public:
56   static void ParseXml(XmlParseHandler * pxph, std::string text);
57 
58   explicit XmlParser(XmlParseHandler * pxph);
59   bool Parse(const char * data, size_t len, bool isFinal);
60   void Reset();
61   virtual ~XmlParser();
62 
63   // expat callbacks
64   void ExpatStartElement(const char * name, const char ** atts);
65   void ExpatEndElement(const char * name);
66   void ExpatCharacterData(const char * text, int len);
67   void ExpatXmlDecl(const char * ver, const char * enc, int standalone);
68 
69 private:
70 
71   class ParseContext : public XmlParseContext {
72   public:
73     ParseContext();
74     virtual ~ParseContext();
75     virtual QName ResolveQName(const char * qname, bool isAttr);
RaiseError(XML_Error err)76     virtual void RaiseError(XML_Error err) { if (!raised_) raised_ = err; }
77     virtual void GetPosition(unsigned long * line, unsigned long * column,
78                              unsigned long * byte_index);
RaisedError()79     XML_Error RaisedError() { return raised_; }
80     void Reset();
81 
82     void StartElement();
83     void EndElement();
84     void StartNamespace(const char * prefix, const char * ns);
85     void SetPosition(int line, int column, long byte_index);
86 
87   private:
88     XmlnsStack xmlnsstack_;
89     XML_Error raised_;
90     XML_Size line_number_;
91     XML_Size column_number_;
92     XML_Index byte_index_;
93   };
94 
95   ParseContext context_;
96   XML_Parser expat_;
97   XmlParseHandler * pxph_;
98   bool sentError_;
99 };
100 
101 }  // namespace jingle_xmpp
102 
103 #endif  // THIRD_PARTY_LIBJINGLE_XMPP_XMLLITE_XMLPARSER_H_
104