Ejemplo 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);
            }
        }
Ejemplo n.º 2
0
        public void ParsingGZipByFilenameAuto2()
        {
            foreach (String filename in this._autoTestFiles)
            {
                Graph g = new Graph();

                IRdfReader reader = MimeTypesHelper.GetParserByFileExtension(MimeTypesHelper.GetTrueFileExtension(filename));
                reader.Load(g, filename);

                Assert.AreEqual(this._g, g, "Graphs for file " + filename + " were not equal");
            }
        }
Ejemplo n.º 3
0
        public void ParsingGZipByFilenameManual2()
        {
            foreach (String filename in this._manualTestFiles)
            {
                Graph g = new Graph();

                IRdfReader reader = MimeTypesHelper.GetParserByFileExtension(MimeTypesHelper.GetTrueFileExtension(filename));
                reader.Load(g, filename);

                Assert.Equal(this._g, g);
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Implements the import
 /// </summary>
 /// <param name="handler">Handler</param>
 protected override void ImportUsingHandler(IRdfHandler handler)
 {
     this.Information = "Importing from File " + this._file;
     try
     {
         //Assume a RDF Graph
         IRdfReader reader = MimeTypesHelper.GetParserByFileExtension(MimeTypesHelper.GetTrueFileExtension(this._file));
         FileLoader.Load(handler, this._file, reader);
     }
     catch (RdfParserSelectionException)
     {
         //Assume a RDF Dataset
         FileLoader.LoadDataset(handler, this._file);
     }
 }
Ejemplo n.º 5
0
        public static void Load(IRdfHandler handler, string filename, Stream inputStream, IRdfReader parser)
        {
            if (handler == null)
            {
                throw new RdfParseException("Cannot read RDF using a null RDF Handler");
            }
            if (inputStream == null)
            {
                throw new RdfParseException("Cannot read RDF from a null Stream");
            }

            if (parser == null)
            {
                if (filename != null)
                {
                    try
                    {
                        String ext = MimeTypesHelper.GetTrueFileExtension(filename);
                        parser = MimeTypesHelper.GetParserByFileExtension(ext);
                    }
                    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
                RaiseWarning("Attempting to select parser based on analysis of the data file, this requires loading the file into memory");
                StreamReader reader = new StreamReader(inputStream);
                String       data   = reader.ReadToEnd();
                reader.Close();
                parser = StringParser.GetParser(data);
                RaiseWarning("Used the StringParser to guess the parser to use - it guessed " + parser.GetType().Name);
                parser.Warning += RaiseWarning;
                parser.Load(handler, new StringReader(data));
            }
            else
            {
                //Parser was selected based on File Extension or one was explicitly specified
                parser.Warning += RaiseWarning;
                parser.Load(handler, new StreamReader(inputStream));
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Loads the Configuration Graph
        /// </summary>
        /// <returns></returns>
        internal IGraph LoadConfigurationGraph()
        {
            IGraph g = null;

            try
            {
                if (File.Exists(this._configFile))
                {
                    g = new Graph();
                    FileLoader.Load(g, this._configFile);
                }
                else
                {
                    //Try to get from embedded resource instead
                    Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(this._configFile);
                    if (stream == null)
                    {
                        Console.Error.WriteLine("rdfServer: Error: Configuration File '" + this.ConfigurationFile + "' was not found");
                    }
                    else
                    {
                        IRdfReader reader = MimeTypesHelper.GetParserByFileExtension(MimeTypesHelper.GetTrueFileExtension(this._configFile));
                        g = new Graph();
                        reader.Load(g, new StreamReader(stream));
                    }
                }
            }
            catch (RdfParserSelectionException)
            {
                g = null;
                Console.Error.WriteLine("rdfServer: Error: Configuration File '" + this.ConfigurationFile + "' could not be loaded as a suitable parser was not found");
            }
            catch (RdfParseException)
            {
                g = null;
                Console.Error.WriteLine("rdfServer: Error: Configuration File '" + this.ConfigurationFile + "' was not valid RDF");
            }

            //If got a Graph OK then prep the dotNetRDF Configuration API
            if (g != null)
            {
                ConfigurationLoader.AutoConfigure(g);
            }
            return(g);
        }
        private static IGraph GraphFromUri(Uri uri)
        {
            var incorrectBaseUri = new Uri(baseUri.AbsoluteUri.Replace(@"https://", "http://"));

            var uriMapping = new Dictionary <Uri, Uri> {
                {
                    incorrectBaseUri,
                    baseUri
                }
            };

            var graph   = new Graph();
            var handler = new UriMappingHandler(new GraphHandler(graph), graph, uriMapping);
            var parser  = MimeTypesHelper.GetParserByFileExtension(Path.GetExtension(uri.LocalPath));

            graph.BaseUri = uri;
            UriLoader.Load(handler, uri, parser);

            return(graph);
        }
Ejemplo n.º 8
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);
                }
            }
        }
Ejemplo n.º 9
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");
            }
        }