Beispiel #1
0
        private string GenerateNodeOutput(INode node, TripleSegment segment)
        {
            switch (node.NodeType)
            {
            case NodeType.Blank:
                if (segment == TripleSegment.Predicate)
                {
                    throw new RdfOutputException(WriterErrorMessages.BlankPredicatesUnserializable("Turtle"));
                }

                return(Format(node, segment));

            case NodeType.Uri:
                return(Format(node, segment));

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

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

            default:
                throw new RdfOutputException(WriterErrorMessages.UnknownNodeTypeUnserializable("Turtle"));
            }
        }
        /// <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());
        }