Beispiel #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));
                            }
                        }
                    }
                }
            }
        }