SvgAttributeAttribute.cs 4.39 KB
Newer Older
davescriven's avatar
davescriven committed
1
2
using System;
using System.Collections.Generic;
3
using System.Linq;
davescriven's avatar
davescriven committed
4
5
6
7
8
using System.Text;
using System.ComponentModel;

namespace Svg
{
9
10
11
    /// <summary>
    /// Specifies the SVG attribute name of the associated property.
    /// </summary>
Tebjan Halm's avatar
Tebjan Halm committed
12
    [AttributeUsage(AttributeTargets.Property|AttributeTargets.Event)]
davescriven's avatar
davescriven committed
13
14
    public class SvgAttributeAttribute : System.Attribute
    {
15
16
17
		/// <summary>
		/// Gets a <see cref="string"/> containing the XLink namespace (http://www.w3.org/1999/xlink).
		/// </summary>
Eric Domke's avatar
Eric Domke committed
18
		public const string SvgNamespace = "http://www.w3.org/2000/svg";
19
20
		public const string XLinkPrefix = "xlink";
		public const string XLinkNamespace = "http://www.w3.org/1999/xlink";
Eric Domke's avatar
Eric Domke committed
21
        public const string XmlNamespace = "http://www.w3.org/XML/1998/namespace";
22
23
24

		public static readonly List<KeyValuePair<string, string>> Namespaces = new List<KeyValuePair<string, string>>()
    	                                                                	{
Eric Domke's avatar
Eric Domke committed
25
																				new KeyValuePair<string, string>("", SvgNamespace),
26
27
28
																				new KeyValuePair<string, string>(XLinkPrefix, XLinkNamespace),
																				new KeyValuePair<string, string>("xml", XmlNamespace)
		                                                                    };
Eric Domke's avatar
Eric Domke committed
29
        private bool _inAttrDictionary;
davescriven's avatar
davescriven committed
30
31
32
        private string _name;
        private string _namespace;

33
34
35
36
37
38
39
        /// <summary>
        /// When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.
        /// </summary>
        /// <param name="obj">An <see cref="T:System.Object"/> to compare with this instance of <see cref="T:System.Attribute"/>.</param>
        /// <returns>
        /// true if this instance equals <paramref name="obj"/>; otherwise, false.
        /// </returns>
davescriven's avatar
davescriven committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
        public override bool Match(object obj)
        {
            SvgAttributeAttribute indicator = obj as SvgAttributeAttribute;

            if (indicator == null)
                return false;

            // Always match if either value is String.Empty (wildcard)
            if (indicator.Name == String.Empty)
                return false;

            return String.Compare(indicator.Name, this.Name) == 0;
        }

54
55
56
57
58
59
60
		/// <summary>
		/// Gets the name of the SVG attribute.
		/// </summary>
		public string NamespaceAndName
		{
			get
			{
Eric Domke's avatar
Eric Domke committed
61
				if (_namespace == SvgNamespace)
62
63
64
65
66
67
					return _name;
				return Namespaces.First(x => x.Value == _namespace).Key + ":" + _name;
			}
		}


68
69
70
        /// <summary>
        /// Gets the name of the SVG attribute.
        /// </summary>
davescriven's avatar
davescriven committed
71
72
73
74
75
        public string Name
        {
            get { return this._name; }
        }

76
77
78
        /// <summary>
        /// Gets the namespace of the SVG attribute.
        /// </summary>
davescriven's avatar
davescriven committed
79
80
81
82
83
        public string NameSpace
        {
            get { return this._namespace; }
        }

Eric Domke's avatar
Eric Domke committed
84
85
86
87
88
        public bool InAttributeDictionary
        {
            get { return this._inAttrDictionary; }
        }

89
90
91
        /// <summary>
        /// Initializes a new instance of the <see cref="SvgAttributeAttribute"/> class.
        /// </summary>
davescriven's avatar
davescriven committed
92
93
94
95
96
        internal SvgAttributeAttribute()
        {
            this._name = String.Empty;
        }

97
98
99
100
        /// <summary>
        /// Initializes a new instance of the <see cref="SvgAttributeAttribute"/> class with the specified attribute name.
        /// </summary>
        /// <param name="name">The name of the SVG attribute.</param>
davescriven's avatar
davescriven committed
101
102
103
        internal SvgAttributeAttribute(string name)
        {
            this._name = name;
Eric Domke's avatar
Eric Domke committed
104
            this._namespace = SvgNamespace;
davescriven's avatar
davescriven committed
105
106
        }

Eric Domke's avatar
Eric Domke committed
107
108
109
110
111
112
113
        internal SvgAttributeAttribute(string name, bool inAttrDictionary)
        {
            this._name = name;
            this._namespace = SvgNamespace;
            this._inAttrDictionary = inAttrDictionary;
        }

114
115
116
117
118
        /// <summary>
        /// Initializes a new instance of the <see cref="SvgAttributeAttribute"/> class with the specified SVG attribute name and namespace.
        /// </summary>
        /// <param name="name">The name of the SVG attribute.</param>
        /// <param name="nameSpace">The namespace of the SVG attribute (e.g. http://www.w3.org/2000/svg).</param>
davescriven's avatar
davescriven committed
119
120
121
122
123
124
125
        public SvgAttributeAttribute(string name, string nameSpace)
        {
            this._name = name;
            this._namespace = nameSpace;
        }
    }
}