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 SvgNodeReader : XmlNodeReader { private Dictionary _entities; private string _value; private bool _customValue = false; private string _localName; public SvgNodeReader(XmlNode node, Dictionary entities) : base(node) { this._entities = entities; } /// /// Gets the text value of the current node. /// /// /// The value returned depends on the 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. public override string Value { get { return (this._customValue) ? this._value : base.Value; } } /// /// Gets the local name of the current node. /// /// /// The name of the current node with the prefix removed. For example, LocalName is book for the element <bk:book>.For node types that do not have a name (like Text, Comment, and so on), this property returns String.Empty. public override string LocalName { get { return (this._customValue) ? this._localName : base.LocalName; } } private IDictionary Entities { get { if (this._entities == null) { this._entities = new Dictionary(); } return this._entities; } } /// /// Moves to the next attribute. /// /// /// true if there is a next attribute; false if there are no more attributes. /// 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; } /// /// Reads the next node from the stream. /// /// /// true if the next node was read successfully; false if there are no more nodes to read. /// /// An error occurred while parsing the XML. 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 = " /// Resolves the entity reference for EntityReference nodes. /// 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; } } } }