/// <summary> /// Generates the Description for each of the Nodes to be described /// </summary> /// <param name="handler">RDF Handler</param> /// <param name="context">SPARQL Evaluation Context</param> /// <param name="nodes">Nodes to be described</param> protected override void DescribeInternal(IRdfHandler handler, SparqlEvaluationContext context, IEnumerable<INode> nodes) { //Rewrite Blank Node IDs for DESCRIBE Results Dictionary<String, INode> bnodeMapping = new Dictionary<string, INode>(); //Get Triples for this Subject Queue<INode> bnodes = new Queue<INode>(); HashSet<INode> expandedBNodes = new HashSet<INode>(); INode rdfsLabel = handler.CreateUriNode(UriFactory.Create(NamespaceMapper.RDFS + "label")); foreach (INode n in nodes) { //Get Triples where the Node is the Subject foreach (Triple t in context.Data.GetTriplesWithSubject(n)) { if (t.Object.NodeType == NodeType.Blank) { if (!expandedBNodes.Contains(t.Object)) bnodes.Enqueue(t.Object); } if (!handler.HandleTriple((this.RewriteDescribeBNodes(t, bnodeMapping, handler)))) ParserHelper.Stop(); } //Compute the Blank Node Closure for this Subject while (bnodes.Count > 0) { INode bsubj = bnodes.Dequeue(); if (expandedBNodes.Contains(bsubj)) continue; expandedBNodes.Add(bsubj); foreach (Triple t2 in context.Data.GetTriplesWithSubjectPredicate(bsubj, rdfsLabel)) { if (!handler.HandleTriple((this.RewriteDescribeBNodes(t2, bnodeMapping, handler)))) ParserHelper.Stop(); } } } }
/// <summary> /// Creates a new Paging Handler /// </summary> /// <param name="handler">Inner Handler to use</param> /// <param name="limit">Limit</param> /// <param name="offset">Offset</param> /// <remarks> /// If you just want to use an offset and not apply a limit then set limit to be less than zero /// </remarks> public PagingHandler(IRdfHandler handler, int limit, int offset) : base(handler) { if (handler == null) throw new ArgumentNullException("handler"); this._handler = handler; this._limit = Math.Max(-1, limit); this._offset = Math.Max(0, offset); }
/// <summary> /// Creates a new Base Parser Context /// </summary> /// <param name="handler">RDF Handler</param> /// <param name="traceParsing">Whether to trace parsing</param> public BaseParserContext(IRdfHandler handler, bool traceParsing) { if (handler == null) throw new ArgumentNullException("handler"); this._handler = handler; this._traceParsing = traceParsing; this._baseUri = this._handler.GetBaseUri(); }
/// <summary> /// Executes a SPARQL Query on the Graph handling the results with the given handlers /// </summary> /// <param name="rdfHandler">RDF Handler</param> /// <param name="resultsHandler">SPARQL Results Handler</param> /// <param name="sparqlQuery">SPARQL Query</param> public void ExecuteQuery(IRdfHandler rdfHandler, ISparqlResultsHandler resultsHandler, String sparqlQuery) { if (this._store == null) { this._store = new TripleStore(); this._store.Add(this); } this._store.ExecuteQuery(rdfHandler, resultsHandler, sparqlQuery); }
protected override void ImportUsingHandler(IRdfHandler handler) { this.Information = "Importing from URI " + this._u.ToString(); try { //Assume a RDF Graph UriLoader.Load(handler, this._u); } catch (RdfParserSelectionException) { //Assume a RDF Dataset UriLoader.LoadDataset(handler, this._u); } }
protected override void ImportUsingHandler(IRdfHandler handler) { this.Information = "Importing from File " + this._file; try { //Assume a RDF Graph IRdfReader reader = MimeTypesHelper.GetParser(MimeTypesHelper.GetMimeTypes(Path.GetExtension(this._file))); FileLoader.Load(handler, this._file, reader); } catch (RdfParserSelectionException) { //Assume a RDF Dataset FileLoader.LoadDataset(handler, this._file); } }
/// <summary> /// Generates the Description for each of the Nodes to be described /// </summary> /// <param name="handler">RDF Handler</param> /// <param name="context">SPARQL Evaluation Context</param> /// <param name="nodes">Nodes to be described</param> protected override void DescribeInternal(IRdfHandler handler, SparqlEvaluationContext context, IEnumerable<INode> nodes) { //Rewrite Blank Node IDs for DESCRIBE Results Dictionary<String, INode> bnodeMapping = new Dictionary<string, INode>(); //Get Triples for this Subject foreach (INode subj in nodes) { //Get Triples where the Node is the Subject foreach (Triple t in context.Data.GetTriplesWithSubject(subj)) { if (!handler.HandleTriple((this.RewriteDescribeBNodes(t, bnodeMapping, handler)))) ParserHelper.Stop(); } } }
public void ExecuteSparql(IRdfHandler rdfHandler, ISparqlResultsHandler resultsHandler, string sparqlQuery, IStore store) { try { var query = ParseSparql(sparqlQuery); var queryProcessor = new BrightstarQueryProcessor(store, new StoreSparqlDataset(store)); queryProcessor.ProcessQuery(rdfHandler, resultsHandler, query); } catch (Exception ex) { Logging.LogError(BrightstarEventId.SparqlExecutionError, "Error Executing Sparql {0}. Cause: {1}", sparqlQuery, ex); throw; } }
/// <summary> /// Generates the Description for each of the Nodes to be described /// </summary> /// <param name="handler">RDF Handler</param> /// <param name="context">SPARQL Evaluation Context</param> /// <param name="nodes">Nodes to be described</param> protected override void DescribeInternal(IRdfHandler handler, SparqlEvaluationContext context, IEnumerable<INode> nodes) { //Rewrite Blank Node IDs for DESCRIBE Results Dictionary<String, INode> bnodeMapping = new Dictionary<string, INode>(); foreach (INode n in nodes) { if (n.NodeType == NodeType.Uri) { IGraph g = context.Data[((IUriNode)n).Uri]; foreach (Triple t in g.Triples) { if (!handler.HandleTriple(this.RewriteDescribeBNodes(t, bnodeMapping, handler))) ParserHelper.Stop(); } } } }
/// <inheritdoc /> public void Load(IRdfHandler handler, TextReader input) { bool finished = false; try { handler.StartRdf(); using (JsonReader jsonReader = new JsonTextReader(input)) { jsonReader.DateParseHandling = DateParseHandling.None; JToken json = JsonLD.Core.JsonLdProcessor.Expand(JToken.Load(jsonReader)); foreach (JObject subjectJObject in json) { string subject = subjectJObject["@id"].ToString(); JToken type; if (subjectJObject.TryGetValue("@type", out type)) { HandleType(handler, subject, type); } foreach (var property in subjectJObject.Properties().Where(property => (property.Name != "@id") && (property.Name != "@type"))) { HandleProperty(handler, subject, property); } } } finished = true; handler.EndRdf(true); } catch { finished = true; handler.EndRdf(false); throw; } finally { if (!finished) { handler.EndRdf(true); } } }
/// <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> /// Gets the Description Graph based on the Query Results from the given Evaluation Context passing the resulting Triples to the given RDF Handler /// </summary> /// <param name="handler">RDF Handler</param> /// <param name="context">SPARQL Evaluation Context</param> public void Describe(IRdfHandler handler, SparqlEvaluationContext context) { try { handler.StartRdf(); //Apply Base URI and Namespaces to the Handler if (context.Query != null) { if (context.Query.BaseUri != null) { if (!handler.HandleBaseUri(context.Query.BaseUri)) ParserHelper.Stop(); } foreach (String prefix in context.Query.NamespaceMap.Prefixes) { if (!handler.HandleNamespace(prefix, context.Query.NamespaceMap.GetNamespaceUri(prefix))) ParserHelper.Stop(); } } //Get the Nodes needing describing List<INode> nodes = this.GetNodes(handler, context); if (nodes.Count > 0) { //If there is at least 1 Node then start describing this.DescribeInternal(handler, context, nodes); } handler.EndRdf(true); } catch (RdfParsingTerminatedException) { handler.EndRdf(true); } catch { handler.EndRdf(false); throw; } }
/// <summary> /// Processes a SPARQL Query passing the results to the RDF or Results handler as appropriate /// </summary> /// <param name="rdfHandler">RDF Handler</param> /// <param name="resultsHandler">Results Handler</param> /// <param name="query">SPARQL Query</param> public void ProcessQuery(IRdfHandler rdfHandler, ISparqlResultsHandler resultsHandler, SparqlQuery query) { #if !SILVERLIGHT query.QueryExecutionTime = null; DateTime start = DateTime.Now; try { switch (query.QueryType) { case SparqlQueryType.Ask: case SparqlQueryType.Select: case SparqlQueryType.SelectAll: case SparqlQueryType.SelectAllDistinct: case SparqlQueryType.SelectAllReduced: case SparqlQueryType.SelectDistinct: case SparqlQueryType.SelectReduced: this._endpoint.QueryWithResultSet(resultsHandler, this._formatter.Format(query)); break; case SparqlQueryType.Construct: case SparqlQueryType.Describe: case SparqlQueryType.DescribeAll: this._endpoint.QueryWithResultGraph(rdfHandler, this._formatter.Format(query)); break; default: throw new RdfQueryException("Unable to execute an unknown query type against a Remote Endpoint"); } } finally { TimeSpan elapsed = (DateTime.Now - start); query.QueryExecutionTime = elapsed; } #else throw new NotSupportedException("Synchronous remote query is not supported under Silverlight/WP7 - please use one of the alternative overload of this methods which takes a callback"); #endif }
/// <summary> /// Gets the Base URI from the RDF Handler. /// </summary> /// <param name="handler">RDF Handler.</param> /// <returns></returns> internal static Uri GetBaseUri(this IRdfHandler handler) { if (handler is GraphHandler) { return(((GraphHandler)handler).BaseUri); } else if (handler is IWrappingRdfHandler) { IRdfHandler temp = ((IWrappingRdfHandler)handler).InnerHandlers.FirstOrDefault(h => h.GetBaseUri() != null); if (temp == null) { return(null); } else { return(temp.GetBaseUri()); } } else { return(null); } }
/// <summary> /// Read RDF/JSON Syntax from some Stream using a RDF Handler /// </summary> /// <param name="handler">RDF Handler to use</param> /// <param name="input">Stream to read from</param> public void Load(IRdfHandler handler, StreamReader input) { if (handler == null) { throw new RdfParseException("Cannot read RDF into a null RDF Handler"); } if (input == null) { throw new RdfParseException("Cannot read RDF from a null Stream"); } //Issue a Warning if the Encoding of the Stream is not UTF-8 if (!input.CurrentEncoding.Equals(Encoding.UTF8)) { #if !SILVERLIGHT this.RaiseWarning("Expected Input Stream to be encoded as UTF-8 but got a Stream encoded as " + input.CurrentEncoding.EncodingName + " - Please be aware that parsing errors may occur as a result"); #else this.RaiseWarning("Expected Input Stream to be encoded as UTF-8 but got a Stream encoded as " + input.CurrentEncoding.GetType().Name + " - Please be aware that parsing errors may occur as a result"); #endif } this.Load(handler, (TextReader)input); }
/// <summary> /// Helper method for doing async load operations, callers just need to provide an appropriately prepared HTTP request /// </summary> /// <param name="request">HTTP Request</param> /// <param name="handler">Handler to load with</param> /// <param name="callback">Callback</param> /// <param name="state">State to pass to the callback</param> protected internal void LoadGraphAsync(HttpWebRequest request, IRdfHandler handler, AsyncStorageCallback callback, Object state) { Tools.HttpDebugRequest(request); request.BeginGetResponse(r => { try { HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(r); Tools.HttpDebugResponse(response); //Parse the retrieved RDF IRdfReader parser = MimeTypesHelper.GetParser(response.ContentType); parser.Load(handler, new StreamReader(response.GetResponseStream())); //If we get here then it was OK response.Close(); callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.LoadWithHandler, handler), state); } catch (WebException webEx) { if (webEx.Response != null) { Tools.HttpDebugResponse((HttpWebResponse)webEx.Response); if (((HttpWebResponse)webEx.Response).StatusCode == HttpStatusCode.NotFound) { callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.LoadWithHandler, handler), state); return; } } callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.LoadWithHandler, handler, new RdfStorageException("A HTTP Error occurred while trying to load a Graph from the Store", webEx)), state); } catch (Exception ex) { callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.LoadWithHandler, handler, StorageHelper.HandleError(ex, "loading a Graph asynchronously from")), state); } }, state); }
/// <summary> /// Processes a SPARQL Query against the Knowledge Base passing the results to the RDF or Results handler as appropriate /// </summary> /// <param name="rdfHandler">RDF Handler</param> /// <param name="resultsHandler">Results Handler</param> /// <param name="sparqlQuery">SPARQL Query</param> /// <param name="callback">Callback to invoke once handling of results has completed</param> /// <param name="state">State to pass to the callback</param> public void Query(IRdfHandler rdfHandler, ISparqlResultsHandler resultsHandler, String sparqlQuery, QueryCallback callback, Object state) { SparqlQueryParser parser = new SparqlQueryParser(); SparqlQuery q = parser.ParseFromString(sparqlQuery); SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(UriFactory.Create(this._sparqlUri)); switch (q.QueryType) { case SparqlQueryType.Ask: case SparqlQueryType.Select: case SparqlQueryType.SelectAll: case SparqlQueryType.SelectAllDistinct: case SparqlQueryType.SelectAllReduced: case SparqlQueryType.SelectDistinct: case SparqlQueryType.SelectReduced: endpoint.QueryWithResultSet(sparqlQuery, (rs, _) => { resultsHandler.Apply(rs); callback(rdfHandler, resultsHandler, state); }, state); break; case SparqlQueryType.Construct: case SparqlQueryType.Describe: case SparqlQueryType.DescribeAll: endpoint.QueryWithResultGraph(sparqlQuery, (g, _) => { rdfHandler.Apply(g); callback(rdfHandler, resultsHandler, state); }, state); break; default: throw new RdfQueryException("Cannot execute unknown query types against Pellet Server"); } }
public Converter( IRdfHandler rdfHandler, ITableResolver resolver = null, ConverterMode mode = ConverterMode.Standard, Action <string> errorMessageSink = null, IProgress <int> conversionProgress = null, int reportInterval = 50, bool suppressStringDatatype = false) { _resolver = resolver ?? new DefaultResolver(); _rdfHandler = rdfHandler; Mode = mode; _errors = new List <string>(); _progress = conversionProgress; _errorMessageSink = errorMessageSink; _reportInterval = reportInterval; _suppressStringDatatype = suppressStringDatatype; using (var reader = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("DataDock.CsvWeb.Resources.csvw.jsonld"), Encoding.UTF8)) { _csvwContext = JsonConvert.DeserializeObject <JObject>(reader.ReadToEnd())["@context"] as JObject; } }
/// <summary> /// Loads a Graph from the 4store instance /// </summary> /// <param name="handler">RDF Handler</param> /// <param name="graphUri">URI of the Graph to load</param> public void LoadGraph(IRdfHandler handler, String graphUri) { try { #if !NO_RWLOCK this._lockManager.EnterReadLock(); #endif if (!graphUri.Equals(String.Empty)) { this._endpoint.QueryWithResultGraph(handler, "CONSTRUCT { ?s ?p ?o } FROM <" + graphUri.Replace(">", "\\>") + "> WHERE { ?s ?p ?o }"); } else { throw new RdfStorageException("Cannot retrieve a Graph from 4store without specifying a Graph URI"); } } finally { #if !NO_RWLOCK this._lockManager.ExitReadLock(); #endif } }
public RdfACoreParserContext(IRdfHandler handler, IRdfAHostLanguage language, TextReader reader, bool traceParsing) { this._handler = handler; this._traceParsing = traceParsing; this._language = language; //Set up the Event Queue IEventGenerator <IRdfAEvent> generator = this._language.GetEventGenerator(reader); if (generator is IJitEventGenerator <IRdfAEvent> ) { this._events = new StreamingEventQueue <IRdfAEvent>((IJitEventGenerator <IRdfAEvent>)generator); } else if (generator is IPreProcessingEventGenerator <IRdfAEvent, RdfACoreParserContext> ) { this._events = new EventQueue <IRdfAEvent>(generator); ((IPreProcessingEventGenerator <IRdfAEvent, RdfACoreParserContext>)generator).GetAllEvents(this); } this._events = new DualEventQueue <IRdfAEvent>(this._events); //Setup final things this._baseUri = new NestedReference <Uri>((this._language.InitialBaseUri != null ? this._language.InitialBaseUri : this._handler.GetBaseUri())); this._defaultVocabUri = new NestedReference <Uri>(this._language.DefaultVocabularyUri); }
/// <summary> /// Applies the triples to an RDF Handler. /// </summary> /// <param name="handler">RDF Handler.</param> /// <param name="ts">Triples.</param> public static void Apply(this IRdfHandler handler, IEnumerable <Triple> ts) { try { handler.StartRdf(); foreach (Triple t in ts) { if (!handler.HandleTriple(t)) { ParserHelper.Stop(); } } handler.EndRdf(false); } catch (RdfParsingTerminatedException) { handler.EndRdf(true); } catch { handler.EndRdf(false); throw; } }
/// <summary> /// Processes a SPARQL Query passing the results to the RDF or Results handler as appropriate /// </summary> /// <param name="rdfHandler">RDF Handler</param> /// <param name="resultsHandler">Results Handler</param> /// <param name="query">SPARQL Query</param> public void ProcessQuery(IRdfHandler rdfHandler, ISparqlResultsHandler resultsHandler, SparqlQuery query) { #if !SILVERLIGHT query.QueryExecutionTime = null; DateTime start = DateTime.Now; try { this._svc.Query(rdfHandler, resultsHandler, query.ToString()); } finally { TimeSpan elapsed = (DateTime.Now - start); query.QueryExecutionTime = (DateTime.Now - start); } #else throw new NotSupportedException("Synchronous remote query is not supported under Silverlight/WP7 - please use one of the alternative overload of this methods which takes a callback"); #endif }
/// <summary> /// Creates a new Parser Context /// </summary> /// <param name="handler">RDF Handler to use</param> /// <param name="document">HTML Document</param> public RdfAParserContext(IRdfHandler handler, HtmlDocument document) : this(handler, document, false) { }
/// <summary> /// Parses NTriples Syntax from the given Input using a RDF Handler /// </summary> /// <param name="handler">RDF Handler to use</param> /// <param name="input">Input to read input from</param> public void Load(IRdfHandler handler, TextReader input) { if (handler == null) throw new RdfParseException("Cannot read RDF into a null RDF Handler"); if (input == null) throw new RdfParseException("Cannot read RDF from a null TextReader"); try { TokenisingParserContext context = new TokenisingParserContext(handler, new NTriplesTokeniser(input), this._queuemode, this._traceparsing, this._tracetokeniser); this.Parse(context); } catch { throw; } finally { try { input.Close(); } catch { //Catch is just here in case something goes wrong with closing the stream //This error can be ignored } } }
/// <summary> /// Loads a Graph from the Store asynchronously. /// </summary> /// <param name="handler">Handler to load with.</param> /// <param name="graphUri">URI of the Graph to load.</param> /// <param name="callback">Callback.</param> /// <param name="state">State to pass to the callback.</param> public void LoadGraph(IRdfHandler handler, Uri graphUri, AsyncStorageCallback callback, object state) { this.AsyncLoadGraph(handler, graphUri, callback, state); }
private void Parse(IRdfHandler handler, ITokenQueue tokens) { IToken next; IToken s, p, o; try { handler.StartRdf(); //Expect a BOF token at start next = tokens.Dequeue(); if (next.TokenType != Token.BOF) { throw ParserHelper.Error("Unexpected Token '" + next.GetType().ToString() + "' encountered, expected a BOF token at the start of the input", next); } do { next = tokens.Peek(); if (next.TokenType == Token.EOF) return; s = this.TryParseSubject(tokens); p = this.TryParsePredicate(tokens); o = this.TryParseObject(tokens); Uri context = this.TryParseContext(tokens); this.TryParseTriple(handler, s, p, o, context); next = tokens.Peek(); } while (next.TokenType != Token.EOF); handler.EndRdf(true); } catch (RdfParsingTerminatedException) { handler.EndRdf(true); //Discard this - it justs means the Handler told us to stop } catch { handler.EndRdf(false); throw; } }
/// <summary> /// Loads a Graph from the underlying Store /// </summary> /// <param name="handler">RDF Handler</param> /// <param name="graphUri">URI of the Graph to load</param> public void LoadGraph(IRdfHandler handler, Uri graphUri) { _manager.LoadGraph(handler, graphUri); }
/// <summary> /// Generates the Description for each of the Nodes to be described. /// </summary> /// <param name="handler">RDF Handler.</param> /// <param name="context">SPARQL Evaluation Context.</param> /// <param name="nodes">Nodes to be described.</param> protected override void DescribeInternal(IRdfHandler handler, SparqlEvaluationContext context, IEnumerable <INode> nodes) { // Rewrite Blank Node IDs for DESCRIBE Results Dictionary <String, INode> bnodeMapping = new Dictionary <string, INode>(); // Get Triples for this Subject Queue <INode> bnodes = new Queue <INode>(); HashSet <INode> expandedBNodes = new HashSet <INode>(); foreach (INode n in nodes) { foreach (Triple t in context.Data.GetTriplesWithSubject(n).ToList()) { if (t.Object.NodeType == NodeType.Blank) { if (!expandedBNodes.Contains(t.Object)) { bnodes.Enqueue(t.Object); } } if (!handler.HandleTriple((RewriteDescribeBNodes(t, bnodeMapping, handler)))) { ParserHelper.Stop(); } } if (n.NodeType == NodeType.Blank) { foreach (Triple t in context.Data.GetTriplesWithPredicate(n).ToList()) { if (t.Subject.NodeType == NodeType.Blank) { if (!expandedBNodes.Contains(t.Subject)) { bnodes.Enqueue(t.Subject); } } if (t.Object.NodeType == NodeType.Blank) { if (!expandedBNodes.Contains(t.Object)) { bnodes.Enqueue(t.Object); } } if (!handler.HandleTriple((RewriteDescribeBNodes(t, bnodeMapping, handler)))) { ParserHelper.Stop(); } } } foreach (Triple t in context.Data.GetTriplesWithObject(n).ToList()) { if (t.Subject.NodeType == NodeType.Blank) { if (!expandedBNodes.Contains(t.Object)) { bnodes.Enqueue(t.Subject); } } if (!handler.HandleTriple((RewriteDescribeBNodes(t, bnodeMapping, handler)))) { ParserHelper.Stop(); } } } // Expand BNodes while (bnodes.Count > 0) { INode n = bnodes.Dequeue(); if (expandedBNodes.Contains(n)) { continue; } expandedBNodes.Add(n); foreach (Triple t in context.Data.GetTriplesWithSubject(n).ToList()) { if (t.Object.NodeType == NodeType.Blank) { if (!expandedBNodes.Contains(t.Object)) { bnodes.Enqueue(t.Object); } } if (!handler.HandleTriple((RewriteDescribeBNodes(t, bnodeMapping, handler)))) { ParserHelper.Stop(); } } if (n.NodeType == NodeType.Blank) { foreach (Triple t in context.Data.GetTriplesWithPredicate(n).ToList()) { if (t.Subject.NodeType == NodeType.Blank) { if (!expandedBNodes.Contains(t.Subject)) { bnodes.Enqueue(t.Subject); } } if (t.Object.NodeType == NodeType.Blank) { if (!expandedBNodes.Contains(t.Object)) { bnodes.Enqueue(t.Object); } } if (!handler.HandleTriple((RewriteDescribeBNodes(t, bnodeMapping, handler)))) { ParserHelper.Stop(); } } } foreach (Triple t in context.Data.GetTriplesWithObject(n).ToList()) { if (t.Subject.NodeType == NodeType.Blank) { if (!expandedBNodes.Contains(t.Object)) { bnodes.Enqueue(t.Subject); } } if (!handler.HandleTriple((RewriteDescribeBNodes(t, bnodeMapping, handler)))) { ParserHelper.Stop(); } } } }
/// <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> /// <remarks> /// <para> /// Attempts to select the relevant Store Parser based on the Content Type header returned in the HTTP Response. /// </para> /// </remarks> public static void LoadDataset(IRdfHandler handler, Uri u) { UriLoader.Load(handler, u, (IStoreReader)null); }
/// <summary> /// Attempts to load a RDF Graph from the given URI using a RDF Handler /// </summary> /// <param name="handler">RDF Handler to use</param> /// <param name="u">URI to attempt to get RDF from</param> /// <param name="parser">Parser to use</param> /// <remarks> /// <para> /// Uses the supplied parser to attempt parsing regardless of the actual Content Type returned /// </para> /// <para> /// In the event that the URI is a File URI the <see cref="FileLoader">FileLoader</see> will be used instead /// </para> /// <para> /// If the URI is a Data URI then the <see cref="DataUriLoader">DataUriLoader</see> will be used instead. /// </para> /// </remarks> public static void Load(IRdfHandler handler, Uri u, IRdfReader parser) { if (handler == null) { throw new RdfParseException("Cannot read RDF using a null RDF Handler"); } if (u == null) { throw new RdfParseException("Cannot load RDF from a null URI"); } try { #if SILVERLIGHT if (u.IsFile()) #else if (u.IsFile) #endif { //Invoke FileLoader instead RaiseWarning("This is a file: URI so invoking the FileLoader instead"); if (Path.DirectorySeparatorChar == '/') { FileLoader.Load(handler, u.ToString().Substring(7), parser); } else { FileLoader.Load(handler, u.ToString().Substring(8), parser); } return; } if (u.Scheme.Equals("data")) { //Invoke DataUriLoader instead RaiseWarning("This is a data: URI so invoking the DataUriLoader instead"); DataUriLoader.Load(handler, u); return; } //Sanitise the URI to remove any Fragment ID u = Tools.StripUriFragment(u); #if !NO_URICACHE //Use Cache if possible String etag = String.Empty; String local = null; if (Options.UriLoaderCaching) { if (_cache.HasETag(u)) { //Get the ETag and then we'll include an If-None-Match header in our request etag = _cache.GetETag(u); } else if (_cache.HasLocalCopy(u, true)) { //Just try loading from the local copy local = _cache.GetLocalCopy(u); if (local != null) { try { FileLoader.Load(handler, local, new TurtleParser()); } catch { //If we get an Exception we failed to access the file successfully _cache.RemoveETag(u); _cache.RemoveLocalCopy(u); UriLoader.Load(handler, u, parser); } return; } } } #endif //Set-up the Request HttpWebRequest httpRequest; httpRequest = (HttpWebRequest)WebRequest.Create(u); //Want to ask for RDF formats if (parser != null) { //If a non-null parser set up a HTTP Header that is just for the given parser httpRequest.Accept = MimeTypesHelper.CustomHttpAcceptHeader(parser); } else { httpRequest.Accept = MimeTypesHelper.HttpAcceptHeader; } #if !NO_URICACHE if (Options.UriLoaderCaching) { if (!etag.Equals(String.Empty)) { httpRequest.Headers.Add(HttpRequestHeader.IfNoneMatch, etag); } } #endif //Use HTTP GET httpRequest.Method = "GET"; #if !SILVERLIGHT httpRequest.Timeout = Options.UriLoaderTimeout; #endif if (_userAgent != null && !_userAgent.Equals(String.Empty)) { httpRequest.UserAgent = _userAgent; } #if DEBUG //HTTP Debugging if (Options.HttpDebugging) { Tools.HttpDebugRequest(httpRequest); } #endif using (HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse()) { #if DEBUG //HTTP Debugging if (Options.HttpDebugging) { Tools.HttpDebugResponse(httpResponse); } #endif #if !NO_URICACHE if (Options.UriLoaderCaching) { //Are we using ETag based caching? if (!etag.Equals(String.Empty)) { //Did we get a Not-Modified response? if (httpResponse.StatusCode == HttpStatusCode.NotModified) { //If so then we need to load the Local Copy assuming it exists? if (_cache.HasLocalCopy(u, false)) { local = _cache.GetLocalCopy(u); try { FileLoader.Load(handler, local, new TurtleParser()); } catch { //If we get an Exception we failed to access the file successfully _cache.RemoveETag(u); _cache.RemoveLocalCopy(u); UriLoader.Load(handler, u, parser); } return; } else { //If the local copy didn't exist then we need to redo the response without //the ETag as we've lost the cached copy somehow _cache.RemoveETag(u); UriLoader.Load(handler, u, parser); return; } } //If we didn't get a Not-Modified response then we'll continue and parse the new response } } #endif //Get a Parser and Load the RDF if (parser == null) { //Only need to auto-detect the parser if a specific one wasn't specified parser = MimeTypesHelper.GetParser(httpResponse.ContentType); } parser.Warning += RaiseWarning; #if !NO_URICACHE //To do caching we ask the cache to give us a handler and then we tie it to IRdfHandler cacheHandler = _cache.ToCache(u, Tools.StripUriFragment(httpResponse.ResponseUri), httpResponse.Headers["ETag"]); if (cacheHandler != null) { //Note: We can ONLY use caching when we know that the Handler will accept all the data returned //i.e. if the Handler may trim the data in some way then we shouldn't cache the data returned if (handler.AcceptsAll) { handler = new MultiHandler(new IRdfHandler[] { handler, cacheHandler }); } else { cacheHandler = null; } } try { #endif parser.Load(handler, new StreamReader(httpResponse.GetResponseStream())); #if !NO_URICACHE } catch { //If we were trying to cache the response and something went wrong discard the cached copy _cache.RemoveETag(u); _cache.RemoveETag(Tools.StripUriFragment(httpResponse.ResponseUri)); _cache.RemoveLocalCopy(u); _cache.RemoveLocalCopy(Tools.StripUriFragment(httpResponse.ResponseUri)); } #endif } } catch (UriFormatException uriEx) { //Uri Format Invalid throw new RdfParseException("Unable to load from the given URI '" + u.ToString() + "' since it's format was invalid", uriEx); } catch (WebException webEx) { #if DEBUG if (webEx.Response != null && Options.HttpDebugging) { Tools.HttpDebugResponse((HttpWebResponse)webEx.Response); } #endif #if !NO_URICACHE if (webEx.Response != null) { if (((HttpWebResponse)webEx.Response).StatusCode == HttpStatusCode.NotModified) { //If so then we need to load the Local Copy assuming it exists? if (_cache.HasLocalCopy(u, false)) { String local = _cache.GetLocalCopy(u); try { FileLoader.Load(handler, local, new TurtleParser()); } catch { //If we get an Exception we failed to access the file successfully _cache.RemoveETag(u); _cache.RemoveLocalCopy(u); UriLoader.Load(handler, u, parser); } return; } else { //If the local copy didn't exist then we need to redo the response without //the ETag as we've lost the cached copy somehow _cache.RemoveETag(u); UriLoader.Load(handler, u, parser); return; } } } #endif //Some sort of HTTP Error occurred throw new WebException("A HTTP Error occurred resolving the URI '" + u.ToString() + "'", webEx); } }
/// <summary> /// Loads a Graph from the underlying Store /// </summary> /// <param name="handler">RDF Handler</param> /// <param name="graphUri">URI of the Graph to load</param> public void LoadGraph(IRdfHandler handler, Uri graphUri) { this._manager.LoadGraph(handler, graphUri); }
/// <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> /// Loads a Graph from the Store asynchronously /// </summary> /// <param name="handler">Handler to load with</param> /// <param name="graphUri">URI of the Graph to load</param> /// <param name="callback">Callback</param> /// <param name="state">State to pass to the callback</param> public abstract void LoadGraph(IRdfHandler handler, String graphUri, AsyncStorageCallback callback, Object state);
/// <summary> /// Loads a Graph from the Store asynchronously /// </summary> /// <param name="handler">Handler to load with</param> /// <param name="graphUri">URI of the Graph to load</param> /// <param name="callback">Callback</param> /// <param name="state">State to pass to the callback</param> public virtual void LoadGraph(IRdfHandler handler, Uri graphUri, AsyncStorageCallback callback, Object state) { this.LoadGraph(handler, graphUri.ToSafeString(), callback, state); }
/// <summary> /// Processes a SPARQL Query asynchronously passing the results to the relevant handler and invoking the callback when the query completes /// </summary> /// <param name="rdfHandler">RDF Handler</param> /// <param name="resultsHandler">Results Handler</param> /// <param name="query">SPARQL Query</param> /// <param name="callback">Callback</param> /// <param name="state">State to pass to the callback</param> public void ProcessQuery(IRdfHandler rdfHandler, ISparqlResultsHandler resultsHandler, SparqlQuery query, QueryCallback callback, Object state) { query.QueryExecutionTime = null; DateTime start = DateTime.Now; try { this._svc.Query(rdfHandler, resultsHandler, query.ToString(), callback, state); } finally { TimeSpan elapsed = (DateTime.Now - start); query.QueryExecutionTime = (DateTime.Now - start); } }
/// <summary> /// Creates a new Notation 3 Parser Context with custom settings /// </summary> /// <param name="handler">RDF Handler to use</param> /// <param name="tokeniser">Tokeniser to use</param> /// <param name="queueMode">Tokeniser Queue Mode</param> public Notation3ParserContext(IRdfHandler handler, ITokeniser tokeniser, TokenQueueMode queueMode) : base(handler, tokeniser, queueMode) { }
/// <summary> /// Loads a RDF Dataset 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, IStoreReader parser) { if (resource == null) throw new RdfParseException("Cannot read a RDF Dataset from a null Resource"); if (handler == null) throw new RdfParseException("Cannot read a RDF Dataset 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)) as Assembly; if (asm != null) { //Resource is in the loaded assembly EmbeddedResourceLoader.LoadDatasetInternal(handler, asm, resourceName, parser); } else { throw new RdfParseException("The Embedded Resource '" + resourceName + "' cannot be loaded as the required assembly '" + assemblyName + "' could not be loaded. Please ensure that the assembly name is correct and that is is referenced/accessible in your application."); } } else { //Resource is in dotNetRDF EmbeddedResourceLoader.LoadDatasetInternal(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> /// Loads a Graph from the Store. /// </summary> /// <param name="handler">Handler to load with.</param> /// <param name="graphUri">URI of the Graph to load.</param> public abstract void LoadGraph(IRdfHandler handler, string graphUri);
/// <summary> /// Executes a SPARQL Query on the underlying Store processing the results with 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) { _queryManager.Query(rdfHandler, resultsHandler, sparqlQuery); }
/// <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> /// Loads a RDF Dataset from the NQuads input using a RDF Handler /// </summary> /// <param name="handler">RDF Handler to use</param> /// <param name="parameters">Parameters indicating the Stream to read from</param> public void Load(IRdfHandler handler, IStoreParams parameters) { if (handler == null) throw new ArgumentNullException("handler", "Cannot parse an RDF Dataset using a null RDF Handler"); if (parameters == null) throw new ArgumentNullException("parameters", "Cannot parse an RDF Dataset using null Parameters"); //Try and get the Input from the parameters TextReader input = null; if (parameters is StreamParams) { //Get Input Stream input = ((StreamParams)parameters).StreamReader; #if !SILVERLIGHT //Issue a Warning if the Encoding of the Stream is not ASCII if (!((StreamReader)input).CurrentEncoding.Equals(Encoding.ASCII)) { this.RaiseWarning("Expected Input Stream to be encoded as ASCII but got a Stream encoded as " + ((StreamReader)input).CurrentEncoding.EncodingName + " - Please be aware that parsing errors may occur as a result"); } #endif } else if (parameters is TextReaderParams) { input = ((TextReaderParams)parameters).TextReader; } if (input != null) { try { //Setup Token Queue and Tokeniser NTriplesTokeniser tokeniser = new NTriplesTokeniser(input); tokeniser.NQuadsMode = true; TokenQueue tokens = new TokenQueue(); tokens.Tokeniser = tokeniser; tokens.Tracing = this._tracetokeniser; tokens.InitialiseBuffer(); //Invoke the Parser this.Parse(handler, tokens); } catch { throw; } finally { try { input.Close(); } catch { //No catch actions - just cleaning up } } } else { throw new RdfStorageException("Parameters for the NQuadsParser must be of the type StreamParams/TextReaderParams"); } }
/// <summary> /// Generates the Description for each of the Nodes to be described /// </summary> /// <param name="handler">RDF Handler</param> /// <param name="context">SPARQL Evaluation Context</param> /// <param name="nodes">Nodes to be described</param> protected abstract void DescribeInternal(IRdfHandler handler, SparqlEvaluationContext context, IEnumerable <INode> nodes);
private void TryParseTriple(IRdfHandler handler, IToken s, IToken p, IToken o, Uri graphUri) { INode subj, pred, obj; switch (s.TokenType) { case Token.BLANKNODEWITHID: subj = handler.CreateBlankNode(s.Value.Substring(2)); break; case Token.URI: subj = ParserHelper.TryResolveUri(handler, s); break; default: throw ParserHelper.Error("Unexpected Token '" + s.GetType().ToString() + "' encountered, expected a Blank Node/URI as the Subject of a Triple", s); } switch (p.TokenType) { case Token.URI: pred = ParserHelper.TryResolveUri(handler, p); break; default: throw ParserHelper.Error("Unexpected Token '" + p.GetType().ToString() + "' encountered, expected a URI as the Predicate of a Triple", p); } switch (o.TokenType) { case Token.BLANKNODEWITHID: obj = handler.CreateBlankNode(o.Value.Substring(2)); break; case Token.LITERAL: obj = handler.CreateLiteralNode(o.Value); break; case Token.LITERALWITHDT: String dtUri = ((LiteralWithDataTypeToken)o).DataType; obj = handler.CreateLiteralNode(o.Value, new Uri(dtUri.Substring(1, dtUri.Length - 2))); break; case Token.LITERALWITHLANG: obj = handler.CreateLiteralNode(o.Value, ((LiteralWithLanguageSpecifierToken)o).Language); break; case Token.URI: obj = ParserHelper.TryResolveUri(handler, o); break; default: throw ParserHelper.Error("Unexpected Token '" + o.GetType().ToString() + "' encountered, expected a Blank Node/Literal/URI as the Object of a Triple", o); } if (!handler.HandleTriple(new Triple(subj, pred, obj, graphUri))) throw ParserHelper.Stop(); }
/// <summary> /// Loads a RDF Dataset 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> public static void LoadDataset(IRdfHandler handler, String resource) { EmbeddedResourceLoader.Load(handler, resource, (IStoreReader)null); }
/// <summary> /// Loads a Graph from the Store asynchronously. /// </summary> /// <param name="handler">Handler to load with.</param> /// <param name="graphUri">URI of the Graph to load.</param> /// <param name="callback">Callback.</param> /// <param name="state">State to pass to the callback.</param> public void LoadGraph(IRdfHandler handler, string graphUri, AsyncStorageCallback callback, object state) { Uri u = (String.IsNullOrEmpty(graphUri) ? null : UriFactory.Create(graphUri)); LoadGraph(handler, u, callback, state); }
/// <summary> /// Creates a new JSON Parser Context /// </summary> /// <param name="handler">RDF Handler to use</param> /// <param name="input">JSON Text Reader to read from</param> public JsonParserContext(IRdfHandler handler, JsonTextReader input) : base(handler) { this._input = input; }
/// <summary> /// Makes a Query where the expected result is a Graph i.e. a CONSTRUCT or DESCRIBE query /// </summary> /// <param name="handler">RDF Handler to process the results</param> /// <param name="sparqlQuery">SPARQL Query</param> public override void QueryWithResultGraph(IRdfHandler handler, string sparqlQuery) { //If no endpoints do nothing if (this._endpoints.Count == 0) { return; } //Fire off all the Asychronous Requests List <AsyncQueryWithResultGraph> asyncCalls = new List <AsyncQueryWithResultGraph>(); List <IAsyncResult> asyncResults = new List <IAsyncResult>(); int count = 0; foreach (SparqlRemoteEndpoint endpoint in this._endpoints) { //Limit the number of simultaneous requests we make to the user defined level (default 4) //We do this limiting check before trying to issue a request so that when the last request //is issued we'll always drop out of the loop and move onto our WaitAll() while (count >= this._maxSimultaneousRequests) { //First check that the count of active requests is accurate int active = asyncResults.Count(r => !r.IsCompleted); if (active < count) { //Some of the requests have already completed so we don't need to wait count = active; break; } else if (active > count) { //There are more active requests then we thought count = active; } //While the number of requests is at/above the maximum we'll wait for any of the requests to finish //Then we can decrement the count and if this drops back below our maximum then we'll go back into the //main loop and fire off our next request WaitHandle.WaitAny(asyncResults.Select(r => r.AsyncWaitHandle).ToArray()); count--; } //Make an asynchronous query to the next endpoint AsyncQueryWithResultGraph d = new AsyncQueryWithResultGraph(endpoint.QueryWithResultGraph); asyncCalls.Add(d); IAsyncResult asyncResult = d.BeginInvoke(sparqlQuery, null, null); asyncResults.Add(asyncResult); count++; } //Wait for all our requests to finish int waitTimeout = (base.Timeout > 0) ? base.Timeout : System.Threading.Timeout.Infinite; WaitHandle.WaitAll(asyncResults.Select(r => r.AsyncWaitHandle).ToArray(), waitTimeout); //Check for and handle timeouts if (!this._ignoreFailedRequests && !asyncResults.All(r => r.IsCompleted)) { for (int i = 0; i < asyncCalls.Count; i++) { try { asyncCalls[i].EndInvoke(asyncResults[i]); } catch { //Exceptions don't matter as we're just ensuring all the EndInvoke() calls are made } } throw new RdfQueryTimeoutException("Federated Querying failed due to one/more endpoints failing to return results within the Timeout specified which is currently " + (base.Timeout / 1000) + " seconds"); } //Now merge all the results together HashSet <String> varsSeen = new HashSet <string>(); bool cont = true; for (int i = 0; i < asyncCalls.Count; i++) { //Retrieve the result for this call AsyncQueryWithResultGraph call = asyncCalls[i]; IGraph g; try { g = call.EndInvoke(asyncResults[i]); } catch (Exception ex) { if (!this._ignoreFailedRequests) { //Clean up in the event of an error for (int j = i + 1; j < asyncCalls.Count; j++) { try { asyncCalls[j].EndInvoke(asyncResults[j]); } catch { //Exceptions don't matter as we're just ensuring all the EndInvoke() calls are made } } //If a single request fails then the entire query fails throw new RdfQueryException("Federated Querying failed due to the query against the endpoint '" + this._endpoints[i] + "' failing", ex); } else { //If we're ignoring failed requests we continue here continue; } } //Merge the result into the final results //If the handler has previously told us to stop we skip this step if (cont) { handler.StartRdf(); foreach (Triple t in g.Triples) { cont = handler.HandleTriple(t); //Stop if the Handler tells us to if (!cont) { break; } } handler.EndRdf(true); } } }
/// <summary> /// Read RDF/JSON Syntax from some Stream using a RDF Handler /// </summary> /// <param name="handler">RDF Handler to use</param> /// <param name="input">Stream to read from</param> public void Load(IRdfHandler handler, StreamReader input) { if (handler == null) throw new RdfParseException("Cannot read RDF into a null RDF Handler"); if (input == null) throw new RdfParseException("Cannot read RDF from a null Stream"); //Issue a Warning if the Encoding of the Stream is not UTF-8 if (!input.CurrentEncoding.Equals(Encoding.UTF8)) { #if !SILVERLIGHT this.RaiseWarning("Expected Input Stream to be encoded as UTF-8 but got a Stream encoded as " + input.CurrentEncoding.EncodingName + " - Please be aware that parsing errors may occur as a result"); #else this.RaiseWarning("Expected Input Stream to be encoded as UTF-8 but got a Stream encoded as " + input.CurrentEncoding.GetType().Name + " - Please be aware that parsing errors may occur as a result"); #endif } this.Load(handler, (TextReader)input); }
/// <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="callback">Callback to invoke when the operation completes.</param> /// <param name="state">State to pass to the callback.</param> /// <remarks> /// <para> /// Attempts to select the relevant Store Parser based on the Content Type header returned in the HTTP Response. /// </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 LoadDataset(IRdfHandler handler, Uri u, RdfHandlerCallback callback, Object state) { Load(handler, u, (IStoreReader)null, callback, state); }
/// <summary> /// Creates a new Export Progress handler /// </summary> /// <param name="handler">Handler</param> /// <param name="task">Export Task</param> /// <param name="initCount">Initial Count</param> public ExportProgressHandler(IRdfHandler handler, ExportTask task, int initCount) { this._handler = handler; this._task = task; this._count = initCount; }
/// <summary> /// Executes a SPARQL Query on the underlying Store processing the results with 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) { this._queryManager.Query(rdfHandler, resultsHandler, sparqlQuery); }
/// <summary> /// Creates a new Parser Context /// </summary> /// <param name="handler">RDF Handler to use</param> /// <param name="document">HTML Document</param> /// <param name="traceParsing">Whether to Trace Parsing</param> public RdfAParserContext(IRdfHandler handler, HtmlDocument document, bool traceParsing) : base(handler, traceParsing) { this._document = document; }
/// <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> /// <remarks> /// <para> /// Attempts to select the relevant 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 just open a HTTP Stream yourself and pass it to an instance of the correct Parser. /// </para> /// <para> /// In the event that the URI is a File URI the <see cref="FileLoader">FileLoader</see> will be used instead. 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) { UriLoader.Load(handler, u, (IRdfReader)null); }
/// <summary> /// Creates a new StripStringHandler. /// </summary> /// <param name="handler">Inner handler to use.</param> public StripStringHandler(IRdfHandler handler) : base(handler) { _handler = handler ?? throw new ArgumentNullException("handler"); }
/// <summary> /// Attempts to load a RDF dataset from the given URI using a RDF Handler /// </summary> /// <param name="handler">RDF Handler to use</param> /// <param name="u">URI to attempt to get a RDF dataset from</param> /// <param name="parser">Parser to use to parse the RDF dataset</param> /// <remarks> /// <para> /// If the <paramref name="parser"/> parameter is set to null then this method attempts to select the relevant Store Parser based on the Content Type header returned in the HTTP Response. /// </para> /// <para> /// If you know ahead of time the Content Type you can explicitly pass in the parser to use. /// </para> /// </remarks> public static void Load(IRdfHandler handler, Uri u, IStoreReader parser) { if (u == null) { throw new RdfParseException("Cannot read a RDF dataset from a null URI"); } if (handler == null) { throw new RdfParseException("Cannot read a RDF dataset using a null RDF handler"); } try { #if SILVERLIGHT if (u.IsFile()) #else if (u.IsFile) #endif { //Invoke FileLoader instead RaiseWarning("This is a file: URI so invoking the FileLoader instead"); if (Path.DirectorySeparatorChar == '/') { FileLoader.Load(handler, u.ToString().Substring(7), parser); } else { FileLoader.Load(handler, u.ToString().Substring(8), parser); } return; } //Sanitise the URI to remove any Fragment ID u = Tools.StripUriFragment(u); //Set-up the Request HttpWebRequest httpRequest; httpRequest = (HttpWebRequest)WebRequest.Create(u); //Want to ask for TriG, NQuads or TriX if (parser != null) { //If a non-null parser set up a HTTP Header that is just for the given parser httpRequest.Accept = MimeTypesHelper.CustomHttpAcceptHeader(parser); } else { httpRequest.Accept = MimeTypesHelper.HttpRdfDatasetAcceptHeader; } //Use HTTP GET httpRequest.Method = "GET"; #if !SILVERLIGHT httpRequest.Timeout = Options.UriLoaderTimeout; #endif if (_userAgent != null && !_userAgent.Equals(String.Empty)) { httpRequest.UserAgent = _userAgent; } #if DEBUG //HTTP Debugging if (Options.HttpDebugging) { Tools.HttpDebugRequest(httpRequest); } #endif using (HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse()) { #if DEBUG //HTTP Debugging if (Options.HttpDebugging) { Tools.HttpDebugResponse(httpResponse); } #endif //Get a Parser and Load the RDF if (parser == null) { try { parser = MimeTypesHelper.GetStoreParser(httpResponse.ContentType); parser.Warning += RaiseStoreWarning; parser.Load(handler, new StreamParams(httpResponse.GetResponseStream())); } catch (RdfParserSelectionException selEx) { String data = new StreamReader(httpResponse.GetResponseStream()).ReadToEnd(); parser = StringParser.GetDatasetParser(data); parser.Warning += RaiseStoreWarning; parser.Load(handler, new TextReaderParams(new StringReader(data))); } } else { parser.Warning += RaiseStoreWarning; parser.Load(handler, new StreamParams(httpResponse.GetResponseStream())); } } } catch (UriFormatException uriEx) { //Uri Format Invalid throw new RdfException("Unable to load from the given URI '" + u.ToString() + "' since it's format was invalid, see inner exception for details", uriEx); } catch (WebException webEx) { #if DEBUG if (Options.HttpDebugging) { if (webEx.Response != null) { Tools.HttpDebugResponse((HttpWebResponse)webEx.Response); } } #endif //Some sort of HTTP Error occurred throw new WebException("A HTTP Error occurred resolving the URI '" + u.ToString() + "', see innner exception for details", webEx); } }
/// <summary> /// Read RDF/JSON Syntax from some Input using a RDF Handler /// </summary> /// <param name="handler">RDF Handler to use</param> /// <param name="input">Input to read from</param> public void Load(IRdfHandler handler, TextReader input) { if (handler == null) throw new RdfParseException("Cannot read RDF into a null RDF Handler"); if (input == null) throw new RdfParseException("Cannot read RDF from a null Stream"); try { this.Parse(handler, input); } catch { throw; } finally { try { input.Close(); } catch { //Catch is just here in case something goes wrong with closing the stream //This error can be ignored } } }
/// <summary> /// Creates a new Notation 3 Parser Context with default settings /// </summary> /// <param name="handler">RDF Handler to use</param> /// <param name="tokeniser">Tokeniser to use</param> public Notation3ParserContext(IRdfHandler handler, ITokeniser tokeniser) : base(handler, tokeniser) { }
/// <summary> /// Read RDF/JSON Syntax from a file using a RDF Handler /// </summary> /// <param name="handler">RDF Handler to use</param> /// <param name="filename">File to read from</param> public void Load(IRdfHandler handler, String filename) { if (handler == null) throw new RdfParseException("Cannot read RDF into a null RDF Handler"); if (filename == null) throw new RdfParseException("Cannot read RDF from a null File"); this.Load(handler, new StreamReader(filename, Encoding.UTF8)); }
/// <summary> /// Creates a new Notation 3 Parser Context with custom settings /// </summary> /// <param name="handler">RDF Handler to use</param> /// <param name="tokeniser">Tokeniser to use</param> /// <param name="queueMode">Tokeniser Queue Mode</param> /// <param name="traceParsing">Whether to trace parsing</param> /// <param name="traceTokeniser">Whether to trace tokenisation</param> public Notation3ParserContext(IRdfHandler handler, ITokeniser tokeniser, TokenQueueMode queueMode, bool traceParsing, bool traceTokeniser) : base(handler, tokeniser, queueMode, traceParsing, traceTokeniser) { }
/// <summary> /// Internal top level Parse method which parses the Json /// </summary> /// <param name="handler">RDF Handler to use</param> /// <param name="input">Stream to read from</param> private void Parse(IRdfHandler handler, TextReader input) { JsonParserContext context = new JsonParserContext(handler, new CommentIgnoringJsonTextReader(input)); try { context.Handler.StartRdf(); this.ParseGraphObject(context); context.Handler.EndRdf(true); } catch (RdfParsingTerminatedException) { context.Handler.EndRdf(true); //Discard this - it justs means the Handler told us to stop } catch { context.Handler.EndRdf(false); throw; } }