Skip to content

Commit

Permalink
Apply code optimizations from VS
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco De Salvo committed Mar 14, 2024
1 parent 7b08135 commit 6ceeb30
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 31 deletions.
5 changes: 2 additions & 3 deletions RDFSharp/Model/RDFNamespace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,8 @@ public RDFNamespace(string prefix, string uri)
throw new RDFModelException("Cannot create RDFNamespace because \"prefix\" parameter cannot be \"bnode\" or \"xmlns\"");

//Uri must be absolute and cannot start with "bnode:" or "xmlns:"
Uri finalUri = RDFModelUtilities.GetUriFromString(uri.Trim());
if (finalUri == null)
throw new RDFModelException("Cannot create RDFNamespace because \"uri\" parameter is not a valid Uri");
Uri finalUri = RDFModelUtilities.GetUriFromString(uri.Trim())
?? throw new RDFModelException("Cannot create RDFNamespace because \"uri\" parameter is not a valid Uri");
if (finalUri.ToString().StartsWith("bnode", StringComparison.OrdinalIgnoreCase) || finalUri.ToString().StartsWith("xmlns", StringComparison.OrdinalIgnoreCase))
throw new RDFModelException("Cannot create RDFNamespace because \"uri\" parameter cannot start with \"bnode:\" or \"xmlns:\"");

Expand Down
36 changes: 15 additions & 21 deletions RDFSharp/Model/Serializers/RDFXml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -452,9 +452,9 @@ private static List<RDFResource> ParseNodeList(XmlNodeList nodeList, RDFGraph re

//Assert resource as subject
XmlAttribute xmlLangSubj = GetXmlLangAttribute(subjNode) ?? xmlLangParent;
RDFResource subj = subjectParent ?? GetSubjectNode(subjNode, xmlBase, result, hashContext);
if (subj == null)
subj = new RDFResource();
RDFResource subj = subjectParent
?? GetSubjectNode(subjNode, xmlBase, result, hashContext)
?? new RDFResource();
subjects.Add(subj);
#endregion

Expand Down Expand Up @@ -670,12 +670,9 @@ private static List<RDFResource> ParseNodeList(XmlNodeList nodeList, RDFGraph re
/// </summary>
private static XmlNode GetRdfRootNode(XmlDocument xmlDoc, XmlNamespaceManager nsMgr)
{
XmlNode rdf = xmlDoc.SelectSingleNode("rdf:RDF", nsMgr) ?? xmlDoc.SelectSingleNode("RDF", nsMgr);

//Invalid RDF/XML file: root node is neither "rdf:RDF" or "RDF"
if (rdf == null)
throw new Exception("Given file has not a valid \"rdf:RDF\" or \"RDF\" root node");

XmlNode rdf = xmlDoc.SelectSingleNode("rdf:RDF", nsMgr)
?? xmlDoc.SelectSingleNode("RDF", nsMgr)
?? throw new Exception("Given file has not a valid \"rdf:RDF\" or \"RDF\" root node");
return rdf;
}

Expand All @@ -691,16 +688,15 @@ private static XmlAttributeCollection GetXmlnsNamespaces(XmlNode rdfRDF, XmlName
while (iEnum != null && iEnum.MoveNext())
{
XmlAttribute attr = (XmlAttribute)iEnum.Current;
if (attr.LocalName.ToUpperInvariant() != "XMLNS"
&& attr.Name.ToUpperInvariant() != "XML:LANG"
&& attr.Name.ToUpperInvariant() != "XML:BASE")
if (!string.Equals(attr.LocalName, "xmlns", StringComparison.OrdinalIgnoreCase)
&& !string.Equals(attr.Name, "xml:lang", StringComparison.OrdinalIgnoreCase)
&& !string.Equals(attr.Name, "xml:base", StringComparison.OrdinalIgnoreCase))
{
//Try to resolve the current namespace against the namespace register;
//if not resolved, create new namespace with scope limited to actual node
RDFNamespace ns =
(RDFNamespaceRegister.GetByPrefix(attr.LocalName) ??
RDFNamespaceRegister.GetByUri(attr.Value) ??
new RDFNamespace(attr.LocalName, attr.Value));
RDFNamespace ns = RDFNamespaceRegister.GetByPrefix(attr.LocalName)
?? RDFNamespaceRegister.GetByUri(attr.Value)
?? new RDFNamespace(attr.LocalName, attr.Value);

nsMgr.AddNamespace(ns.NamespacePrefix, ns.NamespaceUri.ToString());
}
Expand All @@ -717,11 +713,9 @@ private static RDFNamespace GenerateNamespace(string namespaceString, bool isDat
if (namespaceString != null && namespaceString.Trim() != string.Empty)
{
//Extract the prefixable part from the Uri
Uri uriNS = RDFModelUtilities.GetUriFromString(namespaceString);
if (uriNS == null)
throw new RDFModelException("Cannot create RDFNamespace because given \"namespaceString\" (" + namespaceString + ") parameter cannot be converted to a valid Uri");

string fragment = null;
Uri uriNS = RDFModelUtilities.GetUriFromString(namespaceString)
?? throw new RDFModelException("Cannot create RDFNamespace because given \"namespaceString\" (" + namespaceString + ") parameter cannot be converted to a valid Uri");
string fragment;
string nspace = uriNS.AbsoluteUri;

// e.g.: "http://www.w3.org/2001/XMLSchema#integer"
Expand Down
13 changes: 6 additions & 7 deletions RDFSharp/Store/RDFContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,14 @@ public RDFContext() : this(RDFNamespaceRegister.DefaultNamespace.NamespaceUri) {
/// </summary>
public RDFContext(string ctxUri)
{
Uri tempUri = RDFModelUtilities.GetUriFromString(ctxUri);
Uri tempUri = RDFModelUtilities.GetUriFromString(ctxUri)
?? throw new RDFStoreException("Cannot create RDFContext because given \"ctxUri\" parameter is null or does not represent a valid Uri.");

#region Guards
if (tempUri == null)
throw new RDFStoreException("Cannot create RDFContext because given \"ctxUri\" parameter is null or does not represent a valid Uri.");
if (tempUri.ToString().StartsWith("bnode:", StringComparison.OrdinalIgnoreCase)
|| tempUri.ToString().StartsWith("xmlns:", StringComparison.OrdinalIgnoreCase))
//Do not accept a context starting with reserved "bnode:" or "xmlns:" prefixes
string tempUriString = tempUri.ToString();
if (tempUriString.StartsWith("bnode:", StringComparison.OrdinalIgnoreCase)
|| tempUriString.StartsWith("xmlns:", StringComparison.OrdinalIgnoreCase))
throw new RDFStoreException("Cannot create RDFContext because given \"ctxUri\" parameter represents a blank node Uri.");
#endregion

Context = tempUri;
}
Expand Down

0 comments on commit 6ceeb30

Please sign in to comment.