forked from cursedtoxi/WebControls
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSvgWriter.cs
More file actions
114 lines (103 loc) · 4.05 KB
/
SvgWriter.cs
File metadata and controls
114 lines (103 loc) · 4.05 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
// Copyright (c) 2018 Aurigma Inc. All rights reserved.
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
//
using System.Xml;
namespace Aurigma.Svg
{
public class SvgWriter
{
private ITypeResolver _typeResolver;
private XmlDocument _xmlDocument;
public SvgWriter(XmlDocument xmlDocument)
: this(xmlDocument, new TypeResolver())
{ }
public SvgWriter(XmlDocument xmlDocument, ITypeResolver typeResolver)
{
_xmlDocument = xmlDocument;
_typeResolver = typeResolver;
}
public virtual void Write(SvgNode svgNode, XmlElement xmlElement)
{
WriteAttributes(svgNode, xmlElement);
var composite = svgNode as ISvgCompositeElement;
if (composite != null)
{
composite.WriteContent(xmlElement, this);
}
else
{
WriteChildNodes(svgNode, xmlElement);
}
}
protected virtual void WriteChildNodes(SvgNode svgNode, XmlElement xmlElement)
{
var childNodes = svgNode.ChildNodes;
if (childNodes != null)
{
foreach (var childNode in svgNode.ChildNodes)
{
var childElement = CreateXmlElementFromSvg(childNode);
if (childElement != null)
{
xmlElement.AppendChild(childElement);
Write(childNode, childElement);
}
}
}
}
protected virtual void WriteAttributes(SvgNode svgNode, XmlElement xmlElement)
{
var topParentNode = xmlElement;
while (topParentNode.ParentNode as XmlElement != null)
{
topParentNode = topParentNode.ParentNode as XmlElement;
}
var prefix = xmlElement.GetPrefixOfNamespace(XmlNamespace.AurigmaSvg);
if (string.IsNullOrEmpty(prefix))
{
topParentNode.SetAttribute("xmlns:aursvg", XmlNamespace.AurigmaSvg);
}
var type = _typeResolver.ResolveTypeAttribute(svgNode.GetType());
if (!string.IsNullOrEmpty(type))
{
xmlElement.SetAttributeNode("type", XmlNamespace.AurigmaSvg).Value = type;
}
var attributes = svgNode.GetAttributes();
if (attributes != null)
{
foreach (var attr in attributes)
{
var value = attr.GetValue();
if (attr.DefaultValue != value && !string.IsNullOrEmpty(value))
{
XmlAttribute attrNode;
if (attr.NamespaceUri == XmlNamespace.Svg)
{
attrNode = _xmlDocument.CreateAttribute(attr.LocalName);
}
else
{
prefix = xmlElement.GetPrefixOfNamespace(attr.NamespaceUri);
if (string.IsNullOrEmpty(prefix))
{
prefix = _typeResolver.GetPrefix(attr.NamespaceUri);
if (!string.IsNullOrEmpty(prefix))
{
topParentNode.SetAttribute("xmlns:" + prefix, attr.NamespaceUri);
}
}
attrNode = _xmlDocument.CreateAttribute(attr.LocalName, attr.NamespaceUri);
}
attrNode.Value = value;
xmlElement.Attributes.Append(attrNode);
}
}
}
}
public virtual XmlElement CreateXmlElementFromSvg(SvgNode svgNode)
{
var element = _xmlDocument.CreateElement(svgNode.LocalName, svgNode.NamespaceURI);
return element;
}
}
}