00001 #include "otsdaq-core/ConfigurationInterface/ConfigurationHandler.h"
00002 #include "otsdaq-core/ConfigurationInterface/TimeFormatter.h"
00003
00004 #include "otsdaq-core/XmlUtilities/DOMTreeErrorReporter.h"
00005 #include "otsdaq-core/XmlUtilities/ConvertToXML.h"
00006 #include "otsdaq-core/XmlUtilities/ConvertFromXML.h"
00007 #include "otsdaq-core/ConfigurationDataFormats/ConfigurationBase.h"
00008 #include "otsdaq-core/Macros/CoutHeaderMacros.h"
00009
00010 #include <xercesc/parsers/XercesDOMParser.hpp>
00011 #include <xercesc/dom/DOMElement.hpp>
00012 #include <xercesc/dom/DOMImplementation.hpp>
00013 #include <xercesc/dom/DOMImplementationRegistry.hpp>
00014 #include <xercesc/dom/DOMNodeList.hpp>
00015 #include <xercesc/dom/DOMText.hpp>
00016 #include <xercesc/dom/DOMLSSerializer.hpp>
00017 #include <xercesc/dom/DOMLSOutput.hpp>
00018 #include <xercesc/util/XMLUni.hpp>
00019
00020
00021 #include <xercesc/util/OutOfMemoryException.hpp>
00022 #include <xercesc/framework/LocalFileFormatTarget.hpp>
00023
00024 #include <stdexcept>
00025 #include <iostream>
00026 #include <sstream>
00027 #include <sys/types.h>
00028
00029 #include <sys/stat.h>
00030 #include <errno.h>
00031
00032
00033 using namespace ots;
00034
00035 #undef __COUT_HDR__
00036 #define __COUT_HDR__ "ConfigHandler"
00037
00038
00039
00040 XMLCh* ConfigurationHandler::rootTag_ = 0;
00041 XMLCh* ConfigurationHandler::headerTag_ = 0;
00042 XMLCh* ConfigurationHandler::typeTag_ = 0;
00043 XMLCh* ConfigurationHandler::extensionTableNameTag_ = 0;
00044 XMLCh* ConfigurationHandler::nameTag_ = 0;
00045 XMLCh* ConfigurationHandler::runTag_ = 0;
00046 XMLCh* ConfigurationHandler::runTypeTag_ = 0;
00047 XMLCh* ConfigurationHandler::runNumberTag_ = 0;
00048 XMLCh* ConfigurationHandler::runBeginTimestampTag_ = 0;
00049 XMLCh* ConfigurationHandler::locationTag_ = 0;
00050 XMLCh* ConfigurationHandler::datasetTag_ = 0;
00051 XMLCh* ConfigurationHandler::versionTag_ = 0;
00052 XMLCh* ConfigurationHandler::commentDescriptionTag_ = 0;
00053 XMLCh* ConfigurationHandler::createdByUserTag_ = 0;
00054 XMLCh* ConfigurationHandler::partTag_ = 0;
00055 XMLCh* ConfigurationHandler::nameLabelTag_ = 0;
00056 XMLCh* ConfigurationHandler::kindOfPartTag_ = 0;
00057 XMLCh* ConfigurationHandler::dataTag_ = 0;
00058
00059
00060 ConfigurationHandler::ConfigurationHandler(void)
00061 {}
00062
00063
00064 ConfigurationHandler::~ConfigurationHandler(void)
00065 {}
00066
00067
00068 void ConfigurationHandler::initPlatform(void)
00069 {
00070 try
00071 {
00072 xercesc::XMLPlatformUtils::Initialize();
00073 }
00074 catch( xercesc::XMLException& e )
00075 {
00076 mf::LogError(__FILE__) << "XML toolkit initialization error: " << XML_TO_CHAR(e.getMessage()) << std::endl;
00077
00078 }
00079
00080 rootTag_ = xercesc::XMLString::transcode("ROOT");
00081 headerTag_ = xercesc::XMLString::transcode("HEADER");
00082 typeTag_ = xercesc::XMLString::transcode("TYPE");
00083 extensionTableNameTag_ = xercesc::XMLString::transcode("EXTENSION_TABLE_NAME");
00084 nameTag_ = xercesc::XMLString::transcode("NAME");
00085 runTag_ = xercesc::XMLString::transcode("RUN");
00086 runTypeTag_ = xercesc::XMLString::transcode("RUN_TYPE");
00087 runNumberTag_ = xercesc::XMLString::transcode("RUN_NUMBER");
00088 runBeginTimestampTag_ = xercesc::XMLString::transcode("RUN_BEGIN_TIMESTAMP");
00089 locationTag_ = xercesc::XMLString::transcode("LOCATION");
00090 datasetTag_ = xercesc::XMLString::transcode("DATA_SET");
00091 versionTag_ = xercesc::XMLString::transcode("VERSION");
00092 commentDescriptionTag_ = xercesc::XMLString::transcode("COMMENT_DESCRIPTION");
00093 createdByUserTag_ = xercesc::XMLString::transcode("CREATED_BY_USER");
00094 partTag_ = xercesc::XMLString::transcode("PART");
00095 nameLabelTag_ = xercesc::XMLString::transcode("NAME_LABEL");
00096 kindOfPartTag_ = xercesc::XMLString::transcode("KIND_OF_PART");
00097 dataTag_ = xercesc::XMLString::transcode("DATA");
00098 }
00099
00100
00101 void ConfigurationHandler::terminatePlatform(void)
00102 {
00103 try
00104 {
00105 xercesc::XMLString::release( &rootTag_ );
00106 xercesc::XMLString::release( &headerTag_ );
00107 xercesc::XMLString::release( &typeTag_ );
00108 xercesc::XMLString::release( &extensionTableNameTag_ );
00109 xercesc::XMLString::release( &nameTag_ );
00110 xercesc::XMLString::release( &runTag_ );
00111 xercesc::XMLString::release( &runTypeTag_ );
00112 xercesc::XMLString::release( &runNumberTag_ );
00113 xercesc::XMLString::release( &runBeginTimestampTag_ );
00114 xercesc::XMLString::release( &locationTag_ );
00115 xercesc::XMLString::release( &datasetTag_ );
00116 xercesc::XMLString::release( &versionTag_ );
00117 xercesc::XMLString::release( &commentDescriptionTag_ );
00118 xercesc::XMLString::release( &createdByUserTag_ );
00119 xercesc::XMLString::release( &partTag_ );
00120 xercesc::XMLString::release( &nameLabelTag_ );
00121 xercesc::XMLString::release( &kindOfPartTag_ );
00122 xercesc::XMLString::release( &dataTag_ );
00123 }
00124 catch( ... )
00125 {
00126 mf::LogError(__FILE__) << "Unknown exception encountered in TagNames destructor" << std::endl;
00127 }
00128
00129 try
00130 {
00131 xercesc::XMLPlatformUtils::Terminate();
00132 }
00133 catch( xercesc::XMLException& e )
00134 {
00135 mf::LogError(__FILE__) << "XML ttolkit teardown error: " << XML_TO_CHAR(e.getMessage()) << std::endl;
00136 }
00137 }
00138
00139
00140 bool ConfigurationHandler::validateNode(XMLCh* tagName, xercesc::DOMNode* node, const std::string& expectedValue)
00141 {
00142 if( node->getFirstChild() == 0 )
00143 {
00144 __COUT__<< "Tag " << XML_TO_CHAR(tagName) << " doesn't have a value!" << std::endl;
00145 return false;
00146 }
00147
00148 if( XML_TO_STRING(node->getFirstChild()->getNodeValue()) != expectedValue)
00149 {
00150 __COUT__<< "The tag " << XML_TO_CHAR(tagName) << " with value " << XML_TO_CHAR(node->getFirstChild()->getNodeValue())
00151 << " doesn't match the expected value " << expectedValue << std::endl;
00152 return false;
00153 }
00154
00155 return true;
00156 }
00157
00158
00159 xercesc::DOMNode* ConfigurationHandler::getNode(XMLCh* tagName, xercesc::DOMNode* parent, unsigned int itemNumber)
00160 {
00161 return getNode(tagName, dynamic_cast< xercesc::DOMElement* >(parent), itemNumber);
00162 }
00163
00164
00165 xercesc::DOMNode* ConfigurationHandler::getNode(XMLCh* tagName, xercesc::DOMElement* parent, unsigned int itemNumber)
00166 {
00167 xercesc::DOMNodeList* nodeList = parent->getElementsByTagName(tagName);
00168
00169 if( !nodeList )
00170 {
00171 throw(std::runtime_error( std::string("Can't find ") + XML_TO_STRING(tagName) + " tag!"));
00172 __COUT__ << (std::string("Can't find ") + XML_TO_STRING(tagName) + " tag!") << std::endl;
00173 }
00174
00175
00176
00177
00178 return nodeList->item(itemNumber);
00179 }
00180
00181
00182 xercesc::DOMElement* ConfigurationHandler::getElement(XMLCh* tagName, xercesc::DOMNode* parent, unsigned int itemNumber)
00183 {
00184 return dynamic_cast< xercesc::DOMElement* >(getNode(tagName,parent,itemNumber));
00185 }
00186
00187
00188 xercesc::DOMElement* ConfigurationHandler::getElement(XMLCh* tagName, xercesc::DOMElement* parent, unsigned int itemNumber)
00189 {
00190 return dynamic_cast< xercesc::DOMElement* >(getNode(tagName,parent,itemNumber));
00191 }
00192
00193
00194 void ConfigurationHandler::readXML(ConfigurationBase* configuration, ConfigurationVersion version)
00195 {
00196 readXML(*configuration,version);
00197 }
00198
00199
00200 void ConfigurationHandler::readXML(ConfigurationBase& configuration, ConfigurationVersion version)
00201 {
00202
00203 initPlatform();
00204 std::string configFile = getXMLFileName(configuration,version);
00205
00206 __COUT__ << "Reading: " << configFile << std::endl;
00207 __COUT__ << "Into View with Table Name: " <<
00208 configuration.getViewP()->getTableName() << std::endl;
00209 __COUT__ << "Into View with version: " <<
00210 configuration.getViewP()->getVersion() << " and version-to-read: " <<
00211 version << std::endl;
00212
00213 struct stat fileStatus;
00214
00215 if(stat(configFile.c_str(), &fileStatus) < 0)
00216 {
00217 __COUT__ << "Error reading path: " << configFile << std::endl;
00218 std::stringstream ss; ss << __MF_HDR__;
00219 if( errno == ENOENT )
00220 ss << ("Path file_name does not exist.");
00221 else if( errno == ENOTDIR )
00222 ss << ("A component of the path is not a directory.");
00223 else if( errno == ELOOP )
00224 ss << ("Too many symbolic links encountered while traversing the path.");
00225 else if( errno == EACCES )
00226 ss << ("Permission denied.");
00227 else if( errno == ENAMETOOLONG )
00228 ss << ("File name too long.");
00229 else
00230 ss << ("File can not be read.");
00231 ss << std::endl;
00232 __COUT_ERR__ << ss.str();
00233 throw std::runtime_error(ss.str());
00234 }
00235
00236 xercesc::XercesDOMParser* parser = new xercesc::XercesDOMParser;
00237
00238
00239 parser->setValidationScheme(xercesc::XercesDOMParser::Val_Auto);
00240 parser->setDoNamespaces ( true );
00241 parser->setDoSchema ( false );
00242
00243 parser->useCachedGrammarInParse ( false );
00244 DOMTreeErrorReporter* errorHandler = new DOMTreeErrorReporter() ;
00245 parser->setErrorHandler(errorHandler);
00246
00247
00248 try
00249 {
00250
00251 parser->parse( configFile.c_str() );
00252
00253
00254
00255 xercesc::DOMDocument* document = parser->getDocument();
00256
00257
00258 xercesc::DOMElement* elementRoot = document->getDocumentElement();
00259
00260 if( !elementRoot ) throw(std::runtime_error( "empty XML document" ));
00261
00262
00263 xercesc::DOMNode* headerNode = getNode(headerTag_, elementRoot, 0);
00264 if( !headerNode ) throw(std::runtime_error( std::string("The document is missing the mandatory tag: ") + XML_TO_STRING(headerTag_ )));
00265
00266
00267
00268 xercesc::DOMElement* typeElement = getElement(typeTag_, headerNode, 0);
00269 if( !typeElement ) throw(std::runtime_error( std::string("The document is missing the mandatory tag: ") + XML_TO_STRING(typeTag_ )));
00270 xercesc::DOMNode* extensionTableNameNode = getNode (extensionTableNameTag_, typeElement, 0);
00271 if(!validateNode(extensionTableNameTag_,extensionTableNameNode,configuration.getView().getTableName()))
00272 throw(std::runtime_error( std::string("The document is missing the mandatory tag: ") + XML_TO_STRING(extensionTableNameTag_ )));
00273 xercesc::DOMNode* nameNode = getNode (nameTag_, typeElement, 0);
00274 if(!validateNode(nameTag_,nameNode,configuration.getConfigurationName()))
00275 throw(std::runtime_error( std::string("The document is missing the mandatory tag: ") + XML_TO_STRING(nameTag_ )));
00276
00277
00278
00279
00280
00281 xercesc::DOMElement* runElement = getElement(runTag_, headerNode,0);
00282 if( !runElement ) throw(std::runtime_error( std::string("The document is missing the mandatory tag: ") + XML_TO_STRING(runTag_ )));
00283 xercesc::DOMNode* runTypeNode = getNode (runTypeTag_, runElement, 0);
00284 assert(validateNode(runTypeTag_,runTypeNode,configuration.getConfigurationName()));
00285 xercesc::DOMNode* runNumberNode = getNode (runNumberTag_, runElement, 0);
00286 if( !runNumberNode ) throw(std::runtime_error( std::string("The document is missing the mandatory tag: ") + XML_TO_STRING(runNumberTag_ )));
00287 xercesc::DOMNode* runBeginTimestampNode = getNode (runBeginTimestampTag_, runElement, 0);
00288 if( !runBeginTimestampNode ) throw(std::runtime_error( std::string("The document is missing the mandatory tag: ") + XML_TO_STRING(runBeginTimestampTag_ )));
00289 xercesc::DOMNode* locationNode = getNode (locationTag_, runElement, 0);
00290 if( !locationNode ) throw(std::runtime_error( std::string("The document is missing the mandatory tag: ") + XML_TO_STRING(locationTag_ )));
00291
00292
00293
00294
00295
00296
00297
00298 xercesc::DOMElement* datasetElement = getElement(datasetTag_, elementRoot, 0);
00299 if( !datasetElement ) throw(std::runtime_error( std::string("The document is missing the mandatory tag: ") + XML_TO_STRING(datasetTag_ )));
00300
00301
00302
00303 xercesc::DOMNode* partNode = getNode (partTag_, datasetElement, 0);
00304 xercesc::DOMNode* nameLabelNode = getNode (nameLabelTag_, partNode, 0);
00305 xercesc::DOMNode* kindOfPartNode = getNode (kindOfPartTag_, partNode, 0);
00306
00307
00308 xercesc::DOMNode* versionNode = getNode (versionTag_, datasetElement, 0);
00309
00310 if(versionNode->getFirstChild() == 0)
00311 {
00312 throw(std::runtime_error( std::string("Missing version tag: ") + XML_TO_CHAR(versionNode->getFirstChild()->getNodeValue())));
00313 }
00314 else
00315 {
00316 char tmpVersionStr[100];
00317 sprintf(tmpVersionStr, "%d", version.version());
00318 __COUT__ << version << "-" << XML_TO_CHAR(versionNode->getFirstChild()->getNodeValue()) << std::endl;
00319 if(strcmp(tmpVersionStr,XML_TO_CHAR(versionNode->getFirstChild()->getNodeValue())) != 0)
00320 throw(std::runtime_error( std::string("Mis-matched version tag: ") + XML_TO_CHAR(versionNode->getFirstChild()->getNodeValue()) +
00321 " vs " + tmpVersionStr));
00322 }
00323
00324 configuration.getViewP()->setVersion(XML_TO_CHAR(versionNode->getFirstChild()->getNodeValue()));
00325
00326 xercesc::DOMNode* commentDescriptionNode = getNode (commentDescriptionTag_, datasetElement, 0);
00327 if(commentDescriptionNode->getFirstChild() != 0)
00328 configuration.getViewP()->setComment(XML_TO_CHAR(commentDescriptionNode->getFirstChild()->getNodeValue()));
00329
00330 xercesc::DOMNode* createdByUserNode = getNode (createdByUserTag_, datasetElement, 0);
00331 if(createdByUserNode->getFirstChild() != 0)
00332 configuration.getViewP()->setAuthor(XML_TO_CHAR(createdByUserNode->getFirstChild()->getNodeValue()));
00333
00334
00335
00336 xercesc::DOMNodeList* dataNodeList = datasetElement->getElementsByTagName(dataTag_);
00337
00338 if( !dataNodeList )
00339 throw(std::runtime_error( std::string("Can't find ") + XML_TO_STRING(dataTag_) + " tag!"));
00340
00341
00342
00343
00344 if(!dataNodeList->getLength())
00345 {
00346 __SS__ << "Must be non-empty data set!";
00347 throw std::runtime_error(ss.str());
00348 }
00349
00350
00351
00352 configuration.getViewP()->resizeDataView(dataNodeList->getLength(), configuration.getView().getNumberOfColumns());
00353
00354 for( XMLSize_t row = 0; row < dataNodeList->getLength(); row++ )
00355 {
00356
00357 xercesc::DOMNodeList* columnNodeList = dataNodeList->item(row)->getChildNodes();
00358 unsigned int colNumber = 0;
00359
00360
00361 for( XMLSize_t col = 0; col < columnNodeList->getLength(); col++ )
00362 {
00363
00364
00365 if( !columnNodeList->item(col)->getNodeType() || columnNodeList->item(col)->getNodeType() != xercesc::DOMNode::ELEMENT_NODE )
00366 continue;
00367
00368 xercesc::DOMElement* columnElement = dynamic_cast< xercesc::DOMElement* >( columnNodeList->item(col) );
00369
00370 if( configuration.getView().getColumnInfo(colNumber).getStorageName() != XML_TO_STRING(columnElement->getTagName()))
00371 {
00372 std::stringstream error;
00373 error << __COUT_HDR__ << std::endl
00374 << "The column number " << colNumber
00375 << " named " << configuration.getView().getColumnInfo(colNumber).getStorageName()
00376 << " defined in the view " << configuration.getView().getTableName()
00377 << " doesn't match the file column order, since the "
00378 << colNumber+1 << (colNumber==0?"st":(colNumber==1?"nd":(colNumber==2?"rd":"th")))
00379 << " element found in the file at " << XML_TO_CHAR(dataTag_) << " tag number " << row
00380 << " is " << XML_TO_CHAR(columnElement->getTagName());
00381 mf::LogError(__FILE__) << error.str();
00382 throw(std::runtime_error( error.str() ));
00383 }
00384
00385
00386
00387
00388
00389
00390 configuration.getViewP()->setValueAsString(
00391 XML_TO_STRING(columnNodeList->item(col)->getFirstChild()->getNodeValue())
00392 , row
00393 , colNumber);
00394
00395 ++colNumber;
00396 }
00397 }
00398 }
00399 catch( xercesc::XMLException& e )
00400 {
00401 __COUT__ << "Error parsing file: " << configFile << std::endl;
00402 std::ostringstream errBuf;
00403 errBuf << "Error parsing file: " << XML_TO_CHAR(e.getMessage()) << std::flush;
00404 }
00405
00406 __COUT__ << "Done with configuration file: " << configFile << std::endl;
00407
00408 delete parser;
00409 delete errorHandler;
00410 terminatePlatform();
00411 }
00412
00413
00414
00415 std::string ConfigurationHandler::writeXML(const ConfigurationBase& configuration)
00416 {
00417 initPlatform();
00418
00419 std::string configFile = getXMLFileName(configuration,configuration.getViewVersion());
00420
00421
00422 xercesc::DOMImplementation* implementation = xercesc::DOMImplementationRegistry::getDOMImplementation(CONVERT_TO_XML("Core"));
00423 if (implementation != 0)
00424 {
00425 try
00426 {
00427
00428 xercesc::DOMDocument* document = implementation->createDocument(
00429 0,
00430 rootTag_,
00431 0);
00432
00433 xercesc::DOMElement* rootElement = document->getDocumentElement();
00434 rootElement->setAttribute(CONVERT_TO_XML("xmlns:xsi"),CONVERT_TO_XML("http://www.w3.org/2001/XMLSchema-instance"));
00435 rootElement->setAttribute(CONVERT_TO_XML("xsi:noNamespaceSchemaLocation"),CONVERT_TO_XML("ConfigurationBase.xsd"));
00436
00437
00438
00439 xercesc::DOMElement* headerElement = document->createElement(headerTag_);
00440 rootElement->appendChild(headerElement);
00441
00442
00443 xercesc::DOMElement* typeElement = document->createElement(typeTag_);
00444 headerElement->appendChild(typeElement);
00445
00446 xercesc::DOMElement* extensionTableNameElement = document->createElement(extensionTableNameTag_);
00447 typeElement->appendChild(extensionTableNameElement);
00448 xercesc::DOMText* extensionTableNameValue = document->createTextNode(CONVERT_TO_XML(configuration.getView().getTableName().c_str()));
00449 extensionTableNameElement->appendChild(extensionTableNameValue);
00450
00451 xercesc::DOMElement* nameElement = document->createElement(nameTag_);
00452 typeElement->appendChild(nameElement);
00453 xercesc::DOMText* nameValue = document->createTextNode(CONVERT_TO_XML(configuration.getConfigurationName().c_str()));
00454 nameElement->appendChild(nameValue);
00455
00456
00457
00458 xercesc::DOMElement* runElement = document->createElement(runTag_);
00459 headerElement->appendChild(runElement);
00460
00461 xercesc::DOMElement* runTypeElement = document->createElement(runTypeTag_);
00462 runElement->appendChild(runTypeElement);
00463 xercesc::DOMText* runTypeValue = document->createTextNode(CONVERT_TO_XML(configuration.getConfigurationName().c_str()));
00464 runTypeElement->appendChild(runTypeValue);
00465
00466 xercesc::DOMElement* runNumberElement = document->createElement(runNumberTag_);
00467 runElement->appendChild(runNumberElement);
00468 xercesc::DOMText* runNumberValue = document->createTextNode(CONVERT_TO_XML("1"));
00469 runNumberElement->appendChild(runNumberValue);
00470
00471 xercesc::DOMElement* runBeginTimestampElement = document->createElement(runBeginTimestampTag_);
00472 runElement->appendChild(runBeginTimestampElement);
00473 xercesc::DOMText* runBeginTimestampValue = document->createTextNode(CONVERT_TO_XML(TimeFormatter::getTime().c_str()));
00474 runBeginTimestampElement->appendChild(runBeginTimestampValue);
00475
00476 xercesc::DOMElement* locationElement = document->createElement(locationTag_);
00477 runElement->appendChild(locationElement);
00478 xercesc::DOMText* locationValue = document->createTextNode(CONVERT_TO_XML("CERN P5"));
00479 locationElement->appendChild(locationValue);
00480
00481
00482
00483 xercesc::DOMElement* datasetElement = document->createElement(datasetTag_);
00484 rootElement->appendChild(datasetElement);
00485
00486 xercesc::DOMElement* partElement = document->createElement(partTag_);
00487 datasetElement->appendChild(partElement);
00488
00489 xercesc::DOMElement* nameLabelElement = document->createElement(nameLabelTag_);
00490 partElement->appendChild(nameLabelElement);
00491 xercesc::DOMText* nameLabelValue = document->createTextNode(CONVERT_TO_XML("CMS--ROOT"));
00492 nameLabelElement->appendChild(nameLabelValue);
00493
00494 xercesc::DOMElement* kindOfPartElement = document->createElement(kindOfPartTag_);
00495 partElement->appendChild(kindOfPartElement);
00496 xercesc::DOMText* kindOfPartValue = document->createTextNode(CONVERT_TO_XML("Detector ROOT"));
00497 kindOfPartElement->appendChild(kindOfPartValue);
00498
00499 xercesc::DOMElement* versionElement = document->createElement(versionTag_);
00500 datasetElement->appendChild(versionElement);
00501 xercesc::DOMText* versionValue = document->createTextNode(CONVERT_TO_XML(configuration.getView().getVersion().version()));
00502 versionElement->appendChild(versionValue);
00503
00504 xercesc::DOMElement* commentDescriptionElement = document->createElement(commentDescriptionTag_);
00505 datasetElement->appendChild(commentDescriptionElement);
00506 xercesc::DOMText* commentDescriptionValue = document->createTextNode(CONVERT_TO_XML(configuration.getView().getComment().c_str()));
00507 commentDescriptionElement->appendChild(commentDescriptionValue);
00508
00509 xercesc::DOMElement* createdByUserElement = document->createElement(createdByUserTag_);
00510 datasetElement->appendChild(createdByUserElement);
00511 xercesc::DOMText* createdByUserValue = document->createTextNode(CONVERT_TO_XML(configuration.getView().getAuthor().c_str()));
00512 createdByUserElement->appendChild(createdByUserValue);
00513
00514
00515 for(unsigned int row=0; row<configuration.getView().getNumberOfRows(); row++)
00516 {
00517 xercesc::DOMElement* dataElement = document->createElement(dataTag_);
00518 datasetElement->appendChild(dataElement);
00519
00520 for(unsigned int col=0; col<configuration.getView().getNumberOfColumns(); col++)
00521 {
00522 xercesc::DOMElement* element = document->createElement(CONVERT_TO_XML(configuration.getView().getColumnInfo(col).getStorageName().c_str()));
00523 dataElement->appendChild(element);
00524 xercesc::DOMText* value = document->createTextNode(CONVERT_TO_XML(configuration.getView().getDataView()[row][col].c_str()));
00525 element->appendChild(value);
00526 }
00527 }
00528
00529 outputXML(document, configFile);
00530
00531 document->release();
00532 }
00533 catch (const xercesc::OutOfMemoryException&)
00534 {
00535 XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl;
00536
00537 }
00538 catch (const xercesc::DOMException& e)
00539 {
00540 XERCES_STD_QUALIFIER cerr << "DOMException code is: " << e.code << XERCES_STD_QUALIFIER endl;
00541
00542 }
00543 catch(const xercesc::XMLException& e)
00544 {
00545 std::string message = XML_TO_STRING(e.getMessage());
00546 __COUT__ << "Error Message: " << message << std::endl;
00547
00548 return message;
00549 }
00550 catch (...)
00551 {
00552 XERCES_STD_QUALIFIER cerr << "An error occurred creating the document" << XERCES_STD_QUALIFIER endl;
00553
00554 }
00555 }
00556 else
00557 {
00558 XERCES_STD_QUALIFIER cerr << "Requested implementation is not supported" << XERCES_STD_QUALIFIER endl;
00559
00560 }
00561
00562 terminatePlatform();
00563 return configFile;
00564 }
00565
00566
00567
00568
00569 void ConfigurationHandler::outputXML(xercesc::DOMDocument* pmyDOMDocument, std::string fileName)
00570 {
00571 std::string directory = fileName.substr(0,fileName.rfind("/")+1);
00572 __COUT__ << "Saving Configuration to " << fileName << " in directory: " << directory << std::endl;
00573
00574 mkdir(directory.c_str(), 0755);
00575
00576
00577
00578 xercesc::DOMImplementation *implementation = xercesc::DOMImplementationRegistry::getDOMImplementation(CONVERT_TO_XML("LS"));
00579
00580 #if _XERCES_VERSION >= 30000
00581
00582 xercesc::DOMLSSerializer* serializer = ((xercesc::DOMImplementationLS*)implementation)->createLSSerializer();
00583
00584
00585
00586 if (serializer->getDomConfig()->canSetParameter(xercesc::XMLUni::fgDOMWRTFormatPrettyPrint, true))
00587 serializer->getDomConfig()->setParameter(xercesc::XMLUni::fgDOMWRTFormatPrettyPrint, true);
00588
00589
00590
00591
00592
00593
00594
00595 xercesc::XMLFormatTarget* formatTarget = new xercesc::LocalFileFormatTarget(CONVERT_TO_XML(fileName));
00596
00597
00598 xercesc::DOMLSOutput* output = ((xercesc::DOMImplementationLS*)implementation)->createLSOutput();
00599
00600
00601 output->setByteStream(formatTarget);
00602
00603
00604 serializer->write(pmyDOMDocument, output);
00605
00606 #else
00607 xercesc::DOMWriter *serializer = ((xercesc::DOMImplementationLS*)implementation)->createDOMWriter();
00608
00609 serializer->setFeature(xercesc::XMLUni::fgDOMWRTFormatPrettyPrint, true);
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621 xercesc::XMLFormatTarget* formatTarget = new xercesc::LocalFileFormatTarget(CONVERT_TO_XML(fileName.c_str()));
00622
00623
00624 serializer->writeNode(formatTarget, *pmyDOMDocument);
00625
00626 #endif
00627
00628
00629 serializer->release();
00630
00631 delete formatTarget;
00632
00633 #if _XERCES_VERSION >= 30000
00634
00635 output->release();
00636
00637 #endif
00638 __COUT__ << "Done writing " << std::endl;
00639 }
00640
00641
00642 std::string ConfigurationHandler::writeXML(const ConfigurationBase* configuration)
00643 {
00644 return writeXML(*configuration);
00645 }
00646
00647
00648 std::string ConfigurationHandler::getXMLFileName(const ConfigurationBase& configuration, ConfigurationVersion version)
00649 {
00650 std::stringstream fileName;
00651 fileName << getXMLDir(&configuration)
00652 << version << '/'
00653 << configuration.getConfigurationName() << "_v" << version << ".xml";
00654 return fileName.str();
00655
00656 }
00657
00658
00659 std::string ConfigurationHandler::getXMLDir(const ConfigurationBase* configuration)
00660 {
00661 return std::string(getenv("CONFIGURATION_DATA_PATH")) + '/' + configuration->getConfigurationName() + '/';
00662 }