Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes

mitk::PropertyListSerializer Class Reference

Serializes a mitk::PropertyList. More...

#include <mitkPropertyListSerializer.h>

List of all members.

Public Types

typedef PropertyListSerializer Self
typedef itk::Object Superclass
typedef itk::SmartPointer< SelfPointer
typedef itk::SmartPointer
< const Self
ConstPointer

Public Member Functions

virtual const char * GetClassName () const
virtual void SetFilenameHint (const char *_arg)
virtual const char * GetFilenameHint () const
virtual void SetWorkingDirectory (const char *_arg)
virtual const char * GetWorkingDirectory () const
virtual void SetPropertyList (PropertyList *_arg)
virtual std::string Serialize ()
 Serializes given PropertyList object.
PropertyListGetFailedProperties ()

Static Public Member Functions

static Pointer New ()

Protected Member Functions

 PropertyListSerializer ()
virtual ~PropertyListSerializer ()
TiXmlElementSerializeOneProperty (const std::string &key, const BaseProperty *property)

Protected Attributes

std::string m_FilenameHint
std::string m_WorkingDirectory
PropertyList::Pointer m_PropertyList
PropertyList::Pointer m_FailedProperties

Detailed Description

Serializes a mitk::PropertyList.

Definition at line 35 of file mitkPropertyListSerializer.h.


Member Typedef Documentation

typedef itk::SmartPointer<const Self> mitk::PropertyListSerializer::ConstPointer

Definition at line 39 of file mitkPropertyListSerializer.h.

typedef itk::SmartPointer<Self> mitk::PropertyListSerializer::Pointer

Definition at line 39 of file mitkPropertyListSerializer.h.

Definition at line 39 of file mitkPropertyListSerializer.h.

Definition at line 39 of file mitkPropertyListSerializer.h.


Constructor & Destructor Documentation

mitk::PropertyListSerializer::PropertyListSerializer (  ) [protected]

Definition at line 27 of file mitkPropertyListSerializer.cpp.

: m_FilenameHint("unnamed")
, m_WorkingDirectory("")
{
}
mitk::PropertyListSerializer::~PropertyListSerializer (  ) [protected, virtual]

Definition at line 33 of file mitkPropertyListSerializer.cpp.

{
}

Member Function Documentation

virtual const char* mitk::PropertyListSerializer::GetClassName (  ) const [virtual]
mitk::PropertyList * mitk::PropertyListSerializer::GetFailedProperties (  )

Definition at line 157 of file mitkPropertyListSerializer.cpp.

{
  if (m_FailedProperties.IsNotNull() && !m_FailedProperties->IsEmpty())
  {
    return m_FailedProperties;
  }
  else
  {
    return NULL;
  }
}
virtual const char* mitk::PropertyListSerializer::GetFilenameHint (  ) const [virtual]
virtual const char* mitk::PropertyListSerializer::GetWorkingDirectory (  ) const [virtual]
static Pointer mitk::PropertyListSerializer::New (  ) [static]
std::string mitk::PropertyListSerializer::Serialize (  ) [virtual]

Serializes given PropertyList object.

Returns:
the filename of the newly created file.

Definition at line 37 of file mitkPropertyListSerializer.cpp.

References MITK_ERROR, mitk::PropertyList::New(), and TiXmlElement::SetAttribute().

{
  m_FailedProperties = PropertyList::New();

  if ( m_PropertyList.IsNull() && m_PropertyList->IsEmpty() )
  {
    MITK_ERROR << "Not serializing NULL or empty PropertyList";
    return "";
  }

  // tmpname
  static unsigned long count = 1;
        unsigned long n = count++;
  std::ostringstream name;
  for (int i = 0; i < 6; ++i)
        {
                name << char('a' + (n % 26));
                n /= 26;
        }
  std::string filename;
  filename.append(name.str());

  std::string fullname(m_WorkingDirectory);
  fullname += "/";
  fullname += filename;
  fullname = itksys::SystemTools::ConvertToOutputPath(fullname.c_str());

  TiXmlDocument document;
  TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0", "", "" ); // TODO what to write here? encoding? etc....
  document.LinkEndChild( decl );

  TiXmlElement* version = new TiXmlElement("Version");
  version->SetAttribute("Writer",  __FILE__ );
  version->SetAttribute("Revision",  "$Revision: 17055 $" );
  version->SetAttribute("FileVersion",  1 );
  document.LinkEndChild(version);

  // add XML contents
  const PropertyList::PropertyMap* propmap = m_PropertyList->GetMap();
  for ( PropertyList::PropertyMap::const_iterator iter = propmap->begin();
        iter != propmap->end();
        ++iter )
  {
    std::string key = iter->first;
    const BaseProperty* property = iter->second.first;
    TiXmlElement* element = SerializeOneProperty( key, property );
    if (element)
    {
      document.LinkEndChild( element );
      // TODO test serializer for error
    }
    else
    {
      m_FailedProperties->ReplaceProperty( key, const_cast<BaseProperty*>(property) );
    }
  }
 
  // save XML file
  if ( !document.SaveFile( fullname ) )
  {
    MITK_ERROR << "Could not write PropertyList to " << fullname << "\nTinyXML reports '" << document.ErrorDesc() << "'";
    return "";
  }

  return filename;
}
TiXmlElement * mitk::PropertyListSerializer::SerializeOneProperty ( const std::string &  key,
const BaseProperty property 
) [protected]

Definition at line 104 of file mitkPropertyListSerializer.cpp.

References TiXmlNode::LinkEndChild(), MITK_ERROR, MITK_WARN, and TiXmlElement::SetAttribute().

{
  TiXmlElement* keyelement = new TiXmlElement("property");
  keyelement->SetAttribute("key", key);
  keyelement->SetAttribute("type", property->GetNameOfClass());
  
  // construct name of serializer class
  std::string serializername(property->GetNameOfClass());
  serializername += "Serializer";
  
  std::list<itk::LightObject::Pointer> allSerializers = itk::ObjectFactoryBase::CreateAllInstance(serializername.c_str());
  if (allSerializers.size() < 1)
  {
    MITK_ERROR << "No serializer found for " << property->GetNameOfClass() << ". Skipping object";
    m_FailedProperties->ReplaceProperty( key, const_cast<BaseProperty*>(property) );
  }
  if (allSerializers.size() > 1)
  {
    MITK_WARN << "Multiple serializers found for " << property->GetNameOfClass() << "Using arbitrarily the first one.";
  }

  for ( std::list<itk::LightObject::Pointer>::iterator iter = allSerializers.begin();
        iter != allSerializers.end();
        ++iter )
  {
    if (BasePropertySerializer* serializer = dynamic_cast<BasePropertySerializer*>( iter->GetPointer() ) )
    {
      serializer->SetProperty(property);
      try
      {
        TiXmlElement* valueelement = serializer->Serialize();
        if (valueelement)
        {
          keyelement->LinkEndChild( valueelement );
          // \TODO: put 'return keyelement;' here?
        }
        else
        {
          m_FailedProperties->ReplaceProperty( key, const_cast<BaseProperty*>(property) );
        }
      }
      catch (std::exception& e)
      {
        MITK_ERROR << "Serializer " << serializer->GetNameOfClass() << " failed: " << e.what();
        m_FailedProperties->ReplaceProperty( key, const_cast<BaseProperty*>(property) );
        // \TODO: log only if all potential serializers fail?
      }
      break;
    }
  }
  return keyelement;
}
virtual void mitk::PropertyListSerializer::SetFilenameHint ( const char *  _arg ) [virtual]
virtual void mitk::PropertyListSerializer::SetPropertyList ( PropertyList _arg ) [virtual]
virtual void mitk::PropertyListSerializer::SetWorkingDirectory ( const char *  _arg ) [virtual]

Member Data Documentation

Definition at line 69 of file mitkPropertyListSerializer.h.

Definition at line 65 of file mitkPropertyListSerializer.h.

Definition at line 67 of file mitkPropertyListSerializer.h.

Definition at line 66 of file mitkPropertyListSerializer.h.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines