Exemplo n.º 1
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 " + GetAssemblyName(asm));
                }
                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 = MimeTypesHelper.GetTrueResourceExtension(resource);
                        MimeTypeDefinition def = MimeTypesHelper.GetDefinitionsByFileExtension(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();
#if !PORTABLE
                                reader.Close();
#endif
                            }
                            parser = StringParser.GetParser(data);
                            parser.Load(handler, new StringReader(data));
                        }
                    }
                }
            }
        }
Exemplo n.º 2
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));
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Loads the contents of the given File using a RDF Handler using the given RDF Parser
        /// </summary>
        /// <param name="handler">RDF Handler to use</param>
        /// <param name="filename">File to load from</param>
        /// <param name="parser">Parser to use</param>
        public static void Load(IRdfHandler handler, String filename, IRdfReader parser)
        {
            if (handler == null)
            {
                throw new RdfParseException("Cannot read RDF using a null RDF Handler");
            }
            if (filename == null)
            {
                throw new RdfParseException("Cannot read RDF from a null File");
            }

            //Try to get a Parser from the File Extension if one isn't explicitly specified
            if (parser == null)
            {
                try
                {
                    parser = MimeTypesHelper.GetParser(MimeTypesHelper.GetMimeTypes(Path.GetExtension(filename)));
                    RaiseWarning("Selected Parser " + parser.ToString() + " based on file extension, if this is incorrect consider specifying the parser explicitly");
                }
                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(filename);
                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, filename);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Loads RDF data using an RDF Handler from a data: URI
        /// </summary>
        /// <param name="handler">RDF Handler</param>
        /// <param name="u">URI to load from</param>
        /// <remarks>
        /// Invokes the normal <see cref="UriLoader">UriLoader</see> instead if a the URI provided is not a data: URI
        /// </remarks>
        /// <exception cref="UriFormatException">Thrown if the metadata portion of the URI which indicates the MIME Type, Character Set and whether Base64 encoding is used is malformed</exception>
        public static void Load(IRdfHandler handler, Uri u)
        {
            if (u == null)
            {
                throw new RdfParseException("Cannot load RDF from a null URI");
            }
            if (handler == null)
            {
                throw new RdfParseException("Cannot read RDF into a null RDF Handler");
            }

            if (!u.Scheme.Equals("data"))
            {
                // Invoke the normal URI Loader if not a data: URI
                UriLoader.Load(handler, u);
                return;
            }

            String mimetype = "text/plain";
            bool   mimeSet  = false;
            bool   base64   = false;

            String[] uri      = u.AbsolutePath.Split(',');
            String   metadata = uri[0];
            String   data     = uri[1];

            // Process the metadata
            if (metadata.Equals(String.Empty))
            {
                // Nothing to do
            }
            else if (metadata.Contains(';'))
            {
                if (metadata.StartsWith(";"))
                {
                    metadata = metadata.Substring(1);
                }
                String[] meta = metadata.Split(';');
                for (int i = 0; i < meta.Length; i++)
                {
                    if (meta[i].StartsWith("charset="))
                    {
                        // OPT: Do we need to process the charset parameter here at all?
                        // String charset = meta[i].Substring(meta[i].IndexOf('=') + 1);
                    }
                    else if (meta[i].Equals("base64"))
                    {
                        base64 = true;
                    }
                    else if (meta[i].Contains('/'))
                    {
                        mimetype = meta[i];
                        mimeSet  = true;
                    }
                    else
                    {
                        throw new UriFormatException("This data: URI appears to be malformed as encountered the parameter value '" + meta[i] + "' in the metadata section of the URI");
                    }
                }
            }
            else
            {
                if (metadata.StartsWith("charset="))
                {
                    // OPT: Do we need to process the charset parameter here at all?
                }
                else if (metadata.Equals(";base64"))
                {
                    base64 = true;
                }
                else if (metadata.Contains('/'))
                {
                    mimetype = metadata;
                    mimeSet  = true;
                }
                else
                {
                    throw new UriFormatException("This data: URI appears to be malformed as encountered the parameter value '" + metadata + "' in the metadata section of the URI");
                }
            }

            // Process the data
            if (base64)
            {
                StringWriter temp = new StringWriter();
                foreach (byte b in Convert.FromBase64String(data))
                {
                    temp.Write((char)((int)b));
                }
                data = temp.ToString();
            }
            else
            {
                data = Uri.UnescapeDataString(data);
            }

            // Now either select a parser based on the MIME type or let StringParser guess the format
            try
            {
                if (mimeSet)
                {
                    // If the MIME Type was explicitly set then we'll try and get a parser and use it
                    IRdfReader reader = MimeTypesHelper.GetParser(mimetype);
                    reader.Load(handler, new StringReader(data));
                }
                else
                {
                    // If the MIME Type was not explicitly set we'll let the StringParser guess the format
                    IRdfReader reader = StringParser.GetParser(data);
                    reader.Load(handler, new StringReader(data));
                }
            }
            catch (RdfParserSelectionException)
            {
                // If we fail to get a parser then we'll let the StringParser guess the format
                IRdfReader reader = StringParser.GetParser(data);
                reader.Load(handler, new StringReader(data));
            }
        }