Пример #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);
        }