Example #1
0
        /// <summary>
        /// Loads a RDF dataset from GZipped input
        /// </summary>
        /// <param name="handler">RDF Handler to use</param>
        /// <param name="parameters">Store Parameters</param>
        public void Load(IRdfHandler handler, IStoreParams parameters)
        {
            if (handler == null)
            {
                throw new RdfParseException("Cannot parse RDF Dataset using a null Handler");
            }
            if (parameters == null)
            {
                throw new RdfParseException("Cannot parse RDF Dataset from null parameters");
            }

            if (parameters is StreamParams)
            {
                StreamParams sp    = (StreamParams)parameters;
                StreamReader input = sp.StreamReader;

                if (input.BaseStream is GZipStream)
                {
                    this._parser.Load(handler, sp);
                }
                else
                {
                    //Force the inner stream to be GZipped
                    this._parser.Load(handler, new StreamParams(new GZipStream(input.BaseStream, CompressionMode.Decompress)));
                }
            }
            else
            {
                throw new RdfParseException("GZip Dataset Parsers can only read from StreamParams instances");
            }
        }
Example #2
0
        /// <summary>
        /// Saves a RDF Dataset as GZipped output
        /// </summary>
        /// <param name="store">Store to save</param>
        /// <param name="parameters">Storage Parameters</param>
        public void Save(ITripleStore store, IStoreParams parameters)
        {
            if (store == null)
            {
                throw new RdfOutputException("Cannot output a new Triple Store");
            }
            if (parameters == null)
            {
                throw new RdfOutputException("Cannot output using null parameters");
            }

            if (parameters is StreamParams)
            {
                StreamParams sp     = (StreamParams)parameters;
                StreamWriter output = sp.StreamWriter;

                if (output.BaseStream is GZipStream)
                {
                    this._writer.Save(store, sp);
                }
                else
                {
                    this._writer.Save(store, new StreamParams(new GZipStream(output.BaseStream, CompressionMode.Compress)));
                }
            }
            else
            {
                throw new RdfOutputException("GZip Dataset Writers can only write to StreamParams instances");
            }
        }
Example #3
0
 /// <summary>
 /// Loads Graphs into the store using the settings the Reader was instantiated with
 /// </summary>
 /// <param name="store">Store to load into</param>
 /// <param name="parameters">Parameters indicating where to read from</param>
 public void Load(ITripleStore store, IStoreParams parameters)
 {
     if (store == null)
     {
         throw new RdfParseException("Cannot read RDF from a Folder Store into a null Triple Store");
     }
     this.Load(new StoreHandler(store), parameters);
 }
Example #4
0
 /// <summary>
 /// Loads a RDF dataset from GZipped input
 /// </summary>
 /// <param name="store">Triple Store to load into</param>
 /// <param name="parameters">Store Parameters</param>
 public void Load(ITripleStore store, IStoreParams parameters)
 {
     if (store == null)
     {
         throw new RdfParseException("Cannot parse an RDF Dataset into a null store");
     }
     this.Load(new StoreHandler(store), parameters);
 }
Example #5
0
        /// <summary>
        /// Saves a Store in TriX format
        /// </summary>
        /// <param name="store">Store to save</param>
        /// <param name="parameters">Parameters indicating a Stream to write to</param>
        public void Save(ITripleStore store, IStoreParams parameters)
        {
            //Try and get the TextWriter to output to
            TextWriter output = null;

            if (parameters is StreamParams)
            {
                output = ((StreamParams)parameters).StreamWriter;
            }
            else if (parameters is TextWriterParams)
            {
                output = ((TextWriterParams)parameters).TextWriter;
            }

            if (output != null)
            {
                try
                {
                    //Setup the XML document
                    XmlWriter writer = XmlWriter.Create(output, this.GetSettings());
                    writer.WriteStartDocument();
                    writer.WriteStartElement("TriX", TriXParser.TriXNamespaceURI);
                    writer.WriteStartAttribute("xmlns");
                    writer.WriteRaw(TriXParser.TriXNamespaceURI);
                    writer.WriteEndAttribute();

                    //Output Graphs as XML <graph> elements
                    foreach (IGraph g in store.Graphs)
                    {
                        this.GraphToTriX(g, writer);
                    }

                    //Save the XML to disk
                    writer.WriteEndDocument();
                    writer.Close();
                    output.Close();
                }
                catch
                {
                    try
                    {
                        output.Close();
                    }
                    catch
                    {
                        //Just cleaning up
                    }
                    throw;
                }
            }
            else
            {
                throw new RdfStorageException("Parameters for the TriXWriter must be of the type StreamParams/TextWriterParams");
            }
        }
Example #6
0
        /// <summary>
        /// Saves a Store in TriX format
        /// </summary>
        /// <param name="store">Store to save</param>
        /// <param name="parameters">Parameters indicating a Stream to write to</param>
        public void Save(ITripleStore store, IStoreParams parameters)
        {
            //Try and get the TextWriter to output to
            TextWriter output = null;
            if (parameters is StreamParams)
            {
                output = ((StreamParams)parameters).StreamWriter;
            } 
            else if (parameters is TextWriterParams)
            {
                output = ((TextWriterParams)parameters).TextWriter;
            }

            if (output != null)
            {
                try
                {
                    //Setup the XML document
                    XmlWriter writer = XmlWriter.Create(output, this.GetSettings());
                    writer.WriteStartDocument();
                    writer.WriteStartElement("TriX", TriXParser.TriXNamespaceURI);
                    writer.WriteStartAttribute("xmlns");
                    writer.WriteRaw(TriXParser.TriXNamespaceURI);
                    writer.WriteEndAttribute();

                    //Output Graphs as XML <graph> elements
                    foreach (IGraph g in store.Graphs) 
                    {
                        this.GraphToTriX(g, writer);
                    }

                    //Save the XML to disk
                    writer.WriteEndDocument();
                    writer.Close();
                    output.Close();
                }
                catch
                {
                    try
                    {
                        output.Close();
                    }
                    catch
                    {
                        //Just cleaning up
                    }
                    throw;
                }
            }
            else
            {
                throw new RdfStorageException("Parameters for the TriXWriter must be of the type StreamParams/TextWriterParams");
            }
        }
Example #7
0
        /// <summary>
        /// Saves the given Triple Store to the Target Store that this class was instantiated with
        /// </summary>
        /// <param name="store">Store to Save</param>
        /// <param name="parameters">Parameters for the Store</param>
        public override void Save(ITripleStore store, IStoreParams parameters)
        {
            if (parameters is ThreadedSqlIOParams)
            {
                //Setup context
                ThreadedSqlStoreWriterContext context = new ThreadedSqlStoreWriterContext(store, (ThreadedSqlIOParams)parameters);
                bool transSetting = context.Manager.DisableTransactions;
                context.Manager.DisableTransactions = true;

                //Queue Graphs for Writing
                foreach (IGraph g in store.Graphs)
                {
                    context.Add(g);
                }

                //Start making the async calls
                List <IAsyncResult> results = new List <IAsyncResult>();
                WriteGraphsDelegate d       = new WriteGraphsDelegate(this.WriteGraphs);
                for (int i = 0; i < context.Threads; i++)
                {
                    results.Add(d.BeginInvoke(context, null, null));
                }

                //Wait for all the async calls to complete
                WaitHandle.WaitAll(results.Select(r => r.AsyncWaitHandle).ToArray());
                RdfThreadedOutputException outputEx = new RdfThreadedOutputException(WriterErrorMessages.ThreadedOutputFailure("Folder Store"));
                foreach (IAsyncResult result in results)
                {
                    try
                    {
                        d.EndInvoke(result);
                    }
                    catch (Exception ex)
                    {
                        outputEx.AddException(ex);
                    }
                }

                //Reset Transactions setting
                context.Manager.DisableTransactions = transSetting;

                //If there were any errors we'll throw an RdfThreadedOutputException now
                if (outputEx.InnerExceptions.Any())
                {
                    throw outputEx;
                }
            }
            else
            {
                throw new RdfStorageException("Parameters for the ThreadedSQLStoreWriter must be of type ThreadedSQLIOParams");
            }
        }
Example #8
0
        /// <summary>
        /// Saves the given Triple Store to an arbitrary store
        /// </summary>
        /// <param name="store">Store to Save</param>
        /// <param name="parameters">Parameters for the Store</param>
        /// <remarks>
        /// Parameters must be of type <see cref="GenericIOParams">GenericIOParams</see>
        /// </remarks>
        public void Save(ITripleStore store, IStoreParams parameters)
        {
            if (parameters is GenericIOParams)
            {
                //Create the Writer Context
                GenericStoreWriterContext context = new GenericStoreWriterContext(store, (GenericIOParams)parameters);

                //Queue Graphs for Writing
                foreach (IGraph g in store.Graphs)
                {
                    context.Add(g.BaseUri);
                }

                //Start making the async calls
                List <IAsyncResult> results = new List <IAsyncResult>();
                WriteGraphsDelegate d       = new WriteGraphsDelegate(this.WriteGraphs);
                for (int i = 0; i < context.Threads; i++)
                {
                    results.Add(d.BeginInvoke(context, null, null));
                }

                //Wait for all the async calls to complete
                WaitHandle.WaitAll(results.Select(r => r.AsyncWaitHandle).ToArray());
                RdfThreadedOutputException outputEx = new RdfThreadedOutputException(WriterErrorMessages.ThreadedOutputFailure("TSV"));
                foreach (IAsyncResult result in results)
                {
                    try
                    {
                        d.EndInvoke(result);
                    }
                    catch (Exception ex)
                    {
                        outputEx.AddException(ex);
                    }
                }

                //If there were any errors we'll throw an RdfThreadedOutputException now
                if (outputEx.InnerExceptions.Any())
                {
                    throw outputEx;
                }
            }
            else
            {
                throw new RdfStorageException("Parameters for the GenericStoreWriter must be of type GenericIOParams");
            }
        }
Example #9
0
        /// <summary>
        /// Saves the given Triple Store to the SQL Store that this class was instantiated with
        /// </summary>
        /// <param name="store">Store you wish to Save</param>
        /// <param name="parameters">Parameters for the Store</param>
        public virtual void Save(ITripleStore store, IStoreParams parameters)
        {
            if (parameters is ISQLIOParams)
            {
                SqlIOParams writeParams = (SqlIOParams)parameters;
                writeParams.Manager.PreserveState = true;
                SqlWriter writer = new SqlWriter(writeParams.Manager);

                foreach (IGraph g in store.Graphs)
                {
                    writer.Save(g, writeParams.ClearIfExists);
                }
            }
            else
            {
                throw new RdfStorageException("Parameters for the SQLStoreWriter must implement the interface ISQLIOParams");
            }
        }
Example #10
0
        /// <summary>
        /// Saves the given Triple Store to the SQL Store that this class was instantiated with
        /// </summary>
        /// <param name="store">Store you wish to Save</param>
        /// <param name="parameters">Parameters for the Store</param>
        public virtual void Save(ITripleStore store, IStoreParams parameters)
        {
            if (parameters is ISQLIOParams)
            {
                SqlIOParams writeParams = (SqlIOParams)parameters;
                writeParams.Manager.PreserveState = true;
                SqlWriter writer = new SqlWriter(writeParams.Manager);

                foreach (IGraph g in store.Graphs)
                {
                    writer.Save(g, writeParams.ClearIfExists);
                }
            }
            else
            {
                throw new RdfStorageException("Parameters for the SQLStoreWriter must implement the interface ISQLIOParams");
            }
        }
        /// <summary>
        /// Saves the given Triple Store to an arbitrary store
        /// </summary>
        /// <param name="store">Store to Save</param>
        /// <param name="parameters">Parameters for the Store</param>
        /// <remarks>
        /// Parameters must be of type <see cref="GenericIOParams">GenericIOParams</see>
        /// </remarks>
        public void Save(ITripleStore store, IStoreParams parameters)
        {
            if (parameters is GenericIOParams)
            {
                //Create the Writer Context
                GenericStoreWriterContext context = new GenericStoreWriterContext(store, (GenericIOParams)parameters);

                //Queue Graphs for Writing
                foreach (IGraph g in store.Graphs)
                {
                    context.Add(g.BaseUri);
                }
                
                //Start making the async calls
                List<IAsyncResult> results = new List<IAsyncResult>();
                WriteGraphsDelegate d = new WriteGraphsDelegate(this.WriteGraphs);
                for (int i = 0; i < context.Threads; i++)
                {
                    results.Add(d.BeginInvoke(context, null, null));
                }

                //Wait for all the async calls to complete
                WaitHandle.WaitAll(results.Select(r => r.AsyncWaitHandle).ToArray());
                RdfThreadedOutputException outputEx = new RdfThreadedOutputException(WriterErrorMessages.ThreadedOutputFailure("TSV"));
                foreach (IAsyncResult result in results)
                {
                    try
                    {
                        d.EndInvoke(result);
                    }
                    catch (Exception ex)
                    {
                        outputEx.AddException(ex);
                    }
                }

                //If there were any errors we'll throw an RdfThreadedOutputException now
                if (outputEx.InnerExceptions.Any()) throw outputEx;
            }
            else
            {
                throw new RdfStorageException("Parameters for the GenericStoreWriter must be of type GenericIOParams");
            }
        }
Example #12
0
        /// <summary>
        /// Loads the named Graphs from the TriG input using the given RDF Handler
        /// </summary>
        /// <param name="handler">RDF Handler to use</param>
        /// <param name="parameters">Parameters indicating the input to read from</param>
        public void Load(IRdfHandler handler, IStoreParams parameters)
        {
            if (handler == null) throw new ArgumentNullException("handler", "Cannot parse an RDF Dataset using a null RDF Handler");
            if (parameters == null) throw new ArgumentNullException("parameters", "Cannot parse an RDF Dataset using null Parameters");

            //Try and get the Input from the parameters
            TextReader input = null;
            if (parameters is StreamParams)
            {
                //Get Input Stream
                input = ((StreamParams)parameters).StreamReader;

                //Issue a Warning if the Encoding of the Stream is not UTF-8
                if (!((StreamReader)input).CurrentEncoding.Equals(Encoding.UTF8))
                {
#if !SILVERLIGHT
                    this.RaiseWarning("Expected Input Stream to be encoded as UTF-8 but got a Stream encoded as " + ((StreamReader)input).CurrentEncoding.EncodingName + " - Please be aware that parsing errors may occur as a result");
#else
                this.RaiseWarning("Expected Input Stream to be encoded as UTF-8 but got a Stream encoded as " + ((StreamReader)input).CurrentEncoding.GetType().Name + " - Please be aware that parsing errors may occur as a result");
#endif
                }
            } 
            else if (parameters is TextReaderParams)
            {
                input = ((TextReaderParams)parameters).TextReader;
            }

            if (input != null)
            {
                try
                {
                    //Create the Parser Context and Invoke the Parser
                    TriGParserContext context = new TriGParserContext(handler, new TriGTokeniser(input), TokenQueueMode.SynchronousBufferDuringParsing, false, this._tracetokeniser);
                    this.Parse(context);
                }
                catch
                {
                    throw;
                }
                finally
                {
                    try
                    {
                        input.Close();
                    }
                    catch
                    {
                        //No catch actions just cleaning up
                    }
                }
            }
            else
            {
                throw new RdfStorageException("Parameters for the TriGParser must be of the type StreamParams/TextReaderParams");
            }
        }
Example #13
0
        /// <summary>
        /// Loads the RDF Dataset from the TriX input using a RDF Handler
        /// </summary>
        /// <param name="handler">RDF Handler to use</param>
        /// <param name="parameters">Parameters indicating the input to read from</param>
        public void Load(IRdfHandler handler, IStoreParams parameters)
        {
            if (handler == null) throw new ArgumentNullException("handler", "Cannot parse an RDF Dataset using a null RDF Handler");
            if (parameters == null) throw new ArgumentNullException("parameters", "Cannot parse an RDF Dataset using null Parameters");

            //Try and get the Input from the parameters
            TextReader input = null;
            if (parameters is StreamParams)
            {
                //Get Input Stream
                input = ((StreamParams)parameters).StreamReader;

                //Issue a Warning if the Encoding of the Stream is not UTF-8
                if (!((StreamReader)input).CurrentEncoding.Equals(Encoding.UTF8))
                {
                    this.RaiseWarning("Expected Input Stream to be encoded as UTF-8 but got a Stream encoded as " + ((StreamReader)input).CurrentEncoding.EncodingName + " - Please be aware that parsing errors may occur as a result");
                }
            } 
            else if (parameters is TextReaderParams)
            {
                input = ((TextReaderParams)parameters).TextReader;
            }

            if (input != null)
            {
                //First try and load as XML and apply any stylesheets
                try
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(input);

                    input.Close();

                    XmlDocument inputDoc = new XmlDocument();
                    bool inputReady = false;

                    //If XSL isn't supported we can't apply it
#if !NO_XSL

                    //Try and apply any stylesheets (which are used to extend TriX) to get basic TriX syntax
                    foreach (XmlNode child in doc.ChildNodes)
                    {
                        if (child.NodeType == XmlNodeType.ProcessingInstruction)
                        {
                            if (child.Name == "xml-stylesheet")
                            {
                                //Load in the XML a 2nd time so we can transform it properly if needed
                                if (!inputReady)
                                {
                                    inputDoc.LoadXml(doc.OuterXml);
                                    inputReady = true;
                                }

                                Regex getStylesheetURI = new Regex("href=\"([^\"]+)\"");
                                String stylesheetUri = getStylesheetURI.Match(child.Value).Groups[1].Value;

                                //Load the Transform
                                XslCompiledTransform transform = new XslCompiledTransform();
                                XsltSettings settings = new XsltSettings();
                                transform.Load(stylesheetUri, settings, null);

                                //Apply the Transform
                                MemoryStream temp = new MemoryStream();
                                transform.Transform(inputDoc, XmlWriter.Create(temp));
                                temp.Flush();
                                temp.Seek(0, SeekOrigin.Begin);
                                inputDoc.Load(temp);
                            }
                        }
                    }

#endif

                    //Start parsing
                    if (!inputReady) inputDoc = doc;
                    this.TryParseGraphset(inputDoc, handler);

                    input.Close();
                }
                catch (XmlException xmlEx)
                {
                    try
                    {
                        input.Close();
                    }
                    catch
                    {
                        //No catch actions - just cleaning up
                    }
                    //Wrap in a RDF Parse Exception
                    throw new RdfParseException("Unable to Parse this TriX since System.Xml was unable to parse the document into a DOM Tree, see the inner exception for details", xmlEx);
                }
                catch
                {
                    try
                    {
                        input.Close();
                    }
                    catch
                    {
                        //No catch actions - just cleaning up
                    }
                    throw;
                }
            }
            else
            {
                throw new RdfStorageException("Parameters for the TriXParser must be of the type StreamParams/TextReaderParams");
            }
        }
Example #14
0
        public void Load(IRdfHandler handler, IStoreParams parameters)
        {
            if (handler == null) throw new ArgumentNullException("handler", "Cannot parse an RDF Dataset using a null RDF Handler");
            if (parameters == null) throw new ArgumentNullException("parameters", "Cannot parse an RDF Dataset using null Parameters");

            //Try and get the Input from the parameters
            TextReader input = null;
            if (parameters is StreamParams)
            {
                //Get Input Stream
                input = ((StreamParams)parameters).StreamReader;
            }
            else if (parameters is TextReaderParams)
            {
                input = ((TextReaderParams)parameters).TextReader;
            }

            if (input != null)
            {
                //First try and load as XML and apply any stylesheets
                try
                {
                    //Get the reader and start parsing
                    XmlReader reader = XmlReader.Create(input, this.GetSettings());
                    this.RaiseWarning("The TriX Parser is operating without XSL support, if your TriX file requires XSL then it will not be parsed successfully");
                    this.TryParseGraphset(reader, handler);

                    input.Close();
                }
                catch (XmlException xmlEx)
                {
                    try
                    {
                        input.Close();
                    }
                    catch
                    {
                        //No catch actions - just cleaning up
                    }
                    //Wrap in a RDF Parse Exception
                    throw new RdfParseException("Unable to Parse this TriX since the XmlReader encountered an error, see the inner exception for details", xmlEx);
                }
                catch
                {
                    try
                    {
                        input.Close();
                    }
                    catch
                    {
                        //No catch actions - just cleaning up
                    }
                    throw;
                }
            }
            else
            {
                throw new RdfStorageException("Parameters for the TriXParser must be of the type StreamParams/TextReaderParams");
            }
        }
Example #15
0
        /// <summary>
        /// Loads RDF using the RDF Handler using the settings the Reader was instantiated with
        /// </summary>
        /// <param name="handler">RDF Handler to use</param>
        /// <param name="parameters">Parameters indicating where to read from</param>
        public void Load(IRdfHandler handler, IStoreParams parameters)
        {
            if (parameters is FolderStoreParams)
            {
                //Create the Parser Context
                FolderStoreParserContext context = new FolderStoreParserContext(handler, (FolderStoreParams)parameters);

                //Create the Folder
                if (!Directory.Exists(context.Folder))
                {
                    throw new RdfStorageException("Cannot read a Folder Store from a Folder that doesn't exist");
                }

                //Read list of Graphs and Queue Filenames of Graphs for reading by the Threads
                StreamReader graphlist = new StreamReader(Path.Combine(context.Folder, "graphs.fstore"));

                //First line contains format information
                String ext = graphlist.ReadLine();
                if (context.Format == FolderStoreFormat.AutoDetect)
                {
                    if (ext.Equals(".ttl"))
                    {
                        context.Format = FolderStoreFormat.Turtle;
                    }
                    else if (ext.Equals(".n3"))
                    {
                        context.Format = FolderStoreFormat.Notation3;
                    }
                    else if (ext.Equals(".rdf"))
                    {
                        context.Format = FolderStoreFormat.RdfXml;
                    }
                    else
                    {
                        throw new RdfStorageException("Folder Store Format auto-detection failed");
                    }
                }

                String file;
                while (!graphlist.EndOfStream)
                {
                    file = graphlist.ReadLine();
                    if (!file.Equals(String.Empty))
                    {
                        context.Add(file);
                    }
                }
                graphlist.Close();

                //Start making the async calls
                List <IAsyncResult> results = new List <IAsyncResult>();
                LoadGraphsDelegate  d       = new LoadGraphsDelegate(this.LoadGraphs);
                for (int i = 0; i < context.Threads; i++)
                {
                    results.Add(d.BeginInvoke(context, null, null));
                }

                //Wait for all the async calls to complete
                WaitHandle.WaitAll(results.Select(r => r.AsyncWaitHandle).ToArray());
                RdfThreadedParsingException parsingEx = new RdfThreadedParsingException("One/more errors occurred while parsing RDF from a Folder Store using a multi-threaded parsing process");
                foreach (IAsyncResult result in results)
                {
                    try
                    {
                        d.EndInvoke(result);
                    }
                    catch (Exception ex)
                    {
                        parsingEx.AddException(ex);
                    }
                }

                //If there were any errors we'll throw an RdfThreadedOutputException now
                if (parsingEx.InnerExceptions.Any())
                {
                    throw parsingEx;
                }
            }
            else
            {
                throw new RdfStorageException("Parameters for the FolderStoreReader must be of type FolderStoreParams");
            }
        }
Example #16
0
        public void Load(IRdfHandler handler, IStoreParams parameters)
        {
            if (handler == null)
            {
                throw new ArgumentNullException("handler", "Cannot parse an RDF Dataset using a null RDF Handler");
            }
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters", "Cannot parse an RDF Dataset using null Parameters");
            }

            //Try and get the Input from the parameters
            TextReader input = null;

            if (parameters is StreamParams)
            {
                //Get Input Stream
                input = ((StreamParams)parameters).StreamReader;
            }
            else if (parameters is TextReaderParams)
            {
                input = ((TextReaderParams)parameters).TextReader;
            }

            if (input != null)
            {
                //First try and load as XML and apply any stylesheets
                try
                {
                    //Get the reader and start parsing
                    XmlReader reader = XmlReader.Create(input, this.GetSettings());
                    this.RaiseWarning("The TriX Parser is operating without XSL support, if your TriX file requires XSL then it will not be parsed successfully");
                    this.TryParseGraphset(reader, handler);

                    input.Close();
                }
                catch (XmlException xmlEx)
                {
                    try
                    {
                        input.Close();
                    }
                    catch
                    {
                        //No catch actions - just cleaning up
                    }
                    //Wrap in a RDF Parse Exception
                    throw new RdfParseException("Unable to Parse this TriX since the XmlReader encountered an error, see the inner exception for details", xmlEx);
                }
                catch
                {
                    try
                    {
                        input.Close();
                    }
                    catch
                    {
                        //No catch actions - just cleaning up
                    }
                    throw;
                }
            }
            else
            {
                throw new RdfStorageException("Parameters for the TriXParser must be of the type StreamParams/TextReaderParams");
            }
        }
        /// <summary>
        /// Loads RDF using the RDF Handler using the settings the Reader was instantiated with
        /// </summary>
        /// <param name="handler">RDF Handler to use</param>
        /// <param name="parameters">Parameters indicating where to read from</param>
        public void Load(IRdfHandler handler, IStoreParams parameters)
        {
            if (parameters is FolderStoreParams)
            {
                //Create the Parser Context
                FolderStoreParserContext context = new FolderStoreParserContext(handler, (FolderStoreParams)parameters);

                //Create the Folder
                if (!Directory.Exists(context.Folder))
                {
                    throw new RdfStorageException("Cannot read a Folder Store from a Folder that doesn't exist");
                }

                //Read list of Graphs and Queue Filenames of Graphs for reading by the Threads
                StreamReader graphlist = new StreamReader(Path.Combine(context.Folder, "graphs.fstore"));

                //First line contains format information
                String ext = graphlist.ReadLine();
                if (context.Format == FolderStoreFormat.AutoDetect)
                {
                    if (ext.Equals(".ttl"))
                    {
                        context.Format = FolderStoreFormat.Turtle;
                    }
                    else if (ext.Equals(".n3"))
                    {
                        context.Format = FolderStoreFormat.Notation3;
                    }
                    else if (ext.Equals(".rdf"))
                    {
                        context.Format = FolderStoreFormat.RdfXml;
                    }
                    else
                    {
                        throw new RdfStorageException("Folder Store Format auto-detection failed");
                    }
                }

                String file;
                while (!graphlist.EndOfStream)
                {
                    file = graphlist.ReadLine();
                    if (!file.Equals(String.Empty))
                    {
                        context.Add(file);
                    }
                }
                graphlist.Close();

                //Start making the async calls
                List<IAsyncResult> results = new List<IAsyncResult>();
                LoadGraphsDelegate d = new LoadGraphsDelegate(this.LoadGraphs);
                for (int i = 0; i < context.Threads; i++)
                {
                    results.Add(d.BeginInvoke(context, null, null));
                }

                //Wait for all the async calls to complete
                WaitHandle.WaitAll(results.Select(r => r.AsyncWaitHandle).ToArray());
                RdfThreadedParsingException parsingEx = new RdfThreadedParsingException("One/more errors occurred while parsing RDF from a Folder Store using a multi-threaded parsing process");
                foreach (IAsyncResult result in results)
                {
                    try
                    {
                        d.EndInvoke(result);
                    }
                    catch (Exception ex)
                    {
                        parsingEx.AddException(ex);
                    }
                }

                //If there were any errors we'll throw an RdfThreadedOutputException now
                if (parsingEx.InnerExceptions.Any()) throw parsingEx;
            }
            else
            {
                throw new RdfStorageException("Parameters for the FolderStoreReader must be of type FolderStoreParams");
            }
        }
Example #18
0
        /// <summary>
        /// Loads the RDF Dataset from the TriX input using a RDF Handler
        /// </summary>
        /// <param name="handler">RDF Handler to use</param>
        /// <param name="parameters">Parameters indicating the input to read from</param>
        public void Load(IRdfHandler handler, IStoreParams parameters)
        {
            if (handler == null)
            {
                throw new ArgumentNullException("handler", "Cannot parse an RDF Dataset using a null RDF Handler");
            }
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters", "Cannot parse an RDF Dataset using null Parameters");
            }

            //Try and get the Input from the parameters
            TextReader input = null;

            if (parameters is StreamParams)
            {
                //Get Input Stream
                input = ((StreamParams)parameters).StreamReader;

                //Issue a Warning if the Encoding of the Stream is not UTF-8
                if (!((StreamReader)input).CurrentEncoding.Equals(Encoding.UTF8))
                {
                    this.RaiseWarning("Expected Input Stream to be encoded as UTF-8 but got a Stream encoded as " + ((StreamReader)input).CurrentEncoding.EncodingName + " - Please be aware that parsing errors may occur as a result");
                }
            }
            else if (parameters is TextReaderParams)
            {
                input = ((TextReaderParams)parameters).TextReader;
            }

            if (input != null)
            {
                //First try and load as XML and apply any stylesheets
                try
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(input);

                    input.Close();

                    XmlDocument inputDoc   = new XmlDocument();
                    bool        inputReady = false;

                    //If XSL isn't supported we can't apply it
#if !NO_XSL
                    //Try and apply any stylesheets (which are used to extend TriX) to get basic TriX syntax
                    foreach (XmlNode child in doc.ChildNodes)
                    {
                        if (child.NodeType == XmlNodeType.ProcessingInstruction)
                        {
                            if (child.Name == "xml-stylesheet")
                            {
                                //Load in the XML a 2nd time so we can transform it properly if needed
                                if (!inputReady)
                                {
                                    inputDoc.LoadXml(doc.OuterXml);
                                    inputReady = true;
                                }

                                Regex  getStylesheetURI = new Regex("href=\"([^\"]+)\"");
                                String stylesheetUri    = getStylesheetURI.Match(child.Value).Groups[1].Value;

                                //Load the Transform
                                XslCompiledTransform transform = new XslCompiledTransform();
                                XsltSettings         settings  = new XsltSettings();
                                transform.Load(stylesheetUri, settings, null);

                                //Apply the Transform
                                MemoryStream temp = new MemoryStream();
                                transform.Transform(inputDoc, XmlWriter.Create(temp));
                                temp.Flush();
                                temp.Seek(0, SeekOrigin.Begin);
                                inputDoc.Load(temp);
                            }
                        }
                    }
#endif

                    //Start parsing
                    if (!inputReady)
                    {
                        inputDoc = doc;
                    }
                    this.TryParseGraphset(inputDoc, handler);

                    input.Close();
                }
                catch (XmlException xmlEx)
                {
                    try
                    {
                        input.Close();
                    }
                    catch
                    {
                        //No catch actions - just cleaning up
                    }
                    //Wrap in a RDF Parse Exception
                    throw new RdfParseException("Unable to Parse this TriX since System.Xml was unable to parse the document into a DOM Tree, see the inner exception for details", xmlEx);
                }
                catch
                {
                    try
                    {
                        input.Close();
                    }
                    catch
                    {
                        //No catch actions - just cleaning up
                    }
                    throw;
                }
            }
            else
            {
                throw new RdfStorageException("Parameters for the TriXParser must be of the type StreamParams/TextReaderParams");
            }
        }
Example #19
0
        /// <summary>
        /// Saves a Triple Store to TSV format
        /// </summary>
        /// <param name="store">Triple Store to save</param>
        /// <param name="parameters">A set of <see cref="StreamParams">StreamParams</see></param>
        public void Save(ITripleStore store, IStoreParams parameters)
        {
            ThreadedStoreWriterContext context = null;

            if (parameters is StreamParams)
            {
                //Create a new Writer Context
                context = new ThreadedStoreWriterContext(store, ((StreamParams)parameters).StreamWriter);
            }
            else if (parameters is TextWriterParams)
            {
                context = new ThreadedStoreWriterContext(store, ((TextWriterParams)parameters).TextWriter);
            }

            if (context != null)
            {
                //Check there's something to do
                if (context.Store.Graphs.Count == 0)
                {
                    context.Output.Close();
                    return;
                }

                //Queue the Graphs to be written
                foreach (IGraph g in context.Store.Graphs)
                {
                    context.Add(g.BaseUri);
                }

                //Start making the async calls
                List <IAsyncResult> results = new List <IAsyncResult>();
                SaveGraphsDelegate  d       = new SaveGraphsDelegate(this.SaveGraphs);
                for (int i = 0; i < this._threads; i++)
                {
                    results.Add(d.BeginInvoke(context, null, null));
                }

                //Wait for all the async calls to complete
                WaitHandle.WaitAll(results.Select(r => r.AsyncWaitHandle).ToArray());
                RdfThreadedOutputException outputEx = new RdfThreadedOutputException(WriterErrorMessages.ThreadedOutputFailure("TSV"));
                foreach (IAsyncResult result in results)
                {
                    try
                    {
                        d.EndInvoke(result);
                    }
                    catch (Exception ex)
                    {
                        outputEx.AddException(ex);
                    }
                }
                context.Output.Close();

                //If there were any errors we'll throw an RdfThreadedOutputException now
                if (outputEx.InnerExceptions.Any())
                {
                    throw outputEx;
                }
            }
            else
            {
                throw new RdfStorageException("Parameters for the TsvStoreWriter must be of the type StreamParams/TextWriterParams");
            }
        }
Example #20
0
        /// <summary>
        /// Loads a RDF Dataset from the NQuads input into the given Triple Store
        /// </summary>
        /// <param name="store">Triple Store to load into</param>
        /// <param name="parameters">Parameters indicating the Stream to read from</param>
        public void Load(ITripleStore store, IStoreParams parameters)
        {
            if (store == null) throw new RdfParseException("Cannot read a RDF dataset into a null Store");

            this.Load(new StoreHandler(store), parameters);
        }
Example #21
0
        /// <summary>
        /// Saves a Triple Store to CSV Format
        /// </summary>
        /// <param name="store">Triple Store to save</param>
        /// <param name="parameters">A set of <see cref="StreamParams">StreamParams</see></param>
        public void Save(ITripleStore store, IStoreParams parameters)
        {
            ThreadedStoreWriterContext context = null;
            if (parameters is StreamParams)
            {
                //Create a new Writer Context
                context = new ThreadedStoreWriterContext(store, ((StreamParams)parameters).StreamWriter);
            }
            else if (parameters is TextWriterParams)
            {
                context = new ThreadedStoreWriterContext(store, ((TextWriterParams)parameters).TextWriter);
            }

            if (context != null)
            {
                //Check there's something to do
                if (context.Store.Graphs.Count == 0)
                {
                    context.Output.Close();
                    return;
                }

                //Queue the Graphs to be written
                foreach (IGraph g in context.Store.Graphs)
                {
                    context.Add(g.BaseUri);
                }

                //Start making the async calls
                List<IAsyncResult> results = new List<IAsyncResult>();
                SaveGraphsDeletegate d = new SaveGraphsDeletegate(this.SaveGraphs);
                for (int i = 0; i < this._threads; i++)
                {
                    results.Add(d.BeginInvoke(context, null, null));
                }

                //Wait for all the async calls to complete
                WaitHandle.WaitAll(results.Select(r => r.AsyncWaitHandle).ToArray());
                RdfThreadedOutputException outputEx = new RdfThreadedOutputException(WriterErrorMessages.ThreadedOutputFailure("CSV"));
                foreach (IAsyncResult result in results)
                {
                    try
                    {
                        d.EndInvoke(result);
                    }
                    catch (Exception ex)
                    {
                        outputEx.AddException(ex);
                    }
                }
                context.Output.Close();

                //If there were any errors we'll throw an RdfThreadedOutputException now
                if (outputEx.InnerExceptions.Any()) throw outputEx;
            }
            else
            {
                throw new RdfStorageException("Parameters for the CsvStoreWriter must be of the type StreamParams/TextWriterParams");
            }
        }
Example #22
0
        /// <summary>
        /// Loads a RDF Dataset from the NQuads input using a RDF Handler
        /// </summary>
        /// <param name="handler">RDF Handler to use</param>
        /// <param name="parameters">Parameters indicating the Stream to read from</param>
        public void Load(IRdfHandler handler, IStoreParams parameters)
        {
            if (handler == null)
            {
                throw new ArgumentNullException("handler", "Cannot parse an RDF Dataset using a null RDF Handler");
            }
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters", "Cannot parse an RDF Dataset using null Parameters");
            }

            //Try and get the Input from the parameters
            TextReader input = null;

            if (parameters is StreamParams)
            {
                //Get Input Stream
                input = ((StreamParams)parameters).StreamReader;

#if !SILVERLIGHT
                //Issue a Warning if the Encoding of the Stream is not ASCII
                if (!((StreamReader)input).CurrentEncoding.Equals(Encoding.ASCII))
                {
                    this.RaiseWarning("Expected Input Stream to be encoded as ASCII but got a Stream encoded as " + ((StreamReader)input).CurrentEncoding.EncodingName + " - Please be aware that parsing errors may occur as a result");
                }
#endif
            }
            else if (parameters is TextReaderParams)
            {
                input = ((TextReaderParams)parameters).TextReader;
            }

            if (input != null)
            {
                try
                {
                    //Setup Token Queue and Tokeniser
                    NTriplesTokeniser tokeniser = new NTriplesTokeniser(input);
                    tokeniser.NQuadsMode = true;
                    TokenQueue tokens = new TokenQueue();
                    tokens.Tokeniser = tokeniser;
                    tokens.Tracing   = this._tracetokeniser;
                    tokens.InitialiseBuffer();

                    //Invoke the Parser
                    this.Parse(handler, tokens);
                }
                catch
                {
                    throw;
                }
                finally
                {
                    try
                    {
                        input.Close();
                    }
                    catch
                    {
                        //No catch actions - just cleaning up
                    }
                }
            }
            else
            {
                throw new RdfStorageException("Parameters for the NQuadsParser must be of the type StreamParams/TextReaderParams");
            }
        }
Example #23
0
        /// <summary>
        /// Saves a Store in NQuads format
        /// </summary>
        /// <param name="store">Store to save</param>
        /// <param name="parameters">Parameters indicating a Stream to write to</param>
        public void Save(ITripleStore store, IStoreParams parameters)
        {
            ThreadedStoreWriterContext context = null;

            if (parameters is StreamParams)
            {
                //Create a new Writer Context
#if !SILVERLIGHT
                ((StreamParams)parameters).Encoding = Encoding.ASCII;
#endif
                context = new ThreadedStoreWriterContext(store, ((StreamParams)parameters).StreamWriter, this._prettyPrint, false);
            }
            else if (parameters is TextWriterParams)
            {
                context = new ThreadedStoreWriterContext(store, ((TextWriterParams)parameters).TextWriter, this._prettyPrint, false);
            }

            if (context != null)
            {
                //Check there's something to do
                if (context.Store.Graphs.Count == 0)
                {
                    context.Output.Close();
                    return;
                }

                try
                {
                    if (this._multiThreaded)
                    {
                        //Queue the Graphs to be written
                        foreach (IGraph g in context.Store.Graphs)
                        {
                            if (g.BaseUri == null)
                            {
                                context.Add(UriFactory.Create(GraphCollection.DefaultGraphUri));
                            }
                            else
                            {
                                context.Add(g.BaseUri);
                            }
                        }

                        //Start making the async calls
                        List <IAsyncResult> results = new List <IAsyncResult>();
                        SaveGraphsDelegate  d       = new SaveGraphsDelegate(this.SaveGraphs);
                        for (int i = 0; i < this._threads; i++)
                        {
                            results.Add(d.BeginInvoke(context, null, null));
                        }

                        //Wait for all the async calls to complete
                        WaitHandle.WaitAll(results.Select(r => r.AsyncWaitHandle).ToArray());
                        RdfThreadedOutputException outputEx = new RdfThreadedOutputException(WriterErrorMessages.ThreadedOutputFailure("TSV"));
                        foreach (IAsyncResult result in results)
                        {
                            try
                            {
                                d.EndInvoke(result);
                            }
                            catch (Exception ex)
                            {
                                outputEx.AddException(ex);
                            }
                        }
                        context.Output.Close();

                        //If there were any errors we'll throw an RdfThreadedOutputException now
                        if (outputEx.InnerExceptions.Any())
                        {
                            throw outputEx;
                        }
                    }
                    else
                    {
                        foreach (IGraph g in context.Store.Graphs)
                        {
                            NTriplesWriterContext graphContext = new NTriplesWriterContext(g, context.Output);
                            foreach (Triple t in g.Triples)
                            {
                                context.Output.WriteLine(this.TripleToNQuads(graphContext, t));
                            }
                        }
                        context.Output.Close();
                    }
                }
                catch
                {
                    try
                    {
                        context.Output.Close();
                    }
                    catch
                    {
                        //Just cleaning up
                    }
                    throw;
                }
            }
            else
            {
                throw new RdfStorageException("Parameters for the NQuadsWriter must be of the type StreamParams/TextWriterParams");
            }
        }
Example #24
0
        /// <summary>
        /// Loads a RDF dataset from GZipped input
        /// </summary>
        /// <param name="handler">RDF Handler to use</param>
        /// <param name="parameters">Store Parameters</param>
        public void Load(IRdfHandler handler, IStoreParams parameters)
        {
            if (handler == null) throw new RdfParseException("Cannot parse RDF Dataset using a null Handler");
            if (parameters == null) throw new RdfParseException("Cannot parse RDF Dataset from null parameters");

            if (parameters is StreamParams)
            {
                StreamParams sp = (StreamParams)parameters;
                StreamReader input = sp.StreamReader;

                if (input.BaseStream is GZipStream)
                {
                    this._parser.Load(handler, sp);
                }
                else
                {
                    //Force the inner stream to be GZipped
                    this._parser.Load(handler, new StreamParams(new GZipStream(input.BaseStream, CompressionMode.Decompress)));
                }
            }
            else
            {
                throw new RdfParseException("GZip Dataset Parsers can only read from StreamParams instances");
            }
        }
Example #25
0
        /// <summary>
        /// Saves a RDF Dataset as GZipped output
        /// </summary>
        /// <param name="store">Store to save</param>
        /// <param name="parameters">Storage Parameters</param>
        public void Save(ITripleStore store, IStoreParams parameters)
        {
            if (store == null) throw new RdfOutputException("Cannot output a new Triple Store");
            if (parameters == null) throw new RdfOutputException("Cannot output using null parameters");

            if (parameters is StreamParams)
            {
                StreamParams sp = (StreamParams)parameters;
                StreamWriter output = sp.StreamWriter;

                if (output.BaseStream is GZipStream)
                {
                    this._writer.Save(store, sp);
                }
                else
                {
                    this._writer.Save(store, new StreamParams(new GZipStream(output.BaseStream, CompressionMode.Compress)));
                }
            }
            else
            {
                throw new RdfOutputException("GZip Dataset Writers can only write to StreamParams instances");
            }
        }
Example #26
0
        /// <summary>
        /// Saves the given Triple Store to the Target Store that this class was instantiated with
        /// </summary>
        /// <param name="store">Store to Save</param>
        /// <param name="parameters">Parameters for the Store</param>
        public override void Save(ITripleStore store, IStoreParams parameters)
        {
            if (parameters is ThreadedSqlIOParams)
            {
                //Setup context
                ThreadedSqlStoreWriterContext context = new ThreadedSqlStoreWriterContext(store, (ThreadedSqlIOParams)parameters);
                bool transSetting = context.Manager.DisableTransactions;
                context.Manager.DisableTransactions = true;

                //Queue Graphs for Writing
                foreach (IGraph g in store.Graphs)
                {
                    context.Add(g);
                }

                //Start making the async calls
                List<IAsyncResult> results = new List<IAsyncResult>();
                WriteGraphsDelegate d = new WriteGraphsDelegate(this.WriteGraphs);
                for (int i = 0; i < context.Threads; i++)
                {
                    results.Add(d.BeginInvoke(context, null, null));
                }

                //Wait for all the async calls to complete
                WaitHandle.WaitAll(results.Select(r => r.AsyncWaitHandle).ToArray());
                RdfThreadedOutputException outputEx = new RdfThreadedOutputException(WriterErrorMessages.ThreadedOutputFailure("Folder Store"));
                foreach (IAsyncResult result in results)
                {
                    try
                    {
                        d.EndInvoke(result);
                    }
                    catch (Exception ex)
                    {
                        outputEx.AddException(ex);
                    }
                }

                //Reset Transactions setting
                context.Manager.DisableTransactions = transSetting;

                //If there were any errors we'll throw an RdfThreadedOutputException now
                if (outputEx.InnerExceptions.Any()) throw outputEx;

            }
            else
            {
                throw new RdfStorageException("Parameters for the ThreadedSQLStoreWriter must be of type ThreadedSQLIOParams");
            }
        }
        /// <summary>
        /// Saves the given Store to Disk using the settings the Writer was instantiated with
        /// </summary>
        /// <param name="store">Store to save</param>
        /// <param name="parameters">Parameters indicating where to save to</param>
        public void Save(ITripleStore store, IStoreParams parameters)
        {
            if (parameters is FolderStoreParams)
            {
                //Get the Parameters
                FolderStoreWriterContext context = new FolderStoreWriterContext(store, (FolderStoreParams)parameters);

                //Create the Folder
                if (!Directory.Exists(context.Folder))
                {
                    Directory.CreateDirectory(context.Folder);
                }

                //Write list of Graphs and Queue URIs of Graphs for writing by the async calls
                StreamWriter graphlist = new StreamWriter(Path.Combine(context.Folder,"graphs.fstore"));
                String ext;
                switch (context.Format)
                {
                    case FolderStoreFormat.Turtle:
                        ext = ".ttl";
                        break;
                    case FolderStoreFormat.Notation3:
                        ext = ".n3";
                        break;
                    case FolderStoreFormat.RdfXml:
                        ext = ".rdf";
                        break;
                    default:
                        ext = ".ttl";
                        break;
                }
                graphlist.WriteLine(ext);
                foreach (Uri u in context.Store.Graphs.GraphUris)
                {
                    context.Add(u);
                    graphlist.WriteLine(u.GetSha256Hash() + ext);
                }
                graphlist.Close();

                //Start making the async calls
                List<IAsyncResult> results = new List<IAsyncResult>();
                SaveGraphsDelegate d = new SaveGraphsDelegate(this.SaveGraphs);
                for (int i = 0; i < context.Threads; i++)
                {
                    results.Add(d.BeginInvoke(context, null, null));
                }

                //Wait for all the async calls to complete
                WaitHandle.WaitAll(results.Select(r => r.AsyncWaitHandle).ToArray());
                RdfThreadedOutputException outputEx = new RdfThreadedOutputException(WriterErrorMessages.ThreadedOutputFailure("Folder Store"));
                foreach (IAsyncResult result in results)
                {
                    try
                    {
                        d.EndInvoke(result);
                    }
                    catch (Exception ex)
                    {
                        outputEx.AddException(ex);
                    }
                }

                //If there were any errors we'll throw an RdfThreadedOutputException now
                if (outputEx.InnerExceptions.Any()) throw outputEx;
            }
            else
            {
                throw new RdfStorageException("Parameters for the FolderStoreWriter must be of type FolderStoreParams");
            }
        }
Example #28
0
        /// <summary>
        /// Saves the given Store to Disk using the settings the Writer was instantiated with
        /// </summary>
        /// <param name="store">Store to save</param>
        /// <param name="parameters">Parameters indicating where to save to</param>
        public void Save(ITripleStore store, IStoreParams parameters)
        {
            if (parameters is FolderStoreParams)
            {
                //Get the Parameters
                FolderStoreWriterContext context = new FolderStoreWriterContext(store, (FolderStoreParams)parameters);

                //Create the Folder
                if (!Directory.Exists(context.Folder))
                {
                    Directory.CreateDirectory(context.Folder);
                }

                //Write list of Graphs and Queue URIs of Graphs for writing by the async calls
                StreamWriter graphlist = new StreamWriter(Path.Combine(context.Folder, "graphs.fstore"));
                String       ext;
                switch (context.Format)
                {
                case FolderStoreFormat.Turtle:
                    ext = ".ttl";
                    break;

                case FolderStoreFormat.Notation3:
                    ext = ".n3";
                    break;

                case FolderStoreFormat.RdfXml:
                    ext = ".rdf";
                    break;

                default:
                    ext = ".ttl";
                    break;
                }
                graphlist.WriteLine(ext);
                foreach (Uri u in context.Store.Graphs.GraphUris)
                {
                    context.Add(u);
                    graphlist.WriteLine(u.GetSha256Hash() + ext);
                }
                graphlist.Close();

                //Start making the async calls
                List <IAsyncResult> results = new List <IAsyncResult>();
                SaveGraphsDelegate  d       = new SaveGraphsDelegate(this.SaveGraphs);
                for (int i = 0; i < context.Threads; i++)
                {
                    results.Add(d.BeginInvoke(context, null, null));
                }

                //Wait for all the async calls to complete
                WaitHandle.WaitAll(results.Select(r => r.AsyncWaitHandle).ToArray());
                RdfThreadedOutputException outputEx = new RdfThreadedOutputException(WriterErrorMessages.ThreadedOutputFailure("Folder Store"));
                foreach (IAsyncResult result in results)
                {
                    try
                    {
                        d.EndInvoke(result);
                    }
                    catch (Exception ex)
                    {
                        outputEx.AddException(ex);
                    }
                }

                //If there were any errors we'll throw an RdfThreadedOutputException now
                if (outputEx.InnerExceptions.Any())
                {
                    throw outputEx;
                }
            }
            else
            {
                throw new RdfStorageException("Parameters for the FolderStoreWriter must be of type FolderStoreParams");
            }
        }
Example #29
0
        /// <summary>
        /// Loads a RDF Dataset from the NQuads input using a RDF Handler
        /// </summary>
        /// <param name="handler">RDF Handler to use</param>
        /// <param name="parameters">Parameters indicating the Stream to read from</param>
        public void Load(IRdfHandler handler, IStoreParams parameters)
        {
            if (handler == null) throw new ArgumentNullException("handler", "Cannot parse an RDF Dataset using a null RDF Handler");
            if (parameters == null) throw new ArgumentNullException("parameters", "Cannot parse an RDF Dataset using null Parameters");

            //Try and get the Input from the parameters
            TextReader input = null;
            if (parameters is StreamParams)
            {
                //Get Input Stream
                input = ((StreamParams)parameters).StreamReader;

#if !SILVERLIGHT
                //Issue a Warning if the Encoding of the Stream is not ASCII
                if (!((StreamReader)input).CurrentEncoding.Equals(Encoding.ASCII))
                {
                    this.RaiseWarning("Expected Input Stream to be encoded as ASCII but got a Stream encoded as " + ((StreamReader)input).CurrentEncoding.EncodingName + " - Please be aware that parsing errors may occur as a result");
                }
#endif
            } 
            else if (parameters is TextReaderParams)
            {
                input = ((TextReaderParams)parameters).TextReader;
            }

            if (input != null)
            {
                try
                {
                    //Setup Token Queue and Tokeniser
                    NTriplesTokeniser tokeniser = new NTriplesTokeniser(input);
                    tokeniser.NQuadsMode = true;
                    TokenQueue tokens = new TokenQueue();
                    tokens.Tokeniser = tokeniser;
                    tokens.Tracing = this._tracetokeniser;
                    tokens.InitialiseBuffer();

                    //Invoke the Parser
                    this.Parse(handler, tokens);
                }
                catch
                {
                    throw;
                }
                finally
                {
                    try
                    {
                        input.Close();
                    }
                    catch
                    {
                        //No catch actions - just cleaning up
                    }
                }
            }
            else
            {
                throw new RdfStorageException("Parameters for the NQuadsParser must be of the type StreamParams/TextReaderParams");
            }
        }
Example #30
0
        /// <summary>
        /// Saves a Store in TriG (Turtle with Named Graphs) format
        /// </summary>
        /// <param name="store">Store to save</param>
        /// <param name="parameters">Parameters indicating a Stream to write to</param>
        public void Save(ITripleStore store, IStoreParams parameters)
        {
            //Try and determine the TextWriter to output to
            TriGWriterContext context = null;
            if (parameters is StreamParams)
            {
                //Create a new Writer Context
                ((StreamParams)parameters).Encoding = new UTF8Encoding(Options.UseBomForUtf8);
                context = new TriGWriterContext(store, ((StreamParams)parameters).StreamWriter, this._prettyprint, this._allowHiSpeed, this._compressionLevel, this._n3compat);
            } 
            else if (parameters is TextWriterParams)
            {
                context = new TriGWriterContext(store, ((TextWriterParams)parameters).TextWriter, this._prettyprint, this._allowHiSpeed, this._compressionLevel, this._n3compat);
            }

            if (context != null)
            {
                //Check there's something to do
                if (context.Store.Graphs.Count == 0) 
                {
                    context.Output.Close();
                    return;
                }

                //Write the Header of the File
                foreach (IGraph g in context.Store.Graphs)
                {
                    context.NamespaceMap.Import(g.NamespaceMap);
                }
                if (context.CompressionLevel > WriterCompressionLevel.None)
                {
                    //Only add @prefix declarations if compression is enabled
                    context.QNameMapper = new ThreadSafeQNameOutputMapper(context.NamespaceMap);
                    foreach (String prefix in context.NamespaceMap.Prefixes)
                    {
                        if (TurtleSpecsHelper.IsValidQName(prefix + ":"))
                        {
                            context.Output.WriteLine("@prefix " + prefix + ": <" + context.FormatUri(context.NamespaceMap.GetNamespaceUri(prefix)) + ">.");
                        }
                    }
                    context.Output.WriteLine();
                }
                else
                {
                    context.QNameMapper = new ThreadSafeQNameOutputMapper(new NamespaceMapper(true));
                }

                if (this._useMultiThreading)
                {
                    //Standard Multi-Threaded Writing

                    //Queue the Graphs to be written
                    foreach (IGraph g in context.Store.Graphs)
                    {
                        if (g.BaseUri == null)
                        {
                            context.Add(new Uri(GraphCollection.DefaultGraphUri));
                        }
                        else
                        {
                            context.Add(g.BaseUri);
                        }
                    }

                    //Start making the async calls
                    List<IAsyncResult> results = new List<IAsyncResult>();
                    SaveGraphsDelegate d = new SaveGraphsDelegate(this.SaveGraphs);
                    for (int i = 0; i < this._threads; i++)
                    {
                        results.Add(d.BeginInvoke(context, null, null));
                    }

                    //Wait for all the async calls to complete
                    WaitHandle.WaitAll(results.Select(r => r.AsyncWaitHandle).ToArray());
                    RdfThreadedOutputException outputEx = new RdfThreadedOutputException(WriterErrorMessages.ThreadedOutputFailure("TriG"));
                    foreach (IAsyncResult result in results)
                    {
                        try
                        {
                            d.EndInvoke(result);
                        }
                        catch (Exception ex)
                        {
                            outputEx.AddException(ex);
                        }
                    }
                    //Make sure to close the output
                    context.Output.Close();

                    //If there were any errors we'll throw an RdfThreadedOutputException now
                    if (outputEx.InnerExceptions.Any()) throw outputEx;
                }
                else
                {
                    try
                    {
                        //Optional Single Threaded Writing
                        foreach (IGraph g in store.Graphs)
                        {
                            TurtleWriterContext graphContext = new TurtleWriterContext(g, new System.IO.StringWriter(), context.PrettyPrint, context.HighSpeedModePermitted);
                            if (context.CompressionLevel > WriterCompressionLevel.None)
                            {
                                graphContext.NodeFormatter = new TurtleFormatter(context.QNameMapper);
                            }
                            else
                            {
                                graphContext.NodeFormatter = new UncompressedTurtleFormatter();
                            }
                            context.Output.WriteLine(this.GenerateGraphOutput(context, graphContext));
                        }
                        
                        //Make sure to close the output
                        context.Output.Close();
                    }
                    catch
                    {
                        try
                        {
                            //Close the output
                            context.Output.Close();
                        }
                        catch
                        {
                            //No catch actions, just cleaning up the output stream
                        }
                        throw;
                    }
                }
            }
            else
            {
                throw new RdfStorageException("Parameters for the TriGWriter must be of the type StreamParams/TextWriterParams");
            }
        }
Example #31
0
        /// <summary>
        /// Saves a Store in NQuads format
        /// </summary>
        /// <param name="store">Store to save</param>
        /// <param name="parameters">Parameters indicating a Stream to write to</param>
        public void Save(ITripleStore store, IStoreParams parameters)
        {
            ThreadedStoreWriterContext context = null;
            if (parameters is StreamParams)
            {
                //Create a new Writer Context
            #if !SILVERLIGHT
                ((StreamParams)parameters).Encoding = Encoding.ASCII;
            #endif
                context = new ThreadedStoreWriterContext(store, ((StreamParams)parameters).StreamWriter, this._prettyPrint, false);
            }
            else if (parameters is TextWriterParams)
            {
                context = new ThreadedStoreWriterContext(store, ((TextWriterParams)parameters).TextWriter, this._prettyPrint, false);
            }

            if (context != null)
            {
                //Check there's something to do
                if (context.Store.Graphs.Count == 0)
                {
                    context.Output.Close();
                    return;
                }

                try
                {
                    if (this._multiThreaded)
                    {
                        //Queue the Graphs to be written
                        foreach (IGraph g in context.Store.Graphs)
                        {
                            if (g.BaseUri == null)
                            {
                                context.Add(UriFactory.Create(GraphCollection.DefaultGraphUri));
                            }
                            else
                            {
                                context.Add(g.BaseUri);
                            }
                        }

                        //Start making the async calls
                        List<IAsyncResult> results = new List<IAsyncResult>();
                        SaveGraphsDelegate d = new SaveGraphsDelegate(this.SaveGraphs);
                        for (int i = 0; i < this._threads; i++)
                        {
                            results.Add(d.BeginInvoke(context, null, null));
                        }

                        //Wait for all the async calls to complete
                        WaitHandle.WaitAll(results.Select(r => r.AsyncWaitHandle).ToArray());
                        RdfThreadedOutputException outputEx = new RdfThreadedOutputException(WriterErrorMessages.ThreadedOutputFailure("TSV"));
                        foreach (IAsyncResult result in results)
                        {
                            try
                            {
                                d.EndInvoke(result);
                            }
                            catch (Exception ex)
                            {
                                outputEx.AddException(ex);
                            }
                        }
                        context.Output.Close();

                        //If there were any errors we'll throw an RdfThreadedOutputException now
                        if (outputEx.InnerExceptions.Any()) throw outputEx;
                    }
                    else
                    {
                        foreach (IGraph g in context.Store.Graphs)
                        {
                            NTriplesWriterContext graphContext = new NTriplesWriterContext(g, context.Output);
                            foreach (Triple t in g.Triples)
                            {
                                context.Output.WriteLine(this.TripleToNQuads(graphContext, t));
                            }
                        }
                        context.Output.Close();
                    }
                }
                catch
                {
                    try
                    {
                        context.Output.Close();
                    }
                    catch
                    {
                        //Just cleaning up
                    }
                    throw;
                }
            }
            else
            {
                throw new RdfStorageException("Parameters for the NQuadsWriter must be of the type StreamParams/TextWriterParams");
            }
        }
Example #32
0
        /// <summary>
        /// Loads the named Graphs from the TriG input using the given RDF Handler
        /// </summary>
        /// <param name="handler">RDF Handler to use</param>
        /// <param name="parameters">Parameters indicating the input to read from</param>
        public void Load(IRdfHandler handler, IStoreParams parameters)
        {
            if (handler == null)
            {
                throw new ArgumentNullException("handler", "Cannot parse an RDF Dataset using a null RDF Handler");
            }
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters", "Cannot parse an RDF Dataset using null Parameters");
            }

            //Try and get the Input from the parameters
            TextReader input = null;

            if (parameters is StreamParams)
            {
                //Get Input Stream
                input = ((StreamParams)parameters).StreamReader;

                //Issue a Warning if the Encoding of the Stream is not UTF-8
                if (!((StreamReader)input).CurrentEncoding.Equals(Encoding.UTF8))
                {
#if !SILVERLIGHT
                    this.RaiseWarning("Expected Input Stream to be encoded as UTF-8 but got a Stream encoded as " + ((StreamReader)input).CurrentEncoding.EncodingName + " - Please be aware that parsing errors may occur as a result");
#else
                    this.RaiseWarning("Expected Input Stream to be encoded as UTF-8 but got a Stream encoded as " + ((StreamReader)input).CurrentEncoding.GetType().Name + " - Please be aware that parsing errors may occur as a result");
#endif
                }
            }
            else if (parameters is TextReaderParams)
            {
                input = ((TextReaderParams)parameters).TextReader;
            }

            if (input != null)
            {
                try
                {
                    //Create the Parser Context and Invoke the Parser
                    TriGParserContext context = new TriGParserContext(handler, new TriGTokeniser(input), TokenQueueMode.SynchronousBufferDuringParsing, false, this._tracetokeniser);
                    this.Parse(context);
                }
                catch
                {
                    throw;
                }
                finally
                {
                    try
                    {
                        input.Close();
                    }
                    catch
                    {
                        //No catch actions just cleaning up
                    }
                }
            }
            else
            {
                throw new RdfStorageException("Parameters for the TriGParser must be of the type StreamParams/TextReaderParams");
            }
        }
Example #33
0
        /// <summary>
        /// Saves a Store in TriG (Turtle with Named Graphs) format
        /// </summary>
        /// <param name="store">Store to save</param>
        /// <param name="parameters">Parameters indicating a Stream to write to</param>
        public void Save(ITripleStore store, IStoreParams parameters)
        {
            //Try and determine the TextWriter to output to
            TriGWriterContext context = null;

            if (parameters is StreamParams)
            {
                //Create a new Writer Context
                ((StreamParams)parameters).Encoding = new UTF8Encoding(Options.UseBomForUtf8);
                context = new TriGWriterContext(store, ((StreamParams)parameters).StreamWriter, this._prettyprint, this._allowHiSpeed, this._compressionLevel, this._n3compat);
            }
            else if (parameters is TextWriterParams)
            {
                context = new TriGWriterContext(store, ((TextWriterParams)parameters).TextWriter, this._prettyprint, this._allowHiSpeed, this._compressionLevel, this._n3compat);
            }

            if (context != null)
            {
                //Check there's something to do
                if (context.Store.Graphs.Count == 0)
                {
                    context.Output.Close();
                    return;
                }

                //Write the Header of the File
                foreach (IGraph g in context.Store.Graphs)
                {
                    context.NamespaceMap.Import(g.NamespaceMap);
                }
                if (context.CompressionLevel > WriterCompressionLevel.None)
                {
                    //Only add @prefix declarations if compression is enabled
                    context.QNameMapper = new ThreadSafeQNameOutputMapper(context.NamespaceMap);
                    foreach (String prefix in context.NamespaceMap.Prefixes)
                    {
                        if (TurtleSpecsHelper.IsValidQName(prefix + ":"))
                        {
                            context.Output.WriteLine("@prefix " + prefix + ": <" + context.FormatUri(context.NamespaceMap.GetNamespaceUri(prefix)) + ">.");
                        }
                    }
                    context.Output.WriteLine();
                }
                else
                {
                    context.QNameMapper = new ThreadSafeQNameOutputMapper(new NamespaceMapper(true));
                }

                if (this._useMultiThreading)
                {
                    //Standard Multi-Threaded Writing

                    //Queue the Graphs to be written
                    foreach (IGraph g in context.Store.Graphs)
                    {
                        if (g.BaseUri == null)
                        {
                            context.Add(new Uri(GraphCollection.DefaultGraphUri));
                        }
                        else
                        {
                            context.Add(g.BaseUri);
                        }
                    }

                    //Start making the async calls
                    List <IAsyncResult> results = new List <IAsyncResult>();
                    SaveGraphsDelegate  d       = new SaveGraphsDelegate(this.SaveGraphs);
                    for (int i = 0; i < this._threads; i++)
                    {
                        results.Add(d.BeginInvoke(context, null, null));
                    }

                    //Wait for all the async calls to complete
                    WaitHandle.WaitAll(results.Select(r => r.AsyncWaitHandle).ToArray());
                    RdfThreadedOutputException outputEx = new RdfThreadedOutputException(WriterErrorMessages.ThreadedOutputFailure("TriG"));
                    foreach (IAsyncResult result in results)
                    {
                        try
                        {
                            d.EndInvoke(result);
                        }
                        catch (Exception ex)
                        {
                            outputEx.AddException(ex);
                        }
                    }
                    //Make sure to close the output
                    context.Output.Close();

                    //If there were any errors we'll throw an RdfThreadedOutputException now
                    if (outputEx.InnerExceptions.Any())
                    {
                        throw outputEx;
                    }
                }
                else
                {
                    try
                    {
                        //Optional Single Threaded Writing
                        foreach (IGraph g in store.Graphs)
                        {
                            TurtleWriterContext graphContext = new TurtleWriterContext(g, new System.IO.StringWriter(), context.PrettyPrint, context.HighSpeedModePermitted);
                            if (context.CompressionLevel > WriterCompressionLevel.None)
                            {
                                graphContext.NodeFormatter = new TurtleFormatter(context.QNameMapper);
                            }
                            else
                            {
                                graphContext.NodeFormatter = new UncompressedTurtleFormatter();
                            }
                            context.Output.WriteLine(this.GenerateGraphOutput(context, graphContext));
                        }

                        //Make sure to close the output
                        context.Output.Close();
                    }
                    catch
                    {
                        try
                        {
                            //Close the output
                            context.Output.Close();
                        }
                        catch
                        {
                            //No catch actions, just cleaning up the output stream
                        }
                        throw;
                    }
                }
            }
            else
            {
                throw new RdfStorageException("Parameters for the TriGWriter must be of the type StreamParams/TextWriterParams");
            }
        }
 /// <summary>
 /// Loads Graphs into the store using the settings the Reader was instantiated with
 /// </summary>
 /// <param name="store">Store to load into</param>
 /// <param name="parameters">Parameters indicating where to read from</param>
 public void Load(ITripleStore store, IStoreParams parameters)
 {
     if (store == null) throw new RdfParseException("Cannot read RDF from a Folder Store into a null Triple Store");
     this.Load(new StoreHandler(store), parameters);
 }