GPPointFileInfo\InputFile.vb
Creating a non-trival custom GP tool - inputs, outputs, and responding to environment settings
GPPointFileInfo\InputFile.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.IO
Imports System.Security
Imports System.Text.RegularExpressions

Namespace GPPointFileInfo
  Public Class InputFile
        Implements GPPointFileInfo.IInputFile
    Private _index As Integer
    Private _name As String
    Private _path As String

    Public Sub New(ByVal inputFileArray As IInputFileArray, ByVal index As Integer)
      _index = index

      Dim fileInfo As FileInfo = GetFileInfo(inputFileArray, _index)

      Dim name As String = GetName(fileInfo)

      ValidateName(name)

      _name = name

      Dim path As String = GetPath(fileInfo)

      ValidatePath(path)

      _path = path
    End Sub

        Public ReadOnly Property Index() As Integer Implements IInputFile.Index
            Get
                Return _index
            End Get
        End Property

    Private Function GetFileInfo(ByVal inputFileArray As IInputFileArray, ByVal index As Integer) As FileInfo
      Return inputFileArray(index)
    End Function

        Public ReadOnly Property Name() As String Implements IInputFile.Name
            Get
                Return _name
            End Get
        End Property

    Private Function GetName(ByVal fileInfo As FileInfo) As String
      Return fileInfo.Name
    End Function

    Private Sub ValidateName(ByVal name As String)
      If String.IsNullOrEmpty(name) Then
        Throw New GPException(GPExceptionSeverity.Error, "Invalid Input File. Name Is Null Or Empty.")
      ElseIf IsBlank(name) Then
        Throw New GPException(GPExceptionSeverity.Error, "Invalid Input File. Name Is Blank." & ObjectToString.GetNewline() & "name: " & ObjectToString.GetToString(name))
      End If
    End Sub

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

            Dim blnIsBlank As Boolean

      Dim regex As Regex = New Regex(Pattern)

            blnIsBlank = regex.IsMatch(text)

            Return blnIsBlank
    End Function

        Public ReadOnly Property Path() As String Implements IInputFile.Path
            Get
                Return _path
            End Get
        End Property

    Private Function GetPath(ByVal fileInfo As FileInfo) As String
      Return fileInfo.FullName
    End Function

    Private Sub ValidatePath(ByVal path As String)
      Dim fileInfo As FileInfo

      Try
        fileInfo = New FileInfo(path)
      Catch ex As UnauthorizedAccessException
        Throw New GPException(GPExceptionSeverity.Error, "Invalid Input File. Unauthorized To Access Path." & ObjectToString.GetNewline() & "path: " & ObjectToString.GetToString(path), ex)
      Catch ex As SecurityException
        Throw New GPException(GPExceptionSeverity.Error, "Invalid Input File. Insufficient Security Privileges To Access Path." & ObjectToString.GetNewline() & "path: " & ObjectToString.GetToString(path), ex)
      Catch ex As ArgumentNullException
        Throw New GPException(GPExceptionSeverity.Error, "Invalid Input File. Path Is Null.", ex)
      Catch ex As PathTooLongException
        Throw New GPException(GPExceptionSeverity.Error, "Invalid Input File. Path Exceeds Maximum Allowable Length." & ObjectToString.GetNewline() & "path: " & ObjectToString.GetToString(path), ex)
      Catch ex As NotSupportedException
        Throw New GPException(GPExceptionSeverity.Error, "Invalid Input File. Path Not Supported." & ObjectToString.GetNewline() & "path: " & ObjectToString.GetToString(path), ex)
      Catch ex As ArgumentException
        Throw New GPException(GPExceptionSeverity.Error, "Invalid Input File. Invalid Path." & ObjectToString.GetNewline() & "path: " & ObjectToString.GetToString(path), ex)
      End Try

      If (Not fileInfo.Exists) Then
        Throw New GPException(GPExceptionSeverity.Error, "Invalid Input File. Path Does Not Exist." & ObjectToString.GetNewline() & "path: " & ObjectToString.GetToString(path))
      End If
    End Sub
  End Class
End Namespace