예제 #1
0
        /// <summary>
        /// Generates Output for Nodes in Turtle syntax
        /// </summary>
        /// <param name="globalContext">Context for writing the Store</param>
        /// <param name="context">Context for writing the Graph</param>
        /// <param name="n">Node to generate output for</param>
        /// <param name="segment">Segment of the Triple being written</param>
        /// <returns></returns>
        private String GenerateNodeOutput(TriGWriterContext globalContext, TurtleWriterContext context, INode n, TripleSegment segment)
        {
            switch (n.NodeType)
            {
            case NodeType.Blank:
                if (segment == TripleSegment.Predicate)
                {
                    throw new RdfOutputException(WriterErrorMessages.BlankPredicatesUnserializable("TriG"));
                }
                break;

            case NodeType.GraphLiteral:
                throw new RdfOutputException(WriterErrorMessages.GraphLiteralsUnserializable("TriG"));

            case NodeType.Literal:
                if (segment == TripleSegment.Subject)
                {
                    throw new RdfOutputException(WriterErrorMessages.LiteralSubjectsUnserializable("TriG"));
                }
                if (segment == TripleSegment.Predicate)
                {
                    throw new RdfOutputException(WriterErrorMessages.LiteralPredicatesUnserializable("TriG"));
                }
                break;

            case NodeType.Uri:
                break;

            default:
                throw new RdfOutputException(WriterErrorMessages.UnknownNodeTypeUnserializable("TriG"));
            }

            return(context.NodeFormatter.Format(n, segment));
        }
예제 #2
0
        /// <summary>
        /// Converts a Node into relevant NTriples Syntax.
        /// </summary>
        /// <param name="n">Node to convert.</param>
        /// <param name="context">Writer Context.</param>
        /// <param name="segment">Triple Segment being written.</param>
        /// <returns></returns>
        private string NodeToNTriples(NTriplesWriterContext context, INode n, TripleSegment segment)
        {
            switch (n.NodeType)
            {
            case NodeType.Blank:
                if (segment == TripleSegment.Predicate)
                {
                    throw new RdfOutputException(WriterErrorMessages.BlankPredicatesUnserializable("NQuads"));
                }
                break;

            case NodeType.Literal:
                if (segment == TripleSegment.Subject)
                {
                    throw new RdfOutputException(WriterErrorMessages.LiteralSubjectsUnserializable("NQuads"));
                }
                if (segment == TripleSegment.Predicate)
                {
                    throw new RdfOutputException(WriterErrorMessages.LiteralPredicatesUnserializable("NQuads"));
                }
                break;

            case NodeType.Uri:
                break;

            case NodeType.GraphLiteral:
                throw new RdfOutputException(WriterErrorMessages.GraphLiteralsUnserializable("NQuads"));

            default:
                throw new RdfOutputException(WriterErrorMessages.UnknownNodeTypeUnserializable("NQuads"));
            }

            return(context.NodeFormatter.Format(n));
        }
예제 #3
0
        private void GeneratePredicateOutput(RdfXmlWriterContext context, Triple t)
        {
            context.NamespaceMap.IncrementNesting();

            // Must ensure a URI predicate
            switch (t.Predicate.NodeType)
            {
            case NodeType.Blank:
                throw new RdfOutputException(WriterErrorMessages.BlankPredicatesUnserializable("RDF/XML"));

            case NodeType.GraphLiteral:
                throw new RdfOutputException(WriterErrorMessages.GraphLiteralsUnserializable("RDF/XML"));

            case NodeType.Literal:
                throw new RdfOutputException(WriterErrorMessages.LiteralPredicatesUnserializable("RDF/XML"));

            case NodeType.Variable:
                throw new RdfOutputException(WriterErrorMessages.VariableNodesUnserializable("RDF/XML"));
            }
            IUriNode p = (IUriNode)t.Predicate;

            // First generate the Predicate Node
            UriRefType outType;
            String     uriref = GenerateUriRef(context, p.Uri, UriRefType.QName, out outType);
            String     tempPrefix = null, tempUri = null;

            if (outType != UriRefType.QName)
            {
                // Need to generate a temporary namespace
                GenerateTemporaryNamespace(context, p, out tempPrefix, out tempUri);
                uriref = GenerateUriRef(context, p.Uri, UriRefType.QName, out outType);
                if (outType != UriRefType.QName)
                {
                    throw new RdfOutputException(WriterErrorMessages.UnreducablePropertyURIUnserializable + " - '" + p.Uri + "'");
                }
            }
            // Use the QName for the Node
            if (uriref.Contains(':'))
            {
                // Create an element in the appropriate namespace
                String ns = context.NamespaceMap.GetNamespaceUri(uriref.Substring(0, uriref.IndexOf(':'))).AbsoluteUri;
                context.Writer.WriteStartElement(uriref.Substring(0, uriref.IndexOf(':')), uriref.Substring(uriref.IndexOf(':') + 1), ns);
            }
            else
            {
                // Create an element in the default namespace
                context.Writer.WriteStartElement(uriref);
            }
            if (tempPrefix != null && tempUri != null)
            {
                context.Writer.WriteAttributeString("xmlns", tempPrefix, null, Uri.EscapeUriString(tempUri));
            }

            // Then generate the Object Output
            GenerateObjectOutput(context, t);

            context.Writer.WriteEndElement();
            context.NamespaceMap.DecrementNesting();
        }
예제 #4
0
        /// <summary>
        /// Saves a Triple Store to TSV format
        /// </summary>
        /// <param name="store">Triple Store to save</param>
        /// <param name="writer">Writer to save to</param>
        public void Save(ITripleStore store, TextWriter writer)
        {
            if (store == null)
            {
                throw new RdfOutputException("Cannot output a null Triple Store");
            }
            if (writer == null)
            {
                throw new RdfOutputException("Cannot output to a null writer");
            }

            ThreadedStoreWriterContext context = new ThreadedStoreWriterContext(store, writer);

            //Check there's something to do
            if (context.Store.Graphs.Count == 0)
            {
                context.Output.Close();
                return;
            }

            //Queue the Graphs to be written
            foreach (IGraph g in context.Store.Graphs)
            {
                context.Add(g.BaseUri);
            }

            //Start making the async calls
            List <IAsyncResult> results = new List <IAsyncResult>();
            SaveGraphsDelegate  d       = new SaveGraphsDelegate(this.SaveGraphs);

            for (int i = 0; i < this._threads; i++)
            {
                results.Add(d.BeginInvoke(context, null, null));
            }

            //Wait for all the async calls to complete
            WaitHandle.WaitAll(results.Select(r => r.AsyncWaitHandle).ToArray());
            RdfThreadedOutputException outputEx = new RdfThreadedOutputException(WriterErrorMessages.ThreadedOutputFailure("TSV"));

            foreach (IAsyncResult result in results)
            {
                try
                {
                    d.EndInvoke(result);
                }
                catch (Exception ex)
                {
                    outputEx.AddException(ex);
                }
            }
            context.Output.Close();

            //If there were any errors we'll throw an RdfThreadedOutputException now
            if (outputEx.InnerExceptions.Any())
            {
                throw outputEx;
            }
        }
예제 #5
0
        private void NodeToTriX(INode n, XmlWriter writer)
        {
            switch (n.NodeType)
            {
            case NodeType.Blank:
                writer.WriteStartElement("id");
                writer.WriteRaw(WriterHelper.EncodeForXml(((IBlankNode)n).InternalID));
                writer.WriteEndElement();
                break;

            case NodeType.GraphLiteral:
                throw new RdfOutputException(WriterErrorMessages.GraphLiteralsUnserializable("TriX"));

            case NodeType.Literal:
                ILiteralNode lit = (ILiteralNode)n;
                if (lit.DataType != null)
                {
                    writer.WriteStartElement("typedLiteral");
                    writer.WriteStartAttribute("datatype");
                    writer.WriteRaw(WriterHelper.EncodeForXml(lit.DataType.AbsoluteUri));
                    writer.WriteEndAttribute();
                    if (lit.DataType.AbsoluteUri.Equals(RdfSpecsHelper.RdfXmlLiteral))
                    {
                        writer.WriteCData(lit.Value);
                    }
                    else
                    {
                        writer.WriteRaw(WriterHelper.EncodeForXml(lit.Value));
                    }
                    writer.WriteEndElement();
                }
                else
                {
                    writer.WriteStartElement("plainLiteral");
                    if (!lit.Language.Equals(String.Empty))
                    {
                        writer.WriteAttributeString("xml", "lang", null, lit.Language);
                    }
                    writer.WriteRaw(WriterHelper.EncodeForXml(lit.Value));
                    writer.WriteEndElement();
                }
                break;

            case NodeType.Uri:
                writer.WriteStartElement("uri");
                writer.WriteRaw(WriterHelper.EncodeForXml(((IUriNode)n).Uri.AbsoluteUri));
                writer.WriteEndElement();
                break;

            default:
                throw new RdfOutputException(WriterErrorMessages.UnknownNodeTypeUnserializable("TriX"));
            }
        }
예제 #6
0
        /// <summary>
        /// Saves the given Triple Store to the Target Store that this class was instantiated with
        /// </summary>
        /// <param name="store">Store to Save</param>
        /// <param name="parameters">Parameters for the Store</param>
        public override void Save(ITripleStore store, IStoreParams parameters)
        {
            if (parameters is ThreadedSqlIOParams)
            {
                //Setup context
                ThreadedSqlStoreWriterContext context = new ThreadedSqlStoreWriterContext(store, (ThreadedSqlIOParams)parameters);
                bool transSetting = context.Manager.DisableTransactions;
                context.Manager.DisableTransactions = true;

                //Queue Graphs for Writing
                foreach (IGraph g in store.Graphs)
                {
                    context.Add(g);
                }

                //Start making the async calls
                List <IAsyncResult> results = new List <IAsyncResult>();
                WriteGraphsDelegate d       = new WriteGraphsDelegate(this.WriteGraphs);
                for (int i = 0; i < context.Threads; i++)
                {
                    results.Add(d.BeginInvoke(context, null, null));
                }

                //Wait for all the async calls to complete
                WaitHandle.WaitAll(results.Select(r => r.AsyncWaitHandle).ToArray());
                RdfThreadedOutputException outputEx = new RdfThreadedOutputException(WriterErrorMessages.ThreadedOutputFailure("Folder Store"));
                foreach (IAsyncResult result in results)
                {
                    try
                    {
                        d.EndInvoke(result);
                    }
                    catch (Exception ex)
                    {
                        outputEx.AddException(ex);
                    }
                }

                //Reset Transactions setting
                context.Manager.DisableTransactions = transSetting;

                //If there were any errors we'll throw an RdfThreadedOutputException now
                if (outputEx.InnerExceptions.Any())
                {
                    throw outputEx;
                }
            }
            else
            {
                throw new RdfStorageException("Parameters for the ThreadedSQLStoreWriter must be of type ThreadedSQLIOParams");
            }
        }
예제 #7
0
        private void GeneratePredicateNode(RdfXmlWriterContext context, INode p)
        {
            switch (p.NodeType)
            {
            case NodeType.GraphLiteral:
                throw new RdfOutputException(WriterErrorMessages.GraphLiteralsUnserializable("RDF/XML"));

            case NodeType.Blank:
                throw new RdfOutputException(WriterErrorMessages.BlankPredicatesUnserializable("RDF/XML"));

            case NodeType.Literal:
                throw new RdfOutputException(WriterErrorMessages.LiteralPredicatesUnserializable("RDF/XML"));

            case NodeType.Uri:
                // OK
                UriRefType rtype;
                String     predRef = this.GenerateUriRef(context, ((IUriNode)p).Uri, UriRefType.QName, out rtype);
                String     prefix, uri;
                prefix = uri = null;
                if (rtype != UriRefType.QName)
                {
                    this.GenerateTemporaryNamespace(context, (IUriNode)p, out prefix, out uri);

                    predRef = this.GenerateUriRef(context, ((IUriNode)p).Uri, UriRefType.QName, out rtype);
                    if (rtype != UriRefType.QName)
                    {
                        throw new RdfOutputException(WriterErrorMessages.UnreducablePropertyURIUnserializable + " - '" + p.ToString() + "'");
                    }
                }

                this.GenerateElement(context, predRef);

                // Add Temporary Namespace to current XML Element
                // CORE-431: This is unecessary and causes malformed XML under monotouch
                // if (prefix != null && uri != null)
                // {
                //    context.Writer.WriteStartAttribute("xmlns", prefix, null);
                //    context.Writer.WriteRaw(Uri.EscapeUriString(WriterHelper.EncodeForXml(uri)));
                //    context.Writer.WriteEndAttribute();
                // }

                break;

            default:
                throw new RdfOutputException(WriterErrorMessages.UnknownNodeTypeUnserializable("RDF/XML"));
            }

            // Write the Predicate
        }
예제 #8
0
        /// <summary>
        /// Saves the given Triple Store to an arbitrary store
        /// </summary>
        /// <param name="store">Store to Save</param>
        /// <param name="parameters">Parameters for the Store</param>
        /// <remarks>
        /// Parameters must be of type <see cref="GenericIOParams">GenericIOParams</see>
        /// </remarks>
        public void Save(ITripleStore store, IStoreParams parameters)
        {
            if (parameters is GenericIOParams)
            {
                //Create the Writer Context
                GenericStoreWriterContext context = new GenericStoreWriterContext(store, (GenericIOParams)parameters);

                //Queue Graphs for Writing
                foreach (IGraph g in store.Graphs)
                {
                    context.Add(g.BaseUri);
                }

                //Start making the async calls
                List <IAsyncResult> results = new List <IAsyncResult>();
                WriteGraphsDelegate d       = new WriteGraphsDelegate(this.WriteGraphs);
                for (int i = 0; i < context.Threads; i++)
                {
                    results.Add(d.BeginInvoke(context, null, null));
                }

                //Wait for all the async calls to complete
                WaitHandle.WaitAll(results.Select(r => r.AsyncWaitHandle).ToArray());
                RdfThreadedOutputException outputEx = new RdfThreadedOutputException(WriterErrorMessages.ThreadedOutputFailure("TSV"));
                foreach (IAsyncResult result in results)
                {
                    try
                    {
                        d.EndInvoke(result);
                    }
                    catch (Exception ex)
                    {
                        outputEx.AddException(ex);
                    }
                }

                //If there were any errors we'll throw an RdfThreadedOutputException now
                if (outputEx.InnerExceptions.Any())
                {
                    throw outputEx;
                }
            }
            else
            {
                throw new RdfStorageException("Parameters for the GenericStoreWriter must be of type GenericIOParams");
            }
        }
예제 #9
0
        /// <summary>
        /// Generates Output for the given Node
        /// </summary>
        /// <param name="context">Writer Context</param>
        /// <param name="n">Node</param>
        /// <param name="segment">Triple Context</param>
        private void GenerateNodeOutput(BaseWriterContext context, INode n, TripleSegment segment)
        {
            switch (n.NodeType)
            {
            case NodeType.GraphLiteral:
                throw new RdfOutputException(WriterErrorMessages.GraphLiteralsUnserializable("TSV"));

            case NodeType.Blank:
            case NodeType.Literal:
            case NodeType.Uri:
                context.Output.Write(this._formatter.Format(n));
                break;

            default:
                throw new RdfOutputException(WriterErrorMessages.UnknownNodeTypeUnserializable("TSV"));
            }
        }
예제 #10
0
        /// <summary>
        /// Generates Output for Nodes in Turtle syntax
        /// </summary>
        /// <param name="context">Writer Context</param>
        /// <param name="n">Node to generate output for</param>
        /// <param name="segment">Segment of the Triple being written</param>
        /// <param name="indent">Indentation</param>
        /// <returns></returns>
        private String GenerateNodeOutput(CompressingTurtleWriterContext context, INode n, TripleSegment segment, int indent)
        {
            StringBuilder output = new StringBuilder();

            switch (n.NodeType)
            {
            case NodeType.Blank:
                if (segment == TripleSegment.Predicate)
                {
                    throw new RdfOutputException(WriterErrorMessages.BlankPredicatesUnserializable("Turtle"));
                }

                if (context.Collections.ContainsKey(n))
                {
                    output.Append(this.GenerateCollectionOutput(context, context.Collections[n], indent));
                }
                else
                {
                    return(context.NodeFormatter.Format(n, segment));
                }
                break;

            case NodeType.GraphLiteral:
                throw new RdfOutputException(WriterErrorMessages.GraphLiteralsUnserializable("Turtle"));

            case NodeType.Literal:
                if (segment == TripleSegment.Subject)
                {
                    throw new RdfOutputException(WriterErrorMessages.LiteralSubjectsUnserializable("Turtle"));
                }
                if (segment == TripleSegment.Predicate)
                {
                    throw new RdfOutputException(WriterErrorMessages.LiteralPredicatesUnserializable("Turtle"));
                }
                return(context.NodeFormatter.Format(n, segment));

            case NodeType.Uri:
                return(context.NodeFormatter.Format(n, segment));

            default:
                throw new RdfOutputException(WriterErrorMessages.UnknownNodeTypeUnserializable("Turtle"));
            }

            return(output.ToString());
        }
예제 #11
0
        private XmlElement GeneratePredicateNode(RdfXmlWriterContext context, INode p, ref int nextNamespaceID, List <String> tempNamespaces, XmlDocument doc, XmlElement subj)
        {
            XmlElement pred;

            switch (p.NodeType)
            {
            case NodeType.GraphLiteral:
                throw new RdfOutputException(WriterErrorMessages.GraphLiteralsUnserializable("RDF/XML"));

            case NodeType.Blank:
                throw new RdfOutputException(WriterErrorMessages.BlankPredicatesUnserializable("RDF/XML"));

            case NodeType.Literal:
                throw new RdfOutputException(WriterErrorMessages.LiteralPredicatesUnserializable("RDF/XML"));

            case NodeType.Uri:
                //OK
                UriRefType rtype;
                String     predRef = this.GenerateUriRef(context, (IUriNode)p, UriRefType.QName, tempNamespaces, out rtype);
                if (rtype != UriRefType.QName)
                {
                    this.GenerateTemporaryNamespace(context, (IUriNode)p, ref nextNamespaceID, tempNamespaces, doc);
                    predRef = this.GenerateUriRef(context, (IUriNode)p, UriRefType.QName, tempNamespaces, out rtype);
                    if (rtype != UriRefType.QName)
                    {
                        throw new RdfOutputException(WriterErrorMessages.UnreducablePropertyURIUnserializable + " - '" + p.ToString() + "'");
                    }
                }

                pred = this.GenerateElement(context, predRef, doc);
                break;

            default:
                throw new RdfOutputException(WriterErrorMessages.UnknownNodeTypeUnserializable("RDF/XML"));
            }

            //Write the Predicate
            subj.AppendChild(pred);
            return(pred);
        }
        /// <summary>
        /// Generates Output for the given Node.
        /// </summary>
        /// <param name="context">Writer Context.</param>
        /// <param name="n">Node.</param>
        /// <param name="segment">Triple Segment.</param>
        private void GenerateNodeOutput(BaseWriterContext context, INode n, TripleSegment segment)
        {
            switch (n.NodeType)
            {
            case NodeType.Blank:
                if (segment == TripleSegment.Predicate)
                {
                    throw new RdfOutputException(WriterErrorMessages.BlankPredicatesUnserializable("CSV"));
                }

                context.Output.Write(_formatter.Format(n));
                break;

            case NodeType.GraphLiteral:
                throw new RdfOutputException(WriterErrorMessages.GraphLiteralsUnserializable("CSV"));

            case NodeType.Literal:
                if (segment == TripleSegment.Subject)
                {
                    throw new RdfOutputException(WriterErrorMessages.LiteralSubjectsUnserializable("CSV"));
                }
                if (segment == TripleSegment.Predicate)
                {
                    throw new RdfOutputException(WriterErrorMessages.LiteralPredicatesUnserializable("CSV"));
                }

                context.Output.Write(_formatter.Format(n));
                break;

            case NodeType.Uri:
                context.Output.Write(_formatter.Format(n));
                break;

            default:
                throw new RdfOutputException(WriterErrorMessages.UnknownNodeTypeUnserializable("CSV"));
            }
        }
예제 #13
0
        /// <summary>
        /// Generates Output for a given Node
        /// </summary>
        /// <param name="context">Writer Context</param>
        /// <param name="n">Node</param>
        /// <param name="t">Triple being written</param>
        private void GenerateNodeOutput(HtmlWriterContext context, INode n, Triple t)
        {
            // Embed RDFa on the Node Output
            bool rdfASerializable = false;

            if (t != null)
            {
                if (t.Predicate.NodeType == NodeType.Uri)
                {
                    // Use @about to specify the Subject
                    if (t.Subject.NodeType == NodeType.Uri)
                    {
                        rdfASerializable = true;
                        context.HtmlWriter.AddAttribute("about", context.UriFormatter.FormatUri(t.Subject.ToString()));
                    }
                    else if (t.Subject.NodeType == NodeType.Blank)
                    {
                        rdfASerializable = true;
                        context.HtmlWriter.AddAttribute("about", "[" + t.Subject.ToString() + "]");
                    }
                    else
                    {
                        RaiseWarning("Cannot serialize a Triple since the Subject is not a URI/Blank Node: " + t.Subject.ToString());
                    }

                    // Then if we can serialize this Triple we serialize the Predicate
                    if (rdfASerializable)
                    {
                        // Get the CURIE for the Predicate
                        String curie;
                        String tempNamespace;
                        if (context.QNameMapper.ReduceToQName(t.Predicate.ToString(), out curie, out tempNamespace))
                        {
                            // Extract the Namespace and make sure it's registered on this Attribute
                            String ns = curie.Substring(0, curie.IndexOf(':'));
                            context.HtmlWriter.AddAttribute("xmlns:" + ns, context.UriFormatter.FormatUri(context.QNameMapper.GetNamespaceUri(ns)));
                        }
                        else
                        {
                            RaiseWarning("Cannot serialize a Triple since the Predicate cannot be reduced to a QName: " + t.Predicate.ToString());
                            rdfASerializable = false;
                        }

                        if (rdfASerializable)
                        {
                            switch (t.Object.NodeType)
                            {
                            case NodeType.Blank:
                            case NodeType.Uri:
                                // If the Object is a URI or a Blank then we specify the predicate with @rel
                                context.HtmlWriter.AddAttribute("rel", curie);
                                break;

                            case NodeType.Literal:
                                // If the Object is a Literal we specify the predicate with @property
                                context.HtmlWriter.AddAttribute("property", curie);
                                break;

                            default:
                                RaiseWarning("Cannot serialize a Triple since the Object is not a URI/Blank/Literal Node: " + t.Object.ToString());
                                rdfASerializable = false;
                                break;
                            }
                        }
                    }
                }
                else
                {
                    RaiseWarning("Cannot serialize a Triple since the Predicate is not a URI Node: " + t.Predicate.ToString());
                }
            }

            String qname;

            switch (n.NodeType)
            {
            case NodeType.Blank:
                if (rdfASerializable)
                {
                    // Need to embed the CURIE for the BNode in the @resource attribute
                    context.HtmlWriter.AddAttribute("resource", "[" + n.ToString() + "]");
                }

                context.HtmlWriter.AddAttribute(HtmlTextWriterAttribute.Class, CssClassBlankNode);
                context.HtmlWriter.RenderBeginTag(HtmlTextWriterTag.Span);
                context.HtmlWriter.WriteEncodedText(n.ToString());
                context.HtmlWriter.RenderEndTag();
                break;

            case NodeType.Literal:
                ILiteralNode lit = (ILiteralNode)n;
                if (lit.DataType != null)
                {
                    if (rdfASerializable)
                    {
                        // Need to embed the datatype in the @datatype attribute
                        String dtcurie, dtnamespace;
                        if (context.QNameMapper.ReduceToQName(lit.DataType.AbsoluteUri, out dtcurie, out dtnamespace))
                        {
                            // Extract the Namespace and make sure it's registered on this Attribute
                            String ns = dtcurie.Substring(0, dtcurie.IndexOf(':'));
                            context.HtmlWriter.AddAttribute("xmlns:" + ns, context.UriFormatter.FormatUri(context.QNameMapper.GetNamespaceUri(ns)));
                            context.HtmlWriter.AddAttribute("datatype", dtcurie);
                        }
                    }

                    context.HtmlWriter.AddAttribute(HtmlTextWriterAttribute.Class, CssClassLiteral);
                    context.HtmlWriter.RenderBeginTag(HtmlTextWriterTag.Span);
                    if (lit.DataType.AbsoluteUri.Equals(Parsing.RdfSpecsHelper.RdfXmlLiteral))
                    {
                        context.HtmlWriter.Write(lit.Value);
                    }
                    else
                    {
                        context.HtmlWriter.WriteEncodedText(lit.Value);
                    }
                    context.HtmlWriter.RenderEndTag();

                    // Output the Datatype
                    context.HtmlWriter.WriteEncodedText("^^");
                    context.HtmlWriter.AddAttribute(HtmlTextWriterAttribute.Href, lit.DataType.AbsoluteUri);
                    context.HtmlWriter.AddAttribute(HtmlTextWriterAttribute.Class, CssClassDatatype);
                    context.HtmlWriter.RenderBeginTag(HtmlTextWriterTag.A);
                    if (context.QNameMapper.ReduceToQName(lit.DataType.AbsoluteUri, out qname))
                    {
                        context.HtmlWriter.WriteEncodedText(qname);
                    }
                    else
                    {
                        context.HtmlWriter.WriteEncodedText(lit.DataType.AbsoluteUri);
                    }
                    context.HtmlWriter.RenderEndTag();
                }
                else
                {
                    if (rdfASerializable)
                    {
                        if (!lit.Language.Equals(String.Empty))
                        {
                            // Need to add the language as an xml:lang attribute
                            context.HtmlWriter.AddAttribute("xml:lang", lit.Language);
                        }
                    }
                    context.HtmlWriter.AddAttribute(HtmlTextWriterAttribute.Class, CssClassLiteral);
                    context.HtmlWriter.RenderBeginTag(HtmlTextWriterTag.Span);
                    context.HtmlWriter.WriteEncodedText(lit.Value);
                    context.HtmlWriter.RenderEndTag();
                    if (!lit.Language.Equals(String.Empty))
                    {
                        context.HtmlWriter.WriteEncodedText("@");
                        context.HtmlWriter.AddAttribute(HtmlTextWriterAttribute.Class, CssClassLangSpec);
                        context.HtmlWriter.RenderBeginTag(HtmlTextWriterTag.Span);
                        context.HtmlWriter.WriteEncodedText(lit.Language);
                        context.HtmlWriter.RenderEndTag();
                    }
                }
                break;

            case NodeType.GraphLiteral:
                // Error
                throw new RdfOutputException(WriterErrorMessages.GraphLiteralsUnserializable("HTML"));

            case NodeType.Uri:
                if (rdfASerializable && !UriPrefix.Equals(String.Empty))
                {
                    // If the URIs are being prefixed with something then we need to set the original
                    // URI in the resource attribute to generate the correct triple
                    context.HtmlWriter.AddAttribute("resource", n.ToString());
                }

                context.HtmlWriter.AddAttribute(HtmlTextWriterAttribute.Class, CssClassUri);
                context.HtmlWriter.AddAttribute(HtmlTextWriterAttribute.Href, UriPrefix + n.ToString());
                context.HtmlWriter.RenderBeginTag(HtmlTextWriterTag.A);
                if (context.QNameMapper.ReduceToQName(n.ToString(), out qname))
                {
                    context.HtmlWriter.WriteEncodedText(qname);
                }
                else
                {
                    context.HtmlWriter.WriteEncodedText(n.ToString());
                }
                context.HtmlWriter.RenderEndTag();
                break;

            default:
                throw new RdfOutputException(WriterErrorMessages.UnknownNodeTypeUnserializable("HTML"));
            }
        }
예제 #14
0
        /// <summary>
        /// Saves the given Store to Disk using the settings the Writer was instantiated with
        /// </summary>
        /// <param name="store">Store to save</param>
        /// <param name="parameters">Parameters indicating where to save to</param>
        public void Save(ITripleStore store, IStoreParams parameters)
        {
            if (parameters is FolderStoreParams)
            {
                //Get the Parameters
                FolderStoreWriterContext context = new FolderStoreWriterContext(store, (FolderStoreParams)parameters);

                //Create the Folder
                if (!Directory.Exists(context.Folder))
                {
                    Directory.CreateDirectory(context.Folder);
                }

                //Write list of Graphs and Queue URIs of Graphs for writing by the async calls
                StreamWriter graphlist = new StreamWriter(Path.Combine(context.Folder, "graphs.fstore"));
                String       ext;
                switch (context.Format)
                {
                case FolderStoreFormat.Turtle:
                    ext = ".ttl";
                    break;

                case FolderStoreFormat.Notation3:
                    ext = ".n3";
                    break;

                case FolderStoreFormat.RdfXml:
                    ext = ".rdf";
                    break;

                default:
                    ext = ".ttl";
                    break;
                }
                graphlist.WriteLine(ext);
                foreach (Uri u in context.Store.Graphs.GraphUris)
                {
                    context.Add(u);
                    graphlist.WriteLine(u.GetSha256Hash() + ext);
                }
                graphlist.Close();

                //Start making the async calls
                List <IAsyncResult> results = new List <IAsyncResult>();
                SaveGraphsDelegate  d       = new SaveGraphsDelegate(this.SaveGraphs);
                for (int i = 0; i < context.Threads; i++)
                {
                    results.Add(d.BeginInvoke(context, null, null));
                }

                //Wait for all the async calls to complete
                WaitHandle.WaitAll(results.Select(r => r.AsyncWaitHandle).ToArray());
                RdfThreadedOutputException outputEx = new RdfThreadedOutputException(WriterErrorMessages.ThreadedOutputFailure("Folder Store"));
                foreach (IAsyncResult result in results)
                {
                    try
                    {
                        d.EndInvoke(result);
                    }
                    catch (Exception ex)
                    {
                        outputEx.AddException(ex);
                    }
                }

                //If there were any errors we'll throw an RdfThreadedOutputException now
                if (outputEx.InnerExceptions.Any())
                {
                    throw outputEx;
                }
            }
            else
            {
                throw new RdfStorageException("Parameters for the FolderStoreWriter must be of type FolderStoreParams");
            }
        }
예제 #15
0
        /// <summary>
        /// Saves a Store in TriG (Turtle with Named Graphs) format
        /// </summary>
        /// <param name="store">Store to save</param>
        /// <param name="parameters">Parameters indicating a Stream to write to</param>
        public void Save(ITripleStore store, IStoreParams parameters)
        {
            //Try and determine the TextWriter to output to
            TriGWriterContext context = null;

            if (parameters is StreamParams)
            {
                //Create a new Writer Context
                ((StreamParams)parameters).Encoding = new UTF8Encoding(Options.UseBomForUtf8);
                context = new TriGWriterContext(store, ((StreamParams)parameters).StreamWriter, this._prettyprint, this._allowHiSpeed, this._compressionLevel, this._n3compat);
            }
            else if (parameters is TextWriterParams)
            {
                context = new TriGWriterContext(store, ((TextWriterParams)parameters).TextWriter, this._prettyprint, this._allowHiSpeed, this._compressionLevel, this._n3compat);
            }

            if (context != null)
            {
                //Check there's something to do
                if (context.Store.Graphs.Count == 0)
                {
                    context.Output.Close();
                    return;
                }

                //Write the Header of the File
                foreach (IGraph g in context.Store.Graphs)
                {
                    context.NamespaceMap.Import(g.NamespaceMap);
                }
                if (context.CompressionLevel > WriterCompressionLevel.None)
                {
                    //Only add @prefix declarations if compression is enabled
                    context.QNameMapper = new ThreadSafeQNameOutputMapper(context.NamespaceMap);
                    foreach (String prefix in context.NamespaceMap.Prefixes)
                    {
                        if (TurtleSpecsHelper.IsValidQName(prefix + ":"))
                        {
                            context.Output.WriteLine("@prefix " + prefix + ": <" + context.FormatUri(context.NamespaceMap.GetNamespaceUri(prefix)) + ">.");
                        }
                    }
                    context.Output.WriteLine();
                }
                else
                {
                    context.QNameMapper = new ThreadSafeQNameOutputMapper(new NamespaceMapper(true));
                }

                if (this._useMultiThreading)
                {
                    //Standard Multi-Threaded Writing

                    //Queue the Graphs to be written
                    foreach (IGraph g in context.Store.Graphs)
                    {
                        if (g.BaseUri == null)
                        {
                            context.Add(new Uri(GraphCollection.DefaultGraphUri));
                        }
                        else
                        {
                            context.Add(g.BaseUri);
                        }
                    }

                    //Start making the async calls
                    List <IAsyncResult> results = new List <IAsyncResult>();
                    SaveGraphsDelegate  d       = new SaveGraphsDelegate(this.SaveGraphs);
                    for (int i = 0; i < this._threads; i++)
                    {
                        results.Add(d.BeginInvoke(context, null, null));
                    }

                    //Wait for all the async calls to complete
                    WaitHandle.WaitAll(results.Select(r => r.AsyncWaitHandle).ToArray());
                    RdfThreadedOutputException outputEx = new RdfThreadedOutputException(WriterErrorMessages.ThreadedOutputFailure("TriG"));
                    foreach (IAsyncResult result in results)
                    {
                        try
                        {
                            d.EndInvoke(result);
                        }
                        catch (Exception ex)
                        {
                            outputEx.AddException(ex);
                        }
                    }
                    //Make sure to close the output
                    context.Output.Close();

                    //If there were any errors we'll throw an RdfThreadedOutputException now
                    if (outputEx.InnerExceptions.Any())
                    {
                        throw outputEx;
                    }
                }
                else
                {
                    try
                    {
                        //Optional Single Threaded Writing
                        foreach (IGraph g in store.Graphs)
                        {
                            TurtleWriterContext graphContext = new TurtleWriterContext(g, new System.IO.StringWriter(), context.PrettyPrint, context.HighSpeedModePermitted);
                            if (context.CompressionLevel > WriterCompressionLevel.None)
                            {
                                graphContext.NodeFormatter = new TurtleFormatter(context.QNameMapper);
                            }
                            else
                            {
                                graphContext.NodeFormatter = new UncompressedTurtleFormatter();
                            }
                            context.Output.WriteLine(this.GenerateGraphOutput(context, graphContext));
                        }

                        //Make sure to close the output
                        context.Output.Close();
                    }
                    catch
                    {
                        try
                        {
                            //Close the output
                            context.Output.Close();
                        }
                        catch
                        {
                            //No catch actions, just cleaning up the output stream
                        }
                        throw;
                    }
                }
            }
            else
            {
                throw new RdfStorageException("Parameters for the TriGWriter must be of the type StreamParams/TextWriterParams");
            }
        }
        /// <summary>
        /// Saves a Triple Store to CSV Format.
        /// </summary>
        /// <param name="store">Triple Store to save.</param>
        /// <param name="writer">Writer to save to.</param>
        /// <param name="leaveOpen">Boolean flag indicating if <paramref name="writer"/> should be left open after the writer completes.</param>
        public void Save(ITripleStore store, TextWriter writer, bool leaveOpen)
        {
            if (store == null)
            {
                throw new RdfOutputException("Cannot output a null Triple Store");
            }
            if (writer == null)
            {
                throw new RdfOutputException("Cannot output to a null writer");
            }

            var context = new ThreadedStoreWriterContext(store, writer);

            // Check there's something to do
            if (context.Store.Graphs.Count == 0)
            {
                if (!leaveOpen)
                {
                    context.Output.Close();
                }
                return;
            }

            // Queue the Graphs to be written
            foreach (var g in context.Store.Graphs)
            {
                context.Add(g.BaseUri);
            }

            // Start making the async calls
            var tasks = new List <Task>();

            for (var i = 0; i < _threads; i++)
            {
                tasks.Add(Task.Factory.StartNew(() => SaveGraphs(context)));
            }

            // Wait for all the async calls to complete
            var outputEx = new RdfThreadedOutputException(WriterErrorMessages.ThreadedOutputFailure("CSV"));

            try
            {
                Task.WaitAll(tasks.ToArray());
            }
            catch (AggregateException ex)
            {
                foreach (var innerEx in ex.InnerExceptions)
                {
                    outputEx.AddException(innerEx);
                }
            }

            if (!leaveOpen)
            {
                context.Output.Close();
            }

            // If there were any errors we'll throw an RdfThreadedOutputException now
            if (outputEx.InnerExceptions.Any())
            {
                throw outputEx;
            }
        }
예제 #17
0
        /// <summary>
        /// Internal method which generates the RDF/Json Output for a Graph
        /// </summary>
        /// <param name="context">Writer Context</param>
        private void GenerateOutput(RdfXmlWriterContext context)
        {
            context.UseDtd = this._useDTD;

            //Create required variables
            int           nextNamespaceID = 0;
            List <String> tempNamespaces  = new List <String>();

            //Always force RDF Namespace to be correctly defined
            context.Graph.NamespaceMap.AddNamespace("rdf", new Uri(NamespaceMapper.RDF));

            //Create an XML Document
            XmlDocument    doc  = new XmlDocument();
            XmlDeclaration decl = doc.CreateXmlDeclaration("1.0", "UTF-8", null);

            doc.AppendChild(decl);

            //Create the DOCTYPE declaration and the rdf:RDF element
            StringBuilder entities = new StringBuilder();
            XmlElement    rdf      = doc.CreateElement("rdf:RDF", NamespaceMapper.RDF);

            if (context.Graph.BaseUri != null)
            {
                XmlAttribute baseUri = doc.CreateAttribute("xml:base");
                baseUri.Value = Uri.EscapeUriString(context.Graph.BaseUri.ToString());
                rdf.Attributes.Append(baseUri);
            }

            XmlAttribute ns;
            String       uri;

            entities.Append('\n');
            foreach (String prefix in context.Graph.NamespaceMap.Prefixes)
            {
                uri = context.Graph.NamespaceMap.GetNamespaceUri(prefix).ToString();
                if (!prefix.Equals(String.Empty))
                {
                    entities.AppendLine("\t<!ENTITY " + prefix + " '" + uri + "'>");
                    ns       = doc.CreateAttribute("xmlns:" + prefix);
                    ns.Value = Uri.EscapeUriString(uri.Replace("'", "&apos;"));
                }
                else
                {
                    ns       = doc.CreateAttribute("xmlns");
                    ns.Value = Uri.EscapeUriString(uri);
                }
                rdf.Attributes.Append(ns);
            }
            if (context.UseDtd)
            {
                XmlDocumentType doctype = doc.CreateDocumentType("rdf:RDF", null, null, entities.ToString());
                doc.AppendChild(doctype);
            }
            doc.AppendChild(rdf);

            //Find the Collections
            if (this._compressionLevel >= WriterCompressionLevel.More)
            {
                WriterHelper.FindCollections(context);
            }

            //Find the Type References
            Dictionary <INode, String> typerefs = this.FindTypeReferences(context, ref nextNamespaceID, tempNamespaces, doc);

            //Get the Triples as a Sorted List
            List <Triple> ts = context.Graph.Triples.Where(t => !context.TriplesDone.Contains(t)).ToList();

            ts.Sort();

            //Variables we need to track our writing
            INode lastSubj, lastPred;

            lastSubj = lastPred = null;
            XmlElement subj, pred;

            //Initialise stuff to keep the compiler happy
            subj = doc.CreateElement("rdf:Description", NamespaceMapper.RDF);
            pred = doc.CreateElement("temp");

            for (int i = 0; i < ts.Count; i++)
            {
                Triple t = ts[i];
                if (context.TriplesDone.Contains(t))
                {
                    continue;                                  //Skip if already done
                }
                if (lastSubj == null || !t.Subject.Equals(lastSubj))
                {
                    //Start a new set of Triples
                    //Validate Subject
                    //Use a Type Reference if applicable
                    if (typerefs.ContainsKey(t.Subject))
                    {
                        String tref = typerefs[t.Subject];
                        String tprefix;
                        if (tref.StartsWith(":"))
                        {
                            tprefix = String.Empty;
                        }
                        else if (tref.Contains(":"))
                        {
                            tprefix = tref.Substring(0, tref.IndexOf(':'));
                        }
                        else
                        {
                            tprefix = String.Empty;
                        }
                        subj = doc.CreateElement(tref, context.Graph.NamespaceMap.GetNamespaceUri(tprefix).ToString());
                    }
                    else
                    {
                        subj = doc.CreateElement("rdf:Description", NamespaceMapper.RDF);
                    }

                    //Write out the Subject
                    doc.DocumentElement.AppendChild(subj);
                    lastSubj = t.Subject;

                    //Apply appropriate attributes
                    switch (t.Subject.NodeType)
                    {
                    case NodeType.GraphLiteral:
                        throw new RdfOutputException(WriterErrorMessages.GraphLiteralsUnserializable("RDF/XML"));

                    case NodeType.Literal:
                        throw new RdfOutputException(WriterErrorMessages.LiteralSubjectsUnserializable("RDF/XML"));

                    case NodeType.Blank:
                        if (context.Collections.ContainsKey(t.Subject))
                        {
                            this.GenerateCollectionOutput(context, t.Subject, subj, ref nextNamespaceID, tempNamespaces, doc);
                        }
                        else
                        {
                            XmlAttribute nodeID = doc.CreateAttribute("rdf:nodeID", NamespaceMapper.RDF);
                            nodeID.Value = ((IBlankNode)t.Subject).InternalID;
                            subj.Attributes.Append(nodeID);
                        }
                        break;

                    case NodeType.Uri:
                        this.GenerateUriOutput(context, (IUriNode)t.Subject, "rdf:about", tempNamespaces, subj, doc);
                        break;

                    default:
                        throw new RdfOutputException(WriterErrorMessages.UnknownNodeTypeUnserializable("RDF/XML"));
                    }

                    //Write the Predicate
                    pred = this.GeneratePredicateNode(context, t.Predicate, ref nextNamespaceID, tempNamespaces, doc, subj);
                    subj.AppendChild(pred);
                    lastPred = t.Predicate;
                }
                else if (lastPred == null || !t.Predicate.Equals(lastPred))
                {
                    //Write the Predicate
                    pred = this.GeneratePredicateNode(context, t.Predicate, ref nextNamespaceID, tempNamespaces, doc, subj);
                    subj.AppendChild(pred);
                    lastPred = t.Predicate;
                }

                //Write the Object
                //Create an Object for the Object
                switch (t.Object.NodeType)
                {
                case NodeType.Blank:
                    if (pred.HasChildNodes || pred.HasAttributes)
                    {
                        //Require a new Predicate
                        pred = this.GeneratePredicateNode(context, t.Predicate, ref nextNamespaceID, tempNamespaces, doc, subj);
                        subj.AppendChild(pred);
                    }

                    if (context.Collections.ContainsKey(t.Object))
                    {
                        //Output a Collection
                        this.GenerateCollectionOutput(context, t.Object, pred, ref nextNamespaceID, tempNamespaces, doc);
                    }
                    else
                    {
                        //Terminate the Blank Node triple by adding a rdf:nodeID attribute
                        XmlAttribute nodeID = doc.CreateAttribute("rdf:nodeID", NamespaceMapper.RDF);
                        nodeID.Value = ((IBlankNode)t.Object).InternalID;
                        pred.Attributes.Append(nodeID);
                    }

                    //Force a new Predicate after Blank Nodes
                    lastPred = null;

                    break;

                case NodeType.GraphLiteral:
                    throw new RdfOutputException(WriterErrorMessages.GraphLiteralsUnserializable("RDF/XML"));

                case NodeType.Literal:
                    ILiteralNode lit = (ILiteralNode)t.Object;

                    if (pred.HasChildNodes || pred.HasAttributes)
                    {
                        //Require a new Predicate
                        pred = this.GeneratePredicateNode(context, t.Predicate, ref nextNamespaceID, tempNamespaces, doc, subj);
                        subj.AppendChild(pred);
                    }

                    this.GenerateLiteralOutput(context, lit, pred, doc);

                    //Force a new Predicate Node after Literals
                    lastPred = null;

                    break;

                case NodeType.Uri:

                    this.GenerateUriOutput(context, (IUriNode)t.Object, "rdf:resource", tempNamespaces, pred, doc);

                    //Force a new Predicate Node after URIs
                    lastPred = null;

                    break;

                default:
                    throw new RdfOutputException(WriterErrorMessages.UnknownNodeTypeUnserializable("RDF/XML"));
                }

                context.TriplesDone.Add(t);
            }

            //Check we haven't failed to output any collections
            foreach (KeyValuePair <INode, OutputRdfCollection> pair in context.Collections)
            {
                if (!pair.Value.HasBeenWritten)
                {
                    if (typerefs.ContainsKey(pair.Key))
                    {
                        String tref = typerefs[pair.Key];
                        String tprefix;
                        if (tref.StartsWith(":"))
                        {
                            tref    = tref.Substring(1);
                            tprefix = String.Empty;
                        }
                        else if (tref.Contains(":"))
                        {
                            tprefix = tref.Substring(0, tref.IndexOf(':'));
                        }
                        else
                        {
                            tprefix = String.Empty;
                        }
                        subj = doc.CreateElement(tref, context.Graph.NamespaceMap.GetNamespaceUri(tprefix).ToString());

                        doc.DocumentElement.AppendChild(subj);

                        this.GenerateCollectionOutput(context, pair.Key, subj, ref nextNamespaceID, tempNamespaces, doc);
                    }
                    else
                    {
                        //Generate an rdf:Description Node with a rdf:nodeID on it
                        XmlElement   colNode = doc.CreateElement("rdf:Description");
                        XmlAttribute nodeID  = doc.CreateAttribute("rdf:nodeID");
                        nodeID.Value = ((IBlankNode)pair.Key).InternalID;
                        colNode.Attributes.Append(nodeID);
                        doc.DocumentElement.AppendChild(colNode);
                        this.GenerateCollectionOutput(context, pair.Key, colNode, ref nextNamespaceID, tempNamespaces, doc);
                        //throw new RdfOutputException("Failed to output a Collection due to an unknown error");
                    }
                }
            }

            //Save to the Output Stream
            InternalXmlWriter writer = new InternalXmlWriter();

            writer.Save(context.Output, doc);

            //Get rid of the Temporary Namespace
            foreach (String tempPrefix in tempNamespaces)
            {
                context.Graph.NamespaceMap.RemoveNamespace(tempPrefix);
            }
        }
예제 #18
0
        /// <summary>
        /// Saves a Store in NQuads format
        /// </summary>
        /// <param name="store">Store to save</param>
        /// <param name="writer">Writer to save to</param>
        /// <param name="leaveOpen">Boolean flag indicating if <paramref name="writer"/> should be left open after the store is written</param>
        public void Save(ITripleStore store, TextWriter writer, bool leaveOpen)
        {
            if (store == null)
            {
                throw new RdfOutputException("Cannot output a null Triple Store");
            }
            if (writer == null)
            {
                throw new RdfOutputException("Cannot output to a null writer");
            }

            var context = new ThreadedStoreWriterContext(store, writer, PrettyPrintMode, false);

            // Check there's something to do
            if (context.Store.Graphs.Count == 0)
            {
                if (!leaveOpen)
                {
                    context.Output.Close();
                }
                return;
            }

            try
            {
                if (UseMultiThreadedWriting)
                {
                    // Queue the Graphs to be written
                    foreach (var g in context.Store.Graphs)
                    {
                        context.Add(g.BaseUri);
                    }

                    // Start making the async calls
                    var results = new List <IAsyncResult>();
                    var d       = new SaveGraphsDelegate(SaveGraphs);
                    for (var i = 0; i < _threads; i++)
                    {
                        results.Add(d.BeginInvoke(context, null, null));
                    }

                    // Wait for all the async calls to complete
                    WaitHandle.WaitAll(results.Select(r => r.AsyncWaitHandle).ToArray());
                    var outputEx = new RdfThreadedOutputException(WriterErrorMessages.ThreadedOutputFailure("TSV"));
                    foreach (var result in results)
                    {
                        try
                        {
                            d.EndInvoke(result);
                        }
                        catch (Exception ex)
                        {
                            outputEx.AddException(ex);
                        }
                    }
                    if (!leaveOpen)
                    {
                        context.Output.Close();
                    }

                    // If there were any errors we'll throw an RdfThreadedOutputException now
                    if (outputEx.InnerExceptions.Any())
                    {
                        throw outputEx;
                    }
                }
                else
                {
                    foreach (var g in context.Store.Graphs)
                    {
                        var graphContext = new NTriplesWriterContext(g, context.Output, NQuadsParser.AsNTriplesSyntax(Syntax));
                        foreach (var t in g.Triples)
                        {
                            context.Output.WriteLine(TripleToNQuads(graphContext, t, g.BaseUri));
                        }
                    }
                    if (!leaveOpen)
                    {
                        context.Output.Close();
                    }
                }
            }
            catch
            {
                try
                {
                    if (!leaveOpen)
                    {
                        context.Output.Close();
                    }
                }
                catch
                {
                    // Just cleaning up
                }
                throw;
            }
        }
예제 #19
0
        /// <summary>
        /// Internal method which generates the RDF/Json Output for a Graph.
        /// </summary>
        /// <param name="g">Graph to save.</param>
        /// <param name="output">Stream to save to.</param>
        private void GenerateOutput(IGraph g, TextWriter output)
        {
            // Get a Blank Node Output Mapper
            BlankNodeOutputMapper bnodeMapper = new BlankNodeOutputMapper(WriterHelper.IsValidBlankNodeID);

            // Get the Writer and Configure Options
            JsonTextWriter writer = new JsonTextWriter(output);

            if (_prettyprint)
            {
                writer.Formatting = Newtonsoft.Json.Formatting.Indented;
            }
            else
            {
                writer.Formatting = Newtonsoft.Json.Formatting.None;
            }

            // Start the overall Object which represents the Graph
            writer.WriteStartObject();

            // Get the Triples as a Sorted List
            List <Triple> ts = g.Triples.ToList();

            ts.Sort(new FullTripleComparer(new FastNodeComparer()));

            // Variables we need to track our writing
            INode lastSubj, lastPred;

            lastSubj = lastPred = null;

            for (int i = 0; i < ts.Count; i++)
            {
                Triple t = ts[i];
                if (lastSubj == null || !t.Subject.Equals(lastSubj))
                {
                    // Terminate previous Triples
                    if (lastSubj != null)
                    {
                        writer.WriteEndArray();
                        writer.WriteEndObject();
                    }

                    // Start a new set of Triples
                    // Validate Subject
                    switch (t.Subject.NodeType)
                    {
                    case NodeType.GraphLiteral:
                        throw new RdfOutputException(WriterErrorMessages.GraphLiteralsUnserializable("RDF/JSON"));

                    case NodeType.Literal:
                        throw new RdfOutputException(WriterErrorMessages.LiteralSubjectsUnserializable("RDF/JSON"));

                    case NodeType.Blank:
                        break;

                    case NodeType.Uri:
                        // OK
                        break;

                    default:
                        throw new RdfOutputException(WriterErrorMessages.UnknownNodeTypeUnserializable("RDF/JSON"));
                    }

                    // Write out the Subject
                    if (t.Subject.NodeType != NodeType.Blank)
                    {
                        writer.WritePropertyName(t.Subject.ToString());
                    }
                    else
                    {
                        // Remap Blank Node IDs as appropriate
                        writer.WritePropertyName("_:" + bnodeMapper.GetOutputID(((IBlankNode)t.Subject).InternalID));
                    }

                    // Start an Object for the Subject
                    writer.WriteStartObject();

                    lastSubj = t.Subject;

                    // Write the first Predicate
                    // Validate Predicate
                    switch (t.Predicate.NodeType)
                    {
                    case NodeType.GraphLiteral:
                        throw new RdfOutputException(WriterErrorMessages.GraphLiteralsUnserializable("RDF/JSON"));

                    case NodeType.Blank:
                        throw new RdfOutputException(WriterErrorMessages.BlankPredicatesUnserializable("RDF/JSON"));

                    case NodeType.Literal:
                        throw new RdfOutputException(WriterErrorMessages.LiteralPredicatesUnserializable("RDF/JSON"));

                    case NodeType.Uri:
                        // OK
                        break;

                    default:
                        throw new RdfOutputException(WriterErrorMessages.UnknownNodeTypeUnserializable("RDF/JSON"));
                    }

                    // Write the Predicate
                    writer.WritePropertyName(t.Predicate.ToString());

                    // Create an Array for the Objects
                    writer.WriteStartArray();

                    lastPred = t.Predicate;
                }
                else if (lastPred == null || !t.Predicate.Equals(lastPred))
                {
                    // Terminate previous Predicate Object list
                    writer.WriteEndArray();

                    // Write the next Predicate
                    // Validate Predicate
                    switch (t.Predicate.NodeType)
                    {
                    case NodeType.GraphLiteral:
                        throw new RdfOutputException(WriterErrorMessages.GraphLiteralsUnserializable("RDF/JSON"));

                    case NodeType.Blank:
                        throw new RdfOutputException(WriterErrorMessages.BlankPredicatesUnserializable("RDF/JSON"));

                    case NodeType.Literal:
                        throw new RdfOutputException(WriterErrorMessages.LiteralPredicatesUnserializable("RDF/JSON"));

                    case NodeType.Uri:
                        // OK
                        break;

                    default:
                        throw new RdfOutputException(WriterErrorMessages.UnknownNodeTypeUnserializable("RDF/JSON"));
                    }

                    // Write the Predicate
                    writer.WritePropertyName(t.Predicate.ToString());

                    // Create an Array for the Objects
                    writer.WriteStartArray();

                    lastPred = t.Predicate;
                }

                // Write the Object
                // Create an Object for the Object
                INode obj = t.Object;
                writer.WriteStartObject();
                writer.WritePropertyName("value");
                switch (obj.NodeType)
                {
                case NodeType.Blank:
                    // Remap Blank Node IDs as appropriate
                    writer.WriteValue("_:" + bnodeMapper.GetOutputID(((IBlankNode)obj).InternalID));
                    writer.WritePropertyName("type");
                    writer.WriteValue("bnode");
                    break;

                case NodeType.GraphLiteral:
                    throw new RdfOutputException(WriterErrorMessages.GraphLiteralsUnserializable("RDF/JSON"));

                case NodeType.Literal:
                    ILiteralNode lit = (ILiteralNode)obj;
                    writer.WriteValue(lit.Value);

                    if (!lit.Language.Equals(String.Empty))
                    {
                        writer.WritePropertyName("lang");
                        writer.WriteValue(lit.Language);
                    }
                    else if (lit.DataType != null)
                    {
                        writer.WritePropertyName("datatype");
                        writer.WriteValue(lit.DataType.AbsoluteUri);
                    }
                    writer.WritePropertyName("type");
                    writer.WriteValue("literal");
                    break;

                case NodeType.Uri:
                    writer.WriteValue(obj.ToString());
                    writer.WritePropertyName("type");
                    writer.WriteValue("uri");
                    break;

                default:
                    throw new RdfOutputException(WriterErrorMessages.UnknownNodeTypeUnserializable("RDF/JSON"));
                }
                writer.WriteEndObject();
            }

            // Terminate the Object which represents the Graph
            writer.WriteEndObject();
        }
예제 #20
0
        /// <summary>
        /// Saves a Store in TriG (Turtle with Named Graphs) format.
        /// </summary>
        /// <param name="store">Store to save.</param>
        /// <param name="writer">Writer to save to.</param>
        /// <param name="leaveOpen">Boolean flag indicating if <paramref name="writer"/> should be left open after the store is saved.</param>
        public void Save(ITripleStore store, TextWriter writer, bool leaveOpen)
        {
            if (store == null)
            {
                throw new RdfOutputException("Cannot output a null Triple Store");
            }
            if (writer == null)
            {
                throw new RdfOutputException("Cannot output to a null writer");
            }

            TriGWriterContext context = new TriGWriterContext(store, writer, _prettyprint, _allowHiSpeed, _compressionLevel, _n3compat);

            // Check there's something to do
            if (context.Store.Graphs.Count == 0)
            {
                if (!leaveOpen)
                {
                    context.Output.Close();
                }
                return;
            }

            // Write the Header of the File
            foreach (var g in context.Store.Graphs)
            {
                context.NamespaceMap.Import(g.NamespaceMap);
            }
            if (context.CompressionLevel > WriterCompressionLevel.None)
            {
                // Only add @prefix declarations if compression is enabled
                context.QNameMapper = new ThreadSafeQNameOutputMapper(context.NamespaceMap);
                foreach (string prefix in context.NamespaceMap.Prefixes)
                {
                    if (TurtleSpecsHelper.IsValidQName(prefix + ":"))
                    {
                        context.Output.WriteLine("@prefix " + prefix + ": <" + context.FormatUri(context.NamespaceMap.GetNamespaceUri(prefix)) + ">.");
                    }
                }
                context.Output.WriteLine();
            }
            else
            {
                context.QNameMapper = new ThreadSafeQNameOutputMapper(new NamespaceMapper(true));
            }

            if (_useMultiThreading)
            {
                // Standard Multi-Threaded Writing

                // Queue the Graphs to be written
                foreach (IGraph g in context.Store.Graphs)
                {
                    context.Add(g.BaseUri);
                }

                // Start making the async calls
                var workers = new Task[_threads];
                for (int i = 0; i < _threads; i++)
                {
                    workers[i] = Task.Factory.StartNew(() => SaveGraphs(context));
                }

                try
                {
                    Task.WaitAll(workers);
                }
                catch (AggregateException ex)
                {
                    var outputException =
                        new RdfThreadedOutputException(WriterErrorMessages.ThreadedOutputFailure("TriG"));
                    foreach (var innerException in ex.InnerExceptions)
                    {
                        outputException.AddException(innerException);
                    }
                }
                finally
                {
                    // Make sure to close the output
                    if (!leaveOpen)
                    {
                        context.Output.Close();
                    }
                }
            }
            else
            {
                try
                {
                    // Optional Single Threaded Writing
                    foreach (IGraph g in store.Graphs)
                    {
                        TurtleWriterContext graphContext = new TurtleWriterContext(g, new System.IO.StringWriter(), context.PrettyPrint, context.HighSpeedModePermitted);
                        if (context.CompressionLevel > WriterCompressionLevel.None)
                        {
                            graphContext.NodeFormatter = new TurtleFormatter(context.QNameMapper);
                        }
                        else
                        {
                            graphContext.NodeFormatter = new UncompressedTurtleFormatter();
                        }
                        context.Output.WriteLine(GenerateGraphOutput(context, graphContext));
                    }

                    // Make sure to close the output
                    if (!leaveOpen)
                    {
                        context.Output.Close();
                    }
                }
                catch
                {
                    try
                    {
                        // Close the output
                        if (!leaveOpen)
                        {
                            context.Output.Close();
                        }
                    }
                    catch
                    {
                        // No catch actions, just cleaning up the output stream
                    }
                    throw;
                }
            }
        }
예제 #21
0
        /// <summary>
        /// Generates Output for Nodes in Notation 3 syntax.
        /// </summary>
        /// <param name="context">Writer Context.</param>
        /// <param name="n">Node to generate output for.</param>
        /// <param name="segment">Segment of the Triple being output.</param>
        /// <param name="indent">Indent to use for pretty printing.</param>
        /// <returns></returns>
        private String GenerateNodeOutput(CompressingTurtleWriterContext context, INode n, TripleSegment segment, int indent)
        {
            StringBuilder output = new StringBuilder();

            switch (n.NodeType)
            {
            case NodeType.Blank:
                if (context.Collections.ContainsKey(n))
                {
                    output.Append(GenerateCollectionOutput(context, context.Collections[n], indent));
                }
                else
                {
                    return(context.NodeFormatter.Format(n, segment));
                }
                break;

            case NodeType.GraphLiteral:
                if (segment == TripleSegment.Predicate)
                {
                    throw new RdfOutputException(WriterErrorMessages.GraphLiteralPredicatesUnserializable("Notation 3"));
                }

                output.Append("{");
                IGraphLiteralNode glit = (IGraphLiteralNode)n;

                StringBuilder temp = new StringBuilder();
                CompressingTurtleWriterContext subcontext = new CompressingTurtleWriterContext(glit.SubGraph, new System.IO.StringWriter(temp));
                subcontext.NodeFormatter = context.NodeFormatter;
                bool contextWritten = false;

                // Write Triples 1 at a Time on a single line
                foreach (Triple t in subcontext.Graph.Triples)
                {
                    if (!contextWritten && t.Context != null && t.Context is VariableContext)
                    {
                        contextWritten = GenerateVariableQuantificationOutput(subcontext, (VariableContext)t.Context);
                        if (contextWritten)
                        {
                            output.Append(temp.ToString());
                        }
                    }

                    output.Append(GenerateNodeOutput(subcontext, t.Subject, TripleSegment.Subject, 0));
                    output.Append(" ");
                    output.Append(GenerateNodeOutput(subcontext, t.Predicate, TripleSegment.Predicate, 0));
                    output.Append(" ");
                    output.Append(GenerateNodeOutput(subcontext, t.Object, TripleSegment.Object, 0));
                    output.Append(". ");
                }

                output.Append("}");
                break;

            case NodeType.Literal:
                if (segment == TripleSegment.Predicate)
                {
                    throw new RdfOutputException(WriterErrorMessages.LiteralPredicatesUnserializable("Notation 3"));
                }
                return(context.NodeFormatter.Format(n, segment));

            case NodeType.Uri:
                return(context.NodeFormatter.Format(n, segment));

            case NodeType.Variable:
                return(context.NodeFormatter.Format(n, segment));

            default:
                throw new RdfOutputException(WriterErrorMessages.UnknownNodeTypeUnserializable("Notation 3"));
            }

            return(output.ToString());
        }
예제 #22
0
        /// <summary>
        /// Saves a Store in NQuads format.
        /// </summary>
        /// <param name="store">Store to save.</param>
        /// <param name="writer">Writer to save to.</param>
        /// <param name="leaveOpen">Boolean flag indicating if <paramref name="writer"/> should be left open after the store is written.</param>
        public void Save(ITripleStore store, TextWriter writer, bool leaveOpen)
        {
            if (store == null)
            {
                throw new RdfOutputException("Cannot output a null Triple Store");
            }
            if (writer == null)
            {
                throw new RdfOutputException("Cannot output to a null writer");
            }

            var context = new ThreadedStoreWriterContext(store, writer, PrettyPrintMode, false);

            // Check there's something to do
            if (context.Store.Graphs.Count == 0)
            {
                if (!leaveOpen)
                {
                    context.Output.Close();
                }
                return;
            }

            try
            {
                if (UseMultiThreadedWriting)
                {
                    // Queue the Graphs to be written
                    foreach (var g in context.Store.Graphs)
                    {
                        context.Add(g.BaseUri);
                    }

                    // Start making the async calls
                    //var results = new List<IAsyncResult>();
                    var workers = new Task[_threads];
                    for (var i = 0; i < _threads; i++)
                    {
                        workers[i] = Task.Factory.StartNew(() => SaveGraphs(context));
                    }

                    try
                    {
                        Task.WaitAll(workers);
                    }
                    catch (AggregateException ex)
                    {
                        var outputException =
                            new RdfThreadedOutputException(WriterErrorMessages.ThreadedOutputFailure("TSV"));
                        foreach (var innerException in ex.InnerExceptions)
                        {
                            outputException.AddException(innerException);
                        }
                    }
                    finally
                    {
                        if (!leaveOpen)
                        {
                            context.Output.Close();
                        }
                    }
                }
                else
                {
                    foreach (var g in context.Store.Graphs)
                    {
                        var graphContext = new NTriplesWriterContext(g, context.Output, NQuadsParser.AsNTriplesSyntax(Syntax));
                        foreach (var t in g.Triples)
                        {
                            context.Output.WriteLine(TripleToNQuads(graphContext, t, g.BaseUri));
                        }
                    }
                    if (!leaveOpen)
                    {
                        context.Output.Close();
                    }
                }
            }
            catch
            {
                try
                {
                    if (!leaveOpen)
                    {
                        context.Output.Close();
                    }
                }
                catch
                {
                    // Just cleaning up
                }
                throw;
            }
        }
예제 #23
0
        private void GenerateCollectionOutput(RdfXmlWriterContext context, INode key, XmlElement pred, ref int nextNamespaceID, List <String> tempNamespaces, XmlDocument doc)
        {
            OutputRdfCollection c = context.Collections[key];

            c.HasBeenWritten = true;

            if (!c.IsExplicit)
            {
                XmlAttribute parseType;
                if (pred.ParentNode != doc.DocumentElement)
                {
                    //Need to set the Predicate to have a rdf:parseType of Resource
                    parseType       = doc.CreateAttribute("rdf:parseType", NamespaceMapper.RDF);
                    parseType.Value = "Resource";
                    pred.Attributes.Append(parseType);
                }

                XmlElement first, rest;
                while (c.Triples.Count > 0)
                {
                    //Get the Next Item and generate rdf:first and rdf:rest Nodes
                    INode next = c.Triples.First().Object;
                    c.Triples.RemoveAt(0);
                    first = doc.CreateElement("rdf:first", NamespaceMapper.RDF);
                    rest  = doc.CreateElement("rdf:rest", NamespaceMapper.RDF);

                    pred.AppendChild(first);
                    pred.AppendChild(rest);

                    //Set the value of the rdf:first Item
                    switch (next.NodeType)
                    {
                    case NodeType.Blank:
                        XmlAttribute nodeID = doc.CreateAttribute("rdf:nodeID", NamespaceMapper.RDF);
                        nodeID.Value = ((IBlankNode)next).InternalID;
                        first.Attributes.Append(nodeID);
                        break;

                    case NodeType.GraphLiteral:
                        throw new RdfOutputException(WriterErrorMessages.GraphLiteralsUnserializable("RDF/XML"));

                    case NodeType.Literal:
                        this.GenerateLiteralOutput(context, (ILiteralNode)next, first, doc);
                        break;

                    case NodeType.Uri:
                        this.GenerateUriOutput(context, (IUriNode)next, "rdf:resource", tempNamespaces, first, doc);
                        break;

                    default:
                        throw new RdfOutputException(WriterErrorMessages.UnknownNodeTypeUnserializable("RDF/XML"));
                    }

                    if (c.Triples.Count >= 1)
                    {
                        //Set Parse Type to resource
                        parseType       = doc.CreateAttribute("rdf:parseType", NamespaceMapper.RDF);
                        parseType.Value = "Resource";
                        rest.Attributes.Append(parseType);

                        pred = rest;
                    }
                    else
                    {
                        //Terminate list with an rdf:nil
                        XmlAttribute res = doc.CreateAttribute("rdf:resource");
                        res.InnerXml = "&rdf;nil";
                        rest.Attributes.Append(res);
                    }
                }
            }
            else
            {
                if (c.Triples.Count == 0)
                {
                    //Terminate the Blank Node triple by adding a rdf:nodeID attribute
                    XmlAttribute nodeID = doc.CreateAttribute("rdf:nodeID", NamespaceMapper.RDF);
                    nodeID.Value = ((IBlankNode)key).InternalID;
                    pred.Attributes.Append(nodeID);
                }
                else
                {
                    //Need to set the Predicate to have a rdf:parseType of Resource
                    if (pred.Name != "rdf:Description" && pred.ParentNode != doc.DocumentElement)
                    {
                        XmlAttribute parseType = doc.CreateAttribute("rdf:parseType", NamespaceMapper.RDF);
                        parseType.Value = "Resource";
                        pred.Attributes.Append(parseType);
                    }

                    //Output the Predicate Object list
                    while (c.Triples.Count > 0)
                    {
                        INode nextPred = c.Triples.First().Predicate;
                        INode nextObj  = c.Triples.First().Object;
                        c.Triples.RemoveAt(0);

                        XmlElement p;

                        //Generate the predicate
                        p = this.GeneratePredicateNode(context, nextPred, ref nextNamespaceID, tempNamespaces, doc, pred);

                        //Output the Object
                        switch (nextObj.NodeType)
                        {
                        case NodeType.Blank:
                            if (context.Collections.ContainsKey(nextObj))
                            {
                                //Output a Collection
                                this.GenerateCollectionOutput(context, nextObj, p, ref nextNamespaceID, tempNamespaces, doc);
                            }
                            else
                            {
                                XmlAttribute nodeID = doc.CreateAttribute("rdf:nodeID", NamespaceMapper.RDF);
                                nodeID.Value = ((IBlankNode)nextObj).InternalID;
                                p.Attributes.Append(nodeID);
                            }
                            break;

                        case NodeType.GraphLiteral:
                            throw new RdfOutputException(WriterErrorMessages.GraphLiteralsUnserializable("RDF/XML"));

                        case NodeType.Literal:
                            this.GenerateLiteralOutput(context, (ILiteralNode)nextObj, p, doc);
                            break;

                        case NodeType.Uri:
                            this.GenerateUriOutput(context, (IUriNode)nextObj, "rdf:resource", tempNamespaces, p, doc);
                            break;

                        default:
                            throw new RdfOutputException(WriterErrorMessages.UnknownNodeTypeUnserializable("RDF/XML"));
                        }
                    }
                }
            }
        }
예제 #24
0
        /// <summary>
        /// Internal method which generates the RDF/Json Output for a Graph
        /// </summary>
        /// <param name="g">Graph to save</param>
        /// <param name="output">Stream to save to</param>
        private void GenerateOutput(IGraph g, TextWriter output)
        {
            // Always force RDF Namespace to be correctly defined
            g.NamespaceMap.Import(this._defaultNamespaces);
            g.NamespaceMap.AddNamespace("rdf", UriFactory.Create(NamespaceMapper.RDF));

            // Create our Writer Context and start the XML Document
            RdfXmlWriterContext context = new RdfXmlWriterContext(g, output);

            context.CompressionLevel = this._compressionLevel;
            context.UseDtd           = this._useDTD;
            context.Writer.WriteStartDocument();

            if (context.UseDtd)
            {
                // Create the DOCTYPE declaration
                StringBuilder entities = new StringBuilder();
                String        uri;
                entities.Append('\n');
                foreach (String prefix in context.NamespaceMap.Prefixes)
                {
                    uri = context.NamespaceMap.GetNamespaceUri(prefix).AbsoluteUri;
                    if (!uri.Equals(context.NamespaceMap.GetNamespaceUri(prefix).ToString()))
                    {
                        context.UseDtd = false;
                        break;
                    }
                    if (!prefix.Equals(String.Empty))
                    {
                        entities.AppendLine("\t<!ENTITY " + prefix + " '" + uri + "'>");
                    }
                }
                if (context.UseDtd)
                {
                    context.Writer.WriteDocType("rdf:RDF", null, null, entities.ToString());
                }
            }

            // Create the rdf:RDF element
            context.Writer.WriteStartElement("rdf", "RDF", NamespaceMapper.RDF);
            if (context.Graph.BaseUri != null)
            {
                context.Writer.WriteAttributeString("xml", "base", null, context.Graph.BaseUri.AbsoluteUri);//Uri.EscapeUriString(context.Graph.BaseUri.ToString()));
            }
            context.NamespaceMap.IncrementNesting();
            foreach (String prefix in context.NamespaceMap.Prefixes)
            {
                if (prefix.Equals("rdf"))
                {
                    continue;
                }

                if (!prefix.Equals(String.Empty))
                {
                    context.Writer.WriteStartAttribute("xmlns", prefix, null);
                    // String nsRef = "&" + prefix + ";";
                    // context.Writer.WriteRaw(nsRef);
                    // context.Writer.WriteEntityRef(prefix);
                    context.Writer.WriteRaw(WriterHelper.EncodeForXml(context.NamespaceMap.GetNamespaceUri(prefix).AbsoluteUri));//Uri.EscapeUriString(WriterHelper.EncodeForXml(context.NamespaceMap.GetNamespaceUri(prefix).AbsoluteUri)));
                    context.Writer.WriteEndAttribute();
                }
                else
                {
                    context.Writer.WriteStartAttribute("xmlns");
                    context.Writer.WriteRaw(WriterHelper.EncodeForXml(context.NamespaceMap.GetNamespaceUri(prefix).AbsoluteUri));//Uri.EscapeUriString(WriterHelper.EncodeForXml(context.NamespaceMap.GetNamespaceUri(prefix).AbsoluteUri)));
                    context.Writer.WriteEndAttribute();
                }
            }

            // Find the Collections and Type References
            if (context.CompressionLevel >= WriterCompressionLevel.More)
            {
                WriterHelper.FindCollections(context, CollectionSearchMode.ImplicitOnly);
                // WriterHelper.FindCollections(context, CollectionSearchMode.All);
            }
            Dictionary <INode, String> typerefs = this.FindTypeReferences(context);

            // Get the Triples as a Sorted List
            List <Triple> ts = context.Graph.Triples.Where(t => !context.TriplesDone.Contains(t)).ToList();

            ts.Sort(new RdfXmlTripleComparer());

            // Variables we need to track our writing
            INode lastSubj, lastPred, lastObj;

            lastSubj = lastPred = lastObj = null;

            for (int i = 0; i < ts.Count; i++)
            {
                Triple t = ts[i];
                if (context.TriplesDone.Contains(t))
                {
                    continue;                                  //Skip if already done
                }
                if (lastSubj == null || !t.Subject.Equals(lastSubj))
                {
                    // Start a new set of Triples
                    if (lastSubj != null)
                    {
                        context.NamespaceMap.DecrementNesting();
                        context.Writer.WriteEndElement();
                    }
                    if (lastPred != null)
                    {
                        context.NamespaceMap.DecrementNesting();
                        context.Writer.WriteEndElement();
                    }

                    // Write out the Subject
                    // Validate Subject
                    // Use a Type Reference if applicable
                    context.NamespaceMap.IncrementNesting();
                    if (typerefs.ContainsKey(t.Subject))
                    {
                        String tref = typerefs[t.Subject];
                        if (tref.StartsWith(":"))
                        {
                            context.Writer.WriteStartElement(tref.Substring(1));
                        }
                        else if (tref.Contains(":"))
                        {
                            context.Writer.WriteStartElement(tref.Substring(0, tref.IndexOf(':')), tref.Substring(tref.IndexOf(':') + 1), null);
                        }
                        else
                        {
                            context.Writer.WriteStartElement(tref);
                        }
                    }
                    else
                    {
                        context.Writer.WriteStartElement("rdf", "Description", NamespaceMapper.RDF);
                    }
                    lastSubj = t.Subject;

                    // Apply appropriate attributes
                    switch (t.Subject.NodeType)
                    {
                    case NodeType.GraphLiteral:
                        throw new RdfOutputException(WriterErrorMessages.GraphLiteralsUnserializable("RDF/XML"));

                    case NodeType.Literal:
                        throw new RdfOutputException(WriterErrorMessages.LiteralSubjectsUnserializable("RDF/XML"));

                    case NodeType.Blank:
                        if (context.Collections.ContainsKey(t.Subject))
                        {
                            this.GenerateCollectionOutput(context, t.Subject);
                        }
                        else
                        {
                            context.Writer.WriteAttributeString("rdf", "nodeID", null, context.BlankNodeMapper.GetOutputID(((IBlankNode)t.Subject).InternalID));
                        }
                        break;

                    case NodeType.Uri:
                        this.GenerateUriOutput(context, (IUriNode)t.Subject, "rdf:about");
                        break;

                    default:
                        throw new RdfOutputException(WriterErrorMessages.UnknownNodeTypeUnserializable("RDF/XML"));
                    }

                    // Write the Predicate
                    context.NamespaceMap.IncrementNesting();
                    this.GeneratePredicateNode(context, t.Predicate);
                    lastPred = t.Predicate;
                    lastObj  = null;
                }
                else if (lastPred == null || !t.Predicate.Equals(lastPred))
                {
                    if (lastPred != null)
                    {
                        context.NamespaceMap.DecrementNesting();
                        context.Writer.WriteEndElement();
                    }

                    // Write the Predicate
                    context.NamespaceMap.IncrementNesting();
                    this.GeneratePredicateNode(context, t.Predicate);
                    lastPred = t.Predicate;
                    lastObj  = null;
                }

                // Write the Object
                if (lastObj != null)
                {
                    // Terminate the previous Predicate Node
                    context.NamespaceMap.DecrementNesting();
                    context.Writer.WriteEndElement();

                    // Start a new Predicate Node
                    context.NamespaceMap.DecrementNesting();
                    context.Writer.WriteEndElement();
                    context.NamespaceMap.IncrementNesting();
                    this.GeneratePredicateNode(context, t.Predicate);
                }
                // Create an Object for the Object
                switch (t.Object.NodeType)
                {
                case NodeType.Blank:
                    if (context.Collections.ContainsKey(t.Object))
                    {
                        // Output a Collection
                        this.GenerateCollectionOutput(context, t.Object);
                    }
                    else
                    {
                        // Terminate the Blank Node triple by adding a rdf:nodeID attribute
                        context.Writer.WriteAttributeString("rdf", "nodeID", null, context.BlankNodeMapper.GetOutputID(((IBlankNode)t.Object).InternalID));
                    }

                    break;

                case NodeType.GraphLiteral:
                    throw new RdfOutputException(WriterErrorMessages.GraphLiteralsUnserializable("RDF/XML"));

                case NodeType.Literal:
                    ILiteralNode lit = (ILiteralNode)t.Object;
                    this.GenerateLiteralOutput(context, lit);

                    break;

                case NodeType.Uri:
                    this.GenerateUriOutput(context, (IUriNode)t.Object, "rdf:resource");
                    break;

                default:
                    throw new RdfOutputException(WriterErrorMessages.UnknownNodeTypeUnserializable("RDF/XML"));
                }
                lastObj = t.Object;

                // Force a new Predicate Node
                context.NamespaceMap.DecrementNesting();
                context.Writer.WriteEndElement();
                lastPred = null;

                context.TriplesDone.Add(t);
            }

            // Check we haven't failed to output any collections
            foreach (KeyValuePair <INode, OutputRdfCollection> pair in context.Collections)
            {
                if (pair.Value.Triples.Count > 0)
                {
                    if (typerefs.ContainsKey(pair.Key))
                    {
                        String tref = typerefs[pair.Key];
                        context.NamespaceMap.IncrementNesting();
                        if (tref.StartsWith(":"))
                        {
                            context.Writer.WriteStartElement(tref.Substring(1));
                        }
                        else if (tref.Contains(":"))
                        {
                            context.Writer.WriteStartElement(tref.Substring(0, tref.IndexOf(':')), tref.Substring(tref.IndexOf(':') + 1), null);
                        }
                        else
                        {
                            context.Writer.WriteStartElement(tref);
                        }

                        this.GenerateCollectionOutput(context, pair.Key);

                        context.Writer.WriteEndElement();
                    }
                    else
                    {
                        context.Writer.WriteStartElement("rdf", "Description", NamespaceMapper.RDF);
                        context.Writer.WriteAttributeString("rdf", "nodeID", NamespaceMapper.RDF, context.BlankNodeMapper.GetOutputID(((IBlankNode)pair.Key).InternalID));
                        this.GenerateCollectionOutput(context, pair.Key);
                        context.Writer.WriteEndElement();
                        // throw new RdfOutputException("Failed to output a Collection due to an unknown error");
                    }
                }
            }

            context.NamespaceMap.DecrementNesting();
            context.Writer.WriteEndDocument();

            // Save to the Output Stream
            context.Writer.Flush();
        }
예제 #25
0
        private void GenerateCollectionOutput(RdfXmlWriterContext context, INode key)
        {
            OutputRdfCollection c = context.Collections[key];

            if (!c.IsExplicit)
            {
                if (context.NamespaceMap.NestingLevel > 2)
                {
                    // Need to set the Predicate to have a rdf:parseType of Resource
                    context.Writer.WriteAttributeString("rdf", "parseType", NamespaceMapper.RDF, "Resource");
                }

                int length = c.Triples.Count;
                while (c.Triples.Count > 0)
                {
                    // Get the Next Item and generate the rdf:first element
                    INode next = c.Triples.First().Object;
                    c.Triples.RemoveAt(0);
                    context.NamespaceMap.IncrementNesting();
                    context.Writer.WriteStartElement("rdf", "first", NamespaceMapper.RDF);

                    // Set the value of the rdf:first Item
                    switch (next.NodeType)
                    {
                    case NodeType.Blank:
                        context.Writer.WriteAttributeString("rdf", "nodeID", NamespaceMapper.RDF, context.BlankNodeMapper.GetOutputID(((IBlankNode)next).InternalID));
                        break;

                    case NodeType.GraphLiteral:
                        throw new RdfOutputException(WriterErrorMessages.GraphLiteralsUnserializable("RDF/XML"));

                    case NodeType.Literal:
                        this.GenerateLiteralOutput(context, (ILiteralNode)next);
                        break;

                    case NodeType.Uri:
                        this.GenerateUriOutput(context, (IUriNode)next, "rdf:resource");
                        break;

                    default:
                        throw new RdfOutputException(WriterErrorMessages.UnknownNodeTypeUnserializable("RDF/XML"));
                    }

                    // Now generate the rdf:rest element
                    context.NamespaceMap.DecrementNesting();
                    context.Writer.WriteEndElement();
                    context.NamespaceMap.IncrementNesting();
                    context.Writer.WriteStartElement("rdf", "rest", NamespaceMapper.RDF);

                    if (c.Triples.Count >= 1)
                    {
                        // Set Parse Type to resource
                        context.Writer.WriteAttributeString("rdf", "parseType", NamespaceMapper.RDF, "Resource");
                    }
                    else
                    {
                        // Terminate list with an rdf:nil
                        context.Writer.WriteStartAttribute("rdf", "resource", NamespaceMapper.RDF);
                        if (context.UseDtd)
                        {
                            context.Writer.WriteRaw("&rdf;nil");
                        }
                        else
                        {
                            context.Writer.WriteRaw(NamespaceMapper.RDF + "nil");
                        }
                        context.Writer.WriteEndAttribute();
                    }
                }
                for (int i = 0; i < length; i++)
                {
                    context.NamespaceMap.DecrementNesting();
                    context.Writer.WriteEndElement();
                }
            }
            else
            {
                if (c.Triples.Count == 0)
                {
                    // Terminate the Blank Node triple by adding a rdf:nodeID attribute
                    context.Writer.WriteAttributeString("rdf", "nodeID", NamespaceMapper.RDF, context.BlankNodeMapper.GetOutputID(((IBlankNode)key).InternalID));
                }
                else
                {
                    // Need to set the Predicate to have a rdf:parseType of Resource
                    if (context.NamespaceMap.NestingLevel > 2)
                    {
                        // Need to set the Predicate to have a rdf:parseType of Resource
                        context.Writer.WriteAttributeString("rdf", "parseType", NamespaceMapper.RDF, "Resource");
                    }

                    // Output the Predicate Object list
                    while (c.Triples.Count > 0)
                    {
                        Triple t = c.Triples[0];
                        c.Triples.RemoveAt(0);
                        INode nextPred = t.Predicate;
                        INode nextObj  = t.Object;

                        // Generate the predicate
                        this.GeneratePredicateNode(context, nextPred);

                        // Output the Object
                        switch (nextObj.NodeType)
                        {
                        case NodeType.Blank:
                            if (context.Collections.ContainsKey(nextObj))
                            {
                                // Output a Collection
                                this.GenerateCollectionOutput(context, nextObj);
                            }
                            else
                            {
                                context.Writer.WriteAttributeString("rdf", "nodeID", NamespaceMapper.RDF, context.BlankNodeMapper.GetOutputID(((IBlankNode)key).InternalID));
                            }
                            break;

                        case NodeType.GraphLiteral:
                            throw new RdfOutputException(WriterErrorMessages.GraphLiteralsUnserializable("RDF/XML"));

                        case NodeType.Literal:
                            this.GenerateLiteralOutput(context, (ILiteralNode)nextObj);
                            break;

                        case NodeType.Uri:
                            this.GenerateUriOutput(context, (IUriNode)nextObj, "rdf:resource");
                            break;

                        default:
                            throw new RdfOutputException(WriterErrorMessages.UnknownNodeTypeUnserializable("RDF/XML"));
                        }

                        context.Writer.WriteEndElement();
                    }
                }
            }
        }
예제 #26
0
        /// <summary>
        /// Saves a SPARQL Result Set to TSV format
        /// </summary>
        /// <param name="results">Result Set</param>
        /// <param name="output">Writer to save to</param>
        public void Save(SparqlResultSet results, TextWriter output)
        {
            try
            {
                if (results.ResultsType == SparqlResultsType.VariableBindings)
                {
                    //Output Variables first
                    String[] vars = results.Variables.ToArray();
                    for (int i = 0; i < vars.Length; i++)
                    {
                        output.Write("?" + vars[i]);
                        if (i < vars.Length - 1)
                        {
                            output.Write('\t');
                        }
                    }
                    output.Write('\n');

                    foreach (SparqlResult result in results)
                    {
                        for (int i = 0; i < vars.Length; i++)
                        {
                            if (result.HasValue(vars[i]))
                            {
                                INode temp = result[vars[i]];
                                if (temp != null)
                                {
                                    switch (temp.NodeType)
                                    {
                                    case NodeType.GraphLiteral:
                                        throw new RdfOutputException(WriterErrorMessages.GraphLiteralsUnserializable("SPARQL TSV"));

                                    case NodeType.Blank:
                                    case NodeType.Literal:
                                    case NodeType.Uri:
                                        output.Write(this._formatter.Format(temp));
                                        break;

                                    default:
                                        throw new RdfOutputException(WriterErrorMessages.UnknownNodeTypeUnserializable("SPARQL TSV"));
                                    }
                                }
                            }
                            if (i < vars.Length - 1)
                            {
                                output.Write('\t');
                            }
                        }
                        output.Write('\n');
                    }
                }
                else
                {
                    output.Write(results.Result.ToString());
                }

                output.Close();
            }
            catch
            {
                try
                {
                    output.Close();
                }
                catch
                {
                    //No error handling, just trying to clean up
                }
                throw;
            }
        }
예제 #27
0
        /// <summary>
        /// Internal method which generates the RDF/Json Output for a Graph.
        /// </summary>
        /// <param name="g">Graph to save.</param>
        /// <param name="output">Stream to save to.</param>
        private void GenerateOutput(IGraph g, TextWriter output)
        {
            // Always force RDF Namespace to be correctly defined
            g.NamespaceMap.Import(_defaultNamespaces);
            g.NamespaceMap.AddNamespace("rdf", UriFactory.Create(NamespaceMapper.RDF));

            // Create our Writer Context and start the XML Document
            RdfXmlWriterContext context = new RdfXmlWriterContext(g, output);

            context.CompressionLevel = _compressionLevel;
            context.UseDtd           = _useDTD;
            context.UseAttributes    = _useAttributes;
            context.Writer.WriteStartDocument();

            if (context.UseDtd)
            {
                // Create the DOCTYPE declaration
                StringBuilder entities = new StringBuilder();
                String        uri;
                entities.Append('\n');
                foreach (String prefix in context.NamespaceMap.Prefixes)
                {
                    uri = context.NamespaceMap.GetNamespaceUri(prefix).AbsoluteUri;
                    if (!uri.Equals(context.NamespaceMap.GetNamespaceUri(prefix).ToString()))
                    {
                        context.UseDtd = false;
                        break;
                    }
                    if (!prefix.Equals(String.Empty))
                    {
                        entities.AppendLine("\t<!ENTITY " + prefix + " '" + uri + "'>");
                    }
                }
                if (context.UseDtd)
                {
                    context.Writer.WriteDocType("rdf:RDF", null, null, entities.ToString());
                }
            }

            // Create the rdf:RDF element
            context.Writer.WriteStartElement("rdf", "RDF", NamespaceMapper.RDF);
            if (context.Graph.BaseUri != null)
            {
                context.Writer.WriteAttributeString("xml", "base", null, context.Graph.BaseUri.AbsoluteUri);
            }

            // Add all the existing Namespace Definitions here
            context.NamespaceMap.IncrementNesting();
            foreach (String prefix in context.NamespaceMap.Prefixes)
            {
                if (prefix.Equals("rdf"))
                {
                    continue;
                }

                if (!prefix.Equals(String.Empty))
                {
                    context.Writer.WriteStartAttribute("xmlns", prefix, null);
                    context.Writer.WriteString(context.NamespaceMap.GetNamespaceUri(prefix).AbsoluteUri);//Uri.EscapeUriString(context.NamespaceMap.GetNamespaceUri(prefix).AbsoluteUri));
                    context.Writer.WriteEndAttribute();
                }
                else
                {
                    context.Writer.WriteStartAttribute("xmlns");
                    context.Writer.WriteString(context.NamespaceMap.GetNamespaceUri(prefix).AbsoluteUri);//Uri.EscapeUriString(context.NamespaceMap.GetNamespaceUri(prefix).AbsoluteUri));
                    context.Writer.WriteEndAttribute();
                }
            }

            // Find the Collections and Type References
            if (context.CompressionLevel >= WriterCompressionLevel.More)
            {
                WriterHelper.FindCollections(context, CollectionSearchMode.All);
            }

            // Get the Triples as a Sorted List
            List <Triple> ts = context.Graph.Triples.Where(t => !context.TriplesDone.Contains(t)).ToList();

            ts.Sort(new RdfXmlTripleComparer());


            INode         lastSubj    = null;
            List <Triple> sameSubject = new List <Triple>();

            for (int i = 0; i < ts.Count; i++)
            {
                // Find the first group of Triples with the same subject
                if (lastSubj == null)
                {
                    // Start of new set of Triples with the same subject
                    lastSubj = ts[i].Subject;
                    sameSubject.Add(ts[i]);

                    if (lastSubj.NodeType == NodeType.GraphLiteral)
                    {
                        throw new RdfOutputException(WriterErrorMessages.GraphLiteralsUnserializable("RDF/XML"));
                    }
                    else if (lastSubj.NodeType == NodeType.Variable)
                    {
                        throw new RdfOutputException(WriterErrorMessages.VariableNodesUnserializable("RDF/XML"));
                    }
                }
                else
                {
                    if (ts[i].Subject.Equals(lastSubj))
                    {
                        // Still finding Triples with same subject
                        sameSubject.Add(ts[i]);
                    }
                    else
                    {
                        // Found the end of current set of Triples with same subject
                        GenerateSubjectOutput(context, sameSubject, true);

                        // Reset so we'll start from a new subject on next iteration
                        sameSubject.Clear();
                        lastSubj = null;
                        i--;
                    }
                }
            }
            // Ensure last set of Triples with same subject gets written
            if (sameSubject.Count > 0)
            {
                GenerateSubjectOutput(context, sameSubject, true);
            }

            // Take care of any collections that weren't yet written
            foreach (KeyValuePair <INode, OutputRdfCollection> kvp in context.Collections)
            {
                if (!kvp.Value.HasBeenWritten)
                {
                    // Generate a rdf:Description node and then write the collection
                    context.Writer.WriteStartElement("rdf", "Description", NamespaceMapper.RDF);
                    if (kvp.Value.Triples.Count > 0 || !kvp.Value.IsExplicit)
                    {
                        context.Writer.WriteAttributeString("rdf", "nodeID", NamespaceMapper.RDF, context.BlankNodeMapper.GetOutputID(((IBlankNode)kvp.Key).InternalID));
                    }
                    GenerateCollectionOutput(context, kvp.Key);
                    context.Writer.WriteEndElement();
                }
            }

            context.NamespaceMap.DecrementNesting();
            context.Writer.WriteEndDocument();

            // Save to the Output Stream
            context.Writer.Flush();
        }
예제 #28
0
        /// <summary>
        /// Saves a Store in NQuads format
        /// </summary>
        /// <param name="store">Store to save</param>
        /// <param name="parameters">Parameters indicating a Stream to write to</param>
        public void Save(ITripleStore store, IStoreParams parameters)
        {
            ThreadedStoreWriterContext context = null;

            if (parameters is StreamParams)
            {
                //Create a new Writer Context
#if !SILVERLIGHT
                ((StreamParams)parameters).Encoding = Encoding.ASCII;
#endif
                context = new ThreadedStoreWriterContext(store, ((StreamParams)parameters).StreamWriter, this._prettyPrint, false);
            }
            else if (parameters is TextWriterParams)
            {
                context = new ThreadedStoreWriterContext(store, ((TextWriterParams)parameters).TextWriter, this._prettyPrint, false);
            }

            if (context != null)
            {
                //Check there's something to do
                if (context.Store.Graphs.Count == 0)
                {
                    context.Output.Close();
                    return;
                }

                try
                {
                    if (this._multiThreaded)
                    {
                        //Queue the Graphs to be written
                        foreach (IGraph g in context.Store.Graphs)
                        {
                            if (g.BaseUri == null)
                            {
                                context.Add(UriFactory.Create(GraphCollection.DefaultGraphUri));
                            }
                            else
                            {
                                context.Add(g.BaseUri);
                            }
                        }

                        //Start making the async calls
                        List <IAsyncResult> results = new List <IAsyncResult>();
                        SaveGraphsDelegate  d       = new SaveGraphsDelegate(this.SaveGraphs);
                        for (int i = 0; i < this._threads; i++)
                        {
                            results.Add(d.BeginInvoke(context, null, null));
                        }

                        //Wait for all the async calls to complete
                        WaitHandle.WaitAll(results.Select(r => r.AsyncWaitHandle).ToArray());
                        RdfThreadedOutputException outputEx = new RdfThreadedOutputException(WriterErrorMessages.ThreadedOutputFailure("TSV"));
                        foreach (IAsyncResult result in results)
                        {
                            try
                            {
                                d.EndInvoke(result);
                            }
                            catch (Exception ex)
                            {
                                outputEx.AddException(ex);
                            }
                        }
                        context.Output.Close();

                        //If there were any errors we'll throw an RdfThreadedOutputException now
                        if (outputEx.InnerExceptions.Any())
                        {
                            throw outputEx;
                        }
                    }
                    else
                    {
                        foreach (IGraph g in context.Store.Graphs)
                        {
                            NTriplesWriterContext graphContext = new NTriplesWriterContext(g, context.Output);
                            foreach (Triple t in g.Triples)
                            {
                                context.Output.WriteLine(this.TripleToNQuads(graphContext, t));
                            }
                        }
                        context.Output.Close();
                    }
                }
                catch
                {
                    try
                    {
                        context.Output.Close();
                    }
                    catch
                    {
                        //Just cleaning up
                    }
                    throw;
                }
            }
            else
            {
                throw new RdfStorageException("Parameters for the NQuadsWriter must be of the type StreamParams/TextWriterParams");
            }
        }
예제 #29
0
        private void GenerateObjectOutput(RdfXmlWriterContext context, Triple t)
        {
            // Take different actions depending on the Node Type to be written
            switch (t.Object.NodeType)
            {
            case NodeType.Blank:
                if (context.Collections.ContainsKey(t.Object))
                {
                    // Blank Node has a collection associated with it
                    GenerateCollectionOutput(context, t.Object);
                }
                else
                {
                    // Isolated Blank Node so use nodeID
                    context.Writer.WriteAttributeString("rdf", "nodeID", NamespaceMapper.RDF, context.BlankNodeMapper.GetOutputID(((IBlankNode)t.Object).InternalID));
                }
                break;

            case NodeType.GraphLiteral:
                throw new RdfOutputException(WriterErrorMessages.GraphLiteralsUnserializable("RDF/XML"));

            case NodeType.Literal:
                // Write as content of the current element
                ILiteralNode lit = (ILiteralNode)t.Object;
                if (lit.DataType != null)
                {
                    if (lit.DataType.AbsoluteUri.Equals(RdfSpecsHelper.RdfXmlLiteral))
                    {
                        // XML Literal
                        context.Writer.WriteAttributeString("rdf", "parseType", NamespaceMapper.RDF, "Literal");
                        context.Writer.WriteRaw(lit.Value);
                    }
                    else
                    {
                        // Datatyped Literal
                        context.Writer.WriteAttributeString("rdf", "datatype", NamespaceMapper.RDF, lit.DataType.AbsoluteUri);    //Uri.EscapeUriString(lit.DataType.ToString()));
                        context.Writer.WriteString(lit.Value);
                    }
                }
                else if (!lit.Language.Equals(String.Empty))
                {
                    // Language specified Literal
                    context.Writer.WriteAttributeString("xml", "lang", null, lit.Language);
                    context.Writer.WriteString(lit.Value);
                }
                else
                {
                    // Simple Literal
                    context.Writer.WriteString(lit.Value);
                }
                break;

            case NodeType.Uri:
                // Simple rdf:resource
                // TODO: Compress this into UriRef where possible
                context.Writer.WriteAttributeString("rdf", "resource", NamespaceMapper.RDF, t.Object.ToString());    //Uri.EscapeUriString(t.Object.ToString()));
                break;

            case NodeType.Variable:
                throw new RdfOutputException(WriterErrorMessages.VariableNodesUnserializable("RDF/XML"));
            }
        }
예제 #30
0
        /// <summary>
        /// Method which generates the RDF Graph of a SPARQL Result Set
        /// </summary>
        /// <param name="results">Result Set</param>
        /// <returns></returns>
        public IGraph GenerateOutput(SparqlResultSet results)
        {
            // Create the Graph for the Output
            IGraph g = new Graph();

            // Add the relevant namespaces
            g.NamespaceMap.AddNamespace("rs", UriFactory.Create(SparqlSpecsHelper.SparqlRdfResultsNamespace));

            // Create relevant Nodes
            IUriNode rdfType        = g.CreateUriNode("rdf:type");
            IUriNode resultSetClass = g.CreateUriNode("rs:ResultSet");
            IUriNode resultVariable = g.CreateUriNode("rs:resultVariable");
            IUriNode solution       = g.CreateUriNode("rs:solution");
            IUriNode binding        = g.CreateUriNode("rs:binding");
            IUriNode value          = g.CreateUriNode("rs:value");
            IUriNode variable       = g.CreateUriNode("rs:variable");
            IUriNode boolean        = g.CreateUriNode("rs:boolean");

            // First we declare a Result Set
            IBlankNode rset = g.CreateBlankNode();

            g.Assert(new Triple(rset, rdfType, resultSetClass));

            if (results.ResultsType == SparqlResultsType.VariableBindings)
            {
                // Assert a Triple for each Result Variable
                foreach (String v in results.Variables)
                {
                    g.Assert(new Triple(rset, resultVariable, g.CreateLiteralNode(v)));
                }

                // Then we're going to define a solution for each result
                foreach (SparqlResult r in results)
                {
                    IBlankNode sln = g.CreateBlankNode();
                    g.Assert(new Triple(rset, solution, sln));

                    foreach (String v in results.Variables)
                    {
                        // Only define Bindings if there is a value and it is non-null
                        if (r.HasValue(v) && r[v] != null)
                        {
                            IBlankNode bnd = g.CreateBlankNode();
                            g.Assert(new Triple(sln, binding, bnd));
                            g.Assert(new Triple(bnd, variable, g.CreateLiteralNode(v)));
                            switch (r[v].NodeType)
                            {
                            case NodeType.Blank:
                                IBlankNode b = (IBlankNode)r[v];
                                IBlankNode bMapped;
                                if (b.GraphUri == null)
                                {
                                    bMapped = g.CreateBlankNode(b.InternalID + "def");
                                }
                                else
                                {
                                    bMapped = g.CreateBlankNode(b.InternalID + b.GraphUri.GetEnhancedHashCode());
                                }
                                g.Assert(new Triple(bnd, value, bMapped));
                                break;

                            case NodeType.GraphLiteral:
                                throw new RdfOutputException(WriterErrorMessages.GraphLiteralsUnserializable("SPARQL Results RDF Serialization"));

                            case NodeType.Literal:
                            case NodeType.Uri:
                                g.Assert(new Triple(bnd, value, r[v].CopyNode(g)));
                                break;

                            default:
                                throw new RdfOutputException(WriterErrorMessages.UnknownNodeTypeUnserializable("SPARQL Results RDF Serialization"));
                            }
                        }
                    }
                }
            }
            else
            {
                // A Boolean Result Set
                g.Assert(new Triple(rset, boolean, g.CreateLiteralNode(results.Result.ToString(), UriFactory.Create(XmlSpecsHelper.XmlSchemaDataTypeBoolean))));
            }

            return(g);
        }