File indexing completed on 2023-03-17 10:41:54
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
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
0023
0024
0025
0026 XERCES_CPP_NAMESPACE_USE
0027 using namespace std;
0028
0029
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
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
0048
0049 init("ROOT");
0050 }
0051
0052 XMLDOMBlock::XMLDOMBlock(std::string _root, int rootElementName) {
0053
0054
0055 init(_root);
0056 }
0057
0058 XMLDOMBlock::XMLDOMBlock(InputSource& _source) {
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073 theProcessor = XMLProcessor::getInstance();
0074
0075
0076
0077
0078 parser = new XercesDOMParser();
0079 parser->setValidationScheme(XercesDOMParser::Val_Always);
0080 parser->setDoNamespaces(true);
0081
0082 errHandler = (ErrorHandler*)new HandlerBase();
0083 parser->setErrorHandler(errHandler);
0084
0085
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
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
0098 } catch (...) {
0099 std::cout << "Unexpected Exception \n";
0100
0101 }
0102
0103
0104 document = parser->getDocument();
0105 }
0106
0107 void XMLDOMBlock::parse(InputSource& _source) {
0108 theProcessor = XMLProcessor::getInstance();
0109
0110
0111
0112
0113 parser = new XercesDOMParser();
0114 parser->setValidationScheme(XercesDOMParser::Val_Always);
0115 parser->setDoNamespaces(true);
0116
0117 errHandler = (ErrorHandler*)new HandlerBase();
0118 parser->setErrorHandler(errHandler);
0119
0120
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
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
0133 } catch (...) {
0134 std::cout << "Unexpected Exception \n";
0135
0136 }
0137
0138
0139 document = parser->getDocument();
0140 }
0141
0142 int XMLDOMBlock::init(std::string _root) {
0143 theProcessor = XMLProcessor::getInstance();
0144
0145
0146
0147
0148 parser = new XercesDOMParser();
0149 parser->setValidationScheme(XercesDOMParser::Val_Always);
0150 parser->setDoNamespaces(true);
0151
0152 errHandler = (ErrorHandler*)new HandlerBase();
0153 parser->setErrorHandler(errHandler);
0154
0155 DOMImplementation* impl = DOMImplementation::getImplementation();
0156
0157 document = impl->createDocument(nullptr,
0158
0159 XMLProcessor::_toXMLCh(_root),
0160 nullptr);
0161
0162 the_string = nullptr;
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177 return 0;
0178 }
0179
0180 XMLDOMBlock::XMLDOMBlock(std::string xmlFileName) {
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195 theProcessor = XMLProcessor::getInstance();
0196
0197 theFileName = xmlFileName;
0198
0199
0200 parser = new XercesDOMParser();
0201 parser->setValidationScheme(XercesDOMParser::Val_Always);
0202 parser->setDoNamespaces(true);
0203
0204 errHandler = (ErrorHandler*)new HandlerBase();
0205 parser->setErrorHandler(errHandler);
0206
0207
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
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
0220 } catch (...) {
0221 std::cout << "Unexpected Exception \n";
0222
0223 }
0224
0225
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
0237 parser = new XercesDOMParser();
0238 parser->setValidationScheme(XercesDOMParser::Val_Always);
0239 parser->setDoNamespaces(true);
0240
0241 errHandler = (ErrorHandler*)new HandlerBase();
0242 parser->setErrorHandler(errHandler);
0243
0244
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
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
0257 } catch (...) {
0258 std::cout << "Unexpected Exception \n";
0259
0260 }
0261
0262
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
0282
0283
0284
0285
0286
0287
0288
0289
0290
0291
0292
0293
0294
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
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
0431
0432
0433
0434
0435
0436
0437
0438
0439
0440
0441
0442
0443
0444
0445
0446
0447
0448
0449
0450
0451
0452
0453
0454
0455
0456
0457
0458
0459
0460
0461
0462
0463
0464
0465
0466
0467
0468
0469
0470
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 }