< Summary

Information
Class: ValidateLib.IRINormalization.IRINormalizator
Assembly: validatelib.dll
File(s): C:\skola_karlovka\RP\code\csv-validator\CSV_Validator\ValidateLib\IRINormalization\IRINormalizator.cs
Line coverage
95%
Covered lines: 21
Uncovered lines: 1
Coverable lines: 22
Total lines: 112
Line coverage: 95.4%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

C:\skola_karlovka\RP\code\csv-validator\CSV_Validator\ValidateLib\IRINormalization\IRINormalizator.cs

#LineLine coverage
 1namespace ValidateLib.IRINormalization
 2{
 3    /// <summary>
 4    /// Provides methods for normalizing IRIs.
 5    /// </summary>
 6    public class IRINormalizator
 7    {
 8        /// <summary>
 9        /// Normalizes absolute IRI.
 10        /// </summary>
 11        /// <param name="IRI"> absolute iri</param>
 12        /// <returns> normalized absolute iri</returns>
 13        public static string NormalizeIri(string IRI)
 14        {
 115            var uri = new Uri(IRI, UriKind.Absolute);
 116            return uri.ToString();
 17        }
 18
 19        /// <summary>
 20        /// Turns url into absolute using base url.
 21        /// </summary>
 22        /// <param name="url"> Url to be turned into absolute. </param>
 23        /// <param name="baseUrl"> Base url. </param>
 24        /// <returns></returns>
 25        public static string TurnUrlIntoAbsoluteUsingBase(string url, string baseUrl)
 26        {
 127            string normalizedUrl = baseUrl + url;
 128            return NormalizeIri(normalizedUrl);
 29
 30        }
 31
 32        /// <summary>
 33        /// Extracts base url from url.
 34        /// </summary>
 35        /// <param name="fileURL"> url from which we want to extract the base part. </param>
 36        /// <returns></returns>
 37        public static string GetBaseUrl(string fileURL)
 38        {
 139            string fileName = Path.GetFileName(fileURL);
 140            return fileURL.Replace(fileName, string.Empty);
 41        }
 42
 43        /// <summary>
 44        /// Turns url into absolute using another url that references it.
 45        /// </summary>
 46        /// <param name="referencingUrl"> referencing url, typically the url of metadata file</param>
 47        /// <param name="targetUrl"> url to turn into absolute </param>
 48        /// <returns></returns>
 49        public static string TurnUrlIntoAbsoluteWithReferencedUrl(string referencingUrl, string targetUrl)
 50        {
 151            if (IsAbsoluteUrl(targetUrl))
 52            {
 153                return targetUrl;
 54            }
 55            else
 56            {
 157                return TurnUrlIntoAbsoluteUsingBase(targetUrl, GetBaseUrl(referencingUrl));
 58            }
 59        }
 60
 61        /// <summary>
 62        /// Turns url into absolute with base. Also checks whether the url is already absolute.
 63        /// </summary>
 64        /// <param name="baseURL"> base url</param>
 65        /// <param name="targetUrl"> url to be turned into absolute</param>
 66        /// <returns> absolute url created from <paramref name="targetUrl"/></returns>
 67        public static string TurnUrlIntoAbsoluteWithBase(string baseURL, string targetUrl)
 68        {
 169            if (IsAbsoluteUrl(targetUrl))
 70            {
 071                return targetUrl;
 72            }
 73            else
 74            {
 175                return TurnUrlIntoAbsoluteUsingBase(targetUrl, baseURL);
 76            }
 77        }
 78        /// <summary>
 79        /// normalzies url for link property
 80        /// </summary>
 81        /// <param name="url"> url to be normalized </param>
 82        /// <param name="baseUrl"> base url against which the normalization is done </param>
 83        /// <returns></returns>
 84        public static string NormalizeUrlForLinkProperty(string url, string baseUrl)
 85        {
 186            string normalizedUrl = url;
 187            if (!IsAbsoluteUrl(url))
 88            {
 189                normalizedUrl = TurnUrlIntoAbsoluteUsingBase(url, baseUrl);
 190                normalizedUrl = NormalizeIri(normalizedUrl);
 91            }
 192            return normalizedUrl;
 93        }
 94        /// <summary>
 95        /// Checks whether an url is absolute
 96        /// </summary>
 97        /// <param name="iri"> iri to be checked</param>
 98        /// <returns> true if <paramref name="iri"/> is absolute IRI . </returns>
 99        public static bool IsAbsoluteUrl(string iri)
 100        {
 1101            if (Uri.TryCreate(iri, UriKind.Absolute, out Uri? uri))
 102            {
 1103                if (uri.Scheme == Uri.UriSchemeFile)
 104                {
 1105                    return !string.IsNullOrEmpty(uri.Scheme);
 106                }
 1107                return !string.IsNullOrEmpty(uri.Scheme) && !string.IsNullOrEmpty(uri.Host);
 108            }
 1109            return false;
 110        }
 111    }
 112}