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