Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:47:18

0001 // -*- C++ -*-
0002 //
0003 // Package:     XMLTools
0004 // Class  :     XMLDOMBlock
0005 //
0006 // Implementation:
0007 //     <Notes on implementation>
0008 //
0009 // Original Author:  Gena Kukartsev
0010 //         Created:  Thu Sep 27 01:43:42 CEST 2007
0011 //
0012 
0013 // system include files
0014 #include <iostream>
0015 #include <string>
0016 #include <ctime>
0017 #include <xercesc/parsers/XercesDOMParser.hpp>
0018 #include <xercesc/sax/HandlerBase.hpp>
0019 #include <xercesc/dom/DOM.hpp>
0020 
0021 //
0022 //_____ following removed as a xalan-c component_____________________
0023 //
0024 //#include <xalanc/XPath/XObject.hpp>
0025 
0026 XERCES_CPP_NAMESPACE_USE
0027 using namespace std;
0028 
0029 // user include files
0030 #include "CalibCalorimetry/HcalTPGAlgos/interface/XMLDOMBlock.h"
0031 #include "CalibCalorimetry/HcalTPGAlgos/interface/XMLProcessor.h"
0032 
0033 XMLDOMBlock& XMLDOMBlock::operator+=(const XMLDOMBlock& other) {
0034   DOMNodeList* _children = other.getDocumentConst()->getDocumentElement()->getChildNodes();
0035   int _length = _children->getLength();
0036   //std::cout << "Children nodes:" << _length << std::endl;
0037   DOMNode* _node;
0038   for (int i = 0; i != _length; i++) {
0039     _node = _children->item(i);
0040     DOMNode* i_node = this->getDocument()->importNode(_node, true);
0041     this->getDocument()->getDocumentElement()->appendChild(i_node);
0042   }
0043   return *this;
0044 }
0045 
0046 XMLDOMBlock::XMLDOMBlock() {
0047   //std::cout << "XMLDOMBlock (or derived): default constructor called - this is meaningless!" << std::endl;
0048   //std::cout << "XMLDOMBlock (or derived): use yourClass( loaderBaseConfig & ) instead." << std::endl;
0049   init("ROOT");
0050 }
0051 
0052 XMLDOMBlock::XMLDOMBlock(std::string _root, int rootElementName) {
0053   //std::cout << "XMLDOMBlock (or derived): default constructor called - this is meaningless!" << std::endl;
0054   //std::cout << "XMLDOMBlock (or derived): use yourClass( loaderBaseConfig & ) instead." << std::endl;
0055   init(_root);
0056 }
0057 
0058 XMLDOMBlock::XMLDOMBlock(InputSource& _source) {
0059   //
0060   //_____ following removed as a xalan-c component_____________________
0061   //
0062   /*
0063   // xalan objects initialization
0064   theSourceTreeInit = 0;
0065   theDOMSupport = 0;
0066   theLiaison = 0;
0067   theInputSource = 0;
0068   theDocument = 0;
0069   thePrefixResolver = 0;
0070   theEvaluator = 0;
0071   */
0072 
0073   theProcessor = XMLProcessor::getInstance();
0074 
0075   //theFileName = xmlFileName;
0076 
0077   // initialize the parser
0078   parser = new XercesDOMParser();
0079   parser->setValidationScheme(XercesDOMParser::Val_Always);
0080   parser->setDoNamespaces(true);  // optional
0081 
0082   errHandler = (ErrorHandler*)new HandlerBase();
0083   parser->setErrorHandler(errHandler);
0084 
0085   // parse the input xml file
0086   try {
0087     parser->parse(_source);
0088   } catch (const XMLException& toCatch) {
0089     char* message = XMLString::transcode(toCatch.getMessage());
0090     std::cout << "Exception message is: \n" << message << "\n";
0091     XMLString::release(&message);
0092     //return -1;
0093   } catch (const DOMException& toCatch) {
0094     char* message = XMLString::transcode(toCatch.msg);
0095     std::cout << "Exception message is: \n" << message << "\n";
0096     XMLString::release(&message);
0097     //return -1;
0098   } catch (...) {
0099     std::cout << "Unexpected Exception \n";
0100     //return -1;
0101   }
0102 
0103   //get the XML document
0104   document = parser->getDocument();
0105 }
0106 
0107 void XMLDOMBlock::parse(InputSource& _source) {
0108   theProcessor = XMLProcessor::getInstance();
0109 
0110   //theFileName = xmlFileName;
0111 
0112   // initialize the parser
0113   parser = new XercesDOMParser();
0114   parser->setValidationScheme(XercesDOMParser::Val_Always);
0115   parser->setDoNamespaces(true);  // optional
0116 
0117   errHandler = (ErrorHandler*)new HandlerBase();
0118   parser->setErrorHandler(errHandler);
0119 
0120   // parse the input xml file
0121   try {
0122     parser->parse(_source);
0123   } catch (const XMLException& toCatch) {
0124     char* message = XMLString::transcode(toCatch.getMessage());
0125     std::cout << "Exception message is: \n" << message << "\n";
0126     XMLString::release(&message);
0127     //return -1;
0128   } catch (const DOMException& toCatch) {
0129     char* message = XMLString::transcode(toCatch.msg);
0130     std::cout << "Exception message is: \n" << message << "\n";
0131     XMLString::release(&message);
0132     //return -1;
0133   } catch (...) {
0134     std::cout << "Unexpected Exception \n";
0135     //return -1;
0136   }
0137 
0138   //get the XML document
0139   document = parser->getDocument();
0140 }
0141 
0142 int XMLDOMBlock::init(std::string _root) {
0143   theProcessor = XMLProcessor::getInstance();
0144 
0145   //theFileName = xmlFileName;
0146 
0147   // initialize the parser
0148   parser = new XercesDOMParser();
0149   parser->setValidationScheme(XercesDOMParser::Val_Always);
0150   parser->setDoNamespaces(true);  // optional
0151 
0152   errHandler = (ErrorHandler*)new HandlerBase();
0153   parser->setErrorHandler(errHandler);
0154 
0155   DOMImplementation* impl = DOMImplementation::getImplementation();
0156 
0157   document = impl->createDocument(nullptr,  // root element namespace URI.
0158                                   //XMLString::transcode("ROOT"), // root element name
0159                                   XMLProcessor::_toXMLCh(_root),  // root element name
0160                                   nullptr);                       // document type object (DTD).
0161 
0162   the_string = nullptr;
0163 
0164   //
0165   //_____ following removed as a xalan-c component_____________________
0166   //
0167   /*
0168   // xalan objects initialization
0169   theSourceTreeInit = 0;
0170   theDOMSupport = 0;
0171   theLiaison = 0;
0172   theInputSource = 0;
0173   theDocument = 0;
0174   thePrefixResolver = 0;
0175   theEvaluator = 0;
0176   */
0177   return 0;
0178 }
0179 
0180 XMLDOMBlock::XMLDOMBlock(std::string xmlFileName) {
0181   //
0182   //_____ following removed as a xalan-c component_____________________
0183   //
0184   /*
0185   // xalan objects initialization
0186   theSourceTreeInit = 0;
0187   theDOMSupport = 0;
0188   theLiaison = 0;
0189   theInputSource = 0;
0190   theDocument = 0;
0191   thePrefixResolver = 0;
0192   theEvaluator = 0;
0193   */
0194 
0195   theProcessor = XMLProcessor::getInstance();
0196 
0197   theFileName = xmlFileName;
0198 
0199   // initialize the parser
0200   parser = new XercesDOMParser();
0201   parser->setValidationScheme(XercesDOMParser::Val_Always);
0202   parser->setDoNamespaces(true);  // optional
0203 
0204   errHandler = (ErrorHandler*)new HandlerBase();
0205   parser->setErrorHandler(errHandler);
0206 
0207   // parse the input xml file
0208   try {
0209     parser->parse(theFileName.c_str());
0210   } catch (const XMLException& toCatch) {
0211     char* message = XMLString::transcode(toCatch.getMessage());
0212     std::cout << "Exception message is: \n" << message << "\n";
0213     XMLString::release(&message);
0214     //return -1;
0215   } catch (const DOMException& toCatch) {
0216     char* message = XMLString::transcode(toCatch.msg);
0217     std::cout << "Exception message is: \n" << message << "\n";
0218     XMLString::release(&message);
0219     //return -1;
0220   } catch (...) {
0221     std::cout << "Unexpected Exception \n";
0222     //return -1;
0223   }
0224 
0225   //get the XML document
0226   document = parser->getDocument();
0227 }
0228 
0229 DOMDocument* XMLDOMBlock::getNewDocument(std::string xmlFileName) {
0230   delete document;
0231 
0232   theProcessor = XMLProcessor::getInstance();
0233 
0234   theFileName = xmlFileName;
0235 
0236   // initialize the parser
0237   parser = new XercesDOMParser();
0238   parser->setValidationScheme(XercesDOMParser::Val_Always);
0239   parser->setDoNamespaces(true);  // optional
0240 
0241   errHandler = (ErrorHandler*)new HandlerBase();
0242   parser->setErrorHandler(errHandler);
0243 
0244   // parse the input xml file
0245   try {
0246     parser->parse(theFileName.c_str());
0247   } catch (const XMLException& toCatch) {
0248     char* message = XMLString::transcode(toCatch.getMessage());
0249     std::cout << "Exception message is: \n" << message << "\n";
0250     XMLString::release(&message);
0251     //return -1;
0252   } catch (const DOMException& toCatch) {
0253     char* message = XMLString::transcode(toCatch.msg);
0254     std::cout << "Exception message is: \n" << message << "\n";
0255     XMLString::release(&message);
0256     //return -1;
0257   } catch (...) {
0258     std::cout << "Unexpected Exception \n";
0259     //return -1;
0260   }
0261 
0262   //get the XML document
0263   document = parser->getDocument();
0264 
0265   return document;
0266 }
0267 
0268 DOMDocument* XMLDOMBlock::getDocument(void) { return document; }
0269 
0270 DOMDocument* XMLDOMBlock::getDocumentConst(void) const { return document; }
0271 
0272 int XMLDOMBlock::write(std::string target) {
0273   theProcessor->write(this, target);
0274 
0275   return 0;
0276 }
0277 
0278 XMLDOMBlock::~XMLDOMBlock() {
0279   delete parser;
0280   delete errHandler;
0281   //if (the_string) delete the_string;
0282 
0283   //
0284   //_____ following removed as a xalan-c component_____________________
0285   //
0286   /*
0287   // delete xalan objects
0288   delete theSourceTreeInit;
0289   delete theDOMSupport;
0290   delete theLiaison;
0291   delete theInputSource;
0292   //delete theDocument; // noneed to delete - belongs to theLiaison
0293   delete thePrefixResolver;
0294   delete theEvaluator;
0295   */
0296 }
0297 
0298 const char* XMLDOMBlock::getTagValue(const std::string& tagName, int _item, DOMDocument* _document) {
0299   if (!_document)
0300     _document = document;
0301   const char* _result = XMLString::transcode(
0302       _document->getElementsByTagName(XMLProcessor::_toXMLCh(tagName))->item(_item)->getFirstChild()->getNodeValue());
0303   return _result;
0304 }
0305 
0306 const char* XMLDOMBlock::getTagValue(const std::string& tagName, int _item, DOMElement* _document) {
0307   if (!_document)
0308     return nullptr;
0309   const char* _result = XMLString::transcode(
0310       _document->getElementsByTagName(XMLProcessor::_toXMLCh(tagName))->item(_item)->getFirstChild()->getNodeValue());
0311   return _result;
0312 }
0313 
0314 DOMNode* XMLDOMBlock::setTagValue(const std::string& tagName,
0315                                   const std::string& tagValue,
0316                                   int _item,
0317                                   DOMDocument* _document) {
0318   if (!_document)
0319     _document = document;
0320   DOMNode* the_tag = _document->getElementsByTagName(XMLProcessor::_toXMLCh(tagName))->item(_item);
0321   the_tag->getFirstChild()->setNodeValue(XMLProcessor::_toXMLCh(tagValue));
0322   return the_tag;
0323 }
0324 
0325 DOMNode* XMLDOMBlock::setTagValue(DOMElement* _elem,
0326                                   const std::string& tagName,
0327                                   const std::string& tagValue,
0328                                   int _item) {
0329   if (!_elem)
0330     return nullptr;
0331   DOMNode* the_tag = _elem->getElementsByTagName(XMLProcessor::_toXMLCh(tagName))->item(_item);
0332   if (the_tag) {
0333     the_tag->getFirstChild()->setNodeValue(XMLProcessor::_toXMLCh(tagValue));
0334   }
0335   return the_tag;
0336 }
0337 
0338 DOMNode* XMLDOMBlock::setTagValue(const std::string& tagName, const int& tagValue, int _item, DOMDocument* _document) {
0339   if (!_document)
0340     _document = document;
0341   DOMNode* the_tag = _document->getElementsByTagName(XMLProcessor::_toXMLCh(tagName))->item(_item);
0342   the_tag->getFirstChild()->setNodeValue(XMLProcessor::_toXMLCh(tagValue));
0343   return the_tag;
0344 }
0345 
0346 DOMNode* XMLDOMBlock::setTagValue(DOMElement* _elem, const std::string& tagName, const int& tagValue, int _item) {
0347   if (!_elem)
0348     return nullptr;
0349   DOMNode* the_tag = _elem->getElementsByTagName(XMLProcessor::_toXMLCh(tagName))->item(_item);
0350   if (the_tag) {
0351     the_tag->getFirstChild()->setNodeValue(XMLProcessor::_toXMLCh(tagValue));
0352   }
0353   return the_tag;
0354 }
0355 
0356 const char* XMLDOMBlock::getTagAttribute(const std::string& tagName, const std::string& attrName, int _item) {
0357   DOMElement* _tag = (DOMElement*)(document->getElementsByTagName(XMLProcessor::_toXMLCh(tagName))->item(_item));
0358   const char* _result = XMLString::transcode(_tag->getAttribute(XMLProcessor::_toXMLCh(attrName)));
0359 
0360   return _result;
0361 }
0362 
0363 DOMNode* XMLDOMBlock::setTagAttribute(const std::string& tagName,
0364                                       const std::string& attrName,
0365                                       const std::string& attrValue,
0366                                       int _item) {
0367   DOMNode* the_tag = document->getElementsByTagName(XMLProcessor::_toXMLCh(tagName))->item(_item);
0368   DOMElement* _tag = (DOMElement*)the_tag;
0369   _tag->setAttribute(XMLProcessor::_toXMLCh(attrName), XMLProcessor::_toXMLCh(attrValue));
0370   return the_tag;
0371 }
0372 
0373 DOMNode* XMLDOMBlock::setTagAttribute(DOMElement* _elem,
0374                                       const std::string& tagName,
0375                                       const std::string& attrName,
0376                                       const std::string& attrValue,
0377                                       int _item) {
0378   if (!_elem)
0379     return nullptr;
0380   DOMNode* the_tag = _elem->getElementsByTagName(XMLProcessor::_toXMLCh(tagName))->item(_item);
0381   if (the_tag) {
0382     DOMElement* _tag = (DOMElement*)the_tag;
0383     _tag->setAttribute(XMLProcessor::_toXMLCh(attrName), XMLProcessor::_toXMLCh(attrValue));
0384   }
0385   return the_tag;
0386 }
0387 
0388 DOMNode* XMLDOMBlock::setTagAttribute(const std::string& tagName,
0389                                       const std::string& attrName,
0390                                       const int& attrValue,
0391                                       int _item) {
0392   DOMNode* the_tag = document->getElementsByTagName(XMLProcessor::_toXMLCh(tagName))->item(_item);
0393   DOMElement* _tag = (DOMElement*)the_tag;
0394   _tag->setAttribute(XMLProcessor::_toXMLCh(attrName), XMLProcessor::_toXMLCh(attrValue));
0395   return the_tag;
0396 }
0397 
0398 DOMNode* XMLDOMBlock::setTagAttribute(
0399     DOMElement* _elem, const std::string& tagName, const std::string& attrName, const int& attrValue, int _item) {
0400   if (!_elem)
0401     return nullptr;
0402   DOMNode* the_tag = _elem->getElementsByTagName(XMLProcessor::_toXMLCh(tagName))->item(_item);
0403   if (the_tag) {
0404     DOMElement* _tag = (DOMElement*)the_tag;
0405     _tag->setAttribute(XMLProcessor::_toXMLCh(attrName), XMLProcessor::_toXMLCh(attrValue));
0406   }
0407   return the_tag;
0408 }
0409 
0410 string XMLDOMBlock::getTimestamp(time_t _time) {
0411   char timebuf[50];
0412   //strftime( timebuf, 50, "%c", gmtime( &_time ) );
0413   strftime(timebuf, 50, "%Y-%m-%d %H:%M:%S.0", gmtime(&_time));
0414   std::string creationstamp = timebuf;
0415 
0416   return creationstamp;
0417 }
0418 
0419 std::string& XMLDOMBlock::getString(void) { return getString(this->getDocument()); }
0420 
0421 std::string& XMLDOMBlock::getString(DOMNode* _node) {
0422   if (the_string)
0423     delete the_string;
0424   std::string _target = "string";
0425   the_string = new std::string(XMLString::transcode(theProcessor->serializeDOM(_node, _target)));
0426   return (*the_string);
0427 }
0428 
0429 //
0430 //_____ following removed as a xalan-c component_____________________
0431 //
0432 /*
0433 int XMLDOMBlock::read_xml_file_xalan( std::string filename ){
0434   theSourceTreeInit = new XalanSourceTreeInit();
0435   theDOMSupport = new XalanSourceTreeDOMSupport();
0436   theLiaison = new XalanSourceTreeParserLiaison(*theDOMSupport);
0437   const XalanDOMString theFileName(filename.c_str());
0438   theInputSource = new LocalFileInputSource(theFileName.c_str());
0439   theDocument = theLiaison->parseXMLStream(*theInputSource);
0440   assert(theDocument != 0);
0441   thePrefixResolver = new XalanDocumentPrefixResolver(theDocument);
0442   theEvaluator = new XPathEvaluator;
0443   return 0;
0444 }
0445 */
0446 
0447 //
0448 //_____ following removed as a xalan-c component_____________________
0449 //
0450 /*
0451 const XObjectPtr XMLDOMBlock::eval_xpath( std::string context, std::string expression ){
0452   XalanNode* const theContextNode = theEvaluator->selectSingleNode(
0453                                    *theDOMSupport,
0454                                   theDocument,
0455                                   XalanDOMString(context.c_str()).c_str(),
0456                                   *thePrefixResolver);
0457   if (theContextNode == 0){
0458     //std::cerr << "Warning -- No nodes matched the location path " << context << std::endl;
0459     XObjectPtr _null;
0460     return _null;
0461   }
0462 
0463   
0464   const XObjectPtr theResult(
0465                  theEvaluator->evaluate(
0466                             *theDOMSupport,
0467                             theContextNode,
0468                             XalanDOMString(expression.c_str()).c_str(),
0469                             *thePrefixResolver));
0470   return theResult;
0471 }
0472 */
0473 
0474 DOMElement* XMLDOMBlock::add_element(DOMElement* parent, XMLCh* tagname, XMLCh* value) {
0475   DOMElement* _elem = document->createElement(tagname);
0476   parent->appendChild(_elem);
0477   DOMText* _value = document->createTextNode(value);
0478   _elem->appendChild(_value);
0479   return _elem;
0480 }