|
14 | 14 | * limitations under the License.
|
15 | 15 | */
|
16 | 16 |
|
| 17 | +using NVG.Data; |
| 18 | +using System; |
| 19 | +using System.Xml; |
| 20 | + |
17 | 21 | namespace NVG.IO
|
18 | 22 | {
|
19 |
| - public class NvgReader |
| 23 | + /// <summary> |
| 24 | + /// Represents a NVG reader for XML documents. |
| 25 | + /// </summary> |
| 26 | + public class NvgReader : IDisposable |
20 | 27 | {
|
| 28 | + private readonly XmlTextReader _xmlTextReader; |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Creates a new reader instance using the specified file. |
| 32 | + /// </summary> |
| 33 | + /// <param name="filePath">The path to the NVG file.</param> |
| 34 | + public NvgReader(string filePath) |
| 35 | + { |
| 36 | + _xmlTextReader = new XmlTextReader(filePath); |
| 37 | + } |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Reads the next NVG element from the stream. |
| 41 | + /// </summary> |
| 42 | + /// <returns>The next NVG element or <code>null</code> when there are no more NVG elements.</returns> |
| 43 | + public NvgElement ReadNextElement() |
| 44 | + { |
| 45 | + while(_xmlTextReader.Read()) |
| 46 | + { |
| 47 | + switch (_xmlTextReader.NodeType) |
| 48 | + { |
| 49 | + case XmlNodeType.Element: |
| 50 | + if (0 == string.CompareOrdinal(@"nvg", _xmlTextReader.Name.ToLowerInvariant())) |
| 51 | + { |
| 52 | + return ReadNvgElement(); |
| 53 | + } |
| 54 | + break; |
| 55 | + } |
| 56 | + } |
| 57 | + return null; |
| 58 | + } |
| 59 | + |
| 60 | + private NvgElement ReadNvgElement() |
| 61 | + { |
| 62 | + NvgElement element = new NvgElement(); |
| 63 | + |
| 64 | + // Read the NVG attributes |
| 65 | + while (_xmlTextReader.MoveToNextAttribute()) |
| 66 | + { |
| 67 | + if (0 == string.CompareOrdinal(@"version", _xmlTextReader.Name)) |
| 68 | + { |
| 69 | + element.Version = _xmlTextReader.Value; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + while (_xmlTextReader.Read()) |
| 74 | + { |
| 75 | + switch (_xmlTextReader.NodeType) |
| 76 | + { |
| 77 | + case XmlNodeType.Element: |
| 78 | + if (0 == string.CompareOrdinal(@"a", _xmlTextReader.Name.ToLowerInvariant())) |
| 79 | + { |
| 80 | + var hyperlinkElement = ReadHyperlinkElement(); |
| 81 | + element.HyperlinkElements.Add(hyperlinkElement); |
| 82 | + } |
| 83 | + break; |
| 84 | + |
| 85 | + case XmlNodeType.EndElement: |
| 86 | + if (0 == string.CompareOrdinal(@"nvg", _xmlTextReader.Name.ToLowerInvariant())) |
| 87 | + { |
| 88 | + return element; |
| 89 | + } |
| 90 | + break; |
| 91 | + |
| 92 | + case XmlNodeType.Attribute: |
| 93 | + break; |
| 94 | + } |
| 95 | + } |
| 96 | + return null; |
| 97 | + } |
| 98 | + |
| 99 | + private NvgHyperlinkElement ReadHyperlinkElement() |
| 100 | + { |
| 101 | + NvgHyperlinkElement hyperlinkElement = new NvgHyperlinkElement(); |
| 102 | + |
| 103 | + // Read the hyperlink attributes |
| 104 | + while (_xmlTextReader.MoveToNextAttribute()) |
| 105 | + { |
| 106 | + if (0 == string.CompareOrdinal(@"href", _xmlTextReader.Name)) |
| 107 | + { |
| 108 | + hyperlinkElement.Url = _xmlTextReader.Value; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + while (_xmlTextReader.Read()) |
| 113 | + { |
| 114 | + switch (_xmlTextReader.NodeType) |
| 115 | + { |
| 116 | + case XmlNodeType.Element: |
| 117 | + if (0 == string.CompareOrdinal(@"point", _xmlTextReader.Name.ToLowerInvariant())) |
| 118 | + { |
| 119 | + // TODO: Read the next point element |
| 120 | + } |
| 121 | + break; |
| 122 | + |
| 123 | + case XmlNodeType.EndElement: |
| 124 | + if (0 == string.CompareOrdinal(@"a", _xmlTextReader.Name.ToLowerInvariant())) |
| 125 | + { |
| 126 | + return hyperlinkElement; |
| 127 | + } |
| 128 | + break; |
| 129 | + |
| 130 | + case XmlNodeType.Attribute: |
| 131 | + break; |
| 132 | + } |
| 133 | + } |
| 134 | + return null; |
| 135 | + } |
| 136 | + |
| 137 | + /// <summary> |
| 138 | + /// Disposes this reader instance. |
| 139 | + /// </summary> |
| 140 | + public void Dispose() |
| 141 | + { |
| 142 | + _xmlTextReader.Dispose(); |
| 143 | + } |
21 | 144 | }
|
22 | 145 | }
|
0 commit comments