private string NormalizeFieldValue(string fieldName, string fieldValue, int?size, bool truncate, bool?quoteField,
                                           ChoFieldValueJustification fieldValueJustification, char fillChar, bool isHeader = false)
        {
            string lFieldValue = fieldValue;
            bool   retValue    = false;

            if (retValue)
            {
                return(lFieldValue);
            }

            if (fieldValue.IsNull())
            {
                fieldValue = String.Empty;
            }

            if (quoteField == null || !quoteField.Value)
            {
                if (fieldValue.StartsWith("\"") && fieldValue.EndsWith("\""))
                {
                }
                else
                {
                    if (fieldValue.Contains(Configuration.Delimiter))
                    {
                        if (isHeader)
                        {
                            throw new ChoParserException("Field header '{0}' value contains delimiter character.".FormatString(fieldName));
                        }
                        else
                        {
                            throw new ChoParserException("Field '{0}' value contains delimiter character.".FormatString(fieldName));
                        }
                    }

                    if (fieldValue.Contains(Configuration.EOLDelimiter))
                    {
                        if (isHeader)
                        {
                            throw new ChoParserException("Field header '{0}' value contains EOL delimiter character.".FormatString(fieldName));
                        }
                        else
                        {
                            throw new ChoParserException("Field '{0}' value contains EOL delimiter character.".FormatString(fieldName));
                        }
                    }
                }
            }
            else
            {
                if (fieldValue.StartsWith("\"") && fieldValue.EndsWith("\""))
                {
                }
                else
                {
                    fieldValue = "\"{0}\"".FormatString(fieldValue);
                }
            }

            if (size != null)
            {
                if (fieldValue.Length < size.Value)
                {
                    if (fillChar != ChoCharEx.NUL)
                    {
                        if (fieldValueJustification == ChoFieldValueJustification.Right)
                        {
                            fieldValue = fieldValue.PadLeft(size.Value, fillChar);
                        }
                        else if (fieldValueJustification == ChoFieldValueJustification.Left)
                        {
                            fieldValue = fieldValue.PadRight(size.Value, fillChar);
                        }
                    }
                }
                else if (fieldValue.Length > size.Value)
                {
                    if (truncate)
                    {
                        fieldValue = fieldValue.Substring(0, size.Value);
                    }
                    else
                    {
                        if (isHeader)
                        {
                            throw new ApplicationException("Field header value length overflowed for '{0}' member [Expected: {1}, Actual: {2}].".FormatString(fieldName, size, fieldValue.Length));
                        }
                        else
                        {
                            throw new ApplicationException("Field value length overflowed for '{0}' member [Expected: {1}, Actual: {2}].".FormatString(fieldName, size, fieldValue.Length));
                        }
                    }
                }
            }

            return(fieldValue);
        }
Esempio n. 2
0
        private string NormalizeFieldValue(string fieldName, string fieldValue, int?size, bool truncate, bool?quoteField,
                                           ChoFieldValueJustification fieldValueJustification, char fillChar, bool isHeader = false, string nullValue = null,
                                           ChoFieldValueTrimOption?fieldValueTrimOption = null)
        {
            string lFieldValue = fieldValue;
            bool   retValue    = false;

            if (retValue)
            {
                return(lFieldValue);
            }

            if (fieldValue.IsNull())
            {
                fieldValue = String.Empty;
            }
            bool quoteValue = quoteField != null ? quoteField.Value : false;

            if (quoteField == null || !quoteField.Value)
            {
                if (fieldValue.StartsWith(Configuration.QuoteChar.ToString()) && fieldValue.EndsWith(Configuration.QuoteChar.ToString()))
                {
                }
                else
                {
                    if (fieldValue.Contains(Configuration.EOLDelimiter))
                    {
                        if (isHeader)
                        {
                            throw new ChoParserException("Field header '{0}' value contains EOL delimiter character.".FormatString(fieldName));
                        }
                        else
                        {
                            quoteValue = true;
                        }
                    }
                }
            }
            else
            {
                if (fieldValue.StartsWith(Configuration.QuoteChar.ToString()) && fieldValue.EndsWith(Configuration.QuoteChar.ToString()))
                {
                }
                else
                {
                    quoteValue = true;
                }
            }


            if (fieldValue.IsNullOrEmpty())
            {
                if (nullValue != null)
                {
                    fieldValue = nullValue;
                }
            }

            if (size != null)
            {
                if (quoteValue)
                {
                    size = size.Value - 2;
                }
                if (size <= 0)
                {
                    return(String.Empty);
                }

                if (fieldValue.Length < size.Value)
                {
                    if (fillChar != ChoCharEx.NUL)
                    {
                        if (fieldValueJustification == ChoFieldValueJustification.Right)
                        {
                            fieldValue = fieldValue.PadLeft(size.Value, fillChar);
                        }
                        else if (fieldValueJustification == ChoFieldValueJustification.Left)
                        {
                            fieldValue = fieldValue.PadRight(size.Value, fillChar);
                        }
                    }
                }
                else if (fieldValue.Length > size.Value)
                {
                    if (truncate)
                    {
                        if (fieldValueTrimOption != null)
                        {
                            if (fieldValueTrimOption == ChoFieldValueTrimOption.TrimStart)
                            {
                                fieldValue = fieldValue.Right(size.Value);
                            }
                            else
                            {
                                fieldValue = fieldValue.Substring(0, size.Value);
                            }
                        }
                        else
                        {
                            fieldValue = fieldValue.Substring(0, size.Value);
                        }
                    }
                    else
                    {
                        if (isHeader)
                        {
                            throw new ApplicationException("Field header value length overflowed for '{0}' member [Expected: {1}, Actual: {2}].".FormatString(fieldName, size, fieldValue.Length));
                        }
                        else
                        {
                            throw new ApplicationException("Field value length overflowed for '{0}' member [Expected: {1}, Actual: {2}].".FormatString(fieldName, size, fieldValue.Length));
                        }
                    }
                }
            }

            if (quoteValue)
            {
                fieldValue = "{1}{0}{1}".FormatString(fieldValue, Configuration.QuoteChar);
            }

            return(fieldValue);
        }
Esempio n. 3
0
        private string NormalizeFieldValue(string fieldName, string fieldValue, int?size, bool truncate, bool?quoteField,
                                           ChoFieldValueJustification fieldValueJustification, char fillChar, bool isHeader = false)
        {
            string lFieldValue = fieldValue;
            bool   retValue    = false;

            quoteValue = false;

            if (retValue)
            {
                return(lFieldValue);
            }

            if (fieldValue.IsNull())
            {
                fieldValue = String.Empty;
            }

            if (quoteField == null || !quoteField.Value)
            {
                if (fieldValue.StartsWith("\"") && fieldValue.EndsWith("\""))
                {
                }
                else
                {
                    if (searchStrings == null)
                    {
                        searchStrings = (@"""" + Configuration.Delimiter + Configuration.EOLDelimiter).ToArray();
                    }

                    if (fieldValue.IndexOfAny(searchStrings) >= 0)
                    {
                        //******** ORDER IMPORTANT *********

                        //Fields that contain double quote characters must be surounded by double-quotes, and the embedded double-quotes must each be represented by a pair of consecutive double quotes.
                        if (fieldValue.IndexOf('"') >= 0)
                        {
                            fieldValue = fieldValue.Replace(@"""", @"""""");
                            quoteValue = true;
                        }

                        if (fieldValue.IndexOf(Configuration.Delimiter) >= 0)
                        {
                            if (isHeader)
                            {
                                throw new ChoParserException("Field header '{0}' value contains delimiter character.".FormatString(fieldName));
                            }
                            else
                            {
                                //Fields with embedded commas must be delimited with double-quote characters.
                                quoteValue = true;
                                //throw new ChoParserException("Field '{0}' value contains delimiter character.".FormatString(fieldName));
                            }
                        }

                        if (fieldValue.IndexOf(Configuration.EOLDelimiter) >= 0)
                        {
                            if (isHeader)
                            {
                                throw new ChoParserException("Field header '{0}' value contains EOL delimiter character.".FormatString(fieldName));
                            }
                            else
                            {
                                //A field that contains embedded line-breaks must be surounded by double-quotes
                                quoteValue = true;
                                //throw new ChoParserException("Field '{0}' value contains EOL delimiter character.".FormatString(fieldName));
                            }
                        }
                    }

                    //Fields with leading or trailing spaces must be delimited with double-quote characters.
                    if (!fieldValue.IsNullOrWhiteSpace() && (char.IsWhiteSpace(fieldValue[0]) || char.IsWhiteSpace(fieldValue[fieldValue.Length - 1])))
                    {
                        quoteValue = true;
                    }

                    if (quoteValue)
                    {
                        fieldValue = "\"{0}\"".FormatString(fieldValue);
                    }
                }
            }
            else
            {
                if (fieldValue.StartsWith("\"") && fieldValue.EndsWith("\""))
                {
                }
                else
                {
                    //Fields that contain double quote characters must be surrounded by double-quotes, and the embedded double-quotes must each be represented by a pair of consecutive double quotes.
                    if (fieldValue.IndexOf('"') >= 0)
                    {
                        fieldValue = "\"{0}\"".FormatString(fieldValue.Replace(@"""", @""""""));
                    }
                    else
                    {
                        fieldValue = "\"{0}\"".FormatString(fieldValue);
                    }
                }
            }

            if (size != null)
            {
                if (fieldValue.Length < size.Value)
                {
                    if (fillChar != ChoCharEx.NUL)
                    {
                        if (fieldValueJustification == ChoFieldValueJustification.Right)
                        {
                            fieldValue = fieldValue.PadLeft(size.Value, fillChar);
                        }
                        else if (fieldValueJustification == ChoFieldValueJustification.Left)
                        {
                            fieldValue = fieldValue.PadRight(size.Value, fillChar);
                        }
                    }
                }
                else if (fieldValue.Length > size.Value)
                {
                    if (truncate)
                    {
                        fieldValue = fieldValue.Substring(0, size.Value);
                    }
                    else
                    {
                        if (isHeader)
                        {
                            throw new ApplicationException("Field header value length overflowed for '{0}' member [Expected: {1}, Actual: {2}].".FormatString(fieldName, size, fieldValue.Length));
                        }
                        else
                        {
                            throw new ApplicationException("Field value length overflowed for '{0}' member [Expected: {1}, Actual: {2}].".FormatString(fieldName, size, fieldValue.Length));
                        }
                    }
                }
            }

            return(fieldValue);
        }
Esempio n. 4
0
        private string NormalizeFieldValue(string fieldName, string fieldValue, int?size, bool truncate, bool?quoteField,
                                           ChoFieldValueJustification fieldValueJustification, char fillChar, bool isHeader = false, ChoFieldValueTrimOption?fieldValueTrimOption = null)
        {
            string lFieldValue = fieldValue;
            bool   retValue    = false;

            if (retValue)
            {
                return(lFieldValue);
            }

            if (fieldValue.IsNull())
            {
                fieldValue = String.Empty;
            }

            if (quoteField == null || !quoteField.Value)
            {
                if (fieldValue.StartsWith("\"") && fieldValue.EndsWith("\""))
                {
                }
                else
                {
                    if (!EOLDelimiter.IsNullOrEmpty() && fieldValue.Contains(EOLDelimiter))
                    {
                        if (isHeader)
                        {
                            throw new ChoParserException("Field header '{0}' value contains EOL delimiter character.".FormatString(fieldName));
                        }
                        else
                        {
                            fieldValue = "\"{0}\"".FormatString(fieldValue);
                        }
                    }
                }
            }
            else
            {
                if (fieldValue.StartsWith("\"") && fieldValue.EndsWith("\""))
                {
                }
                else
                {
                    fieldValue = "\"{0}\"".FormatString(fieldValue);
                }
            }

            if (size != null)
            {
                if (fieldValue.Length < size.Value)
                {
                    if (fillChar != ChoCharEx.NUL)
                    {
                        if (fieldValueJustification == ChoFieldValueJustification.Right)
                        {
                            fieldValue = fieldValue.PadLeft(size.Value, fillChar);
                        }
                        else if (fieldValueJustification == ChoFieldValueJustification.Left)
                        {
                            fieldValue = fieldValue.PadRight(size.Value, fillChar);
                        }
                    }
                }
                else if (fieldValue.Length > size.Value)
                {
                    if (truncate)
                    {
                        if (fieldValueTrimOption != null)
                        {
                            if (fieldValueTrimOption == ChoFieldValueTrimOption.TrimStart)
                            {
                                fieldValue = fieldValue.Right(size.Value);
                            }
                            else
                            {
                                fieldValue = fieldValue.Substring(0, size.Value);
                            }
                        }
                        else
                        {
                            fieldValue = fieldValue.Substring(0, size.Value);
                        }
                    }
                    else
                    {
                        if (isHeader)
                        {
                            throw new ApplicationException("Field header value length overflowed for '{0}' member [Expected: {1}, Actual: {2}].".FormatString(fieldName, size, fieldValue.Length));
                        }
                        else
                        {
                            throw new ApplicationException("Field value length overflowed for '{0}' member [Expected: {1}, Actual: {2}].".FormatString(fieldName, size, fieldValue.Length));
                        }
                    }
                }
            }

            //return fieldValue.StartsWith("<![CDATA[") ? fieldValue : System.Net.WebUtility.HtmlEncode(fieldValue);

            //escape quotes
            if (fieldValue.Contains('"'))
            {
                fieldValue = fieldValue.Replace(@"""", @"\""");
            }

            return(fieldValue);
        }
Esempio n. 5
0
        private string NormalizeFieldValue(string fieldName, string fieldValue, int?size, bool truncate, bool?quoteField,
                                           ChoFieldValueJustification fieldValueJustification, char fillChar, bool isHeader = false, string nullValue = null,
                                           ChoFieldValueTrimOption?fieldValueTrimOption = null, ChoCSVRecordFieldConfiguration fieldConfig            = null)
        {
            string lFieldValue = fieldValue;
            bool   retValue    = false;

            quoteValue = quoteField != null ? quoteField.Value : false;

            if (retValue)
            {
                return(lFieldValue);
            }

            if (fieldValue.IsNull())
            {
                fieldValue = String.Empty;
            }

            if (fieldValue.StartsWith(Configuration.QuoteChar.ToString()) && fieldValue.EndsWith(Configuration.QuoteChar.ToString()))
            {
            }
            else
            {
                if (quoteField == null)
                {
                    if (searchStrings == null)
                    {
                        searchStrings = (Configuration.QuoteChar.ToString() + Configuration.Delimiter + Configuration.EOLDelimiter).ToArray();
                    }

                    if (fieldValue.IndexOfAny(searchStrings) >= 0)
                    {
                        //******** ORDER IMPORTANT *********

                        //Fields that contain double quote characters must be surounded by double-quotes, and the embedded double-quotes must each be represented by a pair of consecutive double quotes.
                        if (fieldValue.IndexOf(Configuration.QuoteChar) >= 0)
                        {
                            if (!Configuration.EscapeQuoteAndDelimiter)
                            {
                                fieldValue = fieldValue.Replace(Configuration.QuoteChar.ToString(), Configuration.DoubleQuoteChar);
                            }
                            else
                            {
                                fieldValue = fieldValue.Replace(Configuration.QuoteChar.ToString(), "\\{0}".FormatString(Configuration.QuoteChar));
                            }

                            quoteValue = true;
                        }

                        if (fieldValue.IndexOf(Configuration.Delimiter) >= 0)
                        {
                            if (isHeader)
                            {
                                if (fieldConfig == null || fieldConfig.ValueSelector == null)
                                {
                                    throw new ChoParserException("Field header '{0}' value contains delimiter character.".FormatString(fieldName));
                                }
                            }
                            else
                            {
                                //Fields with embedded commas must be delimited with double-quote characters.
                                if (Configuration.EscapeQuoteAndDelimiter)
                                {
                                    fieldValue = fieldValue.Replace(Configuration.Delimiter, "\\{0}".FormatString(Configuration.Delimiter));
                                }

                                quoteValue = true;
                                //throw new ChoParserException("Field '{0}' value contains delimiter character.".FormatString(fieldName));
                            }
                        }

                        if (fieldValue.IndexOf(Configuration.EOLDelimiter) >= 0)
                        {
                            if (isHeader)
                            {
                                throw new ChoParserException("Field header '{0}' value contains EOL delimiter character.".FormatString(fieldName));
                            }
                            else
                            {
                                //A field that contains embedded line-breaks must be surounded by double-quotes
                                //if (Configuration.EscapeQuoteAndDelimiters)
                                //    fieldValue = fieldValue.Replace(Configuration.EOLDelimiter, "\\{0}".FormatString(Configuration.EOLDelimiter));

                                quoteValue = true;
                                //throw new ChoParserException("Field '{0}' value contains EOL delimiter character.".FormatString(fieldName));
                            }
                        }
                    }

                    if (!isHeader)
                    {
                        //Fields with leading or trailing spaces must be delimited with double-quote characters.
                        if (!fieldValue.IsNullOrWhiteSpace() && (char.IsWhiteSpace(fieldValue[0]) || char.IsWhiteSpace(fieldValue[fieldValue.Length - 1])))
                        {
                            quoteValue = true;
                        }
                    }
                }
                else
                {
                    quoteValue = quoteField.Value;
                }
            }
            //}
            //else
            //{
            //    if (fieldValue.StartsWith(Configuration.QuoteChar.ToString()) && fieldValue.EndsWith(Configuration.QuoteChar.ToString()))
            //    {

            //    }
            //    else
            //    {
            //        //Fields that contain double quote characters must be surrounded by double-quotes, and the embedded double-quotes must each be represented by a pair of consecutive double quotes.
            //        if (fieldValue.IndexOf(Configuration.QuoteChar) >= 0)
            //        {
            //            fieldValue = "{1}{0}{1}".FormatString(fieldValue.Replace(Configuration.QuoteChar.ToString(), Configuration.DoubleQuoteChar), Configuration.QuoteChar);
            //        }
            //        else
            //            fieldValue = "{1}{0}{1}".FormatString(fieldValue, Configuration.QuoteChar);
            //    }
            //}

            //if (quoteValue)
            //	size = size - 2;

            if (fieldValue.IsNullOrEmpty())
            {
                if (nullValue != null)
                {
                    fieldValue = nullValue;
                }
            }

            if (size != null)
            {
                if (quoteValue)
                {
                    size = size.Value - 2;
                }
                if (size <= 0)
                {
                    return(String.Empty);
                }

                if (fieldValue.Length < size.Value)
                {
                    if (fillChar != ChoCharEx.NUL)
                    {
                        if (fieldValueJustification == ChoFieldValueJustification.Right)
                        {
                            fieldValue = fieldValue.PadLeft(size.Value, fillChar);
                        }
                        else if (fieldValueJustification == ChoFieldValueJustification.Left)
                        {
                            fieldValue = fieldValue.PadRight(size.Value, fillChar);
                        }
                    }
                }
                else if (fieldValue.Length > size.Value)
                {
                    if (truncate)
                    {
                        if (fieldValueTrimOption != null)
                        {
                            if (fieldValueTrimOption == ChoFieldValueTrimOption.TrimStart)
                            {
                                fieldValue = fieldValue.Right(size.Value);
                            }
                            else
                            {
                                fieldValue = fieldValue.Substring(0, size.Value);
                            }
                        }
                        else
                        {
                            fieldValue = fieldValue.Substring(0, size.Value);
                        }
                    }
                    else
                    {
                        if (isHeader)
                        {
                            throw new ApplicationException("Field header value length overflowed for '{0}' member [Expected: {1}, Actual: {2}].".FormatString(fieldName, size, fieldValue.Length));
                        }
                        else
                        {
                            throw new ApplicationException("Field value length overflowed for '{0}' member [Expected: {1}, Actual: {2}].".FormatString(fieldName, size, fieldValue.Length));
                        }
                    }
                }
            }

            if (fieldConfig != null && fieldConfig.ValueSelector != null)
            {
                quoteValue = false;
            }

            if (quoteValue)
            {
                fieldValue = "{1}{0}{1}".FormatString(fieldValue, Configuration.QuoteChar);
            }

            return(fieldValue);
        }
 public ChoFixedLengthWriter <T> WithField(string name, int startIndex, int size, bool?quoteField = null, char fillChar = ' ', ChoFieldValueJustification fieldValueJustification = ChoFieldValueJustification.Left,
                                           bool truncate = true, string fieldName = null)
 {
     return(WithField(name, startIndex, size, typeof(string), quoteField, fillChar, fieldValueJustification, truncate, fieldName));
 }
        public ChoFixedLengthWriter <T> WithField(string name, int startIndex, int size, Type fieldType, bool?quoteField = null, char fillChar = ' ', ChoFieldValueJustification fieldValueJustification = ChoFieldValueJustification.Left,
                                                  bool truncate = true, string fieldName = null)
        {
            if (!name.IsNullOrEmpty())
            {
                if (!_clearFields)
                {
                    Configuration.FixedLengthRecordFieldConfigurations.Clear();
                    _clearFields = true;
                }
                if (fieldType == null)
                {
                    fieldType = typeof(string);
                }

                Configuration.FixedLengthRecordFieldConfigurations.Add(new ChoFixedLengthRecordFieldConfiguration(name.Trim(), startIndex, size)
                {
                    FieldType = fieldType, QuoteField = quoteField,
                    FillChar  = fillChar,
                    FieldValueJustification = fieldValueJustification,
                    Truncate  = truncate,
                    FieldName = fieldName.IsNullOrWhiteSpace() ? name : fieldName
                });
            }

            return(this);
        }
Esempio n. 8
0
 public ChoCSVWriter <T> WithField(string fieldName, bool?quoteField = null, char fillChar = ' ', ChoFieldValueJustification fieldValueJustification = ChoFieldValueJustification.Left,
                                   bool truncate = true)
 {
     return(WithField(fieldName, typeof(string), quoteField, fillChar, fieldValueJustification, truncate));
 }
Esempio n. 9
0
        public ChoCSVWriter <T> WithField(string fieldName, Type fieldType, bool?quoteField = null, char fillChar = ' ', ChoFieldValueJustification fieldValueJustification = ChoFieldValueJustification.Left,
                                          bool truncate = true)
        {
            if (!fieldName.IsNullOrEmpty())
            {
                if (fieldType == null)
                {
                    fieldType = typeof(string);
                }

                if (!_clearFields)
                {
                    Configuration.CSVRecordFieldConfigurations.Clear();
                    _clearFields = true;
                }

                int maxFieldPos = Configuration.CSVRecordFieldConfigurations.Count > 0 ? Configuration.CSVRecordFieldConfigurations.Max(f => f.FieldPosition) : 0;
                Configuration.CSVRecordFieldConfigurations.Add(new ChoCSVRecordFieldConfiguration(fieldName.Trim(), ++maxFieldPos)
                {
                    FieldType = fieldType, QuoteField = quoteField,
                    FillChar  = fillChar,
                    FieldValueJustification = fieldValueJustification,
                    Truncate = truncate
                });
            }

            return(this);
        }