0
|
1 using System;
|
|
2 using System.IO;
|
|
3 using System.Text;
|
|
4
|
|
5 using BLToolkit.Reflection;
|
|
6
|
|
7 namespace BLToolkit.Mapping
|
|
8 {
|
|
9 public class TextDataWriter : ISupportMapping
|
|
10 {
|
|
11 #region Constructors
|
|
12
|
|
13 public TextDataWriter(Stream stream, params string[] fieldNames)
|
|
14 : this(stream, Map.DefaultSchema, fieldNames)
|
|
15 {
|
|
16 }
|
|
17
|
|
18 public TextDataWriter(Stream stream, Type type)
|
|
19 : this(stream, Map.DefaultSchema, Map.GetObjectMapper(type).FieldNames)
|
|
20 {
|
|
21 }
|
|
22
|
|
23 public TextDataWriter(Stream stream, MappingSchema mappingSchema, params string[] fieldNames)
|
|
24 : this(new StreamWriter(stream), mappingSchema, fieldNames)
|
|
25 {
|
|
26 }
|
|
27
|
|
28 public TextDataWriter(Stream stream, MappingSchema mappingSchema, Type type)
|
|
29 : this(stream, mappingSchema, mappingSchema.GetObjectMapper(type).FieldNames)
|
|
30 {
|
|
31 }
|
|
32
|
|
33 public TextDataWriter(TextWriter writer, params string[] fieldNames)
|
|
34 : this(writer, Map.DefaultSchema, fieldNames)
|
|
35 {
|
|
36 }
|
|
37
|
|
38 public TextDataWriter(TextWriter writer, Type type)
|
|
39 : this(writer, Map.DefaultSchema, Map.GetObjectMapper(type).FieldNames)
|
|
40 {
|
|
41 }
|
|
42
|
|
43 public TextDataWriter(TextWriter writer, MappingSchema mappingSchema, params string[] fieldNames)
|
|
44 {
|
|
45 GC.SuppressFinalize(this);
|
|
46
|
|
47 if (mappingSchema == null) throw new ArgumentNullException("mappingSchema");
|
|
48
|
|
49 _writer = writer;
|
|
50 _names = fieldNames;
|
|
51
|
|
52 _values = new string[_names.Length];
|
|
53
|
|
54 WriteHeader();
|
|
55 }
|
|
56
|
|
57 public TextDataWriter(TextWriter writer, MappingSchema mappingSchema, Type type)
|
|
58 : this(writer, mappingSchema, mappingSchema.GetObjectMapper(type).FieldNames)
|
|
59 {
|
|
60 }
|
|
61
|
|
62 #endregion
|
|
63
|
|
64 #region Public Members
|
|
65
|
|
66 public virtual void WriteEnd()
|
|
67 {
|
|
68 _writer.WriteLine("*-");
|
|
69 _writer.Flush();
|
|
70 }
|
|
71
|
|
72 public virtual Type GetFieldType(int index)
|
|
73 {
|
|
74 return typeof(string);
|
|
75 }
|
|
76
|
|
77 public virtual int GetOrdinal(string name)
|
|
78 {
|
|
79 for (int i = 0; i < _names.Length; i++)
|
|
80 if (_names[i] == name)
|
|
81 return i;
|
|
82
|
|
83 return 0;
|
|
84 }
|
|
85
|
|
86 public virtual void SetValue(int index, object value)
|
|
87 {
|
|
88 _values[index] = value == null? null: value.ToString();
|
|
89 }
|
|
90
|
|
91 public virtual void SetValue(string name, object value)
|
|
92 {
|
|
93 SetValue(GetOrdinal(name), value);
|
|
94 }
|
|
95
|
|
96 #endregion
|
|
97
|
|
98 #region Protected Members
|
|
99
|
|
100 private readonly TextWriter _writer;
|
|
101 private readonly string[] _names;
|
|
102 private readonly string[] _values;
|
|
103
|
|
104 private void WriteHeader()
|
|
105 {
|
|
106 _writer.Write("*");
|
|
107
|
|
108 foreach (string name in _names)
|
|
109 {
|
|
110 _writer.Write(':');
|
|
111 _writer.Write(name);
|
|
112 }
|
|
113
|
|
114 _writer.WriteLine();
|
|
115 }
|
|
116
|
|
117 private void WriteRecord()
|
|
118 {
|
|
119 _writer.Write("**");
|
|
120
|
|
121 char[] delimiters = new char[] { ',', ':', '|', '-', '_' };
|
|
122 char delimiter = '\0';
|
|
123
|
|
124 foreach (char ch in delimiters)
|
|
125 {
|
|
126 bool found = false;
|
|
127
|
|
128 foreach (string value in _values)
|
|
129 {
|
|
130 if (value != null && value.IndexOf(ch) >= 0)
|
|
131 {
|
|
132 found = true;
|
|
133 break;
|
|
134 }
|
|
135 }
|
|
136
|
|
137 if (!found)
|
|
138 {
|
|
139 delimiter = ch;
|
|
140 break;
|
|
141 }
|
|
142 }
|
|
143
|
|
144 if (delimiter == 0)
|
|
145 delimiter = delimiters[0];
|
|
146
|
|
147 char[] exChars = new char[] { delimiter, '\r', '\n', '\t', '\0' };
|
|
148
|
|
149 foreach (string value in _values)
|
|
150 {
|
|
151 _writer.Write(delimiter);
|
|
152
|
|
153 if (value != null)
|
|
154 {
|
|
155 if (value.Length == 0)
|
|
156 _writer.Write('*');
|
|
157 else
|
|
158 {
|
|
159 if (value.IndexOfAny(exChars) >= 0)
|
|
160 {
|
|
161 _writer.Write('+');
|
|
162 _writer.Write(Convert.ToBase64String(Encoding.Unicode.GetBytes(value)));
|
|
163 }
|
|
164 else
|
|
165 {
|
|
166 if (value[0] == '*' || value[0] == '+')
|
|
167 _writer.Write('*');
|
|
168
|
|
169 _writer.Write(value);
|
|
170 }
|
|
171 }
|
|
172 }
|
|
173 }
|
|
174
|
|
175 _writer.WriteLine();
|
|
176 _writer.Flush();
|
|
177 }
|
|
178
|
|
179 #endregion
|
|
180
|
|
181 #region ISupportMapping Members
|
|
182
|
|
183 void ISupportMapping.BeginMapping(InitContext initContext)
|
|
184 {
|
|
185 }
|
|
186
|
|
187 void ISupportMapping.EndMapping(InitContext initContext)
|
|
188 {
|
|
189 WriteRecord();
|
|
190 }
|
|
191
|
|
192 #endregion
|
|
193 }
|
|
194 }
|