00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include <string>
00016 #include <xercesc/parsers/XercesDOMParser.hpp>
00017 #include <xercesc/sax/HandlerBase.hpp>
00018 #include <xercesc/dom/DOM.hpp>
00019 #include<time.h>
00020
00021 #if defined(XERCES_NEW_IOSTREAMS)
00022 #include <iostream>
00023 #else
00024 #include <iostream.h>
00025 #endif
00026
00027 XERCES_CPP_NAMESPACE_USE
00028 using namespace std;
00029
00030
00031 #include "CaloOnlineTools/HcalOnlineDb/interface/XMLDOMBlock.h"
00032 #include "CaloOnlineTools/HcalOnlineDb/interface/XMLProcessor.h"
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 XMLDOMBlock::XMLDOMBlock()
00047 {
00048
00049
00050 init( "ROOT" );
00051 }
00052
00053 XMLDOMBlock::XMLDOMBlock( string _root, int rootElementName )
00054 {
00055
00056
00057 init( _root );
00058 }
00059
00060
00061 XMLDOMBlock::XMLDOMBlock( InputSource & _source )
00062 {
00063
00064 theProcessor = XMLProcessor::getInstance();
00065
00066
00067
00068
00069 parser = new XercesDOMParser();
00070 parser->setValidationScheme(XercesDOMParser::Val_Always);
00071 parser->setDoNamespaces(true);
00072
00073 errHandler = (ErrorHandler*) new HandlerBase();
00074 parser->setErrorHandler(errHandler);
00075
00076
00077 try
00078 {
00079 parser->parse( _source );
00080 }
00081 catch (const XMLException& toCatch) {
00082 char* message = XMLString::transcode(toCatch.getMessage());
00083 cout << "Exception message is: \n"
00084 << message << "\n";
00085 XMLString::release(&message);
00086
00087 }
00088 catch (const DOMException& toCatch) {
00089 char* message = XMLString::transcode(toCatch.msg);
00090 cout << "Exception message is: \n"
00091 << message << "\n";
00092 XMLString::release(&message);
00093
00094 }
00095 catch (...) {
00096 cout << "Unexpected Exception \n" ;
00097
00098 }
00099
00100
00101 document = parser -> getDocument();
00102 }
00103
00104
00105
00106 int XMLDOMBlock::init( string _root )
00107 {
00108 theProcessor = XMLProcessor::getInstance();
00109
00110
00111
00112
00113 parser = new XercesDOMParser();
00114 parser->setValidationScheme(XercesDOMParser::Val_Always);
00115 parser->setDoNamespaces(true);
00116
00117 errHandler = (ErrorHandler*) new HandlerBase();
00118 parser->setErrorHandler(errHandler);
00119
00120 DOMImplementation* impl = DOMImplementation::getImplementation();
00121
00122 document = impl->createDocument(
00123 0,
00124
00125 XMLProcessor::_toXMLCh(_root),
00126 0);
00127
00128 the_string = 0;
00129
00130 return 0;
00131 }
00132
00133
00134
00135 XMLDOMBlock::XMLDOMBlock( string xmlFileName )
00136 {
00137
00138 theProcessor = XMLProcessor::getInstance();
00139
00140 theFileName = xmlFileName;
00141
00142
00143 parser = new XercesDOMParser();
00144 parser->setValidationScheme(XercesDOMParser::Val_Always);
00145 parser->setDoNamespaces(true);
00146
00147 errHandler = (ErrorHandler*) new HandlerBase();
00148 parser->setErrorHandler(errHandler);
00149
00150
00151 try
00152 {
00153 parser->parse( theFileName . c_str() );
00154 }
00155 catch (const XMLException& toCatch) {
00156 char* message = XMLString::transcode(toCatch.getMessage());
00157 cout << "Exception message is: \n"
00158 << message << "\n";
00159 XMLString::release(&message);
00160
00161 }
00162 catch (const DOMException& toCatch) {
00163 char* message = XMLString::transcode(toCatch.msg);
00164 cout << "Exception message is: \n"
00165 << message << "\n";
00166 XMLString::release(&message);
00167
00168 }
00169 catch (...) {
00170 cout << "Unexpected Exception \n" ;
00171
00172 }
00173
00174
00175 document = parser -> getDocument();
00176
00177 }
00178
00179 DOMDocument * XMLDOMBlock::getNewDocument( string xmlFileName )
00180 {
00181 delete document;
00182
00183 theProcessor = XMLProcessor::getInstance();
00184
00185 theFileName = xmlFileName;
00186
00187
00188 parser = new XercesDOMParser();
00189 parser->setValidationScheme(XercesDOMParser::Val_Always);
00190 parser->setDoNamespaces(true);
00191
00192 errHandler = (ErrorHandler*) new HandlerBase();
00193 parser->setErrorHandler(errHandler);
00194
00195
00196 try
00197 {
00198 parser->parse( theFileName . c_str() );
00199 }
00200 catch (const XMLException& toCatch) {
00201 char* message = XMLString::transcode(toCatch.getMessage());
00202 cout << "Exception message is: \n"
00203 << message << "\n";
00204 XMLString::release(&message);
00205
00206 }
00207 catch (const DOMException& toCatch) {
00208 char* message = XMLString::transcode(toCatch.msg);
00209 cout << "Exception message is: \n"
00210 << message << "\n";
00211 XMLString::release(&message);
00212
00213 }
00214 catch (...) {
00215 cout << "Unexpected Exception \n" ;
00216
00217 }
00218
00219
00220 document = parser -> getDocument();
00221
00222 return document;
00223 }
00224
00225 DOMDocument * XMLDOMBlock::getDocument( void )
00226 {
00227 return document;
00228 }
00229
00230 int XMLDOMBlock::write( string target )
00231 {
00232 theProcessor -> write( this, target );
00233
00234 return 0;
00235 }
00236
00237 XMLDOMBlock::~XMLDOMBlock()
00238 {
00239 delete parser;
00240 delete errHandler;
00241
00242 }
00243
00244 const char * XMLDOMBlock::getTagValue( const string & tagName, int _item, DOMDocument * _document )
00245 {
00246 if (!_document) _document = document;
00247 const char * _result = XMLString::transcode( _document -> getElementsByTagName( XMLProcessor::_toXMLCh( tagName ) ) -> item( _item ) -> getFirstChild()-> getNodeValue() );
00248 return _result;
00249 }
00250
00251 int XMLDOMBlock::setTagValue( const string & tagName, const string & tagValue, int _item, DOMDocument * _document )
00252 {
00253 if (!_document) _document = document;
00254 _document -> getElementsByTagName( XMLProcessor::_toXMLCh( tagName ) ) -> item( _item ) -> getFirstChild() -> setNodeValue( XMLProcessor::_toXMLCh( tagValue ) );
00255 return 0;
00256 }
00257
00258 int XMLDOMBlock::setTagValue( const string & tagName, const int & tagValue, int _item, DOMDocument * _document )
00259 {
00260 if (!_document) _document = document;
00261 _document -> getElementsByTagName( XMLProcessor::_toXMLCh( tagName ) ) -> item( _item ) -> getFirstChild() -> setNodeValue( XMLProcessor::_toXMLCh( tagValue ) );
00262 return 0;
00263 }
00264
00265 const char * XMLDOMBlock::getTagAttribute( const string & tagName, const string & attrName, int _item )
00266 {
00267 DOMElement * _tag = (DOMElement *)(document -> getElementsByTagName( XMLProcessor::_toXMLCh( tagName ) ) -> item( _item ));
00268 const char * _result = XMLString::transcode( _tag -> getAttribute( XMLProcessor::_toXMLCh( attrName ) ) );
00269
00270 return _result;
00271 }
00272
00273 int XMLDOMBlock::setTagAttribute( const string & tagName, const string & attrName, const string & attrValue, int _item )
00274 {
00275 DOMElement * _tag = (DOMElement *)(document -> getElementsByTagName( XMLProcessor::_toXMLCh( tagName ) ) -> item( _item ));
00276 _tag -> setAttribute( XMLProcessor::_toXMLCh( attrName ), XMLProcessor::_toXMLCh( attrValue ) );
00277
00278 return 0;
00279 }
00280
00281 string XMLDOMBlock::getTimestamp( time_t _time )
00282 {
00283 char timebuf[50];
00284
00285 strftime( timebuf, 50, "%Y-%m-%d %H:%M:%S.0", gmtime( &_time ) );
00286 string creationstamp = timebuf;
00287
00288 return creationstamp;
00289 }
00290
00291
00292
00293
00294 std::string & XMLDOMBlock::getString( void )
00295 {
00296 return getString( this->getDocument() );
00297 }
00298
00299
00300
00301
00302 std::string & XMLDOMBlock::getString( DOMNode * _node )
00303 {
00304 if (the_string) delete the_string;
00305 std::string _target = "string";
00306 the_string = new std::string( XMLString::transcode( theProcessor->serializeDOM(_node,_target) ) );
00307 return (*the_string);
00308 }
00309
00310
00311