private void ParsingUsingWriteThroughHandler(Type formatterType)
        {
            if (!System.IO.File.Exists("temp.ttl"))
            {
                Graph g = new Graph();
                EmbeddedResourceLoader.Load(g, "VDS.RDF.Configuration.configuration.ttl");
                g.SaveToFile("temp.ttl");
            }

            WriteThroughHandler handler = new WriteThroughHandler(formatterType, Console.Out, false);
            TurtleParser parser = new TurtleParser();
            parser.Load(handler, "temp.ttl");
        }
        private void ParsingUsingWriteThroughHandler(Type formatterType)
        {
            if (!System.IO.File.Exists("write_through_handler_tests_temp.ttl"))
            {
                Graph g = new Graph();
                EmbeddedResourceLoader.Load(g, "VDS.RDF.Configuration.configuration.ttl");
                g.SaveToFile("write_through_handler_tests_temp.ttl");
            }

            WriteThroughHandler handler = new WriteThroughHandler(formatterType, Console.Out, false);
            TurtleParser        parser  = new TurtleParser();

            parser.Load(handler, "write_through_handler_tests_temp.ttl");
        }
예제 #3
0
        public IRdfHandler ToCache(Uri requestUri, Uri responseUri, String etag)
        {
            IRdfHandler handler = null;
            try
            {
                bool cacheTwice = !requestUri.ToString().Equals(responseUri.ToString(), StringComparison.OrdinalIgnoreCase);

                //Cache the ETag if present
                if (this._canCacheETag && etag != null && !etag.Equals(String.Empty))
                {
                    int id = requestUri.GetEnhancedHashCode();
                    bool requireAdd = false;
                    if (this._etags.ContainsKey(id))
                    {
                        if (!this._etags[id].Equals(etag))
                        {
                            //If the ETag has changed remove it and then re-add it
                            this.RemoveETag(requestUri);
                            requireAdd = true;
                        }
                    }
                    else
                    {
                        requireAdd = true;
                    }

                    if (requireAdd)
                    {
                        //Add a New ETag
                        this._etags.Add(id, etag);
                        using (StreamWriter writer = new StreamWriter(this._etagFile, true, Encoding.UTF8))
                        {
                            writer.WriteLine(id + "\t" + etag);
                            writer.Close();
                        }
                    }

                    //Cache under the Response URI as well if applicable
                    if (cacheTwice)
                    {
                        id = responseUri.GetEnhancedHashCode();
                        requireAdd = false;
                        if (this._etags.ContainsKey(id))
                        {
                            if (!this._etags[id].Equals(etag))
                            {
                                //If the ETag has changed remove it and then re-add it
                                this.RemoveETag(responseUri);
                                requireAdd = true;
                            }
                        }
                        else
                        {
                            requireAdd = true;
                        }

                        if (requireAdd)
                        {
                            using (StreamWriter writer = new StreamWriter(this._etagFile, true, Encoding.UTF8))
                            {
                                writer.WriteLine(id + "\t" + etag);
                                writer.Close();
                            }
                        }
                    }
                }

                //Then if we are caching Graphs return WriteThroughHandlers to do the caching for us
                if (this._canCacheGraphs)
                {
                    String graph = Path.Combine(this._graphDir, requestUri.GetSha256Hash());
                    handler = new WriteThroughHandler(this._formatterType, new StreamWriter(graph), true);

                    if (cacheTwice)
                    {
                        graph = Path.Combine(this._graphDir, responseUri.GetSha256Hash());
                        handler = new MultiHandler(new IRdfHandler[] { handler, new WriteThroughHandler(this._formatterType, new StreamWriter(graph), true) });
                    }
                }
            }
            catch (IOException)
            {
                //Ignore - if we get an IO Exception we failed to cache somehow
            }
            catch (RdfOutputException)
            {
                //Ignore - if we get an RDF Output Exception then we failed to cache
            }
            return handler;
        }
예제 #4
0
 protected override void StartRdfInternal()
 {
     _handler = new WriteThroughHandler(_formatterType, new StringWriter(_buffer));
     _handler.StartRdf();
 }
예제 #5
0
 private void LocalImportContinuation(MessageBoxResult dialogResult = MessageBoxResult.Yes)
 {
     try
     {
         if (dialogResult == MessageBoxResult.Yes)
         {
             var ext = MimeTypesHelper.GetTrueFileExtension(_importFileName);
             bool isGZipped = ext.EndsWith(MimeTypesHelper.DefaultGZipExtension);
             string lines;
             var fileTypeDefinition =
                 MimeTypesHelper.GetDefinitionsByFileExtension(ext).FirstOrDefault(d => d.CanParseRdf);
             var rdfReader = fileTypeDefinition == null ? null : fileTypeDefinition.GetRdfParser();
             if (rdfReader == null || rdfReader is NTriplesParser || rdfReader is NQuadsParser)
             {
                 // We can't determine the file type, or it is NQuads or NTriples
                 if (isGZipped)
                 {
                     using (var fileStream = new FileStream(_importFileName, FileMode.Open))
                     {
                         var gZipStream = new GZipStream(fileStream, CompressionMode.Decompress);
                         var reader = new StreamReader(gZipStream);
                         lines = reader.ReadToEnd();
                     }
                 }
                 else
                 {
                     lines = File.ReadAllText(_importFileName);
                 }
             }
             else
             {
                 using (var textWriter = new StringWriter())
                 {
                     try
                     {
                         var nQuadsFormatter = new NQuadsFormatter();
                         var writeThroughHandler = new WriteThroughHandler(nQuadsFormatter, textWriter);
                         rdfReader.Load(writeThroughHandler, _importFileName);
                         lines = textWriter.ToString();
                     }
                     catch (Exception ex)
                     {
                         Messenger.Default.Send(new ShowDialogMessage(
                                                    Strings.ParseErrorTitle,
                                                    String.Format(Strings.ParseErrorDescription, _importFileName,
                                                                  ex.Message),
                                                    MessageBoxImage.Error,
                                                    MessageBoxButton.OK),
                                                "MainWindow");
                         return;
                     }
                 }
             }
             var client = BrightstarService.GetClient(Store.Source.ConnectionString);
             _transactionJob = client.ExecuteTransaction(Store.Location, String.Empty, String.Empty, lines, waitForCompletion:false);
             _dispatcher.BeginInvoke(DispatcherPriority.SystemIdle,
                                     new TransactionViewModel.JobMonitorDelegate(CheckJobStatus));
         }
     }
     catch (OutOfMemoryException)
     {
         Messenger.Default.Send(new ShowDialogMessage(Strings.ParseErrorTitle,
                                                      Strings.ImportFileTooLarge,
                                                      MessageBoxImage.Error,
                                                      MessageBoxButton.OK), "MainWindow");
     }
 }
예제 #6
0
 protected override void StartRdfInternal()
 {
     this._handler = new WriteThroughHandler(this._formatterType, new StreamWriter(this._file, false, this._encoding));
     this._handler.StartRdf();
 }