CMS 3D CMS Logo

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