1 /***************************************************************************
2                     ulxr_xmlparse_base.h  -  parse xml files
3                              -------------------
4     begin                : Fri Jan 09 2004
5     copyright            : (C) 2002-2007 by Ewald Arnold
6     email                : ulxmlrpcpp@ewald-arnold.de
7 
8     $Id: ulxr_xmlparse_base.h 940 2006-12-30 18:22:05Z ewald-arnold $
9 
10  ***************************************************************************/
11 
12 /**************************************************************************
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU Lesser General Public License as
16  * published by the Free Software Foundation; either version 2 of the License,
17  * or (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27  *
28  ***************************************************************************/
29 
30 #ifndef ULXR_XMLPARSE_BASE
31 #define ULXR_XMLPARSE_BASE
32 
33 #ifdef HAVE_XMLPARSE_H
34 #include <xmlparse.h>
35 #else
36 #include <expat.h>
37 #undef  XMLPARSEAPI
38 #define XMLPARSEAPI
39 #endif
40 
41 #include <ulxmlrpcpp/ulxmlrpcpp.h>  // always first header
42 
43 #include <stack>
44 
45 
46 namespace ulxr {
47 
48 
49 /** Base class for XML parsing.
50   * @ingroup grp_ulxr_parser
51   */
52 class ULXR_API_DECL0 XmlParserBase
53 {
54  public:
55 
56  /** Constructs a parser.
57    */
58    XmlParserBase();
59 
60  /** Destroys the parser.
61    * The derived class is responsible for cleaning up stack<ParserState*>.
62    */
63    virtual ~XmlParserBase();
64 
65  /** Tests if parsing has completed.
66    * Completed means that all opening tags have been correctly closed.
67    * @return true if completed
68    */
69    bool isComplete() const;
70 
71  /** Sets the complete state.
72    * @param  comp true if file read
73    */
74    void setComplete(bool comp);
75 
76  /** Parse a pice of xml data.
77    * @param buffer   pointer start of next data chunk
78    * @param len      len of this chunk
79    * @param isFinal  true: last call to parser
80    * @return error condition, 0 = ok
81    */
82    virtual int parse(const char* buffer, int len, int isFinal) = 0;
83 
84   /** Gets the code for the current error.
85    * @return error code
86    */
87    virtual unsigned getErrorCode() const = 0;
88 
89  /** Gets the description for an error code
90    * @param code  error code
91    * @return  pointer to description
92    */
93    virtual CppString getErrorString(unsigned code) const = 0;
94 
95  /** Gets the line number in the xml data.
96    * Because the binary data has nothing like a line number, the occurence number
97    * of the previous tag is returned.
98    * @return  line number
99    */
100    virtual int getCurrentLineNumber() const = 0;
101 
102  /** Maps expat error codes to xml-rpc error codes.
103    * @param  xpatcode   error code from expat
104    * @return  the according xml-rpc error
105    */
106    virtual int mapToFaultCode(int xpatcode) const = 0;
107 
108    enum State
109    {
110      eNone,               //!<  state after start
111      eUnknown,            //!<  unknwon state when an error occured
112      eXmlParserLast       //!<  used to chain next parser class
113    };
114 
115 /** Helper class to represent the data of the current parsing step.
116   */
117   class ULXR_API_DECL0 ParserState
118   {
119    public:
120 
121    /** Constructs a ParserState.
122      * @param  st  the actual ParserState
123      */
124      ParserState (unsigned st);
125 
126    /** Destroys the ParserState.
127      */
128      virtual ~ParserState();
129 
130    /** Gets the ParserState of this ParserState
131      * @return the actual ParserState
132      */
133      unsigned getParserState() const;
134 
135    /** Gets the privious ParserState
136      * @return the previous ParserState
137      */
138      unsigned getPrevParserState() const;
139 
140    /** Sets the privious ParserState
141      * @param  prev the previous ParserState
142      */
143      void setPrevParserState(unsigned prev);
144 
145    /** Gets the name of the ParserState.
146      * Useful only for debugging.
147      * @return the name of actual ParserState
148      */
149      virtual CppString getStateName() const;
150 
151    /** Appends some characters of the ParserState.
152      * This is a part of the data of an xml rpc element.
153      * @param  s   the current chunk of text
154      * @param  len valid len.
155      */
156      void appendCharData(const XML_Char *s, int len);
157 
158    /** Appends some characters of the ParserState.
159      * This is a part of the data of an xml rpc element.
160      * @param  s   the current chunk of text
161      */
162      void appendCharData(const std::string &s);
163 
164    /** Gets the characters of the ParserState.
165      * @return  the data element
166      */
167      CppString getCharData() const;
168 
169    private:
170 
171      ParserState(const ParserState&); // forbid this
172      ParserState& operator= (const ParserState&);
173 
174      CppString  cdata;
175      unsigned   state;
176      unsigned   prevstate;
177   };
178 
179  protected:
180 
181  /** Removes all states from the state stack.
182    */
183    void clearStates();
184 
185  protected:
186 
187    std::stack<ParserState*>  states;
188 
189  private:
190 
191    bool complete;
192 };
193 
194 
195 }  // namespace ulxr
196 
197 
198 #endif // ULXR_XMLPARSE_BASE
199