示例#1
0
        /// <summary>
        /// Attempts to load a RDF Graph from a URI asynchronously
        /// </summary>
        /// <param name="g">Graph to assert triple in</param>
        /// <param name="u">URI to load from</param>
        /// <param name="parser">Parser to use</param>
        /// <param name="callback">Callback to invoke when the operation completes</param>
        /// <param name="state">State to pass to the callback</param>
        /// <remarks>
        /// <para>
        /// Uses the supplied parser to attempt parsing regardless of the actual Content Type returned
        /// </para>
        /// <para>
        /// In the event that the URI is a File URI the <see cref="FileLoader">FileLoader</see> will be used instead. If the URI is a Data URI then the <see cref="DataUriLoader">DataUriLoader</see> will be used instead.
        /// </para>
        /// <para>
        /// <strong>Note:</strong> UriLoader will assign the Graph the source URI as it's Base URI unless the Graph already has a Base URI or is non-empty prior to attempting parsing.  Note that any Base URI specified in the RDF contained in the file will override this initial Base URI.  In some cases this may lead to invalid RDF being accepted and generating strange relative URIs, if you encounter this either set a Base URI prior to calling this method or create an instance of the relevant parser and invoke it directly.
        /// </para>
        /// <para>
        /// If the loading completes normally the callback will be invoked normally, if an error occurs it will be invoked and passed an instance of <see cref="AsyncError"/> as the state which contains details of the error and the original state.
        /// </para>
        /// </remarks>
        public static void Load(IGraph g, Uri u, IRdfReader parser, GraphCallback callback, Object state)
        {
            if (g == null)
            {
                throw new RdfParseException("Cannot read RDF into a null Graph");
            }
            if (u == null)
            {
                throw new RdfParseException("Cannot read RDF from a null URI");
            }

#if SILVERLIGHT
            if (u.IsFile())
#else
            if (u.IsFile)
#endif
            {
#if PORTABLE
                throw new PlatformNotSupportedException("FileLoader is not supported by the Portable Class Library build");
#else
                //Invoke FileLoader instead
                UriLoader.RaiseWarning("This is a file: URI so invoking the FileLoader instead");
                if (Path.DirectorySeparatorChar == '/')
                {
                    FileLoader.Load(g, u.AbsoluteUri.Substring(7), parser);
                }
                else
                {
                    FileLoader.Load(g, u.AbsoluteUri.Substring(8), parser);
                }
                //FileLoader.Load() will run synchronously so once this completes we can invoke the callback
                callback(g, state);
                return;
#endif
            }
            if (u.Scheme.Equals("data"))
            {
                //Invoke DataUriLoader instead
                RaiseWarning("This is a data: URI so invoking the DataUriLoader instead");
                DataUriLoader.Load(g, u);
                //After DataUriLoader.Load() has run (which happens synchronously) can invoke the callback
                callback(g, state);
                return;
            }

            //Set Base URI if necessary
            if (g.BaseUri == null && g.IsEmpty)
            {
                g.BaseUri = u;
            }

            UriLoader.Load(new GraphHandler(g), u, parser, (_, s) => callback(g, s), state);
        }
示例#2
0
 /// <summary>
 /// Attempts to load a RDF dataset asynchronously from the given URI into the given Triple Store
 /// </summary>
 /// <param name="store">Triple Store to load into</param>
 /// <param name="u">URI to attempt to get a RDF dataset from</param>
 /// <param name="parser">Parser to use to parse the RDF dataset</param>
 /// <param name="callback">Callback to invoke when the operation completes</param>
 /// <param name="state">State to pass to the callback</param>
 /// <remarks>
 /// <para>
 /// If the <paramref name="parser"/> parameter is set to null then this method attempts to select the relevant Store Parser based on the Content Type header returned in the HTTP Response.
 /// </para>
 /// <para>
 /// If you know ahead of time the Content Type you can explicitly pass in the parser to use.
 /// </para>
 /// <para>
 /// If the loading completes normally the callback will be invoked normally, if an error occurs it will be invoked and passed an instance of <see cref="AsyncError"/> as the state which contains details of the error and the original state.
 /// </para>
 /// </remarks>
 public static void Load(ITripleStore store, Uri u, IStoreReader parser, TripleStoreCallback callback, Object state)
 {
     if (store == null)
     {
         throw new RdfParseException("Cannot read a RDF dataset into a null Triple Store");
     }
     if (u == null)
     {
         throw new RdfParseException("Cannot read a RDF dataset from a null URI");
     }
     UriLoader.Load(new StoreHandler(store), u, parser, (_, s) => callback(store, s), state);
 }
示例#3
0
 /// <summary>
 /// Attempts to load a RDF dataset from the given URI into the given Triple Store
 /// </summary>
 /// <param name="store">Triple Store to load into</param>
 /// <param name="u">URI to attempt to get a RDF dataset from</param>
 /// <param name="parser">Parser to use to parse the RDF dataset</param>
 /// <remarks>
 /// <para>
 /// If the <paramref name="parser"/> parameter is set to null then this method attempts to select the relevant Store Parser based on the Content Type header returned in the HTTP Response.
 /// </para>
 /// <para>
 /// If you know ahead of time the Content Type you can explicitly pass in the parser to use.
 /// </para>
 /// </remarks>
 public static void Load(ITripleStore store, Uri u, IStoreReader parser)
 {
     if (store == null)
     {
         throw new RdfParseException("Cannot read a RDF dataset into a null Triple Store");
     }
     if (u == null)
     {
         throw new RdfParseException("Cannot read a RDF dataset from a null URI");
     }
     UriLoader.Load(new StoreHandler(store), u, parser);
 }
        public void ParsingUriLoaderCache()
        {
            //Load the Graph
            Graph g = new Graph();

            UriLoader.Load(g, test);

            //Then reload the Graph which it should now come from the cache instead
            Graph h = new Graph();

            UriLoader.Load(h, test);

            Assert.Equal(g, h);
        }
        public void ParsingTextReaderBlockingNetworkStreamTurtle()
        {
            try
            {
                Options.UriLoaderCaching = false;

                Graph g = new Graph();
                UriLoader.Load(g, new Uri("http://www.dotnetrdf.org/configuration#"), new TurtleParser());

                TestTools.ShowGraph(g);
            }
            finally
            {
                Options.UriLoaderCaching = true;
            }
        }
        public void ParsingTextReaderBlockingNetworkStreamRdfJson()
        {
            try
            {
                SetUriLoaderCaching(false);

                Graph g = new Graph();
                UriLoader.Load(g, new Uri("http://www.dotnetrdf.org/configuration#"), new RdfJsonParser());

                TestTools.ShowGraph(g);
            }
            finally
            {
                SetUriLoaderCaching(true);
            }
        }
示例#7
0
        public void ParsingUriLoaderDBPedia2()
        {
            Skip.IfNot(TestConfigManager.GetSettingAsBoolean(TestConfigManager.UseRemoteParsing), "Test Config marks Remote Parsing as unavailable, test cannot be run");

            IGraph g = new Graph();

            UriLoader.Load(g, new Uri("http://de.dbpedia.org/resource/Disillusion"));

            INodeFormatter formatter = new TurtleW3CFormatter();

            foreach (INode p in g.Triples.Select(t => t.Predicate).Distinct())
            {
                Console.WriteLine("ToString() = " + p.ToString());
                Console.WriteLine("Formatted = " + p.ToString(formatter));
                Console.WriteLine("URI ToString() = " + ((IUriNode)p).Uri.ToString());
            }
        }
示例#8
0
        /// <summary>
        /// Attempts to load a RDF Graph from the given URI into the given Graph
        /// </summary>
        /// <param name="g">Graph to assert Triples in</param>
        /// <param name="u">URI to attempt to get RDF from</param>
        /// <param name="parser">Parser to use</param>
        /// <remarks>
        /// <para>
        /// Uses the supplied parser to attempt parsing regardless of the actual Content Type returned
        /// </para>
        /// <para>
        /// In the event that the URI is a File URI the <see cref="FileLoader">FileLoader</see> will be used instead
        /// </para>
        /// <para>
        /// If the URI is a Data URI then the <see cref="DataUriLoader">DataUriLoader</see> will be used instead.
        /// </para>
        /// </remarks>
        public static void Load(IGraph g, Uri u, IRdfReader parser)
        {
            if (g == null)
            {
                throw new RdfParseException("Cannot read RDF into a null Graph");
            }
            if (u == null)
            {
                throw new RdfParseException("Cannot load RDF from a null URI");
            }
#if SILVERLIGHT
            if (u.IsFile())
#else
            if (u.IsFile)
#endif
            {
                //Invoke FileLoader instead
                RaiseWarning("This is a file: URI so invoking the FileLoader instead");
                if (Path.DirectorySeparatorChar == '/')
                {
                    FileLoader.Load(g, u.ToString().Substring(7), parser);
                }
                else
                {
                    FileLoader.Load(g, u.ToString().Substring(8), parser);
                }
                return;
            }
            if (u.Scheme.Equals("data"))
            {
                //Invoke DataUriLoader instead
                RaiseWarning("This is a data: URI so invoking the DataUriLoader instead");
                DataUriLoader.Load(g, u);
                return;
            }

            //Set Base Uri if necessary
            if (g.BaseUri == null && g.IsEmpty)
            {
                g.BaseUri = u;
            }

            UriLoader.Load(new GraphHandler(g), u, parser);
        }
        public void ParsingBaseUriAssignmentUriLoader()
        {
            int defaultTimeout = Options.UriLoaderTimeout;

            try
            {
                //DBPedia can be slow so up the timeout for this test
                Options.UriLoaderTimeout = 45000;
                Graph g = new Graph();
                UriLoader.Load(g, new Uri("http://dbpedia.org/resource/Ilkeston"));
                Console.WriteLine("Base URI: " + ShowBaseUri(g.BaseUri));
                Assert.IsNotNull(g.BaseUri, "Base URI should not be null");
            }
            finally
            {
                //Remember to reset timeout afterwards
                Options.UriLoaderTimeout = defaultTimeout;
            }
        }
        public void ParsingTextReaderBlockingNetworkStreamNotation3()
        {
            int defaultTimeout = Options.UriLoaderTimeout;

            try
            {
                SetUriLoaderCaching(false);
                Options.UriLoaderTimeout = 45000;

                Graph g = new Graph();
                UriLoader.Load(g, new Uri("http://www.dotnetrdf.org/configuration#"), new Notation3Parser());

                TestTools.ShowGraph(g);
            }
            finally
            {
                SetUriLoaderCaching(true);
                Options.UriLoaderTimeout = defaultTimeout;
            }
        }
        public void ParsingTextReaderBlockingNetworkStreamNotation3()
        {
            int defaultTimeout = Options.UriLoaderTimeout;

            try
            {
                Options.UriLoaderCaching = false;
                Options.UriLoaderTimeout = 45000;

                Graph g = new Graph();
                UriLoader.Load(g, new Uri("http://dbpedia.org/resource/Southampton"), new Notation3Parser());

                TestTools.ShowGraph(g);
            }
            finally
            {
                Options.UriLoaderCaching = true;
                Options.UriLoaderTimeout = defaultTimeout;
            }
        }
        public void ParsingBaseUriAssignmentUriLoader()
        {
            Skip.IfNot(TestConfigManager.GetSettingAsBoolean(TestConfigManager.UseRemoteParsing), "Test Config marks Remote Parsing as unavailable, test cannot be run");

            int defaultTimeout = Options.UriLoaderTimeout;

            try
            {
                //DBPedia can be slow so up the timeout for this test
                Options.UriLoaderTimeout = 45000;
                Graph g = new Graph();
                UriLoader.Load(g, new Uri("http://dbpedia.org/resource/Ilkeston"));
                Console.WriteLine("Base URI: " + ShowBaseUri(g.BaseUri));
                Assert.NotNull(g.BaseUri);
            }
            finally
            {
                //Remember to reset timeout afterwards
                Options.UriLoaderTimeout = defaultTimeout;
            }
        }
        public void ParsingUriLoaderUriSantisation()
        {
            Uri a = new Uri(ConfigurationLoader.ClassTripleStore);
            Uri b = new Uri(ConfigurationLoader.ClassGraph);

            Console.WriteLine("URI A: " + a.AbsoluteUri + " is equivalent to " + Tools.StripUriFragment(a).AbsoluteUri);
            Console.WriteLine("URI B:" + b.AbsoluteUri + " is equivalent to " + Tools.StripUriFragment(b).AbsoluteUri);

            Assert.Equal(Tools.StripUriFragment(a).AbsoluteUri, Tools.StripUriFragment(b).AbsoluteUri);

            Graph g = new Graph();

            UriLoader.Load(g, a);

            Assert.True(UriLoader.IsCached(a), "Content should have been cached as a result of loading from the URI");

            Graph h = new Graph();

            UriLoader.Load(h, b);

            Assert.Equal(g, h);
        }
示例#14
0
        public void ParsingUriLoaderResponseUriCaching()
        {
            int defaultTimeout = Options.UriLoaderTimeout;

            try
            {
                Options.UriLoaderTimeout = 45000;
                Uri soton     = new Uri("http://dbpedia.org/resource/Southampton");
                Uri sotonPage = new Uri("http://dbpedia.org/page/Southampton.html");
                Uri sotonData = new Uri("http://dbpedia.org/data/Southampton.xml");

                Graph g = new Graph();
                UriLoader.Load(g, soton);

                Assert.IsTrue(UriLoader.IsCached(soton), "Resource URI should have been cached");
                Assert.IsTrue(UriLoader.IsCached(sotonData), "Data URI should have been cached");
                Assert.IsFalse(UriLoader.IsCached(sotonPage), "Page URI should not have been cached");
            }
            finally
            {
                Options.UriLoaderTimeout = defaultTimeout;
            }
        }
示例#15
0
        public void ParsingUriLoaderDBPedia3()
        {
            Skip.IfNot(TestConfigManager.GetSettingAsBoolean(TestConfigManager.UseRemoteParsing), "Test Config marks Remote Parsing as unavailable, test cannot be run");

            int defaultTimeout = Options.UriLoaderTimeout;

            try
            {
                Options.HttpDebugging = true;
                SetUriLoaderCaching(false);
                Options.UriLoaderTimeout = 45000;

                Graph g = new Graph();
                UriLoader.Load(g, new Uri("http://dbpedia.org/ontology/wikiPageRedirects"), new RdfXmlParser());
                Assert.False(g.IsEmpty, "Graph should not be empty");
                TestTools.ShowGraph(g);
            }
            finally
            {
                Options.HttpDebugging = false;
                SetUriLoaderCaching(true);
                Options.UriLoaderTimeout = defaultTimeout;
            }
        }
示例#16
0
 /// <summary>
 /// Attempts to load a RDF dataset from the given URI using a RDF Handler
 /// </summary>
 /// <param name="handler">RDF Handler to use</param>
 /// <param name="u">URI to attempt to get a RDF dataset from</param>
 /// <remarks>
 /// <para>
 /// Attempts to select the relevant Store Parser based on the Content Type header returned in the HTTP Response.
 /// </para>
 /// </remarks>
 public static void LoadDataset(IRdfHandler handler, Uri u)
 {
     UriLoader.Load(handler, u, (IStoreReader)null);
 }
示例#17
0
 /// <summary>
 /// Attempts to load a RDF dataset from the given URI into the given Triple Store
 /// </summary>
 /// <param name="store">Triple Store to load into</param>
 /// <param name="u">URI to attempt to get a RDF dataset from</param>
 /// <remarks>
 /// <para>
 /// Attempts to select the relevant Store Parser based on the Content Type header returned in the HTTP Response.
 /// </para>
 /// </remarks>
 public static void Load(ITripleStore store, Uri u)
 {
     UriLoader.Load(store, u, null);
 }
示例#18
0
        /// <summary>
        /// Attempts to load a RDF Graph from the given URI using a RDF Handler
        /// </summary>
        /// <param name="handler">RDF Handler to use</param>
        /// <param name="u">URI to attempt to get RDF from</param>
        /// <param name="parser">Parser to use</param>
        /// <remarks>
        /// <para>
        /// Uses the supplied parser to attempt parsing regardless of the actual Content Type returned
        /// </para>
        /// <para>
        /// In the event that the URI is a File URI the <see cref="FileLoader">FileLoader</see> will be used instead
        /// </para>
        /// <para>
        /// If the URI is a Data URI then the <see cref="DataUriLoader">DataUriLoader</see> will be used instead.
        /// </para>
        /// </remarks>
        public static void Load(IRdfHandler handler, Uri u, IRdfReader parser)
        {
            if (handler == null)
            {
                throw new RdfParseException("Cannot read RDF using a null RDF Handler");
            }
            if (u == null)
            {
                throw new RdfParseException("Cannot load RDF from a null URI");
            }
            try
            {
#if SILVERLIGHT
                if (u.IsFile())
#else
                if (u.IsFile)
#endif
                {
                    //Invoke FileLoader instead
                    RaiseWarning("This is a file: URI so invoking the FileLoader instead");
                    if (Path.DirectorySeparatorChar == '/')
                    {
                        FileLoader.Load(handler, u.ToString().Substring(7), parser);
                    }
                    else
                    {
                        FileLoader.Load(handler, u.ToString().Substring(8), parser);
                    }
                    return;
                }
                if (u.Scheme.Equals("data"))
                {
                    //Invoke DataUriLoader instead
                    RaiseWarning("This is a data: URI so invoking the DataUriLoader instead");
                    DataUriLoader.Load(handler, u);
                    return;
                }

                //Sanitise the URI to remove any Fragment ID
                u = Tools.StripUriFragment(u);

#if !NO_URICACHE
                //Use Cache if possible
                String etag  = String.Empty;
                String local = null;
                if (Options.UriLoaderCaching)
                {
                    if (_cache.HasETag(u))
                    {
                        //Get the ETag and then we'll include an If-None-Match header in our request
                        etag = _cache.GetETag(u);
                    }
                    else if (_cache.HasLocalCopy(u, true))
                    {
                        //Just try loading from the local copy
                        local = _cache.GetLocalCopy(u);
                        if (local != null)
                        {
                            try
                            {
                                FileLoader.Load(handler, local, new TurtleParser());
                            }
                            catch
                            {
                                //If we get an Exception we failed to access the file successfully
                                _cache.RemoveETag(u);
                                _cache.RemoveLocalCopy(u);
                                UriLoader.Load(handler, u, parser);
                            }
                            return;
                        }
                    }
                }
#endif

                //Set-up the Request
                HttpWebRequest httpRequest;
                httpRequest = (HttpWebRequest)WebRequest.Create(u);

                //Want to ask for RDF formats
                if (parser != null)
                {
                    //If a non-null parser set up a HTTP Header that is just for the given parser
                    httpRequest.Accept = MimeTypesHelper.CustomHttpAcceptHeader(parser);
                }
                else
                {
                    httpRequest.Accept = MimeTypesHelper.HttpAcceptHeader;
                }

#if !NO_URICACHE
                if (Options.UriLoaderCaching)
                {
                    if (!etag.Equals(String.Empty))
                    {
                        httpRequest.Headers.Add(HttpRequestHeader.IfNoneMatch, etag);
                    }
                }
#endif

                //Use HTTP GET
                httpRequest.Method = "GET";
#if !SILVERLIGHT
                httpRequest.Timeout = Options.UriLoaderTimeout;
#endif
                if (_userAgent != null && !_userAgent.Equals(String.Empty))
                {
                    httpRequest.UserAgent = _userAgent;
                }
#if DEBUG
                //HTTP Debugging
                if (Options.HttpDebugging)
                {
                    Tools.HttpDebugRequest(httpRequest);
                }
#endif

                using (HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse())
                {
#if DEBUG
                    //HTTP Debugging
                    if (Options.HttpDebugging)
                    {
                        Tools.HttpDebugResponse(httpResponse);
                    }
#endif

#if !NO_URICACHE
                    if (Options.UriLoaderCaching)
                    {
                        //Are we using ETag based caching?
                        if (!etag.Equals(String.Empty))
                        {
                            //Did we get a Not-Modified response?
                            if (httpResponse.StatusCode == HttpStatusCode.NotModified)
                            {
                                //If so then we need to load the Local Copy assuming it exists?
                                if (_cache.HasLocalCopy(u, false))
                                {
                                    local = _cache.GetLocalCopy(u);
                                    try
                                    {
                                        FileLoader.Load(handler, local, new TurtleParser());
                                    }
                                    catch
                                    {
                                        //If we get an Exception we failed to access the file successfully
                                        _cache.RemoveETag(u);
                                        _cache.RemoveLocalCopy(u);
                                        UriLoader.Load(handler, u, parser);
                                    }
                                    return;
                                }
                                else
                                {
                                    //If the local copy didn't exist then we need to redo the response without
                                    //the ETag as we've lost the cached copy somehow
                                    _cache.RemoveETag(u);
                                    UriLoader.Load(handler, u, parser);
                                    return;
                                }
                            }
                            //If we didn't get a Not-Modified response then we'll continue and parse the new response
                        }
                    }
#endif

                    //Get a Parser and Load the RDF
                    if (parser == null)
                    {
                        //Only need to auto-detect the parser if a specific one wasn't specified
                        parser = MimeTypesHelper.GetParser(httpResponse.ContentType);
                    }
                    parser.Warning += RaiseWarning;
#if !NO_URICACHE
                    //To do caching we ask the cache to give us a handler and then we tie it to
                    IRdfHandler cacheHandler = _cache.ToCache(u, Tools.StripUriFragment(httpResponse.ResponseUri), httpResponse.Headers["ETag"]);
                    if (cacheHandler != null)
                    {
                        //Note: We can ONLY use caching when we know that the Handler will accept all the data returned
                        //i.e. if the Handler may trim the data in some way then we shouldn't cache the data returned
                        if (handler.AcceptsAll)
                        {
                            handler = new MultiHandler(new IRdfHandler[] { handler, cacheHandler });
                        }
                        else
                        {
                            cacheHandler = null;
                        }
                    }
                    try
                    {
#endif
                    parser.Load(handler, new StreamReader(httpResponse.GetResponseStream()));

#if !NO_URICACHE
                }
                catch
                {
                    //If we were trying to cache the response and something went wrong discard the cached copy
                    _cache.RemoveETag(u);
                    _cache.RemoveETag(Tools.StripUriFragment(httpResponse.ResponseUri));
                    _cache.RemoveLocalCopy(u);
                    _cache.RemoveLocalCopy(Tools.StripUriFragment(httpResponse.ResponseUri));
                }
#endif
                }
            }
            catch (UriFormatException uriEx)
            {
                //Uri Format Invalid
                throw new RdfParseException("Unable to load from the given URI '" + u.ToString() + "' since it's format was invalid", uriEx);
            }
            catch (WebException webEx)
            {
#if DEBUG
                if (webEx.Response != null && Options.HttpDebugging)
                {
                    Tools.HttpDebugResponse((HttpWebResponse)webEx.Response);
                }
#endif

#if !NO_URICACHE
                if (webEx.Response != null)
                {
                    if (((HttpWebResponse)webEx.Response).StatusCode == HttpStatusCode.NotModified)
                    {
                        //If so then we need to load the Local Copy assuming it exists?
                        if (_cache.HasLocalCopy(u, false))
                        {
                            String local = _cache.GetLocalCopy(u);
                            try
                            {
                                FileLoader.Load(handler, local, new TurtleParser());
                            }
                            catch
                            {
                                //If we get an Exception we failed to access the file successfully
                                _cache.RemoveETag(u);
                                _cache.RemoveLocalCopy(u);
                                UriLoader.Load(handler, u, parser);
                            }
                            return;
                        }
                        else
                        {
                            //If the local copy didn't exist then we need to redo the response without
                            //the ETag as we've lost the cached copy somehow
                            _cache.RemoveETag(u);
                            UriLoader.Load(handler, u, parser);
                            return;
                        }
                    }
                }
#endif

                //Some sort of HTTP Error occurred
                throw new WebException("A HTTP Error occurred resolving the URI '" + u.ToString() + "'", webEx);
            }
        }
示例#19
0
 /// <summary>
 /// Attempts to load a RDF Graph from the given URI using a RDF Handler
 /// </summary>
 /// <param name="handler">RDF Handler to use</param>
 /// <param name="u">URI to attempt to get RDF from</param>
 /// <remarks>
 /// <para>
 /// Attempts to select the relevant Parser based on the Content Type header returned in the HTTP Response.
 /// </para>
 /// <para>
 /// If you know ahead of time the Content Type you can just open a HTTP Stream yourself and pass it to an instance of the correct Parser.
 /// </para>
 /// <para>
 /// In the event that the URI is a File URI the <see cref="FileLoader">FileLoader</see> will be used instead.  If the URI is a Data URI then the <see cref="DataUriLoader">DataUriLoader</see> will be used instead.
 /// </para>
 /// </remarks>
 public static void Load(IRdfHandler handler, Uri u)
 {
     UriLoader.Load(handler, u, (IRdfReader)null);
 }
示例#20
0
 /// <summary>
 /// Attempts to load a RDF Graph from a URI asynchronously
 /// </summary>
 /// <param name="g">Graph to assert triple in</param>
 /// <param name="u">URI to load from</param>
 /// <param name="callback">Callback to invoke when the operation completes</param>
 /// <param name="state">State to pass to the callback</param>
 /// <remarks>
 /// <para>
 /// Will attempt to autodetect the format of the RDF based on the Content-Type header of the HTTP response
 /// </para>
 /// <para>
 /// In the event that the URI is a File URI the <see cref="FileLoader">FileLoader</see> will be used instead. If the URI is a Data URI then the <see cref="DataUriLoader">DataUriLoader</see> will be used instead.
 /// </para>
 /// <para>
 /// <strong>Note:</strong> UriLoader will assign the Graph the source URI as it's Base URI unless the Graph already has a Base URI or is non-empty prior to attempting parsing.  Note that any Base URI specified in the RDF contained in the file will override this initial Base URI.  In some cases this may lead to invalid RDF being accepted and generating strange relative URIs, if you encounter this either set a Base URI prior to calling this method or create an instance of the relevant parser and invoke it directly.
 /// </para>
 /// <para>
 /// If the loading completes normally the callback will be invoked normally, if an error occurs it will be invoked and passed an instance of <see cref="AsyncError"/> as the state which contains details of the error and the original state.
 /// </para>
 /// </remarks>
 public static void Load(IGraph g, Uri u, GraphCallback callback, Object state)
 {
     UriLoader.Load(g, u, null, callback, state);
 }
示例#21
0
 /// <summary>
 /// Attempts to load a RDF Graph from the given URI into the given Graph
 /// </summary>
 /// <param name="g">Graph to assert Triples in</param>
 /// <param name="u">URI to attempt to get RDF from</param>
 /// <remarks>
 /// <para>
 /// Attempts to select the relevant Parser based on the Content Type header returned in the HTTP Response.
 /// </para>
 /// <para>
 /// If you know ahead of time the Content Type you can just open a HTTP Stream yourself and pass it to an instance of the correct Parser.
 /// </para>
 /// <para>
 /// In the event that the URI is a File URI the <see cref="FileLoader">FileLoader</see> will be used instead.  If the URI is a Data URI then the <see cref="DataUriLoader">DataUriLoader</see> will be used instead.
 /// </para>
 /// </remarks>
 public static void Load(IGraph g, Uri u)
 {
     UriLoader.Load(g, u, null);
 }
示例#22
0
 /// <summary>
 /// Attempts to load a RDF dataset asynchronously from the given URI into the given Triple Store
 /// </summary>
 /// <param name="store">Triple Store to load into</param>
 /// <param name="u">URI to attempt to get a RDF dataset from</param>
 /// <param name="callback">Callback to invoke when the operation completes</param>
 /// <param name="state">State to pass to the callback</param>
 /// <remarks>
 /// <para>
 /// Attempts to select the relevant Store Parser based on the Content Type header returned in the HTTP Response.
 /// </para>
 /// <para>
 /// If the loading completes normally the callback will be invoked normally, if an error occurs it will be invoked and passed an instance of <see cref="AsyncError"/> as the state which contains details of the error and the original state.
 /// </para>
 /// </remarks>
 public static void Load(ITripleStore store, Uri u, TripleStoreCallback callback, Object state)
 {
     UriLoader.Load(store, u, null, callback, state);
 }
示例#23
0
 /// <summary>
 /// Attempts to load a RDF dataset asynchronously from the given URI using a RDF Handler
 /// </summary>
 /// <param name="handler">RDF Handler to use</param>
 /// <param name="u">URI to attempt to get a RDF dataset from</param>
 /// <param name="callback">Callback to invoke when the operation completes</param>
 /// <param name="state">State to pass to the callback</param>
 /// <remarks>
 /// <para>
 /// Attempts to select the relevant Store Parser based on the Content Type header returned in the HTTP Response.
 /// </para>
 /// <para>
 /// If the loading completes normally the callback will be invoked normally, if an error occurs it will be invoked and passed an instance of <see cref="AsyncError"/> as the state which contains details of the error and the original state.
 /// </para>
 /// </remarks>
 public static void LoadDataset(IRdfHandler handler, Uri u, RdfHandlerCallback callback, Object state)
 {
     UriLoader.Load(handler, u, (IStoreReader)null, callback, state);
 }
示例#24
0
        public void ParsingDBPedia()
        {
            if (!TestConfigManager.GetSettingAsBoolean(TestConfigManager.UseRemoteParsing))
            {
                throw new SkipTestException("Test Config marks Remote Parsing as unavailable, test cannot be run");
            }

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://dbpedia.org/resource/London");

            request.Accept = "application/rdf+xml";
            request.Method = "GET";
#if NET40
            request.Timeout = 45000;
#endif

            try
            {
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    Console.WriteLine("OK");
                    Console.WriteLine("Content Length: " + response.ContentLength);
                    Console.WriteLine("Content Type: " + response.ContentType);
                    Tools.HttpDebugRequest(request);
                    Tools.HttpDebugResponse(response);
                }
            }
            catch (WebException webEx)
            {
                Console.WriteLine("ERROR");
                Console.WriteLine(webEx.Message);
                Console.WriteLine(webEx.StackTrace);
                Assert.True(false);
            }

            request        = (HttpWebRequest)WebRequest.Create("http://dbpedia.org/data/London");
            request.Accept = "application/rdf+xml";
            request.Method = "GET";
#if NET40
            request.Timeout = 45000;
#endif

            try
            {
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    Console.WriteLine("OK");
                    Console.WriteLine("Content Length: " + response.ContentLength);
                    Console.WriteLine("Content Type: " + response.ContentType);
                    Tools.HttpDebugRequest(request);
                    Tools.HttpDebugResponse(response);
                }
            }
            catch (WebException webEx)
            {
                Console.WriteLine("ERROR");
                Console.WriteLine(webEx.Message);
                Console.WriteLine(webEx.StackTrace);
                Assert.True(false);
            }

            try
            {
                Graph g = new Graph();
                Options.HttpDebugging = true;
                UriLoader.Load(g, new Uri("http://dbpedia.org/resource/London"));
                Console.WriteLine("OK");
                Console.WriteLine(g.Triples.Count + " Triples retrieved");
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR");
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
                Assert.True(false);
            }
            finally
            {
                Options.HttpDebugging = false;
            }
        }
示例#25
0
        /// <summary>
        /// Loads RDF data using an RDF Handler from a data: URI
        /// </summary>
        /// <param name="handler">RDF Handler</param>
        /// <param name="u">URI to load from</param>
        /// <remarks>
        /// Invokes the normal <see cref="UriLoader">UriLoader</see> instead if a the URI provided is not a data: URI
        /// </remarks>
        /// <exception cref="UriFormatException">Thrown if the metadata portion of the URI which indicates the MIME Type, Character Set and whether Base64 encoding is used is malformed</exception>
        public static void Load(IRdfHandler handler, Uri u)
        {
            if (u == null)
            {
                throw new RdfParseException("Cannot load RDF from a null URI");
            }
            if (handler == null)
            {
                throw new RdfParseException("Cannot read RDF into a null RDF Handler");
            }

            if (!u.Scheme.Equals("data"))
            {
                // Invoke the normal URI Loader if not a data: URI
                UriLoader.Load(handler, u);
                return;
            }

            String mimetype = "text/plain";
            bool   mimeSet  = false;
            bool   base64   = false;

            String[] uri      = u.AbsolutePath.Split(',');
            String   metadata = uri[0];
            String   data     = uri[1];

            // Process the metadata
            if (metadata.Equals(String.Empty))
            {
                // Nothing to do
            }
            else if (metadata.Contains(';'))
            {
                if (metadata.StartsWith(";"))
                {
                    metadata = metadata.Substring(1);
                }
                String[] meta = metadata.Split(';');
                for (int i = 0; i < meta.Length; i++)
                {
                    if (meta[i].StartsWith("charset="))
                    {
                        // OPT: Do we need to process the charset parameter here at all?
                        // String charset = meta[i].Substring(meta[i].IndexOf('=') + 1);
                    }
                    else if (meta[i].Equals("base64"))
                    {
                        base64 = true;
                    }
                    else if (meta[i].Contains('/'))
                    {
                        mimetype = meta[i];
                        mimeSet  = true;
                    }
                    else
                    {
                        throw new UriFormatException("This data: URI appears to be malformed as encountered the parameter value '" + meta[i] + "' in the metadata section of the URI");
                    }
                }
            }
            else
            {
                if (metadata.StartsWith("charset="))
                {
                    // OPT: Do we need to process the charset parameter here at all?
                }
                else if (metadata.Equals(";base64"))
                {
                    base64 = true;
                }
                else if (metadata.Contains('/'))
                {
                    mimetype = metadata;
                    mimeSet  = true;
                }
                else
                {
                    throw new UriFormatException("This data: URI appears to be malformed as encountered the parameter value '" + metadata + "' in the metadata section of the URI");
                }
            }

            // Process the data
            if (base64)
            {
                StringWriter temp = new StringWriter();
                foreach (byte b in Convert.FromBase64String(data))
                {
                    temp.Write((char)((int)b));
                }
                data = temp.ToString();
            }
            else
            {
                data = Uri.UnescapeDataString(data);
            }

            // Now either select a parser based on the MIME type or let StringParser guess the format
            try
            {
                if (mimeSet)
                {
                    // If the MIME Type was explicitly set then we'll try and get a parser and use it
                    IRdfReader reader = MimeTypesHelper.GetParser(mimetype);
                    reader.Load(handler, new StringReader(data));
                }
                else
                {
                    // If the MIME Type was not explicitly set we'll let the StringParser guess the format
                    IRdfReader reader = StringParser.GetParser(data);
                    reader.Load(handler, new StringReader(data));
                }
            }
            catch (RdfParserSelectionException)
            {
                // If we fail to get a parser then we'll let the StringParser guess the format
                IRdfReader reader = StringParser.GetParser(data);
                reader.Load(handler, new StringReader(data));
            }
        }
示例#26
0
        public void ParsingDBPedia()
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://dbpedia.org/resource/London");

            request.Accept  = "application/rdf+xml";
            request.Method  = "GET";
            request.Timeout = 45000;

            try
            {
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    Console.WriteLine("OK");
                    Console.WriteLine("Content Length: " + response.ContentLength);
                    Console.WriteLine("Content Type: " + response.ContentType);
                    Tools.HttpDebugRequest(request);
                    Tools.HttpDebugResponse(response);
                }
            }
            catch (WebException webEx)
            {
                Console.WriteLine("ERROR");
                Console.WriteLine(webEx.Message);
                Console.WriteLine(webEx.StackTrace);
                Assert.Fail();
            }

            request         = (HttpWebRequest)WebRequest.Create("http://dbpedia.org/data/London");
            request.Accept  = "application/rdf+xml";
            request.Method  = "GET";
            request.Timeout = 45000;

            try
            {
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    Console.WriteLine("OK");
                    Console.WriteLine("Content Length: " + response.ContentLength);
                    Console.WriteLine("Content Type: " + response.ContentType);
                    Tools.HttpDebugRequest(request);
                    Tools.HttpDebugResponse(response);
                }
            }
            catch (WebException webEx)
            {
                Console.WriteLine("ERROR");
                Console.WriteLine(webEx.Message);
                Console.WriteLine(webEx.StackTrace);
                Assert.Fail();
            }

            try
            {
                Graph g = new Graph();
                Options.HttpDebugging = true;
                UriLoader.Load(g, new Uri("http://dbpedia.org/resource/London"));
                Console.WriteLine("OK");
                Console.WriteLine(g.Triples.Count + " Triples retrieved");
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR");
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
                Assert.Fail();
            }
            finally
            {
                Options.HttpDebugging = false;
            }
        }
示例#27
0
        private bool ParseProfileAttribute(RdfACoreParserContext context, IRdfAEvent evt)
        {
            foreach (Uri u in this.ParseUris(context, evt["profile"]))
            {
                try
                {
                    Graph g = new Graph();
#if !SILVERLIGHT
                    UriLoader.Load(g, u);
#else
                    throw new PlatformNotSupportedException("The @profile attribute is not currently supported under Silverlight/Windows Phone 7");
#endif

                    String prefixQuery = "PREFIX rdfa: <" + RdfAParser.RdfANamespace + "> SELECT SAMPLE(?prefix) AS ?NamespacePrefix SAMPLE(?uri) AS ?NamespaceURI WHERE { ?s rdfa:prefix ?prefix ; rdfa:uri ?uri } GROUP BY ?s HAVING (COUNT(?prefix) = 1 && COUNT(?uri) = 1)";
                    String termQuery   = "PREFIX rdfa: <" + RdfAParser.RdfANamespace + "> SELECT SAMPLE(?term) AS ?Term SAMPLE(?uri) AS ?URI WHERE {?s rdfa:term ?term ; rdfa:uri ?uri } GROUP BY ?s HAVING (COUNT(?term) = 1 && COUNT(?uri) = 1)";

                    //Namespace Mappings
                    Object results = g.ExecuteQuery(prefixQuery);
                    if (results is SparqlResultSet)
                    {
                        SparqlResultSet rset = (SparqlResultSet)results;
                        foreach (SparqlResult r in rset.Results)
                        {
                            INode prefixNode = r["NamespacePrefix"];
                            INode nsNode     = r["NamespaceURI"];
                            if (prefixNode.NodeType == NodeType.Literal && nsNode.NodeType == NodeType.Literal)
                            {
                                String prefix = ((ILiteralNode)prefixNode).Value.ToLower();
                                String ns     = ((ILiteralNode)nsNode).Value;
                                context.Namespaces.AddNamespace(prefix, new Uri(ns));
                            }
                        }
                    }

                    //Term Mappings
                    results = g.ExecuteQuery(termQuery);
                    if (results is SparqlResultSet)
                    {
                        SparqlResultSet rset = (SparqlResultSet)results;
                        foreach (SparqlResult r in rset.Results)
                        {
                            INode termNode = r["Term"];
                            INode uriNode  = r["URI"];
                            if (termNode.NodeType == NodeType.Literal && uriNode.NodeType == NodeType.Literal)
                            {
                                String term = ((ILiteralNode)termNode).Value;
                                String uri  = ((ILiteralNode)uriNode).Value;
                                if (XmlSpecsHelper.IsNCName(term))
                                {
                                    context.Terms.AddNamespace(term, new Uri(uri));
                                }
                            }
                        }
                    }

                    //Vocabulary Setting
                    INode vocabNode = g.GetTriplesWithPredicate(g.CreateUriNode(new Uri(RdfAParser.RdfANamespace + "vocabulary"))).Select(t => t.Object).FirstOrDefault();
                    if (vocabNode != null)
                    {
                        if (vocabNode.NodeType == NodeType.Literal)
                        {
                            context.DefaultVocabularyUri = new Uri(((ILiteralNode)vocabNode).Value);
                        }
                        else if (vocabNode.NodeType == NodeType.Uri)
                        {
                            context.DefaultVocabularyUri = ((IUriNode)vocabNode).Uri;
                        }
                    }
                }
                catch
                {
                    return(false);
                }
            }
            return(true);
        }