Beispiel #1
0
        private IToken TryParseObject(ITokenQueue tokens)
        {
            IToken next = tokens.Dequeue();

            switch (next.TokenType)
            {
            case Token.BLANKNODEWITHID:
            case Token.LITERALWITHDT:
            case Token.LITERALWITHLANG:
            case Token.URI:
                //OK
                return(next);

            case Token.LITERAL:
                //Check for Datatype/Language
                IToken temp = tokens.Peek();
                if (temp.TokenType == Token.DATATYPE)
                {
                    tokens.Dequeue();
                    return(new LiteralWithDataTypeToken(next, (DataTypeToken)temp));
                }
                else if (temp.TokenType == Token.LANGSPEC)
                {
                    tokens.Dequeue();
                    return(new LiteralWithLanguageSpecifierToken(next, (LanguageSpecifierToken)temp));
                }
                else
                {
                    return(next);
                }

            default:
                throw ParserHelper.Error("Unexpected Token '" + next.GetType().ToString() + "' encountered, expected a Blank Node/Literal/URI as the Object of a Triple", next);
            }
        }
Beispiel #2
0
        private IToken TryParsePredicate(ITokenQueue tokens)
        {
            IToken next = tokens.Dequeue();

            switch (next.TokenType)
            {
            case Token.URI:
                //OK
                return(next);

            default:
                throw ParserHelper.Error("Unexpected Token '" + next.GetType().ToString() + "' encountered, expected a URI as the Predicate of a Triple", next);
            }
        }
Beispiel #3
0
        private IToken TryParseSubject(ITokenQueue tokens)
        {
            IToken next = tokens.Dequeue();

            switch (next.TokenType)
            {
            case Token.BLANKNODEWITHID:
            case Token.URI:
                //OK
                return(next);

            default:
                throw ParserHelper.Error("Unexpected Token '" + next.GetType().ToString() + "' encountered, expected a Blank Node/URI as the Subject of a Triple", next);
            }
        }
Beispiel #4
0
        private void Parse(IRdfHandler handler, ITokenQueue tokens)
        {
            IToken next;
            IToken s, p, o;

            try
            {
                handler.StartRdf();

                //Expect a BOF token at start
                next = tokens.Dequeue();
                if (next.TokenType != Token.BOF)
                {
                    throw ParserHelper.Error("Unexpected Token '" + next.GetType().ToString() + "' encountered, expected a BOF token at the start of the input", next);
                }

                do
                {
                    next = tokens.Peek();
                    if (next.TokenType == Token.EOF)
                    {
                        return;
                    }

                    s = this.TryParseSubject(tokens);
                    p = this.TryParsePredicate(tokens);
                    o = this.TryParseObject(tokens);
                    Uri context = this.TryParseContext(tokens);

                    this.TryParseTriple(handler, s, p, o, context);

                    next = tokens.Peek();
                } while (next.TokenType != Token.EOF);

                handler.EndRdf(true);
            }
            catch (RdfParsingTerminatedException)
            {
                handler.EndRdf(true);
                //Discard this - it justs means the Handler told us to stop
            }
            catch
            {
                handler.EndRdf(false);
                throw;
            }
        }
        private Uri TryParseContext(ITokenQueue tokens)
        {
            IToken next = tokens.Dequeue();
            if (next.TokenType == Token.DOT)
            {
                return null;
            }
            else
            {
                INode context;
                switch (next.TokenType)
                {
                    case Token.BLANKNODEWITHID:
                        context = new BlankNode(null, next.Value.Substring(2));
                        break;
                    case Token.URI:
                        context = new UriNode(null, new Uri(next.Value));
                        break;
                    case Token.LITERAL:
                        //Check for Datatype/Language
                        IToken temp = tokens.Peek();
                        if (temp.TokenType == Token.LANGSPEC)
                        {
                            tokens.Dequeue();
                            context = new LiteralNode(null, next.Value, temp.Value);
                        }
                        else if (temp.TokenType == Token.DATATYPE)
                        {
                            tokens.Dequeue();
                            context = new LiteralNode(null, next.Value, new Uri(temp.Value.Substring(1, temp.Value.Length - 2)));
                        }
                        else
                        {
                            context = new LiteralNode(null, next.Value);
                        }
                        break;
                    default:
                        throw ParserHelper.Error("Unexpected Token '" + next.GetType().ToString() + "' encountered, expected a Blank Node/Literal/URI as the Context of the Triple", next);
                }

                //Ensure we then see a . to terminate the Quad
                next = tokens.Dequeue();
                if (next.TokenType != Token.DOT)
                {
                    throw ParserHelper.Error("Unexpected Token '" + next.GetType().ToString() + "' encountered, expected a Dot Token (Line Terminator) to terminate a Triple", next);
                }

                //Finally return the Context URI
                if (context.NodeType == NodeType.Uri)
                {
                    return ((IUriNode)context).Uri;
                }
                else if (context.NodeType == NodeType.Blank)
                {
                    return new Uri("nquads:bnode:" + context.GetHashCode());
                }
                else if (context.NodeType == NodeType.Literal)
                {
                    return new Uri("nquads:literal:" + context.GetHashCode());
                }
                else
                {
                    throw ParserHelper.Error("Cannot turn a Node of type '" + context.GetType().ToString() + "' into a Context URI for a Triple", next);
                }
            }
        }
        private IToken TryParseObject(ITokenQueue tokens)
        {
            IToken next = tokens.Dequeue();
            switch (next.TokenType)
            {
                case Token.BLANKNODEWITHID:
                case Token.LITERALWITHDT:
                case Token.LITERALWITHLANG:
                case Token.URI:
                    //OK
                    return next;

                case Token.LITERAL:
                    //Check for Datatype/Language
                    IToken temp = tokens.Peek();
                    if (temp.TokenType == Token.DATATYPE)
                    {
                        tokens.Dequeue();
                        return new LiteralWithDataTypeToken(next, (DataTypeToken)temp);
                    }
                    else if (temp.TokenType == Token.LANGSPEC)
                    {
                        tokens.Dequeue();
                        return new LiteralWithLanguageSpecifierToken(next, (LanguageSpecifierToken)temp);
                    }
                    else
                    {
                        return next;
                    }
                default:
                    throw ParserHelper.Error("Unexpected Token '" + next.GetType().ToString() + "' encountered, expected a Blank Node/Literal/URI as the Object of a Triple", next);
            }
        }
 private IToken TryParsePredicate(ITokenQueue tokens)
 {
     IToken next = tokens.Dequeue();
     switch (next.TokenType)
     {
         case Token.URI:
             //OK
             return next;
         default:
             throw ParserHelper.Error("Unexpected Token '" + next.GetType().ToString() + "' encountered, expected a URI as the Predicate of a Triple", next);
     }
 }
        private IToken TryParseSubject(ITokenQueue tokens)
        {
            IToken next = tokens.Dequeue();
            switch (next.TokenType)
            {
                case Token.BLANKNODEWITHID:
                case Token.URI:
                    //OK
                    return next;

                default:
                    throw ParserHelper.Error("Unexpected Token '" + next.GetType().ToString() + "' encountered, expected a Blank Node/URI as the Subject of a Triple", next);
            }
        }
        private void Parse(IRdfHandler handler, ITokenQueue tokens)
        {
            IToken next;
            IToken s, p, o;

            try
            {
                handler.StartRdf();

                //Expect a BOF token at start
                next = tokens.Dequeue();
                if (next.TokenType != Token.BOF)
                {
                    throw ParserHelper.Error("Unexpected Token '" + next.GetType().ToString() + "' encountered, expected a BOF token at the start of the input", next);
                }

                do
                {
                    next = tokens.Peek();
                    if (next.TokenType == Token.EOF) return;

                    s = this.TryParseSubject(tokens);
                    p = this.TryParsePredicate(tokens);
                    o = this.TryParseObject(tokens);
                    Uri context = this.TryParseContext(tokens);

                    this.TryParseTriple(handler, s, p, o, context);

                    next = tokens.Peek();
                } while (next.TokenType != Token.EOF);

                handler.EndRdf(true);
            }
            catch (RdfParsingTerminatedException)
            {
                handler.EndRdf(true);
                //Discard this - it justs means the Handler told us to stop
            }
            catch
            {
                handler.EndRdf(false);
                throw;
            }
        }
Beispiel #10
0
        private Uri TryParseContext(ITokenQueue tokens)
        {
            IToken next = tokens.Dequeue();

            if (next.TokenType == Token.DOT)
            {
                return(null);
            }
            else
            {
                INode context;
                switch (next.TokenType)
                {
                case Token.BLANKNODEWITHID:
                    context = new BlankNode(null, next.Value.Substring(2));
                    break;

                case Token.URI:
                    context = new UriNode(null, new Uri(next.Value));
                    break;

                case Token.LITERAL:
                    //Check for Datatype/Language
                    IToken temp = tokens.Peek();
                    if (temp.TokenType == Token.LANGSPEC)
                    {
                        tokens.Dequeue();
                        context = new LiteralNode(null, next.Value, temp.Value);
                    }
                    else if (temp.TokenType == Token.DATATYPE)
                    {
                        tokens.Dequeue();
                        context = new LiteralNode(null, next.Value, new Uri(temp.Value.Substring(1, temp.Value.Length - 2)));
                    }
                    else
                    {
                        context = new LiteralNode(null, next.Value);
                    }
                    break;

                default:
                    throw ParserHelper.Error("Unexpected Token '" + next.GetType().ToString() + "' encountered, expected a Blank Node/Literal/URI as the Context of the Triple", next);
                }

                //Ensure we then see a . to terminate the Quad
                next = tokens.Dequeue();
                if (next.TokenType != Token.DOT)
                {
                    throw ParserHelper.Error("Unexpected Token '" + next.GetType().ToString() + "' encountered, expected a Dot Token (Line Terminator) to terminate a Triple", next);
                }

                //Finally return the Context URI
                if (context.NodeType == NodeType.Uri)
                {
                    return(((IUriNode)context).Uri);
                }
                else if (context.NodeType == NodeType.Blank)
                {
                    return(new Uri("nquads:bnode:" + context.GetHashCode()));
                }
                else if (context.NodeType == NodeType.Literal)
                {
                    return(new Uri("nquads:literal:" + context.GetHashCode()));
                }
                else
                {
                    throw ParserHelper.Error("Cannot turn a Node of type '" + context.GetType().ToString() + "' into a Context URI for a Triple", next);
                }
            }
        }
Beispiel #11
0
        private Uri TryParseContext(IRdfHandler handler, ITokenQueue tokens)
        {
            IToken next = tokens.Dequeue();

            if (next.TokenType == Token.DOT)
            {
                return(null);
            }
            INode context;

            switch (next.TokenType)
            {
            case Token.BLANKNODEWITHID:
                context = handler.CreateBlankNode(next.Value.Substring(2));
                break;

            case Token.URI:
                context = TryParseUri(handler, next.Value);
                break;

            case Token.LITERAL:
                if (this.Syntax != NQuadsSyntax.Original)
                {
                    throw new RdfParseException("Only a Blank Node/URI may be used as the graph name in RDF NQuads 1.1");
                }

                //Check for Datatype/Language
                IToken temp = tokens.Peek();
                switch (temp.TokenType)
                {
                case Token.LANGSPEC:
                    tokens.Dequeue();
                    context = handler.CreateLiteralNode(next.Value, temp.Value);
                    break;

                case Token.DATATYPE:
                    tokens.Dequeue();
                    context = handler.CreateLiteralNode(next.Value, ((IUriNode)TryParseUri(handler, temp.Value.Substring(1, temp.Value.Length - 2))).Uri);
                    break;

                default:
                    context = handler.CreateLiteralNode(next.Value);
                    break;
                }
                break;

            default:
                throw ParserHelper.Error("Unexpected Token '" + next.GetType().ToString() + "' encountered, expected a Blank Node/Literal/URI as the Context of the Triple", next);
            }

            //Ensure we then see a . to terminate the Quad
            next = tokens.Dequeue();
            if (next.TokenType != Token.DOT)
            {
                throw ParserHelper.Error("Unexpected Token '" + next.GetType().ToString() + "' encountered, expected a Dot Token (Line Terminator) to terminate a Triple", next);
            }

            //Finally return the Context URI
            if (context.NodeType == NodeType.Uri)
            {
                return(((IUriNode)context).Uri);
            }
            else if (context.NodeType == NodeType.Blank)
            {
                return(UriFactory.Create("nquads:bnode:" + context.GetHashCode()));
            }
            else if (context.NodeType == NodeType.Literal)
            {
                return(UriFactory.Create("nquads:literal:" + context.GetHashCode()));
            }
            else
            {
                throw ParserHelper.Error("Cannot turn a Node of type '" + context.GetType().ToString() + "' into a Context URI for a Triple", next);
            }
        }