Repository navigation
Expand file tree
/
Copy pathDataStructure.cs
More file actions
219 lines (174 loc) · 5.82 KB
/
Copy pathDataStructure.cs
File metadata and controls
219 lines (174 loc) · 5.82 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
using System.Collections.Generic;
using System.Text.RegularExpressions;
using NeoDoc.Params;
using Newtonsoft.Json;
namespace NeoDoc.DataStructures
{
public abstract class DataStructure
{
[JsonIgnore]
public bool Ignore { get; set; } = false;
[JsonIgnore]
public virtual string GlobalWrapper { get; set; } = "none";
[JsonIgnore]
public string Realm { get; set; } = "shared";
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public List<Param> ParamsList;
[JsonProperty("path")]
public string FoundPath;
[JsonProperty("line")]
public int FoundLine;
public abstract Regex GetRegex(); // returns the exact RegEx to match e.g. the Function
public abstract void Process(FileParser fileParser); // process data based on given line string
public abstract bool CheckMatch(string line); // returns whether the current DocTarget is matched in this line
public abstract string GetName(); // returns an identification name
public abstract string GetDatastructureName(); // returns the individual name of the matched datasctructure
public abstract object GetData(); // returns data
public virtual void ProcessDatastructure(FileParser fileParser) // used to set default data
{
bool foundRealm = false;
// if param "@realm" or "@ignore" found
if (ParamsList != null)
{
List<Param> copyParamList = new List<Param>();
for (int i = 0; i < ParamsList.Count; i++)
{
Param curParam = ParamsList[i];
if (curParam is RealmParam realmParam)
{
Realm = realmParam.Value;
foundRealm = true;
}
else if (curParam is IgnoreParam)
Ignore = true;
else if (curParam is DescParam descParam)
{
if (!string.IsNullOrEmpty(descParam.Text)) // just add if desc is not empty
copyParamList.Add(curParam);
}
else
copyParamList.Add(curParam);
}
ParamsList = copyParamList.Count > 0 ? copyParamList : null;
}
if (!Ignore)
Process(fileParser);
if (!Ignore && !foundRealm)
NeoDoc.WriteErrors("Missing essential param", new List<string>{
"Missing '@realm' in " + GetName() + " '" + GetDatastructureName() + "'"
}, FoundPath, FoundLine, (int)NeoDoc.ERROR_CODES.MISSING_ESSENTIAL_PARAMS);
}
public virtual void Check() // used to finally check for errors and to print them into the console
{
}
public virtual string GetFullJSONData() // returns full json data, used for the entire page structure
{
Dictionary<string, object> jsonDict = new Dictionary<string, object>
{
{ "type", "datastructure" },
{ "subtype", GetName() },
{ "name", GetDatastructureName() },
{ "realm", Realm },
{ "source", new Dictionary<string, object>()
{
{ "file", FoundPath },
{ "line", FoundLine }
}
}
};
object data = GetData();
if (data != null)
jsonDict.Add("data", data);
if (ParamsList != null && ParamsList.Count > 0)
{
// merge same datastructures together
SortedDictionary<string, object> paramsDict = new SortedDictionary<string, object>();
foreach (Param param in ParamsList)
{
if (!paramsDict.TryGetValue(param.GetName(), out object paramsJSONList))
{
paramsJSONList = new List<object>();
paramsDict.Add(param.GetName(), paramsJSONList);
}
Dictionary<string, object> jsonData = param.GetJSONData();
if (jsonData == null)
{
if (!(paramsJSONList is bool) && ((List<object>)paramsJSONList).Count == 0) // just replace by a bool if there is no data inserted
{
paramsDict.Remove(param.GetName());
paramsDict.Add(param.GetName(), true);
}
}
else if (jsonData.Count > 0) // if there is some additional data, transform into a list.
{
if (paramsJSONList is List<object> tmpList)
tmpList.Add(jsonData);
else
{
paramsDict.Remove(param.GetName());
paramsDict.Add(param.GetName(), new List<object>
{
jsonData
});
}
}
}
jsonDict.Add("params", paramsDict);
}
return JsonConvert.SerializeObject(jsonDict, Formatting.None, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});
}
public virtual DataStructure CheckDataStructureTransformation() // checks whether the data structure should be transformed
{
return this;
}
public virtual bool IsGlobal() // whether the DataStructure should be excluded from the sections / wrappers data structuring
{
return false;
}
internal int CompareTo(DataStructure y)
{
return GetDatastructureName().CompareTo(y.GetDatastructureName());
}
public virtual void Initialize(FileParser fileParser)
{
if (fileParser.paramsList.Count > 0)
ParamsList = new List<Param>(fileParser.paramsList); // set the params with a copy of the list
// set meta information
FoundLine = fileParser.CurrentLineCount + 1;
FoundPath = fileParser.relPath;
ProcessDatastructure(fileParser);
if (Ignore) // just add this datastructure if it does not contain the "Ignore" flag
return;
DataStructure dataStructure = CheckDataStructureTransformation();
if (dataStructure == null)
return;
dataStructure.Check();
// now add the datastructure into the current section of the current container
if (!fileParser.CurrentSection.DataStructureDict.TryGetValue(dataStructure.GetName(), out List<DataStructure> dsList))
{
dsList = new List<DataStructure>();
fileParser.CurrentSection.DataStructureDict.Add(dataStructure.GetName(), dsList);
}
dsList.Add(dataStructure);
}
public virtual DataStructure Merge(DataStructure dataStructure)
{
return null;
}
}
class DataStructureComparator : IComparer<DataStructure>
{
public int Compare(DataStructure x, DataStructure y)
{
if (x == null || y == null)
{
return 0;
}
// "CompareTo()" method
return x.CompareTo(y);
}
}
}