RdsContainer.cxx

Go to the documentation of this file.
00001 /*------------------------------------------------------------------------------
00002  
00003     Copyright (c) 2004 Media Development Loan Fund
00004   
00005     This file is part of the Campcaster project.
00006     https://www.campware.org/
00007     To report bugs, send an e-mail to [email protected]
00008   
00009     Campcaster is free software; you can redistribute it and/or modify
00010     it under the terms of the GNU General Public License as published by
00011     the Free Software Foundation; either version 2 of the License, or
00012     (at your option) any later version.
00013    
00014     Campcaster is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017     GNU General Public License for more details.
00018   
00019     You should have received a copy of the GNU General Public License
00020     along with Campcaster; if not, write to the Free Software
00021     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022   
00023  
00024     Author   : $Author: fgerlits $
00025     Version  : $Revision: 2898 $
00026     Location : $URL: svn://code.campware.org/campcaster/trunk/campcaster/src/modules/core/src/RdsContainer.cxx $
00027  
00028 ------------------------------------------------------------------------------*/
00029 
00030 /* ============================================================ include files */
00031 
00032 #ifdef HAVE_CONFIG_H
00033  #include "configure.h"
00034 #endif
00035  
00036 #include "LiveSupport/Core/RdsContainer.h"
00037 
00038 
00039 using namespace LiveSupport::Core;
00040 using namespace LiveSupport::Core;
00041 
00042 /* ===================================================  local data structures */
00043 
00044 
00045 /* ================================================  local constants & macros */
00046 
00050 const std::string       RdsContainer::configElementName = "rdsContainer";
00051 
00052 
00053 /* ===============================================  local function prototypes */
00054 
00055 
00056 /* =============================================================  module code */
00057 
00058 /*------------------------------------------------------------------------------
00059  *  Create an RDS container object based on an XML element.
00060  *----------------------------------------------------------------------------*/
00061 void
00062 RdsContainer :: configure(const xmlpp::Element &    element)
00063                                                 throw (std::invalid_argument)
00064 {
00065     if (element.get_name() != configElementName) {
00066         throw std::invalid_argument("bad coniguration element "
00067                                   + element.get_name());
00068     }
00069 
00070     xmlpp::Node::NodeList childNodes = element.get_children(
00071                                             RdsItem::getConfigElementName());
00072     xmlpp::Node::NodeList::const_iterator   it;
00073 
00074     rdsItemList.clear();
00075     for (it = childNodes.begin(); it != childNodes.end(); ++it) {
00076         const xmlpp::Element *      rdsItemElement 
00077                                     = dynamic_cast<const xmlpp::Element*> (*it);
00078                                     
00079         Ptr<RdsItem>::Ref           rdsItem(new RdsItem());
00080         rdsItem->configure(*rdsItemElement);
00081         
00082         rdsItemList.push_back(rdsItem);
00083     }
00084 }
00085 
00086 
00087 /*------------------------------------------------------------------------------
00088  *  Set the RDS options.
00089  *----------------------------------------------------------------------------*/
00090 void
00091 RdsContainer :: setRdsOptions(Ptr<const Glib::ustring>::Ref  key,
00092                               Ptr<const Glib::ustring>::Ref  value,
00093                               bool                           enabled)
00094                                                                     throw ()
00095 {
00096     RdsItemListType::const_iterator     it;
00097 
00098     bool found = false;
00099     for(it = rdsItemList.begin(); it != rdsItemList.end(); ++it) {
00100         Ptr<RdsItem>::Ref               rdsItem = *it;
00101         if (*rdsItem->getKey() == *key) {
00102             found = true;
00103             rdsItem->setValue(value);
00104             rdsItem->setEnabled(enabled);
00105             break;
00106         }
00107     }
00108     
00109     if (!found) {
00110         Ptr<RdsItem>::Ref   rdsItem(new RdsItem(key, value, enabled));
00111         rdsItemList.push_back(rdsItem);
00112     }
00113     
00114     touched = true;
00115 }
00116 
00117 
00118 /*------------------------------------------------------------------------------
00119  *  Get the value of an RDS string.
00120  *----------------------------------------------------------------------------*/
00121 Ptr<const Glib::ustring>::Ref
00122 RdsContainer :: getRdsValue(Ptr<const Glib::ustring>::Ref  key)
00123                                                 throw (std::invalid_argument)
00124 {
00125     RdsItemListType::const_iterator     it;
00126     for(it = rdsItemList.begin(); it != rdsItemList.end(); ++it) {
00127         Ptr<RdsItem>::Ref               rdsItem = *it;
00128         if (*rdsItem->getKey() == *key) {
00129             return rdsItem->getValue();
00130         }
00131     }
00132     
00133     Glib::ustring   safeKey = key ? *key : "(null)";
00134     throw std::invalid_argument("RDS option " + safeKey + "not found.");
00135 }
00136 
00137 
00138 /*------------------------------------------------------------------------------
00139  *  Get the enabled/disabled state of an RDS option.
00140  *----------------------------------------------------------------------------*/
00141 bool
00142 RdsContainer :: getRdsEnabled(Ptr<const Glib::ustring>::Ref  key)
00143                                                 throw (std::invalid_argument)
00144 {
00145     RdsItemListType::const_iterator     it;
00146     for(it = rdsItemList.begin(); it != rdsItemList.end(); ++it) {
00147         Ptr<RdsItem>::Ref               rdsItem = *it;
00148         if (*rdsItem->getKey() == *key) {
00149             return rdsItem->getEnabled();
00150         }
00151     }
00152     
00153     Glib::ustring   safeKey = key ? *key : "(null)";
00154     throw std::invalid_argument("RDS option " + safeKey + "not found.");
00155 }
00156 
00157 
00158 /*------------------------------------------------------------------------------
00159  *  Convert the object to a string.
00160  *----------------------------------------------------------------------------*/
00161 Ptr<Glib::ustring>::Ref
00162 RdsContainer :: toString(void)                                      throw ()
00163 {
00164     Ptr<Glib::ustring>::Ref     rdsString(new Glib::ustring);
00165     
00166     RdsItemListType::const_iterator     it;
00167     for(it = rdsItemList.begin(); it != rdsItemList.end(); ++it) {
00168         Ptr<RdsItem>::Ref               rdsItem = *it;
00169         rdsString->append(*rdsItem->toString());
00170     }
00171     
00172     return rdsString;
00173 }
00174 
00175 
00176 /*------------------------------------------------------------------------------
00177  *  Convert the object to XML.
00178  *----------------------------------------------------------------------------*/
00179 const xmlpp::Element *
00180 RdsContainer :: toXmlElement(void)                                  throw ()
00181 {
00182     if (!touched && xmlDocument) {
00183         return xmlDocument->get_root_node();
00184     }
00185     
00186     xmlDocument.reset(new xmlpp::Document());
00187     xmlpp::Element *    rootNode = xmlDocument->create_root_node(
00188                                                             configElementName);
00189     RdsItemListType::const_iterator     it;
00190     for(it = rdsItemList.begin(); it != rdsItemList.end(); ++it) {
00191         Ptr<RdsItem>::Ref               rdsItem = *it;
00192         rootNode->import_node(rdsItem->toXmlElement(), true);
00193     }
00194     
00195     touched = false;
00196     return rootNode;
00197 }
00198 

Generated on Thu Sep 20 02:00:29 2007 for Campcaster by  1.4.7