util/qos/StatusQosFactory.cpp

Go to the documentation of this file.
00001 /*------------------------------------------------------------------------------
00002 Name:      StatusQosSaxFactory.cpp
00003 Project:   xmlBlaster.org
00004 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
00005 ------------------------------------------------------------------------------*/
00006 #include <util/MethodName.h>
00007 #include <util/qos/StatusQosFactory.h>
00008 #include <util/Global.h>
00009 
00010 namespace org { namespace xmlBlaster { namespace util { namespace qos {
00011 
00012 using namespace std;
00013 using namespace org::xmlBlaster::util;
00014 using namespace org::xmlBlaster::util::parser;
00015 
00016 void StatusQosFactory::prep()
00017 {
00018    inState_        = false;
00019    inSubscribe_    = false;
00020    inKey_          = false;
00021    inQos_          = false;
00022 }
00023 
00024 StatusQosFactory::StatusQosFactory(Global& global)
00025    : XmlHandlerBase(global),
00026      ME("StatusQosFactory"),
00027      global_(global),
00028      log_(global.getLog("org.xmlBlaster.util.qos")),
00029      statusQosData_(global)
00030 {
00031    prep();
00032 }
00033 
00034 void StatusQosFactory::startElement(const string &name, const AttributeMap& attrs)
00035 {
00036    //if (log_.call()) log_.call(ME, "startElement: " + getStartElementAsString(name, attrs));
00037 
00038    if (name.compare("qos") == 0) {
00039      statusQosData_ = StatusQosData(global_); // kind of reset
00040      prep();
00041      inQos_ = true;
00042      return;
00043    }
00044 
00045    if (name.compare("rcvTimestamp") == 0) {
00046       if (!inQos_) return;
00047       AttributeMap::const_iterator iter = attrs.begin();
00048       while (iter != attrs.end()) {
00049          if (((*iter).first).compare("nanos") == 0) {
00050             statusQosData_.setRcvTimestamp(getTimestampValue((*iter).second));
00051          }
00052          iter++;
00053       }
00054       return;
00055    }
00056  
00057    if (name.compare("state") == 0) {
00058       if (!inQos_) return;
00059       inState_ = true;
00060 
00061       AttributeMap::const_iterator iter = attrs.begin();
00062       string tmpName = (*iter).first;
00063       string tmpValue = (*iter).second;
00064       while (iter != attrs.end()) {
00065          if (tmpName.compare("id") == 0) {
00066             statusQosData_.setState(tmpValue);
00067          }
00068          if (tmpName.compare("info") == 0) {
00069             statusQosData_.setStateInfo(tmpValue);
00070          }
00071          iter++;
00072       }
00073       return;
00074    }
00075 
00076    if (name.compare(MethodName::SUBSCRIBE) == 0) {
00077       if (!inQos_) return;
00078       inSubscribe_ = true;
00079       AttributeMap::const_iterator iter = attrs.begin();
00080       while (iter != attrs.end()) {
00081          if (((*iter).first).compare("id") == 0) {
00082             statusQosData_.setSubscriptionId((*iter).second);
00083          }
00084          iter++;
00085       }
00086       return;
00087    }
00088 
00089    if (name.compare("key") == 0) {
00090       if (!inQos_) return;
00091       inKey_ = true;
00092       AttributeMap::const_iterator iter = attrs.begin();
00093       while (iter != attrs.end()) {
00094          if (((*iter).first).compare("oid") == 0) {
00095             statusQosData_.setKeyOid((*iter).second);
00096          }
00097          iter++;
00098       }
00099       return;
00100    }
00101 
00102    if (name.compare("persistent") == 0) {
00103       if (!inQos_) return;
00104       statusQosData_.setPersistent(true);
00105       return;
00106    }
00107 }
00108 
00109 void StatusQosFactory::endElement(const string &name)
00110 {
00111    if (name.compare("qos") == 0) {
00112       character_.erase();
00113       inQos_ = false;
00114       return;
00115    }
00116    if (name.compare("state") == 0) {
00117       inState_ = false;
00118       character_.erase();
00119       return;
00120    }
00121    if (name.compare(MethodName::SUBSCRIBE) == 0) {
00122       inSubscribe_ = false;
00123       character_.erase();
00124       return;
00125    }
00126    if (name.compare("key") == 0) {
00127       inKey_ = false;
00128       character_.erase();
00129       return;
00130    }
00131    character_.erase();
00132 
00133    if(name.compare("persistent") == 0) {
00134       inIsPersistent_ = false;
00135       statusQosData_.setPersistent(StringTrim::isTrueTrim(character_));
00136       character_.erase();
00137       return;
00138    }
00139 }
00140 
00141 StatusQosData StatusQosFactory::readObject(const string& qos)
00142 {
00143    init(qos);
00144    return statusQosData_;
00145 }
00146 
00147 }}}} // namespaces
00148 
00149 
00150 #ifdef _XMLBLASTER_CLASSTEST
00151 
00152 using namespace std;
00153 using namespace org::xmlBlaster::util::qos;
00154 
00155 int main(int args, char* argv[])
00156 {
00157     try
00158     {
00159        Global& glob = Global::getInstance();
00160        glob.initialize(args, argv);
00161 
00162        StatusQosData    data1(glob);
00163        StatusQosFactory factory(glob);
00164        string           qos   = data1.toXml();
00165        StatusQosData    data2 = factory.readObject(qos);
00166 
00167        cout << "data before parsing: " << data1.toXml() << endl;
00168        cout << "data after parsing : " << data2.toXml() << endl;
00169     }
00170     catch(...)  {
00171        cout << "Error occured";
00172        return 1;
00173     }
00174 
00175    return 0;
00176 }
00177 
00178 #endif
00179