CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
XMLDOMBlock.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: XMLTools
4 // Class : XMLDOMBlock
5 //
6 // Implementation:
7 // <Notes on implementation>
8 //
9 // Original Author: Gena Kukartsev
10 // Created: Thu Sep 27 01:43:42 CEST 2007
11 // $Id: XMLDOMBlock.cc,v 1.5 2010/08/06 20:24:03 wmtan Exp $
12 //
13 
14 // system include files
15 #include <iostream>
16 #include <string>
17 #include<time.h>
18 #include <xercesc/parsers/XercesDOMParser.hpp>
19 #include <xercesc/sax/HandlerBase.hpp>
20 #include <xercesc/dom/DOM.hpp>
21 
22 //
23 //_____ following removed as a xalan-c component_____________________
24 //
25 //#include <xalanc/XPath/XObject.hpp>
26 
27 XERCES_CPP_NAMESPACE_USE
28 using namespace std;
29 
30 // user include files
33 
34 
35 
37 {
38  DOMNodeList * _children = other.getDocumentConst()->getDocumentElement()->getChildNodes();
39  int _length = _children->getLength();
40  //std::cout << "Children nodes:" << _length << std::endl;
41  DOMNode * _node;
42  for(int i=0;i!=_length;i++){
43  _node = _children->item(i);
44  DOMNode * i_node = this->getDocument()->importNode(_node,true);
45  this->getDocument()->getDocumentElement()->appendChild(i_node);
46  }
47  return *this;
48 }
49 
50 
52 {
53  //std::cout << "XMLDOMBlock (or derived): default constructor called - this is meaningless!" << std::endl;
54  //std::cout << "XMLDOMBlock (or derived): use yourClass( loaderBaseConfig & ) instead." << std::endl;
55  init( "ROOT" );
56 }
57 
58 XMLDOMBlock::XMLDOMBlock( std::string _root, int rootElementName )
59 {
60  //std::cout << "XMLDOMBlock (or derived): default constructor called - this is meaningless!" << std::endl;
61  //std::cout << "XMLDOMBlock (or derived): use yourClass( loaderBaseConfig & ) instead." << std::endl;
62  init( _root );
63 }
64 
65 
67 {
68  //
69  //_____ following removed as a xalan-c component_____________________
70  //
71  /*
72  // xalan objects initialization
73  theSourceTreeInit = 0;
74  theDOMSupport = 0;
75  theLiaison = 0;
76  theInputSource = 0;
77  theDocument = 0;
78  thePrefixResolver = 0;
79  theEvaluator = 0;
80  */
81 
82  theProcessor = XMLProcessor::getInstance();
83 
84  //theFileName = xmlFileName;
85 
86  // initialize the parser
87  parser = new XercesDOMParser();
88  parser->setValidationScheme(XercesDOMParser::Val_Always);
89  parser->setDoNamespaces(true); // optional
90 
91  errHandler = (ErrorHandler*) new HandlerBase();
92  parser->setErrorHandler(errHandler);
93 
94  // parse the input xml file
95  try
96  {
97  parser->parse( _source );
98  }
99  catch (const XMLException& toCatch) {
100  char* message = XMLString::transcode(toCatch.getMessage());
101  std::cout << "Exception message is: \n"
102  << message << "\n";
103  XMLString::release(&message);
104  //return -1;
105  }
106  catch (const DOMException& toCatch) {
107  char* message = XMLString::transcode(toCatch.msg);
108  std::cout << "Exception message is: \n"
109  << message << "\n";
110  XMLString::release(&message);
111  //return -1;
112  }
113  catch (...) {
114  std::cout << "Unexpected Exception \n" ;
115  //return -1;
116  }
117 
118  //get the XML document
119  document = parser -> getDocument();
120 }
121 
122 
124 {
125 
126  theProcessor = XMLProcessor::getInstance();
127 
128  //theFileName = xmlFileName;
129 
130  // initialize the parser
131  parser = new XercesDOMParser();
132  parser->setValidationScheme(XercesDOMParser::Val_Always);
133  parser->setDoNamespaces(true); // optional
134 
135  errHandler = (ErrorHandler*) new HandlerBase();
136  parser->setErrorHandler(errHandler);
137 
138  // parse the input xml file
139  try
140  {
141  parser->parse( _source );
142  }
143  catch (const XMLException& toCatch) {
144  char* message = XMLString::transcode(toCatch.getMessage());
145  std::cout << "Exception message is: \n"
146  << message << "\n";
147  XMLString::release(&message);
148  //return -1;
149  }
150  catch (const DOMException& toCatch) {
151  char* message = XMLString::transcode(toCatch.msg);
152  std::cout << "Exception message is: \n"
153  << message << "\n";
154  XMLString::release(&message);
155  //return -1;
156  }
157  catch (...) {
158  std::cout << "Unexpected Exception \n" ;
159  //return -1;
160  }
161 
162  //get the XML document
163  document = parser -> getDocument();
164 }
165 
166 
167 
168 int XMLDOMBlock::init( std::string _root )
169 {
170  theProcessor = XMLProcessor::getInstance();
171 
172  //theFileName = xmlFileName;
173 
174  // initialize the parser
175  parser = new XercesDOMParser();
176  parser->setValidationScheme(XercesDOMParser::Val_Always);
177  parser->setDoNamespaces(true); // optional
178 
179  errHandler = (ErrorHandler*) new HandlerBase();
180  parser->setErrorHandler(errHandler);
181 
182  DOMImplementation* impl = DOMImplementation::getImplementation();
183 
184  document = impl->createDocument(
185  0, // root element namespace URI.
186  //XMLString::transcode("ROOT"), // root element name
187  XMLProcessor::_toXMLCh(_root), // root element name
188  0); // document type object (DTD).
189 
190  the_string = 0;
191 
192  //
193  //_____ following removed as a xalan-c component_____________________
194  //
195  /*
196  // xalan objects initialization
197  theSourceTreeInit = 0;
198  theDOMSupport = 0;
199  theLiaison = 0;
200  theInputSource = 0;
201  theDocument = 0;
202  thePrefixResolver = 0;
203  theEvaluator = 0;
204  */
205  return 0;
206 }
207 
208 
209 
211 {
212  //
213  //_____ following removed as a xalan-c component_____________________
214  //
215  /*
216  // xalan objects initialization
217  theSourceTreeInit = 0;
218  theDOMSupport = 0;
219  theLiaison = 0;
220  theInputSource = 0;
221  theDocument = 0;
222  thePrefixResolver = 0;
223  theEvaluator = 0;
224  */
225 
226  theProcessor = XMLProcessor::getInstance();
227 
228  theFileName = xmlFileName;
229 
230  // initialize the parser
231  parser = new XercesDOMParser();
232  parser->setValidationScheme(XercesDOMParser::Val_Always);
233  parser->setDoNamespaces(true); // optional
234 
235  errHandler = (ErrorHandler*) new HandlerBase();
236  parser->setErrorHandler(errHandler);
237 
238  // parse the input xml file
239  try
240  {
241  parser->parse( theFileName . c_str() );
242  }
243  catch (const XMLException& toCatch) {
244  char* message = XMLString::transcode(toCatch.getMessage());
245  std::cout << "Exception message is: \n"
246  << message << "\n";
247  XMLString::release(&message);
248  //return -1;
249  }
250  catch (const DOMException& toCatch) {
251  char* message = XMLString::transcode(toCatch.msg);
252  std::cout << "Exception message is: \n"
253  << message << "\n";
254  XMLString::release(&message);
255  //return -1;
256  }
257  catch (...) {
258  std::cout << "Unexpected Exception \n" ;
259  //return -1;
260  }
261 
262  //get the XML document
263  document = parser -> getDocument();
264 
265 }
266 
267 DOMDocument * XMLDOMBlock::getNewDocument( std::string xmlFileName )
268 {
269  delete document;
270 
271  theProcessor = XMLProcessor::getInstance();
272 
273  theFileName = xmlFileName;
274 
275  // initialize the parser
276  parser = new XercesDOMParser();
277  parser->setValidationScheme(XercesDOMParser::Val_Always);
278  parser->setDoNamespaces(true); // optional
279 
280  errHandler = (ErrorHandler*) new HandlerBase();
281  parser->setErrorHandler(errHandler);
282 
283  // parse the input xml file
284  try
285  {
286  parser->parse( theFileName . c_str() );
287  }
288  catch (const XMLException& toCatch) {
289  char* message = XMLString::transcode(toCatch.getMessage());
290  std::cout << "Exception message is: \n"
291  << message << "\n";
292  XMLString::release(&message);
293  //return -1;
294  }
295  catch (const DOMException& toCatch) {
296  char* message = XMLString::transcode(toCatch.msg);
297  std::cout << "Exception message is: \n"
298  << message << "\n";
299  XMLString::release(&message);
300  //return -1;
301  }
302  catch (...) {
303  std::cout << "Unexpected Exception \n" ;
304  //return -1;
305  }
306 
307  //get the XML document
308  document = parser -> getDocument();
309 
310  return document;
311 }
312 
313 DOMDocument * XMLDOMBlock::getDocument( void )
314 {
315  return document;
316 }
317 
318 DOMDocument * XMLDOMBlock::getDocumentConst( void ) const
319 {
320  return document;
321 }
322 
323 int XMLDOMBlock::write( std::string target )
324 {
325  theProcessor -> write( this, target );
326 
327  return 0;
328 }
329 
331 {
332  delete parser;
333  delete errHandler;
334  //if (the_string) delete the_string;
335 
336  //
337  //_____ following removed as a xalan-c component_____________________
338  //
339  /*
340  // delete xalan objects
341  delete theSourceTreeInit;
342  delete theDOMSupport;
343  delete theLiaison;
344  delete theInputSource;
345  //delete theDocument; // noneed to delete - belongs to theLiaison
346  delete thePrefixResolver;
347  delete theEvaluator;
348  */
349 }
350 
351 const char * XMLDOMBlock::getTagValue( const std::string & tagName, int _item, DOMDocument * _document )
352 {
353  if (!_document) _document = document;
354  const char * _result = XMLString::transcode( _document -> getElementsByTagName( XMLProcessor::_toXMLCh( tagName ) ) -> item( _item ) -> getFirstChild()-> getNodeValue() );
355  return _result;
356 }
357 
358 const char * XMLDOMBlock::getTagValue( const std::string & tagName, int _item, DOMElement * _document )
359 {
360  if (!_document) return 0;
361  const char * _result = XMLString::transcode( _document -> getElementsByTagName( XMLProcessor::_toXMLCh( tagName ) ) -> item( _item ) -> getFirstChild()-> getNodeValue() );
362  return _result;
363 }
364 
365 DOMNode * XMLDOMBlock::setTagValue( const std::string & tagName, const std::string & tagValue, int _item, DOMDocument * _document )
366 {
367  if (!_document) _document = document;
368  DOMNode * the_tag = _document -> getElementsByTagName( XMLProcessor::_toXMLCh( tagName ) ) -> item( _item );
369  the_tag -> getFirstChild() -> setNodeValue( XMLProcessor::_toXMLCh( tagValue ) );
370  return the_tag;
371 }
372 
373 DOMNode * XMLDOMBlock::setTagValue(DOMElement * _elem, const std::string & tagName, const std::string & tagValue, int _item )
374 {
375  if (!_elem) return 0;
376  DOMNode * the_tag = _elem -> getElementsByTagName( XMLProcessor::_toXMLCh( tagName ) ) -> item( _item );
377  if (the_tag){
378  the_tag -> getFirstChild() -> setNodeValue( XMLProcessor::_toXMLCh( tagValue ) );
379  }
380  return the_tag;
381 }
382 
383 DOMNode * XMLDOMBlock::setTagValue( const std::string & tagName, const int & tagValue, int _item, DOMDocument * _document )
384 {
385  if (!_document) _document = document;
386  DOMNode * the_tag = _document -> getElementsByTagName( XMLProcessor::_toXMLCh( tagName ) ) -> item( _item );
387  the_tag -> getFirstChild() -> setNodeValue( XMLProcessor::_toXMLCh( tagValue ) );
388  return the_tag;
389 }
390 
391 DOMNode * XMLDOMBlock::setTagValue( DOMElement * _elem, const std::string & tagName, const int & tagValue, int _item )
392 {
393  if (!_elem) return 0;
394  DOMNode * the_tag = _elem -> getElementsByTagName( XMLProcessor::_toXMLCh( tagName ) ) -> item( _item );
395  if(the_tag){
396  the_tag -> getFirstChild() -> setNodeValue( XMLProcessor::_toXMLCh( tagValue ) );
397  }
398  return the_tag;
399 }
400 
401 const char * XMLDOMBlock::getTagAttribute( const std::string & tagName, const std::string & attrName, int _item )
402 {
403  DOMElement * _tag = (DOMElement *)(document -> getElementsByTagName( XMLProcessor::_toXMLCh( tagName ) ) -> item( _item ));
404  const char * _result = XMLString::transcode( _tag -> getAttribute( XMLProcessor::_toXMLCh( attrName ) ) );
405 
406  return _result;
407 }
408 
409 DOMNode * XMLDOMBlock::setTagAttribute( const std::string & tagName, const std::string & attrName, const std::string & attrValue, int _item )
410 {
411  DOMNode * the_tag = document -> getElementsByTagName( XMLProcessor::_toXMLCh( tagName ) ) -> item( _item );
412  DOMElement * _tag = (DOMElement *)the_tag;
413  _tag -> setAttribute( XMLProcessor::_toXMLCh( attrName ), XMLProcessor::_toXMLCh( attrValue ) );
414  return the_tag;
415 }
416 
417 DOMNode * XMLDOMBlock::setTagAttribute( DOMElement * _elem, const std::string & tagName, const std::string & attrName, const std::string & attrValue, int _item )
418 {
419  if (!_elem) return 0;
420  DOMNode * the_tag = _elem -> getElementsByTagName( XMLProcessor::_toXMLCh( tagName ) ) -> item( _item );
421  if (the_tag){
422  DOMElement * _tag = (DOMElement *)the_tag;
423  _tag -> setAttribute( XMLProcessor::_toXMLCh( attrName ), XMLProcessor::_toXMLCh( attrValue ) );
424  }
425  return the_tag;
426 }
427 
428 DOMNode * XMLDOMBlock::setTagAttribute( const std::string & tagName, const std::string & attrName, const int & attrValue, int _item )
429 {
430  DOMNode * the_tag = document -> getElementsByTagName( XMLProcessor::_toXMLCh( tagName ) ) -> item( _item );
431  DOMElement * _tag = (DOMElement *)the_tag;
432  _tag -> setAttribute( XMLProcessor::_toXMLCh( attrName ), XMLProcessor::_toXMLCh( attrValue ) );
433  return the_tag;
434 }
435 
436 DOMNode * XMLDOMBlock::setTagAttribute( DOMElement * _elem, const std::string & tagName, const std::string & attrName, const int & attrValue, int _item )
437 {
438  if (!_elem) return 0;
439  DOMNode * the_tag = _elem -> getElementsByTagName( XMLProcessor::_toXMLCh( tagName ) ) -> item( _item );
440  if (the_tag){
441  DOMElement * _tag = (DOMElement *)the_tag;
442  _tag -> setAttribute( XMLProcessor::_toXMLCh( attrName ), XMLProcessor::_toXMLCh( attrValue ) );
443  }
444  return the_tag;
445 }
446 
447 string XMLDOMBlock::getTimestamp( time_t _time )
448 {
449  char timebuf[50];
450  //strftime( timebuf, 50, "%c", gmtime( &_time ) );
451  strftime( timebuf, 50, "%Y-%m-%d %H:%M:%S.0", gmtime( &_time ) );
452  std::string creationstamp = timebuf;
453 
454  return creationstamp;
455 }
456 
457 
458 
459 
460 std::string & XMLDOMBlock::getString( void )
461 {
462  return getString( this->getDocument() );
463 }
464 
465 
466 
467 
468 std::string & XMLDOMBlock::getString( DOMNode * _node )
469 {
470  if (the_string) delete the_string;
471  std::string _target = "string";
472  the_string = new std::string( XMLString::transcode( theProcessor->serializeDOM(_node,_target) ) );
473  return (*the_string);
474 }
475 
476 
477 //
478 //_____ following removed as a xalan-c component_____________________
479 //
480 /*
481 int XMLDOMBlock::read_xml_file_xalan( std::string filename ){
482  theSourceTreeInit = new XalanSourceTreeInit();
483  theDOMSupport = new XalanSourceTreeDOMSupport();
484  theLiaison = new XalanSourceTreeParserLiaison(*theDOMSupport);
485  const XalanDOMString theFileName(filename.c_str());
486  theInputSource = new LocalFileInputSource(theFileName.c_str());
487  theDocument = theLiaison->parseXMLStream(*theInputSource);
488  assert(theDocument != 0);
489  thePrefixResolver = new XalanDocumentPrefixResolver(theDocument);
490  theEvaluator = new XPathEvaluator;
491  return 0;
492 }
493 */
494 
495 //
496 //_____ following removed as a xalan-c component_____________________
497 //
498 /*
499 const XObjectPtr XMLDOMBlock::eval_xpath( std::string context, std::string expression ){
500  XalanNode* const theContextNode = theEvaluator->selectSingleNode(
501  *theDOMSupport,
502  theDocument,
503  XalanDOMString(context.c_str()).c_str(),
504  *thePrefixResolver);
505  if (theContextNode == 0){
506  //std::cerr << "Warning -- No nodes matched the location path " << context << std::endl;
507  XObjectPtr _null;
508  return _null;
509  }
510 
511 
512  const XObjectPtr theResult(
513  theEvaluator->evaluate(
514  *theDOMSupport,
515  theContextNode,
516  XalanDOMString(expression.c_str()).c_str(),
517  *thePrefixResolver));
518  return theResult;
519 }
520 */
521 
522 
523 
524 DOMElement * XMLDOMBlock::add_element(DOMElement * parent, XMLCh * tagname, XMLCh * value){
525  DOMElement * _elem = document -> createElement( tagname );
526  parent -> appendChild(_elem);
527  DOMText * _value = document -> createTextNode(value);
528  _elem->appendChild(_value);
529  return _elem;
530 }
531 
532 
int i
Definition: DBlmapReader.cc:9
int write(std::string target="stdout")
Definition: XMLDOMBlock.cc:323
#define _length
Definition: Pythia6jets.h:4
list parent
Definition: dbtoconf.py:74
std::string getTimestamp(time_t _time)
Definition: XMLDOMBlock.cc:447
int init
Definition: HydjetWrapper.h:63
DOMDocument * getDocumentConst(void) const
Definition: XMLDOMBlock.cc:318
std::string & getString(void)
Definition: XMLDOMBlock.cc:460
DOMDocument * getDocument(void)
Definition: XMLDOMBlock.cc:313
virtual ~XMLDOMBlock()
Definition: XMLDOMBlock.cc:330
DOMDocument * getNewDocument(std::string xmlFileName)
Definition: XMLDOMBlock.cc:267
XMLDOMBlock & operator+=(const XMLDOMBlock &other)
Definition: XMLDOMBlock.cc:36
int init(std::string _root)
Definition: XMLDOMBlock.cc:168
static XMLCh * _toXMLCh(std::string temp)
Definition: XMLProcessor.h:185
DOMElement * add_element(DOMElement *parent, XMLCh *tagname, XMLCh *value)
Definition: XMLDOMBlock.cc:524
void parse(InputSource &_source)
Definition: XMLDOMBlock.cc:123
const char * getTagValue(const std::string &tagName, int _item=0, DOMDocument *_document=NULL)
Definition: XMLDOMBlock.cc:351
tuple cout
Definition: gather_cfg.py:121
DOMNode * setTagValue(const std::string &tagName, const std::string &tagValue, int _item=0, DOMDocument *_document=NULL)
Definition: XMLDOMBlock.cc:365
static XMLProcessor * getInstance()
Definition: XMLProcessor.h:147
Helper class to handle FWLite file input sources.
DOMNode * setTagAttribute(const std::string &tagName, const std::string &attrName, const std::string &attrValue, int _item=0)
Definition: XMLDOMBlock.cc:409
const char * getTagAttribute(const std::string &tagName, const std::string &attrName, int _item=0)
Definition: XMLDOMBlock.cc:401