forked from jgraph/mxgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmxSaxOutputHandler.cs
More file actions
380 lines (318 loc) · 12.4 KB
/
mxSaxOutputHandler.cs
File metadata and controls
380 lines (318 loc) · 12.4 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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
namespace com.mxgraph
{
public class mxSaxOutputHandler
{
/// <summary>
/// Holds the current canvas.
/// </summary>
protected mxICanvas2D canvas;
/// <summary>
/// Holds the handlers for specific XML nodes.
/// </summary>
protected Dictionary<string, ElementHandler> handlers = new Dictionary<string, ElementHandler>();
/// <summary>
/// Defines the requirements for an object that parses a node.
/// </summary>
public delegate void ElementHandler(Dictionary<string, string> atts);
/// <summary>
/// Constructs a new sax output handler for the given canvas.
/// </summary>
public mxSaxOutputHandler(mxICanvas2D canvas)
{
Canvas = canvas;
InitHandlers();
}
/// <summary>
/// Sets or returns the current canvas.
/// </summary>
public mxICanvas2D Canvas
{
get { return canvas; }
set { canvas = value; }
}
/// <summary>
/// Reads the given display XML data and parses all elements.
/// </summary>
/// <param name="reader">Reader that represents the display XML data.</param>
public void Read(XmlReader reader)
{
if (reader != null)
{
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
string tagName = reader.LocalName.ToUpper();
Dictionary<string, string> attrs =
new Dictionary<string, string>();
if (reader.MoveToFirstAttribute())
{
do
{
attrs[reader.LocalName] = reader.Value;
} while (reader.MoveToNextAttribute());
}
ParseElement(tagName, attrs);
}
}
}
}
/// <summary>
/// Parses the given element and paints it onto the canvas.
/// </summary>
/// <param name="tagName">Name of the node to be parsed.</param>
/// <param name="attrs">Attributes of the node to be parsed.</param>
public void ParseElement(string tagName, Dictionary<string, string> atts)
{
string key = tagName.ToLower();
if (handlers.ContainsKey(key))
{
handlers[key](atts);
}
}
protected string GetString(Dictionary<string, string> atts, string key)
{
return GetString(atts, key, null);
}
protected string GetString(Dictionary<string, string> atts, string key, string defaultValue)
{
if (atts.ContainsKey(key))
{
defaultValue = atts[key];
}
return defaultValue;
}
protected double GetDouble(Dictionary<string, string> atts, string key)
{
return GetDouble(atts, key, 0);
}
protected double GetDouble(Dictionary<string, string> atts, string key, double defaultValue)
{
if (atts.ContainsKey(key))
{
defaultValue = double.Parse(atts[key], System.Globalization.CultureInfo.InvariantCulture);
}
return defaultValue;
}
protected int GetInt(Dictionary<string, string> atts, string key)
{
return GetInt(atts, key, 0);
}
protected int GetInt(Dictionary<string, string> atts, string key, int defaultValue)
{
if (atts.ContainsKey(key))
{
defaultValue = int.Parse(atts[key]);
}
return defaultValue;
}
protected bool IsTrue(Dictionary<string, string> atts, string key)
{
return IsTrue(atts, key, false);
}
protected bool IsTrue(Dictionary<string, string> atts, string key, bool defaultValue)
{
if (atts.ContainsKey(key))
{
defaultValue = atts[key].Equals("1");
}
return defaultValue;
}
/// <summary>
///
/// </summary>
protected void InitHandlers()
{
handlers["save"] = delegate(Dictionary<string, string> atts)
{
canvas.Save();
};
handlers["restore"] = delegate(Dictionary<string, string> atts)
{
canvas.Restore();
};
handlers["scale"] = delegate(Dictionary<string, string> atts)
{
canvas.Scale(GetDouble(atts, "scale"));
};
handlers["translate"] = delegate(Dictionary<string, string> atts)
{
canvas.Translate(GetDouble(atts, "dx"), GetDouble(atts, "dy"));
};
handlers["rotate"] = delegate(Dictionary<string, string> atts)
{
canvas.Rotate(GetDouble(atts, "theta"), IsTrue(atts, "flipH"),
IsTrue(atts, "flipV"), GetDouble(atts, "cx"), GetDouble(atts, "cy"));
};
handlers["strokewidth"] = delegate(Dictionary<string, string> atts)
{
canvas.StrokeWidth = GetDouble(atts, "width");
};
handlers["strokecolor"] = delegate(Dictionary<string, string> atts)
{
canvas.StrokeColor = GetString(atts, "color");
};
handlers["dashed"] = delegate(Dictionary<string, string> atts)
{
canvas.Dashed = IsTrue(atts, "dashed");
canvas.FixDash = IsTrue(atts, "fixDash", false);
};
handlers["dashpattern"] = delegate(Dictionary<string, string> atts)
{
canvas.DashPattern = GetString(atts, "pattern");
};
handlers["linecap"] = delegate(Dictionary<string, string> atts)
{
canvas.LineCap = GetString(atts, "cap");
};
handlers["linejoin"] = delegate(Dictionary<string, string> atts)
{
canvas.LineJoin = GetString(atts, "join");
};
handlers["miterlimit"] = delegate(Dictionary<string, string> atts)
{
canvas.MiterLimit = GetDouble(atts, "limit");
};
handlers["fontsize"] = delegate(Dictionary<string, string> atts)
{
canvas.FontSize = GetDouble(atts, "size");
};
handlers["fontcolor"] = delegate(Dictionary<string, string> atts)
{
canvas.FontColor = GetString(atts, "color");
};
handlers["fontbackgroundcolor"] = delegate(Dictionary<string, string> atts)
{
canvas.FontBackgroundColor = GetString(atts, "color");
};
handlers["fontbordercolor"] = delegate(Dictionary<string, string> atts)
{
canvas.FontBorderColor = GetString(atts, "color");
};
handlers["fontfamily"] = delegate(Dictionary<string, string> atts)
{
canvas.FontFamily = GetString(atts, "family");
};
handlers["fontstyle"] = delegate(Dictionary<string, string> atts)
{
canvas.FontStyle = GetInt(atts, "style");
};
handlers["alpha"] = delegate(Dictionary<string, string> atts)
{
canvas.Alpha = GetDouble(atts, "alpha");
};
handlers["strokealpha"] = delegate(Dictionary<string, string> atts)
{
canvas.StrokeAlpha = GetDouble(atts, "alpha");
};
handlers["fillalpha"] = delegate(Dictionary<string, string> atts)
{
canvas.FillAlpha = GetDouble(atts, "alpha");
};
handlers["fillcolor"] = delegate(Dictionary<string, string> atts)
{
canvas.FillColor = GetString(atts, "color");
};
handlers["shadowcolor"] = delegate(Dictionary<string, string> atts)
{
canvas.ShadowColor = GetString(atts, "color");
};
handlers["shadowalpha"] = delegate(Dictionary<string, string> atts)
{
canvas.ShadowAlpha = GetDouble(atts, "alpha");
};
handlers["shadowoffset"] = delegate(Dictionary<string, string> atts)
{
canvas.SetShadowOffset(GetDouble(atts, "dx"), GetDouble(atts, "dy"));
};
handlers["shadow"] = delegate(Dictionary<string, string> atts)
{
canvas.Shadow = IsTrue(atts, "enabled");
};
handlers["gradient"] = delegate(Dictionary<string, string> atts)
{
canvas.SetGradient(GetString(atts, "c1"), GetString(atts, "c2"),
GetDouble(atts, "x"), GetDouble(atts, "y"),
GetDouble(atts, "w"), GetDouble(atts, "h"),
GetString(atts, "direction"), GetDouble(atts, "alpha1", 1),
GetDouble(atts, "alpha2", 1));
};
handlers["rect"] = delegate(Dictionary<string, string> atts)
{
canvas.Rect(GetDouble(atts, "x"), GetDouble(atts, "y"),
GetDouble(atts, "w"), GetDouble(atts, "h"));
};
handlers["roundrect"] = delegate(Dictionary<string, string> atts)
{
canvas.Roundrect(GetDouble(atts, "x"), GetDouble(atts, "y"),
GetDouble(atts, "w"), GetDouble(atts, "h"),
GetDouble(atts, "dx"), GetDouble(atts, "dy"));
};
handlers["ellipse"] = delegate(Dictionary<string, string> atts)
{
canvas.Ellipse(GetDouble(atts, "x"), GetDouble(atts, "y"),
GetDouble(atts, "w"), GetDouble(atts, "h"));
};
handlers["image"] = delegate(Dictionary<string, string> atts)
{
canvas.Image(GetDouble(atts, "x"), GetDouble(atts, "y"),
GetDouble(atts, "w"), GetDouble(atts, "h"),
GetString(atts, "src"), IsTrue(atts, "aspect"),
IsTrue(atts, "flipH"), IsTrue(atts, "flipV"));
};
handlers["text"] = delegate(Dictionary<string, string> atts)
{
canvas.Text(GetDouble(atts, "x"), GetDouble(atts, "y"),
GetDouble(atts, "w"), GetDouble(atts, "h"),
GetString(atts, "str"), GetString(atts, "align"),
GetString(atts, "valign"), IsTrue(atts, "wrap"),
GetString(atts, "format"), GetString(atts, "overflow"),
IsTrue(atts, "clip"), GetDouble(atts, "rotation"),
GetString(atts, "dir"));
};
handlers["begin"] = delegate(Dictionary<string, string> atts)
{
canvas.Begin();
};
handlers["move"] = delegate(Dictionary<string, string> atts)
{
canvas.MoveTo(GetDouble(atts, "x"), GetDouble(atts, "y"));
};
handlers["line"] = delegate(Dictionary<string, string> atts)
{
canvas.LineTo(GetDouble(atts, "x"), GetDouble(atts, "y"));
};
handlers["quad"] = delegate(Dictionary<string, string> atts)
{
canvas.QuadTo(GetDouble(atts, "x1"), GetDouble(atts, "y1"),
GetDouble(atts, "x2"), GetDouble(atts, "y2"));
};
handlers["curve"] = delegate(Dictionary<string, string> atts)
{
canvas.CurveTo(GetDouble(atts, "x1"), GetDouble(atts, "y1"),
GetDouble(atts, "x2"), GetDouble(atts, "y2"),
GetDouble(atts, "x3"), GetDouble(atts, "y3"));
};
handlers["close"] = delegate(Dictionary<string, string> atts)
{
canvas.Close();
};
handlers["stroke"] = delegate(Dictionary<string, string> atts)
{
canvas.Stroke();
};
handlers["fill"] = delegate(Dictionary<string, string> atts)
{
canvas.Fill();
};
handlers["fillstroke"] = delegate(Dictionary<string, string> atts)
{
canvas.FillAndStroke();
};
}
}
}