CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/CalibCalorimetry/HcalTPGAlgos/src/XMLDOMBlock.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:     XMLTools
00004 // Class  :     XMLDOMBlock
00005 // 
00006 // Implementation:
00007 //     <Notes on implementation>
00008 //
00009 // Original Author:  Gena Kukartsev
00010 //         Created:  Thu Sep 27 01:43:42 CEST 2007
00011 // $Id: XMLDOMBlock.cc,v 1.5 2010/08/06 20:24:03 wmtan Exp $
00012 //
00013 
00014 // system include files
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 //_____ following removed as a xalan-c component_____________________
00024 //
00025 //#include <xalanc/XPath/XObject.hpp>
00026 
00027 XERCES_CPP_NAMESPACE_USE 
00028 using namespace std;
00029 
00030 // user include files
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   //std::cout << "Children nodes:" << _length << std::endl;
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   //std::cout << "XMLDOMBlock (or derived): default constructor called - this is meaningless!" << std::endl;
00054   //std::cout << "XMLDOMBlock (or derived): use yourClass( loaderBaseConfig & ) instead." << std::endl;
00055   init( "ROOT" );
00056 }
00057 
00058 XMLDOMBlock::XMLDOMBlock( std::string _root, int rootElementName )
00059 {
00060   //std::cout << "XMLDOMBlock (or derived): default constructor called - this is meaningless!" << std::endl;
00061   //std::cout << "XMLDOMBlock (or derived): use yourClass( loaderBaseConfig & ) instead." << std::endl;
00062   init( _root );
00063 }
00064 
00065 
00066 XMLDOMBlock::XMLDOMBlock( InputSource & _source )
00067 {
00068   //
00069   //_____ following removed as a xalan-c component_____________________
00070   //
00071   /*
00072   // xalan objects initialization
00073   theSourceTreeInit = 0;
00074   theDOMSupport = 0;
00075   theLiaison = 0;
00076   theInputSource = 0;
00077   theDocument = 0;
00078   thePrefixResolver = 0;
00079   theEvaluator = 0;
00080   */
00081 
00082   theProcessor = XMLProcessor::getInstance();
00083 
00084   //theFileName = xmlFileName;
00085 
00086   // initialize the parser
00087   parser = new XercesDOMParser();
00088   parser->setValidationScheme(XercesDOMParser::Val_Always);    
00089   parser->setDoNamespaces(true);    // optional
00090   
00091   errHandler = (ErrorHandler*) new HandlerBase();
00092   parser->setErrorHandler(errHandler);
00093 
00094   // parse the input xml file
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     //return -1;
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     //return -1;
00112   }
00113   catch (...) {
00114     std::cout << "Unexpected Exception \n" ;
00115     //return -1;
00116   }
00117 
00118   //get the XML document
00119   document = parser -> getDocument();
00120 }
00121 
00122 
00123 void XMLDOMBlock::parse( InputSource & _source )
00124 {
00125 
00126   theProcessor = XMLProcessor::getInstance();
00127 
00128   //theFileName = xmlFileName;
00129 
00130   // initialize the parser
00131   parser = new XercesDOMParser();
00132   parser->setValidationScheme(XercesDOMParser::Val_Always);    
00133   parser->setDoNamespaces(true);    // optional
00134   
00135   errHandler = (ErrorHandler*) new HandlerBase();
00136   parser->setErrorHandler(errHandler);
00137 
00138   // parse the input xml file
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     //return -1;
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     //return -1;
00156   }
00157   catch (...) {
00158     std::cout << "Unexpected Exception \n" ;
00159     //return -1;
00160   }
00161 
00162   //get the XML document
00163   document = parser -> getDocument();
00164 }
00165 
00166 
00167 
00168 int XMLDOMBlock::init( std::string _root )
00169 {
00170   theProcessor = XMLProcessor::getInstance();
00171 
00172   //theFileName = xmlFileName;
00173 
00174   // initialize the parser
00175   parser = new XercesDOMParser();
00176   parser->setValidationScheme(XercesDOMParser::Val_Always);    
00177   parser->setDoNamespaces(true);    // optional
00178   
00179   errHandler = (ErrorHandler*) new HandlerBase();
00180   parser->setErrorHandler(errHandler);
00181 
00182   DOMImplementation* impl =  DOMImplementation::getImplementation();
00183   
00184   document = impl->createDocument(
00185                                   0,                      // root element namespace URI.
00186                                   //XMLString::transcode("ROOT"), // root element name
00187                                   XMLProcessor::_toXMLCh(_root), // root element name
00188                                   0);                     // document type object (DTD).
00189 
00190   the_string = 0;
00191 
00192   //
00193   //_____ following removed as a xalan-c component_____________________
00194   //
00195   /*
00196   // xalan objects initialization
00197   theSourceTreeInit = 0;
00198   theDOMSupport = 0;
00199   theLiaison = 0;
00200   theInputSource = 0;
00201   theDocument = 0;
00202   thePrefixResolver = 0;
00203   theEvaluator = 0;
00204   */
00205   return 0;
00206 }
00207 
00208 
00209 
00210 XMLDOMBlock::XMLDOMBlock( std::string xmlFileName )
00211 {
00212   //
00213   //_____ following removed as a xalan-c component_____________________
00214   //
00215   /*
00216   // xalan objects initialization
00217   theSourceTreeInit = 0;
00218   theDOMSupport = 0;
00219   theLiaison = 0;
00220   theInputSource = 0;
00221   theDocument = 0;
00222   thePrefixResolver = 0;
00223   theEvaluator = 0;
00224   */
00225 
00226   theProcessor = XMLProcessor::getInstance();
00227 
00228   theFileName = xmlFileName;
00229 
00230   // initialize the parser
00231   parser = new XercesDOMParser();
00232   parser->setValidationScheme(XercesDOMParser::Val_Always);    
00233   parser->setDoNamespaces(true);    // optional
00234   
00235   errHandler = (ErrorHandler*) new HandlerBase();
00236   parser->setErrorHandler(errHandler);
00237 
00238   // parse the input xml file
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     //return -1;
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     //return -1;
00256   }
00257   catch (...) {
00258     std::cout << "Unexpected Exception \n" ;
00259     //return -1;
00260   }
00261 
00262   //get the XML document
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   // initialize the parser
00276   parser = new XercesDOMParser();
00277   parser->setValidationScheme(XercesDOMParser::Val_Always);    
00278   parser->setDoNamespaces(true);    // optional
00279   
00280   errHandler = (ErrorHandler*) new HandlerBase();
00281   parser->setErrorHandler(errHandler);
00282 
00283   // parse the input xml file
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     //return -1;
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     //return -1;
00301   }
00302   catch (...) {
00303     std::cout << "Unexpected Exception \n" ;
00304     //return -1;
00305   }
00306 
00307   //get the XML document
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   //if (the_string) delete the_string;
00335 
00336   //
00337   //_____ following removed as a xalan-c component_____________________
00338   //
00339   /*
00340   // delete xalan objects
00341   delete theSourceTreeInit;
00342   delete theDOMSupport;
00343   delete theLiaison;
00344   delete theInputSource;
00345   //delete theDocument; // noneed to delete - belongs to theLiaison
00346   delete thePrefixResolver;
00347   delete theEvaluator;
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   //strftime( timebuf, 50, "%c", gmtime( &_time ) );
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 //_____ following removed as a xalan-c component_____________________
00479 //
00480 /*
00481 int XMLDOMBlock::read_xml_file_xalan( std::string filename ){
00482   theSourceTreeInit = new XalanSourceTreeInit();
00483   theDOMSupport = new XalanSourceTreeDOMSupport();
00484   theLiaison = new XalanSourceTreeParserLiaison(*theDOMSupport);
00485   const XalanDOMString theFileName(filename.c_str());
00486   theInputSource = new LocalFileInputSource(theFileName.c_str());
00487   theDocument = theLiaison->parseXMLStream(*theInputSource);
00488   assert(theDocument != 0);
00489   thePrefixResolver = new XalanDocumentPrefixResolver(theDocument);
00490   theEvaluator = new XPathEvaluator;
00491   return 0;
00492 }
00493 */
00494 
00495 //
00496 //_____ following removed as a xalan-c component_____________________
00497 //
00498 /*
00499 const XObjectPtr XMLDOMBlock::eval_xpath( std::string context, std::string expression ){
00500   XalanNode* const theContextNode = theEvaluator->selectSingleNode(
00501                                                                    *theDOMSupport,
00502                                                                   theDocument,
00503                                                                   XalanDOMString(context.c_str()).c_str(),
00504                                                                   *thePrefixResolver);
00505   if (theContextNode == 0){
00506     //std::cerr << "Warning -- No nodes matched the location path " << context << std::endl;
00507     XObjectPtr _null;
00508     return _null;
00509   }
00510 
00511   
00512   const XObjectPtr theResult(
00513                              theEvaluator->evaluate(
00514                                                     *theDOMSupport,
00515                                                     theContextNode,
00516                                                     XalanDOMString(expression.c_str()).c_str(),
00517                                                     *thePrefixResolver));
00518   return theResult;
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