00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include <iostream>
00016 #include <string>
00017 #include<time.h>
00018 #include <xercesc/parsers/XercesDOMParser.hpp>
00019 #include <xercesc/sax/HandlerBase.hpp>
00020 #include <xercesc/dom/DOM.hpp>
00021
00022
00023
00024
00025
00026
00027 XERCES_CPP_NAMESPACE_USE
00028 using namespace std;
00029
00030
00031 #include "CalibCalorimetry/HcalTPGAlgos/interface/XMLDOMBlock.h"
00032 #include "CalibCalorimetry/HcalTPGAlgos/interface/XMLProcessor.h"
00033
00034
00035
00036 XMLDOMBlock & XMLDOMBlock::operator+=( const XMLDOMBlock & other)
00037 {
00038 DOMNodeList * _children = other.getDocumentConst()->getDocumentElement()->getChildNodes();
00039 int _length = _children->getLength();
00040
00041 DOMNode * _node;
00042 for(int i=0;i!=_length;i++){
00043 _node = _children->item(i);
00044 DOMNode * i_node = this->getDocument()->importNode(_node,true);
00045 this->getDocument()->getDocumentElement()->appendChild(i_node);
00046 }
00047 return *this;
00048 }
00049
00050
00051 XMLDOMBlock::XMLDOMBlock()
00052 {
00053
00054
00055 init( "ROOT" );
00056 }
00057
00058 XMLDOMBlock::XMLDOMBlock( std::string _root, int rootElementName )
00059 {
00060
00061
00062 init( _root );
00063 }
00064
00065
00066 XMLDOMBlock::XMLDOMBlock( InputSource & _source )
00067 {
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082 theProcessor = XMLProcessor::getInstance();
00083
00084
00085
00086
00087 parser = new XercesDOMParser();
00088 parser->setValidationScheme(XercesDOMParser::Val_Always);
00089 parser->setDoNamespaces(true);
00090
00091 errHandler = (ErrorHandler*) new HandlerBase();
00092 parser->setErrorHandler(errHandler);
00093
00094
00095 try
00096 {
00097 parser->parse( _source );
00098 }
00099 catch (const XMLException& toCatch) {
00100 char* message = XMLString::transcode(toCatch.getMessage());
00101 std::cout << "Exception message is: \n"
00102 << message << "\n";
00103 XMLString::release(&message);
00104
00105 }
00106 catch (const DOMException& toCatch) {
00107 char* message = XMLString::transcode(toCatch.msg);
00108 std::cout << "Exception message is: \n"
00109 << message << "\n";
00110 XMLString::release(&message);
00111
00112 }
00113 catch (...) {
00114 std::cout << "Unexpected Exception \n" ;
00115
00116 }
00117
00118
00119 document = parser -> getDocument();
00120 }
00121
00122
00123 void XMLDOMBlock::parse( InputSource & _source )
00124 {
00125
00126 theProcessor = XMLProcessor::getInstance();
00127
00128
00129
00130
00131 parser = new XercesDOMParser();
00132 parser->setValidationScheme(XercesDOMParser::Val_Always);
00133 parser->setDoNamespaces(true);
00134
00135 errHandler = (ErrorHandler*) new HandlerBase();
00136 parser->setErrorHandler(errHandler);
00137
00138
00139 try
00140 {
00141 parser->parse( _source );
00142 }
00143 catch (const XMLException& toCatch) {
00144 char* message = XMLString::transcode(toCatch.getMessage());
00145 std::cout << "Exception message is: \n"
00146 << message << "\n";
00147 XMLString::release(&message);
00148
00149 }
00150 catch (const DOMException& toCatch) {
00151 char* message = XMLString::transcode(toCatch.msg);
00152 std::cout << "Exception message is: \n"
00153 << message << "\n";
00154 XMLString::release(&message);
00155
00156 }
00157 catch (...) {
00158 std::cout << "Unexpected Exception \n" ;
00159
00160 }
00161
00162
00163 document = parser -> getDocument();
00164 }
00165
00166
00167
00168 int XMLDOMBlock::init( std::string _root )
00169 {
00170 theProcessor = XMLProcessor::getInstance();
00171
00172
00173
00174
00175 parser = new XercesDOMParser();
00176 parser->setValidationScheme(XercesDOMParser::Val_Always);
00177 parser->setDoNamespaces(true);
00178
00179 errHandler = (ErrorHandler*) new HandlerBase();
00180 parser->setErrorHandler(errHandler);
00181
00182 DOMImplementation* impl = DOMImplementation::getImplementation();
00183
00184 document = impl->createDocument(
00185 0,
00186
00187 XMLProcessor::_toXMLCh(_root),
00188 0);
00189
00190 the_string = 0;
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205 return 0;
00206 }
00207
00208
00209
00210 XMLDOMBlock::XMLDOMBlock( std::string xmlFileName )
00211 {
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226 theProcessor = XMLProcessor::getInstance();
00227
00228 theFileName = xmlFileName;
00229
00230
00231 parser = new XercesDOMParser();
00232 parser->setValidationScheme(XercesDOMParser::Val_Always);
00233 parser->setDoNamespaces(true);
00234
00235 errHandler = (ErrorHandler*) new HandlerBase();
00236 parser->setErrorHandler(errHandler);
00237
00238
00239 try
00240 {
00241 parser->parse( theFileName . c_str() );
00242 }
00243 catch (const XMLException& toCatch) {
00244 char* message = XMLString::transcode(toCatch.getMessage());
00245 std::cout << "Exception message is: \n"
00246 << message << "\n";
00247 XMLString::release(&message);
00248
00249 }
00250 catch (const DOMException& toCatch) {
00251 char* message = XMLString::transcode(toCatch.msg);
00252 std::cout << "Exception message is: \n"
00253 << message << "\n";
00254 XMLString::release(&message);
00255
00256 }
00257 catch (...) {
00258 std::cout << "Unexpected Exception \n" ;
00259
00260 }
00261
00262
00263 document = parser -> getDocument();
00264
00265 }
00266
00267 DOMDocument * XMLDOMBlock::getNewDocument( std::string xmlFileName )
00268 {
00269 delete document;
00270
00271 theProcessor = XMLProcessor::getInstance();
00272
00273 theFileName = xmlFileName;
00274
00275
00276 parser = new XercesDOMParser();
00277 parser->setValidationScheme(XercesDOMParser::Val_Always);
00278 parser->setDoNamespaces(true);
00279
00280 errHandler = (ErrorHandler*) new HandlerBase();
00281 parser->setErrorHandler(errHandler);
00282
00283
00284 try
00285 {
00286 parser->parse( theFileName . c_str() );
00287 }
00288 catch (const XMLException& toCatch) {
00289 char* message = XMLString::transcode(toCatch.getMessage());
00290 std::cout << "Exception message is: \n"
00291 << message << "\n";
00292 XMLString::release(&message);
00293
00294 }
00295 catch (const DOMException& toCatch) {
00296 char* message = XMLString::transcode(toCatch.msg);
00297 std::cout << "Exception message is: \n"
00298 << message << "\n";
00299 XMLString::release(&message);
00300
00301 }
00302 catch (...) {
00303 std::cout << "Unexpected Exception \n" ;
00304
00305 }
00306
00307
00308 document = parser -> getDocument();
00309
00310 return document;
00311 }
00312
00313 DOMDocument * XMLDOMBlock::getDocument( void )
00314 {
00315 return document;
00316 }
00317
00318 DOMDocument * XMLDOMBlock::getDocumentConst( void ) const
00319 {
00320 return document;
00321 }
00322
00323 int XMLDOMBlock::write( std::string target )
00324 {
00325 theProcessor -> write( this, target );
00326
00327 return 0;
00328 }
00329
00330 XMLDOMBlock::~XMLDOMBlock()
00331 {
00332 delete parser;
00333 delete errHandler;
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349 }
00350
00351 const char * XMLDOMBlock::getTagValue( const std::string & tagName, int _item, DOMDocument * _document )
00352 {
00353 if (!_document) _document = document;
00354 const char * _result = XMLString::transcode( _document -> getElementsByTagName( XMLProcessor::_toXMLCh( tagName ) ) -> item( _item ) -> getFirstChild()-> getNodeValue() );
00355 return _result;
00356 }
00357
00358 const char * XMLDOMBlock::getTagValue( const std::string & tagName, int _item, DOMElement * _document )
00359 {
00360 if (!_document) return 0;
00361 const char * _result = XMLString::transcode( _document -> getElementsByTagName( XMLProcessor::_toXMLCh( tagName ) ) -> item( _item ) -> getFirstChild()-> getNodeValue() );
00362 return _result;
00363 }
00364
00365 DOMNode * XMLDOMBlock::setTagValue( const std::string & tagName, const std::string & tagValue, int _item, DOMDocument * _document )
00366 {
00367 if (!_document) _document = document;
00368 DOMNode * the_tag = _document -> getElementsByTagName( XMLProcessor::_toXMLCh( tagName ) ) -> item( _item );
00369 the_tag -> getFirstChild() -> setNodeValue( XMLProcessor::_toXMLCh( tagValue ) );
00370 return the_tag;
00371 }
00372
00373 DOMNode * XMLDOMBlock::setTagValue(DOMElement * _elem, const std::string & tagName, const std::string & tagValue, int _item )
00374 {
00375 if (!_elem) return 0;
00376 DOMNode * the_tag = _elem -> getElementsByTagName( XMLProcessor::_toXMLCh( tagName ) ) -> item( _item );
00377 if (the_tag){
00378 the_tag -> getFirstChild() -> setNodeValue( XMLProcessor::_toXMLCh( tagValue ) );
00379 }
00380 return the_tag;
00381 }
00382
00383 DOMNode * XMLDOMBlock::setTagValue( const std::string & tagName, const int & tagValue, int _item, DOMDocument * _document )
00384 {
00385 if (!_document) _document = document;
00386 DOMNode * the_tag = _document -> getElementsByTagName( XMLProcessor::_toXMLCh( tagName ) ) -> item( _item );
00387 the_tag -> getFirstChild() -> setNodeValue( XMLProcessor::_toXMLCh( tagValue ) );
00388 return the_tag;
00389 }
00390
00391 DOMNode * XMLDOMBlock::setTagValue( DOMElement * _elem, const std::string & tagName, const int & tagValue, int _item )
00392 {
00393 if (!_elem) return 0;
00394 DOMNode * the_tag = _elem -> getElementsByTagName( XMLProcessor::_toXMLCh( tagName ) ) -> item( _item );
00395 if(the_tag){
00396 the_tag -> getFirstChild() -> setNodeValue( XMLProcessor::_toXMLCh( tagValue ) );
00397 }
00398 return the_tag;
00399 }
00400
00401 const char * XMLDOMBlock::getTagAttribute( const std::string & tagName, const std::string & attrName, int _item )
00402 {
00403 DOMElement * _tag = (DOMElement *)(document -> getElementsByTagName( XMLProcessor::_toXMLCh( tagName ) ) -> item( _item ));
00404 const char * _result = XMLString::transcode( _tag -> getAttribute( XMLProcessor::_toXMLCh( attrName ) ) );
00405
00406 return _result;
00407 }
00408
00409 DOMNode * XMLDOMBlock::setTagAttribute( const std::string & tagName, const std::string & attrName, const std::string & attrValue, int _item )
00410 {
00411 DOMNode * the_tag = document -> getElementsByTagName( XMLProcessor::_toXMLCh( tagName ) ) -> item( _item );
00412 DOMElement * _tag = (DOMElement *)the_tag;
00413 _tag -> setAttribute( XMLProcessor::_toXMLCh( attrName ), XMLProcessor::_toXMLCh( attrValue ) );
00414 return the_tag;
00415 }
00416
00417 DOMNode * XMLDOMBlock::setTagAttribute( DOMElement * _elem, const std::string & tagName, const std::string & attrName, const std::string & attrValue, int _item )
00418 {
00419 if (!_elem) return 0;
00420 DOMNode * the_tag = _elem -> getElementsByTagName( XMLProcessor::_toXMLCh( tagName ) ) -> item( _item );
00421 if (the_tag){
00422 DOMElement * _tag = (DOMElement *)the_tag;
00423 _tag -> setAttribute( XMLProcessor::_toXMLCh( attrName ), XMLProcessor::_toXMLCh( attrValue ) );
00424 }
00425 return the_tag;
00426 }
00427
00428 DOMNode * XMLDOMBlock::setTagAttribute( const std::string & tagName, const std::string & attrName, const int & attrValue, int _item )
00429 {
00430 DOMNode * the_tag = document -> getElementsByTagName( XMLProcessor::_toXMLCh( tagName ) ) -> item( _item );
00431 DOMElement * _tag = (DOMElement *)the_tag;
00432 _tag -> setAttribute( XMLProcessor::_toXMLCh( attrName ), XMLProcessor::_toXMLCh( attrValue ) );
00433 return the_tag;
00434 }
00435
00436 DOMNode * XMLDOMBlock::setTagAttribute( DOMElement * _elem, const std::string & tagName, const std::string & attrName, const int & attrValue, int _item )
00437 {
00438 if (!_elem) return 0;
00439 DOMNode * the_tag = _elem -> getElementsByTagName( XMLProcessor::_toXMLCh( tagName ) ) -> item( _item );
00440 if (the_tag){
00441 DOMElement * _tag = (DOMElement *)the_tag;
00442 _tag -> setAttribute( XMLProcessor::_toXMLCh( attrName ), XMLProcessor::_toXMLCh( attrValue ) );
00443 }
00444 return the_tag;
00445 }
00446
00447 string XMLDOMBlock::getTimestamp( time_t _time )
00448 {
00449 char timebuf[50];
00450
00451 strftime( timebuf, 50, "%Y-%m-%d %H:%M:%S.0", gmtime( &_time ) );
00452 std::string creationstamp = timebuf;
00453
00454 return creationstamp;
00455 }
00456
00457
00458
00459
00460 std::string & XMLDOMBlock::getString( void )
00461 {
00462 return getString( this->getDocument() );
00463 }
00464
00465
00466
00467
00468 std::string & XMLDOMBlock::getString( DOMNode * _node )
00469 {
00470 if (the_string) delete the_string;
00471 std::string _target = "string";
00472 the_string = new std::string( XMLString::transcode( theProcessor->serializeDOM(_node,_target) ) );
00473 return (*the_string);
00474 }
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524 DOMElement * XMLDOMBlock::add_element(DOMElement * parent, XMLCh * tagname, XMLCh * value){
00525 DOMElement * _elem = document -> createElement( tagname );
00526 parent -> appendChild(_elem);
00527 DOMText * _value = document -> createTextNode(value);
00528 _elem->appendChild(_value);
00529 return _elem;
00530 }
00531
00532