ulxr_wbxmlparse.cpp

Go to the documentation of this file.
00001 /**************************************************************************
00002                   ulxr_wbxmlparse.cpp  -  parse wbxml files
00003                              -------------------
00004     begin                : Fri Jan 09 2004
00005     copyright            : (C) 2002-2007 by Ewald Arnold
00006     email                : ulxmlrpcpp@ewald-arnold.de
00007 
00008     $Id: ulxr_wbxmlparse.cpp 940 2006-12-30 18:22:05Z ewald-arnold $
00009 
00010  ***************************************************************************/
00011 
00012 /**************************************************************************
00013  *
00014  * This program is free software; you can redistribute it and/or modify
00015  * it under the terms of the GNU Lesser General Public License as
00016  * published by the Free Software Foundation; either version 2 of the License,
00017  * or (at your option) any later version.
00018  *
00019  * This program is distributed in the hope that it will be useful,
00020  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00022  * GNU General Public License for more details.
00023  *
00024  * You should have received a copy of the GNU Lesser General Public License
00025  * along with this program; if not, write to the Free Software
00026  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00027  *
00028  ***************************************************************************/
00029 
00030 //#define ULXR_DEBUG_OUTPUT
00031 
00032 #define ULXR_NEED_EXPORTS
00033 #include <ulxmlrpcpp/ulxmlrpcpp.h>
00034 
00035 #include <ulxmlrpcpp/ulxr_wbxmlparse.h>
00036 #include <ulxmlrpcpp/ulxr_except.h>
00037 #include <ulxmlrpcpp/ulxr_htmlform_handler.h>
00038 
00039 
00040 namespace ulxr {
00041 
00042 
00043 #define MIB_Enum_UTF8      (char) 106
00044 #define MIB_Enum_LATIN1    (char) 4
00045 
00046 //#define MIB_Enum_UTF16     ULXR_MB_INT_14bit(1015)
00047 //#define MIB_Enum_UTF16LE   ULXR_MB_INT_14bit(1014)
00048 //#define MIB_Enum_UTF16BE   ULXR_MB_INT_14bit(1013)
00049 
00050 
00051 ULXR_API_IMPL(const char)
00052   WbXmlParser::wbxml_START_SEQ_STR[]      = {
00053                                               0x01,  // WBXML 1.1
00054                                               0x01,  // unknown PubId
00055 #ifdef ULXR_UNICODE
00056                                               MIB_Enum_UTF8,
00057 #else
00058                                               MIB_Enum_LATIN1,
00059 #endif
00060                                               0x00   // empty string table
00061                                             };
00062 
00063 const unsigned WbXmlParser::wbxml_START_SEQ_LEN = 4;
00064 
00065 enum WbStates
00066 {
00067   stNone,              // at start
00068   stVersion,           // reading version
00069   stPubId,             // reading public identifier
00070   stCharset,           // reading charset
00071   stStrTableLen,       // reading string table length byte
00072   stBody,              // reading wbxml body
00073 
00074   stStr_I              // inline string
00075 };
00076 
00077 
00079 
00080 
00081 ULXR_API_IMPL0 WbXmlParser::WbXmlState::WbXmlState(unsigned in_state)
00082 {
00083   current_str = "";
00084   state = in_state;
00085 }
00086 
00087 
00089 
00090 
00091 ULXR_API_IMPL0 WbXmlParser::WbXmlParser()
00092   : XmlParserBase()
00093 {
00094   init();
00095 }
00096 
00097 
00098 ULXR_API_IMPL(void) WbXmlParser::init()
00099 {
00100   lastErrorCode = 0;
00101   tag_count = 0;
00102   wbxmlstates.push(stNone);
00103 }
00104 
00105 
00106 ULXR_API_IMPL0 WbXmlParser::~WbXmlParser()
00107 {
00108 }
00109 
00110 
00111 ULXR_API_IMPL(unsigned) WbXmlParser::getErrorCode() const
00112 {
00113   return lastErrorCode;
00114 }
00115 
00116 
00117 
00118 ULXR_API_IMPL(int) WbXmlParser::parse(const char* buffer, int len, int isFinal)
00119 {
00120   while(len > 0)
00121   {
00122     unsigned c = *buffer;
00123     ++buffer;
00124     --len;
00125     ULXR_TRACE((ULXR_PCHAR("wb parse: "))+HtmlFormHandler::makeHexNumber(c));
00126     ULXR_TRACE((ULXR_PCHAR("state: "))+HtmlFormHandler::makeNumber(wbxmlstates.top().state));
00127 
00128     switch(wbxmlstates.top().state)
00129     {
00130       case stNone:
00131         wb_version = c;
00132         ULXR_TRACE((ULXR_PCHAR("version: "))+HtmlFormHandler::makeHexNumber(c));
00133         wbxmlstates.top().state = stPubId;
00134       break;
00135 
00136       case stPubId:
00137         wb_pubId = c; // always unknown pub id
00138         ULXR_TRACE((ULXR_PCHAR("pub id: "))+HtmlFormHandler::makeHexNumber(c));
00139         wbxmlstates.top().state = stCharset;
00140       break;
00141 
00142       case stCharset:
00143         wb_charset = c; // always single byte mib enum
00144         ULXR_TRACE(ULXR_PCHAR("char set: ")+HtmlFormHandler::makeHexNumber(c));
00145         wbxmlstates.top().state = stStrTableLen;
00146       break;
00147 
00148       case stStrTableLen: // len always 0
00149         ULXR_TRACE(ULXR_PCHAR("str table len: "));
00150         wbxmlstates.top().state = stBody;
00151       break;
00152 
00153       case stStr_I:
00154         if (c == 0)
00155         {
00156           charData(wbxmlstates.top().current_str);
00157           wbxmlstates.pop();
00158         }
00159         else
00160           wbxmlstates.top().current_str += (char) c;
00161       break;
00162 
00163       case stBody:
00164         switch (c)
00165         {
00166           case wbxml_STR_I:
00167             ULXR_TRACE(ULXR_PCHAR("start inline string"));
00168             wbxmlstates.push(stStr_I);
00169           break;
00170 
00171           case wbxml_END:
00172             ULXR_TRACE(ULXR_PCHAR("end element"));
00173             endElement();
00174             tag_count--;
00175           break;
00176 
00177           default:
00178           {
00179             ULXR_TRACE(ULXR_PCHAR("start element: ")+HtmlFormHandler::makeHexNumber(c));
00180             tag_count++;
00181             Attributes atts;
00182             startElement(c, atts);
00183           }
00184         }
00185       break;
00186     }
00187   }
00188 
00189   if (isFinal && tag_count != 0)
00190     throw XmlException(NotWellformedError,
00191                       ulxr_i18n(ULXR_PCHAR("Problem while parsing wbxml structure")),
00192                       getCurrentLineNumber(),
00193                       ulxr_i18n(ULXR_PCHAR("Opening and closing tags are not equal")));
00194 
00195   return true;
00196 }
00197 
00198 
00199 ULXR_API_IMPL(CppString) WbXmlParser::getErrorString(unsigned /*code*/) const
00200 {
00201   return ULXR_PCHAR("");  // TODO:
00202 }
00203 
00204 
00205 ULXR_API_IMPL(int) WbXmlParser::getCurrentLineNumber() const
00206 {
00207   return tag_count;
00208 }
00209 
00210 
00211 ULXR_API_IMPL(int) WbXmlParser::mapToFaultCode(int xpatcode) const
00212 {
00213   return xpatcode;
00214 }
00215 
00216 
00217 ULXR_API_IMPL(void) WbXmlParser::charData(const std::string &s)
00218 {
00219   ULXR_TRACE(ULXR_PCHAR("WbXmlParser::charData()")
00220              << ULXR_PCHAR("\n  s: >>")
00221              << ULXR_GET_STRING(s)
00222              << ULXR_PCHAR("<<")
00223             );
00224   states.top()->appendCharData(s.data(), s.length());
00225   ULXR_TRACE(ULXR_PCHAR("WbXmlParser::charData() finished"));
00226 }
00227 
00228 
00229 ULXR_API_IMPL(void)
00230   WbXmlParser::startElement(unsigned token, const Attributes &attr)
00231 {
00232   if (!testStartElement(token, attr))
00233     WbXmlParser::testStartElement(token, attr);
00234 }
00235 
00236 
00237 ULXR_API_IMPL(bool) WbXmlParser::testStartElement(unsigned token, const Attributes &/*attr*/)
00238 {
00239   throw XmlException(NotWellformedError,
00240                      ulxr_i18n(ULXR_PCHAR("Problem while parsing wbxml structure")),
00241                      getCurrentLineNumber(),
00242                      ulxr_i18n(ULXR_PCHAR("unexpected opening token: "))+HtmlFormHandler::makeHexNumber(token));
00243 }
00244 
00245 
00246 ULXR_API_IMPL(bool) WbXmlParser::testEndElement()
00247 {
00248   throw XmlException(NotWellformedError,
00249                      ulxr_i18n(ULXR_PCHAR("Problem while parsing wbxml structure")),
00250                      getCurrentLineNumber(),
00251                      ulxr_i18n(ULXR_PCHAR("unexpected closing token")));
00252 }
00253 
00254 
00255 }  // namespace ulxr
00256 
00257 
00258 
00259 
00260 

Generated on Sun Aug 19 20:08:58 2007 for ulxmlrpcpp by  doxygen 1.5.1