forked from cursedtoxi/WebControls
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSvgReader.cs
More file actions
112 lines (97 loc) · 3.46 KB
/
SvgReader.cs
File metadata and controls
112 lines (97 loc) · 3.46 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
// 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;
using System.Xml;
namespace Aurigma.Svg
{
public class SvgReader
{
private ITypeResolver _typeResolver;
private IAttributeReader _customAttributeReader;
public SvgReader()
: this(new TypeResolver(), null)
{
}
public SvgReader(IAttributeReader attributeReader)
: this(new TypeResolver(), attributeReader)
{
}
public SvgReader(ITypeResolver typeResolver)
: this(typeResolver, null)
{
}
public SvgReader(ITypeResolver typeResolver, IAttributeReader attributeReader)
{
_typeResolver = typeResolver;
_customAttributeReader = attributeReader;
}
public virtual void Read(SvgNode svgNode, XmlElement xmlElement)
{
var composite = svgNode as ISvgCompositeElement;
if (composite != null)
{
composite.ReadContent(xmlElement, this);
}
else
{
ReadChildNodes(svgNode, xmlElement);
}
ReadAttributes(svgNode, xmlElement);
}
protected virtual void ReadChildNodes(SvgNode svgNode, XmlElement xmlElement)
{
foreach (XmlNode childNode in xmlElement.ChildNodes)
{
var childElement = childNode as XmlElement;
if (childElement != null)
{
var childSvgNode = CreateSvgNodeFromXml(childElement);
if (childSvgNode != null)
{
Read(childSvgNode, childElement);
svgNode.ChildNodes.Add(childSvgNode);
}
}
}
}
public virtual SvgNode CreateSvgNodeFromXml(XmlElement xmlElement)
{
SvgNode svgNode = null;
var ns = xmlElement.NamespaceURI;
var nodeName = xmlElement.LocalName;
var typeAttr = xmlElement.GetAttribute("type", XmlNamespace.AurigmaSvg);
Type type = _typeResolver.ResolveType(ns, nodeName, typeAttr);
if (type != null)
{
svgNode = (SvgNode)Activator.CreateInstance(type);
}
return svgNode;
}
protected virtual void ReadAttributes(SvgNode svgNode, XmlElement xmlElement)
{
var attributes = svgNode.GetAttributes();
if (attributes != null)
{
foreach (var svgAttribute in attributes)
{
var xmlAttr = xmlElement.GetAttributeNode(svgAttribute.LocalName, svgAttribute.NamespaceUri);
if (xmlAttr == null &&
(svgAttribute.NamespaceUri == xmlElement.NamespaceURI ||
svgAttribute.NamespaceUri == XmlNamespace.Svg))
{
xmlAttr = xmlElement.GetAttributeNode(svgAttribute.LocalName);
}
if (xmlAttr != null)
{
svgAttribute.SetValue(xmlAttr.Value);
}
}
}
if (_customAttributeReader != null)
{
_customAttributeReader.Read(svgNode, xmlElement);
}
}
}
}