1 /*------------------------------------------------------------------------------
2 Name: StatusQosSaxFactory.cpp
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 ------------------------------------------------------------------------------*/
6 #include <util/MethodName.h>
7 #include <util/qos/StatusQosFactory.h>
8 #include <util/Global.h>
9
10 namespace org { namespace xmlBlaster { namespace util { namespace qos {
11
12 using namespace std;
13 using namespace org::xmlBlaster::util;
14 using namespace org::xmlBlaster::util::parser;
15
16 void StatusQosFactory::prep()
17 {
18 inState_ = false;
19 inSubscribe_ = false;
20 inKey_ = false;
21 inQos_ = false;
22 }
23
24 StatusQosFactory::StatusQosFactory(Global& global)
25 : XmlHandlerBase(global),
26 ME("StatusQosFactory"),
27 global_(global),
28 log_(global.getLog("org.xmlBlaster.util.qos")),
29 statusQosData_(global)
30 {
31 prep();
32 }
33
34 void StatusQosFactory::startElement(const string &name, const AttributeMap& attrs)
35 {
36 //if (log_.call()) log_.call(ME, "startElement: " + getStartElementAsString(name, attrs));
37
38 if (name.compare("qos") == 0) {
39 statusQosData_ = StatusQosData(global_); // kind of reset
40 prep();
41 inQos_ = true;
42 return;
43 }
44
45 if (name.compare("rcvTimestamp") == 0) {
46 if (!inQos_) return;
47 AttributeMap::const_iterator iter = attrs.begin();
48 while (iter != attrs.end()) {
49 if (((*iter).first).compare("nanos") == 0) {
50 statusQosData_.setRcvTimestamp(getTimestampValue((*iter).second));
51 }
52 iter++;
53 }
54 return;
55 }
56
57 if (name.compare("state") == 0) {
58 if (!inQos_) return;
59 inState_ = true;
60
61 AttributeMap::const_iterator iter = attrs.begin();
62 string tmpName = (*iter).first;
63 string tmpValue = (*iter).second;
64 while (iter != attrs.end()) {
65 if (tmpName.compare("id") == 0) {
66 statusQosData_.setState(tmpValue);
67 }
68 if (tmpName.compare("info") == 0) {
69 statusQosData_.setStateInfo(tmpValue);
70 }
71 iter++;
72 }
73 return;
74 }
75
76 if (name.compare(MethodName::SUBSCRIBE) == 0) {
77 if (!inQos_) return;
78 inSubscribe_ = true;
79 AttributeMap::const_iterator iter = attrs.begin();
80 while (iter != attrs.end()) {
81 if (((*iter).first).compare("id") == 0) {
82 statusQosData_.setSubscriptionId((*iter).second);
83 }
84 iter++;
85 }
86 return;
87 }
88
89 if (name.compare("key") == 0) {
90 if (!inQos_) return;
91 inKey_ = true;
92 AttributeMap::const_iterator iter = attrs.begin();
93 while (iter != attrs.end()) {
94 if (((*iter).first).compare("oid") == 0) {
95 statusQosData_.setKeyOid((*iter).second);
96 }
97 iter++;
98 }
99 return;
100 }
101
102 if (name.compare("persistent") == 0) {
103 if (!inQos_) return;
104 statusQosData_.setPersistent(true);
105 return;
106 }
107 }
108
109 void StatusQosFactory::endElement(const string &name)
110 {
111 if (name.compare("qos") == 0) {
112 character_.erase();
113 inQos_ = false;
114 return;
115 }
116 if (name.compare("state") == 0) {
117 inState_ = false;
118 character_.erase();
119 return;
120 }
121 if (name.compare(MethodName::SUBSCRIBE) == 0) {
122 inSubscribe_ = false;
123 character_.erase();
124 return;
125 }
126 if (name.compare("key") == 0) {
127 inKey_ = false;
128 character_.erase();
129 return;
130 }
131 character_.erase();
132
133 if(name.compare("persistent") == 0) {
134 inIsPersistent_ = false;
135 statusQosData_.setPersistent(StringTrim::isTrueTrim(character_));
136 character_.erase();
137 return;
138 }
139 }
140
141 StatusQosData StatusQosFactory::readObject(const string& qos)
142 {
143 init(qos);
144 return statusQosData_;
145 }
146
147 }}}} // namespaces
148
149
150 #ifdef _XMLBLASTER_CLASSTEST
151
152 using namespace std;
153 using namespace org::xmlBlaster::util::qos;
154
155 int main(int args, char* argv[])
156 {
157 try
158 {
159 Global& glob = Global::getInstance();
160 glob.initialize(args, argv);
161
162 StatusQosData data1(glob);
163 StatusQosFactory factory(glob);
164 string qos = data1.toXml();
165 StatusQosData data2 = factory.readObject(qos);
166
167 cout << "data before parsing: " << data1.toXml() << endl;
168 cout << "data after parsing : " << data2.toXml() << endl;
169 }
170 catch(...) {
171 cout << "Error occured";
172 return 1;
173 }
174
175 return 0;
176 }
177
178 #endif
syntax highlighted by Code2HTML, v. 0.9.1