Beispiel #1
0
 /// <summary>
 /// Helper method for raising Error messages with attached Line Information
 /// </summary>
 /// <param name="context">Parser Context</param>
 /// <param name="message">Error Message</param>
 /// <returns></returns>
 private RdfParseException Error(JsonParserContext context, String message)
 {
     StringBuilder error = new StringBuilder();
     if (context.Input.HasLineInfo()) 
     {
         error.Append("[Line " + context.Input.LineNumber + " Column " + context.Input.LinePosition + "] ");
     }
     error.AppendLine(context.Input.TokenType.GetType().Name);
     error.Append(message);
     throw new RdfParseException(error.ToString(), context.Input.LineNumber, context.Input.LinePosition);
 }
Beispiel #2
0
 /// <summary>
 /// Helper method for raising Error messages with attached Position Information
 /// </summary>
 /// <param name="context">Parser Context</param>
 /// <param name="message">Error Message</param>
 /// <param name="startPos">Start Position</param>
 /// <returns></returns>
 private RdfParseException Error(JsonParserContext context, String message, PositionInfo startPos)
 {
     PositionInfo info = context.GetPositionRange(startPos);
     StringBuilder error = new StringBuilder();
     error.Append("[Line " + info.StartLine + " Column " + info.StartPosition + " to Line " + info.EndLine + " Column " + info.EndPosition + "] ");
     error.AppendLine(message);
     throw new RdfParseException(error.ToString(), info);
 }
Beispiel #3
0
        /// <summary>
        /// Parser method which parses Json Arrays representing Object Lists
        /// </summary>
        /// <param name="context">Parser Context</param>
        /// <param name="subj">Subject of Triples which comes from the Grandparent Json Object</param>
        /// <param name="pred">Predicate of Triples which comes form the Parent Json Object</param>
        private void ParseObjectList(JsonParserContext context, INode subj, INode pred)
        {
            PositionInfo startPos = context.CurrentPosition;

            if (context.Input.Read())
            {
                //Expect an Array for the Object List
                if (context.Input.TokenType == JsonToken.StartArray)
                {
                    while (context.Input.TokenType != JsonToken.EndArray)
                    {
                        //Try to parse an 'Object' Object!!
                        this.ParseObject(context, subj, pred);
                    }
                }
                else
                {
                    throw Error(context, "Unexpected Token '" + context.Input.TokenType.ToString() + "' encountered, expected the start of a JSON Array to represent an Object List", startPos);
                }
            }
            else
            {
                throw Error(context, "Unexpected End of Input while trying to parse an Object List from the JSON", startPos);
            }
        }
Beispiel #4
0
        /// <summary>
        /// Parser method which parses Json Objects reprsenting Object Nodes
        /// </summary>
        /// <param name="context">Parser Context</param>
        /// <param name="subj">Subject of Triples which comes from the Great-Grandparent Json Object</param>
        /// <param name="pred">Predicate of Triples which comes form the Grandparent Json Object</param>
        private void ParseObject(JsonParserContext context, INode subj, INode pred)
        {
            String token, nodeValue, nodeType, nodeLang, nodeDatatype;
            nodeValue = nodeType = nodeLang = nodeDatatype = null;

            PositionInfo startPos = context.CurrentPosition;

            if (context.Input.Read())
            {
                if (context.Input.TokenType == JsonToken.StartObject)
                {
                    context.Input.Read();
                    while (context.Input.TokenType != JsonToken.EndObject)
                    {
                        if (context.Input.TokenType == JsonToken.PropertyName)
                        {
                            token = context.Input.Value.ToString().ToLower();

                            //Check that we get a Property Value as a String
                            context.Input.Read();
                            if (context.Input.TokenType != JsonToken.String)
                            {
                                throw Error(context, "Unexpected Token '" + context.Input.TokenType.ToString() + "' encountered, expected a Property Value describing one of the properties of an Object Node", startPos);
                            }

                            //Extract the Information from the Object
                            if (token.Equals("value"))
                            {
                                nodeValue = context.Input.Value.ToString();
                            }
                            else if (token.Equals("type"))
                            {
                                nodeType = context.Input.Value.ToString().ToLower();
                            }
                            else if (token.Equals("lang") || token.Equals("xml:lang"))
                            {
                                if (nodeLang == null && nodeDatatype == null)
                                {
                                    nodeLang = context.Input.Value.ToString();
                                }
                                else
                                {
                                    throw Error(context, "Unexpected Language Property specified for an Object Node where a Language or Datatype has already been specified", startPos);
                                }
                            }
                            else if (token.Equals("datatype"))
                            {
                                if (nodeDatatype == null && nodeLang == null)
                                {
                                    nodeDatatype = context.Input.Value.ToString();
                                }
                                else
                                {
                                    throw Error(context, "Unexpected Datatype Property specified for an Object Node where a Language or Datatype has already been specified", startPos);
                                }
                            }
                            else
                            {
                                throw Error(context, "Unexpected Property '" + token + "' specified for an Object Node, only 'value', 'type', 'lang' and 'datatype' are valid properties", startPos);
                            }
                        }
                        else
                        {
                            throw Error(context, "Unexpected Token '" + context.Input.TokenType.ToString() + "' encountered, expected a Property Name describing one of the properties of an Object Node", startPos);
                        }

                        context.Input.Read();
                    }

                    //Validate the Information
                    if (nodeType == null)
                    {
                        throw Error(context, "Cannot parse an Object Node from the JSON where no 'type' property was specified in the JSON Object representing the Node", startPos);
                    }
                    if (nodeValue == null)
                    {
                        throw Error(context, "Cannot parse an Object Node from the JSON where no 'value' property was specified in the JSON Object representing the Node", startPos);
                    }

                    //Turn this information into a Node
                    INode obj;
                    if (nodeType.Equals("uri"))
                    {
                        obj = context.Handler.CreateUriNode(new Uri(nodeValue));
                    }
                    else if (nodeType.Equals("bnode"))
                    {
                        obj = context.Handler.CreateBlankNode(nodeValue.Substring(nodeValue.IndexOf(':') + 1));
                    }
                    else if (nodeType.Equals("literal"))
                    {
                        if (nodeLang != null)
                        {
                            obj = context.Handler.CreateLiteralNode(nodeValue, nodeLang);
                        }
                        else if (nodeDatatype != null)
                        {
                            obj = context.Handler.CreateLiteralNode(nodeValue, new Uri(nodeDatatype));
                        }
                        else
                        {
                            obj = context.Handler.CreateLiteralNode(nodeValue);
                        }
                    }
                    else
                    {
                        throw Error(context, "Cannot parse an Object Node from the JSON where the 'type' property is not set to one of the permitted values 'uri', 'bnode' or 'literal' in the JSON Object representing the Node", startPos);
                    }

                    //Assert as a Triple
                    if (!context.Handler.HandleTriple(new Triple(subj, pred, obj))) ParserHelper.Stop();
                }
            }
            else
            {
                throw Error(context, "Unexpected End of Input while trying to parse an Object Node from the JSON", startPos);
            }
        }
Beispiel #5
0
        /// <summary>
        /// Parser method which parses Json Objects representing Triples
        /// </summary>
        /// <param name="context">Parser Context</param>
        private void ParseTriples(JsonParserContext context)
        {
            PositionInfo startPos = context.CurrentPosition;
            if (context.Input.Read())
            {
                while (context.Input.TokenType != JsonToken.EndObject)
                {
                    //Expect Property Names for Subjects
                    if (context.Input.TokenType == JsonToken.PropertyName)
                    {
                        String subjValue = context.Input.Value.ToString();
                        INode subjNode;
                        if (subjValue.StartsWith("_:"))
                        {
                            subjNode = context.Handler.CreateBlankNode(subjValue.Substring(subjValue.IndexOf(':') + 1));
                        }
                        else
                        {
                            subjNode = context.Handler.CreateUriNode(new Uri(subjValue));
                        }

                        this.ParsePredicateObjectList(context, subjNode);
                    }
                    else
                    {
                        throw Error(context, "Unexpected Token '" + context.Input.TokenType.ToString() + "' encountered, expected a JSON Property Name to represent the Subject of a Triple", startPos);
                    }
                    context.Input.Read();
                }
            }
            else
            {
                throw Error(context, "Unexpected End of Input while trying to parse Triples from the JSON", startPos);
            }
        }
Beispiel #6
0
        /// <summary>
        /// Parser method which parses Json Objects representing Predicate Object Lists
        /// </summary>
        /// <param name="context">Parser Context</param>
        /// <param name="subj">Subject of Triples which comes from the parent Json Object</param>
        private void ParsePredicateObjectList(JsonParserContext context, INode subj)
        {
            PositionInfo startPos = context.CurrentPosition;

            if (context.Input.Read())
            {
                if (context.Input.TokenType == JsonToken.StartObject)
                {
                    context.Input.Read();
                    while (context.Input.TokenType != JsonToken.EndObject)
                    {
                        //Expect Property Names for Predicates
                        if (context.Input.TokenType == JsonToken.PropertyName)
                        {
                            String predValue = context.Input.Value.ToString();
                            INode predNode = context.Handler.CreateUriNode(new Uri(predValue));

                            this.ParseObjectList(context, subj, predNode);
                        }
                        else
                        {
                            throw Error(context, "Unexpected Token '" + context.Input.TokenType.ToString() + "' encountered, expected a Property Name which represents a Predicate", startPos);
                        }

                        context.Input.Read();
                    }
                }
                else
                {
                    throw Error(context, "Unexpected Token '" + context.Input.TokenType.ToString() + "' encountered, expected the start of a JSON Object to represent a Predicate Object List", startPos);
                }
            }
            else
            {
                throw Error(context, "Unexpected End of Input while trying to parse a Predicate Object List from the JSON", startPos);
            }
        }
Beispiel #7
0
        /// <summary>
        /// Internal top level Parse method which parses the Json
        /// </summary>
        /// <param name="handler">RDF Handler to use</param>
        /// <param name="input">Stream to read from</param>
        private void Parse(IRdfHandler handler, TextReader input)
        {
            JsonParserContext context = new JsonParserContext(handler, new CommentIgnoringJsonTextReader(input));

            try
            {
                context.Handler.StartRdf();
                this.ParseGraphObject(context);
                context.Handler.EndRdf(true);
            }
            catch (RdfParsingTerminatedException)
            {
                context.Handler.EndRdf(true);
                //Discard this - it justs means the Handler told us to stop
            }
            catch
            {
                context.Handler.EndRdf(false);
                throw;
            }
        }
Beispiel #8
0
        /// <summary>
        /// Parser method which parses the top level Json Object which represents the overall Graph
        /// </summary>
        /// <param name="context">Parser Context</param>
        private void ParseGraphObject(JsonParserContext context)
        {
            //Can we read the overall Graph Object
            PositionInfo startPos = context.CurrentPosition;
            if (context.Input.Read())
            {
                if (context.Input.TokenType == JsonToken.StartObject)
                {
                    this.ParseTriples(context);

                    //When we get control back we should have already read the last token which should be an End Object
                    //We ignore any content which is beyond the end of the initial object
                    if (context.Input.TokenType != JsonToken.EndObject)
                    {
                        throw Error(context, "Unexpected Token '" + context.Input.TokenType.ToString() + "' encountered, end of the JSON Graph Object was expected", startPos);
                    }
                }
                else
                {
                    throw Error(context, "Unexpected Token '" + context.Input.TokenType.ToString() + "' encountered, start of the JSON Graph Object was expected", startPos);
                }
            }
            else
            {
                throw Error(context, "Unexpected End of Input while trying to parse start of the JSON Graph Object", startPos);
            }
        }
 private INode TryParseNodeValue(JsonParserContext context, String value)
 {
     return TryParseNodeValue(context.Handler, value);
 }
        private INode TryParseNode(JsonParserContext context, out TripleSegment segment)
        {
            if (context.Input.Read())
            {
                if (context.Input.TokenType == JsonToken.PropertyName)
                {
                    //Determine the Triple Segment
                    switch (context.Input.Value.ToString())
                    {
                        case "subject":
                            segment = TripleSegment.Subject;
                            break;
                        case "predicate":
                            segment = TripleSegment.Predicate;
                            break;
                        case "object":
                            segment = TripleSegment.Object;
                            break;
                        default:
                            throw Error(context, "Unexpected Property '" + context.Input.Value.ToString() + "' encountered, expected one of 'subject', 'predicate' or 'object'");
                    }

                    if (context.Input.Read())
                    {
                        String value = context.Input.Value.ToString();
                        return this.TryParseNodeValue(context, value);
                    }
                    else
                    {
                        throw Error(context, "Unexpected End of Input when a Value for a Node of a Triple was expected");
                    }
                }
                else
                {
                    throw Error(context, "Unexpected Token '" + context.Input.TokenType.ToString() + "' encountered, expected a Property Name for the node of a Triple");
                }
            }
            else
            {
                throw Error(context, "Unexpected End of Input when a Property Value pair for a Node of a Triple was expected");
            }
        }
        /// <summary>
        /// Parser method which parses Json Objects representing Triples
        /// </summary>
        /// <param name="context">Parser Context</param>
        private void ParseTriples(JsonParserContext context)
        {
            PositionInfo startPos = context.CurrentPosition;
            do
            {
                if (context.Input.TokenType == JsonToken.StartObject)
                {
                    INode s, p, o;
                    s = p = o = null;
                    INode temp;
                    TripleSegment segment;

                    //Expect 3 Nodes in a Triple
                    for (int i = 0; i < 3; i++)
                    {
                        temp = this.TryParseNode(context, out segment);
                        switch (segment)
                        {
                            case TripleSegment.Object:
                                if (o == null)
                                {
                                    o = temp;
                                }
                                else
                                {
                                    throw Error(context, "Duplicate object property encountered");
                                }
                                break;
                            case TripleSegment.Predicate:
                                if (p == null)
                                {
                                    p = temp;
                                }
                                else
                                {
                                    throw Error(context, "Duplicate predicate property encountered");
                                }
                                break;
                            case TripleSegment.Subject:
                                if (s == null)
                                {
                                    s = temp;
                                }
                                else
                                {
                                    throw Error(context, "Duplicate Subject property encountered");
                                }
                                break;
                        }
                    }

                    if (!context.Handler.HandleTriple((new Triple(s, p, o)))) throw ParserHelper.Stop();
                }
                else
                {
                    throw Error(context, "Unexpected Token '" + context.Input.TokenType.ToString() + "' encountered, start of a JSON Object for a Triple was expected");
                }

                //Then expect the end of the Object
                if (context.Input.Read())
                {
                    if (context.Input.TokenType != JsonToken.EndObject) throw Error(context, "Unexpected Token '" + context.Input.TokenType.ToString() + " encountered, expected the end of the JSON Object for a Triple");
                }
                else
                {
                    throw Error(context, "Unexpected End of Input while trying to parse Triples from JSON, end of a Triple Object was expected", startPos);
                }

                //Then expect an End Array/Start Object
                if (!context.Input.Read()) throw Error(context, "Unexpected End of Input while trying to parse Triples from JSON, end of JSON array or start of a JSON Object was expected", startPos);
            } while (context.Input.TokenType == JsonToken.StartObject);
        }
        /// <summary>
        /// Parser method which parses the top level Json Object which represents the overall Graph
        /// </summary>
        /// <param name="context">Parser Context</param>
        private void ParseTriplesArray(JsonParserContext context)
        {
            try
            {
                context.Handler.StartRdf();

                //Can we read the overall Graph Object
                PositionInfo startPos = context.CurrentPosition;
                if (context.Input.Read())
                {
                    if (context.Input.TokenType == JsonToken.StartArray)
                    {
                        if (!context.Input.Read()) throw Error(context, "Unexpected End of Input encountered, expected the start of a Triple Object/end of the Triples array");

                        if (context.Input.TokenType == JsonToken.StartObject) this.ParseTriples(context);

                        //Should see an End Array when we get back here
                        if (context.Input.TokenType != JsonToken.EndArray) throw Error(context, "Unexpected Token '" + context.Input.TokenType.ToString() + "' encountered, end of the JSON Array was expected");
                    }
                    else
                    {
                        throw Error(context, "Unexpected Token '" + context.Input.TokenType.ToString() + "' encountered, start of the JSON Array was expected", startPos);
                    }
                }
                else
                {
                    throw Error(context, "Unexpected End of Input while trying to parse start of the JSON Triple Array", startPos);
                }

                context.Handler.EndRdf(true);
            }
            catch (RdfParsingTerminatedException)
            {
                context.Handler.EndRdf(true);
            }
            catch
            {
                context.Handler.EndRdf(false);
                throw;
            }
        }
 /// <summary>
 /// Internal top level Parse method which parses the JSON
 /// </summary>
 /// <param name="g">Graph to read into</param>
 /// <param name="input">Stream to read from</param>
 private void Parse(IRdfHandler handler, TextReader input)
 {
     //Create Parser Context and parse
     JsonParserContext context = new JsonParserContext(handler, new CommentIgnoringJsonTextReader(input));
     this.ParseTriplesArray(context);
 }