Exemple #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);
            }
        }
        public IAsyncResult BeginLoad(String sourceFileName, TextReader sourceReader, AsyncCallback callback, object state)
        {
            _filename = sourceFileName;
            IStoreReader reader = MimeTypesHelper.GetStoreParserByFileExtension(MimeTypesHelper.GetTrueFileExtension(sourceFileName));

            return(_loadDelegate.BeginInvoke(sourceReader, reader, callback, state));
        }
        /// <summary>
        /// Internal helper method for loading the data
        /// </summary>
        /// <param name="filename">File to load from</param>
        private void Initialise(String filename)
        {
            try
            {
                IStoreReader reader = MimeTypesHelper.GetStoreParserByFileExtension(MimeTypesHelper.GetTrueFileExtension(filename));
                reader.Load(this._store, filename);

                this._ready = true;
            }
            catch (RdfException rdfEx)
            {
                throw new RdfStorageException("An Error occurred while trying to read the Dataset File", rdfEx);
            }
        }
Exemple #4
0
        public static void LoadFromFile(this ITripleStore store, string fileName)
        {
            IStoreReader parser = null;

            using (var input = new StreamReader(fileName))
            {
                string ext = MimeTypesHelper.GetTrueFileExtension(fileName);
                try
                {
                    parser = MimeTypesHelper.GetStoreParserByFileExtension(ext);
                }
                catch (RdfParserSelectionException)
                {
                    try
                    {
                        IRdfReader rdfParser    = MimeTypesHelper.GetParserByFileExtension(ext);
                        var        storeHandler = new StoreHandler(store);
                        rdfParser.Load(storeHandler, input);
                        return;
                    }
                    catch (RdfParserSelectionException)
                    {
                        // Ignore this. Will try and use format guessing and assume it is a dataset format
                    }
                }
                if (parser == null)
                {
                    string data = input.ReadToEnd();
                    input.Close();
                    parser = StringParser.GetDatasetParser(data);
                    var handler = new StoreHandler(store);
                    parser.Load(handler, new StringReader(data));
                }
                else
                {
                    parser.Load(new StoreHandler(store), input);
                }
            }
        }
Exemple #5
0
        private void btnImportFile_Click(object sender, EventArgs e)
        {
            if (this.txtSourceFile.Text.Equals(string.Empty))
            {
                MessageBox.Show("Please enter a File you wish to import RDF from...", "No File Specified");
            }
            else
            {
                try
                {
                    //Try and get a Graph Parser and load
                    IRdfReader parser = MimeTypesHelper.GetParserByFileExtension(MimeTypesHelper.GetTrueFileExtension(this.txtSourceFile.Text));
                    Graph      g      = new Graph();
                    FileLoader.Load(g, this.txtSourceFile.Text);
                    this.LogImportSuccess(this.txtSourceFile.Text, 1, g.Triples.Count);

                    //Add to Store
                    try
                    {
                        this._tripleCount += g.Triples.Count;
                        this._dataset.AddGraph(g);
                    }
                    catch (Exception ex)
                    {
                        this.LogImportFailure(this.txtSourceFile.Text, ex);
                        MessageBox.Show("An error occurred trying to add the RDF Graph to the Dataset:\n" + ex.Message, "File Import Error");
                        return;
                    }
                }
                catch (RdfParserSelectionException)
                {
                    try
                    {
                        //Try and get a Store Parser and load
                        IStoreReader storeparser = MimeTypesHelper.GetStoreParserByFileExtension(MimeTypesHelper.GetTrueFileExtension(this.txtSourceFile.Text));
                        TripleStore  store       = new TripleStore();
                        storeparser.Load(store, this.txtSourceFile.Text);

                        foreach (IGraph g in store.Graphs)
                        {
                            if (this._dataset.HasGraph(g.BaseUri))
                            {
                                int triplesBefore = this._dataset[g.BaseUri].Triples.Count;
                                this._dataset[g.BaseUri].Merge(g);
                                this._tripleCount += this._dataset[g.BaseUri].Triples.Count - triplesBefore;
                            }
                            else
                            {
                                this._dataset.AddGraph(g);
                                this._tripleCount += g.Triples.Count;
                            }
                        }

                        this.LogImportSuccess(this.txtSourceFile.Text, store.Graphs.Count, store.Graphs.Sum(g => g.Triples.Count));
                    }
                    catch (RdfParserSelectionException selEx)
                    {
                        this.LogImportFailure(this.txtSourceFile.Text, selEx);
                        MessageBox.Show("The given file does not appear to be an RDF Graph/Dataset File Format the tool understands", "File Import Error");
                        return;
                    }
                    catch (Exception ex)
                    {
                        this.LogImportFailure(this.txtSourceFile.Text, ex);
                        MessageBox.Show("An error occurred trying to read an RDF Dataset from the file:\n" + ex.Message, "File Import Error");
                        return;
                    }
                }
                catch (Exception ex)
                {
                    this.LogImportFailure(this.txtSourceFile.Text, ex);
                    MessageBox.Show("An error occurred trying to read an RDF Graph from the file:\n" + ex.Message, "File Import Error");
                    return;
                }

                this._dataset.Flush();
                this.stsGraphs.Text  = this._dataset.GraphUris.Count() + " Graphs";
                this.stsTriples.Text = this._tripleCount + " Triples";
                MessageBox.Show("RDF added to the Dataset OK", "File Import Done");
            }
        }