Example #1
0
        /// <summary>
        /// Generic Helper Function which Resolves Uri References against a Base Uri
        /// </summary>
        /// <param name="uriref">Uri Reference to resolve</param>
        /// <param name="baseUri">Base Uri to resolve against</param>
        /// <returns>Resolved Uri as a String</returns>
        /// <exception cref="RdfParseException">RDF Parse Exception if the Uri cannot be resolved for a know reason</exception>
        /// <exception cref="UriFormatException">Uri Format Exception if one/both of the URIs is malformed</exception>
        public static String ResolveUri(String uriref, String baseUri)
        {
            if (!baseUri.Equals(String.Empty))
            {
                if (uriref.Equals(String.Empty))
                {
                    //Empty Uri reference refers to the Base Uri
                    return(UriFactory.Create(Tools.FixMalformedUriStrings(baseUri)).AbsoluteUri);
                }
                else
                {
                    //Resolve the Uri by combining the Absolute/Relative Uri with the in-scope Base Uri
                    Uri u = new Uri(Tools.FixMalformedUriStrings(uriref), UriKind.RelativeOrAbsolute);
                    if (u.IsAbsoluteUri)
                    {
                        //Uri Reference is an Absolute Uri so no need to resolve against Base Uri
                        return(u.AbsoluteUri);
                    }
                    else
                    {
                        Uri b = UriFactory.Create(Tools.FixMalformedUriStrings(baseUri));

                        //Check that the Base Uri is valid for resolving Relative URIs
                        //If the Uri Reference is a Fragment ID then Base Uri validity is irrelevant
                        //We have to use ToString() here because this is a Relative URI so AbsoluteUri would be invalid here
                        if (u.ToString().StartsWith("#"))
                        {
                            return(Tools.ResolveUri(u, b).AbsoluteUri);
                        }
                        else if (Tools.IsValidBaseUri(b))
                        {
                            return(Tools.ResolveUri(u, b).AbsoluteUri);
                        }
                        else
                        {
                            throw new RdfParseException("Cannot resolve a URI since the Base URI is not a valid for resolving Relative URIs against");
                        }
                    }
                }
            }
            else
            {
                if (uriref.Equals(String.Empty))
                {
                    throw new RdfParseException("Cannot use an Empty URI to refer to the document Base URI since there is no in-scope Base URI!");
                }

                try
                {
                    return(new Uri(Tools.FixMalformedUriStrings(uriref), UriKind.Absolute).AbsoluteUri);
                }
#if PORTABLE
                catch (FormatException)
#else
                catch (UriFormatException)
#endif
                {
                    throw new RdfParseException("Cannot resolve a Relative URI Reference since there is no in-scope Base URI!");
                }
            }
        }