SvgElementFactory.cs 5.25 KB
Newer Older
davescriven's avatar
davescriven committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Collections;
using System.Text;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Xml;
using System.ComponentModel;
using System.Reflection;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Diagnostics;
using System.Threading;
15
using System.Linq;
davescriven's avatar
davescriven committed
16
17
18
19
20
21
22

using Svg.Transforms;

namespace Svg
{
    internal class SvgElementFactory
    {
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
        private static List<ElementInfo> availableElements;

        private static List<ElementInfo> AvailableElements
        {
            get
            {
                if (availableElements == null)
                {
                    var svgTypes = from t in typeof(SvgDocument).Assembly.GetExportedTypes()
                                   where t.GetCustomAttributes(typeof(SvgElementAttribute), true).Length > 0
                                   select new ElementInfo { ElementName = ((SvgElementAttribute)t.GetCustomAttributes(typeof(SvgElementAttribute), true)[0]).ElementName, ElementType = t };

                    availableElements = svgTypes.ToList();
                }

                return availableElements;
            }
        }

davescriven's avatar
davescriven committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
        public static SvgDocument CreateDocument(XmlTextReader reader)
        {
            return (SvgDocument)CreateElement(reader, true, null);
        }

        public static SvgElement CreateElement(XmlTextReader reader, SvgDocument document)
        {
            return CreateElement(reader, false, document);
        }

        private static SvgElement CreateElement(XmlTextReader reader, bool fragmentIsDocument, SvgDocument document)
        {
            SvgElement createdElement = null;
            SvgFragment fragment;
            string elementName = reader.LocalName;

58
59
            Trace.TraceInformation("Begin CreateElement: {0}", elementName);

60
            if (elementName == "svg")
davescriven's avatar
davescriven committed
61
            {
62
63
64
65
66
                createdElement = (fragmentIsDocument) ? new SvgDocument() : new SvgFragment();
            }
            else
            {
                var validTypes = AvailableElements.Where(e => e.ElementName == elementName);
davescriven's avatar
davescriven committed
67

68
69
70
71
                if (validTypes.Count() > 0)
                {
                    createdElement = Activator.CreateInstance(validTypes.First().ElementType) as SvgElement;
                }
davescriven's avatar
davescriven committed
72
73
            }

74
75
76
77
78
79
80
            if (createdElement != null)
            {
                createdElement.ElementName = elementName;
                SetAttributes(createdElement, reader, document);
            }

            Trace.TraceInformation("End CreateElement");
davescriven's avatar
davescriven committed
81
82
83
84
85
86

            return createdElement;
        }

        private static void SetAttributes(SvgElement element, XmlTextReader reader, SvgDocument document)
        {
87
88
            Trace.TraceInformation("Begin SetAttributes");

davescriven's avatar
davescriven committed
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
            string[] styles = null;
            string[] style = null;
            int i = 0;

            while (reader.MoveToNextAttribute())
            {
                // Special treatment for "style"
                if (reader.LocalName.Equals("style"))
                {
                    styles = reader.Value.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

                    for (i = 0; i < styles.Length; i++)
                    {
                        if (!styles[i].Contains(":"))
                        {
                            continue;
                        }

                        style = styles[i].Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                        SetPropertyValue(element, style[0].Trim(), style[1].Trim(), document);
                    }

                    continue;
                }

                SetPropertyValue(element, reader.LocalName, reader.Value, document);
            }
116
117

            Trace.TraceInformation("End SetAttributes");
davescriven's avatar
davescriven committed
118
119
120
121
        }

        private static void SetPropertyValue(SvgElement element, string attributeName, string attributeValue, SvgDocument document)
        {
122
            var properties = TypeDescriptor.GetProperties(element.GetType(), new SvgAttributeAttribute[] { new SvgAttributeAttribute(attributeName) });
davescriven's avatar
davescriven committed
123
124
125
126
127
128
129
130
            PropertyDescriptor descriptor = null;

            if (properties.Count > 0)
            {
                descriptor = properties[0];

                try
                {
131
                    descriptor.SetValue(element, descriptor.Converter.ConvertFrom(document, CultureInfo.InvariantCulture, attributeValue));
davescriven's avatar
davescriven committed
132
133
134
135
136
137
138
                }
                catch
                {
                    Trace.TraceWarning(string.Format("Attribute '{0}' cannot be set - type '{1}' cannot convert from string '{2}'.", attributeName, descriptor.PropertyType.FullName, attributeValue));
                }
            }
        }
139
140
141
142
143
144
145
146
147
148
149
150
151

        private struct ElementInfo
        {
            public string ElementName { get; set; }
            public Type ElementType { get; set; }

            public ElementInfo(string elementName, Type elementType)
                : this()
            {
                this.ElementName = elementName;
                this.ElementType = elementType;
            }
        }
davescriven's avatar
davescriven committed
152
153
    }
}