XmlRpc::XmlRpcUtil Class Reference

#include <XmlRpcUtil.h>

List of all members.


Detailed Description

Utilities for XML parsing, encoding, and decoding and message handlers.

Static Public Member Functions

static std::string parseTag (const char *tag, std::string const &xml, int *offset)
 Returns contents between <tag> and </tag>, updates offset to char after </tag>.
static bool findTag (const char *tag, std::string const &xml, int *offset)
 Returns true if the tag is found and updates offset to the char after the tag.
static std::string getNextTag (std::string const &xml, int *offset)
 Returns the next tag and updates offset to the char after the tag, or empty string if the next non-whitespace character is not '<'.
static bool nextTagIs (const char *tag, std::string const &xml, int *offset)
 Returns true if the tag is found at the specified offset (modulo any whitespace) and updates offset to the char after the tag.
static std::string xmlEncode (const std::string &raw)
 Convert raw text to encoded xml.
static std::string xmlDecode (const std::string &encoded)
 Convert encoded xml to raw text.
static void log (int level, const char *fmt,...)
 Dump messages somewhere.
static void error (const char *fmt,...)
 Dump error messages somewhere.

Member Function Documentation

std::string XmlRpcUtil::parseTag ( const char *  tag,
std::string const &  xml,
int *  offset 
) [static]

Returns contents between <tag> and </tag>, updates offset to char after </tag>.

Referenced by XmlRpc::XmlRpcServer::parseRequest(), and XmlRpc::XmlRpcValue::structFromXml().

00099 {
00100   if (*offset >= int(xml.length())) return std::string();
00101   size_t istart = xml.find(tag, *offset);
00102   if (istart == std::string::npos) return std::string();
00103   istart += strlen(tag);
00104   std::string etag = "</";
00105   etag += tag + 1;
00106   size_t iend = xml.find(etag, istart);
00107   if (iend == std::string::npos) return std::string();
00108 
00109   *offset = int(iend + etag.length());
00110   return xml.substr(istart, iend-istart);
00111 }

bool XmlRpcUtil::findTag ( const char *  tag,
std::string const &  xml,
int *  offset 
) [static]

Returns true if the tag is found and updates offset to the char after the tag.

Referenced by XmlRpc::XmlRpcValue::fromXml(), XmlRpc::XmlRpcServer::parseRequest(), and XmlRpc::XmlRpcClient::parseResponse().

00117 {
00118   if (*offset >= int(xml.length())) return false;
00119   size_t istart = xml.find(tag, *offset);
00120   if (istart == std::string::npos)
00121     return false;
00122 
00123   *offset = int(istart + strlen(tag));
00124   return true;
00125 }

std::string XmlRpcUtil::getNextTag ( std::string const &  xml,
int *  offset 
) [static]

Returns the next tag and updates offset to the char after the tag, or empty string if the next non-whitespace character is not '<'.

Referenced by XmlRpc::XmlRpcValue::fromXml().

00154 {
00155   if (*offset >= int(xml.length())) return std::string();
00156 
00157   const char* cp = xml.c_str() + size_t(*offset);
00158   const char* startcp = cp;
00159   while (*cp && isspace(*cp))
00160     ++cp;
00161 
00162 
00163   if (*cp != '<') return std::string();
00164 
00165   // Tag includes the non-blank characters after <
00166   const char* start = cp++;
00167   while (*cp != '>' && *cp != 0 && ! isspace(*cp))
00168     ++cp;
00169 
00170   std::string s(start, cp-start+1);
00171 
00172   if (*cp != '>')   // Skip parameters and values
00173   {
00174     while (*cp != '>' && *cp != 0)
00175       ++cp;
00176 
00177     s[s.length()-1] = *cp;
00178   }
00179 
00180   *offset += int(cp - startcp + 1);
00181   return s;
00182 }

bool XmlRpcUtil::nextTagIs ( const char *  tag,
std::string const &  xml,
int *  offset 
) [static]

Returns true if the tag is found at the specified offset (modulo any whitespace) and updates offset to the char after the tag.

Referenced by XmlRpc::XmlRpcValue::arrayFromXml(), XmlRpc::XmlRpcValue::fromXml(), XmlRpc::XmlRpcServer::parseRequest(), XmlRpc::XmlRpcClient::parseResponse(), and XmlRpc::XmlRpcValue::structFromXml().

00132 {
00133   if (*offset >= int(xml.length())) return false;
00134   const char* cp = xml.c_str() + *offset;
00135   int nc = 0;
00136   while (*cp && isspace(*cp)) {
00137     ++cp;
00138     ++nc;
00139   }
00140 
00141   int len = int(strlen(tag));
00142   if  (*cp && (strncmp(cp, tag, len) == 0)) {
00143     *offset += nc + len;
00144     return true;
00145   }
00146   return false;
00147 }

std::string XmlRpcUtil::xmlEncode ( const std::string &  raw  )  [static]

Convert raw text to encoded xml.

Referenced by XmlRpc::XmlRpcValue::stringToXml(), and XmlRpc::XmlRpcValue::structToXml().

00234 {
00235   std::string::size_type iRep = raw.find_first_of(rawEntity);
00236   if (iRep == std::string::npos)
00237     return raw;
00238 
00239   std::string encoded(raw, 0, iRep);
00240   std::string::size_type iSize = raw.size();
00241 
00242   while (iRep != iSize) {
00243     int iEntity;
00244     for (iEntity=0; rawEntity[iEntity] != 0; ++iEntity)
00245       if (raw[iRep] == rawEntity[iEntity])
00246       {
00247         encoded += AMP;
00248         encoded += xmlEntity[iEntity];
00249         break;
00250       }
00251     if (rawEntity[iEntity] == 0)
00252       encoded += raw[iRep];
00253     ++iRep;
00254   }
00255   return encoded;
00256 }

std::string XmlRpcUtil::xmlDecode ( const std::string &  encoded  )  [static]

Convert encoded xml to raw text.

Referenced by XmlRpc::XmlRpcValue::stringFromXml().

00197 {
00198   std::string::size_type iAmp = encoded.find(AMP);
00199   if (iAmp == std::string::npos)
00200     return encoded;
00201 
00202   std::string decoded(encoded, 0, iAmp);
00203   std::string::size_type iSize = encoded.size();
00204   decoded.reserve(iSize);
00205 
00206   const char* ens = encoded.c_str();
00207   while (iAmp != iSize) {
00208     if (encoded[iAmp] == AMP && iAmp+1 < iSize) {
00209       int iEntity;
00210       for (iEntity=0; xmlEntity[iEntity] != 0; ++iEntity)
00211         //if (encoded.compare(iAmp+1, xmlEntLen[iEntity], xmlEntity[iEntity]) == 0)
00212         if (strncmp(ens+iAmp+1, xmlEntity[iEntity], xmlEntLen[iEntity]) == 0)
00213         {
00214           decoded += rawEntity[iEntity];
00215           iAmp += xmlEntLen[iEntity]+1;
00216           break;
00217         }
00218       if (xmlEntity[iEntity] == 0)    // unrecognized sequence
00219         decoded += encoded[iAmp++];
00220 
00221     } else {
00222       decoded += encoded[iAmp++];
00223     }
00224   }
00225     
00226   return decoded;
00227 }

void XmlRpcUtil::log ( int  level,
const char *  fmt,
  ... 
) [static]

void XmlRpcUtil::error ( const char *  fmt,
  ... 
) [static]


The documentation for this class was generated from the following files:

Generated on Fri Sep 19 13:05:09 2008 for ITM OverSim by  doxygen 1.5.5