From 2aea2785ca388f14a651870a0bed8f2bd18007b0 Mon Sep 17 00:00:00 2001 From: maroy Date: Wed, 27 Apr 2005 17:39:51 +0000 Subject: [PATCH] added MetadataType and MetadataTypeContainer classes solution for issue #877, see http://bugs.campware.org/view.php?id=877 --- livesupport/modules/core/etc/Makefile.in | 11 +- livesupport/modules/core/etc/metadataType.xml | 15 + .../core/etc/metadataTypeContainer.xml | 19 + .../LiveSupport/Core/LocalizedConfigurable.h | 22 +- .../include/LiveSupport/Core/MetadataType.h | 232 +++++++++++ .../LiveSupport/Core/MetadataTypeContainer.h | 257 ++++++++++++ livesupport/modules/core/src/MetadataType.cxx | 143 +++++++ .../core/src/MetadataTypeContainer.cxx | 158 ++++++++ .../core/src/MetadataTypeContainerTest.cxx | 381 ++++++++++++++++++ .../core/src/MetadataTypeContainerTest.h | 139 +++++++ livesupport/modules/core/var/hu.txt | 2 + livesupport/modules/core/var/jp.txt | 2 + livesupport/modules/core/var/root.txt | 2 + 13 files changed, 1377 insertions(+), 6 deletions(-) create mode 100644 livesupport/modules/core/etc/metadataType.xml create mode 100644 livesupport/modules/core/etc/metadataTypeContainer.xml create mode 100644 livesupport/modules/core/include/LiveSupport/Core/MetadataType.h create mode 100644 livesupport/modules/core/include/LiveSupport/Core/MetadataTypeContainer.h create mode 100644 livesupport/modules/core/src/MetadataType.cxx create mode 100644 livesupport/modules/core/src/MetadataTypeContainer.cxx create mode 100644 livesupport/modules/core/src/MetadataTypeContainerTest.cxx create mode 100644 livesupport/modules/core/src/MetadataTypeContainerTest.h diff --git a/livesupport/modules/core/etc/Makefile.in b/livesupport/modules/core/etc/Makefile.in index c0b091c8f..7eccec62e 100644 --- a/livesupport/modules/core/etc/Makefile.in +++ b/livesupport/modules/core/etc/Makefile.in @@ -20,8 +20,8 @@ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # -# Author : $Author: fgerlits $ -# Version : $Revision: 1.27 $ +# Author : $Author: maroy $ +# Version : $Revision: 1.28 $ # Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/etc/Makefile.in,v $ # # @configure_input@ @@ -127,7 +127,9 @@ CORE_LIB_OBJS = ${TMP_DIR}/UniqueId.o \ ${TMP_DIR}/XmlRpcException.o \ ${TMP_DIR}/TagConversion.o \ ${TMP_DIR}/BaseTestMethod.o \ - ${TMP_DIR}/SearchCriteria.o + ${TMP_DIR}/SearchCriteria.o \ + ${TMP_DIR}/MetadataType.o \ + ${TMP_DIR}/MetadataTypeContainer.o TEST_RUNNER_OBJS = ${TMP_DIR}/TestRunner.o \ ${TMP_DIR}/UniqueIdTest.o \ @@ -143,7 +145,8 @@ TEST_RUNNER_OBJS = ${TMP_DIR}/TestRunner.o \ ${TMP_DIR}/Md5Test.o \ ${TMP_DIR}/XmlRpcToolsTest.o \ ${TMP_DIR}/TagConversionTest.o \ - ${TMP_DIR}/SearchCriteriaTest.o + ${TMP_DIR}/SearchCriteriaTest.o \ + ${TMP_DIR}/MetadataTypeContainerTest.o TEST_RUNNER_RES = ${TMP_DIR}/${PACKAGE_NAME}_root.res \ ${TMP_DIR}/${PACKAGE_NAME}_en.res \ diff --git a/livesupport/modules/core/etc/metadataType.xml b/livesupport/modules/core/etc/metadataType.xml new file mode 100644 index 000000000..aaa3ced8b --- /dev/null +++ b/livesupport/modules/core/etc/metadataType.xml @@ -0,0 +1,15 @@ + + + + + + +]> + + + diff --git a/livesupport/modules/core/etc/metadataTypeContainer.xml b/livesupport/modules/core/etc/metadataTypeContainer.xml new file mode 100644 index 000000000..c0953d6ce --- /dev/null +++ b/livesupport/modules/core/etc/metadataTypeContainer.xml @@ -0,0 +1,19 @@ + + + + + + + + +]> + + + + + diff --git a/livesupport/modules/core/include/LiveSupport/Core/LocalizedConfigurable.h b/livesupport/modules/core/include/LiveSupport/Core/LocalizedConfigurable.h index 6668b11db..e9ab53114 100644 --- a/livesupport/modules/core/include/LiveSupport/Core/LocalizedConfigurable.h +++ b/livesupport/modules/core/include/LiveSupport/Core/LocalizedConfigurable.h @@ -22,7 +22,7 @@ Author : $Author: maroy $ - Version : $Revision: 1.1 $ + Version : $Revision: 1.2 $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/include/LiveSupport/Core/LocalizedConfigurable.h,v $ ------------------------------------------------------------------------------*/ @@ -59,8 +59,26 @@ namespace Core { * A configurable element, that is also localized. Reads localization * information from the configuration file itself. * + * The configure() function expects the following XML element: + * + *

+ *  
+ *  
+ * + *

+ *  
+ *  
+ *  
+ *
+ *  ]>
+ *  
+ * * @author $Author: maroy $ - * @version $Revision: 1.1 $ + * @version $Revision: 1.2 $ */ class LocalizedConfigurable : public Configurable, public LocalizedObject { diff --git a/livesupport/modules/core/include/LiveSupport/Core/MetadataType.h b/livesupport/modules/core/include/LiveSupport/Core/MetadataType.h new file mode 100644 index 000000000..5b661216d --- /dev/null +++ b/livesupport/modules/core/include/LiveSupport/Core/MetadataType.h @@ -0,0 +1,232 @@ +/*------------------------------------------------------------------------------ + + Copyright (c) 2004 Media Development Loan Fund + + This file is part of the LiveSupport project. + http://livesupport.campware.org/ + To report bugs, send an e-mail to bugs@campware.org + + LiveSupport is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + LiveSupport is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with LiveSupport; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + + Author : $Author: maroy $ + Version : $Revision: 1.1 $ + Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/include/LiveSupport/Core/MetadataType.h,v $ + +------------------------------------------------------------------------------*/ +#ifndef LiveSupport_Core_MetadataType_h +#define LiveSupport_Core_MetadataType_h + +#ifndef __cplusplus +#error This is a C++ include file +#endif + + +/* ============================================================ include files */ + +#ifdef HAVE_CONFIG_H +#include "configure.h" +#endif + +#include + +#include "LiveSupport/Core/Ptr.h" +#include "LiveSupport/Core/Configurable.h" + + +namespace LiveSupport { +namespace Core { + + +/* ================================================================ constants */ + + +/* =================================================================== macros */ + + +/* =============================================================== data types */ + +class MetadataTypeContainer; + + +/** + * A class for representing a metadata type. + * + * This object has to be configured with an XML configuration element + * called playlist. This may look like the following: + * + *

+ *  
+ *  
+ * + * The DTD for the expected XML element looks like the following: + * + *

+ *  
+ *  
+ *  
+ *  
+ *  
+ * + * + * @author $Author: maroy $ + * @version $Revision: 1.1 $ + * @see MetadataTypeContainer + */ +class MetadataType : public Configurable +{ + private: + /** + * The name of the configuration XML element used by MetadataType. + */ + static const std::string configElementNameStr; + + /** + * A reference to a metadata type container. + */ + Ptr::Ref container; + + /** + * The Dublic Core name of this metadata type. + */ + Ptr::Ref dcName; + + /** + * The ID3v2 tag for this metadata type. + */ + Ptr::Ref id3Tag; + + /** + * The localization key for this metadata type. + */ + Ptr::Ref localizationKey; + + + public: + /** + * Default constructor. + * + * @param container the container this metadata type is held in. + */ + MetadataType(Ptr::Ref container) + throw (); + + /** + * Constructor. + * + * @param container the container this metadata type is held in. + * @param dcName the Dublic Core metadata name. + * @param id3Tag the ID3v2 tag assciated with the metadata. + * @param localizationKey the key to get the localized name for + * the metadata + */ + MetadataType(Ptr::Ref container, + Glib::ustring dcName, + Glib::ustring id3Tag, + Glib::ustring localizationKey) + throw (); + + + /** + * A virtual destructor, as this class has virtual functions. + */ + virtual + ~MetadataType(void) throw () + { + } + + /** + * Return the name of the XML element this object expects + * to be sent to a call to configure(). + * + * @return the name of the expected XML configuration element. + */ + static const std::string + getConfigElementName(void) throw () + { + return configElementNameStr; + } + + /** + * Configure the metadata object based on an XML configuration element. + * + * @param elemen the XML configuration element. + * @exception std::invalid_argument of the supplied XML element + * contains bad configuration information + */ + virtual void + configure(const xmlpp::Element &element) + throw (std::invalid_argument); + + /** + * Return the Dublic Core name of the metadata type. + * + * @return the Dublic Core name of the metadata type. + */ + Ptr::Ref + getDcName(void) const throw () + { + return dcName; + } + + /** + * Return the ID3v2 tag name for the metadata type. + * + * @return the ID3v2 tag name for the metadata type, or a null + * pointer, if no ID3v2 tag exists for the metadata type. + */ + Ptr::Ref + getId3Tag(void) const throw () + { + return id3Tag; + } + + /** + * Return the localization key for the metadata type. + * + * @return the localization key for the metadata type. + */ + Ptr::Ref + getLocalizationKey(void) const throw () + { + return localizationKey; + } + + /** + * Return the localized name for the metadata type. + * + * @return the localized name for the metadata type. + * @exception std::invalid_argument if there is no localized + * name for this metadata type. + */ + Ptr::Ref + getLocalizedName(void) const throw (std::invalid_argument); +}; + + +/* ================================================= external data structures */ + + +/* ====================================================== function prototypes */ + + +} // namespace Core +} // namespace LiveSupport + +#endif // LiveSupport_Core_MetadataType_h + diff --git a/livesupport/modules/core/include/LiveSupport/Core/MetadataTypeContainer.h b/livesupport/modules/core/include/LiveSupport/Core/MetadataTypeContainer.h new file mode 100644 index 000000000..21fbd86c8 --- /dev/null +++ b/livesupport/modules/core/include/LiveSupport/Core/MetadataTypeContainer.h @@ -0,0 +1,257 @@ +/*------------------------------------------------------------------------------ + + Copyright (c) 2004 Media Development Loan Fund + + This file is part of the LiveSupport project. + http://livesupport.campware.org/ + To report bugs, send an e-mail to bugs@campware.org + + LiveSupport is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + LiveSupport is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with LiveSupport; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + + Author : $Author: maroy $ + Version : $Revision: 1.1 $ + Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/include/LiveSupport/Core/MetadataTypeContainer.h,v $ + +------------------------------------------------------------------------------*/ +#ifndef LiveSupport_Core_MetadataTypeContainer_h +#define LiveSupport_Core_MetadataTypeContainer_h + +#ifndef __cplusplus +#error This is a C++ include file +#endif + + +/* ============================================================ include files */ + +#ifdef HAVE_CONFIG_H +#include "configure.h" +#endif + +#include + +#include + +#include "LiveSupport/Core/Ptr.h" +#include "LiveSupport/Core/Configurable.h" +#include "LiveSupport/Core/LocalizedObject.h" +#include "LiveSupport/Core/MetadataType.h" + + +namespace LiveSupport { +namespace Core { + + +/* ================================================================ constants */ + + +/* =================================================================== macros */ + + +/* =============================================================== data types */ + +/** + * Container holding MetadataType objects. + * + * This object has to be configured with an XML configuration element + * called playlist. This may look like the following: + * + *

+ *  
+ *      
+ *      
+ *      ...
+ *      
+ *  
+ *  
+ * + * The DTD for the expected XML element is the following: + * + *

+ *  
+ *  
+ * + * For a description of the metadataType XML element, see the documentation + * for the MetadataType class. + * + * @author $Author: maroy $ + * @version $Revision: 1.1 $ + * @see MetadataType + */ +class MetadataTypeContainer : public Configurable, + public LocalizedObject, + public boost::enable_shared_from_this +{ + public: + /** + * A vector type holding contant MetadataType references. + */ + typedef std::vector::Ref> Vector; + + + private: + /** + * Map type for storing MetadataType objects by Glib::ustrings. + */ + typedef std::map::Ref> NameMap; + + /** + * The name of the configuration XML element used by + * MetadataTypeContainer. + */ + static const std::string configElementNameStr; + + /** + * A vector holding all MetadataType references. + */ + Vector vector; + + /** + * Map of MetadaType objects, stored by DC name. + */ + NameMap dcNameMap; + + /** + * Map of MetadaType objects, stored by ID3v2 tags. + */ + NameMap id3TagMap; + + + public: + /** + * Constructor. + * + * @param bundle the resource bundle holding the localized resources. + */ + MetadataTypeContainer(Ptr::Ref bundle) + throw () + : LocalizedObject(bundle) + { + } + + /** + * A virtual destructor, as this class has virtual functions. + */ + virtual + ~MetadataTypeContainer(void) throw () + { + } + + /** + * Return the name of the XML element this object expects + * to be sent to a call to configure(). + * + * @return the name of the expected XML configuration element. + */ + static const std::string + getConfigElementName(void) throw () + { + return configElementNameStr; + } + + /** + * Configure the metadata object based on an XML configuration element. + * + * @param elemen the XML configuration element. + * @exception std::invalid_argument of the supplied XML element + * contains bad configuration information + */ + virtual void + configure(const xmlpp::Element &element) + throw (std::invalid_argument); + + /** + * Return an interator holding all MetadataType objects. + * The iterator holds objects of type Ptr::Ref. + * + * @return an iterator holding the available metadata types. + */ + Vector::const_iterator + begin(void) const throw () + { + return vector.begin(); + } + + /** + * Return an interator pointing to the end of MetadataType objects. + * The iterator holds objects of type Ptr::Ref. + * + * @return an iterator pointing to the end of metadata types. + */ + Vector::const_iterator + end(void) const throw () + { + return vector.end(); + } + + /** + * Tells if a MetadataType object exists with the specified + * Dublic Core name. + * + * @param dcName the DC name of the metadata type. + * @return true if a metadata type exists with the specified name, + * false otherwise + */ + bool + existsByDcName(const Glib::ustring dcName) const throw (); + + /** + * Return a MetadataType object, by Dublic Core name. + * + * @param dcName the DC name of the metadata type. + * @return a MetadataType object with the supplied DC name + * @exception std::invalid_argument if no metadata type exists + * with the suplied name. + */ + Ptr::Ref + getByDcName(const Glib::ustring dcName) + throw (std::invalid_argument); + + /** + * Tells if a MetadataType object exists with the specified + * ID3v2 tag. + * + * @param id3Tag the ID3v2 tag of the metadata type. + * @return true if a metadata type exists with the specified tag name, + * false otherwise + */ + bool + existsById3Tag(const Glib::ustring id3Tag) const throw (); + + /** + * Return a MetadataType object, by ID3v2 tag. + * + * @param id3Tag the ID3v2 tag of the metadata type. + * @return a MetadataType object with the supplied ID3v2 tag name. + * @exception std::invalid_argument if no metadata type exists + * with the suplied tag name. + */ + Ptr::Ref + getById3Tag(const Glib::ustring id3Tag) + throw (std::invalid_argument); +}; + + +/* ================================================= external data structures */ + + +/* ====================================================== function prototypes */ + + +} // namespace Core +} // namespace LiveSupport + +#endif // LiveSupport_Core_MetadataTypeContainer_h + diff --git a/livesupport/modules/core/src/MetadataType.cxx b/livesupport/modules/core/src/MetadataType.cxx new file mode 100644 index 000000000..5634e02c0 --- /dev/null +++ b/livesupport/modules/core/src/MetadataType.cxx @@ -0,0 +1,143 @@ +/*------------------------------------------------------------------------------ + + Copyright (c) 2004 Media Development Loan Fund + + This file is part of the LiveSupport project. + http://livesupport.campware.org/ + To report bugs, send an e-mail to bugs@campware.org + + LiveSupport is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + LiveSupport is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with LiveSupport; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + + Author : $Author: maroy $ + Version : $Revision: 1.1 $ + Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/MetadataType.cxx,v $ + +------------------------------------------------------------------------------*/ + +/* ============================================================ include files */ + +#ifdef HAVE_CONFIG_H +#include "configure.h" +#endif + +#include "LiveSupport/Core/MetadataTypeContainer.h" +#include "LiveSupport/Core/MetadataType.h" + + +using namespace LiveSupport::Core; + +/* =================================================== local data structures */ + + +/* ================================================ local constants & macros */ + +/*------------------------------------------------------------------------------ + * The name of the config element for this class + *----------------------------------------------------------------------------*/ +const std::string MetadataType::configElementNameStr = "metadataType"; + +/** + * The name of the attribute of the DC name in the metadataType element. + */ +static const std::string dcNameAttrName = "dcName"; + +/** + * The name of the attribute of the ID3v2 tag in the metadataType element + */ +static const std::string id3TagAttrName = "id3Tag"; + +/** + * The name of the attribute of the localization key in the + * metadataType element. + */ +static const std::string localizationKeyAttrName = "localizationKey"; + + +/* =============================================== local function prototypes */ + + +/* ============================================================= module code */ + +/*------------------------------------------------------------------------------ + * Constructor. + *----------------------------------------------------------------------------*/ +MetadataType :: MetadataType(Ptr::Ref container) + throw () +{ + this->container = container; +} + + +/*------------------------------------------------------------------------------ + * Constructor. + *----------------------------------------------------------------------------*/ +MetadataType :: MetadataType(Ptr::Ref container, + Glib::ustring dcName, + Glib::ustring id3Tag, + Glib::ustring localizationKey) + throw () +{ + this->container = container; + this->dcName.reset(new Glib::ustring(dcName)); + this->id3Tag.reset(new Glib::ustring(id3Tag)); + this->localizationKey.reset(new Glib::ustring(localizationKey)); +} + + +/*------------------------------------------------------------------------------ + * Create a metadata type element object based on an XML element. + *----------------------------------------------------------------------------*/ +void +MetadataType :: configure(const xmlpp::Element & element) + throw (std::invalid_argument) +{ + if (element.get_name() != configElementNameStr) { + throw std::invalid_argument("bad coniguration element " + + element.get_name()); + } + + const xmlpp::Attribute* attribute; + + // set the DC name + if (!(attribute = element.get_attribute(dcNameAttrName))) { + throw std::invalid_argument("missing attribute " + dcNameAttrName); + } + dcName.reset(new Glib::ustring(attribute->get_value())); + + // get the ID3v2 tag name, optional + if ((attribute = element.get_attribute(id3TagAttrName))) { + id3Tag.reset(new Glib::ustring(attribute->get_value())); + } + + // get the localization key + if (!(attribute = element.get_attribute(localizationKeyAttrName))) { + throw std::invalid_argument("missing attribute " + + localizationKeyAttrName); + } + localizationKey.reset(new Glib::ustring(attribute->get_value())); +} + + +/*------------------------------------------------------------------------------ + * Return the localized name for this metadata type. + *----------------------------------------------------------------------------*/ +Ptr::Ref +MetadataType :: getLocalizedName(void) const + throw (std::invalid_argument) +{ + return container->getResourceUstring(*localizationKey); +} + diff --git a/livesupport/modules/core/src/MetadataTypeContainer.cxx b/livesupport/modules/core/src/MetadataTypeContainer.cxx new file mode 100644 index 000000000..470abdbb6 --- /dev/null +++ b/livesupport/modules/core/src/MetadataTypeContainer.cxx @@ -0,0 +1,158 @@ +/*------------------------------------------------------------------------------ + + Copyright (c) 2004 Media Development Loan Fund + + This file is part of the LiveSupport project. + http://livesupport.campware.org/ + To report bugs, send an e-mail to bugs@campware.org + + LiveSupport is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + LiveSupport is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with LiveSupport; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + + Author : $Author: maroy $ + Version : $Revision: 1.1 $ + Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/MetadataTypeContainer.cxx,v $ + +------------------------------------------------------------------------------*/ + +/* ============================================================ include files */ + +#ifdef HAVE_CONFIG_H +#include "configure.h" +#endif + +#include "LiveSupport/Core/MetadataTypeContainer.h" + + +using namespace LiveSupport::Core; + +/* =================================================== local data structures */ + + +/* ================================================ local constants & macros */ + +/*------------------------------------------------------------------------------ + * The name of the config element for this class + *----------------------------------------------------------------------------*/ +const std::string MetadataTypeContainer::configElementNameStr + = "metadataTypeContainer"; + + +/* =============================================== local function prototypes */ + + +/* ============================================================= module code */ + +/*------------------------------------------------------------------------------ + * Create a metadata type container element object based on an XML element. + *----------------------------------------------------------------------------*/ +void +MetadataTypeContainer :: configure(const xmlpp::Element & element) + throw (std::invalid_argument) +{ + if (element.get_name() != configElementNameStr) { + throw std::invalid_argument("bad coniguration element " + + element.get_name()); + } + + // get the metadataType child elements, and process them + xmlpp::Node::NodeList childNodes = element.get_children( + MetadataType::getConfigElementName()); + xmlpp::Node::NodeList::iterator it = childNodes.begin(); + xmlpp::Node::NodeList::iterator end = childNodes.end(); + + while (it != end) { + const xmlpp::Element * metadataTypeElement + = dynamic_cast (*it); + Ptr::Ref metadataType( + new MetadataType(shared_from_this())); + + metadataType->configure(*metadataTypeElement); + + if (dcNameMap.find(*metadataType->getDcName()) != dcNameMap.end()) { + throw std::invalid_argument("trying to insert duplicate metadata " + "type: " + *metadataType->getDcName()); + } + if (metadataType->getId3Tag().get()) { + if (id3TagMap.find(*metadataType->getId3Tag()) != id3TagMap.end()) { + throw std::invalid_argument("trying to insert duplicate " + "metadata by ID3v2 tag: " + + *metadataType->getId3Tag()); + } + } + + dcNameMap[*metadataType->getDcName()] = metadataType; + if (metadataType->getId3Tag().get()) { + id3TagMap[*metadataType->getId3Tag()] = metadataType; + } + vector.push_back(metadataType); + + ++it; + } +} + + +/*------------------------------------------------------------------------------ + * Tell if a metadata type object by the Dublin Core name exists + *----------------------------------------------------------------------------*/ +bool +MetadataTypeContainer :: existsByDcName(const Glib::ustring dcName) const + throw () +{ + return dcNameMap.find(dcName) != dcNameMap.end(); +} + + +/*------------------------------------------------------------------------------ + * Look for a metadata type object by the Dublin Core name + *----------------------------------------------------------------------------*/ +Ptr::Ref +MetadataTypeContainer :: getByDcName(const Glib::ustring dcName) + throw (std::invalid_argument) +{ + if (!existsByDcName(dcName)) { + throw std::invalid_argument("no metadata type by the DC name " +dcName); + } + + return dcNameMap[dcName]; +} + + +/*------------------------------------------------------------------------------ + * Tell if a metadata type object by the ID3v2 tag name exists. + *----------------------------------------------------------------------------*/ +bool +MetadataTypeContainer :: existsById3Tag(const Glib::ustring id3Tag) const + throw () +{ + return id3TagMap.find(id3Tag) != id3TagMap.end(); +} + + +/*------------------------------------------------------------------------------ + * Look for a metadata type object by the ID3v2 tag name. + *----------------------------------------------------------------------------*/ +Ptr::Ref +MetadataTypeContainer :: getById3Tag(const Glib::ustring id3Tag) + throw (std::invalid_argument) +{ + if (!existsById3Tag(id3Tag)) { + throw std::invalid_argument("no metadata type by the ID3v2 Tag " + + id3Tag); + } + + return id3TagMap[id3Tag]; +} + diff --git a/livesupport/modules/core/src/MetadataTypeContainerTest.cxx b/livesupport/modules/core/src/MetadataTypeContainerTest.cxx new file mode 100644 index 000000000..a7da4607e --- /dev/null +++ b/livesupport/modules/core/src/MetadataTypeContainerTest.cxx @@ -0,0 +1,381 @@ +/*------------------------------------------------------------------------------ + + Copyright (c) 2004 Media Development Loan Fund + + This file is part of the LiveSupport project. + http://livesupport.campware.org/ + To report bugs, send an e-mail to bugs@campware.org + + LiveSupport is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + LiveSupport is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with LiveSupport; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + + Author : $Author: maroy $ + Version : $Revision: 1.1 $ + Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/MetadataTypeContainerTest.cxx,v $ + +------------------------------------------------------------------------------*/ + +/* ============================================================ include files */ + +#ifdef HAVE_CONFIG_H +#include "configure.h" +#endif + +#if HAVE_UNISTD_H +#include +#else +#error "Need unistd.h" +#endif + + +#include +#include + +#include "LiveSupport/Core/MetadataTypeContainer.h" + +#include "MetadataTypeContainerTest.h" + +using namespace LiveSupport::Core; + +/* =================================================== local data structures */ + + +/* ================================================ local constants & macros */ + +CPPUNIT_TEST_SUITE_REGISTRATION(MetadataTypeContainerTest); + +/** + * The name of the configuration file for the resource bundle. + */ +static const std::string bundleConfigFileName = "etc/resourceBundle.xml"; + +/** + * The name of the configuration file for the metadataType element. + */ +static const std::string metadataTypeConfigFileName = "etc/metadataType.xml"; + +/** + * The name of the configuration file for the metadataTypeContainer element. + */ +static const std::string configFileName = "etc/metadataTypeContainer.xml"; + + +/* =============================================== local function prototypes */ + + +/* ============================================================= module code */ + +/*------------------------------------------------------------------------------ + * Set up the test environment + *----------------------------------------------------------------------------*/ +void +MetadataTypeContainerTest :: setUp(void) throw () +{ + try { + Ptr::Ref parser( + new xmlpp::DomParser(bundleConfigFileName, true)); + const xmlpp::Document * document = parser->get_document(); + const xmlpp::Element * root = document->get_root_node(); + + bundle = LocalizedObject::getBundle(*root); + } catch (std::invalid_argument &e) { + CPPUNIT_FAIL("semantic error in configuration file"); + } catch (std::exception &e) { + CPPUNIT_FAIL(e.what()); + } + CPPUNIT_ASSERT(bundle.get()); +} + + +/*------------------------------------------------------------------------------ + * Clean up the test environment + *----------------------------------------------------------------------------*/ +void +MetadataTypeContainerTest :: tearDown(void) throw () +{ + bundle.reset(); +} + + +/*------------------------------------------------------------------------------ + * Test to see if the singleton Hello object is accessible + *----------------------------------------------------------------------------*/ +void +MetadataTypeContainerTest :: firstTest(void) + throw (CPPUNIT_NS::Exception) +{ + Ptr::Ref metadataType; + Ptr::Ref container; + bool gotException; + + // test configuration from a configuration file + try { + Ptr::Ref parser( + new xmlpp::DomParser(configFileName, true)); + const xmlpp::Document * document = parser->get_document(); + const xmlpp::Element * root = document->get_root_node(); + + container.reset(new MetadataTypeContainer(bundle)); + container->configure(*root); + + } catch (std::invalid_argument &e) { + CPPUNIT_FAIL(std::string("semantic error in configuration file:\n") + + e.what()); + } catch (xmlpp::exception &e) { + CPPUNIT_FAIL(std::string("XML error in configuration file:\n") + + e.what()); + } + + // test double-configuration + try { + Ptr::Ref parser( + new xmlpp::DomParser(configFileName, true)); + const xmlpp::Document * document = parser->get_document(); + const xmlpp::Element * root = document->get_root_node(); + + gotException = false; + try { + container->configure(*root); + } catch (std::invalid_argument &e) { + gotException = true; + } + CPPUNIT_ASSERT(gotException); + + } catch (xmlpp::exception &e) { + CPPUNIT_FAIL(std::string("XML error in configuration file:\n") + + e.what()); + } + + // a simple positive check on the DC name + CPPUNIT_ASSERT(container->existsByDcName("dc:creator")); + metadataType = container->getByDcName("dc:creator"); + + CPPUNIT_ASSERT(*metadataType->getDcName() == "dc:creator"); + CPPUNIT_ASSERT(*metadataType->getId3Tag() == "TPE2"); + CPPUNIT_ASSERT(*metadataType->getLocalizationKey() == "dc_creator"); + + // a negative check on the DC name + CPPUNIT_ASSERT(!container->existsByDcName("dc:nonExistent")); + + gotException = false; + try { + container->getByDcName("dc:nonExistent"); + } catch (std::invalid_argument &e) { + gotException = true; + } + CPPUNIT_ASSERT(gotException); + + // a simple positive check on the ID3v2 tag + CPPUNIT_ASSERT(container->existsById3Tag("TPE2")); + metadataType = container->getById3Tag("TPE2"); + + CPPUNIT_ASSERT(*metadataType->getDcName() == "dc:creator"); + CPPUNIT_ASSERT(*metadataType->getId3Tag() == "TPE2"); + CPPUNIT_ASSERT(*metadataType->getLocalizationKey() == "dc_creator"); + + // a negative check on the DC name + CPPUNIT_ASSERT(!container->existsByDcName("NonExistentTag")); + + gotException = false; + try { + container->getByDcName("NonExistentTag"); + } catch (std::invalid_argument &e) { + gotException = true; + } + CPPUNIT_ASSERT(gotException); +} + + +/*------------------------------------------------------------------------------ + * Test the MetadataType class + *----------------------------------------------------------------------------*/ +void +MetadataTypeContainerTest :: metadataTypeTest(void) + throw (CPPUNIT_NS::Exception) +{ + Ptr::Ref container; + + // create a container + try { + Ptr::Ref parser( + new xmlpp::DomParser(configFileName, true)); + const xmlpp::Document * document = parser->get_document(); + const xmlpp::Element * root = document->get_root_node(); + + container.reset(new MetadataTypeContainer(bundle)); + container->configure(*root); + + } catch (std::invalid_argument &e) { + CPPUNIT_FAIL(std::string("semantic error in configuration file:\n") + + e.what()); + } catch (xmlpp::exception &e) { + CPPUNIT_FAIL(std::string("XML error in configuration file:\n") + + e.what()); + } + + Ptr::Ref metadataType; + + // first check the trivial constructor + metadataType.reset(new MetadataType(container, + "dc:creator", + "TPE2", + "dc_creator")); + + CPPUNIT_ASSERT(*metadataType->getDcName() == "dc:creator"); + CPPUNIT_ASSERT(*metadataType->getId3Tag() == "TPE2"); + CPPUNIT_ASSERT(*metadataType->getLocalizationKey() == "dc_creator"); + metadataType.reset(); + + // test configuration from a configuration file + try { + Ptr::Ref parser( + new xmlpp::DomParser(metadataTypeConfigFileName, true)); + const xmlpp::Document * document = parser->get_document(); + const xmlpp::Element * root = document->get_root_node(); + + metadataType.reset(new MetadataType(container)); + metadataType->configure(*root); + + CPPUNIT_ASSERT(*metadataType->getDcName() == "dc:creator"); + CPPUNIT_ASSERT(*metadataType->getId3Tag() == "TPE2"); + CPPUNIT_ASSERT(*metadataType->getLocalizationKey() == "dc_creator"); + } catch (std::invalid_argument &e) { + CPPUNIT_FAIL(std::string("semantic error in configuration file:\n") + + e.what()); + } catch (xmlpp::exception &e) { + CPPUNIT_FAIL(std::string("XML error in configuration file:\n") + + e.what()); + } +} + + +/*------------------------------------------------------------------------------ + * Test the iterator feature of the container. + *----------------------------------------------------------------------------*/ +void +MetadataTypeContainerTest :: iteratorTest(void) + throw (CPPUNIT_NS::Exception) +{ + Ptr::Ref metadataType; + Ptr::Ref container; + MetadataTypeContainer::Vector::const_iterator it; + MetadataTypeContainer::Vector::const_iterator end; + + // test on an empty container + container.reset(new MetadataTypeContainer(bundle)); + it = container->begin(); + end = container->end(); + CPPUNIT_ASSERT(it == end); + container.reset(); + + // test configuration from a configuration file + try { + Ptr::Ref parser( + new xmlpp::DomParser(configFileName, true)); + const xmlpp::Document * document = parser->get_document(); + const xmlpp::Element * root = document->get_root_node(); + + container.reset(new MetadataTypeContainer(bundle)); + container->configure(*root); + + } catch (std::invalid_argument &e) { + CPPUNIT_FAIL(std::string("semantic error in configuration file:\n") + + e.what()); + } catch (xmlpp::exception &e) { + CPPUNIT_FAIL(std::string("XML error in configuration file:\n") + + e.what()); + } + + // cycle through the iterator, should be one element + it = container->begin(); + end = container->end(); + while (it != end) { + metadataType = (Ptr::Ref) *it; + CPPUNIT_ASSERT(*metadataType->getDcName() == "dc:creator"); + CPPUNIT_ASSERT(*metadataType->getId3Tag() == "TPE2"); + CPPUNIT_ASSERT(*metadataType->getLocalizationKey() == "dc_creator"); + + ++it; + } +} + + +/*------------------------------------------------------------------------------ + * Test localized metadata type names. + *----------------------------------------------------------------------------*/ +void +MetadataTypeContainerTest :: localizedTest(void) + throw (CPPUNIT_NS::Exception) +{ + Ptr::Ref metadataType; + Ptr::Ref container; + + // test configuration from a configuration file + try { + Ptr::Ref parser( + new xmlpp::DomParser(configFileName, true)); + const xmlpp::Document * document = parser->get_document(); + const xmlpp::Element * root = document->get_root_node(); + + container.reset(new MetadataTypeContainer(bundle)); + container->configure(*root); + + } catch (std::invalid_argument &e) { + CPPUNIT_FAIL(std::string("semantic error in configuration file:\n") + + e.what()); + } catch (xmlpp::exception &e) { + CPPUNIT_FAIL(std::string("XML error in configuration file:\n") + + e.what()); + } + + CPPUNIT_ASSERT(container->existsByDcName("dc:creator")); + metadataType = container->getByDcName("dc:creator"); + + CPPUNIT_ASSERT(*metadataType->getLocalizedName() == "Creator"); + + UErrorCode status = U_ZERO_ERROR; + Ptr::Ref huBundle; + Ptr::Ref jpBundle; + Ptr::Ref ustr; + + // test with hungarian + huBundle.reset(new ResourceBundle("./tmp/" PACKAGE_NAME, "hu", status)); + CPPUNIT_ASSERT(U_SUCCESS(status)); + container->setBundle(huBundle); + + ustr = metadataType->getLocalizedName(); + CPPUNIT_ASSERT((*ustr)[0] == 0x004c); // 'L' + CPPUNIT_ASSERT((*ustr)[1] == 0x00e9); // 'e' with acute + CPPUNIT_ASSERT((*ustr)[2] == 0x0074); // 't' + CPPUNIT_ASSERT((*ustr)[3] == 0x0072); // 'r' + CPPUNIT_ASSERT((*ustr)[4] == 0x0065); // 'e' + CPPUNIT_ASSERT((*ustr)[5] == 0x0068); // 'h' + CPPUNIT_ASSERT((*ustr)[6] == 0x006f); // 'o' + CPPUNIT_ASSERT((*ustr)[7] == 0x007a); // 'z' + CPPUNIT_ASSERT((*ustr)[8] == 0x00f3); // 'o' with acute + + // test with japanese + jpBundle.reset(new ResourceBundle("./tmp/" PACKAGE_NAME, "jp", status)); + CPPUNIT_ASSERT(U_SUCCESS(status)); + container->setBundle(jpBundle); + + ustr = metadataType->getLocalizedName(); + CPPUNIT_ASSERT((*ustr)[0] == 0x30af); // katakana ku + CPPUNIT_ASSERT((*ustr)[1] == 0x30ea); // katakana ri + CPPUNIT_ASSERT((*ustr)[2] == 0x30a8); // katakana e + CPPUNIT_ASSERT((*ustr)[3] == 0x30fc); // katakana '-' + CPPUNIT_ASSERT((*ustr)[4] == 0x30bf); // katakana ta +} + diff --git a/livesupport/modules/core/src/MetadataTypeContainerTest.h b/livesupport/modules/core/src/MetadataTypeContainerTest.h new file mode 100644 index 000000000..19f6ee56e --- /dev/null +++ b/livesupport/modules/core/src/MetadataTypeContainerTest.h @@ -0,0 +1,139 @@ +/*------------------------------------------------------------------------------ + + Copyright (c) 2004 Media Development Loan Fund + + This file is part of the LiveSupport project. + http://livesupport.campware.org/ + To report bugs, send an e-mail to bugs@campware.org + + LiveSupport is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + LiveSupport is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with LiveSupport; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + + Author : $Author: maroy $ + Version : $Revision: 1.1 $ + Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/MetadataTypeContainerTest.h,v $ + +------------------------------------------------------------------------------*/ +#ifndef MetadataTypeContainerTest_h +#define MetadataTypeContainerTest_h + +#ifndef __cplusplus +#error This is a C++ include file +#endif + + +/* ============================================================ include files */ + +#ifdef HAVE_CONFIG_H +#include "configure.h" +#endif + +#include + + +namespace LiveSupport { +namespace Core { + +/* ================================================================ constants */ + + +/* =================================================================== macros */ + + +/* =============================================================== data types */ + +/** + * Unit test for the MetadataTypeContainer class. + * + * @author $Author: maroy $ + * @version $Revision: 1.1 $ + * @see MetadataTypeContainer + */ +class MetadataTypeContainerTest : public CPPUNIT_NS::TestFixture +{ + CPPUNIT_TEST_SUITE(MetadataTypeContainerTest); + CPPUNIT_TEST(firstTest); + CPPUNIT_TEST(metadataTypeTest); + CPPUNIT_TEST(iteratorTest); + CPPUNIT_TEST(localizedTest); + CPPUNIT_TEST_SUITE_END(); + + protected: + + /** + * The resource bundle used by the container. + */ + Ptr::Ref bundle; + + /** + * A simple test. + * + * @exception CPPUNIT_NS::Exception on test failures. + */ + void + firstTest(void) throw (CPPUNIT_NS::Exception); + + /** + * Test the MetadataType class (which needs a container as well) + * + * @exception CPPUNIT_NS::Exception on test failures. + */ + void + metadataTypeTest(void) throw (CPPUNIT_NS::Exception); + + /** + * Testing the iterators available from the container. + * + * @exception CPPUNIT_NS::Exception on test failures. + */ + void + iteratorTest(void) throw (CPPUNIT_NS::Exception); + + /** + * Test to see if localized metadata type names work. + * + * @exception CPPUNIT_NS::Exception on test failures. + */ + void + localizedTest(void) throw (CPPUNIT_NS::Exception); + + + public: + + /** + * Set up the environment for the test case. + */ + void + setUp(void) throw (); + + /** + * Clean up the environment after the test case. + */ + void + tearDown(void) throw (); +}; + + +/* ================================================= external data structures */ + + +/* ====================================================== function prototypes */ + + +} // namespace Core +} // namespace LiveSupport + +#endif // MetadataTypeContainerTest_h + diff --git a/livesupport/modules/core/var/hu.txt b/livesupport/modules/core/var/hu.txt index 7bbdc07a4..40f248202 100644 --- a/livesupport/modules/core/var/hu.txt +++ b/livesupport/modules/core/var/hu.txt @@ -5,5 +5,7 @@ hu:table foo:string { "fú" } bar:string { "bár" } } + + dc_creator:string { "Létrehozó" } } diff --git a/livesupport/modules/core/var/jp.txt b/livesupport/modules/core/var/jp.txt index ad23d8782..050f069be 100644 --- a/livesupport/modules/core/var/jp.txt +++ b/livesupport/modules/core/var/jp.txt @@ -5,5 +5,7 @@ jp:table foo:string { "ふ" } bar:string { "ばる" } } + + dc_creator:string { "クリエーター" } } diff --git a/livesupport/modules/core/var/root.txt b/livesupport/modules/core/var/root.txt index cd191b362..66a3657ac 100644 --- a/livesupport/modules/core/var/root.txt +++ b/livesupport/modules/core/var/root.txt @@ -11,5 +11,7 @@ root:table aMessage:string { "parameter 0: {0}, parameter 1: {1}" } aMessage3Args:string { "p0: {0}, p2: {2}, p1: {1}" } } + + dc_creator:string { "Creator" } }