コード例 #1
0
        public fclsSparqlGui()
        {
            InitializeComponent();
            Constants.WindowIcon = this.Icon;
            this._store          = new TripleStore();
            this._dataset        = new InMemoryQuadDataset(this._store);
            this._processor      = new LeviathanQueryProcessor(this._dataset);

            //Enable UTF-8 BOM setting if user set
            Options.UseBomForUtf8 = false;
            if (Properties.Settings.Default.UseUtf8Bom)
            {
                Options.UseBomForUtf8      = true;
                this.chkUseUtf8Bom.Checked = true;
            }

            String temp = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            String sep  = new String(new char[] { Path.DirectorySeparatorChar });

            if (!temp.EndsWith(sep))
            {
                temp += sep;
            }
            temp = Path.Combine(temp, @"dotNetRDF\");
            if (!System.IO.Directory.Exists(temp))
            {
                System.IO.Directory.CreateDirectory(temp);
            }
            temp = Path.Combine(temp, @"SparqlGUI\");
            if (!System.IO.Directory.Exists(temp))
            {
                System.IO.Directory.CreateDirectory(temp);
            }
            this._logfile = Path.Combine(temp, "SparqlGui-" + DateTime.Now.ToString("MMM-yyyy") + ".log");

            this.ofdBrowse.Filter = MimeTypesHelper.GetFilenameFilter(true, true, false, false, false, true);
            this.ofdQuery.Filter  = MimeTypesHelper.GetFilenameFilter(false, false, false, true, false, true);
        }
コード例 #2
0
        public string StoreCKeditorImageWithoutQuota(int tenant, string user, int mailboxId, string fileName, byte[] imageData, IDataStore storage)
        {
            try
            {
                if (imageData == null || imageData.Length == 0)
                {
                    throw new ArgumentNullException("imageData");
                }

                var ext = string.IsNullOrEmpty(fileName) ? ".jpg" : Path.GetExtension(fileName);

                if (string.IsNullOrEmpty(ext))
                {
                    ext = ".jpg";
                }

                var storeName = imageData.GetMd5();
                storeName = Path.ChangeExtension(storeName, ext);

                var contentDisposition = ContentDispositionUtil.GetHeaderValue(storeName);
                var contentType        = MimeTypesHelper.GetMimeqType(ext);

                var signatureImagePath = MailStoragePathCombiner.GerStoredSignatureImagePath(mailboxId, storeName);

                using (var reader = new MemoryStream(imageData))
                {
                    var uploadUrl = storage.UploadWithoutQuota(user, signatureImagePath, reader, contentType, contentDisposition);
                    return(MailStoragePathCombiner.GetStoredUrl(uploadUrl));
                }
            }
            catch (Exception e)
            {
                _log.Error("StoreCKeditorImageWithoutQuota(). filename: {0} Exception:\r\n{1}\r\n", fileName,
                           e.ToString());

                throw;
            }
        }
コード例 #3
0
        /// <summary>
        /// Gets the raw Cluster Graph for the Knowledge Base
        /// </summary>
        /// <param name="number">Number of Clusters</param>
        /// <param name="type">QName of a Type to Cluster around</param>
        /// <returns></returns>
        public IGraph ClusterRaw(int number, String type)
        {
            if (number < 2)
            {
                throw new RdfReasoningException("Pellet Server requires the number of Clusters to be at least 2");
            }

            String requestUri = this._clusterUri + number + "/" + type;

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);

            request.Method = this.Endpoint.HttpMethods.First();
            request.Accept = MimeTypesHelper.CustomHttpAcceptHeader(this.MimeTypes.Where(t => !t.Equals("text/json")), MimeTypesHelper.SupportedRdfMimeTypes);

            Tools.HttpDebugRequest(request);

            try
            {
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    Tools.HttpDebugResponse(response);
                    IRdfReader parser = MimeTypesHelper.GetParser(response.ContentType);
                    Graph      g      = new Graph();
                    parser.Load(g, new StreamReader(response.GetResponseStream()));

                    response.Close();
                    return(g);
                }
            }
            catch (WebException webEx)
            {
                if (webEx.Response != null)
                {
                    Tools.HttpDebugResponse((HttpWebResponse)webEx.Response);
                }
                throw new RdfReasoningException("A HTTP error occurred while communicating with the Pellet Server", webEx);
            }
        }
コード例 #4
0
        /// <summary>
        /// Lists the Graphs from the Repository
        /// </summary>
        /// <param name="callback">Callback</param>
        /// <param name="state">State to pass to the callback</param>
        /// <returns></returns>
        public override void ListGraphs(AsyncStorageCallback callback, Object state)
        {
            try
            {
                //Use the /contexts method to get the Graph URIs
                //HACK: Have to use SPARQL JSON as currently Dydra's SPARQL XML Results are malformed
                HttpWebRequest request = this.CreateRequest("/contexts", MimeTypesHelper.CustomHttpAcceptHeader(MimeTypesHelper.SparqlResultsJson), "GET", new Dictionary <string, string>());
                request.BeginGetResponse(r =>
                {
                    try
                    {
                        HttpWebResponse response    = (HttpWebResponse)request.EndGetResponse(r);
                        ISparqlResultsReader parser = MimeTypesHelper.GetSparqlParser(response.ContentType);
                        ListUrisHandler handler     = new ListUrisHandler("contextID");
                        parser.Load(handler, new StreamReader(response.GetResponseStream()));
                        response.Close();

                        callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.ListGraphs, handler.Uris), state);
                    }
                    catch (WebException webEx)
                    {
                        callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.ListGraphs, StorageHelper.HandleHttpError(webEx, "list Graphs asynchronously from")), state);
                    }
                    catch (Exception ex)
                    {
                        callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.ListGraphs, StorageHelper.HandleError(ex, "list Graphs asynchronously from")), state);
                    }
                }, state);
            }
            catch (WebException webEx)
            {
                callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.ListGraphs, StorageHelper.HandleHttpError(webEx, "list Graphs asynchronously from")), state);
            }
            catch (Exception ex)
            {
                callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.ListGraphs, StorageHelper.HandleError(ex, "list Graphs asynchronously from")), state);
            }
        }
コード例 #5
0
        /// <summary>
        /// Gets the Graph which comprises the class hierarchy and individuals of those classes
        /// </summary>
        /// <param name="callback">Callback to invoke when the operation completes</param>
        /// <param name="state">State to pass to the callback</param>
        public void Realize(GraphCallback callback, Object state)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this.Endpoint.Uri);

            request.Method = this.Endpoint.HttpMethods.First();
            request.Accept = MimeTypesHelper.CustomHttpAcceptHeader(this.MimeTypes, MimeTypesHelper.SupportedRdfMimeTypes);

            Tools.HttpDebugRequest(request);

            request.BeginGetResponse(result =>
            {
                using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result))
                {
                    Tools.HttpDebugResponse(response);
                    IRdfReader parser = MimeTypesHelper.GetParser(response.ContentType);
                    Graph g           = new Graph();
                    parser.Load(g, new StreamReader(response.GetResponseStream()));

                    response.Close();
                    callback(g, state);
                }
            }, null);
        }
コード例 #6
0
        public void ParsingRdfXmlNamespaceAttributes()
        {
            if (!TestConfigManager.GetSettingAsBoolean(TestConfigManager.UseRemoteParsing))
            {
                throw new SkipTestException("Test Config marks Remote Parsing as unavailable, test cannot be run");
            }

            Graph          g       = new Graph();
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://dbpedia.org/resource/Southampton");

            request.Method = "GET";
            request.Accept = MimeTypesHelper.HttpAcceptHeader;

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            IRdfReader      parser   = MimeTypesHelper.GetParser(response.ContentType);

            parser.Load(g, new StreamReader(response.GetResponseStream()));

            foreach (Triple t in g.Triples)
            {
                Console.WriteLine(t.ToString());
            }
        }
コード例 #7
0
        /// <summary>
        /// Extracts an RDF Dataset which details the Constraints violated (if any) and whether Constraints are satisified
        /// </summary>
        /// <param name="callback">Callback to invoke when the operation completes</param>
        /// <param name="state">State to pass to the callback</param>
        public void Validate(TripleStoreCallback callback, Object state)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this.Endpoint.Uri);

            request.Method = this.Endpoint.HttpMethods.First();
            request.Accept = MimeTypesHelper.CustomHttpAcceptHeader(this.MimeTypes, MimeTypesHelper.SupportedRdfDatasetMimeTypes);

            Tools.HttpDebugRequest(request);

            request.BeginGetResponse(result =>
            {
                using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result))
                {
                    Tools.HttpDebugResponse(response);
                    IStoreReader parser = MimeTypesHelper.GetStoreParser(response.ContentType);
                    TripleStore store   = new TripleStore();
                    parser.Load(store, new StreamReader(response.GetResponseStream()));

                    response.Close();
                    callback(store, state);
                }
            }, null);
        }
コード例 #8
0
        /// <summary>
        /// Determines whether the Knowledge Base is consistent
        /// </summary>
        /// <param name="callback">Callback to invoke when the operation completes</param>
        /// <param name="state">State to be passed to the callback</param>
        public void IsConsistent(PelletConsistencyCallback callback, Object state)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this.Endpoint.Uri);

            request.Method = this.Endpoint.HttpMethods.First();
            request.Accept = MimeTypesHelper.HttpSparqlAcceptHeader;

            Tools.HttpDebugRequest(request);

            request.BeginGetResponse(result =>
            {
                using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result))
                {
                    Tools.HttpDebugResponse(response);
                    ISparqlResultsReader parser = MimeTypesHelper.GetSparqlParser(response.ContentType);
                    SparqlResultSet results     = new SparqlResultSet();
                    parser.Load(results, new StreamReader(response.GetResponseStream()));

                    //Expect a boolean result set
                    callback(results.Result, state);
                }
            }, null);
        }
コード例 #9
0
        /// <summary>
        /// Helper method for doing async load operations, callers just need to provide an appropriately prepared HTTP request
        /// </summary>
        /// <param name="request">HTTP Request</param>
        /// <param name="handler">Handler to load with</param>
        /// <param name="callback">Callback</param>
        /// <param name="state">State to pass to the callback</param>
        protected internal void LoadGraphAsync(HttpWebRequest request, IRdfHandler handler, AsyncStorageCallback callback, Object state)
        {
            Tools.HttpDebugRequest(request);
            request.BeginGetResponse(r =>
            {
                try
                {
                    HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(r);
                    Tools.HttpDebugResponse(response);
                    //Parse the retrieved RDF
                    IRdfReader parser = MimeTypesHelper.GetParser(response.ContentType);
                    parser.Load(handler, new StreamReader(response.GetResponseStream()));

                    //If we get here then it was OK
                    response.Close();

                    callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.LoadWithHandler, handler), state);
                }
                catch (WebException webEx)
                {
                    if (webEx.Response != null)
                    {
                        Tools.HttpDebugResponse((HttpWebResponse)webEx.Response);
                        if (((HttpWebResponse)webEx.Response).StatusCode == HttpStatusCode.NotFound)
                        {
                            callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.LoadWithHandler, handler), state);
                            return;
                        }
                    }
                    callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.LoadWithHandler, handler, new RdfStorageException("A HTTP Error occurred while trying to load a Graph from the Store", webEx)), state);
                }
                catch (Exception ex)
                {
                    callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.LoadWithHandler, handler, StorageHelper.HandleError(ex, "loading a Graph asynchronously from")), state);
                }
            }, state);
        }
コード例 #10
0
        /// <summary>
        /// Gets the raw Cluster Graph for the Knowledge Base
        /// </summary>
        /// <param name="number">Number of Clusters</param>
        /// <returns></returns>
        public IGraph ClusterRaw(int number)
        {
            if (number < 2)
            {
                throw new RdfReasoningException("Pellet Server requires the number of Clusters to be at least 2");
            }

            String requestUri = this._clusterUri + number + "/";

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);

            request.Method = this.Endpoint.HttpMethods.First();
            request.Accept = MimeTypesHelper.CustomHttpAcceptHeader(this.MimeTypes.Where(type => !type.Equals("text/json")), MimeTypesHelper.SupportedRdfMimeTypes);

#if DEBUG
            if (Options.HttpDebugging)
            {
                Tools.HttpDebugRequest(request);
            }
#endif

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
#if DEBUG
                if (Options.HttpDebugging)
                {
                    Tools.HttpDebugResponse(response);
                }
#endif
                IRdfReader parser = MimeTypesHelper.GetParser(response.ContentType);
                Graph      g      = new Graph();
                parser.Load(g, new StreamReader(response.GetResponseStream()));

                response.Close();
                return(g);
            }
        }
コード例 #11
0
        /// <summary>
        /// Sends the given Graph to the Client via the HTTP Response.
        /// </summary>
        /// <param name="context">HTTP Context.</param>
        /// <param name="g">Graph to send.</param>
        protected void SendResultsToClient(IHttpContext context, IGraph g)
        {
            IRdfWriter writer;
            String     ctype;

            // Look up the MIME Type Definition - if none use GetWriter instead
            MimeTypeDefinition definition = MimeTypesHelper.GetDefinitions(context.GetAcceptTypes()).FirstOrDefault(d => d.CanWriteRdf);

            if (definition != null)
            {
                writer = definition.GetRdfWriter();
                ctype  = definition.CanonicalMimeType;
            }
            else
            {
                writer = MimeTypesHelper.GetWriter(context.GetAcceptTypes(), out ctype);
            }

            // Set up the Writer
            if (writer is ICompressingWriter)
            {
                ((ICompressingWriter)writer).CompressionLevel = Options.DefaultCompressionLevel;
            }

            // Send Content to Client
            context.Response.ContentType = ctype;
            if (definition != null)
            {
                context.Response.ContentEncoding = definition.Encoding;
                writer.Save(g, new StreamWriter(context.Response.OutputStream, definition.Encoding));
            }
            else
            {
                writer.Save(g, new StreamWriter(context.Response.OutputStream));
            }
        }
コード例 #12
0
        public async Task <GetObjectAttachmentQueryResult> Ask(GetObjectAttachmentQueryArgs args)
        {
            var attachment = await _repository.AsQueryable <MapObject>()
                             .WithId(args.ObjectId)
                             .OfMap(args.MapId, args.ActionExecutorId)
                             .SelectMany(mapObject => mapObject.FileReferencesBindings)
                             .Select(binding => binding.FileReference)
                             .WithId(args.AttachmentId)
                             .AsNoTracking()
                             .FirstOrDefaultAsync();

            var result = new GetObjectAttachmentQueryResult
            {
                Id              = attachment.Id,
                AccessUrl       = _fileStorageClient.GenerateGetPreSignedUrl(attachment.ObjectKey),
                MimeType        = attachment.MimeType,
                PreviewTemplate = MimeTypesHelper.IsPreviewSupporting(attachment.MimeType)
                    ? _endpointsBuilder.BuildImagePreviewUrl(attachment.ObjectKey)
                    : null,
                SourceFileName = attachment.SourceFileName
            };

            return(result);
        }
コード例 #13
0
ファイル: Tools.cs プロジェクト: PHParis/im_prototype
        public static async Task <SparqlResultSet> SelectQuery(this string endpointUri, string sparqlQuery)
        {
            WriteLine($"Querying : {endpointUri} whith \"{sparqlQuery}\"");
            SparqlResultSet results = new SparqlResultSet();

            using (var client = new System.Net.Http.HttpClient())
            {
                try
                {
                    client.Timeout = new TimeSpan(23, 23, 59, 59, 59);
                    var httpQuery = Tools.UrlEncode(sparqlQuery);
                    var response  = await client.GetAsync(endpointUri + "?query=" + httpQuery);

                    response.EnsureSuccessStatusCode();
                    ISparqlResultsHandler handler = new VDS.RDF.Parsing.Handlers.ResultSetHandler(results);

                    String ctype = response.Content.Headers.ContentType.ToString();

                    if (ctype.Contains(";"))
                    {
                        ctype = ctype.Substring(0, ctype.IndexOf(";"));
                    }

                    ISparqlResultsReader resultsParser = MimeTypesHelper.GetSparqlParser(ctype);
                    resultsParser.Load(handler, new System.IO.StreamReader(response.Content.ReadAsStreamAsync().Result));
                }
                catch (System.Net.Http.HttpRequestException e)
                {
                    Console.WriteLine("\nException Caught !");
                    WriteLine($"endpoint : {endpointUri}");
                    WriteLine($"sparqlQuery : {sparqlQuery}");
                    Console.WriteLine("Message :{0} ", e.Message);
                }
                return(results);
            }
        }
コード例 #14
0
        /// <summary>
        /// Makes a SPARQL Query against the underlying Store using whatever reasoning mode is currently in-use processing the results using an appropriate handler from those provided
        /// </summary>
        /// <param name="rdfHandler">RDF Handler</param>
        /// <param name="resultsHandler">Results Handler</param>
        /// <param name="sparqlQuery">SPARQL Query</param>
        /// <returns></returns>
        public void Query(IRdfHandler rdfHandler, ISparqlResultsHandler resultsHandler, String sparqlQuery)
        {
            try
            {
                HttpWebRequest request;

                String tID = (this._activeTrans == null) ? String.Empty : "/" + this._activeTrans;

                //String accept = MimeTypesHelper.HttpRdfOrSparqlAcceptHeader;
                String accept = MimeTypesHelper.CustomHttpAcceptHeader(MimeTypesHelper.SparqlResultsXml.Concat(MimeTypesHelper.Definitions.Where(d => d.CanParseRdf).SelectMany(d => d.MimeTypes)));

                //Create the Request
                Dictionary <String, String> queryParams = new Dictionary <string, string>();
                if (sparqlQuery.Length < 2048)
                {
                    queryParams.Add("query", sparqlQuery);

                    request = this.CreateRequest(this._kb + tID + "/query", accept, "GET", queryParams);
                }
                else
                {
                    request = this.CreateRequest(this._kb + tID + "/query", accept, "POST", queryParams);

                    //Build the Post Data and add to the Request Body
                    request.ContentType = MimeTypesHelper.WWWFormURLEncoded;
                    StringBuilder postData = new StringBuilder();
                    postData.Append("query=");
                    postData.Append(Uri.EscapeDataString(sparqlQuery));
                    StreamWriter writer = new StreamWriter(request.GetRequestStream());
                    writer.Write(postData);
                    writer.Close();
                }

#if DEBUG
                if (Options.HttpDebugging)
                {
                    Tools.HttpDebugRequest(request);
                }
#endif

                //Get the Response and process based on the Content Type
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
#if DEBUG
                    if (Options.HttpDebugging)
                    {
                        Tools.HttpDebugResponse(response);
                    }
#endif
                    StreamReader data  = new StreamReader(response.GetResponseStream());
                    String       ctype = response.ContentType;
                    try
                    {
                        //Is the Content Type referring to a Sparql Result Set format?
                        ISparqlResultsReader resreader = MimeTypesHelper.GetSparqlParser(ctype, Regex.IsMatch(sparqlQuery, "ASK", RegexOptions.IgnoreCase));
                        resreader.Load(resultsHandler, data);
                        response.Close();
                    }
                    catch (RdfParserSelectionException)
                    {
                        //If we get a Parser Selection exception then the Content Type isn't valid for a Sparql Result Set

                        //Is the Content Type referring to a RDF format?
                        IRdfReader rdfreader = MimeTypesHelper.GetParser(ctype);
                        rdfreader.Load(rdfHandler, data);
                        response.Close();
                    }
                }
            }
            catch (WebException webEx)
            {
                if (webEx.Response != null)
                {
#if DEBUG
                    if (Options.HttpDebugging)
                    {
                        Tools.HttpDebugResponse((HttpWebResponse)webEx.Response);
                    }
#endif
                    if (webEx.Response.ContentLength > 0)
                    {
                        try
                        {
                            String responseText = new StreamReader(webEx.Response.GetResponseStream()).ReadToEnd();
                            throw new RdfQueryException("A HTTP error occured while querying the Store.  Store returned the following error message: " + responseText, webEx);
                        }
                        catch
                        {
                            throw new RdfQueryException("A HTTP error occurred while querying the Store", webEx);
                        }
                    }
                    else
                    {
                        throw new RdfQueryException("A HTTP error occurred while querying the Store", webEx);
                    }
                }
                else
                {
                    throw new RdfQueryException("A HTTP error occurred while querying the Store", webEx);
                }
            }
        }
コード例 #15
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);

                    String graphUri = !String.IsNullOrWhiteSpace(ImportGraphName)
                        ? ImportGraphName
                        : Constants.DefaultGraphUri;

                    _transactionJob = client.ExecuteTransaction(Store.Location, String.Empty, String.Empty, lines, waitForCompletion: false, defaultGraphUri: graphUri);
                    _dispatcher.BeginInvoke(DispatcherPriority.SystemIdle,
                                            new TransactionViewModel.JobMonitorDelegate(CheckJobStatus));
                }
            }
            catch (OutOfMemoryException)
            {
                Messenger.Default.Send(new ShowDialogMessage(Strings.ParseErrorTitle,
                                                             Strings.ImportFileTooLarge,
                                                             MessageBoxImage.Error,
                                                             MessageBoxButton.OK), "MainWindow");
            }
        }
コード例 #16
0
        /// <summary>
        /// Handles the start of requests by doing conneg wherever applicable
        /// </summary>
        /// <param name="sender">Sender of the Event</param>
        /// <param name="e">Event Arguments</param>
        void context_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication app = sender as HttpApplication;

            if (app == null)
            {
                return;
            }
            HttpContext context = app.Context;

            if (context == null)
            {
                return;
            }

            if (context.Request.Url.AbsolutePath.Contains("."))
            {
                String actualPath = context.Request.MapPath(context.Request.Path);
                if (!File.Exists(actualPath))
                {
                    // Get the File Extension and see if it is for an RDF format
                    String ext = context.Request.Url.AbsolutePath.Substring(context.Request.Url.AbsolutePath.LastIndexOf("."));
                    switch (ext)
                    {
                    case ".aspx":
                    case ".asmx":
                    case ".ashx":
                    case ".axd":
                        // The above file extensions are special to ASP.Net and so may not actually exist as files
                        // so we need to ignore them for the purposes of negotiating by file extension
                        return;
                    }

                    try
                    {
                        List <MimeTypeDefinition> defs = MimeTypesHelper.GetDefinitionsByFileExtension(ext).ToList();
                        if (defs.Count == 0)
                        {
                            return;
                        }

                        context.Request.Headers["Accept"] = String.Join(",", defs.Select(d => d.CanonicalMimeType).ToArray());
                        String filePath = Path.GetFileNameWithoutExtension(actualPath);
                        if (filePath == null || filePath.Equals(String.Empty))
                        {
                            if (context.Request.Url.AbsolutePath.EndsWith(ext))
                            {
                                filePath = context.Request.Url.AbsolutePath.Substring(0, context.Request.Url.AbsolutePath.Length - ext.Length);
                            }
                        }
                        String query = context.Request.Url.Query;
                        if (query.StartsWith("?"))
                        {
                            query = query.Substring(1);
                        }
                        context.RewritePath(filePath, String.Empty, query, true);
                    }
                    catch (RdfParserSelectionException)
                    {
                        // If we get a RdfParserSelectionException we shouldn't do anything, this fixes bug CORE-94
                    }
                }
            }
        }
コード例 #17
0
        /// <summary>
        /// Attempts to load a RDF Graph from the given URI using a RDF Handler
        /// </summary>
        /// <param name="handler">RDF Handler to use</param>
        /// <param name="u">URI to attempt to get RDF from</param>
        /// <param name="parser">Parser to use</param>
        /// <remarks>
        /// <para>
        /// Uses the supplied parser to attempt parsing regardless of the actual Content Type returned
        /// </para>
        /// <para>
        /// In the event that the URI is a File URI the <see cref="FileLoader">FileLoader</see> will be used instead
        /// </para>
        /// <para>
        /// If the URI is a Data URI then the <see cref="DataUriLoader">DataUriLoader</see> will be used instead.
        /// </para>
        /// </remarks>
        public static void Load(IRdfHandler handler, Uri u, IRdfReader parser)
        {
            if (handler == null)
            {
                throw new RdfParseException("Cannot read RDF using a null RDF Handler");
            }
            if (u == null)
            {
                throw new RdfParseException("Cannot load RDF from a null URI");
            }
            try
            {
#if SILVERLIGHT
                if (u.IsFile())
#else
                if (u.IsFile)
#endif
                {
                    //Invoke FileLoader instead
                    RaiseWarning("This is a file: URI so invoking the FileLoader instead");
                    if (Path.DirectorySeparatorChar == '/')
                    {
                        FileLoader.Load(handler, u.ToString().Substring(7), parser);
                    }
                    else
                    {
                        FileLoader.Load(handler, u.ToString().Substring(8), parser);
                    }
                    return;
                }
                if (u.Scheme.Equals("data"))
                {
                    //Invoke DataUriLoader instead
                    RaiseWarning("This is a data: URI so invoking the DataUriLoader instead");
                    DataUriLoader.Load(handler, u);
                    return;
                }

                //Sanitise the URI to remove any Fragment ID
                u = Tools.StripUriFragment(u);

#if !NO_URICACHE
                //Use Cache if possible
                String etag  = String.Empty;
                String local = null;
                if (Options.UriLoaderCaching)
                {
                    if (_cache.HasETag(u))
                    {
                        //Get the ETag and then we'll include an If-None-Match header in our request
                        etag = _cache.GetETag(u);
                    }
                    else if (_cache.HasLocalCopy(u, true))
                    {
                        //Just try loading from the local copy
                        local = _cache.GetLocalCopy(u);
                        if (local != null)
                        {
                            try
                            {
                                FileLoader.Load(handler, local, new TurtleParser());
                            }
                            catch
                            {
                                //If we get an Exception we failed to access the file successfully
                                _cache.RemoveETag(u);
                                _cache.RemoveLocalCopy(u);
                                UriLoader.Load(handler, u, parser);
                            }
                            return;
                        }
                    }
                }
#endif

                //Set-up the Request
                HttpWebRequest httpRequest;
                httpRequest = (HttpWebRequest)WebRequest.Create(u);

                //Want to ask for RDF formats
                if (parser != null)
                {
                    //If a non-null parser set up a HTTP Header that is just for the given parser
                    httpRequest.Accept = MimeTypesHelper.CustomHttpAcceptHeader(parser);
                }
                else
                {
                    httpRequest.Accept = MimeTypesHelper.HttpAcceptHeader;
                }

#if !NO_URICACHE
                if (Options.UriLoaderCaching)
                {
                    if (!etag.Equals(String.Empty))
                    {
                        httpRequest.Headers.Add(HttpRequestHeader.IfNoneMatch, etag);
                    }
                }
#endif

                //Use HTTP GET
                httpRequest.Method = "GET";
#if !SILVERLIGHT
                httpRequest.Timeout = Options.UriLoaderTimeout;
#endif
                if (_userAgent != null && !_userAgent.Equals(String.Empty))
                {
                    httpRequest.UserAgent = _userAgent;
                }
#if DEBUG
                //HTTP Debugging
                if (Options.HttpDebugging)
                {
                    Tools.HttpDebugRequest(httpRequest);
                }
#endif

                using (HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse())
                {
#if DEBUG
                    //HTTP Debugging
                    if (Options.HttpDebugging)
                    {
                        Tools.HttpDebugResponse(httpResponse);
                    }
#endif

#if !NO_URICACHE
                    if (Options.UriLoaderCaching)
                    {
                        //Are we using ETag based caching?
                        if (!etag.Equals(String.Empty))
                        {
                            //Did we get a Not-Modified response?
                            if (httpResponse.StatusCode == HttpStatusCode.NotModified)
                            {
                                //If so then we need to load the Local Copy assuming it exists?
                                if (_cache.HasLocalCopy(u, false))
                                {
                                    local = _cache.GetLocalCopy(u);
                                    try
                                    {
                                        FileLoader.Load(handler, local, new TurtleParser());
                                    }
                                    catch
                                    {
                                        //If we get an Exception we failed to access the file successfully
                                        _cache.RemoveETag(u);
                                        _cache.RemoveLocalCopy(u);
                                        UriLoader.Load(handler, u, parser);
                                    }
                                    return;
                                }
                                else
                                {
                                    //If the local copy didn't exist then we need to redo the response without
                                    //the ETag as we've lost the cached copy somehow
                                    _cache.RemoveETag(u);
                                    UriLoader.Load(handler, u, parser);
                                    return;
                                }
                            }
                            //If we didn't get a Not-Modified response then we'll continue and parse the new response
                        }
                    }
#endif

                    //Get a Parser and Load the RDF
                    if (parser == null)
                    {
                        //Only need to auto-detect the parser if a specific one wasn't specified
                        parser = MimeTypesHelper.GetParser(httpResponse.ContentType);
                    }
                    parser.Warning += RaiseWarning;
#if !NO_URICACHE
                    //To do caching we ask the cache to give us a handler and then we tie it to
                    IRdfHandler cacheHandler = _cache.ToCache(u, Tools.StripUriFragment(httpResponse.ResponseUri), httpResponse.Headers["ETag"]);
                    if (cacheHandler != null)
                    {
                        //Note: We can ONLY use caching when we know that the Handler will accept all the data returned
                        //i.e. if the Handler may trim the data in some way then we shouldn't cache the data returned
                        if (handler.AcceptsAll)
                        {
                            handler = new MultiHandler(new IRdfHandler[] { handler, cacheHandler });
                        }
                        else
                        {
                            cacheHandler = null;
                        }
                    }
                    try
                    {
#endif
                    parser.Load(handler, new StreamReader(httpResponse.GetResponseStream()));

#if !NO_URICACHE
                }
                catch
                {
                    //If we were trying to cache the response and something went wrong discard the cached copy
                    _cache.RemoveETag(u);
                    _cache.RemoveETag(Tools.StripUriFragment(httpResponse.ResponseUri));
                    _cache.RemoveLocalCopy(u);
                    _cache.RemoveLocalCopy(Tools.StripUriFragment(httpResponse.ResponseUri));
                }
#endif
                }
            }
            catch (UriFormatException uriEx)
            {
                //Uri Format Invalid
                throw new RdfParseException("Unable to load from the given URI '" + u.ToString() + "' since it's format was invalid", uriEx);
            }
            catch (WebException webEx)
            {
#if DEBUG
                if (webEx.Response != null && Options.HttpDebugging)
                {
                    Tools.HttpDebugResponse((HttpWebResponse)webEx.Response);
                }
#endif

#if !NO_URICACHE
                if (webEx.Response != null)
                {
                    if (((HttpWebResponse)webEx.Response).StatusCode == HttpStatusCode.NotModified)
                    {
                        //If so then we need to load the Local Copy assuming it exists?
                        if (_cache.HasLocalCopy(u, false))
                        {
                            String local = _cache.GetLocalCopy(u);
                            try
                            {
                                FileLoader.Load(handler, local, new TurtleParser());
                            }
                            catch
                            {
                                //If we get an Exception we failed to access the file successfully
                                _cache.RemoveETag(u);
                                _cache.RemoveLocalCopy(u);
                                UriLoader.Load(handler, u, parser);
                            }
                            return;
                        }
                        else
                        {
                            //If the local copy didn't exist then we need to redo the response without
                            //the ETag as we've lost the cached copy somehow
                            _cache.RemoveETag(u);
                            UriLoader.Load(handler, u, parser);
                            return;
                        }
                    }
                }
#endif

                //Some sort of HTTP Error occurred
                throw new WebException("A HTTP Error occurred resolving the URI '" + u.ToString() + "'", webEx);
            }
        }
コード例 #18
0
        public void RunConvert(String[] args)
        {
            //Set the Options
            if (!this.SetOptions(args))
            {
                //If SetOptions returns false then some options were invalid and errors have been output to the error stream
                return;
            }

            //First grab the MIME Type Definitions for the conversion
            List <MimeTypeDefinition> defs = MimeTypesHelper.GetDefinitions(this._outFormats).ToList();

            //Process each input to determine the Conversion Handler to use
            foreach (IConversionInput input in this._inputs)
            {
                String outFile;
                String ext = this._outExt;

                //First determine the writer we'll use
                MimeTypeDefinition graphDef = defs.FirstOrDefault(d => d.CanWriteRdf);
                if (graphDef != null)
                {
                    //Then generate the output filename
                    if (ext.Equals(String.Empty))
                    {
                        ext = "." + graphDef.CanonicalFileExtension;
                    }
                    if (this._inputs.Count == 1 && !this._outputFilename.Equals(String.Empty))
                    {
                        outFile = this._outputFilename;
                    }
                    else
                    {
                        outFile = input.GetFilename(this._outputFilename, ext);
                    }

                    //Check it doesn't already exist or overwrite is enabled
                    if (File.Exists(outFile) && !this._overwrite)
                    {
                        Console.Error.WriteLine("rdfConvert: Warning: Skipping Conversion of Input " + input.ToString() + " as this would generate the Output File '" + outFile + "' which already exists and the -overwrite option was not specified");
                        continue;
                    }

                    //Get the Writer and apply Conversion Options
                    IRdfWriter writer = graphDef.GetRdfWriter();
                    foreach (IConversionOption option in this._options)
                    {
                        option.Apply(writer);
                    }

                    //If -best always use SaveOnCompletionHandler
                    if (this._best)
                    {
                        if (this._verbose)
                        {
                            Console.WriteLine("rdfConvert: Using Best Quality data conversion subject to user specified compression options");
                        }
                        input.ConversionHandler = new SaveOnCompletionHandler(writer, new StreamWriter(outFile, false, graphDef.Encoding));
                    }
                    else
                    {
                        //Use the fast WriteThroughHandler where possible
                        if (writer is IFormatterBasedWriter)
                        {
                            if (this._verbose)
                            {
                                Console.WriteLine("rdfConvert: Using Streaming Conversion with formatter " + ((IFormatterBasedWriter)writer).TripleFormatterType.Name);
                            }
                            input.ConversionHandler = new WriteToFileHandler(outFile, graphDef.Encoding, ((IFormatterBasedWriter)writer).TripleFormatterType);
                        }
                        else
                        {
                            //Can't use it in this case
                            if (this._verbose)
                            {
                                Console.WriteLine("rdfConvert: Warning: Target Format not suitable for streaming conversion, input data will be loaded into memory prior to conversion");
                            }
                            input.ConversionHandler = new SaveOnCompletionHandler(writer, new StreamWriter(outFile, false, graphDef.Encoding));
                        }
                    }
                }
                else
                {
                    MimeTypeDefinition storeDef = defs.FirstOrDefault(d => d.CanWriteRdfDatasets);
                    if (storeDef != null)
                    {
                        //Then generate the output filename
                        if (ext.Equals(String.Empty))
                        {
                            ext = "." + storeDef.CanonicalFileExtension;
                        }
                        outFile = input.GetFilename(this._outputFilename, ext);

                        //Get the Writer and apply conversion options
                        IStoreWriter writer = storeDef.GetRdfDatasetWriter();
                        foreach (IConversionOption option in this._options)
                        {
                            option.Apply(writer);
                        }

                        //If -best always use SaveOnCompletionHandler
                        if (this._best)
                        {
                            if (this._verbose)
                            {
                                Console.WriteLine("rdfConvert: Using Best Quality  data conversion subject to user specified compression options");
                            }
                            input.ConversionHandler = new SaveStoreOnCompletionHandler(writer, new StreamWriter(outFile, false, storeDef.Encoding));
                        }
                        else
                        {
                            //Use the fast WriteThroughHandler where possible
                            if (writer is IFormatterBasedWriter)
                            {
                                if (this._verbose)
                                {
                                    Console.WriteLine("rdfConvert: Using Streaming Conversion with formatter " + ((IFormatterBasedWriter)writer).TripleFormatterType.Name);
                                }
                                input.ConversionHandler = new WriteToFileHandler(outFile, graphDef.Encoding, ((IFormatterBasedWriter)writer).TripleFormatterType);
                            }
                            else
                            {
                                if (this._verbose)
                                {
                                    Console.WriteLine("rdfConvert: Warning: Target Format not suitable for streaming conversion, input data will be loaded into memory prior to conversion");
                                }
                                input.ConversionHandler = new SaveStoreOnCompletionHandler(writer, new StreamWriter(outFile, false, storeDef.Encoding));
                            }
                        }
                    }
                    else
                    {
                        Console.Error.WriteLine("rdfConvert: Warning: Skipping Conversion of Input " + input.ToString() + " as unable to determine how to convert it");
                        continue;
                    }
                }

                //Then do the Conversion
                Console.WriteLine("rdfConvert: Converting Input " + input.ToString() + " to '" + outFile + "'...");
                try
                {
                    if (this._verbose)
                    {
                        input.ConversionHandler = new ConversionProgressHandler(input.ConversionHandler);
                        Console.WriteLine("rdfConvert: Debug: Conversion Handler is " + input.ConversionHandler.GetType().FullName);
                    }
                    input.Convert();
                    Console.WriteLine("rdfConvert: Converted Input " + input.ToString() + " to '" + outFile + "' OK");
                }
                catch (RdfParseException parseEx)
                {
                    Console.Error.WriteLine("rdfConvert: Error: Error Converting Input " + input.ToString() + " due to a RDF Parse Exception");
                    Console.Error.WriteLine(parseEx.Message);
                    if (this._debug)
                    {
                        this.DebugErrors(parseEx);
                    }
                }
                catch (RdfException rdfEx)
                {
                    Console.Error.WriteLine("rdfConvert: Error: Error Converting Input " + input.ToString() + " due to a RDF Exception");
                    Console.Error.WriteLine(rdfEx.Message);
                    if (this._debug)
                    {
                        this.DebugErrors(rdfEx);
                    }
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine("rdfConvert: Error: Error Converting Input " + input.ToString() + " due to a Unexpected Exception");
                    Console.Error.WriteLine(ex.Message);
                    if (this._debug)
                    {
                        this.DebugErrors(ex);
                    }
                }
            }
        }
コード例 #19
0
        public void ParsingGZipResultsByFilenameAuto2()
        {
            foreach (String filename in this._autoResultsTestFiles)
            {
                SparqlResultSet results = new SparqlResultSet();

                ISparqlResultsReader reader = MimeTypesHelper.GetSparqlParserByFileExtension(MimeTypesHelper.GetTrueFileExtension(filename));
                reader.Load(results, filename);

                Assert.IsTrue(this._results.Equals(results), "Result Sets for file " + filename + " were not equal");
            }
        }
コード例 #20
0
        /// <summary>
        /// Requests that the document auto-detect its syntax
        /// </summary>
        public void AutoDetectSyntax()
        {
            if (this._filename != null && !this._filename.Equals(String.Empty))
            {
                try
                {
                    //Try filename based syntax detection
                    MimeTypeDefinition def = MimeTypesHelper.GetDefinitionsByFileExtension(MimeTypesHelper.GetTrueFileExtension(this._filename)).FirstOrDefault();
                    if (def != null)
                    {
                        this.Syntax = def.SyntaxName.GetSyntaxName();
                        return;
                    }
                }
                catch (RdfParserSelectionException)
                {
                    //Ignore and use string based detection instead
                }
            }

            //Otherwise try and use string based detection
            //First take a guess at it being a SPARQL Results format
            String text = this.Text;

            try
            {
                ISparqlResultsReader resultsReader = StringParser.GetResultSetParser(text);
                this.Syntax = resultsReader.GetSyntaxName();
            }
            catch (RdfParserSelectionException)
            {
                //Then see whether it may be a SPARQL query
                if (text.Contains("SELECT") || text.Contains("CONSTRUCT") || text.Contains("DESCRIBE") || text.Contains("ASK"))
                {
                    //Likely a SPARQL Query
                    this.Syntax = "SparqlQuery11";
                }
                else
                {
                    //Then take a guess at it being a RDF format
                    try
                    {
                        IRdfReader rdfReader = StringParser.GetParser(text);
                        this.Syntax = rdfReader.GetSyntaxName();
                    }
                    catch (RdfParserSelectionException)
                    {
                        //Finally take a guess at it being a RDF Dataset format
                        IStoreReader datasetReader = StringParser.GetDatasetParser(text);
                        this.Syntax = datasetReader.GetSyntaxName();
                    }
                }
            }
        }
コード例 #21
0
ファイル: ExportTask.cs プロジェクト: mungojam/dotnetrdf-fork
        /// <summary>
        /// Runs the task
        /// </summary>
        /// <returns></returns>
        protected override TaskResult RunTaskInternal()
        {
            MimeTypeDefinition def = MimeTypesHelper.GetDefinitionsByFileExtension(MimeTypesHelper.GetTrueFileExtension(this._file)).FirstOrDefault(d => d.CanWriteRdfDatasets);

            if (def == null)
            {
                throw new RdfOutputException("Cannot Export the Store to the selected File since dotNetRDF was unable to select a writer to use based on the File Extension");
            }

            IStoreWriter writer = def.GetRdfDatasetWriter();

            if (writer is IMultiThreadedWriter)
            {
                ((IMultiThreadedWriter)writer).UseMultiThreadedWriting = false;
            }

            TripleStore store = new TripleStore();

            if (writer is TriXWriter)
            {
                //For TriX must load all into memory and then write out all at once
                foreach (Uri u in this.ListGraphs())
                {
                    Graph g = new Graph();
                    this._manager.LoadGraph(g, u);
                    g.BaseUri = u;
                    store.Add(g);
                    this.Information = "Loading into memory prior to export, loaded " + store.Graphs.Sum(x => x.Triples.Count) + " Triple(s) in " + store.Graphs.Count + " Graph(s) so far...";
                    if (this.HasBeenCancelled)
                    {
                        this.Information = "Export Cancelled";
                        return(new TaskResult(true));
                    }
                }
                this.Information = "Exporting Data all at once, have " + store.Graphs.Sum(x => x.Triples.Count) + " Triple(s) in " + store.Graphs.Count + " Graph(s) to export...";
                writer.Save(store, new StreamWriter(this._file));
                this.Information = "Exported " + store.Graphs.Sum(x => x.Triples.Count) + " Triple(s) in " + store.Graphs.Count + " Graph(s)";
            }
            else
            {
                if (File.Exists(this._file))
                {
                    File.Delete(this._file);
                }

                //For non-TriX formats assume it is safe to append one Graph at a time to the file
                int graphCount = 0, tripleCount = 0;
                foreach (Uri u in this.ListGraphs())
                {
                    using (FileStream stream = new FileStream(this._file, FileMode.Append))
                    {
                        if (writer is IFormatterBasedWriter)
                        {
                            //Stream via a WriteThroughHandler
                            this.Information = "Stream Exporting Graph " + (u != null ? u.AbsoluteUri : "Default");
                            WriteThroughHandler   handler     = new WriteThroughHandler(((IFormatterBasedWriter)writer).TripleFormatterType, new StreamWriter(stream), true);
                            ExportProgressHandler progHandler = new ExportProgressHandler(handler, this, tripleCount);
                            this._manager.LoadGraph(progHandler, u);
                            graphCount++;
                            tripleCount = progHandler.TripleCount;

                            this.Information = "Finished Stream Exporting Graph " + (u != null ? u.AbsoluteUri : "Default") + ", exported " + tripleCount + " Triple(s) in " + graphCount + " Graph(s) so far...";
                        }
                        else
                        {
                            //Load Graph into memory
                            Graph g = new Graph();
                            g.BaseUri        = u;
                            this.Information = "Loading Graph " + (u != null ? u.AbsoluteUri : "Default");
                            this._manager.LoadGraph(g, u);
                            g.BaseUri = u;

                            if (this.HasBeenCancelled)
                            {
                                stream.Close();
                                this.Information = "Export Cancelled, exported " + tripleCount + " Triple(s) in " + graphCount + " Graph(s)";
                                return(new TaskResult(true));
                            }

                            graphCount++;
                            tripleCount += g.Triples.Count;

                            //Save it
                            store.Add(g);
                            writer.Save(store, new StreamWriter(stream, def.Encoding));
                            store.Remove(u);

                            this.Information = "Exporting Data graph by graph, exported " + tripleCount + " Triple(s) in " + graphCount + " Graph(s) so far...";
                        }

                        //Check for cancellation
                        if (this.HasBeenCancelled)
                        {
                            stream.Close();
                            this.Information = "Export Cancelled, exported " + tripleCount + " Triple(s) in " + graphCount + " Graph(s)";
                            return(new TaskResult(true));
                        }
                    }
                }
                this.Information = "Exported " + tripleCount + " Triple(s) in " + graphCount + " Graph(s)";
            }

            return(new TaskResult(true));
        }
コード例 #22
0
        /// <summary>
        /// Queries the store asynchronously
        /// </summary>
        /// <param name="sparqlQuery">SPARQL Query</param>
        /// <param name="rdfHandler">RDF Handler</param>
        /// <param name="resultsHandler">Results Handler</param>
        /// <param name="callback">Callback</param>
        /// <param name="state">State to pass to the callback</param>
        public void Query(IRdfHandler rdfHandler, ISparqlResultsHandler resultsHandler, string sparqlQuery, AsyncStorageCallback callback, object state)
        {
            try
            {
                // First off parse the Query to see what kind of query it is
                SparqlQuery q;
                try
                {
                    q = _parser.ParseFromString(sparqlQuery);
                }
                catch (RdfParseException parseEx)
                {
                    callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SparqlQueryWithHandler, parseEx), state);
                    return;
                }
                catch (Exception ex)
                {
                    callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SparqlQueryWithHandler, new RdfStorageException("An unexpected error occurred while trying to parse the SPARQL Query prior to sending it to the Store, see inner exception for details", ex)), state);
                    return;
                }

                // Now select the Accept Header based on the query type
                String accept = (SparqlSpecsHelper.IsSelectQuery(q.QueryType) || q.QueryType == SparqlQueryType.Ask) ? MimeTypesHelper.HttpSparqlAcceptHeader : MimeTypesHelper.HttpAcceptHeader;

                // Create the Request, for simplicity async requests are always POST
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_endpoint.Uri);
                request.Accept      = accept;
                request.Method      = "POST";
                request.ContentType = MimeTypesHelper.Utf8WWWFormURLEncoded;
                request             = ApplyRequestOptions(request);

                Tools.HttpDebugRequest(request);

                request.BeginGetRequestStream(r =>
                {
                    try
                    {
                        Stream stream = request.EndGetRequestStream(r);
                        using (StreamWriter writer = new StreamWriter(stream, new UTF8Encoding(Options.UseBomForUtf8)))
                        {
                            writer.Write("query=");
                            writer.Write(HttpUtility.UrlEncode(sparqlQuery));
                            writer.Close();
                        }

                        request.BeginGetResponse(r2 =>
                        {
                            // Get the Response and process based on the Content Type
                            try
                            {
                                HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(r2);
                                Tools.HttpDebugResponse(response);
                                StreamReader data = new StreamReader(response.GetResponseStream());
                                String ctype      = response.ContentType;
                                if (SparqlSpecsHelper.IsSelectQuery(q.QueryType) || q.QueryType == SparqlQueryType.Ask)
                                {
                                    // ASK/SELECT should return SPARQL Results
                                    ISparqlResultsReader resreader = MimeTypesHelper.GetSparqlParser(ctype, q.QueryType == SparqlQueryType.Ask);
                                    resreader.Load(resultsHandler, data);
                                    response.Close();
                                }
                                else
                                {
                                    // CONSTRUCT/DESCRIBE should return a Graph
                                    IRdfReader rdfreader = MimeTypesHelper.GetParser(ctype);
                                    rdfreader.Load(rdfHandler, data);
                                    response.Close();
                                }
                                callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SparqlQueryWithHandler, sparqlQuery, rdfHandler, resultsHandler), state);
                            }
                            catch (WebException webEx)
                            {
                                callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SparqlQueryWithHandler, StorageHelper.HandleHttpQueryError(webEx)), state);
                            }
                            catch (Exception ex)
                            {
                                callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SparqlQueryWithHandler, StorageHelper.HandleQueryError(ex)), state);
                            }
                        }, state);
                    }
                    catch (WebException webEx)
                    {
                        callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SparqlQueryWithHandler, StorageHelper.HandleHttpQueryError(webEx)), state);
                    }
                    catch (Exception ex)
                    {
                        callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SparqlQueryWithHandler, StorageHelper.HandleQueryError(ex)), state);
                    }
                }, state);
            }
            catch (WebException webEx)
            {
                callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SparqlQueryWithHandler, StorageHelper.HandleHttpQueryError(webEx)), state);
            }
            catch (Exception ex)
            {
                callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SparqlQueryWithHandler, StorageHelper.HandleQueryError(ex)), state);
            }
        }
コード例 #23
0
        private bool SetOptions(String[] args)
        {
            if (args.Length == 0 || (args.Length == 1 && args[0].Equals("-help")))
            {
                this.ShowUsage();
                return(false);
            }

            //Look through the arguments to see what we've been asked to do
            foreach (String arg in args)
            {
                if (arg.StartsWith("-hs"))
                {
                    if (arg.Contains(':'))
                    {
                        bool hs;
                        if (Boolean.TryParse(arg.Substring(arg.IndexOf(':') + 1), out hs))
                        {
                            this._options.Add(new HighSpeedOption(hs));
                        }
                        else
                        {
                            this._options.Add(new HighSpeedOption(true));
                        }
                    }
                    else
                    {
                        this._options.Add(new HighSpeedOption(true));
                    }
                }
                else if (arg.StartsWith("-pp"))
                {
                    if (arg.Contains(':'))
                    {
                        bool pp;
                        if (Boolean.TryParse(arg.Substring(arg.IndexOf(':') + 1), out pp))
                        {
                            this._options.Add(new PrettyPrintingOption(pp));
                        }
                        else
                        {
                            this._options.Add(new PrettyPrintingOption(true));
                        }
                    }
                    else
                    {
                        this._options.Add(new PrettyPrintingOption(true));
                    }
                }
                else if (arg.StartsWith("-c"))
                {
                    if (arg.Contains(':'))
                    {
                        int c;
                        if (Int32.TryParse(arg.Substring(arg.IndexOf(':') + 1), out c))
                        {
                            this._options.Add(new CompressionLevelOption(c));
                        }
                        else
                        {
                            this._options.Add(new CompressionLevelOption(WriterCompressionLevel.Default));
                        }
                    }
                    else
                    {
                        this._options.Add(new CompressionLevelOption(WriterCompressionLevel.Default));
                    }
                }
                else if (arg.StartsWith("-stylesheet:"))
                {
                    String stylesheet = arg.Substring(arg.IndexOf(':') + 1);
                    this._options.Add(new StylesheetOption(stylesheet));
                }
                else if (arg.Equals("-overwrite"))
                {
                    this._overwrite = true;
                }
                else if (arg.StartsWith("-out:") || arg.StartsWith("-output:"))
                {
                    this._outputFilename = arg.Substring(arg.IndexOf(':') + 1);
                }
                else if (arg.StartsWith("-format:") || arg.StartsWith("-outformat:"))
                {
                    String format = arg.Substring(arg.IndexOf(':') + 1);
                    if (!format.Contains("/"))
                    {
                        try
                        {
                            foreach (String mimeType in MimeTypesHelper.GetMimeTypes(format))
                            {
                                this._outFormats.Add(mimeType);
                            }
                        }
                        catch
                        {
                            Console.Error.WriteLine("rdfConvert: Error: The File Extension '" + format + "' is not permissible since dotNetRDF cannot infer a MIME type from the extension");
                            return(false);
                        }
                    }
                    else
                    {
                        //Validate the MIME Type
                        if (!IsValidMimeType(format))
                        {
                            Console.Error.WriteLine("rdfConvert: Error: The MIME Type '" + format + "' is not permissible since dotNetRDF does not support outputting in that format");
                            return(false);
                        }
                        this._outFormats.Add(format);
                    }
                }
                else if (arg.StartsWith("-outext:") || arg.StartsWith("-ext:"))
                {
                    this._outExt = arg.Substring(arg.IndexOf(':') + 1);
                    if (!this._outExt.StartsWith("."))
                    {
                        this._outExt = "." + this._outExt;
                    }
                }
                else if (arg.Equals("-debug"))
                {
                    this._debug = true;
                }
                else if (arg.Equals("-help"))
                {
                    //Ignore help argument if other arguments are present
                }
                else if (arg.Equals("-nocache"))
                {
                    Options.UriLoaderCaching = false;
                }
                else if (arg.Equals("-bom"))
                {
                    Options.UseBomForUtf8 = true;
                }
                else if (arg.Equals("-nobom"))
                {
                    Options.UseBomForUtf8 = false;
                }
                else if (arg.Equals("-best"))
                {
                    this._best = true;
                }
                else if (arg.Equals("-verbose"))
                {
                    this._verbose = true;
                }
                else if (arg.Equals("-warnings"))
                {
                    this._warnings           = true;
                    UriLoader.Warning       += this.ShowWarning;
                    UriLoader.StoreWarning  += this.ShowWarning;
                    FileLoader.Warning      += this.ShowWarning;
                    FileLoader.StoreWarning += this.ShowWarning;
                }
                else
                {
                    //Anything else is treated as an input
                    if (arg.Contains("://"))
                    {
                        try
                        {
                            this._inputs.Add(new UriInput(new Uri(arg)));
                        }
                        catch (UriFormatException uriEx)
                        {
                            Console.Error.WriteLine("rdfConvert: Error: The Input '" + arg + "' which rdfConvert assumed to be a URI is not a valid URI - " + uriEx.Message);
                            return(false);
                        }
                    }
                    else
                    {
                        this._inputs.Add(new FileInput(arg));
                    }
                }
            }

            //If there are no this._inputs then we'll abort
            if (this._inputs.Count == 0)
            {
                Console.Error.WriteLine("rdfConvert: Abort: No Inputs were provided - please provide one/more files or URIs you wish to convert");
                return(false);
            }

            //If there are no writers specified then we'll abort
            if (this._outFormats.Count == 0)
            {
                if (this._inputs.Count == 1 && !this._outputFilename.Equals(String.Empty))
                {
                    //If only 1 input and an output filename can detect the output format from that filename
                    this._outFormats.AddRange(MimeTypesHelper.GetMimeTypes(MimeTypesHelper.GetTrueFileExtension(this._outputFilename)));
                }
                else
                {
                    Console.Error.WriteLine("rdfConvert: Abort: Aborting since no output options have been specified, use either the -format:[mime/type|ext] argument to specify output format for multiple inputs of use -out:filename.ext to specify the output for a single input");
                    return(false);
                }
            }

            //Ensure Output Extension (if specified) is OK
            if (!this._outExt.Equals(String.Empty))
            {
                if (!this._outExt.StartsWith("."))
                {
                    this._outExt = "." + this._outExt;
                }
            }

            //If more than one input and an Output Filename be sure to strip any Extension from the Filename
            if (this._inputs.Count > 1 && !this._outputFilename.Equals(String.Empty))
            {
                this._outputFilename = Path.GetFileNameWithoutExtension(this._outputFilename);
            }

            return(true);
        }
コード例 #24
0
 private bool ParseInputs(String arg, String dir)
 {
     if (arg.Contains("*"))
     {
         //Wildcard is a File wildcard only
         if (arg.Equals("*") || arg.Equals("*.*"))
         {
             //All Files
             this._inputs.AddRange(Directory.GetFiles(dir));
             return(true);
         }
         else if (arg.Contains("*."))
         {
             //All Files with a given Extension
             String ext = arg.Substring(arg.LastIndexOf("*.") + 1);
             this._inputs.AddRange(Directory.GetFiles(dir).Where(f => ext.Equals(MimeTypesHelper.GetTrueFileExtension(f))));
             return(true);
         }
         else
         {
             //Invalid File Wildcard
             Console.Error.WriteLine("rdfOptStats: Error: Input Wildcard '" + arg + "' does not appear to be a valid wildcard - only simple wildcards like *.* or *.rdf are currently supported");
             return(false);
         }
     }
     else
     {
         if (!File.Exists(arg))
         {
             Console.Error.WriteLine("rdfOptStats: Error: Input File '" + arg + "' does not exist");
             return(false);
         }
         else
         {
             this._inputs.Add(arg);
             return(true);
         }
     }
     return(true);
 }
コード例 #25
0
        /// <summary>
        /// Attempts to load a RDF dataset asynchronously from the given URI using a RDF Handler.
        /// </summary>
        /// <param name="handler">RDF Handler to use.</param>
        /// <param name="u">URI to attempt to get a RDF dataset from.</param>
        /// <param name="parser">Parser to use to parse the RDF dataset.</param>
        /// <param name="callback">Callback to invoke when the operation completes.</param>
        /// <param name="state">State to pass to the callback.</param>
        /// <remarks>
        /// <para>
        /// If the <paramref name="parser"/> parameter is set to null then this method attempts to select the relevant Store Parser based on the Content Type header returned in the HTTP Response.
        /// </para>
        /// <para>
        /// If you know ahead of time the Content Type you can explicitly pass in the parser to use.
        /// </para>
        /// <para>
        /// If the loading completes normally the callback will be invoked normally, if an error occurs it will be invoked and passed an instance of <see cref="AsyncError"/> as the state which contains details of the error and the original state.
        /// </para>
        /// </remarks>
        public static void Load(IRdfHandler handler, Uri u, IStoreReader parser, RdfHandlerCallback callback, Object state)
        {
            if (u == null)
            {
                throw new RdfParseException("Cannot read a RDF dataset from a null URI");
            }
            if (handler == null)
            {
                throw new RdfParseException("Cannot read a RDF dataset using a null RDF handler");
            }

            try
            {
                if (u.IsFile)
                {
                    // Invoke FileLoader instead
                    RaiseWarning("This is a file: URI so invoking the FileLoader instead");
                    if (Path.DirectorySeparatorChar == '/')
                    {
                        FileLoader.Load(handler, u.AbsoluteUri.Substring(7), parser);
                    }
                    else
                    {
                        FileLoader.Load(handler, u.AbsoluteUri.Substring(8), parser);
                    }
                    // FileLoader.Load() will run synchronously so once this completes we can invoke the callback
                    callback(handler, state);
                    return;
                }
                if (u.Scheme.Equals("data"))
                {
                    // Invoke DataUriLoader instead
                    RaiseWarning("This is a data: URI so invoking the DataUriLoader instead");
                    DataUriLoader.Load(handler, u);
                    // After DataUriLoader.Load() has run (which happens synchronously) can invoke the callback
                    callback(handler, state);
                    return;
                }

                // Sanitise the URI to remove any Fragment ID
                u = Tools.StripUriFragment(u);

                // Setup the Request
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(u);

                // Want to ask for RDF dataset formats
                if (parser != null)
                {
                    // If a non-null parser set up a HTTP Header that is just for the given parser
                    request.Accept = MimeTypesHelper.CustomHttpAcceptHeader(parser);
                }
                else
                {
                    request.Accept = MimeTypesHelper.HttpAcceptHeader;
                }

                // Use HTTP GET
                request.Method = "GET";
#if !NETCORE
                request.Timeout = Options.UriLoaderTimeout;
#endif
                if (_userAgent != null && !_userAgent.Equals(String.Empty))
                {
#if NETCORE
                    request.Headers[HttpRequestHeader.UserAgent] = _userAgent;
#else
                    request.UserAgent = _userAgent;
#endif
                }

                Tools.HttpDebugRequest(request);

                try
                {
                    request.BeginGetResponse(result =>
                    {
                        try
                        {
                            using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result))
                            {
                                Tools.HttpDebugResponse(response);

                                // Get a Parser and load the RDF
                                if (parser == null)
                                {
                                    try
                                    {
                                        // Only need to auto-detect the parser if a specific one wasn't specified
                                        parser          = MimeTypesHelper.GetStoreParser(response.ContentType);
                                        parser.Warning += RaiseWarning;
                                        parser.Load(handler, new StreamReader(response.GetResponseStream()));
                                    }
                                    catch (RdfParserSelectionException)
                                    {
                                        RaiseStoreWarning("Unable to select a RDF Dataset parser based on Content-Type: " + response.ContentType + " - seeing if the content is an RDF Graph instead");

                                        try
                                        {
                                            // If not a RDF Dataset format see if it is a Graph
                                            IRdfReader rdfParser = MimeTypesHelper.GetParser(response.ContentType);
                                            rdfParser.Load(handler, new StreamReader(response.GetResponseStream()));
                                        }
                                        catch (RdfParserSelectionException)
                                        {
                                            String data     = new StreamReader(response.GetResponseStream()).ReadToEnd();
                                            parser          = StringParser.GetDatasetParser(data);
                                            parser.Warning += RaiseStoreWarning;
                                            parser.Load(handler, new StringReader(data));
                                        }
                                    }
                                }
                                else
                                {
                                    parser.Warning += RaiseStoreWarning;
                                    parser.Load(handler, new StreamReader(response.GetResponseStream()));
                                }

                                // Finally can invoke the callback
                                callback(handler, state);
                            }
                        }
                        catch (WebException webEx)
                        {
                            if (webEx.Response != null)
                            {
                                Tools.HttpDebugResponse((HttpWebResponse)webEx.Response);
                            }
                            callback(handler, new AsyncError(new RdfParseException("A HTTP Error occurred loading the URI '" + u.AbsoluteUri + "' asynchronously, see inner exeption for details", webEx), state));
                        }
                        catch (Exception ex)
                        {
                            callback(handler, new AsyncError(new RdfParseException("Unexpected error while loading the URI '" + u.AbsoluteUri + "' asynchronously, see inner exception for details", ex), state));
                        }
                    }, null);
                }
                catch (WebException webEx)
                {
                    if (webEx.Response != null)
                    {
                        Tools.HttpDebugResponse((HttpWebResponse)webEx.Response);
                    }
                    callback(handler, new AsyncError(new RdfParseException("A HTTP Error occurred loading the URI '" + u.AbsoluteUri + "' asynchronously, see inner exeption for details", webEx), state));
                }
                catch (Exception ex)
                {
                    callback(handler, new AsyncError(new RdfParseException("Unexpected error while loading the URI '" + u.AbsoluteUri + "' asynchronously, see inner exception for details", ex), state));
                }
            }
            catch (UriFormatException uriEx)
            {
                // Uri Format Invalid
                throw new RdfException("Unable to load from the given URI '" + u.AbsoluteUri + "' since it's format was invalid, see inner exception for details", uriEx);
            }
        }
コード例 #26
0
        public void Run()
        {
            if (this._args.Length == 0)
            {
                this.ShowUsage();
            }
            else
            {
                if (!this.ParseOptions())
                {
                    Console.Error.WriteLine("rdfOptStats: Error: One/More options were invalid");
                    return;
                }

                if (this._inputs.Count == 0)
                {
                    Console.Error.WriteLine("rdfOptStats: Error: No Inputs Specified");
                    return;
                }

                List <BaseStatsHandler> handlers = new List <BaseStatsHandler>();
                if (this._subjects && this._predicates && this._objects)
                {
                    handlers.Add(new SPOStatsHandler(this._literals));
                }
                else if (this._subjects && this._predicates)
                {
                    handlers.Add(new SPStatsHandler(this._literals));
                }
                else
                {
                    if (this._subjects)
                    {
                        handlers.Add(new SubjectStatsHandler(this._literals));
                    }
                    if (this._predicates)
                    {
                        handlers.Add(new PredicateStatsHandler(this._literals));
                    }
                    if (this._objects)
                    {
                        handlers.Add(new ObjectStatsHandler(this._literals));
                    }
                }
                if (this._nodes)
                {
                    handlers.Add(new NodeStatsHandler());
                }

                bool        ok = true;
                IRdfHandler handler;
                if (handlers.Count == 1)
                {
                    handler = handlers[0];
                }
                else
                {
                    handler = new MultiHandler(handlers.OfType <IRdfHandler>());
                }

                Stopwatch timer = new Stopwatch();
                timer.Start();
                for (int i = 0; i < this._inputs.Count; i++)
                {
                    Console.WriteLine("rdfOptStats: Processing Input " + (i + 1) + " of " + this._inputs.Count + " - '" + this._inputs[i] + "'");

                    try
                    {
                        FileLoader.Load(handler, this._inputs[i]);
                    }
                    catch (RdfParserSelectionException selEx)
                    {
                        ok = false;
                        Console.Error.WriteLine("rdfOptStats: Error: Unable to select a Parser to read input");
                        break;
                    }
                    catch (RdfParseException parseEx)
                    {
                        ok = false;
                        Console.Error.WriteLine("rdfOptStats: Error: Parsing Error while reading input");
                        break;
                    }
                    catch (RdfException parseEx)
                    {
                        ok = false;
                        Console.Error.WriteLine("rdfOptStats: Error: RDF Error while reading input");
                        break;
                    }
                    catch (Exception ex)
                    {
                        ok = false;
                        Console.Error.WriteLine("rdfOptStats: Error: Unexpected Error while reading input");
                        break;
                    }
                }
                Console.WriteLine("rdfOptStats: Finished Processing Inputs");
                timer.Stop();
                Console.WriteLine("rdfOptStats: Took " + timer.Elapsed + " to process inputs");
                timer.Reset();

                if (ok)
                {
                    //Output the Stats
                    timer.Start();
                    Graph g = new Graph();
                    g.NamespaceMap.Import(handlers.First().Namespaces);
                    try
                    {
                        foreach (BaseStatsHandler h in handlers)
                        {
                            h.GetStats(g);
                        }
                        IRdfWriter writer = MimeTypesHelper.GetWriterByFileExtension(MimeTypesHelper.GetTrueFileExtension(this._file));
                        if (writer is ICompressingWriter)
                        {
                            ((ICompressingWriter)writer).CompressionLevel = WriterCompressionLevel.High;
                        }
                        if (writer is IHighSpeedWriter)
                        {
                            ((IHighSpeedWriter)writer).HighSpeedModePermitted = false;
                        }
                        writer.Save(g, this._file);

                        Console.WriteLine("rdfOptStats: Statistics output to " + this._file);
                        timer.Stop();
                        Console.WriteLine("rdfOptStats: Took " + timer.Elapsed + " to output statistics");
                    }
                    catch (Exception ex)
                    {
                        Console.Error.WriteLine("rdfOptStats: Error: Unexpected error outputting statistics to " + this._file);
                        Console.Error.WriteLine(ex.Message);
                        Console.Error.WriteLine(ex.StackTrace);
                    }
                }
                else
                {
                    Console.Error.WriteLine("rdfOptStats: Error: Unable to output statistics due to errors during input processing");
                }
            }
        }
コード例 #27
0
        private void btnContinue_Click(object sender, RoutedEventArgs e)
        {
            if (this.chkAlwaysCheckFileAssociations.IsChecked != null)
            {
                Properties.Settings.Default.AlwaysCheckFileAssociations = (bool)this.chkAlwaysCheckFileAssociations.IsChecked;
                Properties.Settings.Default.Save();
            }

            //Set Associations appropriately
            foreach (FileAssociationInfo info in this._associations)
            {
                //Ensure File Association exists in the registry
                if (!info.Exists)
                {
                    info.Create();
                    try
                    {
                        info.ContentType = MimeTypesHelper.GetMimeType(info.Extension);
                    }
                    catch
                    {
                        //Ignore error, just means we don't know the MIME type for the Extension
                    }
                    info.PerceivedType = PerceivedTypes.Text;
                }

                //Ensure there is always a Perceived Type and a Content Type registered
                if (info.PerceivedType == PerceivedTypes.None)
                {
                    info.PerceivedType = PerceivedTypes.Text;
                }
                if (info.ContentType.Equals(String.Empty))
                {
                    try
                    {
                        String mimeType = MimeTypesHelper.GetMimeType(info.Extension);
                        info.ContentType = mimeType;
                    }
                    catch
                    {
                        //Ignore error, just means we don't know the MIME type for the Extension
                    }
                }

                //Add/Remove the association
                bool addAssociation = this.IsAssociationChecked(info.Extension);
                if (addAssociation)
                {
                    info.ProgID = RegistryProgramID;
                }
                else if (this._currentAssociations.Contains(info.Extension))
                {
                    //We want to remove the association to ourselves
                    info.ProgID = info.Extension.Substring(1) + "_auto_file";
                }

                //TODO: Ensure we are in the OpenWith List
            }

            this.Close();
        }
コード例 #28
0
        public void ProcessRequest(HttpContext context)
        {
            //Turn on Response Buffering
            context.Response.Buffer = true;

            //Prepare the Cache Directories
            if (!Path.IsPathRooted(this._cacheDir))
            {
                this._cacheDir = context.Server.MapPath(this._cacheDir);
            }
            if (this._loader == null)
            {
                this._loader = new ExpansionLoader(this._cacheDir);
            }

            //Add our Custom Headers
            try
            {
                context.Response.Headers.Add("X-dotNetRDF-Version", Assembly.GetAssembly(typeof(VDS.RDF.IGraph)).GetName().Version.ToString());
            }
            catch (PlatformNotSupportedException)
            {
                context.Response.AddHeader("X-dotNetRDF-Version", Assembly.GetAssembly(typeof(VDS.RDF.IGraph)).GetName().Version.ToString());
            }

            try
            {
                //Retrieve the desired URI and Profile URI from Querystring parameters
                String uri     = context.Request.QueryString["uri"];
                String profile = context.Request.QueryString["profile"];

                if (uri == null)
                {
                    if (context.Request.Url.Query.Equals(String.Empty))
                    {
                        throw new ArgumentNullException("uri", "Required uri parameter used to designate the URI you wish to expand was not found.  Your request must use a URI of the form " + context.Request.Url.ToString() + "?uri=" + Uri.EscapeDataString("http://example.org"));
                    }
                    else
                    {
                        throw new ArgumentNullException("uri", "Required uri parameter used to designate the URI you wish to expand was not found.  Your request must use a URI of the form " + context.Request.Url.ToString().Replace(context.Request.Url.Query, String.Empty) + "?uri=" + Uri.EscapeDataString("http://example.org"));
                    }
                }

                //Note that the ExpansionLoader class automatically handles all the Caching for us
                IInMemoryQueryableStore store;
                String uriHash = new Uri(uri).GetSha256Hash();
                if (profile == null)
                {
                    //Use Default Profile
                    store = this._loader.Load(new Uri(uri));
                }
                else
                {
                    //Use Custom Profile
                    store = this._loader.Load(new Uri(uri), new Uri(profile));
                }

                String       ctype;
                IStoreWriter writer = MimeTypesHelper.GetStoreWriter(context.Request.AcceptTypes, out ctype);
                context.Response.ContentType = ctype;
                writer.Save(store, new StreamParams(context.Response.OutputStream));
            }
            catch (ArgumentNullException argNull)
            {
                HandleErrors(context, "Missing Argument", argNull);
            }
            catch (RdfParseException parseEx)
            {
                HandleErrors(context, "RDF Parser Error", parseEx);
            }
            catch (RdfException rdfEx)
            {
                HandleErrors(context, "RDF Error", rdfEx);
            }
            catch (Exception ex)
            {
                HandleErrors(context, "Error", ex);
            }
        }
コード例 #29
0
        /// <summary>
        /// Attempts to load a RDF dataset from the given URI using a RDF Handler
        /// </summary>
        /// <param name="handler">RDF Handler to use</param>
        /// <param name="u">URI to attempt to get a RDF dataset from</param>
        /// <param name="parser">Parser to use to parse the RDF dataset</param>
        /// <remarks>
        /// <para>
        /// If the <paramref name="parser"/> parameter is set to null then this method attempts to select the relevant Store Parser based on the Content Type header returned in the HTTP Response.
        /// </para>
        /// <para>
        /// If you know ahead of time the Content Type you can explicitly pass in the parser to use.
        /// </para>
        /// </remarks>
        public static void Load(IRdfHandler handler, Uri u, IStoreReader parser)
        {
            if (u == null)
            {
                throw new RdfParseException("Cannot read a RDF dataset from a null URI");
            }
            if (handler == null)
            {
                throw new RdfParseException("Cannot read a RDF dataset using a null RDF handler");
            }

            try
            {
#if SILVERLIGHT
                if (u.IsFile())
#else
                if (u.IsFile)
#endif

                {
                    //Invoke FileLoader instead
                    RaiseWarning("This is a file: URI so invoking the FileLoader instead");
                    if (Path.DirectorySeparatorChar == '/')
                    {
                        FileLoader.Load(handler, u.ToString().Substring(7), parser);
                    }
                    else
                    {
                        FileLoader.Load(handler, u.ToString().Substring(8), parser);
                    }
                    return;
                }

                //Sanitise the URI to remove any Fragment ID
                u = Tools.StripUriFragment(u);

                //Set-up the Request
                HttpWebRequest httpRequest;
                httpRequest = (HttpWebRequest)WebRequest.Create(u);

                //Want to ask for TriG, NQuads or TriX
                if (parser != null)
                {
                    //If a non-null parser set up a HTTP Header that is just for the given parser
                    httpRequest.Accept = MimeTypesHelper.CustomHttpAcceptHeader(parser);
                }
                else
                {
                    httpRequest.Accept = MimeTypesHelper.HttpRdfDatasetAcceptHeader;
                }

                //Use HTTP GET
                httpRequest.Method = "GET";
#if !SILVERLIGHT
                httpRequest.Timeout = Options.UriLoaderTimeout;
#endif
                if (_userAgent != null && !_userAgent.Equals(String.Empty))
                {
                    httpRequest.UserAgent = _userAgent;
                }

#if DEBUG
                //HTTP Debugging
                if (Options.HttpDebugging)
                {
                    Tools.HttpDebugRequest(httpRequest);
                }
#endif

                using (HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse())
                {
#if DEBUG
                    //HTTP Debugging
                    if (Options.HttpDebugging)
                    {
                        Tools.HttpDebugResponse(httpResponse);
                    }
#endif

                    //Get a Parser and Load the RDF
                    if (parser == null)
                    {
                        try
                        {
                            parser          = MimeTypesHelper.GetStoreParser(httpResponse.ContentType);
                            parser.Warning += RaiseStoreWarning;
                            parser.Load(handler, new StreamParams(httpResponse.GetResponseStream()));
                        }
                        catch (RdfParserSelectionException selEx)
                        {
                            String data = new StreamReader(httpResponse.GetResponseStream()).ReadToEnd();
                            parser          = StringParser.GetDatasetParser(data);
                            parser.Warning += RaiseStoreWarning;
                            parser.Load(handler, new TextReaderParams(new StringReader(data)));
                        }
                    }
                    else
                    {
                        parser.Warning += RaiseStoreWarning;
                        parser.Load(handler, new StreamParams(httpResponse.GetResponseStream()));
                    }
                }
            }
            catch (UriFormatException uriEx)
            {
                //Uri Format Invalid
                throw new RdfException("Unable to load from the given URI '" + u.ToString() + "' since it's format was invalid, see inner exception for details", uriEx);
            }
            catch (WebException webEx)
            {
#if DEBUG
                if (Options.HttpDebugging)
                {
                    if (webEx.Response != null)
                    {
                        Tools.HttpDebugResponse((HttpWebResponse)webEx.Response);
                    }
                }
#endif
                //Some sort of HTTP Error occurred
                throw new WebException("A HTTP Error occurred resolving the URI '" + u.ToString() + "', see innner exception for details", webEx);
            }
        }
コード例 #30
0
        /// <summary>
        /// Loads RDF data using an RDF Handler from a data: URI
        /// </summary>
        /// <param name="handler">RDF Handler</param>
        /// <param name="u">URI to load from</param>
        /// <remarks>
        /// Invokes the normal <see cref="UriLoader">UriLoader</see> instead if a the URI provided is not a data: URI
        /// </remarks>
        /// <exception cref="UriFormatException">Thrown if the metadata portion of the URI which indicates the MIME Type, Character Set and whether Base64 encoding is used is malformed</exception>
        public static void Load(IRdfHandler handler, Uri u)
        {
            if (u == null)
            {
                throw new RdfParseException("Cannot load RDF from a null URI");
            }
            if (handler == null)
            {
                throw new RdfParseException("Cannot read RDF into a null RDF Handler");
            }

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

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

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

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

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

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