/// <summary> /// Saves a Result Set as GZipped output /// </summary> /// <param name="results">Result Set to save</param> /// <param name="output">Writer to save to</param> public void Save(SparqlResultSet results, TextWriter output) { if (results == null) { throw new RdfOutputException("Cannot write RDF from a null Graph"); } if (output is StreamWriter) { // Check for inner GZipStream and re-wrap if required StreamWriter streamOutput = (StreamWriter)output; if (streamOutput.BaseStream is GZipStream) { _writer.Save(results, streamOutput); } else { streamOutput = new StreamWriter(new GZipStream(streamOutput.BaseStream, CompressionMode.Compress)); _writer.Save(results, streamOutput); } } else { throw new RdfOutputException("GZipped Output can only be written to StreamWriter instances"); } }
/// <summary> /// Writes the SPARQL Result Set to a String and returns the Output in your chosen format /// </summary> /// <param name="results">SPARQL Result Set</param> /// <param name="writer">Writer to use to generate the SPARQL Results output</param> /// <returns></returns> public static String Write(SparqlResultSet results, ISparqlResultsWriter writer) { System.IO.StringWriter sw = new System.IO.StringWriter(); writer.Save(results, sw); return(sw.ToString()); }
public static void Save(this ISparqlResultsWriter resultsWriter, SparqlResultSet resultSet, string filename) { using (var output = new StreamWriter(filename)) { resultsWriter.Save(resultSet, output); } }
private void EnsureTestData(String file, ISparqlResultsWriter writer) { if (!File.Exists(file)) { Graph g = new Graph(); g.LoadFromEmbeddedResource("VDS.RDF.Configuration.configuration.ttl"); SparqlResultSet results = g.ExecuteQuery("SELECT * WHERE { ?s ?p ?o }") as SparqlResultSet; if (results == null) Assert.Fail("Failed to generate sample SPARQL Results"); writer.Save(results, file); } }
private void EnsureTestData(String file, ISparqlResultsWriter writer) { if (!File.Exists(file)) { Graph g = new Graph(); g.LoadFromEmbeddedResource("VDS.RDF.Configuration.configuration.ttl"); g.Retract(g.Triples.Where(t => !t.IsGroundTriple).ToList()); SparqlResultSet results = g.ExecuteQuery("SELECT * WHERE { ?s ?p ?o }") as SparqlResultSet; if (results == null) { Assert.True(false, "Failed to generate sample SPARQL Results"); } writer.Save(results, file); } }
private void btnExport_Click(object sender, EventArgs e) { ExportResultSetOptionsForm exporter = new ExportResultSetOptionsForm(); if (exporter.ShowDialog() == DialogResult.OK) { ISparqlResultsWriter writer = exporter.Writer; String file = exporter.File; try { writer.Save(this._results, file); MessageBox.Show("Successfully exported the SPARQL Results to the file '" + file + "'", "SPARQL Results Export Success", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show("An error occurred attempting to export the SPARQL Results:\n" + ex.Message, "SPARQL Results Export Failed", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
public void Setup() { this._g = new Graph(); this._g.LoadFromEmbeddedResource("VDS.RDF.Configuration.configuration.ttl"); this._results = this._g.ExecuteQuery("SELECT * WHERE { ?s ?p ?o }") as SparqlResultSet; foreach (MimeTypeDefinition def in MimeTypesHelper.Definitions) { // Omit CSV since that is a lossy format that does not round trip if (def.CanonicalMimeType.Equals("text/csv")) { continue; } if (def.CanWriteRdf && def.CanParseRdf) { IRdfWriter writer = def.GetRdfWriter(); bool isManual = !def.CanonicalFileExtension.EndsWith(".gz"); String filename = "gzip-tests" + (isManual ? String.Empty : "-auto") + "." + def.CanonicalFileExtension + (isManual ? ".gz" : String.Empty); if (isManual) { using (StreamWriter output = new StreamWriter(new GZipStream(new FileStream(filename, FileMode.Create, FileAccess.Write), CompressionMode.Compress))) { writer.Save(this._g, output); output.Close(); } this._manualTestFiles.Add(filename); } else { writer.Save(this._g, filename); this._autoTestFiles.Add(filename); } } else if (def.CanParseRdfDatasets && def.CanWriteRdfDatasets) { IStoreWriter writer = def.GetRdfDatasetWriter(); bool isManual = !def.CanonicalFileExtension.EndsWith(".gz"); String filename = "gzip-tests-datasets" + (isManual ? String.Empty : "-auto") + "." + def.CanonicalFileExtension + (isManual ? ".gz" : String.Empty); TripleStore store = new TripleStore(); store.Add(this._g); if (isManual) { using (Stream output = new GZipStream(new FileStream(filename, FileMode.Create, FileAccess.Write), CompressionMode.Compress)) { writer.Save(store, new StreamWriter(output)); output.Close(); } this._manualDatasetTestFiles.Add(filename); } else { writer.Save(store, new StreamWriter(new FileStream(filename, FileMode.Create, FileAccess.Write))); this._autoDatasetTestFiles.Add(filename); } } else if (def.CanParseSparqlResults && def.CanWriteSparqlResults) { ISparqlResultsWriter writer = def.GetSparqlResultsWriter(); bool isManual = !def.CanonicalFileExtension.EndsWith(".gz"); String filename = "gzip-tests-results" + (isManual ? String.Empty : "-auto") + "." + def.CanonicalFileExtension + (isManual ? ".gz" : String.Empty); if (isManual) { using (StreamWriter output = new StreamWriter(new GZipStream(new FileStream(filename, FileMode.Create, FileAccess.Write), CompressionMode.Compress))) { writer.Save(this._results, output); output.Close(); } this._manualResultsTestFiles.Add(filename); } else { writer.Save(this._results, new StreamWriter(filename)); this._autoResultsTestFiles.Add(filename); } } } }
/// <summary> /// Helper function which returns the Results (Graph/Triple Store/SPARQL Results) back to the Client in one of their accepted formats /// </summary> /// <param name="context">Context of the HTTP Request</param> /// <param name="result">Results of the Sparql Query</param> /// <param name="config">Handler Configuration</param> public static void SendToClient(HttpContext context, Object result, BaseHandlerConfiguration config) { MimeTypeDefinition definition = null; String ctype = "text/plain"; String[] acceptTypes = HandlerHelper.GetAcceptTypes(context); //Return the Results if (result is SparqlResultSet) { ISparqlResultsWriter sparqlWriter = null; //Try and get a MIME Type Definition using the HTTP Requests Accept Header if (acceptTypes != null) { definition = MimeTypesHelper.GetDefinitions(acceptTypes).FirstOrDefault(d => d.CanWriteSparqlResults); } //Try and get the registered Definition for SPARQL Results XML if (definition == null) { definition = MimeTypesHelper.GetDefinitions(MimeTypesHelper.SparqlXml[0]).FirstOrDefault(); } //If Definition is still null create a temporary definition if (definition == null) { definition = new MimeTypeDefinition("SPARQL Results XML", MimeTypesHelper.SparqlXml, Enumerable.Empty <String>()); definition.SparqlResultsWriterType = typeof(VDS.RDF.Writing.SparqlXmlWriter); } //Set up the Writer appropriately sparqlWriter = definition.GetSparqlResultsWriter(); context.Response.ContentType = definition.CanonicalMimeType; HandlerHelper.ApplyWriterOptions(sparqlWriter, config); //Clear any existing Response context.Response.Clear(); //Send Result Set to Client context.Response.ContentEncoding = definition.Encoding; sparqlWriter.Save((SparqlResultSet)result, new StreamWriter(context.Response.OutputStream, definition.Encoding)); } else if (result is IGraph) { IRdfWriter rdfWriter = null; //Try and get a MIME Type Definition using the HTTP Requests Accept Header if (acceptTypes != null) { definition = MimeTypesHelper.GetDefinitions(acceptTypes).FirstOrDefault(d => d.CanWriteRdf); } if (definition == null) { //If no appropriate definition then use the GetWriter method instead rdfWriter = MimeTypesHelper.GetWriter(acceptTypes, out ctype); } else { rdfWriter = definition.GetRdfWriter(); } //Setup the writer if (definition != null) { ctype = definition.CanonicalMimeType; } context.Response.ContentType = ctype; HandlerHelper.ApplyWriterOptions(rdfWriter, config); //Clear any existing Response context.Response.Clear(); //Send Graph to Client if (definition != null) { context.Response.ContentEncoding = definition.Encoding; rdfWriter.Save((IGraph)result, new StreamWriter(context.Response.OutputStream, definition.Encoding)); } else { rdfWriter.Save((IGraph)result, new StreamWriter(context.Response.OutputStream)); } } else if (result is ITripleStore) { IStoreWriter storeWriter = null; //Try and get a MIME Type Definition using the HTTP Requests Accept Header if (acceptTypes != null) { definition = MimeTypesHelper.GetDefinitions(acceptTypes).FirstOrDefault(d => d.CanWriteRdfDatasets); } if (definition == null) { //If no appropriate definition then use the GetStoreWriter method instead storeWriter = MimeTypesHelper.GetStoreWriter(acceptTypes, out ctype); } else { storeWriter = definition.GetRdfDatasetWriter(); } //Setup the writer if (definition != null) { ctype = definition.CanonicalMimeType; } context.Response.ContentType = ctype; HandlerHelper.ApplyWriterOptions(storeWriter, config); //Clear any existing Response context.Response.Clear(); //Send Triple Store to Client if (definition != null) { context.Response.ContentEncoding = definition.Encoding; storeWriter.Save((ITripleStore)result, new VDS.RDF.Storage.Params.StreamParams(context.Response.OutputStream, definition.Encoding)); } else { storeWriter.Save((ITripleStore)result, new VDS.RDF.Storage.Params.StreamParams(context.Response.OutputStream)); } } else if (result is ISparqlDataset) { //Wrap in a Triple Store and then call self so the Triple Store writing branch of this if gets called instead TripleStore store = new TripleStore(new DatasetGraphCollection((ISparqlDataset)result)); HandlerHelper.SendToClient(context, store, config); } else { throw new RdfOutputException("Unexpected Result Object of Type '" + result.GetType().ToString() + "' returned - unable to write Objects of this Type to the HTTP Response"); } }
/// <summary> /// Writes the SPARQL Result Set to a String and returns the Output in your chosen format /// </summary> /// <param name="results">SPARQL Result Set</param> /// <param name="writer">Writer to use to generate the SPARQL Results output</param> /// <returns></returns> public static String Write(SparqlResultSet results, ISparqlResultsWriter writer) { System.IO.StringWriter sw = new System.IO.StringWriter(); writer.Save(results, sw); return sw.ToString(); }
public BrightstarSparqlResultsType ExecuteSparql(SparqlQuery query, IStore store, TextWriter resultsWriter) { try { EnsureValidResultFormat(query); var dataset = MakeDataset(store); if (_defaultGraphUris != null) { dataset.SetDefaultGraph(_defaultGraphUris); } var queryProcessor = new BrightstarQueryProcessor(store, dataset); var queryResult = queryProcessor.ProcessQuery(query); if (queryResult is SparqlResultSet) { var sparqlResultSet = (SparqlResultSet)queryResult; ISparqlResultsWriter sparqlResultsWriter = null; if (_sparqlResultsFormat != null) { sparqlResultsWriter = MimeTypesHelper.GetSparqlWriter(new string[] { _sparqlResultsFormat.ToString() }); } if (sparqlResultsWriter == null) { throw new NoAcceptableFormatException(typeof(SparqlResultsFormat), "No acceptable format provided for writing a SPARQL result set."); } sparqlResultsWriter.Save(sparqlResultSet, resultsWriter); switch (sparqlResultSet.ResultsType) { case SparqlResultsType.Boolean: return(BrightstarSparqlResultsType.Boolean); case SparqlResultsType.VariableBindings: return(BrightstarSparqlResultsType.VariableBindings); default: throw new BrightstarInternalException("Unrecognized SPARQL result type"); } } if (queryResult is IGraph) { var g = (IGraph)queryResult; var rdfWriter = _rdfFormat == null ? null : MimeTypesHelper.GetWriter(new string[] { _rdfFormat.ToString() }); if (rdfWriter == null) { throw new NoAcceptableFormatException(typeof(RdfFormat), "No acceptable format provided for writing an RDF graph result."); } rdfWriter.Save(g, resultsWriter); try { resultsWriter.Flush(); } catch (ObjectDisposedException) { // resultWriter is already closed } return(BrightstarSparqlResultsType.Graph); } throw new BrightstarInternalException( String.Format("Unexpected return type from QueryProcessor.ProcessQuery: {0}", queryResult.GetType())); } catch (Exception ex) { Logging.LogError(BrightstarEventId.SparqlExecutionError, "Error Executing query {0}. Cause: {1}", query.ToString(), ex); throw; } }
// See https://github.com/dotnetrdf/dotnetrdf/blob/5b7fc480346c90eb9b164fb4f8ee09f378442d52/Libraries/dotNetRDF.Web/HandlerHelper.cs#L157 /// <summary> /// Helper function which returns the Results (Graph/Triple Store/SPARQL Results) back to the Client in one of their accepted formats /// </summary> /// <param name="context">Context of the HTTP Request</param> /// <param name="result">Results of the Sparql Query</param> /// <param name="config">Handler Configuration</param> public static void SendToClient(HttpContext context, SparqlQueryResult result) { MimeTypeDefinition definition = null; const string TEXT_PLAIN = "text/plain"; var acceptTypes = context.Request.Headers[HeaderNames.Accept]; // Return the Results if (result.SparqlResultSet != null) { ISparqlResultsWriter sparqlWriter = null; // Try and get a MIME Type Definition using the HTTP Requests Accept Header if (acceptTypes.Count > 0) { definition = MimeTypesHelper.GetDefinitions((IEnumerable <string>)acceptTypes).FirstOrDefault(d => d.CanWriteSparqlResults); } // Try and get the registered Definition for SPARQL Results XML if (definition == null) { definition = MimeTypesHelper.GetDefinitions(MimeTypesHelper.SparqlResultsXml[0]).FirstOrDefault(); } // If Definition is still null create a temporary definition if (definition == null) { definition = new MimeTypeDefinition("SPARQL Results XML", MimeTypesHelper.SparqlResultsXml, Enumerable.Empty <String>()); definition.SparqlResultsWriterType = typeof(VDS.RDF.Writing.SparqlXmlWriter); } // Set up the Writer appropriately sparqlWriter = definition.GetSparqlResultsWriter(); context.Response.ContentType = definition.CanonicalMimeType; // HandlerHelper.ApplyWriterOptions(sparqlWriter, config); // Send Result Set to Client context.Response.Headers[HeaderNames.ContentEncoding] = definition.Encoding.WebName; sparqlWriter.Save(result.SparqlResultSet, new StreamWriter(context.Response.Body, definition.Encoding)); } else if (result.Graph != null) { IRdfWriter rdfWriter = null; var ctype = TEXT_PLAIN; // Try and get a MIME Type Definition using the HTTP Requests Accept Header if (acceptTypes.Count > 0) { definition = MimeTypesHelper.GetDefinitions((IEnumerable <string>)acceptTypes).FirstOrDefault(d => d.CanWriteRdf); } if (definition == null) { // If no appropriate definition then use the GetWriter method instead rdfWriter = MimeTypesHelper.GetWriter((IEnumerable <string>)acceptTypes, out ctype); } else { rdfWriter = definition.GetRdfWriter(); } // Setup the writer if (definition != null) { ctype = definition.CanonicalMimeType; } context.Response.ContentType = ctype; //HandlerHelper.ApplyWriterOptions(rdfWriter, config); // Clear any existing Response //context.Response.Clear(); // Send Graph to Client if (definition != null) { context.Response.Headers[HeaderNames.ContentEncoding] = definition.Encoding.WebName; rdfWriter.Save(result.Graph, new StreamWriter(context.Response.Body, definition.Encoding)); } else { rdfWriter.Save(result.Graph, new StreamWriter(context.Response.Body)); } } else { Debug.Assert(result == SparqlQueryResult.Null); } }
static void DoQuery(Dictionary <String, String> arguments) { SparqlRemoteEndpoint endpoint; bool verbose = arguments.ContainsKey("verbose") || arguments.ContainsKey("v"); if (verbose) { Options.HttpDebugging = true; } //First get the Server to which we are going to connect try { if (arguments.ContainsKey("server") && !arguments["server"].Equals(String.Empty)) { endpoint = new SparqlRemoteEndpoint(new Uri(arguments["server"])); } else if (arguments.ContainsKey("service") && !arguments["service"].Equals(String.Empty)) { endpoint = new SparqlRemoteEndpoint(new Uri(arguments["service"])); } else { Console.Error.WriteLine("soh: Error: Required --server/--service argument not present"); Environment.Exit(-1); return; } } catch (UriFormatException uriEx) { Console.Error.WriteLine("soh: Error: Malformed SPARQL Endpoint URI"); Console.Error.WriteLine(uriEx.Message); Environment.Exit(-1); return; } if (verbose) { Console.Error.WriteLine("soh: SPARQL Endpoint for URI " + endpoint.Uri + " created OK"); } //Then decide where to get the query to execute from SparqlQueryParser parser = new SparqlQueryParser(); SparqlQuery query; try { if (arguments.ContainsKey("query") && !arguments["query"].Equals(String.Empty)) { query = parser.ParseFromFile(arguments["query"]); } else if (arguments.ContainsKey("file") && !arguments["file"].Equals(String.Empty)) { query = parser.ParseFromFile(arguments["file"]); } else if (arguments.ContainsKey("$1") && !arguments["$1"].Equals(String.Empty)) { query = parser.ParseFromString(arguments["$1"]); } else { Console.Error.WriteLine("soh: Error: Required SPARQL Query not found - may be specified as --file/--query FILE or as final argument"); Environment.Exit(-1); return; } } catch (Exception ex) { Console.Error.WriteLine("soh: Error: Error Parsing SPARQL Query"); Console.Error.WriteLine(ex.Message); Environment.Exit(-1); return; } if (verbose) { Console.Error.WriteLine("soh: Parsed Query OK"); Console.Error.WriteLine("soh: dotNetRDF's interpretation of the Query:"); SparqlFormatter formatter = new SparqlFormatter(); Console.Error.WriteLine(formatter.Format(query)); Console.Error.WriteLine("soh: Submitting Query"); } try { using (HttpWebResponse response = endpoint.QueryRaw(query.ToString())) { MimeTypeDefinition definition = MimeTypesHelper.GetDefinitions(response.ContentType).FirstOrDefault(); Encoding enc; if (definition != null) { enc = definition.Encoding; } else if (!response.ContentEncoding.Equals(String.Empty)) { enc = Encoding.GetEncoding(response.ContentEncoding); } else if (response.ContentType.Contains("charset=")) { enc = Encoding.GetEncoding(response.ContentType.Substring(response.ContentType.IndexOf('=') + 1)); } else { enc = Console.OutputEncoding; } if (verbose) { Console.Error.WriteLine("soh: Got Response from SPARQL Endpoint OK"); Console.Error.WriteLine("soh: Content-Type: " + response.ContentType); Console.Error.WriteLine("soh: Content-Encoding: " + enc.WebName); } String requestedType = arguments.ContainsKey("accept") ? arguments["accept"] : null; if (requestedType == null || response.ContentType.StartsWith(requestedType, StringComparison.OrdinalIgnoreCase)) { //If no --accept (OR matches servers content type) then just return whatever the server has given us using (StreamReader reader = new StreamReader(response.GetResponseStream())) { using (StreamWriter writer = new StreamWriter(Console.OpenStandardOutput(), enc)) { while (!reader.EndOfStream) { writer.WriteLine(reader.ReadLine()); } writer.Close(); } reader.Close(); } } else { if (verbose) { Console.Error.WriteLine("soh: Warning: Retrieved Content Type '" + response.ContentType + "' does not match your desired Content Type '" + requestedType + "' so dotNetRDF will not attempt to transcode the response into your desired format"); } //Requested Type Doesn't match servers returned type so parse then serialize MimeTypeDefinition outputDefinition; try { ISparqlResultsReader sparqlParser = MimeTypesHelper.GetSparqlParser(response.ContentType); SparqlResultSet results = new SparqlResultSet(); using (StreamReader reader = new StreamReader(response.GetResponseStream())) { sparqlParser.Load(results, reader); reader.Close(); } outputDefinition = MimeTypesHelper.GetDefinitions(requestedType).FirstOrDefault(d => d.CanWriteSparqlResults || d.CanWriteRdf); if (outputDefinition == null) { throw new RdfWriterSelectionException("No MIME Type Definition for the MIME Type '" + requestedType + "' was found that can write SPARQL Results/RDF"); } ISparqlResultsWriter writer = outputDefinition.GetSparqlResultsWriter(); Console.OutputEncoding = outputDefinition.Encoding; writer.Save(results, new StreamWriter(Console.OpenStandardOutput(), outputDefinition.Encoding)); } catch (RdfParserSelectionException) { try { IRdfReader rdfParser = MimeTypesHelper.GetParser(response.ContentType); Graph g = new Graph(); using (StreamReader reader = new StreamReader(response.GetResponseStream())) { rdfParser.Load(g, reader); reader.Close(); } outputDefinition = MimeTypesHelper.GetDefinitions(requestedType).FirstOrDefault(d => d.CanWriteRdf); if (outputDefinition == null) { throw new RdfWriterSelectionException("No MIME Type Definition for the MIME Type '" + requestedType + "' was found that can write RDF"); } IRdfWriter writer = outputDefinition.GetRdfWriter(); Console.OutputEncoding = outputDefinition.Encoding; writer.Save(g, new StreamWriter(Console.OpenStandardOutput(), outputDefinition.Encoding)); } catch (Exception ex) { //For any other exception show a warning Console.Error.WriteLine("soh: Warning: You wanted results in the format '" + requestedType + "' but the server returned '" + response.ContentType + "' and dotNetRDF was unable to translate the response into your desired format."); Console.Error.WriteLine(ex.Message); } } catch (Exception ex) { //For any other exception show a warning Console.Error.WriteLine("soh: Warning: You wanted results in the format '" + requestedType + "' but the server returned '" + response.ContentType + "' and dotNetRDF was unable to translate the response into your desired format."); Console.Error.WriteLine(ex.Message); } } response.Close(); } if (verbose) { Console.Error.WriteLine("soh: Query Completed OK"); } } catch (Exception ex) { Console.Error.WriteLine("soh: Error: Error while making the SPARQL Query"); Console.Error.WriteLine(ex.Message); Environment.Exit(-1); return; } }