Пример #1
0
        /// <summary>
        /// Creates a new NTriples formatter.
        /// </summary>
        /// <param name="syntax">NTriples syntax to output.</param>
        /// <param name="formatName">Format Name.</param>
        public NTriplesFormatter(NTriplesSyntax syntax, string formatName)
            : base(formatName)
        {
            Syntax = syntax;
            switch (Syntax)
            {
            case NTriplesSyntax.Original:
                _bnodeMapper = new BlankNodeOutputMapper(WriterHelper.IsValidStrictBlankNodeID);
                break;

            default:
                _bnodeMapper = new BlankNodeOutputMapper(WriterHelper.IsValidBlankNodeID);
                break;
            }
        }
Пример #2
0
        /// <summary>
        /// Internal method which generates the NTriples in 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 (this._prettyprint)
            {
                writer.Formatting = Newtonsoft.Json.Formatting.Indented;
            }
            else
            {
                writer.Formatting = Newtonsoft.Json.Formatting.None;
            }

            //Start the overall Array which contains the set of Triple Objects
            writer.WriteStartArray();

            //Start writing Triples
            foreach (Triple t in g.Triples)
            {
                writer.WriteStartObject();

                //Write Subject
                writer.WritePropertyName("subject");
                writer.WriteValue(this._formatter.Format(t.Subject));

                //Write Predicate
                writer.WritePropertyName("predicate");
                writer.WriteValue(this._formatter.Format(t.Predicate));

                //Write Object
                writer.WritePropertyName("object");
                writer.WriteValue(this._formatter.Format(t.Object));

                writer.WriteEndObject();
            }

            //Terminate the Array which represents the Graph
            writer.WriteEndArray();
        }
Пример #3
0
        /// <summary>
        /// Internal method which generates the NTriples in 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 (this._prettyprint)
            {
                writer.Formatting = Newtonsoft.Json.Formatting.Indented;
            }
            else
            {
                writer.Formatting = Newtonsoft.Json.Formatting.None;
            }

            //Start the overall Array which contains the set of Triple Objects
            writer.WriteStartArray();

            //Start writing Triples
            foreach (Triple t in g.Triples)
            {
                writer.WriteStartObject();

                //Write Subject
                writer.WritePropertyName("subject");
                writer.WriteValue(this._formatter.Format(t.Subject));

                //Write Predicate
                writer.WritePropertyName("predicate");
                writer.WriteValue(this._formatter.Format(t.Predicate));

                //Write Object
                writer.WritePropertyName("object");
                writer.WriteValue(this._formatter.Format(t.Object));

                writer.WriteEndObject();
            }

            //Terminate the Array which represents the Graph
            writer.WriteEndArray();
        }
Пример #4
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();
        }
Пример #5
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 (this._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();

            //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.ToString());
                        }
                        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();
        }