added MetadataType and MetadataTypeContainer classes

solution for issue #877, see http://bugs.campware.org/view.php?id=877
This commit is contained in:
maroy 2005-04-27 17:39:51 +00:00
parent a7b1bac0a2
commit 2aea2785ca
13 changed files with 1377 additions and 6 deletions

View file

@ -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 \

View file

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE metadataType [
<!ELEMENT metadataType EMPTY >
<!ATTLIST metadataType dcName NMTOKEN #REQUIRED >
<!ATTLIST metadataType id3Tag NMTOKEN #IMPLIED >
<!ATTLIST metadataType localizationKey NMTOKEN #REQUIRED >
]>
<metadataType dcName = "dc:creator"
id3Tag = "TPE2"
localizationKey = "dc_creator"
/>

View file

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE metadataTypeContainer [
<!ELEMENT metadataType EMPTY >
<!ATTLIST metadataType dcName NMTOKEN #REQUIRED >
<!ATTLIST metadataType id3Tag NMTOKEN #IMPLIED >
<!ATTLIST metadataType localizationKey NMTOKEN #REQUIRED >
<!ELEMENT metadataTypeContainer (metadataType+) >
]>
<metadataTypeContainer>
<metadataType dcName = "dc:creator"
id3Tag = "TPE2"
localizationKey = "dc_creator"
/>
</metadataTypeContainer>

View file

@ -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:
*
* <pre><code>
* <resourceBundle path = "path/to/Bundle"
* locale = "en"
* />
* </code></pre>
*
* <pre><code>
* <!DOCTYPE resourceBundle [
*
* <!ELEMENT resourceBundle EMPTY >
* <!ATTLIST resourceBundle path CDATA #REQUIRED >
* <!ATTLIST resourceBundle locale CDATA #REQUIRED >
*
* ]>
* </code></pre>
*
* @author $Author: maroy $
* @version $Revision: 1.1 $
* @version $Revision: 1.2 $
*/
class LocalizedConfigurable : public Configurable, public LocalizedObject
{

View file

@ -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 <map>
#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:
*
* <pre><code>
* <metadataType dcName = "dc:creator"
* id3Tag = "TPE2"
* localizationKey = "dc_creator"
* />
* </code></pre>
*
* The DTD for the expected XML element looks like the following:
*
* <pre><code>
* <!ELEMENT metadataType EMPTY >
* <!ATTLIST metadataType dcName NMTOKEN #REQUIRED >
* <!ATTLIST metadataType id3Tag NMTOKEN #IMPLIED >
* <!ATTLIST metadataType localizationKey NMTOKEN #REQUIRED >
* </code></pre>
*
*
* @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<MetadataTypeContainer>::Ref container;
/**
* The Dublic Core name of this metadata type.
*/
Ptr<Glib::ustring>::Ref dcName;
/**
* The ID3v2 tag for this metadata type.
*/
Ptr<Glib::ustring>::Ref id3Tag;
/**
* The localization key for this metadata type.
*/
Ptr<Glib::ustring>::Ref localizationKey;
public:
/**
* Default constructor.
*
* @param container the container this metadata type is held in.
*/
MetadataType(Ptr<MetadataTypeContainer>::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<MetadataTypeContainer>::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<const Glib::ustring>::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<const Glib::ustring>::Ref
getId3Tag(void) const throw ()
{
return id3Tag;
}
/**
* Return the localization key for the metadata type.
*
* @return the localization key for the metadata type.
*/
Ptr<const Glib::ustring>::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<const Glib::ustring>::Ref
getLocalizedName(void) const throw (std::invalid_argument);
};
/* ================================================= external data structures */
/* ====================================================== function prototypes */
} // namespace Core
} // namespace LiveSupport
#endif // LiveSupport_Core_MetadataType_h

View file

@ -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 <map>
#include <boost/enable_shared_from_this.hpp>
#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:
*
* <pre><code>
* <metadataTypeContainer>
* <metadataType ... />
* <metadataType ... />
* ...
* <metadataType ... />
* </metadataTypeContainer>
* </code></pre>
*
* The DTD for the expected XML element is the following:
*
* <pre><code>
* <!ELEMENT metadataTypeContainer (metadataType+) >
* </code></pre>
*
* 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<MetadataTypeContainer>
{
public:
/**
* A vector type holding contant MetadataType references.
*/
typedef std::vector<Ptr<const MetadataType>::Ref> Vector;
private:
/**
* Map type for storing MetadataType objects by Glib::ustrings.
*/
typedef std::map<Glib::ustring, Ptr<const MetadataType>::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<ResourceBundle>::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<const MetadataType>::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<const MetadataType>::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<const MetadataType>::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<const MetadataType>::Ref
getById3Tag(const Glib::ustring id3Tag)
throw (std::invalid_argument);
};
/* ================================================= external data structures */
/* ====================================================== function prototypes */
} // namespace Core
} // namespace LiveSupport
#endif // LiveSupport_Core_MetadataTypeContainer_h

View file

@ -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<MetadataTypeContainer>::Ref container)
throw ()
{
this->container = container;
}
/*------------------------------------------------------------------------------
* Constructor.
*----------------------------------------------------------------------------*/
MetadataType :: MetadataType(Ptr<MetadataTypeContainer>::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<const Glib::ustring>::Ref
MetadataType :: getLocalizedName(void) const
throw (std::invalid_argument)
{
return container->getResourceUstring(*localizationKey);
}

View file

@ -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<const xmlpp::Element*> (*it);
Ptr<MetadataType>::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<const MetadataType>::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<const MetadataType>::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];
}

View file

@ -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 <unistd.h>
#else
#error "Need unistd.h"
#endif
#include <string>
#include <iostream>
#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<xmlpp::DomParser>::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<const MetadataType>::Ref metadataType;
Ptr<MetadataTypeContainer>::Ref container;
bool gotException;
// test configuration from a configuration file
try {
Ptr<xmlpp::DomParser>::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<xmlpp::DomParser>::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<MetadataTypeContainer>::Ref container;
// create a container
try {
Ptr<xmlpp::DomParser>::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<MetadataType>::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<xmlpp::DomParser>::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<const MetadataType>::Ref metadataType;
Ptr<MetadataTypeContainer>::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<xmlpp::DomParser>::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<const MetadataType>::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<const MetadataType>::Ref metadataType;
Ptr<MetadataTypeContainer>::Ref container;
// test configuration from a configuration file
try {
Ptr<xmlpp::DomParser>::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<ResourceBundle>::Ref huBundle;
Ptr<ResourceBundle>::Ref jpBundle;
Ptr<const Glib::ustring>::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
}

View file

@ -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 <cppunit/extensions/HelperMacros.h>
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<ResourceBundle>::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

View file

@ -5,5 +5,7 @@ hu:table
foo:string { "fú" }
bar:string { "bár" }
}
dc_creator:string { "Létrehozó" }
}

View file

@ -5,5 +5,7 @@ jp:table
foo:string { "ふ" }
bar:string { "ばる" }
}
dc_creator:string { "クリエーター" }
}

View file

@ -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" }
}