Emeric Vigier | 2f62582 | 2012-08-06 11:09:52 -0400 | [diff] [blame] | 1 | // Copyright (C) 2001-2005 Open Source Telecom Corporation. |
| 2 | // Copyright (C) 2006-2010 David Sugar, Tycho Softworks. |
| 3 | // |
| 4 | // This program is free software; you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation; either version 2 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU General Public License |
| 15 | // along with this program; if not, write to the Free Software |
| 16 | // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | // |
| 18 | // As a special exception, you may use this file as part of a free software |
| 19 | // library without restriction. Specifically, if other files instantiate |
| 20 | // templates or use macros or inline functions from this file, or you compile |
| 21 | // this file and link it with other files to produce an executable, this |
| 22 | // file does not by itself cause the resulting executable to be covered by |
| 23 | // the GNU General Public License. This exception does not however |
| 24 | // invalidate any other reasons why the executable file might be covered by |
| 25 | // the GNU General Public License. |
| 26 | // |
| 27 | // This exception applies only to the code released under the name GNU |
| 28 | // Common C++. If you copy code from other releases into a copy of GNU |
| 29 | // Common C++, as the General Public License permits, the exception does |
| 30 | // not apply to the code that you add in this way. To avoid misleading |
| 31 | // anyone as to the status of such modified files, you must delete |
| 32 | // this exception notice from them. |
| 33 | // |
| 34 | // If you write modifications of your own for GNU Common C++, it is your choice |
| 35 | // whether to permit this exception to apply to your modifications. |
| 36 | // If you do not wish that, delete this exception notice. |
| 37 | // |
| 38 | |
| 39 | /** |
| 40 | * @file xml.h |
| 41 | * @short XML streams abstraction and RPC services. |
| 42 | **/ |
| 43 | |
| 44 | #ifndef CCXX_XML_H_ |
| 45 | #define CCXX_XML_H_ |
| 46 | |
| 47 | #ifndef CCXX_MISSING_H_ |
| 48 | #include <cc++/missing.h> |
| 49 | #endif |
| 50 | |
| 51 | #ifndef CCXX_THREAD_H_ |
| 52 | #include <cc++/thread.h> |
| 53 | #endif |
| 54 | |
| 55 | #ifndef CCXX_SLOG_H_ |
| 56 | #include <cc++/slog.h> |
| 57 | #endif |
| 58 | |
| 59 | #ifdef CCXX_NAMESPACES |
| 60 | namespace ost { |
| 61 | #endif |
| 62 | |
| 63 | /** |
| 64 | * This class impliments a basic XML stream parser that can be used to |
| 65 | * examine an XML resource thru virtual I/O methods. This class must |
| 66 | * be derived into one that can impliment the physical I/O required to |
| 67 | * parse actual data. A mixer class using XMLStream and URLStream would |
| 68 | * seem a likely combination for this purpose. |
| 69 | * |
| 70 | * @author David Sugar <dyfet@ostel.com> |
| 71 | * @short XML Stream Parser (SAX) |
| 72 | */ |
| 73 | class __EXPORT XMLStream |
| 74 | { |
| 75 | private: |
| 76 | int ecount, dcount; |
| 77 | enum { TAG, CDATA, COMMENT, DTD, AMP, NONE} state; |
| 78 | char dbuf[8192]; |
| 79 | unsigned dp; |
| 80 | bool parseChunk(const char *chunk, size_t len); |
| 81 | void parseInit(void); |
| 82 | bool parseTag(void); |
| 83 | void putData(char c); |
| 84 | void clrData(void); |
| 85 | |
| 86 | protected: |
| 87 | virtual ~XMLStream(); |
| 88 | |
| 89 | public: |
| 90 | /** |
| 91 | * May perform an open operation on behalf of a parsed resource. |
| 92 | * In some cases, the parser may be merged with a class that |
| 93 | * already has performed some kind of open, and this method can |
| 94 | * then be ignored. |
| 95 | * |
| 96 | * @return true if open is successful. |
| 97 | * @param resource passed to Parse methods. |
| 98 | */ |
| 99 | virtual bool open(const char *resource); |
| 100 | |
| 101 | /** |
| 102 | * May perform a close operation of an i/o source when the parser |
| 103 | * has completed operation. |
| 104 | */ |
| 105 | virtual void close(void); |
| 106 | |
| 107 | /** |
| 108 | * Get error logging level. |
| 109 | * |
| 110 | * @return error logging level. |
| 111 | */ |
| 112 | virtual Slog::Level getLogging(void); |
| 113 | |
| 114 | /** |
| 115 | * Virtual to receive embedded comments in an XML document being |
| 116 | * parsed. |
| 117 | * |
| 118 | * @param text text comment extracted. |
| 119 | * @param len length of comment. |
| 120 | */ |
| 121 | virtual void comment(const unsigned char *text, size_t len); |
| 122 | |
| 123 | /** |
| 124 | * Read method to aquire data for the parser. |
| 125 | * |
| 126 | * @return number of bytes actually read. |
| 127 | * @param buffer to read data into. |
| 128 | * @param len number of bytes to read. |
| 129 | */ |
| 130 | virtual int read(unsigned char *buffer, size_t len) = 0; |
| 131 | |
| 132 | /** |
| 133 | * Virtual to receive character text extracted from the document |
| 134 | * in the current element. |
| 135 | * |
| 136 | * @param text received. |
| 137 | * @param len length of text received. |
| 138 | */ |
| 139 | virtual void characters(const unsigned char *text, size_t len) = 0; |
| 140 | |
| 141 | /** |
| 142 | * Identify start of document event. |
| 143 | */ |
| 144 | virtual void startDocument(void); |
| 145 | |
| 146 | /** |
| 147 | * Identify end of document event. |
| 148 | */ |
| 149 | virtual void endDocument(void); |
| 150 | |
| 151 | /** |
| 152 | * Identify start of an element in the document. |
| 153 | * |
| 154 | * @param name of element found. |
| 155 | * @param attr list of attributes extracted. |
| 156 | */ |
| 157 | virtual void startElement(const unsigned char *name, const unsigned char **attr) = 0; |
| 158 | |
| 159 | /** |
| 160 | * Identify end of an element in the document. |
| 161 | * |
| 162 | * @param name of element found. |
| 163 | */ |
| 164 | virtual void endElement(const unsigned char *name) = 0; |
| 165 | |
| 166 | /** |
| 167 | * Parse a resource as a stream thru the virtual read method. |
| 168 | * |
| 169 | * @return true if well formed document has been fully parsed. |
| 170 | * @param resource optional name of resource. |
| 171 | */ |
| 172 | bool parse(const char *resource = NULL); |
| 173 | }; |
| 174 | |
| 175 | /** |
| 176 | * This class impliments a core XMLRPC service without the underlying |
| 177 | * transports. It is meant to create and parse XMLRPC messages. To |
| 178 | * use for a fit purpose, one might combine it with URLStream, although |
| 179 | * this implimentation makes no requirement for http based transport. |
| 180 | * |
| 181 | * @author David Sugar <dyfet@ostel.com> |
| 182 | * @short XML-RPC service building class |
| 183 | */ |
| 184 | class __EXPORT XMLRPC : public XMLStream |
| 185 | { |
| 186 | private: |
| 187 | #ifdef HAVE_SSTREAM |
| 188 | std::stringstream strBuf; |
| 189 | #else |
| 190 | char *buffer; |
| 191 | std::strstream *oldStrBuf; |
| 192 | size_t bufSize; |
| 193 | #endif |
| 194 | bool structFlag; |
| 195 | bool reply, fault; |
| 196 | unsigned array; |
| 197 | |
| 198 | protected: |
| 199 | /** |
| 200 | * Used in a derived transport class to deliver the XMLRPC encoded |
| 201 | * request and return true if successful. The Parse method can |
| 202 | * then be used to decode the reply. |
| 203 | * |
| 204 | * @return true if successful. |
| 205 | * @param resource to send to (such as url). |
| 206 | * @param msg well formed XMLRPC request message. |
| 207 | */ |
| 208 | virtual bool post(const char *resource, const char *msg) = 0; |
| 209 | |
| 210 | /** |
| 211 | * Start member struct. |
| 212 | */ |
| 213 | void begStruct(void); |
| 214 | |
| 215 | public: |
| 216 | /** |
| 217 | * Construct XMLRPC workspace. |
| 218 | * |
| 219 | * @param bufferSize size of buffer when using old C++ |
| 220 | * strstreams. When the newer stringstream (\<sstream\>) is |
| 221 | * available, this parameter is silently ignored. |
| 222 | */ |
| 223 | XMLRPC(size_t bufferSize = 512); |
| 224 | |
| 225 | /** |
| 226 | * Destroy XMLRPC object. |
| 227 | */ |
| 228 | virtual ~XMLRPC(); |
| 229 | |
| 230 | /** |
| 231 | * Create an array. |
| 232 | */ |
| 233 | void begArray(void); |
| 234 | |
| 235 | /** |
| 236 | * end an array. |
| 237 | */ |
| 238 | void endArray(void); |
| 239 | |
| 240 | /** |
| 241 | * Create XMLRPC "method" call in buffer. |
| 242 | * |
| 243 | * @param method name of method being called. |
| 244 | */ |
| 245 | void invoke(const char *method); |
| 246 | |
| 247 | /** |
| 248 | * Create XMLRPC "reply" to a method call. |
| 249 | * |
| 250 | * @param fault set true for fault message. |
| 251 | */ |
| 252 | void response(bool fault); |
| 253 | |
| 254 | /** |
| 255 | * Add bool param to XMLRPC request. |
| 256 | * |
| 257 | * @param value to add. |
| 258 | */ |
| 259 | void addParam(bool value); |
| 260 | |
| 261 | /** |
| 262 | * Add bool member to a XMLRPC struct. |
| 263 | * |
| 264 | * @param name of member. |
| 265 | * @param value of member. |
| 266 | */ |
| 267 | void addMember(const char *name, bool value); |
| 268 | |
| 269 | /** |
| 270 | * Add an integer paramater to XMLRPC request. |
| 271 | * |
| 272 | * @param value to add. |
| 273 | */ |
| 274 | void addParam(long value); |
| 275 | |
| 276 | /** |
| 277 | * Add an integer member to XMLRPC struct. |
| 278 | * |
| 279 | * @param name of member. |
| 280 | * @param value of member. |
| 281 | */ |
| 282 | void addMember(const char *name, long value); |
| 283 | |
| 284 | /** |
| 285 | * Add a string paramater to XMLRPC request. |
| 286 | * |
| 287 | * @param string to add. |
| 288 | */ |
| 289 | void addParam(const char *string); |
| 290 | |
| 291 | /** |
| 292 | * Add a string member to XMLRPC struct. |
| 293 | * |
| 294 | * @param name of member. |
| 295 | * @param value of member. |
| 296 | */ |
| 297 | void addMember(const char *name, const char *value); |
| 298 | |
| 299 | /** |
| 300 | * Clear a struct. |
| 301 | */ |
| 302 | void endStruct(void); |
| 303 | |
| 304 | /** |
| 305 | * Complete buffer and send well formed XMLRPC request thru post. |
| 306 | * |
| 307 | * @return true if successful. |
| 308 | * @param resource to send to. |
| 309 | */ |
| 310 | bool send(const char *resource); |
| 311 | }; |
| 312 | |
| 313 | //#else |
| 314 | //#error "XML support has been selected, but libxml could not be found" |
| 315 | //#endif // ifdef HAVE_XML |
| 316 | |
| 317 | //#else |
| 318 | //#error "XML support is not available." |
| 319 | //#endif // ifdef COMMON_XML_PARSING |
| 320 | |
| 321 | #ifdef CCXX_NAMESPACES |
| 322 | } |
| 323 | #endif |
| 324 | |
| 325 | #endif |
| 326 | /** EMACS ** |
| 327 | * Local variables: |
| 328 | * mode: c++ |
| 329 | * c-basic-offset: 4 |
| 330 | * End: |
| 331 | */ |