Skip to content

Commit 811cbe3

Browse files
#1: Read the hyperlink element and added a simple nvg point element class.
1 parent 116118e commit 811cbe3

File tree

8 files changed

+251
-2
lines changed

8 files changed

+251
-2
lines changed

NVGViewer/NVG.Data/NVG.Data.csproj

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
</ItemGroup>
4242
<ItemGroup>
4343
<Compile Include="NvgElement.cs" />
44+
<Compile Include="NvgHyperlinkElement.cs" />
45+
<Compile Include="NvgPointElement.cs" />
4446
<Compile Include="Properties\AssemblyInfo.cs" />
4547
</ItemGroup>
4648
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

NVGViewer/NVG.Data/NvgElement.cs

+22
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,31 @@
1414
* limitations under the License.
1515
*/
1616

17+
using System.Collections.Generic;
18+
1719
namespace NVG.Data
1820
{
21+
/// <summary>
22+
/// Represents a NVG element.
23+
/// </summary>
1924
public class NvgElement
2025
{
26+
/// <summary>
27+
/// Creates a new NVG element instance.
28+
/// </summary>
29+
public NvgElement()
30+
{
31+
HyperlinkElements = new List<NvgHyperlinkElement>();
32+
}
33+
34+
/// <summary>
35+
/// The version of this NVG element.
36+
/// </summary>
37+
public string Version { get; set; }
38+
39+
/// <summary>
40+
/// The hyperlink elements of this NVG element.
41+
/// </summary>
42+
public ICollection<NvgHyperlinkElement> HyperlinkElements { get; private set; }
2143
}
2244
}
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2016 Jan Tschada
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
using System.Collections.Generic;
18+
19+
namespace NVG.Data
20+
{
21+
/// <summary>
22+
/// Represents a NVG hyperlink element.
23+
/// </summary>
24+
public class NvgHyperlinkElement
25+
{
26+
public NvgHyperlinkElement()
27+
{
28+
PointElements = new List<NvgPointElement>();
29+
}
30+
31+
/// <summary>
32+
/// The link this element refers to.
33+
/// </summary>
34+
public string Url { get; set; }
35+
36+
/// <summary>
37+
/// The point elements of this hyperlink element.
38+
/// </summary>
39+
public ICollection<NvgPointElement> PointElements { get; private set; }
40+
}
41+
}

NVGViewer/NVG.Data/NvgPointElement.cs

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2016 Jan Tschada
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
namespace NVG.Data
18+
{
19+
/// <summary>
20+
/// Represents a NVG point element.
21+
/// </summary>
22+
public class NvgPointElement
23+
{
24+
public string Id { get; set; }
25+
26+
public string Label { get; set; }
27+
28+
public double X { get; set; }
29+
30+
public double Y { get; set; }
31+
32+
public string SymbolCode { get; set; }
33+
}
34+
}

NVGViewer/NVG.IO.Testing/NVG.IO.Testing.csproj

+13-1
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,22 @@
5454
<Compile Include="Properties\AssemblyInfo.cs" />
5555
</ItemGroup>
5656
<ItemGroup>
57-
<None Include="Data\AIS.nvg" />
57+
<None Include="Data\AIS.nvg">
58+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
59+
</None>
5860
<None Include="Data\Demo-Link16-recording.nvg" />
5961
<None Include="Data\Demo-NFFI-recording.nvg" />
6062
</ItemGroup>
63+
<ItemGroup>
64+
<ProjectReference Include="..\NVG.Data\NVG.Data.csproj">
65+
<Project>{c928e8ed-e179-45d6-93ee-938690c41309}</Project>
66+
<Name>NVG.Data</Name>
67+
</ProjectReference>
68+
<ProjectReference Include="..\NVG.IO\NVG.IO.csproj">
69+
<Project>{a3bd70bd-f73a-4f63-a980-1c9fea534d1e}</Project>
70+
<Name>NVG.IO</Name>
71+
</ProjectReference>
72+
</ItemGroup>
6173
<Choose>
6274
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
6375
<ItemGroup>

NVGViewer/NVG.IO.Testing/NvgReaderTestSuite.cs

+9
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
using Microsoft.VisualStudio.TestTools.UnitTesting;
18+
using System.IO;
1819

1920
namespace NVG.IO.Testing
2021
{
@@ -24,6 +25,14 @@ public class NvgReaderTestSuite
2425
[TestMethod]
2526
public void TestNvgReadAis()
2627
{
28+
var aisFile = @"Data\AIS.nvg";
29+
Assert.IsTrue(File.Exists(aisFile), @"The NVG file does not exists!");
30+
31+
var reader = new NvgReader(aisFile);
32+
var element = reader.ReadNextElement();
33+
Assert.IsNotNull(element, @"The NVG element must not be null!");
34+
35+
Assert.AreEqual(@"0.3", element.Version, @"The NVG element version is not equal to 0.3!");
2736
}
2837
}
2938
}

NVGViewer/NVG.IO/NVG.IO.csproj

+6
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@
4343
<Compile Include="NvgReader.cs" />
4444
<Compile Include="Properties\AssemblyInfo.cs" />
4545
</ItemGroup>
46+
<ItemGroup>
47+
<ProjectReference Include="..\NVG.Data\NVG.Data.csproj">
48+
<Project>{c928e8ed-e179-45d6-93ee-938690c41309}</Project>
49+
<Name>NVG.Data</Name>
50+
</ProjectReference>
51+
</ItemGroup>
4652
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
4753
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
4854
Other similar extension points exist, see Microsoft.Common.targets.

NVGViewer/NVG.IO/NvgReader.cs

+124-1
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,132 @@
1414
* limitations under the License.
1515
*/
1616

17+
using NVG.Data;
18+
using System;
19+
using System.Xml;
20+
1721
namespace NVG.IO
1822
{
19-
public class NvgReader
23+
/// <summary>
24+
/// Represents a NVG reader for XML documents.
25+
/// </summary>
26+
public class NvgReader : IDisposable
2027
{
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+
}
21144
}
22145
}

0 commit comments

Comments
 (0)