Skip to content

Commit

Permalink
More resilient RDF/XML parser against absence of attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
mdesalvo committed Sep 6, 2020
1 parent 2259e3e commit 9492810
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions RDFSharp/Model/Serializers/RDFXml.cs
Original file line number Diff line number Diff line change
@@ -430,7 +430,8 @@ internal static RDFGraph Deserialize(Stream inputStream)
Uri xmlBase = null;
if (xmlnsAttrs != null && xmlnsAttrs.Count > 0)
{
var xmlBaseAttr = (rdfRDF.Attributes["xml:base"] ?? rdfRDF.Attributes["xmlns"]);
var xmlBaseAttr = (rdfRDF.Attributes?[RDFVocabulary.XML.PREFIX + ":base"]
?? rdfRDF.Attributes?["xmlns"]);
if (xmlBaseAttr != null)
{
xmlBase = RDFModelUtilities.GetUriFromString(xmlBaseAttr.Value);
@@ -851,12 +852,12 @@ private static XmlAttribute GetRdfAboutAttribute(XmlNode subjNode)
{
//rdf:about
XmlAttribute rdfAbout =
(subjNode.Attributes[RDFVocabulary.RDF.PREFIX + ":about"] ??
(subjNode.Attributes["about"] ??
(subjNode.Attributes[RDFVocabulary.RDF.PREFIX + ":nodeID"] ??
(subjNode.Attributes["nodeID"] ??
(subjNode.Attributes[RDFVocabulary.RDF.PREFIX + ":ID"] ??
subjNode.Attributes["ID"])))));
(subjNode.Attributes?[RDFVocabulary.RDF.PREFIX + ":about"] ??
(subjNode.Attributes?["about"] ??
(subjNode.Attributes?[RDFVocabulary.RDF.PREFIX + ":nodeID"] ??
(subjNode.Attributes?["nodeID"] ??
(subjNode.Attributes?[RDFVocabulary.RDF.PREFIX + ":ID"] ??
subjNode.Attributes?["ID"])))));

return rdfAbout;
}
@@ -868,10 +869,10 @@ private static XmlAttribute GetRdfResourceAttribute(XmlNode predNode)
{
//rdf:Resource
XmlAttribute rdfResource =
(predNode.Attributes[RDFVocabulary.RDF.PREFIX + ":resource"] ??
(predNode.Attributes["resource"] ??
(predNode.Attributes[RDFVocabulary.RDF.PREFIX + ":nodeID"] ??
predNode.Attributes["nodeID"])));
(predNode.Attributes?[RDFVocabulary.RDF.PREFIX + ":resource"] ??
(predNode.Attributes?["resource"] ??
(predNode.Attributes?[RDFVocabulary.RDF.PREFIX + ":nodeID"] ??
predNode.Attributes?["nodeID"])));

return rdfResource;
}
@@ -883,8 +884,8 @@ private static XmlAttribute GetRdfDatatypeAttribute(XmlNode predNode)
{
//rdf:datatype
XmlAttribute rdfDatatype =
(predNode.Attributes[RDFVocabulary.RDF.PREFIX + ":datatype"] ??
predNode.Attributes["datatype"]);
(predNode.Attributes?[RDFVocabulary.RDF.PREFIX + ":datatype"] ??
predNode.Attributes?["datatype"]);

return rdfDatatype;
}
@@ -896,8 +897,8 @@ private static XmlAttribute GetXmlLangAttribute(XmlNode predNode)
{
//xml:lang
XmlAttribute xmlLang =
(predNode.Attributes[RDFVocabulary.XML.PREFIX + ":lang"] ??
predNode.Attributes["lang"]);
(predNode.Attributes?[RDFVocabulary.XML.PREFIX + ":lang"] ??
predNode.Attributes?["lang"]);

return xmlLang;
}
@@ -908,8 +909,8 @@ private static XmlAttribute GetXmlLangAttribute(XmlNode predNode)
private static XmlAttribute GetParseTypeCollectionAttribute(XmlNode predNode)
{
XmlAttribute rdfCollection =
(predNode.Attributes[RDFVocabulary.RDF.PREFIX + ":parseType"] ??
predNode.Attributes["parseType"]);
(predNode.Attributes?[RDFVocabulary.RDF.PREFIX + ":parseType"] ??
predNode.Attributes?["parseType"]);

return ((rdfCollection != null && rdfCollection.Value.Equals("Collection", StringComparison.Ordinal)) ? rdfCollection : null);
}
@@ -920,8 +921,8 @@ private static XmlAttribute GetParseTypeCollectionAttribute(XmlNode predNode)
private static XmlAttribute GetParseTypeLiteralAttribute(XmlNode predNode)
{
XmlAttribute rdfLiteral =
(predNode.Attributes[RDFVocabulary.RDF.PREFIX + ":parseType"] ??
predNode.Attributes["parseType"]);
(predNode.Attributes?[RDFVocabulary.RDF.PREFIX + ":parseType"] ??
predNode.Attributes?["parseType"]);

return ((rdfLiteral != null && rdfLiteral.Value.Equals("Literal", StringComparison.Ordinal)) ? rdfLiteral : null);
}

0 comments on commit 9492810

Please sign in to comment.