SvgTextReader.cs 5.4 KB
Newer Older
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Collections.Specialized;

namespace Svg
{
    internal sealed class SvgTextReader : XmlTextReader
    {
        private Dictionary<string, string> _entities;
        private string _value;
        private bool _customValue = false;
        private string _localName;

        public SvgTextReader(Stream stream, Dictionary<string, string> entities)
            : base(stream)
        {
            this.EntityHandling = EntityHandling.ExpandCharEntities;
            this._entities = entities;
        }

        /// <summary>
        /// Gets the text value of the current node.
        /// </summary>
        /// <value></value>
        /// <returns>The value returned depends on the <see cref="P:System.Xml.XmlTextReader.NodeType"/> of the node. The following table lists node types that have a value to return. All other node types return String.Empty.Node Type Value AttributeThe value of the attribute. CDATAThe content of the CDATA section. CommentThe content of the comment. DocumentTypeThe internal subset. ProcessingInstructionThe entire content, excluding the target. SignificantWhitespaceThe white space within an xml:space= 'preserve' scope. TextThe content of the text node. WhitespaceThe white space between markup. XmlDeclarationThe content of the declaration. </returns>
        public override string Value
        {
            get
            {
                return (this._customValue) ? this._value : base.Value;
            }
        }

        /// <summary>
        /// Gets the local name of the current node.
        /// </summary>
        /// <value></value>
        /// <returns>The name of the current node with the prefix removed. For example, LocalName is book for the element &lt;bk:book&gt;.For node types that do not have a name (like Text, Comment, and so on), this property returns String.Empty.</returns>
        public override string LocalName
        {
            get
            {
                return (this._customValue) ? this._localName : base.LocalName;
            }
        }

        private IDictionary<string, string> Entities
        {
            get
            {
                if (this._entities == null)
                {
                    this._entities = new Dictionary<string, string>();
                }

                return this._entities;
            }
        }

        /// <summary>
        /// Moves to the next attribute.
        /// </summary>
        /// <returns>
        /// true if there is a next attribute; false if there are no more attributes.
        /// </returns>
        public override bool MoveToNextAttribute()
        {
            bool moved = base.MoveToNextAttribute();

            if (moved)
            {
                this._localName = base.LocalName;

                if (this.ReadAttributeValue())
                {
                    if (this.NodeType == XmlNodeType.EntityReference)
                    {
                        this.ResolveEntity();
                    }
                    else
                    {
                        this._value = base.Value;
                    }
                }
                this._customValue = true;
            }

            return moved;
        }

        /// <summary>
        /// Reads the next node from the stream.
        /// </summary>
        /// <returns>
        /// true if the next node was read successfully; false if there are no more nodes to read.
        /// </returns>
        /// <exception cref="T:System.Xml.XmlException">An error occurred while parsing the XML. </exception>
        public override bool Read()
        {
            this._customValue = false;
            bool read = base.Read();

            if (this.NodeType == XmlNodeType.DocumentType)
            {
                this.ParseEntities();
            }

            return read;
        }

        private void ParseEntities()
        {
            const string entityText = "<!ENTITY";
            string[] entities = this.Value.Split(new string[]{entityText}, StringSplitOptions.None);
            string[] parts = null;
            string name = null;
            string value = null;

            foreach (string entity in entities)
            {
                if (string.IsNullOrEmpty(entity.Trim()))
                {
                    continue;
                }

                parts = entity.Trim().Split(new char[]{' ', '\t'},  StringSplitOptions.RemoveEmptyEntries);
                name = parts[0];
                value = parts[1].Split(new char[] { this.QuoteChar }, StringSplitOptions.RemoveEmptyEntries)[0];

                this.Entities.Add(name, value);
            }
        }

        /// <summary>
        /// Resolves the entity reference for EntityReference nodes.
        /// </summary>
        public override void ResolveEntity()
        {
            if (this.NodeType == XmlNodeType.EntityReference)
            {
                if (this._entities.ContainsKey(this.Name))
                {
                    this._value = this._entities[this.Name];
                }
                else
                {
                    this._value = string.Empty;
                }

                this._customValue = true;
            }
        }
    }
}