~ubuntu-branches/ubuntu/trusty/smuxi/trusty-proposed

« back to all changes in this revision

Viewing changes to lib/ServiceStack.Text/src/ServiceStack.Text/CsvWriter.cs

  • Committer: Package Import Robot
  • Author(s): Mirco Bauer
  • Date: 2013-05-25 22:11:31 UTC
  • mfrom: (1.2.12)
  • Revision ID: package-import@ubuntu.com-20130525221131-nd2mc0kzubuwyx20
Tags: 0.8.11-1
* [22d13d5] Imported Upstream version 0.8.11
* [6d2b95a] Refreshed patches
* [89eb66e] Added ServiceStack libraries to smuxi-engine package
* [848ab10] Enable Campfire engine
* [c6dbdc7] Always build db4o for predictable build result
* [13ec489] Exclude OS X specific libraries from dh_clideps

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
using System;
 
2
using System.Collections.Generic;
 
3
using System.IO;
 
4
using ServiceStack.Text.Common;
 
5
using ServiceStack.Text.Reflection;
 
6
 
 
7
namespace ServiceStack.Text
 
8
{
 
9
        internal class CsvWriter<T>
 
10
        {
 
11
                public const char DelimiterChar = ',';
 
12
 
 
13
                public static List<string> Headers { get; set; }
 
14
 
 
15
                internal static List<Func<T, object>> PropertyGetters;
 
16
 
 
17
                private static readonly WriteObjectDelegate OptimizedWriter;
 
18
 
 
19
                static CsvWriter()
 
20
                {
 
21
                        if (typeof(T) == typeof(string))
 
22
                        {
 
23
                                OptimizedWriter = (w, o) => WriteRow(w, (IEnumerable<string>)o);
 
24
                                return;
 
25
                        }
 
26
 
 
27
                        Reset();
 
28
                }
 
29
 
 
30
                internal static void Reset()
 
31
                {
 
32
                        Headers = new List<string>();
 
33
 
 
34
                        PropertyGetters = new List<Func<T, object>>();
 
35
                        foreach (var propertyInfo in TypeConfig<T>.Properties)
 
36
                        {
 
37
                                if (!propertyInfo.CanRead || propertyInfo.GetGetMethod() == null) continue;
 
38
                                if (!TypeSerializer.CanCreateFromString(propertyInfo.PropertyType)) continue;
 
39
 
 
40
                                PropertyGetters.Add(propertyInfo.GetValueGetter<T>());
 
41
                                Headers.Add(propertyInfo.Name);
 
42
                        }
 
43
                }
 
44
 
 
45
                internal static void ConfigureCustomHeaders(Dictionary<string, string> customHeadersMap)
 
46
                {
 
47
                        Reset();
 
48
 
 
49
                        for (var i = Headers.Count - 1; i >= 0; i--)
 
50
                        {
 
51
                                var oldHeader = Headers[i];
 
52
                                string newHeaderValue;
 
53
                                if (!customHeadersMap.TryGetValue(oldHeader, out newHeaderValue))
 
54
                                {
 
55
                                        Headers.RemoveAt(i);
 
56
                                        PropertyGetters.RemoveAt(i);
 
57
                                }
 
58
                                else
 
59
                                {
 
60
                                        Headers[i] = newHeaderValue.EncodeJsv();
 
61
                                }
 
62
                        }
 
63
                }
 
64
 
 
65
                private static List<string> GetSingleRow(IEnumerable<T> records, Type recordType)
 
66
                {
 
67
                        var row = new List<string>();
 
68
                        foreach (var value in records)
 
69
                        {
 
70
                                var strValue = recordType == typeof(string)
 
71
                                   ? value as string
 
72
                                   : TypeSerializer.SerializeToString(value);
 
73
 
 
74
                                row.Add(strValue);
 
75
                        }
 
76
                        return row;
 
77
                }
 
78
 
 
79
                public static List<List<string>> GetRows(IEnumerable<T> records)
 
80
                {
 
81
                        var rows = new List<List<string>>();
 
82
 
 
83
                        if (records == null) return rows;
 
84
 
 
85
                        if (typeof(T).IsValueType || typeof(T) == typeof(string))
 
86
                        {
 
87
                                rows.Add(GetSingleRow(records, typeof(T)));
 
88
                                return rows;
 
89
                        }
 
90
 
 
91
                        foreach (var record in records)
 
92
                        {
 
93
                                var row = new List<string>();
 
94
                                foreach (var propertyGetter in PropertyGetters)
 
95
                                {
 
96
                                        var value = propertyGetter(record) ?? "";
 
97
 
 
98
                                        var strValue = value.GetType() == typeof(string)
 
99
                                                ? (string)value
 
100
                                                : TypeSerializer.SerializeToString(value);
 
101
 
 
102
                                        row.Add(strValue);
 
103
                                }
 
104
                                rows.Add(row);
 
105
                        }
 
106
 
 
107
                        return rows;
 
108
                }
 
109
 
 
110
                public static void WriteObject(TextWriter writer, object records)
 
111
                {
 
112
                        Write(writer, (IEnumerable<T>)records);
 
113
                }
 
114
 
 
115
                public static void WriteObjectRow(TextWriter writer, object record)
 
116
                {
 
117
                        WriteRow(writer, (T)record);
 
118
                }
 
119
 
 
120
                public static void Write(TextWriter writer, IEnumerable<T> records)
 
121
                {
 
122
                        if (records == null) return; //AOT
 
123
 
 
124
                        if (OptimizedWriter != null)
 
125
                        {
 
126
                                OptimizedWriter(writer, records);
 
127
                                return;
 
128
                        }
 
129
 
 
130
                        if (!CsvConfig<T>.OmitHeaders && Headers.Count > 0)
 
131
                        {
 
132
                                var ranOnce = false;
 
133
                                foreach (var header in Headers)
 
134
                                {
 
135
                                        JsWriter.WriteItemSeperatorIfRanOnce(writer, ref ranOnce);
 
136
 
 
137
                                        writer.Write(header);
 
138
                                }
 
139
                                writer.WriteLine();
 
140
                        }
 
141
 
 
142
                        if (records == null) return;
 
143
 
 
144
                        if (typeof(T).IsValueType || typeof(T) == typeof(string))
 
145
                        {
 
146
                                var singleRow = GetSingleRow(records, typeof(T));
 
147
                                WriteRow(writer, singleRow);
 
148
                                return;
 
149
                        }
 
150
 
 
151
                        var row = new string[Headers.Count];
 
152
                        foreach (var record in records)
 
153
                        {
 
154
                                for (var i = 0; i < PropertyGetters.Count; i++)
 
155
                                {
 
156
                                        var propertyGetter = PropertyGetters[i];
 
157
                                        var value = propertyGetter(record) ?? "";
 
158
 
 
159
                                        var strValue = value.GetType() == typeof(string)
 
160
                                           ? (string)value
 
161
                                           : TypeSerializer.SerializeToString(value);
 
162
 
 
163
                                        row[i] = strValue;
 
164
                                }
 
165
                                WriteRow(writer, row);
 
166
                        }
 
167
                }
 
168
 
 
169
                public static void WriteRow(TextWriter writer, T row)
 
170
                {
 
171
                        if (row == null) return; //AOT
 
172
 
 
173
                        Write(writer, new[] { row });
 
174
                }
 
175
 
 
176
                public static void WriteRow(TextWriter writer, IEnumerable<string> row)
 
177
                {
 
178
                        var ranOnce = false;
 
179
                        foreach (var field in row)
 
180
                        {
 
181
                                JsWriter.WriteItemSeperatorIfRanOnce(writer, ref ranOnce);
 
182
 
 
183
                                writer.Write(field.ToCsvField());
 
184
                        }
 
185
                        writer.WriteLine();
 
186
                }
 
187
 
 
188
                public static void Write(TextWriter writer, IEnumerable<List<string>> rows)
 
189
                {
 
190
                        if (Headers.Count > 0)
 
191
                        {
 
192
                                var ranOnce = false;
 
193
                                foreach (var header in Headers)
 
194
                                {
 
195
                                        JsWriter.WriteItemSeperatorIfRanOnce(writer, ref ranOnce);
 
196
 
 
197
                                        writer.Write(header);
 
198
                                }
 
199
                                writer.WriteLine();
 
200
                        }
 
201
 
 
202
                        foreach (var row in rows)
 
203
                        {
 
204
                                WriteRow(writer, row);
 
205
                        }
 
206
                }
 
207
        }
 
208
 
 
209
}
 
 
b'\\ No newline at end of file'