Skip to content

Commit fa3b868

Browse files
authored
Change from XmlTextReader to XmlReader. (#828)
* Change from XmlTextReader to XmlReader * related to #90
1 parent fd23a1c commit fa3b868

File tree

5 files changed

+44
-94
lines changed

5 files changed

+44
-94
lines changed

Source/Metadata/SvgDocumentMetadata.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public override SvgElement DeepCopy()
3535
return DeepCopy<SvgDocumentMetadata>();
3636
}
3737

38-
public override void InitialiseFromXML(XmlTextReader reader, SvgDocument document)
38+
public override void InitialiseFromXML(XmlReader reader, SvgDocument document)
3939
{
4040
base.InitialiseFromXML(reader, document);
4141

Source/SvgElement.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ void Attributes_AttributeChanged(object sender, AttributeEventArgs e)
568568
OnAttributeChanged(e);
569569
}
570570

571-
public virtual void InitialiseFromXML(XmlTextReader reader, SvgDocument document)
571+
public virtual void InitialiseFromXML(XmlReader reader, SvgDocument document)
572572
{
573573
throw new NotImplementedException();
574574
}
@@ -963,7 +963,7 @@ private bool TryResolveParentAttributeValue(string attributeKey, out object pare
963963
/// <remarks>
964964
/// Recommendation is to create an XmlWriter by calling a factory method,<br/>
965965
/// e.g. <see cref="XmlWriter.Create(System.IO.Stream)"/>,
966-
/// as per <a href="https://docs.microsoft.com/dotnet/api/system.xml.xmltextreader#remarks">Microsoft documentation</a>.<br/>
966+
/// as per <a href="https://docs.microsoft.com/dotnet/api/system.xml.xmltextwriter#remarks">Microsoft documentation</a>.<br/>
967967
/// <br/>
968968
/// However, unlike an <see cref="XmlTextWriter"/> created via 'new XmlTextWriter()',<br/>
969969
/// a factory-constructed XmlWriter will not flush output until it is closed<br/>

Source/SvgElementFactory.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ where t.GetCustomAttributes(typeof(SvgElementAttribute), true).Length > 0
6464
internal Dictionary<string, List<Type>> AvailableElementsDictionary => availableElementsDictionary;
6565

6666
/// <summary>
67-
/// Creates an <see cref="SvgDocument"/> from the current node in the specified <see cref="XmlTextReader"/>.
67+
/// Creates an <see cref="SvgDocument"/> from the current node in the specified <see cref="XmlReader"/>.
6868
/// </summary>
69-
/// <param name="reader">The <see cref="XmlTextReader"/> containing the node to parse into an <see cref="SvgDocument"/>.</param>
69+
/// <param name="reader">The <see cref="XmlReader"/> containing the node to parse into an <see cref="SvgDocument"/>.</param>
7070
/// <exception cref="ArgumentNullException">The <paramref name="reader"/> parameter cannot be <c>null</c>.</exception>
7171
/// <exception cref="InvalidOperationException">The CreateDocument method can only be used to parse root &lt;svg&gt; elements.</exception>
7272
public T CreateDocument<T>(XmlReader reader) where T : SvgDocument, new()
@@ -85,9 +85,9 @@ where t.GetCustomAttributes(typeof(SvgElementAttribute), true).Length > 0
8585
}
8686

8787
/// <summary>
88-
/// Creates an <see cref="SvgElement"/> from the current node in the specified <see cref="XmlTextReader"/>.
88+
/// Creates an <see cref="SvgElement"/> from the current node in the specified <see cref="XmlReader"/>.
8989
/// </summary>
90-
/// <param name="reader">The <see cref="XmlTextReader"/> containing the node to parse into a subclass of <see cref="SvgElement"/>.</param>
90+
/// <param name="reader">The <see cref="XmlReader"/> containing the node to parse into a subclass of <see cref="SvgElement"/>.</param>
9191
/// <param name="document">The <see cref="SvgDocument"/> that the created element belongs to.</param>
9292
/// <exception cref="ArgumentNullException">The <paramref name="reader"/> and <paramref name="document"/> parameters cannot be <c>null</c>.</exception>
9393
public SvgElement CreateElement(XmlReader reader, SvgDocument document)

Source/SvgNodeReader.cs

+9-26
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Svg
66
{
77
internal sealed class SvgNodeReader : XmlNodeReader
88
{
9-
private Dictionary<string, string> _entities;
9+
private readonly Dictionary<string, string> _entities;
1010
private string _value;
1111
private bool _customValue = false;
1212
private string _localName;
@@ -21,13 +21,10 @@ public SvgNodeReader(XmlNode node, Dictionary<string, string> entities)
2121
/// Gets the text value of the current node.
2222
/// </summary>
2323
/// <value></value>
24-
/// <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>
24+
/// <returns>The value returned depends on the <see cref="P:System.Xml.XmlNodeReader.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>
2525
public override string Value
2626
{
27-
get
28-
{
29-
return _customValue ? _value : base.Value;
30-
}
27+
get { return _customValue ? _value : base.Value; }
3128
}
3229

3330
/// <summary>
@@ -37,10 +34,7 @@ public override string Value
3734
/// <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>
3835
public override string LocalName
3936
{
40-
get
41-
{
42-
return _customValue ? _localName : base.LocalName;
43-
}
37+
get { return _customValue ? _localName : base.LocalName; }
4438
}
4539

4640
/// <summary>
@@ -60,13 +54,9 @@ public override bool MoveToNextAttribute()
6054
if (ReadAttributeValue())
6155
{
6256
if (NodeType == XmlNodeType.EntityReference)
63-
{
6457
ResolveEntity();
65-
}
6658
else
67-
{
6859
_value = base.Value;
69-
}
7060
}
7161
_customValue = true;
7262
}
@@ -87,31 +77,24 @@ public override bool Read()
8777
bool read = base.Read();
8878

8979
if (NodeType == XmlNodeType.DocumentType)
90-
{
9180
ParseEntities();
92-
}
9381

9482
return read;
9583
}
9684

9785
private void ParseEntities()
9886
{
9987
const string entityText = "<!ENTITY";
100-
string[] entities = Value.Split(new string[] { entityText }, StringSplitOptions.None);
101-
string[] parts = null;
102-
string name = null;
103-
string value = null;
88+
var entities = Value.Split(new string[] { entityText }, StringSplitOptions.None);
10489

105-
foreach (string entity in entities)
90+
foreach (var entity in entities)
10691
{
10792
if (string.IsNullOrEmpty(entity.Trim()))
108-
{
10993
continue;
110-
}
11194

112-
parts = entity.Trim().Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
113-
name = parts[0];
114-
value = parts[1].Split(new char[] { QuoteChar }, StringSplitOptions.RemoveEmptyEntries)[0];
95+
var parts = entity.Trim().Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
96+
var name = parts[0];
97+
var value = parts[1].Split(new char[] { QuoteChar }, StringSplitOptions.RemoveEmptyEntries)[0];
11598

11699
_entities.Add(name, value);
117100
}

Source/SvgTextReader.cs

+28-61
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Xml;
63
using System.IO;
7-
using System.Collections.Specialized;
4+
using System.Xml;
85

96
namespace Svg
107
{
118
internal sealed class SvgTextReader : XmlTextReader
129
{
13-
private Dictionary<string, string> _entities;
10+
private readonly Dictionary<string, string> _entities;
1411
private string _value;
1512
private bool _customValue = false;
1613
private string _localName;
@@ -19,16 +16,16 @@ public SvgTextReader(Stream stream, Dictionary<string, string> entities)
1916
: base(stream)
2017
{
2118
if (entities == null)
22-
this.EntityHandling = EntityHandling.ExpandEntities;
23-
this._entities = entities;
19+
EntityHandling = EntityHandling.ExpandEntities;
20+
_entities = entities ?? new Dictionary<string, string>();
2421
}
2522

2623
public SvgTextReader(TextReader reader, Dictionary<string, string> entities)
2724
: base(reader)
2825
{
2926
if (entities == null)
30-
this.EntityHandling = EntityHandling.ExpandEntities;
31-
this._entities = entities;
27+
EntityHandling = EntityHandling.ExpandEntities;
28+
_entities = entities ?? new Dictionary<string, string>();
3229
}
3330

3431
/// <summary>
@@ -38,10 +35,7 @@ public SvgTextReader(TextReader reader, Dictionary<string, string> entities)
3835
/// <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>
3936
public override string Value
4037
{
41-
get
42-
{
43-
return (this._customValue) ? this._value : base.Value;
44-
}
38+
get { return _customValue ? _value : base.Value; }
4539
}
4640

4741
/// <summary>
@@ -51,23 +45,7 @@ public override string Value
5145
/// <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>
5246
public override string LocalName
5347
{
54-
get
55-
{
56-
return (this._customValue) ? this._localName : base.LocalName;
57-
}
58-
}
59-
60-
private IDictionary<string, string> Entities
61-
{
62-
get
63-
{
64-
if (this._entities == null)
65-
{
66-
this._entities = new Dictionary<string, string>();
67-
}
68-
69-
return this._entities;
70-
}
48+
get { return _customValue ? _localName : base.LocalName; }
7149
}
7250

7351
/// <summary>
@@ -82,20 +60,16 @@ public override bool MoveToNextAttribute()
8260

8361
if (moved)
8462
{
85-
this._localName = base.LocalName;
63+
_localName = base.LocalName;
8664

87-
if (this.ReadAttributeValue())
65+
if (ReadAttributeValue())
8866
{
89-
if (this.NodeType == XmlNodeType.EntityReference)
90-
{
91-
this.ResolveEntity();
92-
}
67+
if (NodeType == XmlNodeType.EntityReference)
68+
ResolveEntity();
9369
else
94-
{
95-
this._value = base.Value;
96-
}
70+
_value = base.Value;
9771
}
98-
this._customValue = true;
72+
_customValue = true;
9973
}
10074

10175
return moved;
@@ -110,39 +84,32 @@ public override bool MoveToNextAttribute()
11084
/// <exception cref="T:System.Xml.XmlException">An error occurred while parsing the XML. </exception>
11185
public override bool Read()
11286
{
113-
this._customValue = false;
87+
_customValue = false;
11488
bool read = base.Read();
11589

116-
if (this.NodeType == XmlNodeType.DocumentType)
117-
{
118-
this.ParseEntities();
119-
}
90+
if (NodeType == XmlNodeType.DocumentType)
91+
ParseEntities();
12092

12193
return read;
12294
}
12395

12496
private void ParseEntities()
12597
{
12698
const string entityText = "<!ENTITY";
127-
string[] entities = this.Value.Split(new string[] { entityText }, StringSplitOptions.None);
128-
string name = null;
129-
string value = null;
130-
int quoteIndex;
99+
var entities = Value.Split(new string[] { entityText }, StringSplitOptions.None);
131100

132-
foreach (string entity in entities)
101+
foreach (var entity in entities)
133102
{
134103
if (string.IsNullOrEmpty(entity.Trim()))
135-
{
136104
continue;
137-
}
138105

139-
name = entity.Trim();
140-
quoteIndex = name.IndexOf(this.QuoteChar);
106+
var name = entity.Trim();
107+
var quoteIndex = name.IndexOf(QuoteChar);
141108
if (quoteIndex > 0)
142109
{
143-
value = name.Substring(quoteIndex + 1, name.LastIndexOf(this.QuoteChar) - quoteIndex - 1);
110+
var value = name.Substring(quoteIndex + 1, name.LastIndexOf(QuoteChar) - quoteIndex - 1);
144111
name = name.Substring(0, quoteIndex).Trim();
145-
this.Entities.Add(name, value);
112+
_entities.Add(name, value);
146113
}
147114
}
148115
}
@@ -152,18 +119,18 @@ private void ParseEntities()
152119
/// </summary>
153120
public override void ResolveEntity()
154121
{
155-
if (this.NodeType == XmlNodeType.EntityReference)
122+
if (NodeType == XmlNodeType.EntityReference)
156123
{
157-
if (this._entities.ContainsKey(this.Name))
124+
if (_entities.ContainsKey(Name))
158125
{
159-
this._value = this._entities[this.Name];
126+
_value = _entities[Name];
160127
}
161128
else
162129
{
163-
this._value = string.Empty;
130+
_value = string.Empty;
164131
}
165132

166-
this._customValue = true;
133+
_customValue = true;
167134
}
168135
}
169136
}

0 commit comments

Comments
 (0)