Esempio n. 1
0
        /// <summary>
        /// Loads the contents of the given File using a RDF Handler providing the RDF dataset format can be determined
        /// </summary>
        /// <param name="handler">RDF Handler to use</param>
        /// <param name="filename">File to load from</param>
        /// <param name="parser">Parser to use to parse the given file</param>
        /// <remarks>
        /// <para>
        /// If the <paramref name="parser"/> parameter is set to null then the <see cref="FileLoader">FileLoader</see> attempts to select a Store Parser by examining the file extension to select the most likely MIME type for the file.  This assume that the file extension corresponds to one of the recognized file extensions for a RDF dataset format the library supports.  If this suceeds then a parser is chosen and used to parse the input file.
        /// </para>
        /// </remarks>
        public static void Load(IRdfHandler handler, String filename, IStoreReader parser)
        {
            if (handler == null)
            {
                throw new RdfParseException("Cannot read a RDF Dataset using a null RDF Handler");
            }
            if (filename == null)
            {
                throw new RdfParseException("Cannot read a RDF Dataset from a null File");
            }

            if (!File.Exists(filename))
            {
                ThrowNotFoundException(filename);
            }

            if (parser == null)
            {
                String ext = MimeTypesHelper.GetTrueFileExtension(filename);
                try
                {
                    parser = MimeTypesHelper.GetStoreParserByFileExtension(ext);
                }
                catch (RdfParserSelectionException)
                {
                    // If error then we couldn't determine MIME Type from the File Extension
                    RaiseWarning("Unable to select a dataset parser by determining MIME Type from the File Extension");

                    // Try selecting a RDF parser instead
                    try
                    {
                        IRdfReader rdfParser = MimeTypesHelper.GetParserByFileExtension(ext);
                        Graph      g         = new Graph();
                        rdfParser.Load(handler, filename);
                        return;
                    }
                    catch (RdfParserSelectionException)
                    {
                        // Ignore this, will try and use format guessing and assume is a dataset format
                    }
                }
            }
            if (parser == null)
            {
                // Unable to determine format from File Extension
                // Read file in locally and use the StringParser to select a parser
                StreamReader reader = new StreamReader(File.OpenRead(filename));
                String       data   = reader.ReadToEnd();
                reader.Close();
                parser = StringParser.GetDatasetParser(data);
                RaiseWarning("Used the StringParser to guess the parser to use - it guessed " + parser.GetType().Name);
                parser.Warning += RaiseStoreWarning;
                parser.Load(handler, new StringReader(data));
            }
            else
            {
                parser.Warning += RaiseStoreWarning;
                parser.Load(handler, filename);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Internal Helper method which does the actual loading of the Triple Store from the Resource
        /// </summary>
        /// <param name="handler">RDF Handler to use</param>
        /// <param name="asm">Assembly to get the resource stream from</param>
        /// <param name="resource">Full name of the Resource (without the Assembly Name)</param>
        /// <param name="parser">Parser to use (if null will be auto-selected)</param>
        private static void LoadDatasetInternal(IRdfHandler handler, Assembly asm, String resource, IStoreReader parser)
        {
            //Resource is in the given assembly
            using (Stream s = asm.GetManifestResourceStream(resource))
            {
                if (s == null)
                {
                    //Resource did not exist in this assembly
                    throw new RdfParseException("The Embedded Resource '" + resource + "' does not exist inside of " + GetAssemblyName(asm));
                }
                else
                {
                    //Resource exists
                    //Do we have a predefined Parser?
                    if (parser != null)
                    {
                        parser.Load(handler, new StreamReader(s));
                    }
                    else
                    {
                        //Need to select a Parser or use StringParser
                        String             ext = MimeTypesHelper.GetTrueResourceExtension(resource);
                        MimeTypeDefinition def = MimeTypesHelper.GetDefinitionsByFileExtension(ext).FirstOrDefault(d => d.CanParseRdfDatasets);
                        if (def != null)
                        {
                            //Resource has an appropriate file extension and we've found a candidate parser for it
                            parser = def.GetRdfDatasetParser();
                            parser.Load(handler, new StreamReader(s));
                        }
                        else
                        {
                            //See if the format was actually an RDF graph instead
                            def = MimeTypesHelper.GetDefinitionsByFileExtension(ext).FirstOrDefault(d => d.CanParseRdf);
                            if (def != null)
                            {
                                IRdfReader rdfParser = def.GetRdfParser();
                                rdfParser.Load(handler, new StreamReader(s));
                            }
                            else
                            {
                                //Resource did not have a file extension or we didn't have a parser associated with the extension
                                //Try using StringParser instead
                                String data;
                                using (StreamReader reader = new StreamReader(s))
                                {
                                    data = reader.ReadToEnd();
#if !PORTABLE
                                    reader.Close();
#endif
                                }
                                parser = StringParser.GetDatasetParser(data);
                                parser.Load(handler, new StringReader(data));
                            }
                        }
                    }
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Loads the contents of the given File using a RDF Handler providing the RDF dataset format can be determined
        /// </summary>
        /// <param name="handler">RDF Handler to use</param>
        /// <param name="filename">File to load from</param>
        /// <param name="parser">Parser to use to parse the given file</param>
        /// <remarks>
        /// <para>
        /// If the <paramref name="parser"/> parameter is set to null then the <see cref="FileLoader">FileLoader</see> attempts to select a Store Parser by examining the file extension to select the most likely MIME type for the file.  This assume that the file extension corresponds to one of the recognized file extensions for a RDF dataset format the library supports.  If this suceeds then a parser is chosen and used to parse the input file.
        /// </para>
        /// </remarks>
        public static void Load(IRdfHandler handler, String filename, IStoreReader parser)
        {
            if (handler == null)
            {
                throw new RdfParseException("Cannot read a RDF Dataset using a null RDF Handler");
            }
            if (filename == null)
            {
                throw new RdfParseException("Cannot read a RDF Dataset from a null File");
            }

            if (!File.Exists(filename))
            {
#if SILVERLIGHT
                throw new FileNotFoundException("Cannot read a RDF Dataset from the File '" + filename + "' since it doesn't exist");
#else
                throw new FileNotFoundException("Cannot read a RDF Dataset from a File that doesn't exist", filename);
#endif
            }

            if (parser == null)
            {
                try
                {
                    parser = MimeTypesHelper.GetStoreParser(MimeTypesHelper.GetMimeType(Path.GetExtension(filename)));
                }
                catch (RdfParserSelectionException)
                {
                    //If error then we couldn't determine MIME Type from the File Extension
                    RaiseWarning("Unable to select a parser by determining MIME Type from the File Extension");
                }
            }
            if (parser == null)
            {
                //Unable to determine format from File Extension
                //Read file in locally and use the StringParser to select a parser
                StreamReader reader = new StreamReader(filename);
                String       data   = reader.ReadToEnd();
                reader.Close();
                parser = StringParser.GetDatasetParser(data);
                RaiseWarning("Used the StringParser to guess the parser to use - it guessed " + parser.GetType().Name);
                parser.Warning += RaiseStoreWarning;
                parser.Load(handler, new TextReaderParams(new StringReader(data)));
            }
            else
            {
                parser.Warning += RaiseStoreWarning;
                parser.Load(handler, new StreamParams(filename));
            }
        }
Esempio n. 4
0
 /// <summary>
 /// Internal Helper method which does the actual loading of the Triple Store from the Resource
 /// </summary>
 /// <param name="handler">RDF Handler to use</param>
 /// <param name="asm">Assembly to get the resource stream from</param>
 /// <param name="resource">Full name of the Resource (without the Assembly Name)</param>
 /// <param name="parser">Parser to use (if null will be auto-selected)</param>
 private static void LoadDatasetInternal(IRdfHandler handler, Assembly asm, String resource, IStoreReader parser)
 {
     //Resource is in the given assembly
     using (Stream s = asm.GetManifestResourceStream(resource))
     {
         if (s == null)
         {
             //Resource did not exist in this assembly
             throw new RdfParseException("The Embedded Resource '" + resource + "' does not exist inside of " + asm.GetName().Name);
         }
         else
         {
             //Resource exists
             //Do we have a predefined Parser?
             if (parser != null)
             {
                 parser.Load(handler, new StreamParams(s));
             }
             else
             {
                 //Need to select a Parser or use StringParser
                 String             ext = resource.Substring(resource.LastIndexOf("."));
                 MimeTypeDefinition def = MimeTypesHelper.GetDefinitions(MimeTypesHelper.GetMimeTypes(ext)).FirstOrDefault(d => d.CanParseRdfDatasets);
                 if (def != null)
                 {
                     //Resource has an appropriate file extension and we've found a candidate parser for it
                     parser = def.GetRdfDatasetParser();
                     parser.Load(handler, new StreamParams(s));
                 }
                 else
                 {
                     //Resource did not have a file extension or we didn't have a parser associated with the extension
                     //Try using StringParser instead
                     String data;
                     using (StreamReader reader = new StreamReader(s))
                     {
                         data = reader.ReadToEnd();
                         reader.Close();
                     }
                     parser = StringParser.GetDatasetParser(data);
                     parser.Load(handler, new TextReaderParams(new StringReader(data)));
                 }
             }
         }
     }
 }
Esempio n. 5
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>
        /// <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(IRdfHandler handler, Uri u, IStoreReader parser)
        {
            if (u == null)
            {
                throw new RdfParseException("Cannot read a RDF dataset from a null URI");
            }
            if (handler == null)
            {
                throw new RdfParseException("Cannot read a RDF dataset using a null RDF handler");
            }

            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;
                }

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

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

                //Want to ask for TriG, NQuads or TriX
                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.HttpRdfDatasetAcceptHeader;
                }

                //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

                    //Get a Parser and Load the RDF
                    if (parser == null)
                    {
                        try
                        {
                            parser          = MimeTypesHelper.GetStoreParser(httpResponse.ContentType);
                            parser.Warning += RaiseStoreWarning;
                            parser.Load(handler, new StreamParams(httpResponse.GetResponseStream()));
                        }
                        catch (RdfParserSelectionException selEx)
                        {
                            String data = new StreamReader(httpResponse.GetResponseStream()).ReadToEnd();
                            parser          = StringParser.GetDatasetParser(data);
                            parser.Warning += RaiseStoreWarning;
                            parser.Load(handler, new TextReaderParams(new StringReader(data)));
                        }
                    }
                    else
                    {
                        parser.Warning += RaiseStoreWarning;
                        parser.Load(handler, new StreamParams(httpResponse.GetResponseStream()));
                    }
                }
            }
            catch (UriFormatException uriEx)
            {
                //Uri Format Invalid
                throw new RdfException("Unable to load from the given URI '" + u.ToString() + "' since it's format was invalid, see inner exception for details", uriEx);
            }
            catch (WebException webEx)
            {
#if DEBUG
                if (Options.HttpDebugging)
                {
                    if (webEx.Response != null)
                    {
                        Tools.HttpDebugResponse((HttpWebResponse)webEx.Response);
                    }
                }
#endif
                //Some sort of HTTP Error occurred
                throw new WebException("A HTTP Error occurred resolving the URI '" + u.ToString() + "', see innner exception for details", webEx);
            }
        }
Esempio n. 6
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="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(IRdfHandler handler, Uri u, IStoreReader parser, RdfHandlerCallback callback, Object state)
        {
            if (u == null)
            {
                throw new RdfParseException("Cannot read a RDF dataset from a null URI");
            }
            if (handler == null)
            {
                throw new RdfParseException("Cannot read a RDF dataset using a null RDF handler");
            }

            try
            {
                if (u.IsFile)
                {
                    // Invoke FileLoader instead
                    RaiseWarning("This is a file: URI so invoking the FileLoader instead");
                    if (Path.DirectorySeparatorChar == '/')
                    {
                        FileLoader.Load(handler, u.AbsoluteUri.Substring(7), parser);
                    }
                    else
                    {
                        FileLoader.Load(handler, u.AbsoluteUri.Substring(8), parser);
                    }
                    // FileLoader.Load() will run synchronously so once this completes we can invoke the callback
                    callback(handler, state);
                    return;
                }
                if (u.Scheme.Equals("data"))
                {
                    // Invoke DataUriLoader instead
                    RaiseWarning("This is a data: URI so invoking the DataUriLoader instead");
                    DataUriLoader.Load(handler, u);
                    // After DataUriLoader.Load() has run (which happens synchronously) can invoke the callback
                    callback(handler, state);
                    return;
                }

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

                // Setup the Request
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(u);

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

                // Use HTTP GET
                request.Method = "GET";
#if !NETCORE
                request.Timeout = Options.UriLoaderTimeout;
#endif
                if (_userAgent != null && !_userAgent.Equals(String.Empty))
                {
#if NETCORE
                    request.Headers[HttpRequestHeader.UserAgent] = _userAgent;
#else
                    request.UserAgent = _userAgent;
#endif
                }

                Tools.HttpDebugRequest(request);

                try
                {
                    request.BeginGetResponse(result =>
                    {
                        try
                        {
                            using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result))
                            {
                                Tools.HttpDebugResponse(response);

                                // Get a Parser and load the RDF
                                if (parser == null)
                                {
                                    try
                                    {
                                        // Only need to auto-detect the parser if a specific one wasn't specified
                                        parser          = MimeTypesHelper.GetStoreParser(response.ContentType);
                                        parser.Warning += RaiseWarning;
                                        parser.Load(handler, new StreamReader(response.GetResponseStream()));
                                    }
                                    catch (RdfParserSelectionException)
                                    {
                                        RaiseStoreWarning("Unable to select a RDF Dataset parser based on Content-Type: " + response.ContentType + " - seeing if the content is an RDF Graph instead");

                                        try
                                        {
                                            // If not a RDF Dataset format see if it is a Graph
                                            IRdfReader rdfParser = MimeTypesHelper.GetParser(response.ContentType);
                                            rdfParser.Load(handler, new StreamReader(response.GetResponseStream()));
                                        }
                                        catch (RdfParserSelectionException)
                                        {
                                            String data     = new StreamReader(response.GetResponseStream()).ReadToEnd();
                                            parser          = StringParser.GetDatasetParser(data);
                                            parser.Warning += RaiseStoreWarning;
                                            parser.Load(handler, new StringReader(data));
                                        }
                                    }
                                }
                                else
                                {
                                    parser.Warning += RaiseStoreWarning;
                                    parser.Load(handler, new StreamReader(response.GetResponseStream()));
                                }

                                // Finally can invoke the callback
                                callback(handler, state);
                            }
                        }
                        catch (WebException webEx)
                        {
                            if (webEx.Response != null)
                            {
                                Tools.HttpDebugResponse((HttpWebResponse)webEx.Response);
                            }
                            callback(handler, new AsyncError(new RdfParseException("A HTTP Error occurred loading the URI '" + u.AbsoluteUri + "' asynchronously, see inner exeption for details", webEx), state));
                        }
                        catch (Exception ex)
                        {
                            callback(handler, new AsyncError(new RdfParseException("Unexpected error while loading the URI '" + u.AbsoluteUri + "' asynchronously, see inner exception for details", ex), state));
                        }
                    }, null);
                }
                catch (WebException webEx)
                {
                    if (webEx.Response != null)
                    {
                        Tools.HttpDebugResponse((HttpWebResponse)webEx.Response);
                    }
                    callback(handler, new AsyncError(new RdfParseException("A HTTP Error occurred loading the URI '" + u.AbsoluteUri + "' asynchronously, see inner exeption for details", webEx), state));
                }
                catch (Exception ex)
                {
                    callback(handler, new AsyncError(new RdfParseException("Unexpected error while loading the URI '" + u.AbsoluteUri + "' asynchronously, see inner exception for details", ex), state));
                }
            }
            catch (UriFormatException uriEx)
            {
                // Uri Format Invalid
                throw new RdfException("Unable to load from the given URI '" + u.AbsoluteUri + "' since it's format was invalid, see inner exception for details", uriEx);
            }
        }