Beispiel #1
0
 /// <summary>
 /// Creates a new URI Node with the given URI.
 /// </summary>
 /// <param name="uri">URI for the Node.</param>
 /// <returns></returns>
 /// <remarks>
 /// Generally we expect to be passed an absolute URI, while relative URIs are permitted the behaviour is less well defined.  If there is a Base URI defined for the Graph then relative URIs will be automatically resolved against that Base, if the Base URI is not defined then relative URIs will be left as is.  In this case issues may occur when trying to serialize the data or when accurate round tripping is required.
 /// </remarks>
 public virtual IUriNode CreateUriNode(Uri uri)
 {
     if (!uri.IsAbsoluteUri && _baseuri != null)
     {
         uri = Tools.ResolveUri(uri, _baseuri);
     }
     return(new UriNode(this, uri));
 }
        public void UriResolution()
        {
            String[]   baseUris = { "http://www.bbc.co.uk",
                                    "http://www.bbc.co.uk/",
                                    "http://www.bbc.co.uk/test.txt",
                                    "http://www.bbc.co.uk/test",
                                    "http://www.bbc.co.uk/test/",
                                    "http://www.bbc.co.uk/test/subdir",
                                    "http://www.bbc.co.uk/really/really/long/path",
                                    "http://www.bbc.co.uk#fragment",
                                    "http://www.bbc.co.uk/test.txt#fragment"//,
            };
            String[]   uriRefs = { "test2.txt",
                                   "test2",
                                   "/test2",
                                   "test2/subdir",
                                   "/test2/subdir",
                                   "../test2",
                                   "#fragment2" };
            String[][] expected = { new String[] { "http://www.bbc.co.uk/test2.txt",                    "http://www.bbc.co.uk/test2",                    "http://www.bbc.co.uk/test2", "http://www.bbc.co.uk/test2/subdir",                    "http://www.bbc.co.uk/test2/subdir", "http://www.bbc.co.uk/test2",               "http://www.bbc.co.uk/#fragment2"                        },
                                    new String[] { "http://www.bbc.co.uk/test2.txt",                    "http://www.bbc.co.uk/test2",                    "http://www.bbc.co.uk/test2", "http://www.bbc.co.uk/test2/subdir",                    "http://www.bbc.co.uk/test2/subdir", "http://www.bbc.co.uk/test2",               "http://www.bbc.co.uk/#fragment2"                        },
                                    new String[] { "http://www.bbc.co.uk/test2.txt",                    "http://www.bbc.co.uk/test2",                    "http://www.bbc.co.uk/test2", "http://www.bbc.co.uk/test2/subdir",                    "http://www.bbc.co.uk/test2/subdir", "http://www.bbc.co.uk/test2",               "http://www.bbc.co.uk/test.txt#fragment2"                },
                                    new String[] { "http://www.bbc.co.uk/test2.txt",                    "http://www.bbc.co.uk/test2",                    "http://www.bbc.co.uk/test2", "http://www.bbc.co.uk/test2/subdir",                    "http://www.bbc.co.uk/test2/subdir", "http://www.bbc.co.uk/test2",               "http://www.bbc.co.uk/test#fragment2"                    },
                                    new String[] { "http://www.bbc.co.uk/test/test2.txt",               "http://www.bbc.co.uk/test/test2",               "http://www.bbc.co.uk/test2", "http://www.bbc.co.uk/test/test2/subdir",               "http://www.bbc.co.uk/test2/subdir", "http://www.bbc.co.uk/test2",               "http://www.bbc.co.uk/test/#fragment2"                   },
                                    new String[] { "http://www.bbc.co.uk/test/test2.txt",               "http://www.bbc.co.uk/test/test2",               "http://www.bbc.co.uk/test2", "http://www.bbc.co.uk/test/test2/subdir",               "http://www.bbc.co.uk/test2/subdir", "http://www.bbc.co.uk/test2",               "http://www.bbc.co.uk/test/subdir#fragment2"             },
                                    new String[] { "http://www.bbc.co.uk/really/really/long/test2.txt", "http://www.bbc.co.uk/really/really/long/test2", "http://www.bbc.co.uk/test2", "http://www.bbc.co.uk/really/really/long/test2/subdir", "http://www.bbc.co.uk/test2/subdir", "http://www.bbc.co.uk/really/really/test2", "http://www.bbc.co.uk/really/really/long/path#fragment2" },
                                    new String[] { "http://www.bbc.co.uk/test2.txt",                    "http://www.bbc.co.uk/test2",                    "http://www.bbc.co.uk/test2", "http://www.bbc.co.uk/test2/subdir",                    "http://www.bbc.co.uk/test2/subdir", "http://www.bbc.co.uk/test2",               "http://www.bbc.co.uk/#fragment2"                        },
                                    new String[] { "http://www.bbc.co.uk/test2.txt",                    "http://www.bbc.co.uk/test2",                    "http://www.bbc.co.uk/test2", "http://www.bbc.co.uk/test2/subdir",                    "http://www.bbc.co.uk/test2/subdir", "http://www.bbc.co.uk/test2",               "http://www.bbc.co.uk/test.txt#fragment2"                } };

            for (int i = 0; i < baseUris.Length; i++)
            {
                Console.WriteLine("Resolving against Base URI " + baseUris[i]);

                Uri baseUri = new Uri(baseUris[i]);

                for (int j = 0; j < uriRefs.Length; j++)
                {
                    Console.WriteLine("Resolving " + uriRefs[j]);

                    String result, expectedResult;
                    result         = Tools.ResolveUri(uriRefs[j], baseUris[i]);
                    expectedResult = expected[i][j];

                    Console.WriteLine("Expected: " + expectedResult);
                    Console.WriteLine("Actual: " + result);

                    Assert.Equal(expectedResult, result);
                }

                Console.WriteLine();
            }

            Uri mailto = new Uri("mailto:[email protected]");
            Uri rel    = new Uri("/some/folder", UriKind.Relative);
            Uri res    = new Uri(mailto, rel);

            Console.WriteLine(res.ToString());
        }
Beispiel #3
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);
                }
                catch (UriFormatException)
                {
                    throw new RdfParseException("Cannot resolve a Relative URI Reference since there is no in-scope Base URI!");
                }
            }
        }
Beispiel #4
0
 /// <summary>
 /// Resolves a QName/Uri into a Uri using the Namespace Mapper and Base Uri provided
 /// </summary>
 /// <param name="t">QName/Uri to resolve</param>
 /// <param name="nsmap">Namespace Map to resolve against</param>
 /// <param name="baseUri">Base Uri to resolve against</param>
 /// <returns></returns>
 public static String ResolveUriOrQName(IToken t, INamespaceMapper nsmap, Uri baseUri)
 {
     if (t.TokenType == Token.QNAME)
     {
         return(Tools.ResolveQName(t.Value, nsmap, baseUri));
     }
     else if (t.TokenType == Token.URI)
     {
         String uriBase = (baseUri == null) ? String.Empty : baseUri.ToString();
         return(Tools.ResolveUri(t.Value, uriBase));
     }
     else
     {
         throw new RdfParseException("Unable to resolve a '" + t.GetType().ToString() + "' Token into a URI", t);
     }
 }
Beispiel #5
0
 /// <summary>
 /// Creates a new URI Node that refers to the Base Uri of the Graph.
 /// </summary>
 /// <returns></returns>
 public virtual IUriNode CreateUriNode()
 {
     return(new UriNode(this, UriFactory.Create(Tools.ResolveUri(String.Empty, _baseuri.ToSafeString()))));
 }