public XmlRdfProcessor(IRdfHandler rdfHandler, XmlPlaceholderResolver entityResolver = null) : base(rdfHandler) { rdf = rdfHandler; linkName = entityResolver?.InstructionTarget; ProcessingInstructionMapping["xml-stylesheet"] = xmlNSUri; rdf.StartRdf(); rdf.HandleNamespace("rdf", new Uri(rdfNS)); rdf.HandleNamespace("rdfs", new Uri(rdfsNS)); rdf.HandleNamespace("xsd", new Uri(xsdNS)); }
/// <summary> /// Handles Namespace Declarations by passing them to the inner handler and cancelling handling if it has been requested. /// </summary> /// <param name="prefix">Namespace Prefix.</param> /// <param name="namespaceUri">Namespace URI.</param> /// <returns></returns> protected override bool HandleNamespaceInternal(string prefix, Uri namespaceUri) { if (_cancelled) { return(false); } return(_handler.HandleNamespace(prefix, namespaceUri)); }
/// <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> /// Applies the triples of a Graph to an RDF Handler. /// </summary> /// <param name="handler">RDF Handler.</param> /// <param name="g">Graph.</param> public static void Apply(this IRdfHandler handler, IGraph g) { try { handler.StartRdf(); if (g != null) { // Handle the Base URI if present if (g.BaseUri != null) { if (!handler.HandleBaseUri(g.BaseUri)) { ParserHelper.Stop(); } } // Handle any namespaces foreach (String prefix in g.NamespaceMap.Prefixes) { if (!handler.HandleNamespace(prefix, g.NamespaceMap.GetNamespaceUri(prefix))) { ParserHelper.Stop(); } } // Finally handle triples foreach (Triple t in g.Triples) { if (!handler.HandleTriple(t)) { ParserHelper.Stop(); } } } handler.EndRdf(true); } catch (RdfParsingTerminatedException) { handler.EndRdf(true); } catch { handler.EndRdf(false); throw; } }
/// <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> /// Delegates namespace handling to inner handler. /// </summary> protected override bool HandleNamespaceInternal(string prefix, Uri namespaceUri) => _handler.HandleNamespace(prefix, namespaceUri);
/// <summary> /// Processes a SPARQL Query sending the results to a RDF/SPARQL 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) { //Do Handler null checks before evaluating the query if (query == null) { throw new ArgumentNullException("query", "Cannot evaluate a null query"); } if (rdfHandler == null && (query.QueryType == SparqlQueryType.Construct || query.QueryType == SparqlQueryType.Describe || query.QueryType == SparqlQueryType.DescribeAll)) { throw new ArgumentNullException("rdfHandler", "Cannot use a null RDF Handler when the Query is a CONSTRUCT/DESCRIBE"); } if (resultsHandler == null && (query.QueryType == SparqlQueryType.Ask || SparqlSpecsHelper.IsSelectQuery(query.QueryType))) { throw new ArgumentNullException("resultsHandler", "Cannot use a null resultsHandler when the Query is an ASK/SELECT"); } //Handle the Thread Safety of the Query Evaluation #if !NO_RWLOCK ReaderWriterLockSlim currLock = (this._dataset is IThreadSafeDataset) ? ((IThreadSafeDataset)this._dataset).Lock : this._lock; try { currLock.EnterReadLock(); #endif //Reset Query Timers query.QueryExecutionTime = null; bool datasetOk = false, defGraphOk = false; try { //Set up the Default and Active Graphs if (query.DefaultGraphs.Any()) { //Call HasGraph() on each Default Graph but ignore the results, we just do this //in case a dataset has any kind of load on demand behaviour foreach (Uri defGraphUri in query.DefaultGraphs) { this._dataset.HasGraph(defGraphUri); } this._dataset.SetDefaultGraph(query.DefaultGraphs); defGraphOk = true; } else if (query.NamedGraphs.Any()) { //No FROM Clauses but one/more FROM NAMED means the Default Graph is the empty graph this._dataset.SetDefaultGraph(Enumerable.Empty <Uri>()); } this._dataset.SetActiveGraph(this._dataset.DefaultGraphUris); datasetOk = true; //Convert to Algebra and execute the Query SparqlEvaluationContext context = this.GetContext(query); BaseMultiset result; try { context.StartExecution(); ISparqlAlgebra algebra = query.ToAlgebra(); result = context.Evaluate(algebra); context.EndExecution(); query.QueryExecutionTime = new TimeSpan(context.QueryTimeTicks); } catch (RdfQueryException) { context.EndExecution(); query.QueryExecutionTime = new TimeSpan(context.QueryTimeTicks); throw; } catch { context.EndExecution(); query.QueryExecutionTime = new TimeSpan(context.QueryTimeTicks); throw; } //Return the Results switch (query.QueryType) { case SparqlQueryType.Ask: case SparqlQueryType.Select: case SparqlQueryType.SelectAll: case SparqlQueryType.SelectAllDistinct: case SparqlQueryType.SelectAllReduced: case SparqlQueryType.SelectDistinct: case SparqlQueryType.SelectReduced: //For SELECT and ASK can populate a Result Set directly from the Evaluation Context //return new SparqlResultSet(context); resultsHandler.Apply(context); break; case SparqlQueryType.Construct: //Create a new Empty Graph for the Results try { rdfHandler.StartRdf(); foreach (String prefix in query.NamespaceMap.Prefixes) { if (!rdfHandler.HandleNamespace(prefix, query.NamespaceMap.GetNamespaceUri(prefix))) { ParserHelper.Stop(); } } //Construct the Triples for each Solution foreach (ISet s in context.OutputMultiset.Sets) { //List<Triple> constructedTriples = new List<Triple>(); try { ConstructContext constructContext = new ConstructContext(rdfHandler, s, false); foreach (IConstructTriplePattern p in query.ConstructTemplate.TriplePatterns.OfType <IConstructTriplePattern>()) { try { if (!rdfHandler.HandleTriple(p.Construct(constructContext))) { ParserHelper.Stop(); } //constructedTriples.Add(((IConstructTriplePattern)p).Construct(constructContext)); } catch (RdfQueryException) { //If we get an error here then we could not construct a specific triple //so we continue anyway } } } catch (RdfQueryException) { //If we get an error here this means we couldn't construct for this solution so the //entire solution is discarded continue; } //h.Assert(constructedTriples); } rdfHandler.EndRdf(true); } catch (RdfParsingTerminatedException) { rdfHandler.EndRdf(true); } catch { rdfHandler.EndRdf(false); throw; } break; case SparqlQueryType.Describe: case SparqlQueryType.DescribeAll: //For DESCRIBE we retrieve the Describe algorithm and apply it ISparqlDescribe describer = query.Describer; describer.Describe(rdfHandler, context); break; default: throw new RdfQueryException("Unknown query types cannot be processed by Leviathan"); } } finally { if (defGraphOk) { this._dataset.ResetDefaultGraph(); } if (datasetOk) { this._dataset.ResetActiveGraph(); } } #if !NO_RWLOCK } finally { currLock.ExitReadLock(); } #endif }