CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
MonitorXMLParser.cc
Go to the documentation of this file.
1 // $Id: MonitorXMLParser.cc,v 1.9 2011/09/15 21:00:17 yiiyama Exp $
2 
11 #include <xercesc/util/PlatformUtils.hpp>
12 
13 #include <xercesc/dom/DOMDocument.hpp>
14 #include <xercesc/dom/DOMNodeList.hpp>
15 
16 #include <sstream>
17 #include <stdexcept>
18 
20 
21 MonitorXMLParser::MonitorXMLParser( const std::string& fromFile ) {
22 
23  try{
24 
25  xercesc::XMLPlatformUtils::Initialize();
26 
27  }catch( xercesc::XMLException& e ){
28 
29  char* message = xercesc::XMLString::transcode( e.getMessage() ) ;
30 
31  std::cerr << "XML toolkit initialization error: " << message << std::endl;
32 
33  xercesc::XMLString::release( &message );
34 
36 
37  }
38 
39  xmlFile_ = fromFile;
40  parser_ = new xercesc::XercesDOMParser();
41  tags_ = new TagNames();
42 
43 }
44 
45 // - - - - - - - - - - - - - - - - -
46 
48 
49  try{
50  xercesc::XMLPlatformUtils::Terminate();
51  } catch ( xercesc::XMLException& e ){
52  char* message = xercesc::XMLString::transcode( e.getMessage() );
53  std::cerr << "XML toolkit teardown error: " << message << std::endl;
54  xercesc::XMLString::release( &message ) ;
55  }
56 
57  delete parser_;
58  delete tags_;
59 
60 }
61 
62 
63 
64 // - - - - - - - - - - - - - - - - -
65 
66 void MonitorXMLParser::handleElement( xercesc::DOMElement* element ){
67 
68  if( xercesc::XMLString::equals( tags_->TAG_ME, element->getTagName() ) ) {
69 
70  char* c;
71  std::stringstream s;
72  DB_ME me;
73  bool meok;
74 
75  meok = false;
76 
77  xercesc::DOMNodeList* d1Nodes = element->getElementsByTagName( tags_->TAG_1D );
78  const XMLSize_t d1Count = d1Nodes->getLength();
79 
80  for( XMLSize_t d1Index = 0; d1Index < d1Count; ++d1Index ){
81 
82  xercesc::DOMNode* d1Node = d1Nodes->item( d1Index ) ;
83 
84  xercesc::DOMElement* d1Element = dynamic_cast< xercesc::DOMElement* >( d1Node ) ;
85 
86  const XMLCh* d1titleXMLCh = d1Element->getAttribute( tags_->ATTR_TITLE ) ;
87  c = xercesc::XMLString::transcode( d1titleXMLCh );
88  me.type = "th1d";
89  me.title = c;
90  meok = true;
92 
93  const XMLCh* d1xbinsXMLCh = d1Element->getAttribute( tags_->ATTR_XBINS ) ;
94  c = xercesc::XMLString::transcode( d1xbinsXMLCh );
95  s.clear(); s.str( c );
96  s >> me.xbins;
98 
99  const XMLCh* d1xfromXMLCh = d1Element->getAttribute( tags_->ATTR_XFROM ) ;
100  c = xercesc::XMLString::transcode( d1xfromXMLCh );
101  s.clear(); s.str( c );
102  s >> me.xfrom;
104 
105  const XMLCh* d1xtoXMLCh = d1Element->getAttribute( tags_->ATTR_XTO ) ;
106  c = xercesc::XMLString::transcode( d1xtoXMLCh );
107  s.clear(); s.str( c );
108  s >> me.xto;
110 
111  const XMLCh* d1ncycleXMLCh = d1Element->getAttribute( tags_->ATTR_NCYCLE ) ;
112  c = xercesc::XMLString::transcode( d1ncycleXMLCh );
113  s.clear(); s.str( c );
114  s >> me.ncycle;
116 
117  const XMLCh* d1loopXMLCh = d1Element->getAttribute( tags_->ATTR_LOOP ) ;
118  c = xercesc::XMLString::transcode( d1loopXMLCh );
119  s.clear(); s.str( c );
120  s >> me.loop;
122 
123  me.ybins = 0;
124  me.yfrom = 0.0;
125  me.yto = 0.0;
126  me.zbins = 0;
127  me.zfrom = 0.0;
128  me.zto = 0.0;
129 
130  }
131 
132  xercesc::DOMNodeList* d2Nodes = element->getElementsByTagName( tags_->TAG_2D );
133  const XMLSize_t d2Count = d2Nodes->getLength();
134 
135  for( XMLSize_t d2Index = 0; d2Index < d2Count; ++d2Index ){
136 
137  xercesc::DOMNode* d2Node = d2Nodes->item( d2Index ) ;
138 
139  xercesc::DOMElement* d2Element = dynamic_cast< xercesc::DOMElement* >( d2Node ) ;
140 
141  const XMLCh* d2titleXMLCh = d2Element->getAttribute( tags_->ATTR_TITLE ) ;
142  c = xercesc::XMLString::transcode( d2titleXMLCh );
143  me.type = "th2d";
144  me.title = c;
145  meok = true;
147 
148  const XMLCh* d2xbinsXMLCh = d2Element->getAttribute( tags_->ATTR_XBINS ) ;
149  c = xercesc::XMLString::transcode( d2xbinsXMLCh );
150  s.clear(); s.str( c );
151  s >> me.xbins;
153 
154  const XMLCh* d2xfromXMLCh = d2Element->getAttribute( tags_->ATTR_XFROM ) ;
155  c = xercesc::XMLString::transcode( d2xfromXMLCh );
156  s.clear(); s.str( c );
157  s >> me.xfrom;
159 
160  const XMLCh* d2xtoXMLCh = d2Element->getAttribute( tags_->ATTR_XTO ) ;
161  c = xercesc::XMLString::transcode( d2xtoXMLCh );
162  s.clear(); s.str( c );
163  s >> me.xto;
165 
166  const XMLCh* d2ybinsXMLCh = d2Element->getAttribute( tags_->ATTR_YBINS ) ;
167  c = xercesc::XMLString::transcode( d2ybinsXMLCh );
168  s.clear(); s.str( c );
169  s >> me.ybins;
171 
172  const XMLCh* d2yfromXMLCh = d2Element->getAttribute( tags_->ATTR_YFROM ) ;
173  c = xercesc::XMLString::transcode( d2yfromXMLCh );
174  s.clear(); s.str( c );
175  s >> me.yfrom;
177 
178  const XMLCh* d2ytoXMLCh = d2Element->getAttribute( tags_->ATTR_YTO ) ;
179  c = xercesc::XMLString::transcode( d2ytoXMLCh );
180  s.clear(); s.str( c );
181  s >> me.yto;
183 
184  const XMLCh* d2ncycleXMLCh = d2Element->getAttribute( tags_->ATTR_NCYCLE ) ;
185  c = xercesc::XMLString::transcode( d2ncycleXMLCh );
186  s.clear(); s.str( c );
187  s >> me.ncycle;
189 
190  const XMLCh* d2loopXMLCh = d2Element->getAttribute( tags_->ATTR_LOOP ) ;
191  c = xercesc::XMLString::transcode( d2loopXMLCh );
192  s.clear(); s.str( c );
193  s >> me.loop;
195 
196  me.zbins = 0;
197  me.zfrom = 0.0;
198  me.zto = 0.0;
199 
200  }
201 
202  xercesc::DOMNodeList* tpNodes = element->getElementsByTagName( tags_->TAG_TPROFILE );
203  const XMLSize_t tpCount = tpNodes->getLength();
204 
205  for( XMLSize_t tpIndex = 0; tpIndex < tpCount; ++tpIndex ){
206 
207  xercesc::DOMNode* tpNode = tpNodes->item( tpIndex ) ;
208 
209  xercesc::DOMElement* tpElement = dynamic_cast< xercesc::DOMElement* >( tpNode ) ;
210 
211  const XMLCh* tptitleXMLCh = tpElement->getAttribute( tags_->ATTR_TITLE ) ;
212  c = xercesc::XMLString::transcode( tptitleXMLCh );
213  me.type = "tprofile";
214  me.title = c;
215  meok = true;
217 
218  const XMLCh* tpxbinsXMLCh = tpElement->getAttribute( tags_->ATTR_XBINS ) ;
219  c = xercesc::XMLString::transcode( tpxbinsXMLCh );
220  s.clear(); s.str( c );
221  s >> me.xbins;
223 
224  const XMLCh* tpxfromXMLCh = tpElement->getAttribute( tags_->ATTR_XFROM ) ;
225  c = xercesc::XMLString::transcode( tpxfromXMLCh );
226  s.clear(); s.str( c );
227  s >> me.xfrom;
229 
230  const XMLCh* tpxtoXMLCh = tpElement->getAttribute( tags_->ATTR_XTO ) ;
231  c = xercesc::XMLString::transcode( tpxtoXMLCh );
232  s.clear(); s.str( c );
233  s >> me.xto;
235 
236  const XMLCh* tpybinsXMLCh = tpElement->getAttribute( tags_->ATTR_YBINS ) ;
237  c = xercesc::XMLString::transcode( tpybinsXMLCh );
238  s.clear(); s.str( c );
239  s >> me.ybins;
241 
242  const XMLCh* tpyfromXMLCh = tpElement->getAttribute( tags_->ATTR_YFROM ) ;
243  c = xercesc::XMLString::transcode( tpyfromXMLCh );
244  s.clear(); s.str( c );
245  s >> me.yfrom;
247 
248  const XMLCh* tpytoXMLCh = tpElement->getAttribute( tags_->ATTR_YTO ) ;
249  c = xercesc::XMLString::transcode( tpytoXMLCh );
250  s.clear(); s.str( c );
251  s >> me.yto;
253 
254  const XMLCh* tpncycleXMLCh = tpElement->getAttribute( tags_->ATTR_NCYCLE ) ;
255  c = xercesc::XMLString::transcode( tpncycleXMLCh );
256  s.clear(); s.str( c );
257  s >> me.ncycle;
259 
260  const XMLCh* tploopXMLCh = tpElement->getAttribute( tags_->ATTR_LOOP ) ;
261  c = xercesc::XMLString::transcode( tploopXMLCh );
262  s.clear(); s.str( c );
263  s >> me.loop;
265 
266  me.zbins = 0;
267  me.zfrom = 0.0;
268  me.zto = 0.0;
269 
270  }
271 
272  xercesc::DOMNodeList* tp2dNodes = element->getElementsByTagName( tags_->TAG_TPROFILE2D );
273  const XMLSize_t tp2dCount = tp2dNodes->getLength();
274 
275  for( XMLSize_t tp2dIndex = 0; tp2dIndex < tp2dCount; ++tp2dIndex ){
276 
277  xercesc::DOMNode* tp2dNode = tp2dNodes->item( tp2dIndex ) ;
278 
279  xercesc::DOMElement* tp2dElement = dynamic_cast< xercesc::DOMElement* >( tp2dNode ) ;
280 
281  const XMLCh* tp2dtitleXMLCh = tp2dElement->getAttribute( tags_->ATTR_TITLE ) ;
282  c = xercesc::XMLString::transcode( tp2dtitleXMLCh );
283  me.type = "tprofile2d";
284  me.title = c;
285  meok = true;
287 
288  const XMLCh* tp2dxbinsXMLCh = tp2dElement->getAttribute( tags_->ATTR_XBINS ) ;
289  c = xercesc::XMLString::transcode( tp2dxbinsXMLCh );
290  s.clear(); s.str( c );
291  s >> me.xbins;
293 
294  const XMLCh* tp2dxfromXMLCh = tp2dElement->getAttribute( tags_->ATTR_XFROM ) ;
295  c = xercesc::XMLString::transcode( tp2dxfromXMLCh );
296  s.clear(); s.str( c );
297  s >> me.xfrom;
299 
300  const XMLCh* tp2dxtoXMLCh = tp2dElement->getAttribute( tags_->ATTR_XTO ) ;
301  c = xercesc::XMLString::transcode( tp2dxtoXMLCh );
302  s.clear(); s.str( c );
303  s >> me.xto;
305 
306  const XMLCh* tp2dybinsXMLCh = tp2dElement->getAttribute( tags_->ATTR_YBINS ) ;
307  c = xercesc::XMLString::transcode( tp2dybinsXMLCh );
308  s.clear(); s.str( c );
309  s >> me.ybins;
311 
312  const XMLCh* tp2dyfromXMLCh = tp2dElement->getAttribute( tags_->ATTR_YFROM ) ;
313  c = xercesc::XMLString::transcode( tp2dyfromXMLCh );
314  s.clear(); s.str( c );
315  s >> me.yfrom;
317 
318  const XMLCh* tp2dytoXMLCh = tp2dElement->getAttribute( tags_->ATTR_YTO ) ;
319  c = xercesc::XMLString::transcode( tp2dytoXMLCh );
320  s.clear(); s.str( c );
321  s >> me.yto;
323 
324  const XMLCh* tp2dzbinsXMLCh = tp2dElement->getAttribute( tags_->ATTR_ZBINS ) ;
325  c = xercesc::XMLString::transcode( tp2dzbinsXMLCh );
326  s.clear(); s.str( c );
327  s >> me.zbins;
329 
330  const XMLCh* tp2dzfromXMLCh = tp2dElement->getAttribute( tags_->ATTR_ZFROM ) ;
331  c = xercesc::XMLString::transcode( tp2dzfromXMLCh );
332  s.clear(); s.str( c );
333  s >> me.zfrom;
335 
336  const XMLCh* tp2dztoXMLCh = tp2dElement->getAttribute( tags_->ATTR_ZTO ) ;
337  c = xercesc::XMLString::transcode( tp2dztoXMLCh );
338  s.clear(); s.str( c );
339  s >> me.zto;
341 
342  const XMLCh* tp2dncycleXMLCh = tp2dElement->getAttribute( tags_->ATTR_NCYCLE ) ;
343  c = xercesc::XMLString::transcode( tp2dncycleXMLCh );
344  s.clear(); s.str( c );
345  s >> me.ncycle;
347 
348  const XMLCh* tp2dloopXMLCh = tp2dElement->getAttribute( tags_->ATTR_LOOP ) ;
349  c = xercesc::XMLString::transcode( tp2dloopXMLCh );
350  s.clear(); s.str( c );
351  s >> me.loop;
353 
354  }
355 
356 
357  xercesc::DOMNodeList* qNodes = element->getElementsByTagName( tags_->TAG_QUERY );
358  const XMLSize_t qCount = qNodes->getLength();
359 
360  for( XMLSize_t qIndex = 0; qIndex < qCount; ++qIndex ){
361 
362  xercesc::DOMNode* qNode = qNodes->item( qIndex ) ;
363 
364  xercesc::DOMElement* qElement = dynamic_cast< xercesc::DOMElement* >( qNode ) ;
365 
366  const XMLCh* nameXMLCh = qElement->getAttribute( tags_->ATTR_NAME ) ;
367  c = xercesc::XMLString::transcode( nameXMLCh );
368 
369  const XMLCh* argXMLCh = qElement->getAttribute( tags_->ATTR_ARG ) ;
370  char* d = xercesc::XMLString::transcode( argXMLCh );
371 
372  const XMLCh* aliasXMLCh = qElement->getAttribute( tags_->ATTR_ALIAS ) ;
373  char* e = xercesc::XMLString::transcode( aliasXMLCh );
374 
375  DbQuery tmpQuery;
376  tmpQuery.query = c;
377  tmpQuery.arg = d;
378  tmpQuery.alias = e;
379 
380  me.queries.push_back( tmpQuery );
381 
385 
386 
387  }
388 
389  if( meok ) DBMonitoringElements_.push_back( me );
390 
391  }
392 } // handleElement()
393 
394 
395 // - - - - - - - - - - - - - - - - - - -
396 
397 void MonitorXMLParser::load() throw( std::runtime_error ) {
398 
399  parser_->setValidationScheme( xercesc::XercesDOMParser::Val_Never );
400  parser_->setDoNamespaces( false );
401  parser_->setDoSchema( false );
402  parser_->setLoadExternalDTD( false );
403 
404  try{
405 
406  parser_->parse( xmlFile_.c_str() );
407 
408  xercesc::DOMDocument* xmlDoc = parser_->getDocument();
409 
410  xercesc::DOMElement* dbe = xmlDoc->getDocumentElement();
411 
412  if( NULL == dbe ){
413  throw( std::runtime_error( "empty XML document" ) ) ;
414  }
415 
416  if( xercesc::XMLString::equals( tags_->TAG_DBE, dbe->getTagName() ) ) {
417 
418  xercesc::DOMNodeList* children = dbe->getChildNodes();
419  const XMLSize_t nodeCount = children->getLength();
420 
421  for( XMLSize_t ix = 0 ; ix < nodeCount ; ++ix ){
422  xercesc::DOMNode* currentNode = children->item( ix );
423  if( NULL == currentNode ){
424  // null node...
425  continue;
426  }
427 
428  if( xercesc::DOMNode::ELEMENT_NODE != currentNode->getNodeType() ){
429  continue;
430  }
431 
432  xercesc::DOMElement* currentElement = dynamic_cast< xercesc::DOMElement* >( currentNode );
433 
434  handleElement( currentElement );
435 
436  }
437  }
438 
439  }catch( xercesc::XMLException& e ){
440 
441  char* message = xercesc::XMLString::transcode( e.getMessage() );
442 
443  std::ostringstream buf ;
444  buf << "Error parsing file: " << message << std::endl;
445 
446  xercesc::XMLString::release( &message );
447 
448  throw( std::runtime_error( buf.str() ) );
449 
450  }catch( const xercesc::DOMException& e ){
451 
452  char* message = xercesc::XMLString::transcode( e.getMessage() );
453 
454  std::ostringstream buf;
455  buf << "Encountered DOM Exception: " << message << std::endl;
456 
457  xercesc::XMLString::release( &message );
458 
459  throw( std::runtime_error( buf.str() ) );
460 
461  }
462 
463  return;
464 
465 } // load()
466 
XMLCh * TAG_ME
std::string type
XMLCh * TAG_QUERY
XMLCh * ATTR_YTO
std::string xmlFile_
XMLCh * ATTR_ZBINS
XMLCh * ATTR_XBINS
XMLCh * ATTR_ARG
#define NULL
Definition: scimark2.h:8
MonitorXMLParser(const std::string &fromFile)
std::string alias
XMLCh * TAG_2D
double xto
unsigned int ncycle
double yfrom
XMLCh * ATTR_YFROM
double yto
std::vector< DbQuery > queries
std::vector< DB_ME > DBMonitoringElements_
XMLCh * ATTR_ZTO
XMLCh * TAG_1D
XMLCh * ATTR_YBINS
double zfrom
XMLCh * ATTR_NAME
std::string title
XMLCh * ATTR_XTO
unsigned int loop
void handleElement(xercesc::DOMElement *element)
XMLCh * TAG_TPROFILE2D
XMLCh * ATTR_ALIAS
std::string arg
monitor db xml elements parsing tool
XMLCh * ATTR_NCYCLE
double zto
XMLCh * ATTR_TITLE
XMLCh * ATTR_LOOP
std::string query
XMLCh * ATTR_XFROM
XMLCh * TAG_TPROFILE
double xfrom
xercesc::XercesDOMParser * parser_
XMLCh * TAG_DBE
XMLCh * ATTR_ZFROM