示例#1
0
 /// <summary>
 /// Starts RDF Handling on the inner Handler.
 /// </summary>
 protected override void StartRdfInternal()
 {
     // Note - We don't reset the cancelled flag here as it is possible that in an async environment that
     // Cancel() may get called before handling properly starts at which case we still need to cancel.
     // The cancelled flag only resets when handling ends
     _handler.StartRdf();
 }
示例#2
0
        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));
        }
示例#3
0
        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;
            }
        }
示例#4
0
        /// <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;
            }
        }
示例#5
0
        /// <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;
            }
        }
示例#6
0
        /// <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);
                }
            }
        }
示例#7
0
        /// <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>
        /// 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;
            }
        }
示例#9
0
 /// <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;
     }
 }
示例#10
0
        private void TryParseGraphset(XmlReader reader, IRdfHandler handler)
        {
            try
            {
                handler.StartRdf();

                reader.Read();

                //Skip XML Declaration if present
                if (reader.NodeType == XmlNodeType.XmlDeclaration) reader.Read();

                if (!reader.Name.Equals("TriX"))
                {
                    throw new RdfParseException("Unexpected Document Element '" + reader.Name + "' encountered, expected the Document Element of a TriX Document to be the <TriX> element");
                }

                if (!reader.HasAttributes)
                {
                    throw new RdfParseException("<TriX> fails to define any attributes, the element must define the xmlns attribute to be the TriX namespace");
                }
                else
                {
                    bool trixNSDefined = false;
                    for (int i = 0; i < reader.AttributeCount; i++)
                    {
                        reader.MoveToNextAttribute();

                        if (reader.Name.Equals("xmlns"))
                        {
                            //Ensure that the xmlns attribute is defined and is the TriX namespace
                            if (trixNSDefined) throw new RdfParseException("The xmlns attribute can only occur once on the <TriX> element");
                            if (!reader.Value.Equals(TriXNamespaceURI)) throw new RdfParseException("The xmlns attribute of the <TriX> element must have it's value set to the TriX Namespace URI which is '" + TriXNamespaceURI + "'");
                            trixNSDefined = true;
                        }
                        else if (reader.LocalName.Equals("xmlns"))
                        {
                            //Don't think we need to do anything here
                        }
                    }

                    if (!trixNSDefined) throw new RdfParseException("The <TriX> element fails to define the required xmlns attribute defining the TriX Namespace");
                }

                //Process Child Nodes
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        //For elements we recurse to try and parse a Graph
                        this.TryParseGraph(reader, handler);
                    }
                    else if (reader.NodeType == XmlNodeType.EndElement)
                    {
                        //Stop when we hit an end element
                        break;
                    }
                    else
                    {
                        //Stop if we hit an unexpected element
                        break;
                    }
                }

                //Expect the </TriX> element
                if (reader.NodeType == XmlNodeType.EndElement)
                {
                    if (!reader.Name.Equals("TriX")) throw new RdfParseException("Expected </TriX> element was not found, encountered </" + reader.Name + "> instead");
                }
                else
                {
                    throw new RdfParseException("Unexpected Note Type " + reader.NodeType.ToString() + " encountered, expected a </TriX> element");
                }
                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>
 /// Starts handling of RDF.
 /// </summary>
 protected override void StartRdfInternal()
 {
     base.StartRdfInternal();
     _handler.StartRdf();
 }
示例#12
0
        public void Load(IRdfHandler handler, TextReader input)
        {
            bool finished = false;
            try
            {
                // Tell handler we starting parsing
                handler.StartRdf();

                // Perform actual parsing
                using (JsonReader jsonReader = new JsonTextReader(input))
                {
                    jsonReader.DateParseHandling = DateParseHandling.None;

                    JToken json = JToken.Load(jsonReader);

                    foreach (JObject subjectJObject in json)
                    {
                        string subject = subjectJObject["@id"].ToString();

                        JToken type;
                        if (subjectJObject.TryGetValue("@type", out type))
                        {
                            if (type is JArray)
                            {
                                foreach (JToken t in (JArray) type)
                                {
                                    if (!HandleTriple(handler, subject, "http://www.w3.org/1999/02/22-rdf-syntax-ns#type", t.ToString(), null, false)) return;
                                }
                            }
                            else
                            {
                                if (!HandleTriple(handler, subject, "http://www.w3.org/1999/02/22-rdf-syntax-ns#type", type.ToString(), null, false)) return;
                            }
                        }

                        foreach (JProperty property in subjectJObject.Properties())
                        {
                            if (property.Name == "@id" || property.Name == "@type")
                            {
                                continue;
                            }

                            foreach (JObject objectJObject in property.Value)
                            {
                                JToken id;
                                JToken value;
                                if (objectJObject.TryGetValue("@id", out id))
                                {
                                    if (!HandleTriple(handler, subject, property.Name, id.ToString(), null, false)) return;
                                }
                                else if (objectJObject.TryGetValue("@value", out value))
                                {
                                    string datatype = null;
                                    JToken datatypeJToken;
                                    if (objectJObject.TryGetValue("@type", out datatypeJToken))
                                    {
                                        datatype = datatypeJToken.ToString();
                                    }
                                    else
                                    {
                                        switch (value.Type)
                                        {
                                            case JTokenType.Boolean:
                                                datatype = "http://www.w3.org/2001/XMLSchema#boolean";
                                                break;
                                            case JTokenType.Float:
                                                datatype = "http://www.w3.org/2001/XMLSchema#double";
                                                break;
                                            case JTokenType.Integer:
                                                datatype = "http://www.w3.org/2001/XMLSchema#integer";
                                                break;
                                        }
                                    }

                                    if (!HandleTriple(handler, subject, property.Name, value.ToString(), datatype, true)) return;
                                }
                            }
                        }
                    }
                }

                // Tell handler we've finished parsing
                finished = true;
                handler.EndRdf(true);
            }
            catch
            {
                // Catch all block to fulfill the IRdfHandler contract of informing the handler when the parsing has ended with failure
                finished = true;
                handler.EndRdf(false);
                throw;
            }
            finally
            {
                // Finally block handles the case where we exit the parsing loop early because the handler indicated it did not want
                // to receive further triples.  In this case finished will be set to false and we need to inform the handler we're are done
                if (!finished)
                {
                    handler.EndRdf(true);
                }
            }
        }
示例#13
0
        public void Load(IRdfHandler handler, TextReader input)
        {
            bool finished = false;

            try
            {
                // Tell handler we starting parsing
                handler.StartRdf();

                // Perform actual parsing
                using (JsonReader jsonReader = new JsonTextReader(input))
                {
                    jsonReader.DateParseHandling = DateParseHandling.None;

                    JToken json = JToken.Load(jsonReader);

                    foreach (JObject subjectJObject in json)
                    {
                        string subject = subjectJObject["@id"].ToString();

                        JToken type;
                        if (subjectJObject.TryGetValue("@type", out type))
                        {
                            if (type is JArray)
                            {
                                foreach (JToken t in (JArray)type)
                                {
                                    if (!HandleTriple(handler, subject, "http://www.w3.org/1999/02/22-rdf-syntax-ns#type", t.ToString(), null, false))
                                    {
                                        return;
                                    }
                                }
                            }
                            else
                            {
                                if (!HandleTriple(handler, subject, "http://www.w3.org/1999/02/22-rdf-syntax-ns#type", type.ToString(), null, false))
                                {
                                    return;
                                }
                            }
                        }

                        foreach (JProperty property in subjectJObject.Properties())
                        {
                            if (property.Name == "@id" || property.Name == "@type")
                            {
                                continue;
                            }

                            foreach (JObject objectJObject in property.Value)
                            {
                                JToken id;
                                JToken value;
                                if (objectJObject.TryGetValue("@id", out id))
                                {
                                    if (!HandleTriple(handler, subject, property.Name, id.ToString(), null, false))
                                    {
                                        return;
                                    }
                                }
                                else if (objectJObject.TryGetValue("@value", out value))
                                {
                                    string datatype = null;
                                    JToken datatypeJToken;
                                    if (objectJObject.TryGetValue("@type", out datatypeJToken))
                                    {
                                        datatype = datatypeJToken.ToString();
                                    }
                                    else
                                    {
                                        switch (value.Type)
                                        {
                                        case JTokenType.Boolean:
                                            datatype = "http://www.w3.org/2001/XMLSchema#boolean";
                                            break;

                                        case JTokenType.Float:
                                            datatype = "http://www.w3.org/2001/XMLSchema#double";
                                            break;

                                        case JTokenType.Integer:
                                            datatype = "http://www.w3.org/2001/XMLSchema#integer";
                                            break;
                                        }
                                    }

                                    if (!HandleTriple(handler, subject, property.Name, value.ToString(), datatype, true))
                                    {
                                        return;
                                    }
                                }
                            }
                        }
                    }
                }

                // Tell handler we've finished parsing
                finished = true;
                handler.EndRdf(true);
            }
            catch
            {
                // Catch all block to fulfill the IRdfHandler contract of informing the handler when the parsing has ended with failure
                finished = true;
                handler.EndRdf(false);
                throw;
            }
            finally
            {
                // Finally block handles the case where we exit the parsing loop early because the handler indicated it did not want
                // to receive further triples.  In this case finished will be set to false and we need to inform the handler we're are done
                if (!finished)
                {
                    handler.EndRdf(true);
                }
            }
        }
示例#14
0
        /// <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
        }
        /// <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);
                }
            }
        }
示例#16
0
        /// <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 (_endpoints.Count == 0)
            {
                return;
            }

            // Fire off all the asynchronous requests
            var asyncCalls = new List <Task <IGraph> >();
            var count      = 0;
            var cts        = new CancellationTokenSource();

            foreach (var endpoint in _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 >= _maxSimultaneousRequests)
                {
                    // First check that the count of active requests is accurate
                    int active = asyncCalls.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
                    try
                    {
                        Task.WaitAny(asyncCalls.ToArray());
                    }
                    catch (AggregateException ex)
                    {
                        var faultedTaskIx   = asyncCalls.FindIndex(x => x.IsFaulted);
                        var faultedEndpoint = _endpoints[faultedTaskIx];
                        if (!_ignoreFailedRequests)
                        {
                            throw new RdfQueryException("Federated Querying failed due to the query against the endpoint '" + faultedEndpoint + "' failing", ex);
                        }
                    }

                    count--;
                }

                // Make an asynchronous query to the next endpoint
                asyncCalls.Add(Task.Factory.StartNew(() => endpoint.QueryWithResultGraph(sparqlQuery), cts.Token));
                count++;
            }

            // Wait for all our requests to finish
            var waitTimeout = (Timeout > 0) ? Timeout : System.Threading.Timeout.Infinite;

            // Check for and handle timeouts
            try
            {
                if (!Task.WaitAll(asyncCalls.ToArray(), waitTimeout, cts.Token))
                {
                    // Handle timeouts - cancel overrunning tasks and optionally throw an exception
                    cts.Cancel(false);
                    if (!_ignoreFailedRequests)
                    {
                        throw new RdfQueryTimeoutException(
                                  "Federated Querying failed due to one/more endpoints failing to return results within the Timeout specified which is currently " +
                                  (Timeout / 1000) + " seconds");
                    }
                }
            }
            catch (AggregateException ex)
            {
                if (!_ignoreFailedRequests)
                {
                    // Try to determine which endpoint faulted
                    var faultedTaskIndex   = asyncCalls.FindIndex(t => t.IsFaulted);
                    var faultedEndpointUri = _endpoints[faultedTaskIndex].Uri.AbsoluteUri;
                    throw new RdfQueryException(
                              "Federated querying failed due to the query against the endpoint '" + faultedEndpointUri +
                              "' failing.", ex.InnerException);
                }
            }

            // Now merge all the results together
            var cont = true;

            for (var i = 0; i < asyncCalls.Count; i++)
            {
                if (!asyncCalls[i].IsCompleted)
                {
                    // This is a timed out task that has not transitioned to cancelled state yet
                    // We will only get here if _ignoreFailedRequests is true
                    continue;
                }

                try
                {
                    var g = asyncCalls[i].Result;

                    // 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 (var t in g.Triples)
                        {
                            cont = handler.HandleTriple(t);
                            // Stop if the Handler tells us to
                            if (!cont)
                            {
                                break;
                            }
                        }
                        handler.EndRdf(true);
                    }
                }
                catch (AggregateException ex)
                {
                    if (!_ignoreFailedRequests)
                    {
                        throw new RdfQueryException("Federated Querying failed due to the query against the endpoint '" + _endpoints[i] + "' failing", ex);
                    }
                }
            }
        }
示例#17
0
        private void TryParseGraphset(XmlReader reader, IRdfHandler handler)
        {
            try
            {
                handler.StartRdf();

                reader.Read();

                //Skip XML Declaration if present
                if (reader.NodeType == XmlNodeType.XmlDeclaration)
                {
                    reader.Read();
                }

                if (!reader.Name.Equals("TriX"))
                {
                    throw new RdfParseException("Unexpected Document Element '" + reader.Name + "' encountered, expected the Document Element of a TriX Document to be the <TriX> element");
                }

                if (!reader.HasAttributes)
                {
                    throw new RdfParseException("<TriX> fails to define any attributes, the element must define the xmlns attribute to be the TriX namespace");
                }
                else
                {
                    bool trixNSDefined = false;
                    for (int i = 0; i < reader.AttributeCount; i++)
                    {
                        reader.MoveToNextAttribute();

                        if (reader.Name.Equals("xmlns"))
                        {
                            //Ensure that the xmlns attribute is defined and is the TriX namespace
                            if (trixNSDefined)
                            {
                                throw new RdfParseException("The xmlns attribute can only occur once on the <TriX> element");
                            }
                            if (!reader.Value.Equals(TriXNamespaceURI))
                            {
                                throw new RdfParseException("The xmlns attribute of the <TriX> element must have it's value set to the TriX Namespace URI which is '" + TriXNamespaceURI + "'");
                            }
                            trixNSDefined = true;
                        }
                        else if (reader.LocalName.Equals("xmlns"))
                        {
                            //Don't think we need to do anything here
                        }
                    }

                    if (!trixNSDefined)
                    {
                        throw new RdfParseException("The <TriX> element fails to define the required xmlns attribute defining the TriX Namespace");
                    }
                }

                //Process Child Nodes
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        //For elements we recurse to try and parse a Graph
                        this.TryParseGraph(reader, handler);
                    }
                    else if (reader.NodeType == XmlNodeType.EndElement)
                    {
                        //Stop when we hit an end element
                        break;
                    }
                    else
                    {
                        //Stop if we hit an unexpected element
                        break;
                    }
                }

                //Expect the </TriX> element
                if (reader.NodeType == XmlNodeType.EndElement)
                {
                    if (!reader.Name.Equals("TriX"))
                    {
                        throw new RdfParseException("Expected </TriX> element was not found, encountered </" + reader.Name + "> instead");
                    }
                }
                else
                {
                    throw new RdfParseException("Unexpected Note Type " + reader.NodeType.ToString() + " encountered, expected a </TriX> element");
                }
                handler.EndRdf(true);
            }
            catch (RdfParsingTerminatedException)
            {
                handler.EndRdf(true);
                //Discard this - it justs means the Handler told us to stop
            }
            catch
            {
                handler.EndRdf(false);
                throw;
            }
        }
示例#18
0
        /// <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);
                }
            }
        }
示例#19
0
 /// <summary>
 /// Starts inner handler.
 /// </summary>
 protected override void StartRdfInternal() => _handler.StartRdf();
示例#20
0
        private void TryParseGraphset(XmlDocument doc, IRdfHandler handler)
        {
            try
            {
                handler.StartRdf();

                XmlElement graphsetEl = doc.DocumentElement;
                if (!graphsetEl.Name.Equals("TriX"))
                {
                    throw new RdfParseException("Unexpected Document Element '" + graphsetEl.Name + "' encountered, expected the Document Element of a TriX Document to be the <TriX> element");
                }

                if (!graphsetEl.HasAttributes)
                {
                    throw new RdfParseException("<TriX> fails to define any attributes, the element must define the xmlns attribute to be the TriX namespace");
                }
                else
                {
                    bool trixNSDefined = false;
                    foreach (XmlAttribute attr in graphsetEl.Attributes)
                    {
                        if (attr.Name.Equals("xmlns"))
                        {
                            //Ensure that the xmlns attribute is defined and is the TriX namespace
                            if (trixNSDefined) throw new RdfParseException("The xmlns attribute can only occur once on the <TriX> element");
                            if (!attr.Value.Equals(TriXNamespaceURI)) throw new RdfParseException("The xmlns attribute of the <TriX> element must have it's value set to the TriX Namespace URI which is '" + TriXNamespaceURI + "'");
                            trixNSDefined = true;
                        }
                        else if (attr.LocalName.Equals("xmlns"))
                        {
                            //Don't think we need to do anything here
                        }
                    }

                    if (!trixNSDefined) throw new RdfParseException("The <TriX> element fails to define the required xmlns attribute defining the TriX Namespace");
                }

                //Process Child Nodes
                foreach (XmlNode graph in graphsetEl.ChildNodes)
                {
                    //Ignore non-Element nodes
                    if (graph.NodeType == XmlNodeType.Element)
                    {
                        //OPT: Do this multi-threaded?
                        this.TryParseGraph(graph, handler);
                    }
                }

                handler.EndRdf(true);
            }
            catch (RdfParsingTerminatedException)
            {
                handler.EndRdf(true);
                //Discard this - it justs means the Handler told us to stop
            }
            catch
            {
                handler.EndRdf(false);
                throw;
            }
        }
示例#21
0
        /// <inheritdoc/>
        public void Load(IRdfHandler handler, TextReader input)
        {
            handler.StartRdf();
            var rdfTypeNode = handler.CreateUriNode(new Uri(RdfNs + "type"));

            try
            {
                JToken element;
                using (var reader = new JsonTextReader(input)
                {
                    DateParseHandling = DateParseHandling.None
                })
                {
                    element = JToken.ReadFrom(reader);
                }
                var expandedElement = JsonLdProcessor.Expand(element, ParserOptions);
                var nodeMap         = JsonLdProcessor.GenerateNodeMap(expandedElement);
                foreach (var p in nodeMap.Properties())
                {
                    var graphName = p.Name;
                    var graph     = p.Value as JObject;
                    if (graph == null)
                    {
                        continue;
                    }
                    Uri graphIri;
                    if (graphName == "@default")
                    {
                        graphIri = null;
                    }
                    else
                    {
                        if (!Uri.TryCreate(graphName, UriKind.Absolute, out graphIri))
                        {
                            continue;
                        }
                    }
                    foreach (var gp in graph.Properties())
                    {
                        var   subject = gp.Name;
                        var   node    = gp.Value as JObject;
                        INode subjectNode;
                        if (IsBlankNodeIdentifier(subject))
                        {
                            subjectNode = handler.CreateBlankNode(subject.Substring(2));
                        }
                        else
                        {
                            Uri subjectIri;
                            if (!Uri.TryCreate(subject, UriKind.Absolute, out subjectIri))
                            {
                                continue;
                            }
                            subjectNode = handler.CreateUriNode(subjectIri);
                        }
                        foreach (var np in node.Properties())
                        {
                            var property = np.Name;
                            var values   = np.Value as JArray;
                            if (property.Equals("@type"))
                            {
                                foreach (var type in values)
                                {
                                    var typeNode = MakeNode(handler, type);
                                    handler.HandleTriple(new Triple(subjectNode, rdfTypeNode, typeNode, graphIri));
                                }
                            }
                            else if (JsonLdProcessor.IsKeyword(property))
                            {
                                continue;
                            }
                            else if (JsonLdProcessor.IsBlankNodeIdentifier(property) && !ParserOptions.ProduceGeneralizedRdf)
                            {
                                continue;
                            }
                            else if (JsonLdProcessor.IsRelativeIri(property))
                            {
                                continue;
                            }
                            else
                            {
                                foreach (var item in values)
                                {
                                    var predicateNode = MakeNode(handler, property);
                                    var objectNode    = MakeNode(handler, item);
                                    if (objectNode != null)
                                    {
                                        handler.HandleTriple(new Triple(subjectNode, predicateNode, objectNode,
                                                                        graphIri));
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
                handler.EndRdf(false);
                throw;
            }
            handler.EndRdf(true);
        }
        /// <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 override void LoadGraph(IRdfHandler handler, string graphUri, AsyncStorageCallback callback, object state)
        {
            try
            {
                HttpWebRequest request;
                Dictionary<String, String> serviceParams = new Dictionary<string, string>();
                Uri baseUri = null;

                SparqlParameterizedString construct = new SparqlParameterizedString();
                if (!graphUri.Equals(string.Empty))
                {
                    construct.CommandText = "CONSTRUCT { ?s ?p ?o } WHERE { GRAPH @graph { ?s ?p ?o } }";
                    baseUri = UriFactory.Create(graphUri);
                    construct.SetUri("graph", baseUri);
                }
                else
                {
                    construct.CommandText = "CONSTRUCT { ?s ?p ?o } WHERE { ?s ?p ?o }";
                }
                serviceParams.Add("query", construct.ToString());

                request = this.CreateRequest("/sparql", MimeTypesHelper.HttpAcceptHeader, "GET", serviceParams);

                Tools.HttpDebugRequest(request);

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

                            IRdfReader parser = MimeTypesHelper.GetParser(response.ContentType);
                            parser.Load(handler, new StreamReader(response.GetResponseStream()));
                            if (baseUri != null)
                            {
                                handler.StartRdf();
                                handler.HandleBaseUri(baseUri);
                                handler.EndRdf(true);
                            }
                            response.Close();
                        }

                        callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.LoadWithHandler, handler), state);
                    }
                    catch (WebException webEx)
                    {
                        callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.LoadWithHandler, StorageHelper.HandleHttpError(webEx, "loading a Graph from")), state);
                    }
                    catch (Exception ex)
                    {
                        callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.LoadWithHandler, StorageHelper.HandleError(ex, "loading a Graph from")), state);
                    }
                }, state);
            }
            catch (WebException webEx)
            {
                callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.LoadWithHandler, StorageHelper.HandleHttpError(webEx, "loading a Graph from")), state);
            }
            catch (Exception ex)
            {
                callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.LoadWithHandler, StorageHelper.HandleError(ex, "loading a Graph from")), state);
            }
        }
示例#23
0
        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;
            }
        }
示例#24
0
 /// <summary>
 /// Starts RDF Handler.
 /// </summary>
 protected override void StartRdfInternal()
 {
     _handler.StartRdf();
     _counter = 0;
 }
示例#25
0
        private void TryParseGraphset(XmlDocument doc, IRdfHandler handler)
        {
            try
            {
                handler.StartRdf();

                XmlElement graphsetEl = doc.DocumentElement;
                if (!graphsetEl.Name.Equals("TriX"))
                {
                    throw new RdfParseException("Unexpected Document Element '" + graphsetEl.Name + "' encountered, expected the Document Element of a TriX Document to be the <TriX> element");
                }

                if (!graphsetEl.HasAttributes)
                {
                    throw new RdfParseException("<TriX> fails to define any attributes, the element must define the xmlns attribute to be the TriX namespace");
                }
                else
                {
                    bool trixNSDefined = false;
                    foreach (XmlAttribute attr in graphsetEl.Attributes)
                    {
                        if (attr.Name.Equals("xmlns"))
                        {
                            //Ensure that the xmlns attribute is defined and is the TriX namespace
                            if (trixNSDefined)
                            {
                                throw new RdfParseException("The xmlns attribute can only occur once on the <TriX> element");
                            }
                            if (!attr.Value.Equals(TriXNamespaceURI))
                            {
                                throw new RdfParseException("The xmlns attribute of the <TriX> element must have it's value set to the TriX Namespace URI which is '" + TriXNamespaceURI + "'");
                            }
                            trixNSDefined = true;
                        }
                        else if (attr.LocalName.Equals("xmlns"))
                        {
                            //Don't think we need to do anything here
                        }
                    }

                    if (!trixNSDefined)
                    {
                        throw new RdfParseException("The <TriX> element fails to define the required xmlns attribute defining the TriX Namespace");
                    }
                }

                //Process Child Nodes
                foreach (XmlNode graph in graphsetEl.ChildNodes)
                {
                    //Ignore non-Element nodes
                    if (graph.NodeType == XmlNodeType.Element)
                    {
                        //OPT: Do this multi-threaded?
                        this.TryParseGraph(graph, handler);
                    }
                }

                handler.EndRdf(true);
            }
            catch (RdfParsingTerminatedException)
            {
                handler.EndRdf(true);
                //Discard this - it justs means the Handler told us to stop
            }
            catch
            {
                handler.EndRdf(false);
                throw;
            }
        }
示例#26
0
        /// <summary>
        /// Loads a Graph from the Quad 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)
        {
            if (graphUri == null) throw new RdfStorageException("Cannot load an unnamed Graph from Virtuoso as this would require loading the entirety of the Virtuoso Quad Store into memory!");

            try
            {
                handler.StartRdf();

                //Need to keep Database Open as Literals require extra trips to the Database to get additional
                //information about Language and Type
                this.Open(false);

                DataTable data = this.LoadTriples(graphUri);

                foreach (DataRow row in data.Rows)
                {
                    Object s, p, o;
                    INode subj, pred, obj;

                    //Get Data
                    s = row["S"];
                    p = row["P"];
                    o = row["O"];

                    //Create Nodes
                    subj = this.LoadNode(handler, s);
                    pred = this.LoadNode(handler, p);
                    obj = this.LoadNode(handler, o);

                    //Assert Triple
                    if (!handler.HandleTriple(new Triple(subj, pred, obj))) ParserHelper.Stop();
                }
                handler.EndRdf(true);
                this.Close(false);
            }
            catch (RdfParsingTerminatedException)
            {
                handler.EndRdf(true);
                this.Close(false);
            }
            catch
            {
                handler.EndRdf(false);
                this.Close(true);
                throw;
            }
        }