Exemple #1
0
        /// <summary>
        /// The method evaluates whether give name equals to the header name set
        /// in the descriptor.
        /// </summary>
        /// <remarks>
        /// This internal method detects its result by comparing the determined header
        /// name either by using lower and upper cases or by ignoring it.
        /// </remarks>
        /// <param name="header">
        /// The name to be compared.
        /// </param>
        /// <param name="exactly">
        /// If true, then an case-sensitive string comparison is performed.
        /// Otherwise as case-insensitive string comparison is performed.
        /// </param>
        /// <param name="descriptor">
        /// The column descriptor to get header information from.
        /// </param>
        /// <returns>
        /// True, if given name equals the header name within the descriptor,
        /// and false if not.
        /// </returns>
        private static Boolean IsHeaderName(String header, Boolean exactly, ItemDescriptor descriptor)
        {
            String other = CsvImporter <TInstance> .GetHeaderName(descriptor);

            StringComparison flags = exactly ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase;

            return(String.Compare(header, other, flags) == 0);
        }
Exemple #2
0
        /// <summary>
        /// This method tries to validate given headers by applying exact
        /// validation rules.
        /// </summary>
        /// <remarks>
        /// Exact validation rules means in detail that each string in given headers
        /// must exactly match its corresponding header name. Furthermore, exactly
        /// match means that each header name is compared by applying a case-sensitive
        /// name check. It also means that each header position must be the same
        /// position which is defined within given descriptors.
        /// </remarks>
        /// <param name="headers">
        /// The list of header names to be validated.
        /// </param>
        /// <param name="descriptors">
        /// The list of descriptors representing a single line.
        /// </param>
        /// <exception cref="FormatException">
        /// This exception is thrown as soon as one of the header validation rules
        /// has been violated.
        /// </exception>
        /// <exception cref="ArgumentOutOfRangeException">
        /// This exception might be thrown if accessing the lists fails.
        /// </exception>
        private static void ValidateHeader(List <String> headers, List <ItemDescriptor> descriptors)
        {
            // Keep in mind, the column validation has already checked the column count. And it is trusted
            // in that an Argument Out Of Range Exception is thrown if there was a made mistake beforehand.

            for (Int32 index = 0; index < headers.Count; index++)
            {
                String         header     = headers[index];
                ItemDescriptor descriptor = descriptors[index];

                if (!CsvImporter <TInstance> .IsHeaderName(header, true, descriptor))
                {
                    throw new FormatException(
                              $"Header validation mismatch. Header name \"{header}\" at column {index} does not fit " +
                              $"to the expected header name \"{CsvImporter<TInstance>.GetHeaderName(descriptor)}.\"");
                }
            }
        }