gloox  1.1-svn
parser.h
1 /*
2  Copyright (c) 2004-2009 by Jakob Schroeter <js@camaya.net>
3  This file is part of the gloox library. http://camaya.net/gloox
4 
5  This software is distributed under a license. The full license
6  agreement can be found in the file LICENSE in this distribution.
7  This software may not be copied, modified, sold or distributed
8  other than expressed in the named license agreement.
9 
10  This software is distributed without any warranty.
11 */
12 
13 
14 
15 #ifndef PARSER_H__
16 #define PARSER_H__
17 
18 #include "gloox.h"
19 #include "taghandler.h"
20 #include "tag.h"
21 
22 #include <string>
23 
24 namespace gloox
25 {
26 
27 
34  class GLOOX_API Parser
35  {
36  public:
43  Parser( TagHandler* ph, bool deleteRoot = true );
44 
48  virtual ~Parser();
49 
56  int feed( std::string& data );
57 
63  void cleanup( bool deleteRoot = true );
64 
75  static Tag* parse( std::string& data );
76 
77  private:
78  enum ParserInternalState
79  {
80  Initial,
81  InterTag,
82  TagOpening,
83  TagOpeningSlash,
84  TagOpeningLt,
85  TagInside,
86  TagNameCollect,
87  TagNameComplete,
88  TagNameAlmostComplete,
89  TagAttribute,
90  TagAttributeComplete,
91  TagAttributeEqual,
92  TagClosing,
93  TagClosingSlash,
94  TagValueApos,
95  TagAttributeValue,
96  TagPreamble,
97  TagCDATASection,
98  XMLComment
99  };
100 
101  enum ForwardScanState
102  {
103  ForwardFound,
104  ForwardNotFound,
105  ForwardInsufficientSize
106  };
107 
108  enum DecodeState
109  {
110  DecodeValid,
111  DecodeInvalid,
112  DecodeInsufficient
113  };
114 
119  enum ParserInternalReturn
120  {
121  ParseIncomplete = -1,
122  ParseOK = -2
123  };
124 
125  void addTag();
126  void addAttribute();
127  void addCData();
128  bool closeTag();
129  bool isWhitespace( unsigned char c );
130  bool isValid( unsigned char c );
131  void streamEvent( Tag* tag );
132  ForwardScanState forwardScan( std::string::size_type& pos, const std::string& data,
133  const std::string& needle );
134  DecodeState decode( std::string::size_type& pos, const std::string& data );
135 
136  TagHandler* m_tagHandler;
137  Tag* m_current;
138  Tag* m_root;
139  StringMap* m_xmlnss;
140 
141  ParserInternalState m_state;
142  Tag::AttributeList m_attribs;
143  std::string m_tag;
144  std::string m_cdata;
145  std::string m_attrib;
146  std::string m_value;
147  std::string m_xmlns;
148  std::string m_tagPrefix;
149  std::string m_attribPrefix;
150  std::string m_backBuffer;
151  int m_preamble;
152  int m_return; // used by static Parser::parse()
153  bool m_quote;
154  bool m_haveTagPrefix;
155  bool m_haveAttribPrefix;
156  bool m_attribIsXmlns;
157  bool m_deleteRoot;
158  bool m_nullRoot; // used by static Parser::parse()
159 
160  };
161 
162 }
163 
164 #endif // PARSER_H__