diff --git a/Code/Mantid/Vates/ParaviewPlugins/ParaViewReaders/PeaksReader/vtkPeaksReader.cxx b/Code/Mantid/Vates/ParaviewPlugins/ParaViewReaders/PeaksReader/vtkPeaksReader.cxx index f4927de1ac456ada978fb03ffa06cd30a6abe824..cfd9400233dd4ba6f69b6a273c51e6b1a55fefeb 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 05f43dcd76f01d7f802165ff40874d2f5041e592..6997023a54fa6642839b643f098fdf368ab422c0 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 d12ac8ea93a57e8c2d403f0fe4a828cf9e247a64..5587c3b30e25195b0654c1dab61ac96c77124fee 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 1f8dcdd52193f8c9e2a0afd41d8553041b9ca9ed..824f37c9fb29b6e5206da653bc5403be939bf6cd 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 982844eee5560c50f501518f3672f560cf4969d0..df22ab19a2403cec29fa91524611cc27477a5b29 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 45fdacc3707c04fd6fe2c43dfadc949c1560e66f..d7d9c8e8b3e527bde613f846e017ca8b32aac694 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; + } + };