SvgElementFactory.cs 9.04 KB
Newer Older
davescriven's avatar
davescriven committed
1
2
3
4
5
6
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Xml;
using System.ComponentModel;
using System.Diagnostics;
7
using System.Linq;
davescriven's avatar
davescriven committed
8
9
10

namespace Svg
{
11
12
13
    /// <summary>
    /// Provides the methods required in order to parse and create <see cref="SvgElement"/> instances from XML.
    /// </summary>
davescriven's avatar
davescriven committed
14
15
    internal class SvgElementFactory
    {
16
17
        private static List<ElementInfo> availableElements;

18
19
20
        /// <summary>
        /// Gets a list of available types that can be used when creating an <see cref="SvgElement"/>.
        /// </summary>
21
22
23
24
25
26
27
28
        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
29
                                   && t.IsSubclassOf(typeof(SvgElement))
30
31
32
33
34
35
36
37
38
                                   select new ElementInfo { ElementName = ((SvgElementAttribute)t.GetCustomAttributes(typeof(SvgElementAttribute), true)[0]).ElementName, ElementType = t };

                    availableElements = svgTypes.ToList();
                }

                return availableElements;
            }
        }

39
40
41
42
43
44
        /// <summary>
        /// Creates an <see cref="SvgDocument"/> from the current node in the specified <see cref="XmlTextReader"/>.
        /// </summary>
        /// <param name="reader">The <see cref="XmlTextReader"/> containing the node to parse into an <see cref="SvgDocument"/>.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="reader"/> parameter cannot be <c>null</c>.</exception>
        /// <exception cref="InvalidOperationException">The CreateDocument method can only be used to parse root &lt;svg&gt; elements.</exception>
45
        public static T CreateDocument<T>(XmlTextReader reader) where T : SvgDocument, new()
davescriven's avatar
davescriven committed
46
        {
47
48
49
50
51
52
53
54
55
56
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            if (reader.LocalName != "svg")
            {
                throw new InvalidOperationException("The CreateDocument method can only be used to parse root <svg> elements.");
            }

57
            return (T)CreateElement<T>(reader, true, null);
davescriven's avatar
davescriven committed
58
59
        }

60
61
62
63
64
65
        /// <summary>
        /// Creates an <see cref="SvgElement"/> from the current node in the specified <see cref="XmlTextReader"/>.
        /// </summary>
        /// <param name="reader">The <see cref="XmlTextReader"/> containing the node to parse into a subclass of <see cref="SvgElement"/>.</param>
        /// <param name="document">The <see cref="SvgDocument"/> that the created element belongs to.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="reader"/> and <paramref name="document"/> parameters cannot be <c>null</c>.</exception>
davescriven's avatar
davescriven committed
66
67
        public static SvgElement CreateElement(XmlTextReader reader, SvgDocument document)
        {
68
69
70
71
72
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

73
            return CreateElement<SvgDocument>(reader, false, document);
davescriven's avatar
davescriven committed
74
75
        }

76
        private static SvgElement CreateElement<T>(XmlTextReader reader, bool fragmentIsDocument, SvgDocument document)  where T : SvgDocument, new()
davescriven's avatar
davescriven committed
77
78
79
80
        {
            SvgElement createdElement = null;
            string elementName = reader.LocalName;

81
            //Trace.TraceInformation("Begin CreateElement: {0}", elementName);
82

83
            if (elementName == "svg")
davescriven's avatar
davescriven committed
84
            {
85
                createdElement = (fragmentIsDocument) ? new T() : new SvgFragment();
86
87
88
            }
            else
            {
89
90
                ElementInfo validType = AvailableElements.SingleOrDefault(e => e.ElementName == elementName);
                if (validType != null)
91
                {
92
                    createdElement = (SvgElement)Activator.CreateInstance(validType.ElementType);
93
                }
davescriven's avatar
davescriven committed
94
95
            }

96
97
            if (createdElement != null)
            {
98
				SetAttributes(createdElement, reader, document);
99
100
            }

101
            //Trace.TraceInformation("End CreateElement");
davescriven's avatar
davescriven committed
102
103
104
105
106
107

            return createdElement;
        }

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

davescriven's avatar
davescriven committed
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
            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);
                    }

132
                    continue; 
davescriven's avatar
davescriven committed
133
134
135
136
                }

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

138
            //Trace.TraceInformation("End SetAttributes");
davescriven's avatar
davescriven committed
139
140
        }

ddpruitt's avatar
ddpruitt committed
141
142
        private static Dictionary<Type, Dictionary<string, PropertyDescriptorCollection>> _propertyDescriptors = new Dictionary<Type, Dictionary<string, PropertyDescriptorCollection>>();

davescriven's avatar
davescriven committed
143
144
        private static void SetPropertyValue(SvgElement element, string attributeName, string attributeValue, SvgDocument document)
        {
ddpruitt's avatar
ddpruitt committed
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
            var elementType = element.GetType();

            PropertyDescriptorCollection properties;
            if (_propertyDescriptors.Keys.Contains(elementType))
            {
                if (_propertyDescriptors[elementType].Keys.Contains(attributeName))
                {
                    properties = _propertyDescriptors[elementType][attributeName];
                }
                else
                {
                    properties = TypeDescriptor.GetProperties(elementType, new[] { new SvgAttributeAttribute(attributeName) });
                    _propertyDescriptors[elementType].Add(attributeName, properties);
                }
            }
            else
            {
162
                properties = TypeDescriptor.GetProperties(elementType, new[] { new SvgAttributeAttribute(attributeName) });
ddpruitt's avatar
ddpruitt committed
163
164
165
166
                _propertyDescriptors.Add(elementType, new Dictionary<string, PropertyDescriptorCollection>());

                _propertyDescriptors[elementType].Add(attributeName, properties);
            }
davescriven's avatar
davescriven committed
167
168
169

            if (properties.Count > 0)
            {
170
                PropertyDescriptor descriptor = properties[0];
davescriven's avatar
davescriven committed
171
172
173

                try
                {
174
                    descriptor.SetValue(element, descriptor.Converter.ConvertFrom(document, CultureInfo.InvariantCulture, attributeValue));
davescriven's avatar
davescriven committed
175
176
177
178
179
180
                }
                catch
                {
                    Trace.TraceWarning(string.Format("Attribute '{0}' cannot be set - type '{1}' cannot convert from string '{2}'.", attributeName, descriptor.PropertyType.FullName, attributeValue));
                }
            }
181
182
183
184
185
            else
            {
            	//attribute is not a svg attribute, store it in custom attributes
            	element.CustomAttributes[attributeName] = attributeValue;
            }
davescriven's avatar
davescriven committed
186
        }
187

188
189
190
        /// <summary>
        /// Contains information about a type inheriting from <see cref="SvgElement"/>.
        /// </summary>
191
192
        [DebuggerDisplay("{ElementName}, {ElementType}")]
        internal sealed class ElementInfo
193
        {
194
195
196
            /// <summary>
            /// Gets the SVG name of the <see cref="SvgElement"/>.
            /// </summary>
197
            public string ElementName { get; set; }
198
199
200
            /// <summary>
            /// Gets the <see cref="Type"/> of the <see cref="SvgElement"/> subclass.
            /// </summary>
201
202
            public Type ElementType { get; set; }

203
204
205
206
207
            /// <summary>
            /// Initializes a new instance of the <see cref="ElementInfo"/> struct.
            /// </summary>
            /// <param name="elementName">Name of the element.</param>
            /// <param name="elementType">Type of the element.</param>
208
209
210
211
212
            public ElementInfo(string elementName, Type elementType)
            {
                this.ElementName = elementName;
                this.ElementType = elementType;
            }
213
214
215
216
217
218
219

            /// <summary>
            /// Initializes a new instance of the <see cref="ElementInfo"/> class.
            /// </summary>
            public ElementInfo()
            {
            }
220
        }
davescriven's avatar
davescriven committed
221
222
    }
}