Skip to content

Commit fa7ccd7

Browse files
committed
Rely on embedded GraphML xsd to avoid serialization issue when graphml website is down.
1 parent 5d64909 commit fa7ccd7

9 files changed

Lines changed: 556 additions & 35 deletions

src/QuikGraph.Serialization/GraphMLExtensions.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,15 +237,20 @@ public static void DeserializeAndValidateFromGraphML<TVertex, TEdge, TGraph>(
237237

238238
var serializer = new GraphMLDeserializer<TVertex, TEdge, TGraph>();
239239

240+
var resolver = new GraphMLXmlResolver();
240241
var settings = new XmlReaderSettings
241242
{
242243
ValidationType = ValidationType.Schema,
243-
XmlResolver = new GraphMLXmlResolver(),
244+
XmlResolver = resolver,
244245
DtdProcessing = DtdProcessing.Ignore
245246
};
246247

248+
settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema;
249+
settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation;
250+
settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
251+
247252
// Add GraphML schema
248-
AddGraphMLSchema(settings);
253+
AddGraphMLSchema(settings, resolver);
249254

250255
try
251256
{
@@ -261,12 +266,13 @@ public static void DeserializeAndValidateFromGraphML<TVertex, TEdge, TGraph>(
261266
}
262267
}
263268

264-
private static void AddGraphMLSchema([NotNull] XmlReaderSettings settings)
269+
private static void AddGraphMLSchema([NotNull] XmlReaderSettings settings, [NotNull] XmlResolver resolver)
265270
{
266271
using (Stream xsdStream = typeof(GraphMLExtensions).Assembly.GetManifestResourceStream(typeof(GraphMLExtensions), "graphml.xsd"))
267272
{
268273
Debug.Assert(xsdStream != null, "GraphML schema resource not found.");
269274

275+
settings.Schemas.XmlResolver = resolver;
270276
using (XmlReader xsdReader = XmlReader.Create(xsdStream, settings))
271277
settings.Schemas.Add(GraphMLXmlResolver.GraphMLNamespace, xsdReader);
272278
}

src/QuikGraph.Serialization/GraphMLXmlResolver.cs

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#if SUPPORTS_GRAPHS_SERIALIZATION
22
using System;
3+
using System.Diagnostics;
4+
using System.IO;
35
using System.Net;
46
using System.Xml;
57
using JetBrains.Annotations;
@@ -51,13 +53,40 @@ public override ICredentials Credentials
5153
/// <inheritdoc />
5254
public override object GetEntity(Uri absoluteUri, string role, Type ofObjectToReturn)
5355
{
54-
if (absoluteUri.AbsoluteUri == "http://www.graphdrawing.org/dtds/graphml.dtd")
55-
return typeof(GraphMLExtensions).Assembly.GetManifestResourceStream(typeof(GraphMLExtensions), "graphml.dtd");
56-
if (absoluteUri.AbsoluteUri == "http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd")
57-
return typeof(GraphMLExtensions).Assembly.GetManifestResourceStream(typeof(GraphMLExtensions), "graphml.xsd");
58-
if (absoluteUri.AbsoluteUri == "http://graphml.graphdrawing.org/xmlns/1.0/graphml-structure.xsd")
59-
return typeof(GraphExtensions).Assembly.GetManifestResourceStream(typeof(GraphMLExtensions), "graphml-structure.xsd");
56+
if (absoluteUri.AbsoluteUri.EndsWith("graphml.dtd"))
57+
return GetResource("graphml.dtd");
58+
59+
if (absoluteUri.AbsoluteUri.EndsWith("graphml.xsd"))
60+
return GetResource("graphml.xsd");
61+
62+
if (absoluteUri.AbsoluteUri.EndsWith("graphml-structure.xsd"))
63+
return GetResource("graphml-structure.xsd");
64+
65+
if (absoluteUri.AbsoluteUri.EndsWith("graphml-attributes.xsd"))
66+
return GetResource("graphml-attributes.xsd");
67+
68+
if (absoluteUri.AbsoluteUri.EndsWith("graphml-parseinfo.xsd"))
69+
return GetResource("graphml-parseinfo.xsd");
70+
71+
if (absoluteUri.AbsoluteUri.EndsWith("xlink.xsd"))
72+
return GetResource("xlink.xsd");
73+
6074
return _baseResolver.GetEntity(absoluteUri, role, ofObjectToReturn);
75+
76+
#region Local function
77+
78+
Stream GetResource(string resourceName)
79+
{
80+
Stream resourceStream = typeof(GraphMLExtensions).Assembly
81+
.GetManifestResourceStream(
82+
typeof(GraphMLExtensions),
83+
resourceName);
84+
85+
Debug.Assert(resourceStream != null);
86+
return resourceStream;
87+
}
88+
89+
#endregion
6190
}
6291
}
6392
}

src/QuikGraph.Serialization/QuikGraph.Serialization.csproj

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,13 @@ New:
3434
<PackageTags>QuickGraph QuikGraph Graph Structure Algorithm C# .NET Serialization</PackageTags>
3535
</PropertyGroup>
3636

37-
<ItemGroup Condition="'$(TargetFramework)'!='netstandard1.3'">
38-
<EmbeddedResource Include="graphml-structure.xsd" />
37+
<ItemGroup Condition="'$(TargetFramework)' != 'netstandard1.3'">
3938
<EmbeddedResource Include="graphml.dtd" />
4039
<EmbeddedResource Include="graphml.xsd" />
40+
<EmbeddedResource Include="graphml-structure.xsd" />
41+
<EmbeddedResource Include="graphml-attributes.xsd" />
42+
<EmbeddedResource Include="graphml-parseinfo.xsd" />
43+
<EmbeddedResource Include="xlink.xsd" />
4144
</ItemGroup>
4245

4346
<!-- Targets defines -->
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?xml version="1.0"?>
2+
3+
<xs:schema
4+
targetNamespace="http://graphml.graphdrawing.org/xmlns"
5+
6+
xmlns="http://graphml.graphdrawing.org/xmlns"
7+
xmlns:xs="http://www.w3.org/2001/XMLSchema"
8+
9+
elementFormDefault="qualified"
10+
attributeFormDefault="unqualified"
11+
>
12+
<xs:annotation>
13+
<xs:documentation
14+
source="http://graphml.graphdrawing.org/"
15+
xml:lang="en">
16+
This document defines the attributes extension of the GraphML language.
17+
It redefines the attribut list of &lt;key> by adding two new
18+
attributes:
19+
- attr.name (gives a name for the data function) and
20+
- attr.type (declares the range of values for the data function).
21+
The data values are defined in #PCDATA children of the corresponding
22+
&lt;data> element.
23+
</xs:documentation>
24+
</xs:annotation>
25+
26+
27+
<xs:redefine schemaLocation="http://graphml.graphdrawing.org/xmlns/1.1/graphml-structure.xsd">
28+
29+
<xs:annotation>
30+
<xs:documentation
31+
source="http://graphml.graphdrawing.org/"
32+
xml:lang="en">
33+
Redefinition of file graphml-structure.xsd.
34+
Extends the attribute group key.extra.attrib (which takes
35+
part in the attribute list of &lt;key>) by adding the
36+
attribute group key.attributes.attrib which is defined below.
37+
</xs:documentation>
38+
</xs:annotation>
39+
40+
<xs:attributeGroup name="key.extra.attrib">
41+
<xs:attributeGroup ref="key.extra.attrib"/>
42+
<xs:attributeGroup ref="key.attributes.attrib"/>
43+
</xs:attributeGroup>
44+
45+
</xs:redefine>
46+
47+
<xs:simpleType name="key.name.type" final="#all">
48+
49+
<xs:annotation>
50+
<xs:documentation
51+
source="http://graphml.graphdrawing.org/(Dokumentation der Attributes Erweiterung; entsprechende Stelle.html)"
52+
xml:lang="en">
53+
Simple type for the attr.name attribute of &lt;key>.
54+
key.name.type is final, that is, it may not be extended
55+
or restricted.
56+
key.name.type is a restriction of xs:NMTOKEN
57+
Allowed values: (no restriction)
58+
</xs:documentation>
59+
</xs:annotation>
60+
61+
<xs:restriction base="xs:NMTOKEN"/>
62+
63+
</xs:simpleType>
64+
65+
66+
67+
<xs:simpleType name="key.type.type" final="#all">
68+
69+
<xs:annotation>
70+
<xs:documentation
71+
source="http://graphml.graphdrawing.org/(Dokumentation der Attributes Erweiterung; entsprechende Stelle.html)"
72+
xml:lang="en">
73+
Simple type for the attr.type attribute of &lt;key>.
74+
key.type.type is final, that is, it may not be extended
75+
or restricted.
76+
key.type.type is a restriction of xs:NMTOKEN
77+
Allowed values: boolean, int, long, float, double, string.
78+
</xs:documentation>
79+
</xs:annotation>
80+
81+
<xs:restriction base="xs:NMTOKEN">
82+
<xs:enumeration value="boolean"/>
83+
<xs:enumeration value="int"/>
84+
<xs:enumeration value="long"/>
85+
<xs:enumeration value="float"/>
86+
<xs:enumeration value="double"/>
87+
<xs:enumeration value="string"/>
88+
</xs:restriction>
89+
90+
</xs:simpleType>
91+
92+
93+
<xs:attributeGroup name="key.attributes.attrib">
94+
95+
<xs:annotation>
96+
<xs:documentation
97+
source="http://graphml.graphdrawing.org/"
98+
xml:lang="en">
99+
Definition of the attribute group key.attributes.attrib.
100+
This group consists of the two optional attributes
101+
- attr.name (gives the name for the data function)
102+
- attr.type ((declares the range of values for the data function)
103+
</xs:documentation>
104+
</xs:annotation>
105+
106+
<xs:attribute name="attr.name" type="key.name.type" use="optional"/>
107+
<xs:attribute name="attr.type" type="key.type.type" use="optional"/>
108+
</xs:attributeGroup>
109+
110+
</xs:schema>

0 commit comments

Comments
 (0)