CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_1/src/CondTools/Ecal/src/EcalADCToGeVXMLTranslator.cc

Go to the documentation of this file.
00001 #include <iostream>
00002 #include <sstream>
00003 #include <fstream>
00004 #include <xercesc/dom/DOMNode.hpp>
00005 #include <xercesc/dom/DOM.hpp>
00006 #include <xercesc/parsers/XercesDOMParser.hpp>
00007 #include <xercesc/util/PlatformUtils.hpp>
00008 #include <xercesc/util/XMLString.hpp>
00009 #include <xercesc/sax/SAXException.hpp>
00010 #include <xercesc/framework/LocalFileFormatTarget.hpp>
00011 
00012 
00013 #include "CondFormats/EcalObjects/interface/EcalADCToGeVConstant.h"
00014 #include "CondTools/Ecal/interface/EcalADCToGeVXMLTranslator.h"
00015 #include "CondTools/Ecal/interface/DOMHelperFunctions.h"
00016 
00017 using namespace XERCES_CPP_NAMESPACE;
00018 using namespace xuti;
00019 using namespace std;
00020 
00021  
00022 
00023 int  EcalADCToGeVXMLTranslator::readXML(const std::string& filename, 
00024                                         EcalCondHeader& header,
00025                                         EcalADCToGeVConstant& record){
00026 
00027  
00028   XMLPlatformUtils::Initialize();
00029 
00030   XercesDOMParser* parser = new XercesDOMParser;
00031   parser->setValidationScheme( XercesDOMParser::Val_Never );
00032   parser->setDoNamespaces( false );
00033   parser->setDoSchema( false );
00034   
00035   parser->parse(filename.c_str());
00036 
00037   DOMDocument* xmlDoc = parser->getDocument();
00038   if (!xmlDoc) {
00039     std::cout << "EcalADCToGeVXMLTranslator::Error parsing document" << std::endl;
00040     return -1;
00041   }
00042   
00043   // Get the top-level element
00044   DOMElement* elementRoot = xmlDoc->getDocumentElement();
00045 
00046   xuti::readHeader(elementRoot, header);
00047 
00048   
00049   DOMNode * barrelnode = xuti::getChildNode(elementRoot,Barrel_tag);
00050   DOMNode * endcapnode = xuti::getChildNode(elementRoot,Endcap_tag);
00051   
00052   double barrelvalue=0;
00053   double endcapvalue=0;
00054 
00055   xuti::GetNodeData(barrelnode,barrelvalue);
00056   xuti::GetNodeData(endcapnode,endcapvalue);
00057 
00058   record.setEBValue(barrelvalue); 
00059   record.setEEValue(endcapvalue);
00060 
00061 
00062   delete parser;
00063   XMLPlatformUtils::Terminate();
00064   return 0;
00065 
00066 }
00067 
00068 
00069 
00070 
00071 
00072 int EcalADCToGeVXMLTranslator::writeXML(const std::string& filename, 
00073                                         const EcalCondHeader& header,
00074                                         const EcalADCToGeVConstant& record){
00075   std::fstream fs(filename.c_str(),ios::out);
00076   fs<< dumpXML(header,record);
00077   return 0;  
00078  
00079 }
00080 
00081 std::string EcalADCToGeVXMLTranslator::dumpXML(const EcalCondHeader& header,
00082                                           const EcalADCToGeVConstant& record){
00083 
00084   XMLPlatformUtils::Initialize();
00085   
00086   DOMImplementation*  impl =
00087     DOMImplementationRegistry::getDOMImplementation(fromNative("LS").c_str());
00088   
00089   DOMWriter* writer =static_cast<DOMImplementationLS*>(impl)->createDOMWriter( );
00090   writer->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true);
00091   
00092   DOMDocumentType* doctype = impl->createDocumentType(fromNative("XML").c_str(), 0, 0 );
00093   DOMDocument *    doc = 
00094     impl->createDocument( 0, fromNative(ADCToGeVConstant_tag).c_str(), doctype );
00095 
00096 
00097   doc->setEncoding(fromNative("UTF-8").c_str() );
00098   doc->setStandalone(true);
00099   doc->setVersion(fromNative("1.0").c_str() );
00100     
00101   DOMElement* root = doc->getDocumentElement();
00102  
00103   xuti::writeHeader(root,header);
00104    
00105   xuti::WriteNodeWithValue(root,Barrel_tag,record.getEBValue());
00106   xuti::WriteNodeWithValue(root,Endcap_tag,record.getEEValue());
00107 
00108   std::string dump= toNative(writer->writeToString(*root)); 
00109   doc->release();
00110 
00111   //   XMLPlatformUtils::Terminate();
00112 
00113   return dump;
00114 }
00115