/// <summary> /// This method tries to determine whether given list of cells contain /// all and only header names. /// </summary> /// <remarks> /// The order of headers does not matter in this validation because of /// it only checks whether all the header names are included. Further, /// each name comparison is done by ignoring upper and lower cases. /// </remarks> /// <param name="headers"> /// The list of cells to be verified, which should contain header names. /// </param> /// <param name="descriptors"> /// The list of descriptors representing a single line. /// </param> /// <returns> /// True is returned when all header names have occurred at least once, /// no matter at which position a header name has occurred. Otherwise, /// false is returned. /// </returns> private static Boolean IsHeaderLine(List <String> headers, List <ItemDescriptor> descriptors) { Int32 verified = 0; for (Int32 index = 0; index < headers.Count; index++) { foreach (ItemDescriptor descriptor in descriptors) { if (CsvImporter <TInstance> .IsHeaderName(headers[index], false, descriptor)) { verified++; break; } } } return(verified == headers.Count); }
/// <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)}.\""); } } }