-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.cs
More file actions
167 lines (132 loc) · 5.16 KB
/
Parser.cs
File metadata and controls
167 lines (132 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
namespace parsley
{
public class Parser : IParser
{
protected char Delimiter { get; set; }
public Parser() : this(',')
{
}
public Parser(char delimiter)
{
Delimiter = delimiter;
}
public T[] Parse<T>(string filepath) where T : IFileLine, new()
{
if (string.IsNullOrEmpty(filepath) || !File.Exists(filepath))
return Array.Empty<T>();
var lines = ReadToLines(filepath);
return Parse<T>(lines);
}
public T[] Parse<T>(string[] lines) where T : IFileLine, new()
{
if (lines == null || lines.Length == 0)
return Array.Empty<T>();
;
var list = new T[lines.Length];
var objLock = new object();
var index = 0;
var inputs = lines.Select(line => new { Line = line, Index = index++ });
Parallel.ForEach(inputs, () => new List<T>(),
(obj, loopstate, localStorage) =>
{
var parsed = ParseLine<T>(obj.Line);
parsed.Index = obj.Index;
localStorage.Add(parsed);
return localStorage;
},
finalStorage =>
{
if (finalStorage == null)
return;
lock (objLock)
finalStorage.ForEach(f => list[f.Index] = f);
});
return list;
}
private string[] ReadToLines(string path)
{
var lines = new List<string>();
foreach (var line in File.ReadLines(path))
{
if (line != null)
lines.Add(line);
}
return lines.ToArray<string>();
}
private T ParseLine<T>(string line) where T : IFileLine, new()
{
var obj = new T();
var values = GetDelimiterSeparatedValues(line);
if (values.Length == 0 || values.Length == 1)
{
obj.SetError(Resources.InvalidLineFormat);
return obj;
}
var propInfos = GetLineClassPropertyInfos<T>();
if (propInfos.Length == 0)
{
obj.SetError(string.Format(Resources.NoColumnAttributesFoundFormat, typeof(T).Name));
return obj;
}
if (propInfos.Length != values.Length)
{
obj.SetError(Resources.InvalidLengthErrorFormat);
return obj;
}
foreach (var propInfo in propInfos)
try
{
var attribute = (ColumnAttribute)propInfo.GetCustomAttributes(typeof(ColumnAttribute), true).First();
var pvalue = values[attribute.Index];
if (string.IsNullOrWhiteSpace(pvalue) && attribute.DefaultValue != null)
pvalue = attribute.DefaultValue.ToString();
if (propInfo.PropertyType.IsEnum)
{
if (string.IsNullOrWhiteSpace(pvalue))
{
obj.SetError(string.Format(Resources.InvalidEnumValueErrorFormat, propInfo.Name));
continue;
}
if (long.TryParse(pvalue, out var enumLong))
{
var numeric = Enum.ToObject(propInfo.PropertyType, enumLong);
propInfo.SetValue(obj, numeric, null);
continue;
}
var val = Enum.Parse(propInfo.PropertyType, pvalue, true);
propInfo.SetValue(obj, val, null);
continue;
}
var converter = TypeDescriptor.GetConverter(propInfo.PropertyType);
var value = converter.ConvertFrom(pvalue);
propInfo.SetValue(obj, value, null);
}
catch (Exception e)
{
obj.SetError(string.Format(Resources.LineExceptionFormat, propInfo.Name, e.Message));
}
return obj;
}
private static PropertyInfo[] GetLineClassPropertyInfos<T>() where T : IFileLine, new()
{
var propInfos = typeof(T).GetProperties()
.Where(p => p.GetCustomAttributes(typeof(ColumnAttribute), true).Any() && p.CanWrite)
.ToArray();
return propInfos;
}
private string[] GetDelimiterSeparatedValues(string line)
{
var values = line.Split(Delimiter)
.Select(x => !string.IsNullOrWhiteSpace(x) ? x.Trim() : x)
.ToArray();
return values;
}
}
}