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 __MOUT_HDR__
00036 #define __MOUT_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 __MOUT__<< "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 __MOUT__<< "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 __MOUT__ << (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 __MOUT__ << "Reading: " << configFile << std::endl;
00207 __MOUT__ << "Into View with Table Name: " <<
00208 configuration.getViewP()->getTableName() << std::endl;
00209 __MOUT__ << "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 __MOUT__ << "Error reading path: " << configFile << std::endl;
00218 if( errno == ENOENT )
00219 throw ( std::runtime_error("Path file_name does not exist.") );
00220 else if( errno == ENOTDIR )
00221 throw ( std::runtime_error("A component of the path is not a directory."));
00222 else if( errno == ELOOP )
00223 throw ( std::runtime_error("Too many symbolic links encountered while traversing the path."));
00224 else if( errno == EACCES )
00225 throw ( std::runtime_error("Permission denied."));
00226 else if( errno == ENAMETOOLONG )
00227 throw ( std::runtime_error("File name too long.\n"));
00228 else
00229 throw ( std::runtime_error("File can not be read.\n"));
00230 }
00231
00232 xercesc::XercesDOMParser* parser = new xercesc::XercesDOMParser;
00233
00234
00235 parser->setValidationScheme(xercesc::XercesDOMParser::Val_Auto);
00236 parser->setDoNamespaces ( true );
00237 parser->setDoSchema ( false );
00238
00239 parser->useCachedGrammarInParse ( false );
00240 DOMTreeErrorReporter* errorHandler = new DOMTreeErrorReporter() ;
00241 parser->setErrorHandler(errorHandler);
00242
00243
00244 try
00245 {
00246
00247 parser->parse( configFile.c_str() );
00248
00249
00250
00251 xercesc::DOMDocument* document = parser->getDocument();
00252
00253
00254 xercesc::DOMElement* elementRoot = document->getDocumentElement();
00255
00256 if( !elementRoot ) throw(std::runtime_error( "empty XML document" ));
00257
00258
00259 xercesc::DOMNode* headerNode = getNode(headerTag_, elementRoot, 0);
00260 if( !headerNode ) throw(std::runtime_error( std::string("The document is missing the mandatory tag: ") + XML_TO_STRING(headerTag_ )));
00261
00262
00263
00264 xercesc::DOMElement* typeElement = getElement(typeTag_, headerNode, 0);
00265 if( !typeElement ) throw(std::runtime_error( std::string("The document is missing the mandatory tag: ") + XML_TO_STRING(typeTag_ )));
00266 xercesc::DOMNode* extensionTableNameNode = getNode (extensionTableNameTag_, typeElement, 0);
00267 if(!validateNode(extensionTableNameTag_,extensionTableNameNode,configuration.getView().getTableName()))
00268 throw(std::runtime_error( std::string("The document is missing the mandatory tag: ") + XML_TO_STRING(extensionTableNameTag_ )));
00269 xercesc::DOMNode* nameNode = getNode (nameTag_, typeElement, 0);
00270 if(!validateNode(nameTag_,nameNode,configuration.getConfigurationName()))
00271 throw(std::runtime_error( std::string("The document is missing the mandatory tag: ") + XML_TO_STRING(nameTag_ )));
00272
00273
00274
00275
00276
00277 xercesc::DOMElement* runElement = getElement(runTag_, headerNode,0);
00278 if( !runElement ) throw(std::runtime_error( std::string("The document is missing the mandatory tag: ") + XML_TO_STRING(runTag_ )));
00279 xercesc::DOMNode* runTypeNode = getNode (runTypeTag_, runElement, 0);
00280 assert(validateNode(runTypeTag_,runTypeNode,configuration.getConfigurationName()));
00281 xercesc::DOMNode* runNumberNode = getNode (runNumberTag_, runElement, 0);
00282 if( !runNumberNode ) throw(std::runtime_error( std::string("The document is missing the mandatory tag: ") + XML_TO_STRING(runNumberTag_ )));
00283 xercesc::DOMNode* runBeginTimestampNode = getNode (runBeginTimestampTag_, runElement, 0);
00284 if( !runBeginTimestampNode ) throw(std::runtime_error( std::string("The document is missing the mandatory tag: ") + XML_TO_STRING(runBeginTimestampTag_ )));
00285 xercesc::DOMNode* locationNode = getNode (locationTag_, runElement, 0);
00286 if( !locationNode ) throw(std::runtime_error( std::string("The document is missing the mandatory tag: ") + XML_TO_STRING(locationTag_ )));
00287
00288
00289
00290
00291
00292
00293
00294 xercesc::DOMElement* datasetElement = getElement(datasetTag_, elementRoot, 0);
00295 if( !datasetElement ) throw(std::runtime_error( std::string("The document is missing the mandatory tag: ") + XML_TO_STRING(datasetTag_ )));
00296
00297
00298
00299 xercesc::DOMNode* partNode = getNode (partTag_, datasetElement, 0);
00300 xercesc::DOMNode* nameLabelNode = getNode (nameLabelTag_, partNode, 0);
00301 xercesc::DOMNode* kindOfPartNode = getNode (kindOfPartTag_, partNode, 0);
00302
00303
00304 xercesc::DOMNode* versionNode = getNode (versionTag_, datasetElement, 0);
00305
00306 if(versionNode->getFirstChild() == 0)
00307 {
00308 throw(std::runtime_error( std::string("Missing version tag: ") + XML_TO_CHAR(versionNode->getFirstChild()->getNodeValue())));
00309 }
00310 else
00311 {
00312 char tmpVersionStr[100];
00313 sprintf(tmpVersionStr, "%d", version.version());
00314 __MOUT__ << version << "-" << XML_TO_CHAR(versionNode->getFirstChild()->getNodeValue()) << std::endl;
00315 if(strcmp(tmpVersionStr,XML_TO_CHAR(versionNode->getFirstChild()->getNodeValue())) != 0)
00316 throw(std::runtime_error( std::string("Mis-matched version tag: ") + XML_TO_CHAR(versionNode->getFirstChild()->getNodeValue()) +
00317 " vs " + tmpVersionStr));
00318 }
00319
00320 configuration.getViewP()->setVersion(XML_TO_CHAR(versionNode->getFirstChild()->getNodeValue()));
00321
00322 xercesc::DOMNode* commentDescriptionNode = getNode (commentDescriptionTag_, datasetElement, 0);
00323 if(commentDescriptionNode->getFirstChild() != 0)
00324 configuration.getViewP()->setComment(XML_TO_CHAR(commentDescriptionNode->getFirstChild()->getNodeValue()));
00325
00326 xercesc::DOMNode* createdByUserNode = getNode (createdByUserTag_, datasetElement, 0);
00327 if(createdByUserNode->getFirstChild() != 0)
00328 configuration.getViewP()->setAuthor(XML_TO_CHAR(createdByUserNode->getFirstChild()->getNodeValue()));
00329
00330
00331
00332 xercesc::DOMNodeList* dataNodeList = datasetElement->getElementsByTagName(dataTag_);
00333
00334 if( !dataNodeList )
00335 throw(std::runtime_error( std::string("Can't find ") + XML_TO_STRING(dataTag_) + " tag!"));
00336
00337
00338
00339
00340 if(!dataNodeList->getLength())
00341 {
00342 __SS__ << "Must be non-empty data set!";
00343 throw std::runtime_error(ss.str());
00344 }
00345
00346
00347
00348 configuration.getViewP()->resizeDataView(dataNodeList->getLength(), configuration.getView().getNumberOfColumns());
00349
00350 for( XMLSize_t row = 0; row < dataNodeList->getLength(); row++ )
00351 {
00352
00353 xercesc::DOMNodeList* columnNodeList = dataNodeList->item(row)->getChildNodes();
00354 unsigned int colNumber = 0;
00355
00356
00357 for( XMLSize_t col = 0; col < columnNodeList->getLength(); col++ )
00358 {
00359
00360
00361 if( !columnNodeList->item(col)->getNodeType() || columnNodeList->item(col)->getNodeType() != xercesc::DOMNode::ELEMENT_NODE )
00362 continue;
00363
00364 xercesc::DOMElement* columnElement = dynamic_cast< xercesc::DOMElement* >( columnNodeList->item(col) );
00365
00366 if( configuration.getView().getColumnInfo(colNumber).getStorageName() != XML_TO_STRING(columnElement->getTagName()))
00367 {
00368 std::stringstream error;
00369 error << __COUT_HDR__ << std::endl
00370 << "The column number " << colNumber
00371 << " named " << configuration.getView().getColumnInfo(colNumber).getStorageName()
00372 << " defined in the view " << configuration.getView().getTableName()
00373 << " doesn't match the file column order, since the "
00374 << colNumber+1 << (colNumber==0?"st":(colNumber==1?"nd":(colNumber==2?"rd":"th")))
00375 << " element found in the file at " << XML_TO_CHAR(dataTag_) << " tag number " << row
00376 << " is " << XML_TO_CHAR(columnElement->getTagName());
00377 mf::LogError(__FILE__) << error.str();
00378 throw(std::runtime_error( error.str() ));
00379 }
00380
00381
00382
00383
00384
00385
00386 configuration.getViewP()->setValueAsString(
00387 XML_TO_STRING(columnNodeList->item(col)->getFirstChild()->getNodeValue())
00388 , row
00389 , colNumber);
00390
00391 ++colNumber;
00392 }
00393 }
00394 }
00395 catch( xercesc::XMLException& e )
00396 {
00397 __MOUT__ << "Error parsing file: " << configFile << std::endl;
00398 std::ostringstream errBuf;
00399 errBuf << "Error parsing file: " << XML_TO_CHAR(e.getMessage()) << std::flush;
00400 }
00401
00402 __MOUT__ << "Done with configuration file: " << configFile << std::endl;
00403
00404 delete parser;
00405 delete errorHandler;
00406 terminatePlatform();
00407 }
00408
00409
00410
00411 std::string ConfigurationHandler::writeXML(const ConfigurationBase& configuration)
00412 {
00413 initPlatform();
00414
00415 std::string configFile = getXMLFileName(configuration,configuration.getViewVersion());
00416
00417
00418 xercesc::DOMImplementation* implementation = xercesc::DOMImplementationRegistry::getDOMImplementation(CONVERT_TO_XML("Core"));
00419 if (implementation != 0)
00420 {
00421 try
00422 {
00423
00424 xercesc::DOMDocument* document = implementation->createDocument(
00425 0,
00426 rootTag_,
00427 0);
00428
00429 xercesc::DOMElement* rootElement = document->getDocumentElement();
00430 rootElement->setAttribute(CONVERT_TO_XML("xmlns:xsi"),CONVERT_TO_XML("http://www.w3.org/2001/XMLSchema-instance"));
00431 rootElement->setAttribute(CONVERT_TO_XML("xsi:noNamespaceSchemaLocation"),CONVERT_TO_XML("ConfigurationBase.xsd"));
00432
00433
00434
00435 xercesc::DOMElement* headerElement = document->createElement(headerTag_);
00436 rootElement->appendChild(headerElement);
00437
00438
00439 xercesc::DOMElement* typeElement = document->createElement(typeTag_);
00440 headerElement->appendChild(typeElement);
00441
00442 xercesc::DOMElement* extensionTableNameElement = document->createElement(extensionTableNameTag_);
00443 typeElement->appendChild(extensionTableNameElement);
00444 xercesc::DOMText* extensionTableNameValue = document->createTextNode(CONVERT_TO_XML(configuration.getView().getTableName().c_str()));
00445 extensionTableNameElement->appendChild(extensionTableNameValue);
00446
00447 xercesc::DOMElement* nameElement = document->createElement(nameTag_);
00448 typeElement->appendChild(nameElement);
00449 xercesc::DOMText* nameValue = document->createTextNode(CONVERT_TO_XML(configuration.getConfigurationName().c_str()));
00450 nameElement->appendChild(nameValue);
00451
00452
00453
00454 xercesc::DOMElement* runElement = document->createElement(runTag_);
00455 headerElement->appendChild(runElement);
00456
00457 xercesc::DOMElement* runTypeElement = document->createElement(runTypeTag_);
00458 runElement->appendChild(runTypeElement);
00459 xercesc::DOMText* runTypeValue = document->createTextNode(CONVERT_TO_XML(configuration.getConfigurationName().c_str()));
00460 runTypeElement->appendChild(runTypeValue);
00461
00462 xercesc::DOMElement* runNumberElement = document->createElement(runNumberTag_);
00463 runElement->appendChild(runNumberElement);
00464 xercesc::DOMText* runNumberValue = document->createTextNode(CONVERT_TO_XML("1"));
00465 runNumberElement->appendChild(runNumberValue);
00466
00467 xercesc::DOMElement* runBeginTimestampElement = document->createElement(runBeginTimestampTag_);
00468 runElement->appendChild(runBeginTimestampElement);
00469 xercesc::DOMText* runBeginTimestampValue = document->createTextNode(CONVERT_TO_XML(TimeFormatter::getTime().c_str()));
00470 runBeginTimestampElement->appendChild(runBeginTimestampValue);
00471
00472 xercesc::DOMElement* locationElement = document->createElement(locationTag_);
00473 runElement->appendChild(locationElement);
00474 xercesc::DOMText* locationValue = document->createTextNode(CONVERT_TO_XML("CERN P5"));
00475 locationElement->appendChild(locationValue);
00476
00477
00478
00479 xercesc::DOMElement* datasetElement = document->createElement(datasetTag_);
00480 rootElement->appendChild(datasetElement);
00481
00482 xercesc::DOMElement* partElement = document->createElement(partTag_);
00483 datasetElement->appendChild(partElement);
00484
00485 xercesc::DOMElement* nameLabelElement = document->createElement(nameLabelTag_);
00486 partElement->appendChild(nameLabelElement);
00487 xercesc::DOMText* nameLabelValue = document->createTextNode(CONVERT_TO_XML("CMS--ROOT"));
00488 nameLabelElement->appendChild(nameLabelValue);
00489
00490 xercesc::DOMElement* kindOfPartElement = document->createElement(kindOfPartTag_);
00491 partElement->appendChild(kindOfPartElement);
00492 xercesc::DOMText* kindOfPartValue = document->createTextNode(CONVERT_TO_XML("Detector ROOT"));
00493 kindOfPartElement->appendChild(kindOfPartValue);
00494
00495 xercesc::DOMElement* versionElement = document->createElement(versionTag_);
00496 datasetElement->appendChild(versionElement);
00497 xercesc::DOMText* versionValue = document->createTextNode(CONVERT_TO_XML(configuration.getView().getVersion().version()));
00498 versionElement->appendChild(versionValue);
00499
00500 xercesc::DOMElement* commentDescriptionElement = document->createElement(commentDescriptionTag_);
00501 datasetElement->appendChild(commentDescriptionElement);
00502 xercesc::DOMText* commentDescriptionValue = document->createTextNode(CONVERT_TO_XML(configuration.getView().getComment().c_str()));
00503 commentDescriptionElement->appendChild(commentDescriptionValue);
00504
00505 xercesc::DOMElement* createdByUserElement = document->createElement(createdByUserTag_);
00506 datasetElement->appendChild(createdByUserElement);
00507 xercesc::DOMText* createdByUserValue = document->createTextNode(CONVERT_TO_XML(configuration.getView().getAuthor().c_str()));
00508 createdByUserElement->appendChild(createdByUserValue);
00509
00510
00511 for(unsigned int row=0; row<configuration.getView().getNumberOfRows(); row++)
00512 {
00513 xercesc::DOMElement* dataElement = document->createElement(dataTag_);
00514 datasetElement->appendChild(dataElement);
00515
00516 for(unsigned int col=0; col<configuration.getView().getNumberOfColumns(); col++)
00517 {
00518 xercesc::DOMElement* element = document->createElement(CONVERT_TO_XML(configuration.getView().getColumnInfo(col).getStorageName().c_str()));
00519 dataElement->appendChild(element);
00520 xercesc::DOMText* value = document->createTextNode(CONVERT_TO_XML(configuration.getView().getDataView()[row][col].c_str()));
00521 element->appendChild(value);
00522 }
00523 }
00524
00525 outputXML(document, configFile);
00526
00527 document->release();
00528 }
00529 catch (const xercesc::OutOfMemoryException&)
00530 {
00531 XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl;
00532
00533 }
00534 catch (const xercesc::DOMException& e)
00535 {
00536 XERCES_STD_QUALIFIER cerr << "DOMException code is: " << e.code << XERCES_STD_QUALIFIER endl;
00537
00538 }
00539 catch(const xercesc::XMLException& e)
00540 {
00541 std::string message = XML_TO_STRING(e.getMessage());
00542 __MOUT__ << "Error Message: " << message << std::endl;
00543
00544 return message;
00545 }
00546 catch (...)
00547 {
00548 XERCES_STD_QUALIFIER cerr << "An error occurred creating the document" << XERCES_STD_QUALIFIER endl;
00549
00550 }
00551 }
00552 else
00553 {
00554 XERCES_STD_QUALIFIER cerr << "Requested implementation is not supported" << XERCES_STD_QUALIFIER endl;
00555
00556 }
00557
00558 terminatePlatform();
00559 return configFile;
00560 }
00561
00562
00563
00564
00565 void ConfigurationHandler::outputXML(xercesc::DOMDocument* pmyDOMDocument, std::string fileName)
00566 {
00567 std::string directory = fileName.substr(0,fileName.rfind("/")+1);
00568 __MOUT__ << "Saving Configuration to " << fileName << " in directory: " << directory << std::endl;
00569
00570 mkdir(directory.c_str(), 0755);
00571
00572
00573
00574 xercesc::DOMImplementation *implementation = xercesc::DOMImplementationRegistry::getDOMImplementation(CONVERT_TO_XML("LS"));
00575
00576 #if _XERCES_VERSION >= 30000
00577
00578 xercesc::DOMLSSerializer* serializer = ((xercesc::DOMImplementationLS*)implementation)->createLSSerializer();
00579
00580
00581
00582 if (serializer->getDomConfig()->canSetParameter(xercesc::XMLUni::fgDOMWRTFormatPrettyPrint, true))
00583 serializer->getDomConfig()->setParameter(xercesc::XMLUni::fgDOMWRTFormatPrettyPrint, true);
00584
00585
00586
00587
00588
00589
00590
00591 xercesc::XMLFormatTarget* formatTarget = new xercesc::LocalFileFormatTarget(CONVERT_TO_XML(fileName));
00592
00593
00594 xercesc::DOMLSOutput* output = ((xercesc::DOMImplementationLS*)implementation)->createLSOutput();
00595
00596
00597 output->setByteStream(formatTarget);
00598
00599
00600 serializer->write(pmyDOMDocument, output);
00601
00602 #else
00603 xercesc::DOMWriter *serializer = ((xercesc::DOMImplementationLS*)implementation)->createDOMWriter();
00604
00605 serializer->setFeature(xercesc::XMLUni::fgDOMWRTFormatPrettyPrint, true);
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617 xercesc::XMLFormatTarget* formatTarget = new xercesc::LocalFileFormatTarget(CONVERT_TO_XML(fileName.c_str()));
00618
00619
00620 serializer->writeNode(formatTarget, *pmyDOMDocument);
00621
00622 #endif
00623
00624
00625 serializer->release();
00626
00627 delete formatTarget;
00628
00629 #if _XERCES_VERSION >= 30000
00630
00631 output->release();
00632
00633 #endif
00634 __MOUT__ << "Done writing " << std::endl;
00635 }
00636
00637
00638 std::string ConfigurationHandler::writeXML(const ConfigurationBase* configuration)
00639 {
00640 return writeXML(*configuration);
00641 }
00642
00643
00644 std::string ConfigurationHandler::getXMLFileName(const ConfigurationBase& configuration, ConfigurationVersion version)
00645 {
00646 std::stringstream fileName;
00647 fileName << getXMLDir(&configuration)
00648 << version << '/'
00649 << configuration.getConfigurationName() << "_v" << version << ".xml";
00650 return fileName.str();
00651
00652 }
00653
00654
00655 std::string ConfigurationHandler::getXMLDir(const ConfigurationBase* configuration)
00656 {
00657 return std::string(getenv("CONFIGURATION_DATA_PATH")) + '/' + configuration->getConfigurationName() + '/';
00658 }