From 02638639dfe6b88d34049fbbcd2fbb85882083ac Mon Sep 17 00:00:00 2001
From: Cyrille Valladeau <cyrille.valladeau@c-s.fr>
Date: Tue, 8 Nov 2011 16:55:11 +0100
Subject: [PATCH] ENH: add control on filename setters

---
 .../otbWrapperApplication.cxx                 | 19 ++++--
 .../otbWrapperInputImageListParameter.cxx     | 14 +++-
 .../otbWrapperInputImageListParameter.h       |  6 +-
 .../otbWrapperInputImageParameter.cxx         |  4 +-
 .../otbWrapperInputImageParameter.h           |  2 +-
 ...otbWrapperInputVectorDataListParameter.cxx | 37 ++++++++---
 .../otbWrapperInputVectorDataListParameter.h  |  6 +-
 .../otbWrapperInputVectorDataParameter.h      | 64 ++++---------------
 Code/ApplicationEngine/otbWrapperParameter.h  |  2 +-
 .../otbWrapperCommandLineLauncher.cxx         | 11 +---
 10 files changed, 78 insertions(+), 87 deletions(-)

diff --git a/Code/ApplicationEngine/otbWrapperApplication.cxx b/Code/ApplicationEngine/otbWrapperApplication.cxx
index c359ee8e07..f00c34b437 100644
--- a/Code/ApplicationEngine/otbWrapperApplication.cxx
+++ b/Code/ApplicationEngine/otbWrapperApplication.cxx
@@ -614,7 +614,9 @@ void Application::SetParameterString(std::string parameter, std::string value)
   else if (dynamic_cast<InputImageParameter*>(param))
     {
     InputImageParameter* paramDown = dynamic_cast<InputImageParameter*>(param);
-    paramDown->SetFromFileName(value);
+    if (paramDown->SetFromFileName(value) == true )
+      otbAppLogCRITICAL( <<"Invalid image filename " << value <<".");
+      
     }
   else if (dynamic_cast<ComplexInputImageParameter*>(param))
     {
@@ -624,7 +626,8 @@ void Application::SetParameterString(std::string parameter, std::string value)
   else if (dynamic_cast<InputVectorDataParameter*>(param))
     {
     InputVectorDataParameter* paramDown = dynamic_cast<InputVectorDataParameter*>(param);
-    paramDown->SetFromFileName(value);
+    if (paramDown->SetFromFileName(value) == true )
+      otbAppLogCRITICAL( <<"Invalid vector data filename " << value <<".");
     }
   else if (dynamic_cast<OutputImageParameter*>(param))
     {
@@ -655,12 +658,14 @@ void Application::SetParameterStringList(std::string parameter, std::vector<std:
   if (dynamic_cast<InputImageListParameter*>(param))
     {
     InputImageListParameter* paramDown = dynamic_cast<InputImageListParameter*>(param);
-    paramDown->SetListFromFileName(value);
+    if( paramDown->SetListFromFileName(value) == true )
+    otbAppLogCRITICAL( <<"At least one image filename is invalid.");
     }
   else if (dynamic_cast<InputVectorDataListParameter*>(param))
      {
      InputVectorDataListParameter* paramDown = dynamic_cast<InputVectorDataListParameter*>(param);
-     paramDown->SetListFromFileName(value);
+     if( paramDown->SetListFromFileName(value) == true )
+       otbAppLogCRITICAL( <<"At least one vector data filename is invalid..");
      }
   else if (dynamic_cast<StringListParameter*>(param))
     {
@@ -1085,10 +1090,10 @@ Application::IsApplicationReady()
       it != paramList.end();
       ++it)
     {
+
     // Check all Input Parameters
-    if (!this->HasValue(*it)
-        && IsMandatory(*it)
-        && GetParameterByKey(*it)->GetRoot()->GetActive())
+    if ( !this->HasValue(*it)  && IsMandatory(*it) )
+      if( GetParameterByKey(*it)->IsRoot() || GetParameterByKey(*it)->GetRoot()->GetActive() )
       {
       return false;
       }
diff --git a/Code/ApplicationEngine/otbWrapperInputImageListParameter.cxx b/Code/ApplicationEngine/otbWrapperInputImageListParameter.cxx
index 495933315a..b9f96a973d 100644
--- a/Code/ApplicationEngine/otbWrapperInputImageListParameter.cxx
+++ b/Code/ApplicationEngine/otbWrapperInputImageListParameter.cxx
@@ -35,7 +35,7 @@ InputImageListParameter::~InputImageListParameter()
 {
 }
 
-void
+bool
 InputImageListParameter::SetListFromFileName(const std::vector<std::string> & filenames)
 {
   // First clear previous file choosen
@@ -73,7 +73,10 @@ InputImageListParameter::SetListFromFileName(const std::vector<std::string> & fi
     {
     SetActive(true);
     this->Modified();
+    return false;
     }
+
+  return true;
 }
 
 
@@ -86,7 +89,7 @@ InputImageListParameter::AddNullElement()
     this->Modified();
 }
 
-void
+bool
 InputImageListParameter::AddFromFileName(const std::string & filename)
 {
   // TODO : when the logger will be available, redirect the exception
@@ -103,6 +106,7 @@ InputImageListParameter::AddFromFileName(const std::string & filename)
     catch(itk::ExceptionObject & err)
       {
       this->ClearValue();
+      return true;
       }
   
     // everything went fine, store the object references
@@ -110,10 +114,11 @@ InputImageListParameter::AddFromFileName(const std::string & filename)
     m_ImageList->PushBack(reader->GetOutput());
     SetActive(true);
     this->Modified();
+    return false;
     }
 }
 
-void
+bool
 InputImageListParameter::SetNthFileName( const unsigned int id, const std::string & filename )
 {
   if( m_ReaderList->Size()<id )
@@ -135,11 +140,14 @@ InputImageListParameter::SetNthFileName( const unsigned int id, const std::strin
     catch(itk::ExceptionObject & err)
       {
       this->ClearValue();
+      return true;
+
       }
     m_ReaderList->SetNthElement(id, reader);
     m_ImageList->SetNthElement(id, reader->GetOutput());
 
     this->Modified();
+    return false;
     }
 }
 
diff --git a/Code/ApplicationEngine/otbWrapperInputImageListParameter.h b/Code/ApplicationEngine/otbWrapperInputImageListParameter.h
index b212f6dc56..1ed78c8a45 100644
--- a/Code/ApplicationEngine/otbWrapperInputImageListParameter.h
+++ b/Code/ApplicationEngine/otbWrapperInputImageListParameter.h
@@ -50,16 +50,16 @@ public:
   itkTypeMacro(InputImageListParameter, Parameter);
 
   /** Set image form a list of filename */
-  void SetListFromFileName(const std::vector<std::string> & filenames);
+  bool SetListFromFileName(const std::vector<std::string> & filenames);
 
   /** Add null element to lists. */
   void AddNullElement();
 
   /** Add an image from a filename */
-  void AddFromFileName(const std::string & filename);
+  bool AddFromFileName(const std::string & filename);
 
   /** Set one specific stored image filename. */
-  void SetNthFileName( const unsigned int id, const std::string & filename );
+  bool SetNthFileName( const unsigned int id, const std::string & filename );
 
 
   /** Get the stored image filename list */
diff --git a/Code/ApplicationEngine/otbWrapperInputImageParameter.cxx b/Code/ApplicationEngine/otbWrapperInputImageParameter.cxx
index b83eec67d7..87374459d3 100644
--- a/Code/ApplicationEngine/otbWrapperInputImageParameter.cxx
+++ b/Code/ApplicationEngine/otbWrapperInputImageParameter.cxx
@@ -39,7 +39,7 @@ InputImageParameter::~InputImageParameter()
 {
 }
 
-void
+bool
 InputImageParameter::SetFromFileName(const std::string& filename)
 {
   // First clear previous file choosen
@@ -58,12 +58,14 @@ InputImageParameter::SetFromFileName(const std::string& filename)
       }
     catch(itk::ExceptionObject & err)
       {
+      return true;
       }
 
     // the specified filename is valid => store the value
     m_FileName = filename;
     SetActive(true);
     }
+  return false;
 }
 
 
diff --git a/Code/ApplicationEngine/otbWrapperInputImageParameter.h b/Code/ApplicationEngine/otbWrapperInputImageParameter.h
index 937ec74dad..db9b392f68 100644
--- a/Code/ApplicationEngine/otbWrapperInputImageParameter.h
+++ b/Code/ApplicationEngine/otbWrapperInputImageParameter.h
@@ -48,7 +48,7 @@ public:
   itkTypeMacro(InputImageParameter, Parameter);
 
   /** Set value from filename */
-  void SetFromFileName(const std::string& filename);
+  bool SetFromFileName(const std::string& filename);
   itkGetConstMacro(FileName, std::string);
 
 
diff --git a/Code/ApplicationEngine/otbWrapperInputVectorDataListParameter.cxx b/Code/ApplicationEngine/otbWrapperInputVectorDataListParameter.cxx
index 4f710753d4..2411f09537 100644
--- a/Code/ApplicationEngine/otbWrapperInputVectorDataListParameter.cxx
+++ b/Code/ApplicationEngine/otbWrapperInputVectorDataListParameter.cxx
@@ -18,6 +18,8 @@
 #include "otbWrapperInputVectorDataListParameter.h"
 #include "itksys/SystemTools.hxx"
 
+#include "otbWrapperMacros.h"
+
 namespace otb
 {
 namespace Wrapper
@@ -35,7 +37,7 @@ InputVectorDataListParameter::~InputVectorDataListParameter()
 {
 }
 
-void
+bool
 InputVectorDataListParameter::SetListFromFileName(const std::vector<std::string> & filenames)
 {
   // First clear previous file choosen
@@ -73,7 +75,10 @@ InputVectorDataListParameter::SetListFromFileName(const std::vector<std::string>
     {
     SetActive(true);
     this->Modified();
+    return false;
     }
+
+ return true;
 }
 
 
@@ -86,7 +91,7 @@ InputVectorDataListParameter::AddNullElement()
     this->Modified();
 }
 
-void
+bool
 InputVectorDataListParameter::AddFromFileName(const std::string & filename)
 {
   // TODO : when the logger will be available, redirect the exception
@@ -96,24 +101,26 @@ InputVectorDataListParameter::AddFromFileName(const std::string & filename)
     {
     VectorDataFileReaderType::Pointer reader = VectorDataFileReaderType::New();
     reader->SetFileName(filename);
+
     try
       {
       reader->UpdateOutputInformation();
       }
     catch(itk::ExceptionObject & err)
       {
-      this->ClearValue();
+      return true;
       }
-  
+    
     // everything went fine, store the object references
     m_ReaderList->PushBack(reader);
     m_VectorDataList->PushBack(reader->GetOutput());
     SetActive(true);
     this->Modified();
+  return false;
     }
 }
 
-void
+bool
 InputVectorDataListParameter::SetNthFileName( const unsigned int id, const std::string & filename )
 {
   if( m_ReaderList->Size()<id )
@@ -128,6 +135,7 @@ InputVectorDataListParameter::SetNthFileName( const unsigned int id, const std::
     {
     VectorDataFileReaderType::Pointer reader = VectorDataFileReaderType::New();
     reader->SetFileName(filename);
+
     try
       {
       reader->UpdateOutputInformation();
@@ -135,11 +143,14 @@ InputVectorDataListParameter::SetNthFileName( const unsigned int id, const std::
     catch(itk::ExceptionObject & err)
       {
       this->ClearValue();
+      return true;
       }
+
     m_ReaderList->SetNthElement(id, reader);
     m_VectorDataList->SetNthElement(id, reader->GetOutput());
-
+    
     this->Modified();
+    return false;
     }
 }
 
@@ -147,17 +158,23 @@ InputVectorDataListParameter::SetNthFileName( const unsigned int id, const std::
 std::vector<std::string>
 InputVectorDataListParameter::GetFileNameList() const
 {
+  std::cout<<"InputVectorDataListParameter::GetFileNameList()"<<std::endl;
   if (m_ReaderList)
     {
+    std::cout<<"InputVectorDataListParameter::GetFileNameList().size() "<<m_ReaderList->Size()<<std::endl;
     std::vector<std::string> filenames;
     for(unsigned int i=0; i<m_ReaderList->Size(); i++)
-      {
-      filenames.push_back( m_ReaderList->GetNthElement(i)->GetFileName() );
+      {    
+      std::cout<<i<<std::endl;
+      
+      if( m_ReaderList->GetNthElement(i) )
+        filenames.push_back( m_ReaderList->GetNthElement(i)->GetFileName() );
       }
     
     return filenames;
     }
-  
+
+   std::cout<<"InputVectorDataListParameter::GetFileNameList() out"<<std::endl;
   itkExceptionMacro(<< "No filename value");
 }
 
@@ -174,7 +191,7 @@ InputVectorDataListParameter::GetNthFileName( unsigned int i ) const
     
     return m_ReaderList->GetNthElement(i)->GetFileName();
     }
-  
+ 
   itkExceptionMacro(<< "No filename value");
 }
 
diff --git a/Code/ApplicationEngine/otbWrapperInputVectorDataListParameter.h b/Code/ApplicationEngine/otbWrapperInputVectorDataListParameter.h
index e0c1d4c35c..d7185e70ca 100644
--- a/Code/ApplicationEngine/otbWrapperInputVectorDataListParameter.h
+++ b/Code/ApplicationEngine/otbWrapperInputVectorDataListParameter.h
@@ -50,16 +50,16 @@ public:
   itkTypeMacro(InputVectorDataListParameter, Parameter);
 
   /** Set image form a list of filename */
-  void SetListFromFileName(const std::vector<std::string> & filenames);
+  bool SetListFromFileName(const std::vector<std::string> & filenames);
 
   /** Add null element to lists. */
   void AddNullElement();
 
   /** Add an image from a filename */
-  void AddFromFileName(const std::string & filename);
+  bool AddFromFileName(const std::string & filename);
 
   /** Set one specific stored image filename. */
-  void SetNthFileName( const unsigned int id, const std::string & filename );
+  bool SetNthFileName( const unsigned int id, const std::string & filename );
 
 
   /** Get the stored image filename list */
diff --git a/Code/ApplicationEngine/otbWrapperInputVectorDataParameter.h b/Code/ApplicationEngine/otbWrapperInputVectorDataParameter.h
index 0c54917bf7..2db731ac6c 100644
--- a/Code/ApplicationEngine/otbWrapperInputVectorDataParameter.h
+++ b/Code/ApplicationEngine/otbWrapperInputVectorDataParameter.h
@@ -49,66 +49,30 @@ public:
   typedef otb::VectorData<CoordinatePrecisionType, 2, ValuePrecisionType>  VectorDataType;
 
   /** Set value from filename */
-  void SetFromFileName(const std::string& filename)
-  {
-    VectorDataFileReaderType::Pointer reader = VectorDataFileReaderType::New();
-    reader->SetFileName(filename);
-    reader->UpdateOutputInformation();
-
-    // everything went fine, store the object references
-    m_Reader = reader;
-    m_VectorData = reader->GetOutput();
-    SetActive(true);
-  }
-
-  std::string GetFileName() const
-  {
-    if (m_Reader)
-      {
-      return m_Reader->GetFileName();
-      }
-
-    itkExceptionMacro(<< "No value filename value");
-  }
-
-  VectorDataType* GetVectorData() const
-  {
-    return m_VectorData;
-  }
-
-  void SetVectorData(VectorDataType* vectorData)
-  {
-    m_VectorData = vectorData;
-    m_Reader = VectorDataFileReaderType::Pointer();
-  }
-
-  bool HasValue() const
-  {
-    return m_VectorData.IsNotNull();
-  }
-
-  void ClearValue()
-  {
-    m_VectorData = VectorDataType::Pointer();
-    m_Reader = VectorDataFileReaderType::Pointer();
-  }
+  bool SetFromFileName(const std::string& filename);
+  itkGetConstMacro(FileName, std::string);
+
+  VectorDataType* GetVectorData() const;
+
+  void SetVectorData(VectorDataType* vectorData);
+
+  bool HasValue() const;
+
+  void ClearValue();
 
 protected:
   /** Constructor */
-  InputVectorDataParameter()
-  {
-    this->SetName("Input Vector Data");
-    this->SetKey("ivd");
-  }
+  InputVectorDataParameter();
 
   /** Destructor */
-  virtual ~InputVectorDataParameter()
-  {}
+  virtual ~InputVectorDataParameter();
 
   typedef otb::VectorDataFileReader<VectorDataType> VectorDataFileReaderType;
   VectorDataType::Pointer m_VectorData;
   VectorDataFileReaderType::Pointer m_Reader;
 
+  std::string m_FileName;
+
 private:
   InputVectorDataParameter(const Parameter &); //purposely not implemented
   void operator =(const Parameter&); //purposely not implemented
diff --git a/Code/ApplicationEngine/otbWrapperParameter.h b/Code/ApplicationEngine/otbWrapperParameter.h
index 4dc55d1ac2..5fcfea4080 100644
--- a/Code/ApplicationEngine/otbWrapperParameter.h
+++ b/Code/ApplicationEngine/otbWrapperParameter.h
@@ -173,7 +173,7 @@ public:
   /** Is the paramter a root or a child of another param */
   virtual bool IsRoot()
   {
-    return  m_Root.IsNull();
+    return (this == m_Root);
   }
 
   /** Add a child of this parameter when the param is a Group or a
diff --git a/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.cxx b/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.cxx
index 28b91a4c2f..7bcb124ea2 100644
--- a/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.cxx
+++ b/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.cxx
@@ -334,23 +334,18 @@ CommandLineLauncher::LoadParameters()
       
     const bool paramExists( m_Parser->IsAttributExists( std::string("--").append(paramKey), m_Expression )  );
     const bool hasValue = m_Application->HasValue( paramKey );
+    
     // Check if mandatory parameter are present and have value
-    std::cout<<"=========== Key: "<<paramKey<<std::endl;
-    std::cout<<"manda: "<<param->GetMandatory()<<std::endl;
-    std::cout<<"actibe: "<<param->GetRoot()->GetActive()<<std::endl;
-    std::cout<<"!paramExists: "<<!paramExists<<std::endl;
-    std::cout<<"hasValue: "<<hasValue<<std::endl;
-    std::cout<<"root: "<<param->IsRoot()<<std::endl;
-
     // A param has to be set if it is mandatory and :
     // is root OR its parent is active
     // NB: a root parameter is not active
+    
     bool mustBeSet = false;
     if( param->GetMandatory() == true )
       if( param->GetRoot()->GetActive() || param->IsRoot() )
         mustBeSet = true;
 
-    if( mustBeSet )//param->GetMandatory() == true && param->GetRoot()->GetActive())
+    if( mustBeSet )
       {
       if( !paramExists )
         {
-- 
GitLab