Exemple #1
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));
                            }
                        }
                    }
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Internal Helper method which does the actual loading of the Graph 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 then will be auto-selected)</param>
        private static void LoadGraphInternal(IRdfHandler handler, Assembly asm, String resource, IRdfReader 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

                    //Did we get a defined parser to use?
                    if (parser != null)
                    {
                        parser.Load(handler, new StreamReader(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.CanParseRdf);
                        if (def != null)
                        {
                            //Resource has an appropriate file extension and we've found a candidate parser for it
                            parser = def.GetRdfParser();
                            parser.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();
                                reader.Close();
                            }
                            parser = StringParser.GetParser(data);
                            parser.Load(handler, new StringReader(data));
                        }
                    }
                }
            }
        }
Exemple #3
0
        public void ParsingGZipByGZipStreamAuto()
        {
            foreach (String filename in this._autoTestFiles)
            {
                Graph g = new Graph();

                String ext = MimeTypesHelper.GetTrueFileExtension(filename);
                ext = ext.Substring(1);
                MimeTypeDefinition def = MimeTypesHelper.Definitions.Where(d => d.CanParseRdf && d.SupportsFileExtension(ext)).FirstOrDefault();
                Assert.NotNull(def);

                IRdfReader reader = def.GetRdfParser();
                reader.Load(g, new StreamReader(new GZipStream(new FileStream(filename, FileMode.Open, FileAccess.Read), CompressionMode.Decompress)));

                Assert.Equal(this._g, g);
            }
        }
Exemple #4
0
        public void ParsingGZipByFilenameAuto1()
        {
            foreach (String filename in this._autoTestFiles)
            {
                Graph g = new Graph();

                String ext = MimeTypesHelper.GetTrueFileExtension(filename);
                ext = ext.Substring(1);
                MimeTypeDefinition def = MimeTypesHelper.Definitions.Where(d => d.CanParseRdf && d.SupportsFileExtension(ext)).FirstOrDefault();
                Assert.NotNull(def);

                IRdfReader reader = def.GetRdfParser();
                reader.Load(g, filename);

                Assert.Equal(this._g, g);
            }
        }
Exemple #5
0
        public void ParsingGZipByGZipStreamAuto()
        {
            foreach (String filename in this._autoTestFiles)
            {
                Graph g = new Graph();

                String ext = MimeTypesHelper.GetTrueFileExtension(filename);
                ext = ext.Substring(1);
                MimeTypeDefinition def = MimeTypesHelper.Definitions.Where(d => d.CanParseRdf && d.SupportsFileExtension(ext)).FirstOrDefault();
                if (def == null)
                {
                    Assert.Fail("Failed to find MIME Type Definition for File Extension ." + ext);
                }

                IRdfReader reader = def.GetRdfParser();
                reader.Load(g, new StreamReader(new GZipStream(new FileStream(filename, FileMode.Open, FileAccess.Read), CompressionMode.Decompress)));

                Assert.AreEqual(this._g, g, "Graphs for file " + filename + " were not equal");
            }
        }
Exemple #6
0
        public void ParsingGZipByFilenameManual1()
        {
            foreach (String filename in this._manualTestFiles)
            {
                Graph g = new Graph();

                String ext = MimeTypesHelper.GetTrueFileExtension(filename);
                ext = ext.Substring(1);
                MimeTypeDefinition def = MimeTypesHelper.Definitions.Where(d => d.CanParseRdf && d.SupportsFileExtension(ext)).FirstOrDefault();
                if (def == null)
                {
                    Assert.Fail("Failed to find MIME Type Definition for File Extension ." + ext);
                }

                IRdfReader reader = def.GetRdfParser();
                reader.Load(g, filename);

                Assert.AreEqual(this._g, g, "Graphs for file " + filename + " were not equal");
            }
        }