1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Windows.Forms;
|
---|
5 | using System.Text;
|
---|
6 |
|
---|
7 | namespace Common
|
---|
8 | {
|
---|
9 | public enum OutputFormat
|
---|
10 | {
|
---|
11 | [FieldDisplayName("Excel (Tab separated)")]
|
---|
12 | Excel,
|
---|
13 | [FieldDisplayName("Plain text")]
|
---|
14 | Plain,
|
---|
15 | [FieldDisplayName("CSV")]
|
---|
16 | Csv,
|
---|
17 | [FieldDisplayName("HTML")]
|
---|
18 | Html,
|
---|
19 | [FieldDisplayName("ListView")]
|
---|
20 | ListView,
|
---|
21 | [FieldDisplayName("MediaWiki")]
|
---|
22 | MediaWiki,
|
---|
23 | [FieldDisplayName("JSON")]
|
---|
24 | Json,
|
---|
25 | [FieldDisplayName("XML")]
|
---|
26 | Xml
|
---|
27 | };
|
---|
28 |
|
---|
29 | public class TableRow
|
---|
30 | {
|
---|
31 | public List<string> Cells = new List<string>();
|
---|
32 |
|
---|
33 | public void AddCell(string text)
|
---|
34 | {
|
---|
35 | Cells.Add(text);
|
---|
36 | }
|
---|
37 | }
|
---|
38 |
|
---|
39 | public class TableColumn
|
---|
40 | {
|
---|
41 | public string Name;
|
---|
42 | public ColumnDataType DataType;
|
---|
43 | }
|
---|
44 |
|
---|
45 | public class Table
|
---|
46 | {
|
---|
47 | public string Title;
|
---|
48 | public List<TableRow> Rows = new List<TableRow>();
|
---|
49 | public List<TableColumn> Columns = new List<TableColumn>();
|
---|
50 |
|
---|
51 | public void Clear()
|
---|
52 | {
|
---|
53 | Columns.Clear();
|
---|
54 | Rows.Clear();
|
---|
55 | }
|
---|
56 |
|
---|
57 | public void AddColumn(string name, ColumnDataType dataType = ColumnDataType.String)
|
---|
58 | {
|
---|
59 | Columns.Add(new TableColumn() { DataType = dataType, Name = name });
|
---|
60 | }
|
---|
61 |
|
---|
62 | public TableRow AddRow()
|
---|
63 | {
|
---|
64 | TableRow row = new TableRow();
|
---|
65 | Rows.Add(row);
|
---|
66 | return row;
|
---|
67 | }
|
---|
68 |
|
---|
69 | public string GetOutputTabs()
|
---|
70 | {
|
---|
71 | StringBuilder output = new StringBuilder();
|
---|
72 | output.AppendLine(string.Join("\t", Columns.Select(x => x.Name)));
|
---|
73 | foreach (var row in Rows)
|
---|
74 | {
|
---|
75 | output.AppendLine(string.Join("\t", row.Cells));
|
---|
76 | }
|
---|
77 | return output.ToString();
|
---|
78 | }
|
---|
79 |
|
---|
80 | public string GetOutputPlain()
|
---|
81 | {
|
---|
82 | StringBuilder output = new StringBuilder();
|
---|
83 | output.AppendLine(string.Join(Environment.NewLine, Columns.Select(x => x.Name)));
|
---|
84 | output.AppendLine();
|
---|
85 | output.AppendLine("===========================");
|
---|
86 | output.AppendLine();
|
---|
87 | foreach (var row in Rows)
|
---|
88 | {
|
---|
89 | output.AppendLine(string.Join(Environment.NewLine, row.Cells));
|
---|
90 | output.AppendLine();
|
---|
91 | output.AppendLine("===========================");
|
---|
92 | output.AppendLine();
|
---|
93 | }
|
---|
94 | return output.ToString();
|
---|
95 | }
|
---|
96 |
|
---|
97 | public string GetOutputCsv()
|
---|
98 | {
|
---|
99 | StringBuilder output = new StringBuilder();
|
---|
100 | output.AppendLine(string.Join(",", Columns.Select(x => "\"" + x.Name + "\"")));
|
---|
101 | foreach (var row in Rows)
|
---|
102 | {
|
---|
103 | output.AppendLine(string.Join(",", row.Cells.Select(x => "\"" + x.Replace("\"", "\"\"") + "\"")));
|
---|
104 | }
|
---|
105 | return output.ToString();
|
---|
106 | }
|
---|
107 |
|
---|
108 | public string GetOutputHtml()
|
---|
109 | {
|
---|
110 | StringBuilder output = new StringBuilder();
|
---|
111 | output.AppendLine("<html>");
|
---|
112 | if (!string.IsNullOrEmpty(Title))
|
---|
113 | {
|
---|
114 | output.AppendLine(" <head>");
|
---|
115 | output.AppendLine(" <title>" + Title + "</title>");
|
---|
116 | output.AppendLine(" </head>");
|
---|
117 | }
|
---|
118 | output.AppendLine(" <body>");
|
---|
119 | output.AppendLine(" <table border=\"1\">");
|
---|
120 | output.AppendLine(" <tr><th>" + string.Join("</th><th>", Columns.Select(x => x.Name.Replace(Environment.NewLine, "<br/>"))) + "</th></tr>");
|
---|
121 | foreach (var row in Rows)
|
---|
122 | {
|
---|
123 | output.AppendLine(" <tr><td>" + string.Join("</td><td>", row.Cells.Select(x => x.Replace(Environment.NewLine, "<br/>"))) + "</td></tr>");
|
---|
124 | }
|
---|
125 | output.AppendLine(" </table>");
|
---|
126 | output.AppendLine(" </body>");
|
---|
127 | output.AppendLine("</html>");
|
---|
128 | return output.ToString();
|
---|
129 | }
|
---|
130 |
|
---|
131 | public string GetOutputXml()
|
---|
132 | {
|
---|
133 | StringBuilder output = new StringBuilder();
|
---|
134 | output.AppendLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
|
---|
135 | output.AppendLine("<table>");
|
---|
136 | if (!string.IsNullOrEmpty(Title)) output.AppendLine(" <title>" + Title + "</title>");
|
---|
137 | output.AppendLine(" <rows>");
|
---|
138 | foreach (var row in Rows)
|
---|
139 | {
|
---|
140 | output.AppendLine(" <row>");
|
---|
141 | int x = 0;
|
---|
142 | foreach (var cell in row.Cells)
|
---|
143 | {
|
---|
144 | string columnName = "";
|
---|
145 | if (x < Columns.Count) columnName = Columns[x].Name.Replace("\"", "\\\"");
|
---|
146 | output.AppendLine(" <cell name=\"" + columnName + "\">" + cell + "</cell>");
|
---|
147 | x++;
|
---|
148 | }
|
---|
149 | output.AppendLine(" </row>");
|
---|
150 | }
|
---|
151 | output.AppendLine(" </rows>");
|
---|
152 | output.AppendLine("</table>");
|
---|
153 | return output.ToString();
|
---|
154 | }
|
---|
155 |
|
---|
156 | public string GetOutputMediaWiki()
|
---|
157 | {
|
---|
158 | StringBuilder output = new StringBuilder();
|
---|
159 | output.AppendLine("{| class=\"wikitable sortable\"");
|
---|
160 | output.AppendLine("! " + string.Join(" !! ", Columns.Select(x => x.Name.Replace(Environment.NewLine, "<br/>"))));
|
---|
161 | if (!string.IsNullOrEmpty(Title)) output.AppendLine("|+ " + Title);
|
---|
162 | foreach (var row in Rows)
|
---|
163 | {
|
---|
164 | output.AppendLine("|-");
|
---|
165 | output.AppendLine("| " + string.Join(" || ", row.Cells.Select(x => x.Replace(Environment.NewLine, "<br/>"))));
|
---|
166 | }
|
---|
167 | output.AppendLine("|}");
|
---|
168 | return output.ToString();
|
---|
169 | }
|
---|
170 |
|
---|
171 | public string GetOutputJson()
|
---|
172 | {
|
---|
173 | StringBuilder output = new StringBuilder();
|
---|
174 | output.AppendLine("[");
|
---|
175 | for (int y = 0; y < Rows.Count; y++)
|
---|
176 | {
|
---|
177 | output.AppendLine(" {");
|
---|
178 | for (int x = 0; x < Rows[y].Cells.Count; x++)
|
---|
179 | {
|
---|
180 | string columnName = "";
|
---|
181 | if (x < Columns.Count) columnName = Columns[x].Name.Replace("\"", "\\\"");
|
---|
182 | output.Append(" \"" + columnName + "\": \"" + Rows[y].Cells[x].Replace("\"", "\\\"") + "\"");
|
---|
183 | if (x < Rows[y].Cells.Count - 1)
|
---|
184 | {
|
---|
185 | output.Append(",");
|
---|
186 | }
|
---|
187 | output.AppendLine();
|
---|
188 | }
|
---|
189 | output.Append(" }");
|
---|
190 | if (y < Rows.Count - 1)
|
---|
191 | {
|
---|
192 | output.Append(",");
|
---|
193 | }
|
---|
194 | output.AppendLine();
|
---|
195 | }
|
---|
196 | output.AppendLine("]");
|
---|
197 | return output.ToString();
|
---|
198 | }
|
---|
199 |
|
---|
200 | public void GetOutputListView(ListView listView)
|
---|
201 | {
|
---|
202 | listView.BeginUpdate();
|
---|
203 | try
|
---|
204 | {
|
---|
205 | List<ListViewItem> listViewItems = new List<ListViewItem>
|
---|
206 | {
|
---|
207 | Capacity = Rows.Count
|
---|
208 | };
|
---|
209 |
|
---|
210 | while (listView.Columns.Count < Columns.Count)
|
---|
211 | listView.Columns.Add(Columns[listView.Columns.Count].Name);
|
---|
212 |
|
---|
213 | for (int i = 0; i < Columns.Count; i++)
|
---|
214 | {
|
---|
215 | listView.Columns[i].Tag = i;
|
---|
216 | }
|
---|
217 | ListViewManager listViewManager = new ListViewManager(listView);
|
---|
218 | for (int i = 0; i < Columns.Count; i++)
|
---|
219 | {
|
---|
220 | listViewManager.SetColumnDataType(i, Columns[i].DataType);
|
---|
221 | }
|
---|
222 |
|
---|
223 | foreach (var row in Rows)
|
---|
224 | {
|
---|
225 | while (listView.Columns.Count < row.Cells.Count)
|
---|
226 | listView.Columns.Add("");
|
---|
227 |
|
---|
228 | ListViewItem item = new ListViewItem();
|
---|
229 | int i = 0;
|
---|
230 | foreach (string cell in row.Cells)
|
---|
231 | {
|
---|
232 | if (i == 0) item.Text = cell;
|
---|
233 | else item.SubItems.Add(cell);
|
---|
234 | i++;
|
---|
235 | }
|
---|
236 |
|
---|
237 | listViewItems.Add(item);
|
---|
238 | }
|
---|
239 |
|
---|
240 | // Add prepared list view items into visible list view at once
|
---|
241 | listView.Items.Clear();
|
---|
242 | listView.Items.AddRange(listViewItems.ToArray());
|
---|
243 |
|
---|
244 | // Update columns width
|
---|
245 | foreach (ColumnHeader column in listView.Columns)
|
---|
246 | {
|
---|
247 | column.Width = listView.Width / listView.Columns.Count;
|
---|
248 | }
|
---|
249 | }
|
---|
250 | finally
|
---|
251 | {
|
---|
252 | listView.EndUpdate();
|
---|
253 | }
|
---|
254 | }
|
---|
255 |
|
---|
256 | public string GetOutput(OutputFormat outputFormat)
|
---|
257 | {
|
---|
258 | switch (outputFormat)
|
---|
259 | {
|
---|
260 | case OutputFormat.Excel:
|
---|
261 | return GetOutputTabs();
|
---|
262 | case OutputFormat.Plain:
|
---|
263 | return GetOutputPlain();
|
---|
264 | case OutputFormat.Csv:
|
---|
265 | return GetOutputCsv();
|
---|
266 | case OutputFormat.Html:
|
---|
267 | return GetOutputHtml();
|
---|
268 | case OutputFormat.MediaWiki:
|
---|
269 | return GetOutputMediaWiki();
|
---|
270 | case OutputFormat.Json:
|
---|
271 | return GetOutputJson();
|
---|
272 | case OutputFormat.Xml:
|
---|
273 | return GetOutputXml();
|
---|
274 | default:
|
---|
275 | return "";
|
---|
276 | }
|
---|
277 | }
|
---|
278 |
|
---|
279 | public static string GetFileExt(OutputFormat outputFormat)
|
---|
280 | {
|
---|
281 | string[] outputFormatFileExt = new string[] { ".txt", ".txt", ".csv", ".htm", ".txt", ".txt", ".json", ".xml"};
|
---|
282 |
|
---|
283 | return outputFormatFileExt[(int)outputFormat];
|
---|
284 | }
|
---|
285 |
|
---|
286 | public void LoadFromListView(ListView listView)
|
---|
287 | {
|
---|
288 | Clear();
|
---|
289 | Columns.Capacity = listView.Columns.Count;
|
---|
290 | foreach (ColumnHeader column in listView.Columns)
|
---|
291 | {
|
---|
292 | AddColumn(column.Text);
|
---|
293 | }
|
---|
294 |
|
---|
295 | Rows.Capacity = listView.Items.Count;
|
---|
296 | for (int i = 0; i < listView.Items.Count; i++)
|
---|
297 | {
|
---|
298 | TableRow row = AddRow();
|
---|
299 | row.Cells.Capacity = listView.Items[i].SubItems.Count;
|
---|
300 | foreach (ListViewItem.ListViewSubItem subItem in listView.Items[i].SubItems)
|
---|
301 | {
|
---|
302 | row.AddCell(subItem.Text);
|
---|
303 | }
|
---|
304 | }
|
---|
305 | }
|
---|
306 | }
|
---|
307 | }
|
---|