From 5a9186dd5c058b75f82c489c489a789d0ce30394 Mon Sep 17 00:00:00 2001
From: Owen Arnold <owen.arnold@stfc.ac.uk>
Date: Thu, 22 Nov 2012 14:45:37 +0000
Subject: [PATCH] refs #6216. Better file type identification.

---
 .../PeaksReader/vtkPeaksReader.cxx            | 23 +++++++++++++++++--
 .../inc/MantidVatesAPI/MDEWLoadingPresenter.h |  3 +++
 .../src/EventNexusLoadingPresenter.cpp        |  6 ++++-
 .../src/MDEWEventNexusLoadingPresenter.cpp    |  7 +++++-
 .../VatesAPI/src/MDEWLoadingPresenter.cpp     | 19 ++++++++++++++-
 .../VatesAPI/test/MDEWLoadingPresenterTest.h  | 22 ++++++++++++++++++
 6 files changed, 75 insertions(+), 5 deletions(-)

diff --git a/Code/Mantid/Vates/ParaviewPlugins/ParaViewReaders/PeaksReader/vtkPeaksReader.cxx b/Code/Mantid/Vates/ParaviewPlugins/ParaViewReaders/PeaksReader/vtkPeaksReader.cxx
index f4927de1ac4..cfd9400233d 100644
--- a/Code/Mantid/Vates/ParaviewPlugins/ParaViewReaders/PeaksReader/vtkPeaksReader.cxx
+++ b/Code/Mantid/Vates/ParaviewPlugins/ParaViewReaders/PeaksReader/vtkPeaksReader.cxx
@@ -18,6 +18,8 @@
 #include "MantidAPI/AlgorithmManager.h"
 #include <vtkPVGlyphFilter.h>
 
+#include <boost/algorithm/string.hpp>    
+
 vtkCxxRevisionMacro(vtkPeaksReader, "$Revision: 1.0 $");
 vtkStandardNewMacro(vtkPeaksReader);
 
@@ -137,9 +139,26 @@ void vtkPeaksReader::PrintSelf(ostream& os, vtkIndent indent)
   this->Superclass::PrintSelf(os,indent);
 }
 
-int vtkPeaksReader::CanReadFile(const char* vtkNotUsed(fname))
+int vtkPeaksReader::CanReadFile(const char* fname)
 {
-  return 1; //TODO: Apply checks here.
+  const std::string fileString(fname);
+  const int startExtension = fileString.find_last_of('.');
+  const int endExtension = fileString.length();
+  if(startExtension >= endExtension)
+  {
+    throw std::runtime_error("File has no extension.");
+  }
+  std::string extension = fileString.substr(startExtension, endExtension - startExtension);
+  boost::algorithm::to_lower(extension);
+  boost::algorithm::trim(extension);
+  if(extension == ".peaks")
+  {
+    return 1;
+  }
+  else
+  {
+    return 0; 
+  }
 }
 
 unsigned long vtkPeaksReader::GetMTime()
diff --git a/Code/Mantid/Vates/VatesAPI/inc/MantidVatesAPI/MDEWLoadingPresenter.h b/Code/Mantid/Vates/VatesAPI/inc/MantidVatesAPI/MDEWLoadingPresenter.h
index 05f43dcd76f..6997023a54f 100644
--- a/Code/Mantid/Vates/VatesAPI/inc/MantidVatesAPI/MDEWLoadingPresenter.h
+++ b/Code/Mantid/Vates/VatesAPI/inc/MantidVatesAPI/MDEWLoadingPresenter.h
@@ -56,6 +56,7 @@ namespace Mantid
       Mantid::Geometry::IMDDimension_sptr tDimension;
       virtual void appendMetadata(vtkDataSet* visualDataSet, const std::string& wsName) ;
       virtual void extractMetadata(Mantid::API::IMDEventWorkspace_sptr eventWs);
+      virtual bool canLoadFileBasedOnExtension(const std::string& filename, const std::string& expectedExtension) const;
       virtual bool shouldLoad();
       bool m_isSetup;
       double m_time;
@@ -64,6 +65,8 @@ namespace Mantid
       bool m_firstLoad;
     };
 
+    
+
   }
 }
 
diff --git a/Code/Mantid/Vates/VatesAPI/src/EventNexusLoadingPresenter.cpp b/Code/Mantid/Vates/VatesAPI/src/EventNexusLoadingPresenter.cpp
index d12ac8ea93a..5587c3b30e2 100644
--- a/Code/Mantid/Vates/VatesAPI/src/EventNexusLoadingPresenter.cpp
+++ b/Code/Mantid/Vates/VatesAPI/src/EventNexusLoadingPresenter.cpp
@@ -11,7 +11,6 @@
 
 #include <vtkUnstructuredGrid.h>
 
-
 namespace Mantid
 {
   namespace VATES
@@ -42,6 +41,11 @@ namespace Mantid
     */
     bool EventNexusLoadingPresenter::canReadFile() const
     {
+      if(!canLoadFileBasedOnExtension(m_filename, ".nxs"))
+      {
+        return 0;
+      }
+
       ::NeXus::File * file = NULL;
       try
       {
diff --git a/Code/Mantid/Vates/VatesAPI/src/MDEWEventNexusLoadingPresenter.cpp b/Code/Mantid/Vates/VatesAPI/src/MDEWEventNexusLoadingPresenter.cpp
index 1f8dcdd5219..824f37c9fb2 100644
--- a/Code/Mantid/Vates/VatesAPI/src/MDEWEventNexusLoadingPresenter.cpp
+++ b/Code/Mantid/Vates/VatesAPI/src/MDEWEventNexusLoadingPresenter.cpp
@@ -8,7 +8,6 @@
 #include "MantidNexusCPP/NeXusFile.hpp"
 #include "MantidNexusCPP/NeXusException.hpp"
 #include "MantidAPI/AlgorithmManager.h"
-
 #include <vtkUnstructuredGrid.h>
 
 namespace Mantid
@@ -42,6 +41,12 @@ namespace Mantid
     */
     bool MDEWEventNexusLoadingPresenter::canReadFile() const
     {
+      // Quick check based on extension.
+      if(!canLoadFileBasedOnExtension(m_filename, ".nxs"))
+      {
+        return 0;
+      }
+
       ::NeXus::File * file = NULL;
 
       file = new ::NeXus::File(this->m_filename);
diff --git a/Code/Mantid/Vates/VatesAPI/src/MDEWLoadingPresenter.cpp b/Code/Mantid/Vates/VatesAPI/src/MDEWLoadingPresenter.cpp
index 982844eee55..df22ab19a24 100644
--- a/Code/Mantid/Vates/VatesAPI/src/MDEWLoadingPresenter.cpp
+++ b/Code/Mantid/Vates/VatesAPI/src/MDEWLoadingPresenter.cpp
@@ -7,7 +7,7 @@
 #include "MantidVatesAPI/MetadataToFieldData.h"
 #include "MantidVatesAPI/RebinningCutterXMLDefinitions.h"
 
-
+#include <boost/algorithm/string.hpp>
 #include <vtkFieldData.h>
 #include <vtkDataSet.h>
 
@@ -116,6 +116,23 @@ namespace Mantid
       //Return decision.
       return bExecute;
     }
+
+    /**
+    Determines wheter the file can be loaded based on it's extension.
+    @param filename containing the extension
+    @param expectedExtension expected extension for the file to have
+    @return TRUE, only if the extension is approved.
+    */
+    bool MDEWLoadingPresenter::canLoadFileBasedOnExtension(const std::string& filename, const std::string& expectedExtension) const
+    {
+       // Quick check based on extension.
+      const int startExtension = filename.find_last_of('.');
+      const int endExtension = filename.length();
+      std::string extension = filename.substr(startExtension, endExtension - startExtension);
+      boost::algorithm::to_lower(extension);
+      boost::algorithm::trim(extension);
+      return extension == expectedExtension;
+    }
     
     /*
     Append the geometry and function information onto the outgoing vtkDataSet.
diff --git a/Code/Mantid/Vates/VatesAPI/test/MDEWLoadingPresenterTest.h b/Code/Mantid/Vates/VatesAPI/test/MDEWLoadingPresenterTest.h
index 45fdacc3707..d7d9c8e8b3e 100644
--- a/Code/Mantid/Vates/VatesAPI/test/MDEWLoadingPresenterTest.h
+++ b/Code/Mantid/Vates/VatesAPI/test/MDEWLoadingPresenterTest.h
@@ -64,6 +64,12 @@ private:
       return BaseClass::shouldLoad();
     }
 
+    bool canLoadFileBasedOnExtension(const std::string& filename, const std::string& extension)
+    {
+      //Forwarding method.
+      return BaseClass::canLoadFileBasedOnExtension(filename, extension);
+    }
+
     ~ConcreteMDEWLoadingPresenter(){}
   };
 
@@ -164,6 +170,22 @@ void testDepthChanged()
     TSM_ASSERT("This is a 4D workspace with an integrated T dimension", presenter.hasTDimensionAvailable());
   }
 
+  void testCanLoadFileBasedOnExtension()
+  {
+    MockMDLoadingView* view = new MockMDLoadingView;
+
+    ConcreteMDEWLoadingPresenter presenter(view);
+
+    // constructive tests
+    TSM_ASSERT("Should be an exact match", presenter.canLoadFileBasedOnExtension("somefile.nxs", ".nxs"));
+    TSM_ASSERT("Should lowercase uppercase extension", presenter.canLoadFileBasedOnExtension("somefile.NXS", ".nxs"));
+    TSM_ASSERT("Should strip off whitespace", presenter.canLoadFileBasedOnExtension("somefile.nxs ", ".nxs"));
+    // destructive tests
+    TSM_ASSERT("Extensions do not match, should return false.", !presenter.canLoadFileBasedOnExtension("somefile.nx", ".nxs"));
+
+    delete view;
+  }
+
 
 };
 
-- 
GitLab