GPPointFileInfo\GPInputFileSuffix.vb
Creating a non-trival custom GP tool - inputs, outputs, and responding to environment settings
GPPointFileInfo\GPInputFileSuffix.vb
' Copyright 2008 ESRI
' 
' All rights reserved under the copyright laws of the United States
' and applicable international laws, treaties, and conventions.
' 
' You may freely redistribute and use this sample code, with or
' without modification, provided you include the original copyright
' notice and use restrictions.
' 
' See use restrictions at <your ArcGIS install location>/developerkit/userestrictions.txt.
' 

Imports Microsoft.VisualBasic
Imports System
Imports System.Text.RegularExpressions
Imports ESRI.ArcGIS.Geoprocessing
Imports ESRI.ArcGIS.Geodatabase

Namespace GPPointFileInfo
  Public Class GPInputFileSuffix
        Implements GPPointFileInfo.IGPInputFileSuffix
    Private Shared _parameterIndex As Integer

    Private _fileSuffix As String

    Public Sub New(ByVal parameterValueArray As IParameterValueArray)
      Dim gpValue As IGPValue = parameterValueArray.GetGPValue(_parameterIndex)

      Dim fileSuffix As String = GetFileSuffix(gpValue)

      ValidateFileSuffix(fileSuffix)

      _fileSuffix = fileSuffix
    End Sub

    Public Shared Function GetGPParameterEdit() As IGPParameterEdit
      Dim gpParameterEdit As IGPParameterEdit = New GPParameterClass()

      Dim gpDataType As IGPDataType = New GPStringTypeClass()

      Dim gpValue As IGPValue = New GPStringClass()

      gpParameterEdit.Name = "in_input_file_suffix"
      gpParameterEdit.DisplayName = "Input File Suffix"
      gpParameterEdit.DataType = gpDataType
      gpParameterEdit.Value = gpValue
      gpParameterEdit.Direction = esriGPParameterDirection.esriGPParameterDirectionInput
      gpParameterEdit.ParameterType = esriGPParameterType.esriGPParameterTypeOptional

      Return gpParameterEdit
    End Function

    Public Shared Sub SetGPParameterIndex(ByVal index As Integer)
      _parameterIndex = index
    End Sub

        Public ReadOnly Property FileSuffix() As String Implements IGPInputFileSuffix.FileSuffix
            Get
                Return _fileSuffix
            End Get
        End Property

    Private Function GetFileSuffix(ByVal gpValue As IGPValue) As String
      Dim fileSuffix As String

      Dim gpString As IGPString = TryCast(gpValue, IGPString)

      fileSuffix = gpString.Value

      If String.IsNullOrEmpty(fileSuffix) OrElse IsBlank(fileSuffix) Then
        fileSuffix = "*"
      Else
        If fileSuffix.StartsWith(".") Then
          fileSuffix = fileSuffix.Remove(0, 1)
        End If
      End If

      Return fileSuffix
    End Function

    Private Function IsBlank(ByVal text As String) As Boolean
      Const Pattern As String = "^\s*$"

'INSTANT VB NOTE: The local variable isBlank was renamed since Visual Basic will not allow local variables with the same name as their method:
      Dim isBlank_Renamed As Boolean

      Dim regex As Regex = New Regex(Pattern)

      isBlank_Renamed = regex.IsMatch(text)

      Return isBlank_Renamed
    End Function

    Private Sub ValidateFileSuffix(ByVal fileSuffix As String)
      Dim invalidFileSuffixCharacters As Char() = GetInvalidFileSuffixCharacters()

      Dim invalidFileSuffixCharacterIndex As Integer = fileSuffix.IndexOfAny(invalidFileSuffixCharacters)

      If invalidFileSuffixCharacterIndex >= 0 Then
        Dim invalidFileSuffixCharacter As Char = fileSuffix.Chars(invalidFileSuffixCharacterIndex)

        Throw New GPException(GPExceptionSeverity.Error, "Invalid Input File Suffix. Input File Suffix Contains One Or More Invalid File Suffix Characters." & ObjectToString.GetNewline() & "fileSuffix: " & ObjectToString.GetToString(fileSuffix) & "invalidFileSuffixCharacter: " & ObjectToString.GetToString(invalidFileSuffixCharacter))
      End If
    End Sub

    Private Function GetInvalidFileSuffixCharacters() As Char()
      Const Pattern As String = "[*?]"

      Dim invalidFileSuffixCharacters As Char()

      Dim regex As Regex = New Regex(Pattern)

      Dim invalidFileNameString As String = New String(System.IO.Path.GetInvalidFileNameChars())

      invalidFileNameString = regex.Replace(invalidFileNameString, "")

      invalidFileSuffixCharacters = invalidFileNameString.ToCharArray()

      Return invalidFileSuffixCharacters
    End Function
  End Class
End Namespace