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.7 2008/02/09 10:18:34 dellaric 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 }
58 
59 
60 
61 // - - - - - - - - - - - - - - - - -
62 
63 void MonitorXMLParser::handleElement( xercesc::DOMElement* element ){
64 
65  if( xercesc::XMLString::equals( tags_->TAG_ME, element->getTagName() ) ) {
66 
67  char* c;
68  std::stringstream s;
69  DB_ME me;
70  bool meok;
71 
72  meok = false;
73 
74  xercesc::DOMNodeList* d1Nodes = element->getElementsByTagName( tags_->TAG_1D );
75  const XMLSize_t d1Count = d1Nodes->getLength();
76 
77  for( XMLSize_t d1Index = 0; d1Index < d1Count; ++d1Index ){
78 
79  xercesc::DOMNode* d1Node = d1Nodes->item( d1Index ) ;
80 
81  xercesc::DOMElement* d1Element = dynamic_cast< xercesc::DOMElement* >( d1Node ) ;
82 
83  const XMLCh* d1titleXMLCh = d1Element->getAttribute( tags_->ATTR_TITLE ) ;
84  c = xercesc::XMLString::transcode( d1titleXMLCh );
85  me.type = "th1d";
86  me.title = c;
87  meok = true;
89 
90  const XMLCh* d1xbinsXMLCh = d1Element->getAttribute( tags_->ATTR_XBINS ) ;
91  c = xercesc::XMLString::transcode( d1xbinsXMLCh );
92  s.clear(); s.str( c );
93  s >> me.xbins;
95 
96  const XMLCh* d1xfromXMLCh = d1Element->getAttribute( tags_->ATTR_XFROM ) ;
97  c = xercesc::XMLString::transcode( d1xfromXMLCh );
98  s.clear(); s.str( c );
99  s >> me.xfrom;
101 
102  const XMLCh* d1xtoXMLCh = d1Element->getAttribute( tags_->ATTR_XTO ) ;
103  c = xercesc::XMLString::transcode( d1xtoXMLCh );
104  s.clear(); s.str( c );
105  s >> me.xto;
107 
108  const XMLCh* d1ncycleXMLCh = d1Element->getAttribute( tags_->ATTR_NCYCLE ) ;
109  c = xercesc::XMLString::transcode( d1ncycleXMLCh );
110  s.clear(); s.str( c );
111  s >> me.ncycle;
113 
114  const XMLCh* d1loopXMLCh = d1Element->getAttribute( tags_->ATTR_LOOP ) ;
115  c = xercesc::XMLString::transcode( d1loopXMLCh );
116  s.clear(); s.str( c );
117  s >> me.loop;
119 
120  me.ybins = 0;
121  me.yfrom = 0.0;
122  me.yto = 0.0;
123  me.zbins = 0;
124  me.zfrom = 0.0;
125  me.zto = 0.0;
126 
127  }
128 
129  xercesc::DOMNodeList* d2Nodes = element->getElementsByTagName( tags_->TAG_2D );
130  const XMLSize_t d2Count = d2Nodes->getLength();
131 
132  for( XMLSize_t d2Index = 0; d2Index < d2Count; ++d2Index ){
133 
134  xercesc::DOMNode* d2Node = d2Nodes->item( d2Index ) ;
135 
136  xercesc::DOMElement* d2Element = dynamic_cast< xercesc::DOMElement* >( d2Node ) ;
137 
138  const XMLCh* d2titleXMLCh = d2Element->getAttribute( tags_->ATTR_TITLE ) ;
139  c = xercesc::XMLString::transcode( d2titleXMLCh );
140  me.type = "th2d";
141  me.title = c;
142  meok = true;
144 
145  const XMLCh* d2xbinsXMLCh = d2Element->getAttribute( tags_->ATTR_XBINS ) ;
146  c = xercesc::XMLString::transcode( d2xbinsXMLCh );
147  s.clear(); s.str( c );
148  s >> me.xbins;
150 
151  const XMLCh* d2xfromXMLCh = d2Element->getAttribute( tags_->ATTR_XFROM ) ;
152  c = xercesc::XMLString::transcode( d2xfromXMLCh );
153  s.clear(); s.str( c );
154  s >> me.xfrom;
156 
157  const XMLCh* d2xtoXMLCh = d2Element->getAttribute( tags_->ATTR_XTO ) ;
158  c = xercesc::XMLString::transcode( d2xtoXMLCh );
159  s.clear(); s.str( c );
160  s >> me.xto;
162 
163  const XMLCh* d2ybinsXMLCh = d2Element->getAttribute( tags_->ATTR_YBINS ) ;
164  c = xercesc::XMLString::transcode( d2ybinsXMLCh );
165  s.clear(); s.str( c );
166  s >> me.ybins;
168 
169  const XMLCh* d2yfromXMLCh = d2Element->getAttribute( tags_->ATTR_YFROM ) ;
170  c = xercesc::XMLString::transcode( d2yfromXMLCh );
171  s.clear(); s.str( c );
172  s >> me.yfrom;
174 
175  const XMLCh* d2ytoXMLCh = d2Element->getAttribute( tags_->ATTR_YTO ) ;
176  c = xercesc::XMLString::transcode( d2ytoXMLCh );
177  s.clear(); s.str( c );
178  s >> me.yto;
180 
181  const XMLCh* d2ncycleXMLCh = d2Element->getAttribute( tags_->ATTR_NCYCLE ) ;
182  c = xercesc::XMLString::transcode( d2ncycleXMLCh );
183  s.clear(); s.str( c );
184  s >> me.ncycle;
186 
187  const XMLCh* d2loopXMLCh = d2Element->getAttribute( tags_->ATTR_LOOP ) ;
188  c = xercesc::XMLString::transcode( d2loopXMLCh );
189  s.clear(); s.str( c );
190  s >> me.loop;
192 
193  me.zbins = 0;
194  me.zfrom = 0.0;
195  me.zto = 0.0;
196 
197  }
198 
199  xercesc::DOMNodeList* tpNodes = element->getElementsByTagName( tags_->TAG_TPROFILE );
200  const XMLSize_t tpCount = tpNodes->getLength();
201 
202  for( XMLSize_t tpIndex = 0; tpIndex < tpCount; ++tpIndex ){
203 
204  xercesc::DOMNode* tpNode = tpNodes->item( tpIndex ) ;
205 
206  xercesc::DOMElement* tpElement = dynamic_cast< xercesc::DOMElement* >( tpNode ) ;
207 
208  const XMLCh* tptitleXMLCh = tpElement->getAttribute( tags_->ATTR_TITLE ) ;
209  c = xercesc::XMLString::transcode( tptitleXMLCh );
210  me.type = "tprofile";
211  me.title = c;
212  meok = true;
214 
215  const XMLCh* tpxbinsXMLCh = tpElement->getAttribute( tags_->ATTR_XBINS ) ;
216  c = xercesc::XMLString::transcode( tpxbinsXMLCh );
217  s.clear(); s.str( c );
218  s >> me.xbins;
220 
221  const XMLCh* tpxfromXMLCh = tpElement->getAttribute( tags_->ATTR_XFROM ) ;
222  c = xercesc::XMLString::transcode( tpxfromXMLCh );
223  s.clear(); s.str( c );
224  s >> me.xfrom;
226 
227  const XMLCh* tpxtoXMLCh = tpElement->getAttribute( tags_->ATTR_XTO ) ;
228  c = xercesc::XMLString::transcode( tpxtoXMLCh );
229  s.clear(); s.str( c );
230  s >> me.xto;
232 
233  const XMLCh* tpybinsXMLCh = tpElement->getAttribute( tags_->ATTR_YBINS ) ;
234  c = xercesc::XMLString::transcode( tpybinsXMLCh );
235  s.clear(); s.str( c );
236  s >> me.ybins;
238 
239  const XMLCh* tpyfromXMLCh = tpElement->getAttribute( tags_->ATTR_YFROM ) ;
240  c = xercesc::XMLString::transcode( tpyfromXMLCh );
241  s.clear(); s.str( c );
242  s >> me.yfrom;
244 
245  const XMLCh* tpytoXMLCh = tpElement->getAttribute( tags_->ATTR_YTO ) ;
246  c = xercesc::XMLString::transcode( tpytoXMLCh );
247  s.clear(); s.str( c );
248  s >> me.yto;
250 
251  const XMLCh* tpncycleXMLCh = tpElement->getAttribute( tags_->ATTR_NCYCLE ) ;
252  c = xercesc::XMLString::transcode( tpncycleXMLCh );
253  s.clear(); s.str( c );
254  s >> me.ncycle;
256 
257  const XMLCh* tploopXMLCh = tpElement->getAttribute( tags_->ATTR_LOOP ) ;
258  c = xercesc::XMLString::transcode( tploopXMLCh );
259  s.clear(); s.str( c );
260  s >> me.loop;
262 
263  me.zbins = 0;
264  me.zfrom = 0.0;
265  me.zto = 0.0;
266 
267  }
268 
269  xercesc::DOMNodeList* tp2dNodes = element->getElementsByTagName( tags_->TAG_TPROFILE2D );
270  const XMLSize_t tp2dCount = tp2dNodes->getLength();
271 
272  for( XMLSize_t tp2dIndex = 0; tp2dIndex < tp2dCount; ++tp2dIndex ){
273 
274  xercesc::DOMNode* tp2dNode = tp2dNodes->item( tp2dIndex ) ;
275 
276  xercesc::DOMElement* tp2dElement = dynamic_cast< xercesc::DOMElement* >( tp2dNode ) ;
277 
278  const XMLCh* tp2dtitleXMLCh = tp2dElement->getAttribute( tags_->ATTR_TITLE ) ;
279  c = xercesc::XMLString::transcode( tp2dtitleXMLCh );
280  me.type = "tprofile2d";
281  me.title = c;
282  meok = true;
284 
285  const XMLCh* tp2dxbinsXMLCh = tp2dElement->getAttribute( tags_->ATTR_XBINS ) ;
286  c = xercesc::XMLString::transcode( tp2dxbinsXMLCh );
287  s.clear(); s.str( c );
288  s >> me.xbins;
290 
291  const XMLCh* tp2dxfromXMLCh = tp2dElement->getAttribute( tags_->ATTR_XFROM ) ;
292  c = xercesc::XMLString::transcode( tp2dxfromXMLCh );
293  s.clear(); s.str( c );
294  s >> me.xfrom;
296 
297  const XMLCh* tp2dxtoXMLCh = tp2dElement->getAttribute( tags_->ATTR_XTO ) ;
298  c = xercesc::XMLString::transcode( tp2dxtoXMLCh );
299  s.clear(); s.str( c );
300  s >> me.xto;
302 
303  const XMLCh* tp2dybinsXMLCh = tp2dElement->getAttribute( tags_->ATTR_YBINS ) ;
304  c = xercesc::XMLString::transcode( tp2dybinsXMLCh );
305  s.clear(); s.str( c );
306  s >> me.ybins;
308 
309  const XMLCh* tp2dyfromXMLCh = tp2dElement->getAttribute( tags_->ATTR_YFROM ) ;
310  c = xercesc::XMLString::transcode( tp2dyfromXMLCh );
311  s.clear(); s.str( c );
312  s >> me.yfrom;
314 
315  const XMLCh* tp2dytoXMLCh = tp2dElement->getAttribute( tags_->ATTR_YTO ) ;
316  c = xercesc::XMLString::transcode( tp2dytoXMLCh );
317  s.clear(); s.str( c );
318  s >> me.yto;
320 
321  const XMLCh* tp2dzbinsXMLCh = tp2dElement->getAttribute( tags_->ATTR_ZBINS ) ;
322  c = xercesc::XMLString::transcode( tp2dzbinsXMLCh );
323  s.clear(); s.str( c );
324  s >> me.zbins;
326 
327  const XMLCh* tp2dzfromXMLCh = tp2dElement->getAttribute( tags_->ATTR_ZFROM ) ;
328  c = xercesc::XMLString::transcode( tp2dzfromXMLCh );
329  s.clear(); s.str( c );
330  s >> me.zfrom;
332 
333  const XMLCh* tp2dztoXMLCh = tp2dElement->getAttribute( tags_->ATTR_ZTO ) ;
334  c = xercesc::XMLString::transcode( tp2dztoXMLCh );
335  s.clear(); s.str( c );
336  s >> me.zto;
338 
339  const XMLCh* tp2dncycleXMLCh = tp2dElement->getAttribute( tags_->ATTR_NCYCLE ) ;
340  c = xercesc::XMLString::transcode( tp2dncycleXMLCh );
341  s.clear(); s.str( c );
342  s >> me.ncycle;
344 
345  const XMLCh* tp2dloopXMLCh = tp2dElement->getAttribute( tags_->ATTR_LOOP ) ;
346  c = xercesc::XMLString::transcode( tp2dloopXMLCh );
347  s.clear(); s.str( c );
348  s >> me.loop;
350 
351  }
352 
353 
354  xercesc::DOMNodeList* qNodes = element->getElementsByTagName( tags_->TAG_QUERY );
355  const XMLSize_t qCount = qNodes->getLength();
356 
357  for( XMLSize_t qIndex = 0; qIndex < qCount; ++qIndex ){
358 
359  xercesc::DOMNode* qNode = qNodes->item( qIndex ) ;
360 
361  xercesc::DOMElement* qElement = dynamic_cast< xercesc::DOMElement* >( qNode ) ;
362 
363  const XMLCh* nameXMLCh = qElement->getAttribute( tags_->ATTR_NAME ) ;
364  c = xercesc::XMLString::transcode( nameXMLCh );
365 
366  const XMLCh* argXMLCh = qElement->getAttribute( tags_->ATTR_ARG ) ;
367  char* d = xercesc::XMLString::transcode( argXMLCh );
368 
369  const XMLCh* aliasXMLCh = qElement->getAttribute( tags_->ATTR_ALIAS ) ;
370  char* e = xercesc::XMLString::transcode( aliasXMLCh );
371 
372  DbQuery tmpQuery;
373  tmpQuery.query = c;
374  tmpQuery.arg = d;
375  tmpQuery.alias = e;
376 
377  me.queries.push_back( tmpQuery );
378 
382 
383 
384  }
385 
386  if( meok ) DBMonitoringElements_.push_back( me );
387 
388  }
389 } // handleElement()
390 
391 
392 // - - - - - - - - - - - - - - - - - - -
393 
394 void MonitorXMLParser::load() throw( std::runtime_error ) {
395 
396  parser_->setValidationScheme( xercesc::XercesDOMParser::Val_Never );
397  parser_->setDoNamespaces( false );
398  parser_->setDoSchema( false );
399  parser_->setLoadExternalDTD( false );
400 
401  try{
402 
403  parser_->parse( xmlFile_.c_str() );
404 
405  xercesc::DOMDocument* xmlDoc = parser_->getDocument();
406 
407  xercesc::DOMElement* dbe = xmlDoc->getDocumentElement();
408 
409  if( NULL == dbe ){
410  throw( std::runtime_error( "empty XML document" ) ) ;
411  }
412 
413  if( xercesc::XMLString::equals( tags_->TAG_DBE, dbe->getTagName() ) ) {
414 
415  xercesc::DOMNodeList* children = dbe->getChildNodes();
416  const XMLSize_t nodeCount = children->getLength();
417 
418  for( XMLSize_t ix = 0 ; ix < nodeCount ; ++ix ){
419  xercesc::DOMNode* currentNode = children->item( ix );
420  if( NULL == currentNode ){
421  // null node...
422  continue;
423  }
424 
425  if( xercesc::DOMNode::ELEMENT_NODE != currentNode->getNodeType() ){
426  continue;
427  }
428 
429  xercesc::DOMElement* currentElement = dynamic_cast< xercesc::DOMElement* >( currentNode );
430 
431  handleElement( currentElement );
432 
433  }
434  }
435 
436  }catch( xercesc::XMLException& e ){
437 
438  char* message = xercesc::XMLString::transcode( e.getMessage() );
439 
440  std::ostringstream buf ;
441  buf << "Error parsing file: " << message << std::endl;
442 
443  xercesc::XMLString::release( &message );
444 
445  throw( std::runtime_error( buf.str() ) );
446 
447  }catch( const xercesc::DOMException& e ){
448 
449  char* message = xercesc::XMLString::transcode( e.getMessage() );
450 
451  std::ostringstream buf;
452  buf << "Encountered DOM Exception: " << message << std::endl;
453 
454  xercesc::XMLString::release( &message );
455 
456  throw( std::runtime_error( buf.str() ) );
457 
458  }
459 
460  return;
461 
462 } // load()
463 
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
string message
Definition: argparse.py:126
XMLCh * ATTR_NCYCLE
double zto
XMLCh * ATTR_TITLE
XMLCh * ATTR_LOOP
std::string query
string s
Definition: asciidump.py:422
XMLCh * ATTR_XFROM
XMLCh * TAG_TPROFILE
double xfrom
xercesc::XercesDOMParser * parser_
XMLCh * TAG_DBE
XMLCh * ATTR_ZFROM