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
        public void ParsingGZipDatasetByGZipStreamManual()
        {
            foreach (String filename in this._manualDatasetTestFiles)
            {
                TripleStore store = new TripleStore();

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

                IStoreReader reader = def.GetRdfDatasetParser();
                reader.Load(store, new StreamReader(new GZipStream(new FileStream(filename, FileMode.Open, FileAccess.Read), CompressionMode.Decompress)));

                Assert.Equal(this._g, store.Graphs.First());
            }
        }
Exemple #3
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)));
                 }
             }
         }
     }
 }
Exemple #4
0
        public void ParsingGZipDatasetByGZipStreamAuto()
        {
            foreach (String filename in this._autoDatasetTestFiles)
            {
                TripleStore store = new TripleStore();

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

                IStoreReader reader = def.GetRdfDatasetParser();
                reader.Load(store, new StreamReader(new GZipStream(new FileStream(filename, FileMode.Open, FileAccess.Read), CompressionMode.Decompress)));

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