Function Extension

omri ben zaken 0 Reputation points
2026-01-21T16:27:36.3966667+00:00

Hi Michel,

TryCreateForInput method assume that the gisInputFilePath is a file: a single file or an archive file.

I need to handle also case for folder with extension .gdb.

What is the best practice to do it ?

Thanks in advance,

    /// <summary>
    /// Attempts to create a converter for the specified GIS input file path with detailed diagnostic information.
    /// </summary>
    /// <param name="factory">The converter factory to use.</param>
    /// <param name="gisInputFilePath">The path to the GIS input file.</param>
    /// <param name="converter">When successful, contains the created converter; otherwise, <c>null</c>.</param>
    /// <param name="detectedSourceFormat">Contains diagnostic information about the detection source format process  (whether "GeoJson", "Shapefile", etc.).</param>
    /// <param name="detectReason">Contains diagnostic information about the detection process.</param>
    /// <returns><c>true</c> if converter creation succeeded; otherwise, <c>false</c>.</returns>
    /// <exception cref="ArgumentNullException">Thrown when <paramref name="factory"/> is null.</exception>
    /// <remarks>
    /// Detection priority:
    /// 1. Archives: Fast-path extension checks → KMZ detection → JSON voting → requirement matching
    /// 2. Single files: Explicit extension mapping → JSON format detection (with fallback) → generic extension mapping
    /// 
    /// All file reads are bounded and streaming. Archives are never extracted to disk.
    /// </remarks>
    public static bool TryCreateForInput(
        this IConverterFactory factory,
        string gisInputFilePath,
        out IConverter? converter,
        out string? detectedSourceFormat,
        out string? detectReason)
    {
        converter = null;
        detectReason = null;
        detectedSourceFormat = null;

        if (factory == null) throw new ArgumentNullException(nameof(factory));

        // Validate input path is not null or empty
        if (string.IsNullOrWhiteSpace(gisInputFilePath))
        {
            detectReason = "input path is null or whitespace";
            Log.Error($"TryCreateForInput: {detectReason}");
            return false;
        }

        // Validate file exists
        if (!File.Exists(gisInputFilePath))
        {
            detectReason = $"file does not exist: {gisInputFilePath}";
            Log.Error($"TryCreateForInput: {detectReason}");
            return false;
        }

        // Validate file is not empty
        FileInfo fileInfo;
        try
        {
            fileInfo = new FileInfo(gisInputFilePath);
        }
        catch (Exception ex)
        {
            detectReason = $"failed to get file info: {ex.Message}";
            Log.Error($"TryCreateForInput: {detectReason}", ex);
            return false;
        }

        if (fileInfo.Length == 0)
        {
            detectReason = "file is empty (0 bytes)";
            Log.Warn($"TryCreateForInput: {detectReason}");
            return false;
        }

        try
        {
            return ConverterUtils.IsArchiveFile(gisInputFilePath)
                ? TryDetectArchiveFormat(factory, gisInputFilePath, out converter, out detectedSourceFormat, out detectReason)
                : TryDetectSingleFileFormat(factory, gisInputFilePath, out converter, out detectedSourceFormat, out detectReason);
        }
        catch (Exception ex)
        {
            detectReason = $"unexpected error during format detection: {ex.GetType().Name} - {ex.Message}";
            Log.Error(detectReason, ex);
            return false;
        }
    }

Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Q&A Assist
    2026-01-21T16:27:50.6333333+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    To handle the case for a folder with the extension .gdb in your TryCreateForInput method, you can add a check to determine if the provided path is a directory and specifically if it has the .gdb extension. If it is a directory, you can then implement logic to handle the contents of the .gdb folder appropriately. Here’s a suggested approach:

    1. Check if the path is a directory: Use Directory.Exists to check if the path is a directory.
    2. Check for .gdb extension: Ensure that the directory name ends with .gdb.
    3. Handle the .gdb folder: Implement logic to process the contents of the .gdb folder, which may involve iterating through its files and determining their formats.

    Here's a modified version of your method that includes these checks:

    // Validate if the path is a directory and has a .gdb extension
    if (Directory.Exists(gisInputFilePath) && gisInputFilePath.EndsWith(".gdb", StringComparison.OrdinalIgnoreCase))
    {
        detectReason = "Input path is a .gdb folder";
        Log.Info(detectReason);
        // Implement logic to handle .gdb folder contents
        // For example, you might want to list files in the directory and process them
        var files = Directory.GetFiles(gisInputFilePath);
        foreach (var file in files)
        {
            // Process each file as needed
            // You could call TryDetectSingleFileFormat or similar methods here
        }
        return true; // or appropriate return value based on processing
    }
    

    This approach allows you to extend the functionality of your method to support .gdb folders while maintaining the existing logic for files and archives. Make sure to implement the specific logic for processing the contents of the .gdb folder based on your application's requirements.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.