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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
namespace DHTMLX.Export.PDF.Scheduler
{
public enum ColorProfile
{
Color,
FullColor,
Gray,
BW,
Custom
}
public class XMLParser
{
public List<PDFPage> Pages { get; set; }
public ColorProfile StringToColorProfile(string profile)
{
if (profile == null)
return ColorProfile.Color;
switch (profile.ToLower())
{
case "gray":
return ColorProfile.Gray;
case "full_color":
return ColorProfile.FullColor;
case "color":
return ColorProfile.Color;
case "bw":
return ColorProfile.BW;
case "custom":
return ColorProfile.Custom;
default:
return ColorProfile.Color;
}
}
public static List<XmlNode> GetElementsByTagName(XmlNode parent, string name)
{
var list = new List<XmlNode>();
foreach (XmlNode node in parent.ChildNodes)
{
if (node.LocalName == name)
{
list.Add(node);
}
else
{
list.AddRange(GetElementsByTagName(node, name));
}
}
return list;
}
public void ParsePages(XmlNode root)
{
if (root.Name == "pages")
{
var n1 = GetElementsByTagName(root, "page");
if (n1 != null && n1.Count > 0)
{
for (var i = 0; i < n1.Count; i++)
{
var page = new PDFPage { Node = n1[i] };
Pages.Add(page);
ParseConfig(page);
EventsParsing(page);
}
}
}
else
{
var page = new PDFPage { Node = root };
Pages.Add(page);
ParseConfig(page);
EventsParsing(page);
}
}
public void ParseConfig(PDFPage page)
{
XmlNode scale = null;
var root = page.Node;
foreach (XmlNode nod in root.ChildNodes)
{
if (nod.LocalName == "scale")
{
scale = nod;
break;
}
}
var toCheck = new List<XmlNode> { root };
if (scale != null)
toCheck.Add(scale);
if (root.ParentNode != null && root.ParentNode.NodeType != XmlNodeType.Document && root.ParentNode.Name != "data")
{
toCheck.Add(root.ParentNode);
}
for (var i = 0; i < toCheck.Count; i++)
{
if (toCheck[i].Attributes["profile"] != null && page.Profile == default(ColorProfile))
{
page.Profile = StringToColorProfile(toCheck[i].Attributes["profile"].Value);
}
if (toCheck[i].Attributes["header"] != null && page.Header == default(bool))
{
page.Header = (toCheck[i].Attributes["header"].Value == "true");
}
if (toCheck[i].Attributes["footer"] != null && page.Footer == default(bool))
{
page.Footer = (toCheck[i].Attributes["footer"].Value == "true");
}
if (toCheck[i].Attributes["mode"] != null && page.Mode == default(string))
page.Mode = toCheck[i].Attributes["mode"] != null ? toCheck[i].Attributes["mode"].Value : "";
if (toCheck[i].Attributes["today"] != null && page.TodayLabel == default(string))
page.TodayLabel = toCheck[i].Attributes["today"] != null ? toCheck[i].Attributes["today"].Value : "";
}
}
public IEnumerable<XmlNode> GetPages(string xml)
{
var dom = new XmlDocument();
dom.LoadXml(xml);
var root = dom.DocumentElement;
var res = new List<XmlNode>();
if (root.FirstChild.Name != "data")
{
res.Add(root);
}
else
{
foreach (var nod in root.ChildNodes)
res.Add(nod as XmlNode);
}
return res;
}
public void SetXML(XmlNode root)
{
Pages = new List<PDFPage>();
ParsePages(root);
}
public string[][] MonthColsParsing(PDFPage page)
{
var n1 = GetElementsByTagName(page.Node, "column");
if (n1 != null && n1.Count > 0)
{
page.Cols = new[] { new string[n1.Count] };
for (var i = 0; i < n1.Count; i++)
{
var col = n1[i];
page.Cols[0][i] = col.FirstChild.Value;
}
}
return page.Cols;
}
public IList<MonthRow> GetRowsObjects(PDFPage page)
{
if (page.RowObs == null)
{
var n1 = GetElementsByTagName(page.Node, "row");
if (n1 != null && n1.Count > 0)
{
page.RowObs = new List<MonthRow>(7);
for (var i = 0; i < n1.Count; i++)
{
var r = new MonthRow();
page.RowObs.Add(r);
var node = n1[i];
double height;
var hght = node.Attributes["height"].Value;
if (double.TryParse(hght, out height))
r.Height = height;
else
r.Height = default(double);
var week = node.FirstChild.Value;
r.Cells = week.Split(new[] { "|" }, StringSplitOptions.None);
}
}
}
return page.RowObs;
}
public string[,] MonthRowsParsing(PDFPage page)
{
var n1 = GetElementsByTagName(page.Node, "row");
if (n1 != null && n1.Count > 0)
{
page.Rows = new string[n1.Count, 7];
for (var i = 0; i < n1.Count; i++)
{
var row = n1[i];
var week = row.FirstChild.Value;
var days = week.Split(new[] { "|" }, StringSplitOptions.None);
for (var j = 0; j < days.Length; j++)
page.Rows[i, j] = days[j];
}
}
return page.Rows;
}
public void EventsParsing(PDFPage page)
{
var n1 = GetElementsByTagName(page.Node, "event");
if (n1 != null && n1.Count > 0)
{
for (var i = 0; i < n1.Count; i++)
{
var ev = n1[i];
SchedulerEvent oEv = new SchedulerEvent();
oEv.Parse(ev);
if ((oEv.Type == "event_line") && (page.Mode != "month") && (page.Mode != "timeline") && (page.Mode != "treetimeline"))
{
page.Multiday.Add(oEv);
}
else
{
page.Events.Add(oEv);
}
}
}
}
public int[] ScaleRatios(PDFPage page)
{
var n1 = GetElementsByTagName(page.Node, "column");
return n1.Where(n => n.Attributes["second_scale"] != null).GroupBy(n => n.Attributes["second_scale"].Value).Select(g => g.Count()).ToArray();
}
public string[][] WeekColsParsing(PDFPage page)
{
if (page.Cols != null)
return page.Cols;
var n1 = GetElementsByTagName(page.Node, "column");
if (n1 != null && n1.Count > 0)
{
var scale1 = n1.Where(n => n.Attributes["second_scale"] == null).ToList();
var scale2 = n1.Where(n => n.Attributes["second_scale"] != null).ToList();
string[][] scales;
if (scale2.Count > 0)
{
scales = new string[2][];
scales[0] = new string[scale1.Count];
scales[1] = new string[scale2.Count];
}
else
{
scales = new string[1][];
scales[0] = new string[scale1.Count];
}
for (var i = 0; i < scale1.Count; i++)
{
scales[0][i] = scale1[i].FirstChild.Value;
}
for (var i = 0; i < scale2.Count; i++)
{
scales[1][i] = scale2[i].FirstChild.Value;
}
page.Cols = scales;
}
return page.Cols;
}
public string[] WeekRowsParsing(PDFPage page)
{
string[] rows = null;
var n1 = GetElementsByTagName(page.Node, "row");
if (n1 != null && n1.Count > 0)
{
rows = new string[n1.Count];
for (var i = 0; i < n1.Count; i++)
{
var row = n1[i];
rows[i] = row.FirstChild.Value;
}
}
return rows;
}
public SchedulerMonth[] YearParsing(PDFPage page)
{
SchedulerMonth[] monthes = null;
var n1 = GetElementsByTagName(page.Node, "month");
if (n1 != null && n1.Count > 0)
{
monthes = new SchedulerMonth[n1.Count];
for (var i = 0; i < n1.Count; i++)
{
monthes[i] = new SchedulerMonth();
var mon = n1[i];
monthes[i].Parse(mon);
}
}
return monthes;
}
public string[] AgendaColsParsing(PDFPage page)
{
string[] cols = null;
var n1 = GetElementsByTagName(page.Node, "column");
if (n1 != null && n1.Count > 0)
{
cols = new string[n1.Count];
for (var i = 0; i < n1.Count; i++)
{
var col = n1[i];
cols[i] = col.FirstChild.Value;
}
}
return cols;
}
}
}
|