private static string ReadItemIntoRow(FileExportSpecification spec, object item)
        {
            var columnDelimiter = spec.ColumnDelimiter;
            var sb = new StringBuilder();
            var allPropertyValues = spec.GetPropertiesForType(item.GetType()).Values.ToList();

            for (var i = 0; i < allPropertyValues.Count; i++)
            {
                var property = allPropertyValues[i];

                string formattedValue = property.GetFormattedValue(item);

                if (formattedValue.Contains(columnDelimiter))
                {
                    formattedValue = spec.OnDelimiterFoundInValue(property.PropertyName, columnDelimiter, formattedValue);
                }

                sb.Append(formattedValue);

                if (i < (allPropertyValues.Count - 1))
                {
                    sb.Append(columnDelimiter);
                }
            }

            return(sb.ToString());
        }
 public static void ExportToFile <T>(this FileExportSpecification spec, IEnumerable <T> items, string filePath)
 {
     using (TextWriter fileStream = new StreamWriter(filePath))
     {
         ExportToStream(spec, items, fileStream);
     }
 }
Ejemplo n.º 3
0
        private void ConfigureRestOfProperties(Type objectType, FileExportSpecification fileExportSpecification)
        {
            // fallback formatter for anything that doesn't fit int he custom, or "global default" formatters.
            var globalDefaultFormatter = new PropertyFormatter(context =>
            {
                if (context.ItemValue == null)
                {
                    return(string.Empty);
                }
                return(context.ItemValue.ToString());
            });
            var properties = objectType.GetProperties();

            for (int i = 0; i < properties.Length; i++)
            {
                var propertyInfo = properties[i];
                var propertyName = propertyInfo.Name;

                TypeConfiguration props = fileExportSpecification.GetTypeConfiguration(objectType);

                if (!props.IsPropertyExcluded(propertyName))
                {
                    if (props.IsPropertyDefined(propertyName))
                    {
                        props.Properties[propertyName].Order = i;
                        continue;
                    }

                    var propertyType = propertyInfo.PropertyType;

                    PropertyFormatter defaultPropertyFormatter = globalDefaultFormatter;

                    if (DefaultPropertyFormatters.ContainsKey(propertyType))
                    {
                        defaultPropertyFormatter = DefaultPropertyFormatters[propertyType];
                    }

                    if (fileExportSpecification.DefaultPropertyFormatters.ContainsKey(propertyType))
                    {
                        defaultPropertyFormatter = fileExportSpecification.DefaultPropertyFormatters[propertyType];
                    }

                    // If there's a default
                    if (props.DefaultTypeFormatters.ContainsKey(propertyType))
                    {
                        defaultPropertyFormatter = props.DefaultTypeFormatters[propertyType];
                    }

                    var property = new Property(objectType, propertyName, defaultPropertyFormatter, fileExportSpecification.DefaultHeaderFormatter, i);

                    props.AddProperty(property);
                }
            }
        }
        public static string ExportToString(this FileExportSpecification spec, IEnumerable items)
        {
            var sb = new StringBuilder();

            using (var sw = new StringWriter(sb))
            {
                ExportToStream(spec, items, sw);
            }

            return(sb.ToString());
        }
Ejemplo n.º 5
0
        public FileExportSpecification CreateSpec(IEnumerable <Type> supportedTypes, Action <FileExportSpecification> configuration)
        {
            var fileExportSpecification = new FileExportSpecification(supportedTypes, ",", Environment.NewLine);

            configuration(fileExportSpecification);

            foreach (var supportedType in supportedTypes)
            {
                if (!fileExportSpecification.SkipNonConfiguredProperties)
                {
                    ConfigureRestOfProperties(supportedType, fileExportSpecification);
                }
            }
            return(fileExportSpecification);
        }
        public static void ExportToStream(this FileExportSpecification spec, IEnumerable items, TextWriter writer)
        {
            bool isFirstRow = true;

            foreach (object item in items)
            {
                if (!isFirstRow)
                {
                    writer.Write(spec.RowDelimiter);
                }
                else
                {
                    //TODO: Not happy with the API for prepending and appending to a file

                    // Write any pre-header information
                    var prePendValue = spec.PrePendFileWithValue;
                    if (!string.IsNullOrEmpty(prePendValue))
                    {
                        writer.Write(prePendValue);
                        writer.Write(spec.RowDelimiter);
                    }

                    // Write the Header row
                    if (spec.IncludeHeader)
                    {
                        writer.Write(WriteTheHeader(item.GetType(), spec));
                        writer.Write(spec.RowDelimiter);
                    }
                }
                isFirstRow = false;

                string rowText = ReadItemIntoRow(spec, item);
                writer.Write(rowText);
            }

            writer.Write(spec.RowDelimiter);

            //TODO: Not happy with the API for prepending and appending to a file
            var apendValue = spec.AppendFileWithValue;

            if (!string.IsNullOrEmpty(apendValue))
            {
                writer.Write(apendValue);
            }
        }
        private static string WriteTheHeader(Type type, FileExportSpecification spec)
        {
            var sb = new StringBuilder();
            var allPropertyValues = spec.GetPropertiesForType(type).Values.ToList();

            for (var i = 0; i < allPropertyValues.Count; i++)
            {
                var property = allPropertyValues[i];

                string formattedValue = property.GetFormattedHeader();
                sb.Append(formattedValue);

                if (i < (allPropertyValues.Count - 1))
                {
                    sb.Append(spec.ColumnDelimiter);
                }
            }

            return(sb.ToString());
        }