/// <summary>
 /// Qualifies
 /// </summary>
 /// <param name="cell"></param>
 /// <returns></returns>
 public string TextQualify(string cell)
 {
     cell = string.Concat(
         TextQualifier,
         cell.Replace(TextQualifier.ToString(), string.Concat(TextQualifier, TextQualifier)),
         TextQualifier
         );
     return(cell);
 }
Beispiel #2
0
        /// <summary>
        /// Writes the indexed <paramref name="column"/> from the <paramref name="record"/> to the line data <see cref="StringBuilder"/>.
        /// </summary>
        /// <param name="fcr">The corresponding <see cref="FileColumnReflector"/> configuration.</param>
        /// <param name="record">The related <see cref="FileRecord"/>.</param>
        /// <param name="column">The column index.</param>
        /// <param name="sb">The line data <see cref="StringBuilder"/>.</param>
        /// <returns><c>true</c> indicates that the column write was successful; otherwise, <c>false</c>.</returns>
        protected override bool WriteColumnToLineData(FileColumnReflector fcr, FileRecord record, int column, StringBuilder sb)
        {
            // Delimit each column.
            if (column > 0)
            {
                sb.Append(Delimiter);
            }

            // Get the string value and correct the width if needed.
            var str = column > record.Columns.Length ? null : record.Columns[column];

            // Override if it is the hierarchy column.
            if (HierarchyColumnIndex.HasValue && HierarchyColumnIndex.Value == column)
            {
                str = record.RecordIdentifier;
            }

            // Where null this means there is nothing specific to write.
            if (string.IsNullOrEmpty(str))
            {
                return(true);
            }

            // Validate/correct the string value to ensure column width conformance.
            if (!fcr.StringWidthCorrector(record, ref str))
            {
                return(false);
            }

            // Check if the column content contains the delimiter and handle accordingly.
            var qualify = fcr.PropertyTypeCode == TypeCode.String && TextQualifier != NoCharacter && !TextQualifierOnlyWithDelimiterOnWrite;

            if (str.IndexOf(Delimiter) >= 0)
            {
                if (TextQualifier == NoCharacter)
                {
                    record.Messages.Add(MessageType.Error, "Text delimiter character found inside column text; no text qualifier has been specified and would result in errant record.");
                    return(false);
                }
                else
                {
                    qualify = true;
                }
            }

            // Double qualify a qualifier inside of the text.
            if (TextQualifier != NoCharacter)
            {
                if (!qualify && str.IndexOf(TextQualifier) >= 0)
                {
                    qualify = true;
                }

                if (qualify)
                {
                    str = str.Replace(TextQualifier.ToString(), new string(TextQualifier, 2));
                }
            }

            if (qualify)
            {
                sb.Append(TextQualifier);
            }

            sb.Append(str);

            if (qualify)
            {
                sb.Append(TextQualifier);
            }

            return(true);
        }