-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathCustomDataModel.cs
More file actions
186 lines (155 loc) · 5.51 KB
/
CustomDataModel.cs
File metadata and controls
186 lines (155 loc) · 5.51 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.IO;
using System.Xml.Linq;
using CsvHelper;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SolidifyProject.Engine.Infrastructure.Enums;
using SolidifyProject.Engine.Infrastructure.Models.Base;
using YamlDotNet.Serialization;
namespace SolidifyProject.Engine.Infrastructure.Models
{
public class CustomDataModel : TextContentModel
{
private string _id;
public override string Id
{
get => _id;
set
{
_id = value;
_sanitezedId = null;
}
}
private string _sanitezedId;
public string SanitezedId
{
get
{
if (string.IsNullOrEmpty(_sanitezedId))
{
_sanitezedId = Id
.Replace('/', '.')
.Replace('\\', '.');
if (Path.HasExtension(Id))
{
var extension = Path.GetExtension(Id);
_sanitezedId = _sanitezedId
.Replace(extension, string.Empty);
}
}
return _sanitezedId;
}
}
public CustomDataType? DataType { get; protected set; }
public object CustomData { get; set; }
public override void Parse()
{
var extension = Path.GetExtension(Id).Trim('.');
if (extension.Equals(CustomDataType.Json.ToString(), StringComparison.OrdinalIgnoreCase))
{
DataType = CustomDataType.Json;
ParseJson();
return;
}
if (extension.Equals(CustomDataType.Xml.ToString(), StringComparison.OrdinalIgnoreCase))
{
DataType = CustomDataType.Xml;
ParseXml();
return;
}
if (extension.Equals(CustomDataType.Yaml.ToString(), StringComparison.OrdinalIgnoreCase) ||
extension.Equals(CustomDataType.Yml.ToString(), StringComparison.OrdinalIgnoreCase))
{
DataType = CustomDataType.Yaml;
ParseYaml();
return;
}
if (extension.Equals(CustomDataType.Csv.ToString(), StringComparison.OrdinalIgnoreCase))
{
DataType = CustomDataType.Csv;
ParseCsv();
return;
}
if (extension.Equals(CustomDataType.Txt.ToString(), StringComparison.OrdinalIgnoreCase))
{
DataType = CustomDataType.Txt;
ParseTxt();
return;
}
throw new NotSupportedException($"Unknown custom data type \"{extension}\"");
}
private void ParseJson()
{
CustomData = JObject.Parse(ContentRaw);
}
private void ParseXml()
{
//hack to convert xml document to dynamic object
XDocument doc = XDocument.Parse(ContentRaw);
string jsonText = JsonConvert.SerializeXNode(doc);
CustomData = JObject.Parse(jsonText);
}
private void ParseYaml()
{
var deserializer = new DeserializerBuilder()
.Build();
object obj;
using (var reader = new StringReader(ContentRaw))
{
obj = deserializer.Deserialize(reader);
}
CustomData = ParseYaml(obj);
}
private object ParseYaml(object node)
{
if (node is ICollection<KeyValuePair<object, object>> nestedObjects)
{
var result = new ExpandoObject();
foreach (var property in nestedObjects)
{
((ICollection<KeyValuePair<string, object>>)result)
.Add(new KeyValuePair<string, object>(property.Key.ToString(), ParseYaml(property.Value)));
}
return result;
}
if (node is ICollection<object> properties)
{
var result = new List<object>();
foreach (var property in properties)
{
result.Add(ParseYaml(property));
}
return result;
}
return node;
}
private void ParseCsv()
{
CustomData = new List<dynamic>();
using (var reader = new StringReader(ContentRaw))
using (var csv = new CsvReader(reader))
{
csv.Read();
csv.ReadHeader();
while (csv.Read())
{
ICollection<KeyValuePair<string, object>> row = csv.GetRecord<dynamic>();
ICollection<KeyValuePair<string, object>> data = new ExpandoObject();
foreach (var cell in row)
{
var property = new KeyValuePair<string, object>(cell.Key.Trim(), cell.Value.ToString().Trim());
data.Add(property);
}
((List<dynamic>)CustomData).Add(data);
}
}
}
private void ParseTxt()
{
CustomData = ContentRaw?.Trim();
}
}
}