ServiceConfiguration.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright 2007, 2008, 2009, 2010, 2011 Instituto de Sistemas e Robotica, Instituto Superior Tecnico
00003 
00004 This file is part of MeRMaID.
00005 
00006 MeRMaID is free software: you can redistribute it and/or modify
00007 it under the terms of the GNU Lesser General Public License as published by
00008 the Free Software Foundation, either version 3 of the License, or
00009 (at your option) any later version.
00010 
00011 MeRMaID is distributed in the hope that it will be useful,
00012 but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 GNU Lesser General Public License for more details.
00015 
00016 You should have received a copy of the GNU Lesser General Public License
00017 along with MeRMaID.  If not, see <http://www.gnu.org/licenses/>.
00018 */
00019 
00020 
00021 
00022 /**
00023  * @Filename ServiceConfiguration.cpp
00024  * @Description Support ServiceConfiguration implementation
00025  * @Status Work in Progress
00026  * @Version $Id: ServiceConfiguration.cpp 1 2011-03-04 18:13:18Z jreis $
00027  * @Maintainer mafb
00028  */
00029 
00030 #include "config.h"
00031 
00032 #include "ServiceConfiguration.hpp"
00033 
00034 
00035 #include <Exception.hpp>
00036 
00037 #include <XmlCharData.hpp>
00038 #include <XmlElementVector.hpp>
00039 
00040 #include <sstream>
00041 
00042 using namespace mermaid::support::service;
00043 using mermaid::support::errorhandling::Exception;
00044 using boost::shared_ptr;
00045 using mermaid::support::xml::XmlCharData;
00046 using mermaid::support::xml::XmlElement;
00047 using mermaid::support::xml::XmlElementVector;
00048 
00049 #include <iostream>
00050 
00051 ServiceConfiguration::ServiceConfiguration (shared_ptr<XmlDocument> serviceConfiguration)
00052 {
00053 
00054   shared_ptr<XmlElement> serviceConfigurationElement = serviceConfiguration->getRootElement();
00055   XmlElementVector serviceConfigurationChildren = serviceConfigurationElement->getChildrenElements();
00056   shared_ptr<XmlElement> updateFrequencyElement = serviceConfigurationChildren.getFirstElementWithName ("update-frequency");
00057   shared_ptr<XmlElement> configItemElement = serviceConfigurationChildren.getFirstElementWithName ("config-items");
00058   
00059   if (updateFrequencyElement == false) {
00060     throw Exception ("ServiceConfiguration::ServiceConfiguration() : no update-frequency element found in XML");
00061   };
00062   
00063   // read update-frequency
00064   {
00065     shared_ptr<XmlCharData> updateFrequencyChar = updateFrequencyElement->getFirstNonBlankCharData();
00066     
00067     if (updateFrequencyChar == false) {
00068       throw Exception ("ServiceConfiguration::ServiceConfiguration() : update-frequency item is empty");
00069     }
00070     
00071     std::string updateFrequency = (std::string) ( (*updateFrequencyChar));
00072     
00073     std::istringstream iss (updateFrequency);
00074     iss >> serviceUpdateFrequency_;
00075   }
00076   
00077   
00078   if (configItemElement == false) {
00079     return; //no config items
00080   };
00081   
00082   // read config-items
00083   {
00084     XmlElementVector configItemsChildren = configItemElement->getChildrenElements();
00085     
00086     XmlElementVector::iterator it;
00087     
00088     for (it = configItemsChildren.begin(); it != configItemsChildren.end(); it++) {
00089       shared_ptr<XmlElement> e = *it;
00090       
00091       if (e->getName() == "item") {
00092         XmlElementVector itemChildren = e->getChildrenElements();
00093         shared_ptr<XmlElement> nameElement = itemChildren.getFirstElementWithName ("name");
00094         shared_ptr<XmlElement> valueElement = itemChildren.getFirstElementWithName ("value");
00095         
00096         if (nameElement == false) {
00097           throw Exception ("ServiceConfiguration::ServiceConfiguration() : item's name element is not valid");
00098         }
00099         
00100         if (valueElement == false) {
00101           throw Exception ("ServiceConfiguration::ServiceConfiguration() : item's value element is not valid");
00102         }
00103         
00104         
00105         shared_ptr<XmlCharData> nameChar = nameElement->getFirstNonBlankCharData();
00106         shared_ptr<XmlCharData> valueChar = valueElement->getFirstNonBlankCharData();
00107         
00108         if (nameChar == false) {
00109           throw Exception ("ServiceConfiguration::ServiceConfiguration() : item's name element is empty");
00110         }
00111         
00112         if (valueChar == false) {
00113           throw Exception ("ServiceConfiguration::ServiceConfiguration() : item's value element is empty");
00114         }
00115         
00116         std::string nameString = (std::string) ( (*nameChar));
00117         std::string valueString = (std::string) ( (*valueChar));
00118         
00119         valuesMap_[nameString] = valueString;
00120       }
00121     }
00122   }
00123 } //ServiceConfigurationstatic_pointer_cast<XmlElement>()
00124 
00125 // ServiceConfiguration::ServiceConfiguration(float serviceUpdateFrequency)
00126 // {
00127 //     serviceUpdateFrequency_ = serviceUpdateFrequency;
00128 // }; // ServiceConfiguration()
00129 
00130 float ServiceConfiguration::getServiceUpdateFrequency()
00131 {
00132   return serviceUpdateFrequency_;
00133 }; // getServiceUpdateFrequency()
00134 
00135 std::string ServiceConfiguration::getConfigValue (std::string name)
00136 {
00137   std::map<std::string, std::string>::iterator it = valuesMap_.find (name);
00138   
00139   if (it != valuesMap_.end()) {
00140     return (*it).second;
00141   }
00142   else {
00143     throw Exception ("ServiceConfiguration::getConfigValue : unknown config item name: " + name);
00144   }
00145 }; // getConfigValue()
00146 
Generated on Fri Mar 4 22:14:58 2011 for MeRMaID::support by  doxygen 1.6.3