void mitk::CoreObjectFactoryBase::CreateFileExtensions(MultimapType fileExtensionsMap, std::string& fileExtensions)
{
  std::map<std::string, std::string> aMap;

  // group the extensions by extension-group
  // e.g. aMap["DICOM files"] = "*.dcm *.DCM *.dc3 *.DC3 *.gdcm"
  for (MultimapType::iterator it = fileExtensionsMap.begin(); it != fileExtensionsMap.end(); ++it)
  {
    std::string aValue = aMap[(*it).second];
    if (aValue.compare("") != 0)
    {
      aValue.append(" ");
    }
    aValue.append((*it).first);
    aMap[(*it).second] = aValue;
  }

  // build the "all" entry (it contains all the extensions)
  // and add it to the string in the first position
  // e.g. "all (*.dcm *.DCM *.dc3 *.DC3 *.gdcm *.ima *.mhd ... *.vti *.hdr *.nrrd *.nhdr );;"
  fileExtensions = "known extensions (";
  std::string lastKey = "";
  for (MultimapType::iterator it = fileExtensionsMap.begin(); it != fileExtensionsMap.end(); ++it)
  {
    std::string aKey = (*it).first;

    if (aKey.compare(lastKey) != 0)
    {
      if (lastKey.compare("") != 0)
      {
        fileExtensions.append(" ");
      }
      fileExtensions.append(aKey);
    }
    lastKey = aKey;
  }
  fileExtensions.append(");;all (*);;");

  // build the entry for each extension-group
  // e.g. "Sets of 2D slices (*.pic *.pic.gz *.bmp *.png *.dcm *.gdcm *.ima *.tiff);;"
  for (std::map<std::string, std::string>::iterator it = aMap.begin(); it != aMap.end(); ++it)
  {
    //cout << "  [" << (*it).first << ", " << (*it).second << "]" << endl;
    std::string aKey = (*it).first;
    if (aKey.compare("") != 0)
    {
      fileExtensions.append((*it).first);
      fileExtensions.append(" (");
      fileExtensions.append((*it).second);
      fileExtensions.append(");;");
    }
  }
}
void mitk::CoreObjectFactory::MergeFileExtensions(MultimapType& fileExtensionsMap, MultimapType inputMap)
{
  bool duplicateFound = false;
  std::pair<MultimapType::iterator, MultimapType::iterator> pairOfIter;
  for (MultimapType::iterator it = inputMap.begin(); it != inputMap.end(); ++it)
  {
    duplicateFound = false;
    pairOfIter = fileExtensionsMap.equal_range((*it).first);
    for (MultimapType::iterator it2 = pairOfIter.first; it2 != pairOfIter.second; ++it2)
    {
      //cout << "  [" << (*it).first << ", " << (*it).second << "]" << endl;
      std::string aString = (*it2).second;
      if (aString.compare((*it).second) == 0)
      {
        //cout << "  DUP!! [" << (*it).first << ", " << (*it).second << "]" << endl;
        duplicateFound = true;
        break;
      }
    }
    if (!duplicateFound)
    {
      fileExtensionsMap.insert(std::pair<std::string, std::string>((*it).first, (*it).second));
    }
  }
}
void mitk::CoreObjectFactory::RegisterLegacyWriters(mitk::CoreObjectFactoryBase *factory)
{
  // Get all external Writers
  mitk::CoreObjectFactory::FileWriterList writers = factory->GetFileWriters();

  // We are not really interested in the string, just call the method since
  // many writers initialize the map the first time when this method is called
  factory->GetSaveFileExtensions();

  MultimapType fileExtensionMap = factory->GetSaveFileExtensionsMap();

  for (mitk::CoreObjectFactory::FileWriterList::iterator it = writers.begin(); it != writers.end(); ++it)
  {
    std::vector<std::string> extensions = (*it)->GetPossibleFileExtensions();
    if (extensions.empty())
      continue;

    std::string description;
    for (std::vector<std::string>::iterator ext = extensions.begin(); ext != extensions.end(); ++ext)
    {
      if (ext->empty())
        continue;

      std::string extension = *ext;
      std::string extensionWithStar = extension;
      if (extension.find_first_of('*') == 0)
      {
        // remove "*."
        extension = extension.substr(0, extension.size() - 2);
      }
      else
      {
        extensionWithStar.insert(extensionWithStar.begin(), '*');
      }

      for (MultimapType::iterator fileExtensionIter = fileExtensionMap.begin();
           fileExtensionIter != fileExtensionMap.end();
           ++fileExtensionIter)
      {
        if (fileExtensionIter->first == extensionWithStar)
        {
          description = fileExtensionIter->second;
          break;
        }
      }
      if (!description.empty())
        break;
    }
    if (description.empty())
    {
      description = std::string("Legacy ") + (*it)->GetNameOfClass() + " Reader";
    }

    mitk::FileWriter::Pointer fileWriter(it->GetPointer());
    mitk::LegacyFileWriterService *lfws = new mitk::LegacyFileWriterService(fileWriter, description);
    m_LegacyWriters[factory].push_back(lfws);
  }
}