util/qos/ClientProperty.cpp

Go to the documentation of this file.
00001 /*------------------------------------------------------------------------------
00002 Name:      ClientProperty.cpp
00003 Project:   xmlBlaster.org
00004 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
00005 Comment:   Handling one client property of QosData
00006 ------------------------------------------------------------------------------*/
00007 #include <typeinfo>
00008 #include <vector>
00009 //#include <algorithm>
00010 #include <sstream>
00011 #include <iostream>
00012 #include <util/qos/ClientProperty.h>
00013 #include <util/Base64.h>
00014 #include <util/Constants.h>
00015 #include <util/lexical_cast.h>
00016 
00017 using namespace org::xmlBlaster::util::qos;
00018 using namespace org::xmlBlaster::util;
00019 using namespace std;
00020 
00021 namespace org { namespace xmlBlaster { namespace util { namespace qos {
00022 
00023 ClientProperty::ClientProperty(bool /*dummy*/,
00024                   const std::string& name,
00025                   const std::string& type,
00026                   const std::string& encoding,
00027                   const std::string& charset
00028                   )
00029    : name_(name),
00030      value_(""),
00031      encoding_(encoding),
00032      type_(type),
00033      charset_(charset)
00034 {
00035 }
00036 
00037 ClientProperty::ClientProperty(const std::string& name,
00038                      const char *value,
00039                      const std::string& type)
00040    : name_(name),
00041      value_(value),
00042      encoding_(""),
00043      type_(type),
00044      charset_("")
00045 {
00046    if (needsEncoding()) {
00047 //#     if defined(__sun)
00048 #     if defined(__SUNPRO_CC)
00049       std::vector<unsigned char> vec; // TODO: Better workaround for SunOS CC Sun C++ 5.5 2003/03/12
00050       int len = strlen(value);
00051       for (int i=0; i<len; i++) {
00052          vec.push_back(value[i]);
00053       }
00054 #     else
00055       std::vector<unsigned char> vec(value, value+strlen(value));
00056 #     endif
00057       encoding_ = Constants::ENCODING_BASE64;
00058       value_ = Base64::Encode(vec);
00059    }
00060    else
00061       value_ = value;
00062 }
00063 
00064 ClientProperty::ClientProperty(const std::string& name,
00065                      const std::vector<unsigned char>& value,
00066                      const std::string& type)
00067    : name_(name),
00068      value_(""),
00069      encoding_(""),
00070      type_(type),
00071      charset_("")
00072 {
00073    if (type_ == "") {
00074       type_ = Constants::TYPE_BLOB;
00075    }
00076    encoding_ = Constants::ENCODING_BASE64;
00077    value_ = Base64::Encode(value);
00078 }
00079 /*
00080 ClientProperty::ClientProperty(const std::string& name,
00081                   const std::string& value,
00082                   const std::string& type,
00083                   const std::string& encoding)
00084    : name_(name),
00085      value_(value),
00086      encoding_(encoding),
00087      type_(type)
00088 {
00089 }
00090 */
00091 bool ClientProperty::needsEncoding() const {
00092    if (type_ == Constants::TYPE_BLOB || encoding_ == Constants::ENCODING_BASE64)
00093       return true;
00094    else if (
00095         value_.find("<") != std::string::npos ||
00096         value_.find("&") != std::string::npos ||
00097         value_.find("]]>") != std::string::npos
00098       ) {
00099       return true;
00100    }
00101    return false;
00102 }
00103 
00104 const std::string& ClientProperty::getName() const {
00105    return name_;
00106 }
00107 
00108 std::string ClientProperty::getType() const {
00109    return type_;
00110 }
00111 
00112 void ClientProperty::setCharset(const string& charset) {
00113    charset_ = charset;
00114 }
00115 
00116 std::string ClientProperty::getEncoding() const {
00117    return encoding_;
00118 }
00119 
00120 std::string ClientProperty::getCharset() const {
00121    return charset_;
00122 }
00123 
00124 bool ClientProperty::isBase64() const {
00125    return encoding_ == Constants::ENCODING_BASE64;
00126 }
00127 
00128 std::string ClientProperty::getValueRaw() const {
00129    return value_;
00130 }
00131 
00132 std::string ClientProperty::getStringValue() const {
00133    if (value_ == "") return "";
00134    if (Constants::ENCODING_BASE64 == encoding_) {
00135       std::vector<unsigned char> vec = Base64::Decode(value_);
00136       std::string str;
00137       str.reserve(vec.size());
00138 #     if defined(__sun)
00139          std::vector<unsigned char>::const_iterator it;
00140          for(it = vec.begin(); it != vec.end(); ++it) {
00141             unsigned char c = (*it);
00142             str += c;
00143          }
00144 #     else
00145          str.assign(vec.begin(),vec.end());
00146 #     endif
00147       return str;
00148    }
00149    return value_;
00150 }
00151 
00152 std::vector<unsigned char> ClientProperty::getValue() const {
00153    if (value_ == "") std::vector<unsigned char>();
00154    if (Constants::ENCODING_BASE64 == encoding_) {
00155       return Base64::Decode(value_);
00156    }
00157    std::vector<unsigned char>vec;
00158    vec.reserve(value_.size());
00159    copy(value_.begin(), value_.end(), vec.begin());
00160    return vec;
00161 }
00162 
00163 void ClientProperty::setValue(const string& value) {
00164    value_ = value;
00165    if (needsEncoding()) {
00166       std::vector<unsigned char>vec;
00167       vec.reserve(value.size());
00168       copy(value.begin(), value.end(), vec.begin());
00169       value_ = Base64::Encode(vec);
00170    }
00171 }
00172 
00173 void ClientProperty::setValueRaw(const string& value) {
00174    value_ = value;
00175 }
00176 
00177 std::string ClientProperty::toXml(std::string extraOffset, bool clearText, std::string tagName) const {
00178    std::string sb = std::string();
00179    sb.reserve(256);
00180    std::string offset = Constants::OFFSET + extraOffset;
00181 
00182    sb += offset + "<" + tagName;
00183    if (getName() != "") {
00184       sb += " name='" + getName() + "'";
00185    }
00186    if (getType() != "") {
00187       sb += " type='" + getType() + "'";
00188    }
00189    if (getEncoding() != "") {
00190       sb += " encoding='" + getEncoding() + "'";
00191    }
00192    if (getCharset() != "") {
00193       sb += " charset='" + getCharset() + "'";
00194    }
00195 
00196    std::string val = getValueRaw();
00197    if (val == "")
00198       sb += "/>";
00199    else {
00200       //if (encoding_ == Constants.ENCODING_NONE &&
00201       //    (
00202       //     val.find("%") != std::string::npos ||
00203       //     val.find(">") != std::string::npos ||
00204       //     val.find("&") != std::string::npos
00205       //    )
00206       //   sb += "><![CDATA�[" + val + "]]></clientProperty>";
00207       //else
00208       sb += ">" + (clearText?getStringValue():val) + "</" + tagName + ">";
00209    }
00210 
00211    return sb;
00212 }
00213 
00214 }}}}
00215 
00216 //g++ -o ClientProperty -Wall -g -DCLIENTPROPERTY_MAIN ClientProperty.cpp ../Base64.cpp ../Constants.cpp -I ~/xmlBlaster/src/c++
00217 #ifdef CLIENTPROPERTY_MAIN
00218 # include <iostream>
00219 
00220 namespace org { namespace xmlBlaster { namespace util {
00221    template<> std::string lexical_cast(std::string arg) { return arg; }
00222 }}}
00223 
00224 int main(int argc, char **argv) {
00225    try {
00226       {
00227          ClientProperty cp("key", string("string"));
00228          char *p = setlocale(LC_CTYPE, ""); // Returns "en_US.UTF-8" on Linux
00229          cp.setCharset(p);
00230          
00231          cout << "name=" << cp.getName() 
00232               << ", valueB64=" << cp.getValueRaw()
00233               << ", value=" << cp.getStringValue()
00234               << ", type=" << cp.getType()
00235               << ", charset=" << cp.getCharset()
00236               << ", isBase64=" << cp.isBase64()
00237               << cp.toXml("")
00238               << endl << endl;
00239       }
00240       {
00241          vector<unsigned char> v;
00242          v.push_back('H');
00243          v.push_back('a');
00244          v.push_back('l');
00245          v.push_back('l');
00246          v.push_back('o');
00247          ClientProperty cp("key", v);
00248          cp.setCharset("windows-1252");
00249          cout << "name=" << cp.getName() 
00250               << ", valueB64=" << cp.getValueRaw()
00251               << ", value=" << cp.getStringValue()
00252               << ", type=" << cp.getType()
00253               << ", encoding=" << cp.getEncoding()
00254               << ", charset=" << cp.getCharset()
00255               << ", isBase64=" << cp.isBase64()
00256               << cp.toXml("")
00257               << endl;
00258          {
00259             std::vector<unsigned char> ret = cp.getValue();
00260             std::string str;
00261             str.assign(v.begin(),v.end());
00262             cout << "NEW=" << str << endl;
00263          }
00264       }
00265    }
00266    catch(bad_cast b) {
00267       cout << "EXCEPTION: " << b.what() << endl;
00268    }
00269    return 0;
00270 }
00271 #endif