diff --git a/RDFSharp.Test/Model/RDFDatatypeRegisterTest.cs b/RDFSharp.Test/Model/Registers/RDFDatatypeRegisterTest.cs similarity index 99% rename from RDFSharp.Test/Model/RDFDatatypeRegisterTest.cs rename to RDFSharp.Test/Model/Registers/RDFDatatypeRegisterTest.cs index 1e45c3de..3cd748a1 100644 --- a/RDFSharp.Test/Model/RDFDatatypeRegisterTest.cs +++ b/RDFSharp.Test/Model/Registers/RDFDatatypeRegisterTest.cs @@ -18,7 +18,6 @@ limitations under the License. using RDFSharp.Model; using System; using System.Collections.Generic; -using System.Linq; namespace RDFSharp.Test.Model { diff --git a/RDFSharp.Test/Model/RDFNamespaceRegisterTest.cs b/RDFSharp.Test/Model/Registers/RDFNamespaceRegisterTest.cs similarity index 100% rename from RDFSharp.Test/Model/RDFNamespaceRegisterTest.cs rename to RDFSharp.Test/Model/Registers/RDFNamespaceRegisterTest.cs diff --git a/RDFSharp/Model/RDFDatatypeRegister.cs b/RDFSharp/Model/Registers/RDFDatatypeRegister.cs similarity index 97% rename from RDFSharp/Model/RDFDatatypeRegister.cs rename to RDFSharp/Model/Registers/RDFDatatypeRegister.cs index 1294f918..efe41dee 100644 --- a/RDFSharp/Model/RDFDatatypeRegister.cs +++ b/RDFSharp/Model/Registers/RDFDatatypeRegister.cs @@ -1,108 +1,108 @@ -/* - Copyright 2012-2025 Marco De Salvo - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -using System; -using System.Collections; -using System.Collections.Generic; -using System.Linq; - -namespace RDFSharp.Model -{ - /// - /// RDFDatatypeRegister is a singleton in-memory container for registered RDF datatypes - /// - public sealed class RDFDatatypeRegister : IEnumerable - { - #region Statics - internal static RDFDatatype RDFSLiteral = new RDFDatatype(RDFVocabulary.RDFS.LITERAL.URI, RDFModelEnums.RDFDatatypes.RDFS_LITERAL, null); - #endregion - - #region Properties - /// - /// Singleton instance of the RDFDatatypeRegister class - /// - public static RDFDatatypeRegister Instance { get; internal set; } - - /// - /// List of registered datatypes - /// - internal List Register { get; set; } - - /// - /// Count of the register's namespaces - /// - public static int DatatypesCount - => Instance.Register.Count; - - /// - /// Gets the enumerator on the register's namespaces for iteration - /// - public static IEnumerator DatatypesEnumerator - => Instance.Register.GetEnumerator(); - #endregion - - #region Ctors - /// - /// Default-ctor to initialize the singleton instance of the register - /// - static RDFDatatypeRegister() - { - Instance = new RDFDatatypeRegister { - Register = new List() }; - - foreach (RDFModelEnums.RDFDatatypes datatype in Enum.GetValues(typeof(RDFModelEnums.RDFDatatypes)).Cast()) - Instance.Register.Add(new RDFDatatype(new Uri(RDFModelUtilities.GetDatatypeFromEnum(datatype)), datatype, null) { IsBuiltIn = true }); - } - #endregion - - #region Interfaces - /// - /// Exposes a typed enumerator on the register's datatypes - /// - IEnumerator IEnumerable.GetEnumerator() - => DatatypesEnumerator; - - /// - /// Exposes an untyped enumerator on the register's datatypes - /// - IEnumerator IEnumerable.GetEnumerator() - => DatatypesEnumerator; - #endregion - - #region Methods - /// - /// Adds the given datatype to the register - /// - public static void AddDatatype(RDFDatatype datatype) - { - if (datatype != null && GetDatatype(datatype.ToString()) == null) - Instance.Register.Add(datatype); - } - - /// - /// Retrieves a standard datatype by seeking presence of its Uri - /// - public static RDFDatatype GetDatatype(RDFModelEnums.RDFDatatypes datatype) - => GetDatatype(datatype.GetDatatypeFromEnum()); - - /// - /// Retrieves a datatype by seeking presence of its Uri (null if not found) - /// - public static RDFDatatype GetDatatype(string datatypeUri) - => Instance.Register.Find(dt => string.Equals(dt.ToString(), datatypeUri?.Trim())); - #endregion - } +/* + Copyright 2012-2025 Marco De Salvo + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; + +namespace RDFSharp.Model +{ + /// + /// RDFDatatypeRegister is a singleton in-memory container for registered RDF datatypes + /// + public sealed class RDFDatatypeRegister : IEnumerable + { + #region Statics + internal static RDFDatatype RDFSLiteral = new RDFDatatype(RDFVocabulary.RDFS.LITERAL.URI, RDFModelEnums.RDFDatatypes.RDFS_LITERAL, null); + #endregion + + #region Properties + /// + /// Singleton instance of the RDFDatatypeRegister class + /// + public static RDFDatatypeRegister Instance { get; internal set; } + + /// + /// List of registered datatypes + /// + internal List Register { get; set; } + + /// + /// Count of the register's namespaces + /// + public static int DatatypesCount + => Instance.Register.Count; + + /// + /// Gets the enumerator on the register's namespaces for iteration + /// + public static IEnumerator DatatypesEnumerator + => Instance.Register.GetEnumerator(); + #endregion + + #region Ctors + /// + /// Default-ctor to initialize the singleton instance of the register + /// + static RDFDatatypeRegister() + { + Instance = new RDFDatatypeRegister { + Register = new List() }; + + foreach (RDFModelEnums.RDFDatatypes datatype in Enum.GetValues(typeof(RDFModelEnums.RDFDatatypes)).Cast()) + Instance.Register.Add(new RDFDatatype(new Uri(RDFModelUtilities.GetDatatypeFromEnum(datatype)), datatype, null) { IsBuiltIn = true }); + } + #endregion + + #region Interfaces + /// + /// Exposes a typed enumerator on the register's datatypes + /// + IEnumerator IEnumerable.GetEnumerator() + => DatatypesEnumerator; + + /// + /// Exposes an untyped enumerator on the register's datatypes + /// + IEnumerator IEnumerable.GetEnumerator() + => DatatypesEnumerator; + #endregion + + #region Methods + /// + /// Adds the given datatype to the register + /// + public static void AddDatatype(RDFDatatype datatype) + { + if (datatype != null && GetDatatype(datatype.ToString()) == null) + Instance.Register.Add(datatype); + } + + /// + /// Retrieves a standard datatype by seeking presence of its Uri + /// + public static RDFDatatype GetDatatype(RDFModelEnums.RDFDatatypes datatype) + => GetDatatype(datatype.GetDatatypeFromEnum()); + + /// + /// Retrieves a datatype by seeking presence of its Uri (null if not found) + /// + public static RDFDatatype GetDatatype(string datatypeUri) + => Instance.Register.Find(dt => string.Equals(dt.ToString(), datatypeUri?.Trim())); + #endregion + } } \ No newline at end of file diff --git a/RDFSharp/Model/RDFNamespaceRegister.cs b/RDFSharp/Model/Registers/RDFNamespaceRegister.cs similarity index 97% rename from RDFSharp/Model/RDFNamespaceRegister.cs rename to RDFSharp/Model/Registers/RDFNamespaceRegister.cs index d0ebc253..f0becd32 100644 --- a/RDFSharp/Model/RDFNamespaceRegister.cs +++ b/RDFSharp/Model/Registers/RDFNamespaceRegister.cs @@ -1,237 +1,237 @@ -/* - Copyright 2012-2025 Marco De Salvo - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -using System; -using System.Collections; -using System.Collections.Generic; -using static RDFSharp.Query.RDFQueryUtilities; - -namespace RDFSharp.Model -{ - /// - /// RDFNamespaceRegister is a singleton in-memory container for registered RDF namespaces. - /// - public sealed class RDFNamespaceRegister : IEnumerable - { - #region Properties - /// - /// Default namespace of the library (rdfsharp) - /// - private static readonly RDFNamespace RDFSharpNS = new RDFNamespace(RDFVocabulary.RDFSHARP.PREFIX, RDFVocabulary.RDFSHARP.BASE_URI).SetDereferenceUri(new Uri(RDFVocabulary.RDFSHARP.DEREFERENCE_URI)); - - /// - /// Default namespace of the library - /// - public static RDFNamespace DefaultNamespace { get; internal set; } - - /// - /// Singleton instance of the RDFNamespaceRegister class - /// - public static RDFNamespaceRegister Instance { get; internal set; } - - /// - /// List of registered namespaces - /// - internal List Register { get; set; } - - /// - /// Count of the register's namespaces - /// - public static int NamespacesCount - => Instance.Register.Count; - - /// - /// Gets the enumerator on the register's namespaces for iteration - /// - public static IEnumerator NamespacesEnumerator - => Instance.Register.GetEnumerator(); - #endregion - - #region Ctors - /// - /// Default-ctor to initialize the singleton instance of the register - /// - static RDFNamespaceRegister() - { - Instance = new RDFNamespaceRegister() - { - Register = new List() - { - RDFSharpNS, - - //Basic - new RDFNamespace(RDFVocabulary.RDF.PREFIX, RDFVocabulary.RDF.BASE_URI).SetDereferenceUri(new Uri(RDFVocabulary.RDF.DEREFERENCE_URI)), - new RDFNamespace(RDFVocabulary.RDFS.PREFIX, RDFVocabulary.RDFS.BASE_URI).SetDereferenceUri(new Uri(RDFVocabulary.RDFS.DEREFERENCE_URI)), - new RDFNamespace(RDFVocabulary.XSD.PREFIX, RDFVocabulary.XSD.BASE_URI).SetDereferenceUri(new Uri(RDFVocabulary.XSD.DEREFERENCE_URI)), - new RDFNamespace(RDFVocabulary.OWL.PREFIX, RDFVocabulary.OWL.BASE_URI).SetDereferenceUri(new Uri(RDFVocabulary.OWL.DEREFERENCE_URI)), - new RDFNamespace(RDFVocabulary.SHACL.PREFIX, RDFVocabulary.SHACL.BASE_URI).SetDereferenceUri(new Uri(RDFVocabulary.SHACL.DEREFERENCE_URI)), - new RDFNamespace(RDFVocabulary.SWRL.PREFIX, RDFVocabulary.SWRL.BASE_URI).SetDereferenceUri(new Uri(RDFVocabulary.SWRL.DEREFERENCE_URI)), - new RDFNamespace(RDFVocabulary.SWRL.SWRLB.PREFIX, RDFVocabulary.SWRL.SWRLB.BASE_URI).SetDereferenceUri(new Uri(RDFVocabulary.SWRL.SWRLB.DEREFERENCE_URI)), - new RDFNamespace(RDFVocabulary.XML.PREFIX, RDFVocabulary.XML.BASE_URI).SetDereferenceUri(new Uri(RDFVocabulary.XML.DEREFERENCE_URI)), - - //Extended - new RDFNamespace(RDFVocabulary.DC.PREFIX, RDFVocabulary.DC.BASE_URI).SetDereferenceUri(new Uri(RDFVocabulary.DC.DEREFERENCE_URI)), - new RDFNamespace(RDFVocabulary.DC.DCAM.PREFIX, RDFVocabulary.DC.DCAM.BASE_URI).SetDereferenceUri(new Uri(RDFVocabulary.DC.DCAM.DEREFERENCE_URI)), - new RDFNamespace(RDFVocabulary.DC.DCTERMS.PREFIX, RDFVocabulary.DC.DCTERMS.BASE_URI).SetDereferenceUri(new Uri(RDFVocabulary.DC.DCTERMS.DEREFERENCE_URI)), - new RDFNamespace(RDFVocabulary.DC.DCTYPE.PREFIX, RDFVocabulary.DC.DCTYPE.BASE_URI).SetDereferenceUri(new Uri(RDFVocabulary.DC.DCTYPE.DEREFERENCE_URI)), - new RDFNamespace(RDFVocabulary.FOAF.PREFIX, RDFVocabulary.FOAF.BASE_URI).SetDereferenceUri(new Uri(RDFVocabulary.FOAF.DEREFERENCE_URI)), - new RDFNamespace(RDFVocabulary.GEO.PREFIX, RDFVocabulary.GEO.BASE_URI).SetDereferenceUri(new Uri(RDFVocabulary.GEO.DEREFERENCE_URI)), - new RDFNamespace(RDFVocabulary.GEOSPARQL.PREFIX, RDFVocabulary.GEOSPARQL.BASE_URI).SetDereferenceUri(new Uri(RDFVocabulary.GEOSPARQL.DEREFERENCE_URI)), - new RDFNamespace(RDFVocabulary.GEOSPARQL.SF.PREFIX, RDFVocabulary.GEOSPARQL.SF.BASE_URI).SetDereferenceUri(new Uri(RDFVocabulary.GEOSPARQL.SF.DEREFERENCE_URI)), - new RDFNamespace(RDFVocabulary.GEOSPARQL.GEOF.PREFIX, RDFVocabulary.GEOSPARQL.GEOF.BASE_URI).SetDereferenceUri(new Uri(RDFVocabulary.GEOSPARQL.GEOF.DEREFERENCE_URI)), - new RDFNamespace(RDFVocabulary.SKOS.PREFIX, RDFVocabulary.SKOS.BASE_URI).SetDereferenceUri(new Uri(RDFVocabulary.SKOS.DEREFERENCE_URI)), - new RDFNamespace(RDFVocabulary.SKOS.SKOSXL.PREFIX, RDFVocabulary.SKOS.SKOSXL.BASE_URI).SetDereferenceUri(new Uri(RDFVocabulary.SKOS.SKOSXL.DEREFERENCE_URI)), - new RDFNamespace(RDFVocabulary.TIME.PREFIX, RDFVocabulary.TIME.BASE_URI).SetDereferenceUri(new Uri(RDFVocabulary.TIME.DEREFERENCE_URI)), - new RDFNamespace(RDFVocabulary.TIME.GREG.PREFIX, RDFVocabulary.TIME.GREG.BASE_URI).SetDereferenceUri(new Uri(RDFVocabulary.TIME.GREG.DEREFERENCE_URI)), - new RDFNamespace(RDFVocabulary.TIME.THORS.PREFIX, RDFVocabulary.TIME.THORS.BASE_URI).SetDereferenceUri(new Uri(RDFVocabulary.TIME.THORS.DEREFERENCE_URI)) - } - }; - - DefaultNamespace = RDFSharpNS; - } - #endregion - - #region Interfaces - /// - /// Exposes a typed enumerator on the register's namespaces - /// - IEnumerator IEnumerable.GetEnumerator() - => NamespacesEnumerator; - - /// - /// Exposes an untyped enumerator on the register's namespaces - /// - IEnumerator IEnumerable.GetEnumerator() - => NamespacesEnumerator; - #endregion - - #region Methods - /// - /// Sets the given namespace as default namespace of the library. - /// - public static void SetDefaultNamespace(RDFNamespace nSpace) - { - if (nSpace != null) - { - DefaultNamespace = nSpace; - - //Also add it to the register - AddNamespace(nSpace); - } - } - - /// - /// Resets the default namespace of the library. - /// - public static void ResetDefaultNamespace() - => DefaultNamespace = RDFSharpNS; - - /// - /// Adds the given namespace to the register, if it has unique prefix and uri. - /// - public static void AddNamespace(RDFNamespace nSpace) - { - if (nSpace != null) - { - if (GetByPrefix(nSpace.NamespacePrefix) == null && GetByUri(nSpace.NamespaceUri.ToString()) == null) - Instance.Register.Add(nSpace); - } - } - - /// - /// Removes the namespace having the given Uri. - /// - public static void RemoveByUri(string uri) - { - if (uri != null) - Instance.Register.RemoveAll(ns => string.Equals(ns.NamespaceUri.ToString(), uri.Trim(), StringComparison.OrdinalIgnoreCase)); - } - - /// - /// Removes the namespace having the given prefix. - /// - public static void RemoveByPrefix(string prefix) - { - if (prefix != null) - Instance.Register.RemoveAll(ns => string.Equals(ns.NamespacePrefix, prefix.Trim(), StringComparison.OrdinalIgnoreCase)); - } - - /// - /// Retrieves a namespace by seeking presence of its Uri (cascading a lookup to prefix.cc service, if specified) - /// - public static RDFNamespace GetByUri(string uri, bool enablePrefixCCService = false) - { - if (uri != null) - { - RDFNamespace result = Instance.Register.Find(ns => string.Equals(ns.NamespaceUri.ToString(), uri.Trim(), StringComparison.OrdinalIgnoreCase)); - if (result == null && enablePrefixCCService) - result = LookupPrefixCC(uri.Trim().TrimEnd(new char[] { '#' }), 2); - return result; - } - return null; - } - - /// - /// Retrieves a namespace by seeking presence of its prefix (cascading a lookup to prefix.cc service, if specified) - /// - public static RDFNamespace GetByPrefix(string prefix, bool enablePrefixCCService = false) - { - if (prefix != null) - { - RDFNamespace result = Instance.Register.Find(ns => string.Equals(ns.NamespacePrefix, prefix.Trim(), StringComparison.OrdinalIgnoreCase)); - if (result == null && enablePrefixCCService) - result = LookupPrefixCC(prefix.Trim(), 1); - return result; - } - return null; - } - - /// - /// Lookups the given prefix or namespace into the prefix.cc service - /// - internal static RDFNamespace LookupPrefixCC(string data, int lookupMode) - { - string lookupString = lookupMode == 1 ? string.Concat("http://prefix.cc/", data, ".file.txt") - : string.Concat("http://prefix.cc/reverse?uri=", data, "&format=txt"); - - using (RDFWebClient webclient = new RDFWebClient(1000)) - { - try - { - string response = webclient.DownloadString(lookupString); - string[] splittedResponse = response.Split('\t'); - string prefix = splittedResponse[0]; - string nspace = splittedResponse[1].TrimEnd(Environment.NewLine); - RDFNamespace result = new RDFNamespace(prefix, nspace); - - //Also add the namespace to the register (to avoid future lookups) - AddNamespace(result); - - //Return the found result - return result; - } - catch { return null; } - } - } - - /// - /// Removes namespaces marked as temporary - /// - internal static void RemoveTemporaryNamespaces() - => Instance.Register.RemoveAll(x => x.IsTemporary); - #endregion - } +/* + Copyright 2012-2025 Marco De Salvo + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +using System; +using System.Collections; +using System.Collections.Generic; +using static RDFSharp.Query.RDFQueryUtilities; + +namespace RDFSharp.Model +{ + /// + /// RDFNamespaceRegister is a singleton in-memory container for registered RDF namespaces. + /// + public sealed class RDFNamespaceRegister : IEnumerable + { + #region Properties + /// + /// Default namespace of the library (rdfsharp) + /// + private static readonly RDFNamespace RDFSharpNS = new RDFNamespace(RDFVocabulary.RDFSHARP.PREFIX, RDFVocabulary.RDFSHARP.BASE_URI).SetDereferenceUri(new Uri(RDFVocabulary.RDFSHARP.DEREFERENCE_URI)); + + /// + /// Default namespace of the library + /// + public static RDFNamespace DefaultNamespace { get; internal set; } + + /// + /// Singleton instance of the RDFNamespaceRegister class + /// + public static RDFNamespaceRegister Instance { get; internal set; } + + /// + /// List of registered namespaces + /// + internal List Register { get; set; } + + /// + /// Count of the register's namespaces + /// + public static int NamespacesCount + => Instance.Register.Count; + + /// + /// Gets the enumerator on the register's namespaces for iteration + /// + public static IEnumerator NamespacesEnumerator + => Instance.Register.GetEnumerator(); + #endregion + + #region Ctors + /// + /// Default-ctor to initialize the singleton instance of the register + /// + static RDFNamespaceRegister() + { + Instance = new RDFNamespaceRegister() + { + Register = new List() + { + RDFSharpNS, + + //Basic + new RDFNamespace(RDFVocabulary.RDF.PREFIX, RDFVocabulary.RDF.BASE_URI).SetDereferenceUri(new Uri(RDFVocabulary.RDF.DEREFERENCE_URI)), + new RDFNamespace(RDFVocabulary.RDFS.PREFIX, RDFVocabulary.RDFS.BASE_URI).SetDereferenceUri(new Uri(RDFVocabulary.RDFS.DEREFERENCE_URI)), + new RDFNamespace(RDFVocabulary.XSD.PREFIX, RDFVocabulary.XSD.BASE_URI).SetDereferenceUri(new Uri(RDFVocabulary.XSD.DEREFERENCE_URI)), + new RDFNamespace(RDFVocabulary.OWL.PREFIX, RDFVocabulary.OWL.BASE_URI).SetDereferenceUri(new Uri(RDFVocabulary.OWL.DEREFERENCE_URI)), + new RDFNamespace(RDFVocabulary.SHACL.PREFIX, RDFVocabulary.SHACL.BASE_URI).SetDereferenceUri(new Uri(RDFVocabulary.SHACL.DEREFERENCE_URI)), + new RDFNamespace(RDFVocabulary.SWRL.PREFIX, RDFVocabulary.SWRL.BASE_URI).SetDereferenceUri(new Uri(RDFVocabulary.SWRL.DEREFERENCE_URI)), + new RDFNamespace(RDFVocabulary.SWRL.SWRLB.PREFIX, RDFVocabulary.SWRL.SWRLB.BASE_URI).SetDereferenceUri(new Uri(RDFVocabulary.SWRL.SWRLB.DEREFERENCE_URI)), + new RDFNamespace(RDFVocabulary.XML.PREFIX, RDFVocabulary.XML.BASE_URI).SetDereferenceUri(new Uri(RDFVocabulary.XML.DEREFERENCE_URI)), + + //Extended + new RDFNamespace(RDFVocabulary.DC.PREFIX, RDFVocabulary.DC.BASE_URI).SetDereferenceUri(new Uri(RDFVocabulary.DC.DEREFERENCE_URI)), + new RDFNamespace(RDFVocabulary.DC.DCAM.PREFIX, RDFVocabulary.DC.DCAM.BASE_URI).SetDereferenceUri(new Uri(RDFVocabulary.DC.DCAM.DEREFERENCE_URI)), + new RDFNamespace(RDFVocabulary.DC.DCTERMS.PREFIX, RDFVocabulary.DC.DCTERMS.BASE_URI).SetDereferenceUri(new Uri(RDFVocabulary.DC.DCTERMS.DEREFERENCE_URI)), + new RDFNamespace(RDFVocabulary.DC.DCTYPE.PREFIX, RDFVocabulary.DC.DCTYPE.BASE_URI).SetDereferenceUri(new Uri(RDFVocabulary.DC.DCTYPE.DEREFERENCE_URI)), + new RDFNamespace(RDFVocabulary.FOAF.PREFIX, RDFVocabulary.FOAF.BASE_URI).SetDereferenceUri(new Uri(RDFVocabulary.FOAF.DEREFERENCE_URI)), + new RDFNamespace(RDFVocabulary.GEO.PREFIX, RDFVocabulary.GEO.BASE_URI).SetDereferenceUri(new Uri(RDFVocabulary.GEO.DEREFERENCE_URI)), + new RDFNamespace(RDFVocabulary.GEOSPARQL.PREFIX, RDFVocabulary.GEOSPARQL.BASE_URI).SetDereferenceUri(new Uri(RDFVocabulary.GEOSPARQL.DEREFERENCE_URI)), + new RDFNamespace(RDFVocabulary.GEOSPARQL.SF.PREFIX, RDFVocabulary.GEOSPARQL.SF.BASE_URI).SetDereferenceUri(new Uri(RDFVocabulary.GEOSPARQL.SF.DEREFERENCE_URI)), + new RDFNamespace(RDFVocabulary.GEOSPARQL.GEOF.PREFIX, RDFVocabulary.GEOSPARQL.GEOF.BASE_URI).SetDereferenceUri(new Uri(RDFVocabulary.GEOSPARQL.GEOF.DEREFERENCE_URI)), + new RDFNamespace(RDFVocabulary.SKOS.PREFIX, RDFVocabulary.SKOS.BASE_URI).SetDereferenceUri(new Uri(RDFVocabulary.SKOS.DEREFERENCE_URI)), + new RDFNamespace(RDFVocabulary.SKOS.SKOSXL.PREFIX, RDFVocabulary.SKOS.SKOSXL.BASE_URI).SetDereferenceUri(new Uri(RDFVocabulary.SKOS.SKOSXL.DEREFERENCE_URI)), + new RDFNamespace(RDFVocabulary.TIME.PREFIX, RDFVocabulary.TIME.BASE_URI).SetDereferenceUri(new Uri(RDFVocabulary.TIME.DEREFERENCE_URI)), + new RDFNamespace(RDFVocabulary.TIME.GREG.PREFIX, RDFVocabulary.TIME.GREG.BASE_URI).SetDereferenceUri(new Uri(RDFVocabulary.TIME.GREG.DEREFERENCE_URI)), + new RDFNamespace(RDFVocabulary.TIME.THORS.PREFIX, RDFVocabulary.TIME.THORS.BASE_URI).SetDereferenceUri(new Uri(RDFVocabulary.TIME.THORS.DEREFERENCE_URI)) + } + }; + + DefaultNamespace = RDFSharpNS; + } + #endregion + + #region Interfaces + /// + /// Exposes a typed enumerator on the register's namespaces + /// + IEnumerator IEnumerable.GetEnumerator() + => NamespacesEnumerator; + + /// + /// Exposes an untyped enumerator on the register's namespaces + /// + IEnumerator IEnumerable.GetEnumerator() + => NamespacesEnumerator; + #endregion + + #region Methods + /// + /// Sets the given namespace as default namespace of the library. + /// + public static void SetDefaultNamespace(RDFNamespace nSpace) + { + if (nSpace != null) + { + DefaultNamespace = nSpace; + + //Also add it to the register + AddNamespace(nSpace); + } + } + + /// + /// Resets the default namespace of the library. + /// + public static void ResetDefaultNamespace() + => DefaultNamespace = RDFSharpNS; + + /// + /// Adds the given namespace to the register, if it has unique prefix and uri. + /// + public static void AddNamespace(RDFNamespace nSpace) + { + if (nSpace != null) + { + if (GetByPrefix(nSpace.NamespacePrefix) == null && GetByUri(nSpace.NamespaceUri.ToString()) == null) + Instance.Register.Add(nSpace); + } + } + + /// + /// Removes the namespace having the given Uri. + /// + public static void RemoveByUri(string uri) + { + if (uri != null) + Instance.Register.RemoveAll(ns => string.Equals(ns.NamespaceUri.ToString(), uri.Trim(), StringComparison.OrdinalIgnoreCase)); + } + + /// + /// Removes the namespace having the given prefix. + /// + public static void RemoveByPrefix(string prefix) + { + if (prefix != null) + Instance.Register.RemoveAll(ns => string.Equals(ns.NamespacePrefix, prefix.Trim(), StringComparison.OrdinalIgnoreCase)); + } + + /// + /// Retrieves a namespace by seeking presence of its Uri (cascading a lookup to prefix.cc service, if specified) + /// + public static RDFNamespace GetByUri(string uri, bool enablePrefixCCService = false) + { + if (uri != null) + { + RDFNamespace result = Instance.Register.Find(ns => string.Equals(ns.NamespaceUri.ToString(), uri.Trim(), StringComparison.OrdinalIgnoreCase)); + if (result == null && enablePrefixCCService) + result = LookupPrefixCC(uri.Trim().TrimEnd(new char[] { '#' }), 2); + return result; + } + return null; + } + + /// + /// Retrieves a namespace by seeking presence of its prefix (cascading a lookup to prefix.cc service, if specified) + /// + public static RDFNamespace GetByPrefix(string prefix, bool enablePrefixCCService = false) + { + if (prefix != null) + { + RDFNamespace result = Instance.Register.Find(ns => string.Equals(ns.NamespacePrefix, prefix.Trim(), StringComparison.OrdinalIgnoreCase)); + if (result == null && enablePrefixCCService) + result = LookupPrefixCC(prefix.Trim(), 1); + return result; + } + return null; + } + + /// + /// Lookups the given prefix or namespace into the prefix.cc service + /// + internal static RDFNamespace LookupPrefixCC(string data, int lookupMode) + { + string lookupString = lookupMode == 1 ? string.Concat("http://prefix.cc/", data, ".file.txt") + : string.Concat("http://prefix.cc/reverse?uri=", data, "&format=txt"); + + using (RDFWebClient webclient = new RDFWebClient(1000)) + { + try + { + string response = webclient.DownloadString(lookupString); + string[] splittedResponse = response.Split('\t'); + string prefix = splittedResponse[0]; + string nspace = splittedResponse[1].TrimEnd(Environment.NewLine); + RDFNamespace result = new RDFNamespace(prefix, nspace); + + //Also add the namespace to the register (to avoid future lookups) + AddNamespace(result); + + //Return the found result + return result; + } + catch { return null; } + } + } + + /// + /// Removes namespaces marked as temporary + /// + internal static void RemoveTemporaryNamespaces() + => Instance.Register.RemoveAll(x => x.IsTemporary); + #endregion + } } \ No newline at end of file