otbWrapperDocExampleStructure.h 5.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*=========================================================================

  Program:   ORFEO Toolbox
  Language:  C++
  Date:      $Date$
  Version:   $Revision$


  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
  See OTBCopyright.txt for details.


     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
#ifndef __otbWrapperDocExampleStructure_h
#define __otbWrapperDocExampleStructure_h

21 22 23
#include "itkObject.h"
#include "itkObjectFactory.h"

24 25 26 27 28
#include <string>
#include <iostream>
#include <vector>
#include "itkMacro.h"
#include "otbConfigure.h"
29 30
#include "itkFixedArray.h"

31 32 33 34 35 36 37

namespace otb
{
namespace Wrapper
{

/** \class DocExampleStructure
38 39 40 41 42
 *  \brief This class is a structure that gathered the necessary
 *  element to generate an example (for CommandLine, python, Java
 *  ...).
 * User has to set the application name, the binary path and a list of
 *  key/value couple.
43
 */
44 45
class DocExampleStructure :
  public itk::Object
46 47
{
public:
48 49 50 51 52 53 54 55 56 57 58
  /** Standard class typedefs */
  typedef DocExampleStructure           Self;
  typedef itk::Object                   Superclass;
  typedef itk::SmartPointer<Self>       Pointer;
  typedef itk::SmartPointer<const Self> ConstPointer;

  /** Standards macros */
  itkNewMacro(Self);

  /** Runtime information */
  itkTypeMacro(DocExampleStructure, itk::Object);
59

60 61
  typedef itk::FixedArray<std::string, 3> ThreeStringType;
  typedef std::vector<ThreeStringType> ParameterListType;
62

63 64 65
  /** Parameter list accessors. */
  /** Parameter list accessors : adding key and name */
  void AddParameter( const std::string key, const std::string name )
66
  {
67 68 69 70 71
    ThreeStringType mParam;
    mParam[0] = key;
    mParam[1] = name;
    mParam[2] = "";
    m_ParameterList.push_back( mParam );
72 73
  }
  
74
 void SetParameterValue( const std::string key, const std::string value )
75
  {
76 77 78 79 80 81 82 83 84 85 86
    bool found = false;
    unsigned int i=0;
    while ( i<m_ParameterList.size() && !found )
      {
      if( this->GetParameterKey(i) == key )
        {
        m_ParameterList[i][2] = value;
        found = true;
        }
      i++;
      }
87

88
    if( !found)
89
      itkGenericExceptionMacro( "No parameter with key \""<<key<<"\" found." );
90 91
  }

92
 /** Get the parameter list. */
93 94 95 96 97
  ParameterListType GetParameterList()
  {
    return m_ParameterList;
  }

98 99
  /** Get a specific parameter couple.*/
  ThreeStringType GetParameter( unsigned int i )
100
  {
101 102 103 104
    if( m_ParameterList.size() <= i )
      itkGenericExceptionMacro( "Index "<<i<<" out of range. (max index: "<<m_ParameterList.size()<<")." );

    return m_ParameterList[i];
105 106
  }

107 108
  /** Get a specific parameter couple key.*/
  std::string GetParameterKey( unsigned int i )
109
  {
110
    return this->GetParameter(i)[0];
111 112
  }

113 114
 /** Get a specific parameter couple key.*/
  std::string GetParameterName( unsigned int i )
115
  {
116
    return this->GetParameter(i)[1];
117 118
  }

119 120
  /** Get a specific parameter couple value as string.*/
  std::string GetParameterValue( unsigned int i )
121
  {
122
    return  this->GetParameter(i)[2];
123
  }
124
  /** Set Application name. */
125 126 127 128
  void SetApplicationName( const std::string name )
  {
    m_ApplicationName = name;
  }
129
  /** Get Application name. */
130 131 132 133 134
  std::string GetApplicationName()
  {
    return m_ApplicationName;
  }

135
  /** Generation of the documentation for CommandLine. */
136 137
  std::string GenerateCLExample()
  {
Cyrille Valladeau's avatar
Cyrille Valladeau committed
138
    if( m_ApplicationName.empty() || m_ParameterList.size() == 0 )
139
      {
140
      return "";
141 142 143
      }
    
    itk::OStringStream oss;
OTB Bot's avatar
STYLE  
OTB Bot committed
144
    oss << "otbcli_" << m_ApplicationName << " ";
145 146 147

    for (unsigned int i=0; i< m_ParameterList.size(); i++)
      {
148 149 150 151
      if( this->GetParameterValue(i) != "" )
        {
        oss<< "-" << this->GetParameterKey(i) << " " << this->GetParameterValue(i) <<" ";
        }
152 153 154 155 156 157 158 159 160 161 162 163
      }


    std::string res = oss.str();

    // Supress last added space
    res.erase( res.size()-1, 1);

    return res;
    
  }

164
  /** Generation of teh documentation for Qt. */
Cyrille Valladeau's avatar
Cyrille Valladeau committed
165
  std::string GenerateHtmlExample()
166
  {
Cyrille Valladeau's avatar
Cyrille Valladeau committed
167
    if( m_ApplicationName.empty() || m_ParameterList.size() == 0 )
168 169 170 171 172
      {
      return "";
      }

    itk::OStringStream oss;
Cyrille Valladeau's avatar
Cyrille Valladeau committed
173
    oss << "<ul>";
174 175 176 177
    for (unsigned int i=0; i< m_ParameterList.size(); i++)
      {
      if( this->GetParameterValue(i) != "" )
        {
Cyrille Valladeau's avatar
Cyrille Valladeau committed
178 179 180 181 182 183 184
        oss << "<li>";
        oss << "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">";
        oss << this->GetParameterName(i) << ": ";
        oss << this->GetParameterValue(i);
        
        oss << "</p>";
        oss << "</li>";
185 186
        }
      }
Cyrille Valladeau's avatar
Cyrille Valladeau committed
187
      oss << "</ul>";
188 189 190 191 192 193 194 195 196

    std::string res = oss.str();

    return res;
    
  }


protected:
197
  /** Constructor */
Cyrille Valladeau's avatar
Cyrille Valladeau committed
198
  DocExampleStructure(){}
199 200 201 202 203 204 205 206 207

  /** Destructor */
  virtual ~DocExampleStructure()
  {}

private:
  DocExampleStructure(const DocExampleStructure &); //purposely not implemented
  void operator =(const DocExampleStructure&); //purposely not implemented

Cyrille Valladeau's avatar
Cyrille Valladeau committed
208
  /** List of the application parameters. List of key/name/value couples. */
209
  ParameterListType m_ParameterList;
210
  /** List of the application parameter names. List of key/name couples. */
Cyrille Valladeau's avatar
Cyrille Valladeau committed
211
  ParameterListType m_ParameterNameList; // ???
212
  /** application name */
213 214 215 216 217 218 219 220 221
  std::string m_ApplicationName;


}; // End class Parameter

} // End namespace Wrapper
} // End namespace otb

#endif