/// <summary> /// Loads the contents of the given File into a Graph using the given RDF Parser /// </summary> /// <param name="g">Graph to load into</param> /// <param name="filename">File to load from</param> /// <param name="parser">Parser to use</param> public static void Load(IGraph g, String filename, IRdfReader parser) { if (g == null) throw new RdfParseException("Cannot read RDF into a null Graph"); if (filename == null) throw new RdfParseException("Cannot read RDF from a null File"); if (!File.Exists(filename)) { #if SILVERLIGHT throw new FileNotFoundException("Cannot read RDF from the File '" + filename + "' since it doesn't exist"); #else throw new FileNotFoundException("Cannot read RDF from a File that doesn't exist", filename); #endif } //Assign a File Uri to the Graph if the Graph is Empty //It's possible that when we parse in the RDF this may be changed but it ensures the Graph //has a Base Uri even if the RDF doesn't specify one //Ensure that the Uri is an absolute file Uri if (g.IsEmpty && g.BaseUri == null) { RaiseWarning("Assigned a file: URI as the Base URI for the input Graph"); if (Path.IsPathRooted(filename)) { g.BaseUri = new Uri("file:///" + filename); } else { g.BaseUri = new Uri("file:///" + Path.GetFullPath(filename)); } } FileLoader.Load(new GraphHandler(g), filename, parser); }
public BrightstarRdfParserAdapter(IRdfReader rdfReader, bool decompressStream) { _rdfReader = rdfReader; _decompressStream = decompressStream; #if SILVERLIGHT if (_decompressStream == true) throw new BrightstarInternalException("Compression not supported on Mobile"); #endif }
private void btnOpen_Click(object sender, RoutedEventArgs e) { try { Uri u = new Uri(this.txtUri.Text); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(u.ToString()); request.Accept = MimeTypesHelper.HttpAcceptHeader + ",*.*"; String data; using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { data = new StreamReader(response.GetResponseStream()).ReadToEnd(); try { this._parser = MimeTypesHelper.GetParser(response.ContentType); if (this._parser is NTriplesParser) { if (!response.ContentType.Equals("text/plain")) { this._parser = null; } } } catch (RdfParserSelectionException) { //Ignore here as we'll try and set the parser in another way next } response.Close(); } this._data = data; if (this._parser == null) { this._parser = StringParser.GetParser(data); if (this._parser is NTriplesParser) this._parser = null; } this._u = u; this.DialogResult = true; this.Close(); } catch (UriFormatException) { MessageBox.Show("You have failed to enter a valid URI", "Invalid URI"); } catch (WebException webEx) { MessageBox.Show("A HTTP error occurred opening the URI: " + webEx.Message, "Open URI Failed"); } catch (Exception ex) { MessageBox.Show("An error occurred while opening the URI: " + ex.Message, "Open URI Failed"); } }
/// <summary>Loads data from string with optional automated graph generation.</summary> /// <param name="store">Target store to be loaded with data.</param> /// <param name="data">String with data.</param> /// <param name="parser">RDF reader.</param> /// <param name="metaGraphUri">When provided, store will have automatically created graphs for all resources that are mentioned in the meta graph provided.</param> public static void LoadFromString(this ITripleStore store, string data, IRdfReader parser, Uri metaGraphUri) { ITripleStore targetStore = (metaGraphUri != null ? new TripleStore() : store); IGraph graph = new Graph(); targetStore.Add(graph); graph.LoadFromString(data, parser); if (metaGraphUri != null) { store.ExpandGraphs((TripleStore)targetStore, metaGraphUri); } }
private void ParsingUsingCountHandler(String tempFile, IRdfReader parser) { Graph g = new Graph(); EmbeddedResourceLoader.Load(g, "VDS.RDF.Configuration.configuration.ttl"); g.SaveToFile(tempFile); CountHandler handler = new CountHandler(); parser.Load(handler, tempFile); Console.WriteLine("Counted " + handler.Count + " Triples"); Assert.AreEqual(g.Triples.Count, handler.Count, "Counts should have been equal"); }
/// <summary>Creates a new instance of the file triple store.</summary> /// <param name="filePath">Path of the file to read/write.</param> /// <param name="rdfReader">RDF reader used to read the file.</param> /// <param name="rdfWriter">RDF writer to write the file.</param> public FileTripleStore(string filePath, IRdfReader rdfReader, IRdfWriter rdfWriter) { if (!File.Exists(_filePath = filePath)) { File.Create(filePath).Close(); } _rdfReader = rdfReader; _rdfWriter = rdfWriter; Read(); }
public void ParsingUsingPagingHandler2(String tempFile, IRdfReader parser) { Graph g = new Graph(); EmbeddedResourceLoader.Load(g, "VDS.RDF.Configuration.configuration.ttl"); g.SaveToFile(tempFile); Graph h = new Graph(); PagingHandler handler = new PagingHandler(new GraphHandler(h), 0); parser.Load(handler, tempFile); Assert.IsTrue(h.IsEmpty, "Graph should be empty"); }
private void TestEmptyParsing(IRdfReader reader) { if (!File.Exists("empty.test")) { FileStream temp = File.Create("empty.test"); temp.Close(); } Graph g = new Graph(); reader.Load(g, "empty.test"); Assert.IsTrue(g.IsEmpty, "Graph should be empty"); }
public void ParsingUsingPagingHandler3(String tempFile, IRdfReader parser) { Graph g = new Graph(); EmbeddedResourceLoader.Load(g, "VDS.RDF.Configuration.configuration.ttl"); g.SaveToFile(tempFile); Graph h = new Graph(); PagingHandler handler = new PagingHandler(new GraphHandler(h), -1, 100); parser.Load(handler, tempFile); Assert.IsFalse(h.IsEmpty, "Graph should not be empty"); Assert.AreEqual(g.Triples.Count - 100, h.Triples.Count, "Should have 100 less triples than original graph as first 100 triples are skipped"); }
/// <summary> /// Loads a Graph from an Embedded Resource /// </summary> /// <param name="handler">RDF Handler to use</param> /// <param name="resource">Assembly Qualified Name of the Resource to load</param> /// <param name="parser">Parser to use (leave null for auto-selection)</param> public static void Load(IRdfHandler handler, String resource, IRdfReader parser) { if (resource == null) throw new RdfParseException("Cannot read RDF from a null Resource"); if (handler == null) throw new RdfParseException("Cannot read RDF using a null Handler"); try { String resourceName = resource; if (resource.Contains(',')) { //Resource is an external assembly String assemblyName = resource.Substring(resource.IndexOf(',') + 1).TrimStart(); resourceName = resourceName.Substring(0, resource.IndexOf(',')).TrimEnd(); //Try to load this assembly Assembly asm = assemblyName.Equals(_currAsmName) ? Assembly.GetExecutingAssembly() : Assembly.Load(assemblyName); if (asm != null) { //Resource is in the loaded assembly EmbeddedResourceLoader.LoadGraphInternal(handler, asm, resourceName, parser); } else { throw new RdfParseException("The Embedded Resource '" + resourceName + "' cannot be loaded as the required assembly '" + assemblyName + "' could not be loaded"); } } else { //Resource is in dotNetRDF EmbeddedResourceLoader.LoadGraphInternal(handler, Assembly.GetExecutingAssembly(), resourceName, parser); } } catch (RdfParseException) { throw; } catch (Exception ex) { throw new RdfParseException("Unable to load the Embedded Resource '" + resource + "' as an unexpected error occurred", ex); } }
/// <summary> /// Attempts to load a RDF Graph from a URI asynchronously /// </summary> /// <param name="g">Graph to assert triple in</param> /// <param name="u">URI to load from</param> /// <param name="parser">Parser to use</param> /// <param name="callback">Callback to invoke when the operation completes</param> /// <param name="state">State to pass to the callback</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(IGraph g, Uri u, IRdfReader parser, GraphCallback callback, Object state) { if (g == null) throw new RdfParseException("Cannot read RDF into a null Graph"); if (u == null) throw new RdfParseException("Cannot read RDF from a null URI"); #if SILVERLIGHT if (u.IsFile()) #else if (u.IsFile) #endif { //Invoke FileLoader instead UriLoader.RaiseWarning("This is a file: URI so invoking the FileLoader instead"); if (Path.DirectorySeparatorChar == '/') { FileLoader.Load(g, u.ToString().Substring(7), parser); } else { FileLoader.Load(g, u.ToString().Substring(8), parser); } //FileLoader.Load() will run synchronously so once this completes we can invoke the callback callback(g, state); return; } if (u.Scheme.Equals("data")) { //Invoke DataUriLoader instead RaiseWarning("This is a data: URI so invoking the DataUriLoader instead"); DataUriLoader.Load(g, u); //After DataUriLoader.Load() has run (which happens synchronously) can invoke the callback callback(g, state); return; } //Set Base URI if necessary if (g.BaseUri == null && g.IsEmpty) g.BaseUri = u; UriLoader.Load(new GraphHandler(g), u, parser, (_,s) => callback(g, s), state); }
public void ParsingUsingPagingHandler(String tempFile, IRdfReader parser) { Graph g = new Graph(); EmbeddedResourceLoader.Load(g, "VDS.RDF.Configuration.configuration.ttl"); g.SaveToFile(tempFile); Graph h = new Graph(); PagingHandler handler = new PagingHandler(new GraphHandler(h), 25); parser.Load(handler, tempFile); h.Retract(h.Triples.Where(t => !t.IsGroundTriple)); NTriplesFormatter formatter = new NTriplesFormatter(); foreach (Triple t in h.Triples) { Console.WriteLine(t.ToString(formatter)); } Console.WriteLine(); Assert.IsFalse(h.IsEmpty, "Graph should not be empty"); Assert.IsTrue(h.Triples.Count <= 25, "Graphs should have <= 25 Triples"); Graph i = new Graph(); handler = new PagingHandler(new GraphHandler(i), 25, 25); parser.Load(handler, tempFile); i.Retract(i.Triples.Where(t => !t.IsGroundTriple)); foreach (Triple t in i.Triples) { Console.WriteLine(t.ToString(formatter)); } Console.WriteLine(); Assert.IsFalse(i.IsEmpty, "Graph should not be empty"); Assert.IsTrue(i.Triples.Count <= 25, "Graphs should have <= 25 Triples"); GraphDiffReport report = h.Difference(i); Assert.IsFalse(report.AreEqual, "Graphs should not be equal"); Assert.AreEqual(i.Triples.Count, report.AddedTriples.Count(), "Should be " + i.Triples.Count + " added Triples"); Assert.AreEqual(h.Triples.Count, report.RemovedTriples.Count(), "Should be " + h.Triples.Count + " removed Triples"); }
/// <summary> /// Parses a raw RDF String using the given <see cref="IRdfReader">IRdfReader</see> /// </summary> /// <param name="g">Graph to load into</param> /// <param name="data">Raw RDF String</param> /// <param name="reader">Parser to use to read the data</param> /// <remarks>Use this when you have a raw RDF string and you know the syntax the RDF is in</remarks> public static void Parse(IGraph g, String data, IRdfReader reader) { if (g == null) throw new RdfParseException("Cannot read RDF into a null Graph"); if (data == null) return; if (reader == null) { //If no parser supplied then should auto-detect syntax Parse(g, data); } else { try { reader.Load(g, new StringReader(data)); } catch { throw; } } }
public void ParsingUsingGraphHandlerExplicitTest(String tempFile, IRdfReader parser, bool nsCheck) { Graph g = new Graph(); EmbeddedResourceLoader.Load(g, "VDS.RDF.Configuration.configuration.ttl"); g.SaveToFile(tempFile); Graph h = new Graph(); GraphHandler handler = new GraphHandler(h); parser.Load(handler, tempFile); NTriplesFormatter formatter = new NTriplesFormatter(); foreach (Triple t in h.Triples) { Console.WriteLine(t.ToString(formatter)); } Assert.IsFalse(g.IsEmpty, "Graph should not be empty"); Assert.IsTrue(g.NamespaceMap.HasNamespace("dnr"), "Graph should have the dnr: Namespace"); Assert.IsFalse(h.IsEmpty, "Graph should not be empty"); if (nsCheck) Assert.IsTrue(h.NamespaceMap.HasNamespace("dnr"), "Graph should have the dnr: Namespace"); Assert.AreEqual(g, h, "Graphs should be equal"); }
public SyntaxDefinition(String name, String definitionFile, String[] fileExtensions, IRdfReader defaultParser, IRdfWriter defaultWriter, ISyntaxValidator validator) : this(name, definitionFile, fileExtensions, defaultParser, defaultWriter) { this._validator = validator; }
/// <summary> /// Creates a Custom HTTP Accept Header containing only the Accept Types supported by a specific parser /// </summary> /// <param name="parser">RDF Parser</param> /// <returns></returns> public static String CustomHttpAcceptHeader(IRdfReader parser) { if (!_init) Init(); Type requiredType = parser.GetType(); foreach (MimeTypeDefinition definition in MimeTypesHelper.Definitions) { if (requiredType.Equals(definition.RdfParserType)) { return String.Join(",", definition.MimeTypes.ToArray()); } } return MimeTypesHelper.HttpAcceptHeader; }
/// <summary> /// Loads the contents of the given File using a RDF Handler using the given RDF Parser /// </summary> /// <param name="handler">RDF Handler to use</param> /// <param name="filename">File to load from</param> /// <param name="parser">Parser to use</param> public static void Load(IRdfHandler handler, String filename, IRdfReader parser) { if (handler == null) throw new RdfParseException("Cannot read RDF using a null RDF Handler"); if (filename == null) throw new RdfParseException("Cannot read RDF from a null File"); //Try to get a Parser from the File Extension if one isn't explicitly specified if (parser == null) { try { parser = MimeTypesHelper.GetParser(MimeTypesHelper.GetMimeTypes(MimeTypesHelper.GetTrueFileExtension(filename))); RaiseWarning("Selected Parser " + parser.ToString() + " based on file extension, if this is incorrect consider specifying the parser explicitly"); } catch (RdfParserSelectionException) { //If error then we couldn't determine MIME Type from the File Extension RaiseWarning("Unable to select a parser by determining MIME Type from the File Extension"); } } if (parser == null) { //Unable to determine format from File Extension //Read file in locally and use the StringParser to select a parser RaiseWarning("Attempting to select parser based on analysis of the data file, this requires loading the file into memory"); StreamReader reader = new StreamReader(filename); String data = reader.ReadToEnd(); reader.Close(); parser = StringParser.GetParser(data); RaiseWarning("Used the StringParser to guess the parser to use - it guessed " + parser.GetType().Name); parser.Warning += RaiseWarning; parser.Load(handler, new StringReader(data)); } else { //Parser was selected based on File Extension or one was explicitly specified parser.Warning += RaiseWarning; parser.Load(handler, filename); } }
private bool SetParser(String format) { switch (format) { case "rdfxml": this._parser = new RdfXmlParser(); break; case "ntriples": this._parser = new NTriplesParser(); break; case "turtle": this._parser = new TurtleParser(); break; case "n3": this._parser = new Notation3Parser(); break; case "json": this._parser = new RdfJsonParser(); break; case "html": case "rdfa": this._parser = new RdfAParser(); break; case "guess": this._guess = true; break; default: Console.Error.WriteLine("rdfConvert: The value '" + format + "' is not a valid format parameter"); return false; } return true; }
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; } }
/// <summary> /// Creates a new Strict RDF Syntax Validator /// </summary> /// <param name="parser">Parser</param> public RdfStrictSyntaxValidator(IRdfReader parser) : base(parser) { parser.Warning += this.OnWarning; }
/// <summary> /// Creates a new GZipped input parser /// </summary> /// <param name="parser">Underlying parser</param> public BaseGZipParser(IRdfReader parser) { if (parser == null) throw new ArgumentNullException("parser"); this._parser = parser; this._parser.Warning += this.RaiseWarning; }
/// <summary> /// Executes a SPARQL Query on the Fuseki store 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; //Create the Request String queryUri = this._queryUri; if (sparqlQuery.Length < 2048) { queryUri += "?query=" + Uri.EscapeDataString(sparqlQuery); request = (HttpWebRequest)WebRequest.Create(queryUri); request.Method = "GET"; request.Accept = MimeTypesHelper.HttpRdfOrSparqlAcceptHeader; request = base.ApplyRequestOptions(request); } else { request = (HttpWebRequest)WebRequest.Create(queryUri); request.Method = "POST"; request.Accept = MimeTypesHelper.HttpRdfOrSparqlAcceptHeader; request = base.ApplyRequestOptions(request); //Build the Post Data and add to the Request Body request.ContentType = MimeTypesHelper.Utf8WWWFormURLEncoded; StringBuilder postData = new StringBuilder(); postData.Append("query="); postData.Append(HttpUtility.UrlEncode(sparqlQuery)); using (StreamWriter writer = new StreamWriter(request.GetRequestStream(), new UTF8Encoding(Options.UseBomForUtf8))) { writer.Write(postData); writer.Close(); } } Tools.HttpDebugRequest(request); //Get the Response and process based on the Content Type using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { Tools.HttpDebugResponse(response); StreamReader data = new StreamReader(response.GetResponseStream()); String ctype = response.ContentType; try { //Is the Content Type referring to a RDF format? IRdfReader rdfreader = MimeTypesHelper.GetParser(ctype); rdfreader.Load(rdfHandler, data); response.Close(); } catch (RdfParserSelectionException) { //If we get a Parser selection exception then the Content Type isn't valid for a RDF Graph //Is the Content Type referring to a Sparql Result Set format? ISparqlResultsReader resreader = MimeTypesHelper.GetSparqlParser(ctype, true); resreader.Load(resultsHandler, data); response.Close(); } } } catch (WebException webEx) { throw StorageHelper.HandleHttpQueryError(webEx); } }
public void WritingUriEscaping() { Graph g = new Graph(); g.BaseUri = new Uri("http://example.org/space in/base"); g.NamespaceMap.AddNamespace("ex", new Uri("http://example.org/space in/namespace")); IUriNode subj = g.CreateUriNode(new Uri("http://example.org/subject")); IUriNode pred = g.CreateUriNode(new Uri("http://example.org/predicate")); IUriNode obj = g.CreateUriNode(new Uri("http://example.org/with%20uri%20escapes")); IUriNode obj2 = g.CreateUriNode(new Uri("http://example.org/needs escapes")); g.Assert(subj, pred, obj); g.Assert(subj, pred, obj2); NTriplesFormatter formatter = new NTriplesFormatter(); List <IRdfWriter> writers = new List <IRdfWriter>() { new CompressingTurtleWriter(TurtleSyntax.Original), new CompressingTurtleWriter(TurtleSyntax.W3C), new PrettyRdfXmlWriter(), new HtmlWriter(), new Notation3Writer(), new NTriplesWriter(NTriplesSyntax.Original), new NTriplesWriter(NTriplesSyntax.Rdf11), new PrettyRdfXmlWriter(), new RdfJsonWriter(), new RdfXmlWriter(), new TurtleWriter() }; List <IRdfReader> parsers = new List <IRdfReader>() { new TurtleParser(TurtleSyntax.Original), new TurtleParser(TurtleSyntax.W3C), new RdfXmlParser(), new RdfAParser(), new Notation3Parser(), new NTriplesParser(NTriplesSyntax.Original), new NTriplesParser(NTriplesSyntax.Rdf11), new RdfXmlParser(), new RdfJsonParser(), new RdfXmlParser(), new TurtleParser() }; Assert.Equal(parsers.Count, writers.Count); Console.WriteLine("Input Data:"); foreach (Triple t in g.Triples) { Console.WriteLine(t.ToString(formatter)); } Console.WriteLine(); for (int i = 0; i < writers.Count; i++) { IRdfWriter writer = writers[i]; Console.WriteLine("Using " + writer.ToString()); System.IO.StringWriter strWriter = new System.IO.StringWriter(); writer.Save(g, strWriter); Console.WriteLine(strWriter.ToString()); Console.WriteLine(); Console.Out.Flush(); IRdfReader parser = parsers[i]; Graph h = new Graph(true); parser.Load(h, new StringReader(strWriter.ToString())); Console.WriteLine("Parsed Data:"); foreach (Triple t in h.Triples) { Console.WriteLine(t.ToString(formatter)); } Console.WriteLine(); Assert.Equal(g, h); if (h.NamespaceMap.HasNamespace("ex")) { Assert.Equal(g.NamespaceMap.GetNamespaceUri("ex"), h.NamespaceMap.GetNamespaceUri("ex")); } if (h.BaseUri != null) { Assert.Equal(g.BaseUri, h.BaseUri); } } }
/// <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); } }
/// <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); } }
/// <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 (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); } 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 !NETCORE httpRequest.Timeout = Options.UriLoaderTimeout; #endif if (_userAgent != null && !_userAgent.Equals(String.Empty)) { #if NETCORE httpRequest.Headers[HttpRequestHeader.UserAgent] = _userAgent; #else httpRequest.UserAgent = _userAgent; #endif } // HTTP Debugging Tools.HttpDebugRequest(httpRequest); using (HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse()) { Tools.HttpDebugResponse(httpResponse); // Get a Parser and Load the RDF if (parser == null) { try { parser = MimeTypesHelper.GetStoreParser(httpResponse.ContentType); parser.Warning += RaiseStoreWarning; parser.Load(handler, new StreamReader(httpResponse.GetResponseStream())); } catch (RdfParserSelectionException) { RaiseStoreWarning("Unable to select a RDF Dataset parser based on Content-Type: " + httpResponse.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(httpResponse.ContentType); rdfParser.Load(handler, new StreamReader(httpResponse.GetResponseStream())); } catch (RdfParserSelectionException) { // Finally fall back to assuming a dataset and trying format guessing String data = new StreamReader(httpResponse.GetResponseStream()).ReadToEnd(); parser = StringParser.GetDatasetParser(data); parser.Warning += RaiseStoreWarning; parser.Load(handler, new StringReader(data)); } } } else { parser.Warning += RaiseStoreWarning; parser.Load(handler, new StreamReader(httpResponse.GetResponseStream())); } } } 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); } catch (WebException webEx) { if (webEx.Response != null) { Tools.HttpDebugResponse((HttpWebResponse)webEx.Response); } // Some sort of HTTP Error occurred throw new WebException("A HTTP Error occurred resolving the URI '" + u.AbsoluteUri + "', see innner exception for details", webEx); } }
/// <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 (u.IsFile) { // 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); // 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); Load(handler, u, parser); } return; } } } // 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 (Options.UriLoaderCaching) { if (!etag.Equals(String.Empty)) { httpRequest.Headers[HttpRequestHeader.IfNoneMatch] = etag; } } // Use HTTP GET httpRequest.Method = "GET"; #if !NETCORE httpRequest.Timeout = Options.UriLoaderTimeout; #endif if (_userAgent != null && !_userAgent.Equals(String.Empty)) { #if NETCORE httpRequest.Headers[HttpRequestHeader.UserAgent] = _userAgent; #else httpRequest.UserAgent = _userAgent; #endif } Tools.HttpDebugRequest(httpRequest); using (HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse()) { Tools.HttpDebugResponse(httpResponse); 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); 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); Load(handler, u, parser); return; } } // If we didn't get a Not-Modified response then we'll continue and parse the new response } } // 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; // To do caching we ask the cache to give us a handler and then we tie it to if (Options.UriLoaderCaching) { 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 { parser.Load(handler, new StreamReader(httpResponse.GetResponseStream())); } catch { // If we were trying to cache the response and something went wrong discard the cached copy if (Options.UriLoaderCaching) { _cache.RemoveETag(u); _cache.RemoveETag(Tools.StripUriFragment(httpResponse.ResponseUri)); _cache.RemoveLocalCopy(u); _cache.RemoveLocalCopy(Tools.StripUriFragment(httpResponse.ResponseUri)); } throw; } } } catch (UriFormatException uriEx) { // Uri Format Invalid throw new RdfParseException("Unable to load from the given URI '" + u.AbsoluteUri + "' since it's format was invalid", uriEx); } catch (WebException webEx) { if (webEx.Response != null) { Tools.HttpDebugResponse((HttpWebResponse)webEx.Response); } if (Options.UriLoaderCaching) { 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); 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); Load(handler, u, parser); return; } } } } // Some sort of HTTP Error occurred throw new WebException("A HTTP Error occurred resolving the URI '" + u.AbsoluteUri + "'", webEx); } }
public void Setup() { Reader = new T(); ScenarioSetup(); TheTest(); }
public void LoadFromFile(IGraph graph, string filename, IRdfReader parser) { FileLoader.Load(graph, filename, parser); }
public SyntaxDefinition(String name, IHighlightingDefinition definition, String[] fileExtensions, IRdfReader defaultParser) : this(name, definition, fileExtensions) { this._parser = defaultParser; }
public void LoadFromUri(IGraph graph, Uri uri, IRdfReader parser) { UriLoader.Load(graph, uri, parser); }
/// <summary> /// Makes a SPARQL Query against the Joseki store processing the results with the 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; //Create the Request Dictionary <String, String> queryParams = new Dictionary <string, string>(); if (sparqlQuery.Length < 2048) { queryParams.Add("query", sparqlQuery); request = this.CreateRequest(this._queryService, MimeTypesHelper.HttpRdfOrSparqlAcceptHeader, "GET", queryParams); } else { request = this.CreateRequest(this._queryService, MimeTypesHelper.HttpRdfOrSparqlAcceptHeader, "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(HttpUtility.UrlEncode(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, true); resreader.Load(resultsHandler, data); response.Close(); } catch (RdfParserSelectionException) { //If we get a Parse 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 DEBUG if (Options.HttpDebugging) { if (webEx.Response != null) { Tools.HttpDebugResponse((HttpWebResponse)webEx.Response); } } #endif throw new RdfQueryException("A HTTP error occurred while querying the Store", webEx); } }
/// <summary> /// Creates a new RDF Syntax Validator using the given Parser /// </summary> /// <param name="parser">Parser</param> public RdfSyntaxValidator(IRdfReader parser) { this._parser = parser; }
/// <summary> /// Attempt to auto-detect the syntax of the current document using the filename as a guide /// </summary> public void AutoDetectSyntax(String filename) { if (this._editor == null) { return; //Not yet ready } if (filename == null || System.IO.Path.GetExtension(filename).Equals(String.Empty)) { try { //First see if it's an RDF format IRdfReader parser = StringParser.GetParser(this._editor.Text); if (parser is NTriplesParser) { //NTriples is the fallback so if we get this check if it's actually SPARQL Results try { ISparqlResultsReader sparqlParser = StringParser.GetResultSetParser(this._editor.Text); } catch (RdfParserSelectionException) { //Not a valid SPARQL Results format - may be a SPARQL Query or a SPARQL Update? SparqlQueryParser queryParser = new SparqlQueryParser(SparqlQuerySyntax.Sparql_1_1); try { SparqlQuery q = queryParser.ParseFromString(this._editor.Text); this.SetHighlighter("SparqlQuery11"); } catch (RdfParseException) { //Not a valid SPARQL Query - valid SPARQL Update? SparqlUpdateParser updateParser = new SparqlUpdateParser(); try { SparqlUpdateCommandSet cmds = updateParser.ParseFromString(this._editor.Text); this.SetHighlighter("SparqlUpdate11"); } catch (RdfParseException) { //Was probably actually NTriples this.SetHighlighter(parser); } } } } else { //Got a non NTriples RDF parser so use that to set Highlighter this.SetHighlighter(parser); } } catch (RdfParserSelectionException) { this.SetNoHighlighting(); } return; } try { IHighlightingDefinition def = HighlightingManager.Instance.GetDefinitionByExtension(System.IO.Path.GetExtension(filename)); if (this._enableHighlighting) { this._editor.SyntaxHighlighting = def; } if (def != null) { this._currSyntax = def.Name; this.SetCurrentHighlighterChecked(def.Name); this.SetCurrentValidator(def.Name); this.SetCurrentAutoCompleter(def.Name); } else { this.SetNoHighlighting(); } } catch { this.SetNoHighlighting(); } }
private void CreateIOHandlers(string extension) { switch (extension) { case ".nq": _storeReader = new NQuadsParser(); _storeWriter = new NQuadsWriter(); break; case ".ttl": _rdfReader = new TurtleParser(); _rdfWriter = new CompressingTurtleWriter(); break; case ".trig": _storeReader = new TriGParser(); _storeWriter = new TriGWriter(); break; case ".xml": _rdfReader = new RdfXmlParser(); _rdfWriter = new RdfXmlWriter(); break; case ".n3": _rdfReader = new Notation3Parser(); _rdfWriter = new Notation3Writer(); break; case ".trix": _storeReader = new TriXParser(); _storeWriter = new TriXWriter(); break; case ".json": _rdfReader = new RdfJsonParser(); _rdfWriter = new RdfJsonWriter(); break; default: throw new ArgumentOutOfRangeException(System.String.Format("Provided file path does not allow to detect a type of the RDF serialization type.")); } }
/// <summary> /// Makes a Query asynchronously where the expected Result is an RDF Graph ie. CONSTRUCT and DESCRIBE Queries /// </summary> /// <param name="query">SPARQL Query String</param> /// <param name="handler">RDF Handler</param> /// <param name="callback">Callback to invoke when the query completes</param> /// <param name="state">State to pass to the callback</param> public void QueryWithResultGraph(IRdfHandler handler, String query, QueryCallback callback, Object state) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this.Uri); request.Method = "POST"; request.ContentType = MimeTypesHelper.Utf8WWWFormURLEncoded; request.Accept = this.ResultsAcceptHeader; this.ApplyRequestOptions(request); Tools.HttpDebugRequest(request); request.BeginGetRequestStream(result => { try { Stream stream = request.EndGetRequestStream(result); using (StreamWriter writer = new StreamWriter(stream, new UTF8Encoding(Options.UseBomForUtf8))) { writer.Write("query="); writer.Write(HttpUtility.UrlEncode(query)); foreach (String u in this.DefaultGraphs) { writer.Write("&default-graph-uri="); writer.Write(HttpUtility.UrlEncode(u)); } foreach (String u in this.NamedGraphs) { writer.Write("&named-graph-uri="); writer.Write(HttpUtility.UrlEncode(u)); } writer.Close(); } request.BeginGetResponse(innerResult => { try { HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(innerResult); Tools.HttpDebugResponse(response); IRdfReader parser = MimeTypesHelper.GetParser(response.ContentType); parser.Load(handler, new StreamReader(response.GetResponseStream())); callback(handler, null, state); } catch (SecurityException secEx) { callback(handler, null, new AsyncError(new RdfQueryException("Calling code does not have permission to access the specified remote endpoint, see inner exception for details", secEx), state)); } catch (WebException webEx) { if (webEx.Response != null) { Tools.HttpDebugResponse((HttpWebResponse)webEx.Response); } callback(handler, null, new AsyncError(new RdfQueryException("A HTTP error occurred while making an asynchronous query, see inner exception for details", webEx), state)); } catch (Exception ex) { callback(handler, null, new AsyncError(new RdfQueryException("Unexpected error while making an asynchronous query, see inner exception for details", ex), state)); } }, null); } catch (SecurityException secEx) { callback(handler, null, new AsyncError(new RdfQueryException("Calling code does not have permission to access the specified remote endpoint, see inner exception for details", secEx), state)); } catch (WebException webEx) { if (webEx.Response != null) { Tools.HttpDebugResponse((HttpWebResponse)webEx.Response); } callback(handler, null, new AsyncError(new RdfQueryException("A HTTP error occurred while making an asynchronous query, see inner exception for details", webEx), state)); } catch (Exception ex) { callback(handler, null, new AsyncError(new RdfQueryException("Unexpected error while making an asynchronous query, see inner exception for details", ex), state)); } }, null); }
/// <summary>Creates a new instance of the file triple store.</summary> /// <param name="fileStream">Stream to read/write.</param> /// <param name="rdfReader">RDF reader used to read the file.</param> /// <param name="rdfWriter">RDF writer to write the file.</param> public FileTripleStore(Stream fileStream, IRdfReader rdfReader, IRdfWriter rdfWriter) { _fileStream = fileStream; _rdfReader = rdfReader; _rdfWriter = rdfWriter; Read(); }
/// <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 #if !SILVERLIGHT UriLoader.Load(handler, u); #else UriLoader.Load(handler, u, (_, _1) => { }, null); #endif 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)); } }
public void RunConvert(String[] args) { //Single Help Argument means Show Help if (args.Length == 1 && (args[0].Equals("-h") || args[0].Equals("--help"))) { this.ShowUsage(); return; } //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; } if (this._input == null) { //If no Input then abort Console.Error.WriteLine("The required argument Input URI was not specified"); return; } if (this._writer == null && !this._count) { //If no Output Format was specified and the Count option was not specified we abort if (!this._quiet) Console.Error.WriteLine("rdfConvert: No Ouput Format specified - using default serializer NTriples"); this._writer = new NTriplesWriter(); } if (this._parser == null && !this._guess) { //Use guess mode if no specific input format or guess mode was specified if (!this._quiet) Console.Error.WriteLine("rdfConvert: No Input Format was specified and the guess option was not specified - using default parser RDF/XML"); this._parser = new RdfXmlParser(); } //Set Parser to Null if using guess mode if (this._guess) this._parser = null; if (this._parser != null && !this._ignoreWarnings) { //Output warnings to stderror if not ignoring warnings or using a mode where can't add a handler to the warning this._parser.Warning += this.ShowWarnings; if (this._writer != null) this._writer.Warning += this.ShowWarnings; } else if (!this._ignoreWarnings) { UriLoader.Warning += this.ShowWarnings; UriLoader.StoreWarning += this.ShowWarnings; FileLoader.Warning += this.ShowWarnings; FileLoader.StoreWarning += this.ShowWarnings; } //Try to parse in the Input try { if (!this._quiet) { if (this._parser != null) { Console.Error.WriteLine("rdfConvert: Parsing URI " + this._input + " with Parser " + this._parser.GetType().Name); } else { Console.Error.WriteLine("rdfConvert: Parsing URI " + this._input + " with guessing of Content Type"); } } if (this._input == "-") { //Read from Standard In if (this._guess) { StringParser.Parse(this._g, Console.In.ReadToEnd()); } else { this._parser.Load(this._g, new StreamReader(Console.OpenStandardInput())); } } else { try { Uri u = new Uri(this._input); if (u.IsAbsoluteUri) { //Valid Absolute URI UriLoader.Load(this._g, u, this._parser); } else { //If not an absolute URI then probably a filename FileLoader.Load(this._g, this._input, this._parser); } } catch (UriFormatException) { //If not a valid URI then probably a filename FileLoader.Load(this._g, this._input, this._parser); } } } catch (RdfParseException parseEx) { this.ShowErrors(parseEx, "Parse Error"); return; } catch (RdfException rdfEx) { this.ShowErrors(rdfEx, "RDF Error"); return; } catch (Exception ex) { this.ShowErrors(ex, "Error"); return; } if (!this._quiet) Console.Error.WriteLine("rdfConvert: Parsing returned " + this._g.Triples.Count + " Triples"); //Show only count if that was asked for if (this._count) { Console.WriteLine(this._g.Triples.Count); return; } //Show Namespaces if asked for if (this._showNamespaces) { foreach (String prefix in this._g.NamespaceMap.Prefixes) { Console.WriteLine(prefix + ": <" + this._g.NamespaceMap.GetNamespaceUri(prefix).ToString() + ">"); } } //Now do the serialization if (this._outputBase != null || this._useNullOutputBase) { //Set the Output Base URI if specified this._g.BaseUri = this._outputBase; } else if (this._useInputBase) { //Set the Output Base URI to the Input Base URI if specified //Have to reset this since parsing the Input may have changed the Base URI this._g.BaseUri = this._inputBase; } try { if (!this._quiet) Console.Error.WriteLine("rdfConvert: Serializing with serializer " + this._writer.GetType().Name); //Save the Graph to Standard Out this._writer.Save(this._g, Console.Out); } catch (RdfOutputException outEx) { this.ShowErrors(outEx, "Output Error"); return; } catch (RdfException rdfEx) { this.ShowErrors(rdfEx, "RDF Error"); return; } catch (Exception ex) { this.ShowErrors(ex, "Error"); return; } }
/// <summary> /// Internal Helper method which does the actual loading of the Graph from the Resource /// </summary> /// <param name="handler">RDF Handler to use</param> /// <param name="asm">Assembly to get the resource stream from</param> /// <param name="resource">Full name of the Resource (without the Assembly Name)</param> /// <param name="parser">Parser to use (if null then will be auto-selected)</param> private static void LoadGraphInternal(IRdfHandler handler, Assembly asm, String resource, IRdfReader parser) { //Resource is in the given assembly using (Stream s = asm.GetManifestResourceStream(resource)) { if (s == null) { //Resource did not exist in this assembly throw new RdfParseException("The Embedded Resource '" + resource + "' does not exist inside of " + asm.GetName().Name); } else { //Resource exists //Did we get a defined parser to use? if (parser != null) { parser.Load(handler, new StreamReader(s)); } else { //Need to select a Parser or use StringParser String ext = MimeTypesHelper.GetTrueResourceExtension(resource); MimeTypeDefinition def = MimeTypesHelper.GetDefinitionsByFileExtension(ext).FirstOrDefault(d => d.CanParseRdf); if (def != null) { //Resource has an appropriate file extension and we've found a candidate parser for it parser = def.GetRdfParser(); parser.Load(handler, new StreamReader(s)); } else { //Resource did not have a file extension or we didn't have a parser associated with the extension //Try using StringParser instead String data; using (StreamReader reader = new StreamReader(s)) { data = reader.ReadToEnd(); reader.Close(); } parser = StringParser.GetParser(data); parser.Load(handler, new StringReader(data)); } } } } }
/// <summary> /// Registers a parser as the default RDF Parser for all the given MIME types and updates relevant definitions to include the MIME types and file extensions /// </summary> /// <param name="parser">RDF Parser</param> /// <param name="mimeTypes">MIME Types</param> /// <param name="fileExtensions">File Extensions</param> public static void RegisterParser(IRdfReader parser, IEnumerable<String> mimeTypes, IEnumerable<String> fileExtensions) { if (!_init) Init(); if (!mimeTypes.Any()) throw new RdfException("Cannot register a parser without specifying at least 1 MIME Type"); //Get any existing defintions that are to be altered IEnumerable<MimeTypeDefinition> existing = GetDefinitions(mimeTypes); foreach (MimeTypeDefinition def in existing) { foreach (String type in mimeTypes) { def.AddMimeType(type); } foreach (String ext in fileExtensions) { def.AddFileExtension(ext); } def.RdfParserType = parser.GetType(); } //Create any new defintions IEnumerable<String> newTypes = mimeTypes.Where(t => !GetDefinitions(t).Any()); if (newTypes.Any()) { MimeTypeDefinition newDef = new MimeTypeDefinition(String.Empty, newTypes, fileExtensions); newDef.RdfParserType = parser.GetType(); AddDefinition(newDef); } }
/// <summary> /// Loads a Graph from an Embedded Resource /// </summary> /// <param name="g">Graph to load into</param> /// <param name="resource">Assembly Qualified Name of the Resource to load</param> /// <param name="parser">Parser to use (leave null for auto-selection)</param> public static void Load(IGraph g, String resource, IRdfReader parser) { if (g == null) throw new RdfParseException("Cannot read RDF into a null Graph"); EmbeddedResourceLoader.Load(new GraphHandler(g), resource, (IRdfReader)null); }
public SyntaxDefinition(String name, String definitionFile, String[] fileExtensions, IRdfReader defaultParser, IRdfWriter defaultWriter) : this(name, definitionFile, fileExtensions, defaultParser) { this._writer = defaultWriter; }
/// <summary> /// Creates a new SPARQL RDF Parser which will use the given RDF Parser. /// </summary> /// <param name="parser">RDF Parser.</param> public SparqlRdfParser(IRdfReader parser) { _parser = parser; }
/// <summary> /// Internal Helper method which does the actual loading of the Graph from the Resource /// </summary> /// <param name="handler">RDF Handler to use</param> /// <param name="asm">Assembly to get the resource stream from</param> /// <param name="resource">Full name of the Resource (without the Assembly Name)</param> /// <param name="parser">Parser to use (if null then will be auto-selected)</param> private static void LoadGraphInternal(IRdfHandler handler, Assembly asm, String resource, IRdfReader parser) { //Resource is in the given assembly using (Stream s = asm.GetManifestResourceStream(resource)) { if (s == null) { //Resource did not exist in this assembly throw new RdfParseException("The Embedded Resource '" + resource + "' does not exist inside of " + asm.GetName().Name); } else { //Resource exists //Did we get a defined parser to use? if (parser != null) { parser.Load(handler, new StreamReader(s)); } else { //Need to select a Parser or use StringParser String ext = resource.Substring(resource.LastIndexOf(".")); MimeTypeDefinition def = MimeTypesHelper.GetDefinitions(MimeTypesHelper.GetMimeTypes(ext)).FirstOrDefault(d => d.CanParseRdf); if (def != null) { //Resource has an appropriate file extension and we've found a candidate parser for it parser = def.GetRdfParser(); parser.Load(handler, new StreamReader(s)); } else { //Resource did not have a file extension or we didn't have a parser associated with the extension //Try using StringParser instead String data; using (StreamReader reader = new StreamReader(s)) { data = reader.ReadToEnd(); reader.Close(); } parser = StringParser.GetParser(data); parser.Load(handler, new StringReader(data)); } } } } }
/// <summary> /// Executes a SPARQL Query on the Fuseki store 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> /// <param name="callback">Callback</param> /// <param name="state">State to pass to the callback</param> /// <returns></returns> public void Query(IRdfHandler rdfHandler, ISparqlResultsHandler resultsHandler, String sparqlQuery, AsyncStorageCallback callback, Object state) { try { HttpWebRequest request; //Create the Request, always use POST for async for simplicity String queryUri = this._queryUri; request = (HttpWebRequest)WebRequest.Create(queryUri); request.Method = "POST"; request.Accept = MimeTypesHelper.HttpRdfOrSparqlAcceptHeader; request = base.GetProxiedRequest(request); //Build the Post Data and add to the Request Body request.ContentType = MimeTypesHelper.WWWFormURLEncoded; StringBuilder postData = new StringBuilder(); postData.Append("query="); postData.Append(HttpUtility.UrlEncode(sparqlQuery)); request.BeginGetRequestStream(r => { try { Stream stream = request.EndGetRequestStream(r); StreamWriter writer = new StreamWriter(stream); writer.Write(postData); writer.Close(); Tools.HttpDebugRequest(request); //Get the Response and process based on the Content Type request.BeginGetResponse(r2 => { try { HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(r2); Tools.HttpDebugResponse(response); 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, true); resreader.Load(resultsHandler, data); response.Close(); } catch (RdfParserSelectionException) { //If we get a Parse 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(); } 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); } }
public void WritingFormattingGraphs() { List <IGraph> graphs = new List <IGraph>(); Graph g = new Graph(); g.LoadFromEmbeddedResource("VDS.RDF.Configuration.configuration.ttl"); graphs.Add(g); g = new Graph(); g.LoadFromFile("resources\\InferenceTest.ttl"); graphs.Add(g); g = new Graph(); g.LoadFromFile("resources\\cyrillic.rdf"); graphs.Add(g); g = new Graph(); g.LoadFromFile("resources\\complex-collections.nt"); graphs.Add(g); List <IGraphFormatter> formatters = new List <IGraphFormatter>() { new RdfXmlFormatter() }; List <IRdfReader> parsers = new List <IRdfReader>() { new RdfXmlParser() }; for (int i = 0; i < formatters.Count; i++) { IGraphFormatter formatter = formatters[i]; Console.WriteLine("Using Formatter " + formatter.GetType().ToString()); foreach (IGraph graph in graphs) { //Console.WriteLine("Testing Graph " + (graph.BaseUri != null ? graph.BaseUri.ToString() : String.Empty)); StringBuilder output = new StringBuilder(); output.AppendLine(formatter.FormatGraphHeader(graph)); foreach (Triple t in graph.Triples) { output.AppendLine(formatter.Format(t)); } output.AppendLine(formatter.FormatGraphFooter()); Console.WriteLine(output.ToString()); //Try parsing to check it round trips Graph h = new Graph(); IRdfReader parser = parsers[i]; parser.Load(h, new StringReader(output.ToString())); GraphDiffReport diff = graph.Difference(h); if (!diff.AreEqual) { TestTools.ShowDifferences(diff); } Assert.Equal(graph, h); } } Console.WriteLine(); }
/// <summary> /// Loads the contents of the given File using a RDF Handler providing the RDF dataset format can be determined /// </summary> /// <param name="handler">RDF Handler to use</param> /// <param name="filename">File to load from</param> /// <param name="parser">Parser to use to parse the given file</param> /// <remarks> /// <para> /// If the <paramref name="parser"/> parameter is set to null then the <see cref="FileLoader">FileLoader</see> attempts to select a Store Parser by examining the file extension to select the most likely MIME type for the file. This assume that the file extension corresponds to one of the recognized file extensions for a RDF dataset format the library supports. If this suceeds then a parser is chosen and used to parse the input file. /// </para> /// </remarks> public static void Load(IRdfHandler handler, String filename, IStoreReader parser) { if (handler == null) { throw new RdfParseException("Cannot read a RDF Dataset using a null RDF Handler"); } if (filename == null) { throw new RdfParseException("Cannot read a RDF Dataset from a null File"); } if (!File.Exists(filename)) { #if SILVERLIGHT throw new FileNotFoundException("Cannot read a RDF Dataset from the File '" + filename + "' since it doesn't exist"); #else throw new FileNotFoundException("Cannot read a RDF Dataset from the File '" + filename + "' since it doesn't exist", filename); #endif } if (parser == null) { try { parser = MimeTypesHelper.GetStoreParser(MimeTypesHelper.GetMimeType(MimeTypesHelper.GetTrueFileExtension(filename))); } catch (RdfParserSelectionException) { //If error then we couldn't determine MIME Type from the File Extension RaiseWarning("Unable to select a dataset parser by determining MIME Type from the File Extension"); //Try selecting a RDF parser instead try { IRdfReader rdfParser = MimeTypesHelper.GetParser(MimeTypesHelper.GetMimeType(MimeTypesHelper.GetTrueFileExtension(filename))); Graph g = new Graph(); rdfParser.Load(handler, filename); return; } catch (RdfParserSelectionException) { //Ignore this, will try and use format guessing and assume is a dataset format } } } if (parser == null) { //Unable to determine format from File Extension //Read file in locally and use the StringParser to select a parser StreamReader reader = new StreamReader(filename); String data = reader.ReadToEnd(); reader.Close(); parser = StringParser.GetDatasetParser(data); RaiseWarning("Used the StringParser to guess the parser to use - it guessed " + parser.GetType().Name); parser.Warning += RaiseStoreWarning; parser.Load(handler, new TextReaderParams(new StringReader(data))); } else { parser.Warning += RaiseStoreWarning; parser.Load(handler, new StreamParams(filename)); } }
/// <summary> /// Sets the Syntax Highlighter based on a Parser /// </summary> /// <param name="parser">RDF Parser</param> public void SetHighlighter(IRdfReader parser) { if (parser is NTriplesParser) { this.SetHighlighter("NTriples"); } else if (parser is TurtleParser) { this.SetHighlighter("Turtle"); } else if (parser is Notation3Parser) { this.SetHighlighter("Notation3"); } else if (parser is RdfXmlParser) { this.SetHighlighter("RdfXml"); } else if (parser is RdfJsonParser) { this.SetHighlighter("RdfJson"); } else if (parser is RdfAParser) { this.SetHighlighter("XHtmlRdfA"); } else { this.SetNoHighlighting(); } }
/// <summary> /// Attempts to load a RDF Graph from a URI asynchronously using an RDF Handler /// </summary> /// <param name="handler">RDF Handler to use</param> /// <param name="u">URI to load from</param> /// <param name="parser">Parser to use</param> /// <param name="callback">Callback to invoke when the operation completes</param> /// <param name="state">State to pass to the callback</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, RdfHandlerCallback callback, Object state) { 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); } //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); //TODO: Add use of Cache into here, this is tricky because this code is primarily intended for Silverlight where we disable the cache purposefully //Setup the Request HttpWebRequest request = (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 request.Accept = MimeTypesHelper.CustomHttpAcceptHeader(parser); } else { request.Accept = MimeTypesHelper.HttpAcceptHeader; } //Use HTTP GET request.Method = "GET"; #if !SILVERLIGHT request.Timeout = Options.UriLoaderTimeout; #endif if (_userAgent != null && !_userAgent.Equals(String.Empty)) { request.UserAgent = _userAgent; } #if DEBUG //HTTP Debugging if (Options.HttpDebugging) { Tools.HttpDebugRequest(request); } #endif request.BeginGetResponse(result => { using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result)) { #if DEBUG if (Options.HttpDebugging) { Tools.HttpDebugResponse(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(response.ContentType); } parser.Warning += RaiseWarning; parser.Load(handler, new StreamReader(response.GetResponseStream())); //Finally can invoke the callback callback(handler, state); } }, null); } catch (UriFormatException uriEx) { //URI Format Invalid throw new RdfParseException("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 throw new WebException("A HTTP Error occurrred resolving the URI '" + u.ToString() + "', see inner exception for details", webEx); } }