/// <summary> /// Tries to parse Literal Tokens into Literal Nodes. /// </summary> /// <param name="context">Parser Context.</param> /// <param name="lit">Literal Token.</param> /// <returns></returns> private INode TryParseLiteral(TurtleParserContext context, IToken lit) { IToken next; String dturi; switch (lit.TokenType) { case Token.LITERAL: case Token.LONGLITERAL: next = context.Tokens.Peek(); if (next.TokenType == Token.LANGSPEC) { // Has a Language Specifier next = context.Tokens.Dequeue(); return(context.Handler.CreateLiteralNode(lit.Value, next.Value)); } else if (next.TokenType == Token.DATATYPE) { // Has a Datatype next = context.Tokens.Dequeue(); try { if (next.Value.StartsWith("<")) { dturi = next.Value.Substring(1, next.Value.Length - 2); return(context.Handler.CreateLiteralNode(lit.Value, UriFactory.Create(Tools.ResolveUri(dturi, context.BaseUri.ToSafeString())))); } else { dturi = Tools.ResolveQName(next.Value, context.Namespaces, context.BaseUri); return(context.Handler.CreateLiteralNode(lit.Value, UriFactory.Create(dturi))); } } catch (RdfException rdfEx) { throw new RdfParseException("Unable to resolve the Datatype '" + next.Value + "' due to the following error:\n" + rdfEx.Message, next, rdfEx); } } else { // Just an untyped Literal return(context.Handler.CreateLiteralNode(lit.Value)); } case Token.LITERALWITHDT: LiteralWithDataTypeToken litdt = (LiteralWithDataTypeToken)lit; try { if (litdt.DataType.StartsWith("<")) { dturi = litdt.DataType.Substring(1, litdt.DataType.Length - 2); return(context.Handler.CreateLiteralNode(litdt.Value, UriFactory.Create(Tools.ResolveUri(dturi, context.BaseUri.ToSafeString())))); } else { dturi = Tools.ResolveQName(litdt.DataType, context.Namespaces, context.BaseUri); return(context.Handler.CreateLiteralNode(litdt.Value, UriFactory.Create(dturi))); } } catch (RdfException rdfEx) { throw new RdfParseException("Unable to resolve the Datatype '" + litdt.DataType + "' due to the following error:\n" + rdfEx.Message, litdt, rdfEx); } case Token.LITERALWITHLANG: LiteralWithLanguageSpecifierToken langlit = (LiteralWithLanguageSpecifierToken)lit; return(context.Handler.CreateLiteralNode(langlit.Value, langlit.Language)); case Token.PLAINLITERAL: // Attempt to infer Type if (TurtleSpecsHelper.IsValidPlainLiteral(lit.Value, _syntax)) { if (TurtleSpecsHelper.IsValidDouble(lit.Value)) { return(context.Handler.CreateLiteralNode(lit.Value, UriFactory.Create(XmlSpecsHelper.XmlSchemaDataTypeDouble))); } else if (TurtleSpecsHelper.IsValidInteger(lit.Value)) { return(context.Handler.CreateLiteralNode(lit.Value, UriFactory.Create(XmlSpecsHelper.XmlSchemaDataTypeInteger))); } else if (TurtleSpecsHelper.IsValidDecimal(lit.Value)) { return(context.Handler.CreateLiteralNode(lit.Value, UriFactory.Create(XmlSpecsHelper.XmlSchemaDataTypeDecimal))); } else { return(context.Handler.CreateLiteralNode(lit.Value.ToLower(), UriFactory.Create(XmlSpecsHelper.XmlSchemaDataTypeBoolean))); } } else { throw ParserHelper.Error("The value '" + lit.Value + "' is not valid as a Plain Literal in Turtle", lit); } default: throw ParserHelper.Error("Unexpected Token '" + lit.GetType().ToString() + "' encountered, expected a valid Literal Token to convert to a Node", lit); } }