Esempio n. 1
0
        /// <summary>
        /// Formats a Graph Header by creating an <strong>&lt;rdf:RDF&gt;</strong> element and adding namespace definitions.
        /// </summary>
        /// <param name="namespaces">Namespaces.</param>
        /// <returns></returns>
        public string FormatGraphHeader(INamespaceMapper namespaces)
        {
            _mapper = new QNameOutputMapper(namespaces);
            StringBuilder output = new StringBuilder();

            output.Append(GetGraphHeaderBase());
            foreach (String prefix in namespaces.Prefixes)
            {
                if (!prefix.Equals("rdf"))
                {
                    if (prefix.Equals(String.Empty))
                    {
                        output.Append(" xmlns=\"" + WriterHelper.EncodeForXml(namespaces.GetNamespaceUri(prefix).AbsoluteUri) + "\"");
                    }
                    else
                    {
                        output.Append(" xmlns:" + prefix + "=\"" + WriterHelper.EncodeForXml(namespaces.GetNamespaceUri(prefix).AbsoluteUri) + "\"");
                    }
                }
            }
            output.Append(">");
            return(output.ToString());
        }
        /// <summary>
        /// Formats a SPARQL Result
        /// </summary>
        /// <param name="result">SPARQL Result</param>
        /// <returns></returns>
        public string Format(SparqlResult result)
        {
            StringBuilder output = new StringBuilder();

            output.AppendLine(" <result>");
            foreach (String var in result.Variables)
            {
                if (result.HasValue(var))
                {
                    INode value = result[var];
                    if (value != null)
                    {
                        output.Append("  <binding name=\"" + var + "\">");
                        switch (value.NodeType)
                        {
                        case NodeType.Blank:
                            output.Append("<bnode>" + ((IBlankNode)value).InternalID + "</bnode>");
                            break;

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

                        case NodeType.Literal:
                            ILiteralNode lit = (ILiteralNode)value;
                            output.Append("<literal");
                            if (lit.DataType != null)
                            {
                                output.Append(" datatype=\"" + WriterHelper.EncodeForXml(lit.DataType.AbsoluteUri) + "\">" + WriterHelper.EncodeForXml(lit.Value) + "</literal>");
                            }
                            else if (!lit.Language.Equals(String.Empty))
                            {
                                output.Append(" xml:lang=\"" + lit.Language + "\">" + lit.Value + "</literal>");
                            }
                            else
                            {
                                output.Append(">" + WriterHelper.EncodeForXml(lit.Value) + "</literal>");
                            }
                            break;

                        case NodeType.Uri:
                            output.Append("<uri>" + WriterHelper.EncodeForXml(value.ToString()) + "</uri>");
                            break;

                        case NodeType.Variable:
                            throw new RdfOutputException(WriterErrorMessages.VariableNodesUnserializable("SPARQL XML Results"));

                        default:
                            throw new RdfOutputException(WriterErrorMessages.UnknownNodeTypeUnserializable("SPARQL XML Results"));
                        }
                        output.AppendLine("</binding>");
                    }
                }
            }
            output.Append(" </result>");
            return(output.ToString());
        }
Esempio n. 3
0
        /// <summary>
        /// Formats a Triple as a <strong>&lt;rdf:Description&gt;</strong> element.
        /// </summary>
        /// <param name="t">Triple.</param>
        /// <returns></returns>
        public string Format(Triple t)
        {
            StringBuilder output = new StringBuilder();

            output.Append("<rdf:Description ");
            switch (t.Subject.NodeType)
            {
            case NodeType.Uri:
                output.Append("rdf:about=\"" + WriterHelper.EncodeForXml(t.Subject.ToString()) + "\"");
                break;

            case NodeType.Blank:
                output.Append("rdf:nodeID=\"" + ((IBlankNode)t.Subject).InternalID + "\"");
                break;

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

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

            case NodeType.Variable:
                throw new RdfOutputException(WriterErrorMessages.VariableNodesUnserializable("RDF/XML"));

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

            String qname;
            String ns;

            switch (t.Predicate.NodeType)
            {
            case NodeType.Uri:
                Uri u = ((IUriNode)t.Predicate).Uri;
                GetQName(u, out qname, out ns);
                output.Append('\t');
                if (ns.Equals(String.Empty))
                {
                    output.Append("<" + qname);
                }
                else
                {
                    output.Append("<" + qname + " xmlns=\"" + WriterHelper.EncodeForXml(ns) + "\"");
                }
                break;

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

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

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

            case NodeType.Variable:
                throw new RdfOutputException(WriterErrorMessages.VariableNodesUnserializable("RDF/XML"));

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

            switch (t.Object.NodeType)
            {
            case NodeType.Blank:
                output.AppendLine(" rdf:nodeID=\"" + ((IBlankNode)t.Object).InternalID + "\" />");
                break;

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

            case NodeType.Literal:
                ILiteralNode lit = (ILiteralNode)t.Object;
                if (lit.DataType != null)
                {
                    if (lit.DataType.ToString().Equals(RdfSpecsHelper.RdfXmlLiteral))
                    {
                        output.AppendLine(" rdf:parseType=\"Literal\">" + lit.Value + "</" + qname + ">");
                    }
                    else
                    {
                        output.AppendLine(" rdf:datatype=\"" + WriterHelper.EncodeForXml(lit.DataType.AbsoluteUri) + "\">" + WriterHelper.EncodeForXml(lit.Value) + "</" + qname + ">");
                    }
                }
                else if (!lit.Language.Equals(String.Empty))
                {
                    output.AppendLine(" xml:lang=\"" + lit.Language + "\">" + WriterHelper.EncodeForXml(lit.Value) + "</" + qname + ">");
                }
                else
                {
                    output.AppendLine(">" + WriterHelper.EncodeForXml(lit.Value) + "</" + qname + ">");
                }
                break;

            case NodeType.Uri:
                output.AppendLine(" rdf:resource=\"" + WriterHelper.EncodeForXml(t.Object.ToString()) + "\" />");
                break;

            case NodeType.Variable:
                throw new RdfOutputException(WriterErrorMessages.VariableNodesUnserializable("RDF/XML"));

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

            output.Append("</rdf:Description>");
            return(output.ToString());
        }
Esempio n. 4
0
        protected override bool HandleResultInternal(SparqlResult result)
        {
            if (_firstResult)
            {
                _xmlWriter.WriteEndElement();
                _xmlWriter.WriteStartElement("results");
                _firstResult = false;
            }

            _xmlWriter.WriteStartElement("result");

            foreach (var variable in result.Variables)
            {
                _xmlWriter.WriteStartElement("binding");
                _xmlWriter.WriteAttributeString("name", variable);

                var node = result.Value(variable);

                switch (node.NodeType)
                {
                case NodeType.Blank:
                    _xmlWriter.WriteStartElement("bnode");
                    _xmlWriter.WriteRaw(((IBlankNode)node).InternalID);
                    _xmlWriter.WriteEndElement();
                    break;

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

                case NodeType.Literal:
                    _xmlWriter.WriteStartElement("literal");
                    ILiteralNode literal = (ILiteralNode)node;

                    if (!literal.Language.Equals(String.Empty))
                    {
                        _xmlWriter.WriteStartAttribute("xml", "lang", XmlSpecsHelper.NamespaceXml);
                        _xmlWriter.WriteRaw(literal.Language);
                        _xmlWriter.WriteEndAttribute();
                    }
                    else if (literal.DataType != null)
                    {
                        _xmlWriter.WriteStartAttribute("datatype");
                        _xmlWriter.WriteRaw(WriterHelper.EncodeForXml(literal.DataType.AbsoluteUri));
                        _xmlWriter.WriteEndAttribute();
                    }

                    _xmlWriter.WriteRaw(WriterHelper.EncodeForXml(literal.Value));
                    _xmlWriter.WriteEndElement();
                    break;

                case NodeType.GraphLiteral:
                    throw new RdfOutputException("Result Sets which contain Graph Literal Nodes cannot be serialized in the SPARQL Query Results XML Format");

                case NodeType.Variable:
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }

                _xmlWriter.WriteEndElement();
            }

            _xmlWriter.WriteEndElement();
            return(true);
        }