internal void Parse(INode n, XsdtType x) { switch (n.NodeType) { case NodeType.Uri: x.NetType = typeof(Uri); x.ValuePart = n.ToString(); x.XsdtTypeName = XsdtPrimitiveDataType.XsdtAnyUri; ConvertValueToNet(x); break; case NodeType.Literal: ILiteralNode lit = (ILiteralNode)n; x.XsdtTypeName = XsdtPrimitiveDataType.XsdtUnknown; x.ValuePart = lit.Value; if (lit.DataType != null) { if (lit.DataType.ToString().StartsWith(NamespaceMapper.XMLSCHEMA)) { x.TypePart = lit.DataType.ToString().Substring(NamespaceMapper.XMLSCHEMA.Length); } else { x.TypePart = "string"; } } else { if (TurtleSpecsHelper.IsValidPlainLiteral(lit.Value)) { if (TurtleSpecsHelper.IsValidInteger(lit.Value)) { x.TypePart = "integer"; } else if (TurtleSpecsHelper.IsValidDecimal(lit.Value)) { x.TypePart = "decimal"; } else if (TurtleSpecsHelper.IsValidDouble(lit.Value)) { x.TypePart = "double"; } else { x.TypePart = "boolean"; } } else { x.TypePart = "string"; } } foreach (int i in Enum.GetValues(typeof(XsdtPrimitiveDataType))) { var result = (XsdtPrimitiveDataType)i; XsdtAttribute attr = GetXsdtAttrFor(result); if (attr.TypeUri == x.TypePart) { x.XsdtTypeName = result; } } if (TypeLookup.ContainsKey(x.XsdtTypeName)) { x.NetType = TypeLookup[x.XsdtTypeName]; } else { throw new LinqToRdfException("XsdtTypeConverter does not know how to convert the XML Schema Datatype " + x.TypePart); } ConvertValueToNet(x); break; default: throw new LinqToRdfException("XsdtTypeConverter can only convert URI and Literal Nodes"); } }
/// <summary> /// Formats a Literal Node as a String. /// </summary> /// <param name="l">Literal Node.</param> /// <param name="segment">Triple Segment.</param> /// <returns></returns> protected override string FormatLiteralNode(ILiteralNode l, TripleSegment?segment) { var output = new StringBuilder(); bool longlit, plainlit; longlit = TurtleSpecsHelper.IsLongLiteral(l.Value); plainlit = TurtleSpecsHelper.IsValidPlainLiteral(l.Value, l.DataType, TurtleSyntax.Original); if (plainlit) { if (TurtleSpecsHelper.IsValidDecimal(l.Value) && l.Value.EndsWith(".")) { // Ensure we strip the trailing dot of any xsd:decimal and add a datatype definition output.Append('"'); output.Append(l.Value.Substring(0, l.Value.Length - 1)); output.Append("\"^^<"); output.Append(FormatUri(XmlSpecsHelper.XmlSchemaDataTypeDecimal)); output.Append('>'); } else { // Otherwise just write out the value output.Append(l.Value); } // For integers ensure we insert a space after the literal to ensure it can't ever be confused with a decimal if (TurtleSpecsHelper.IsValidInteger(l.Value)) { output.Append(' '); } } else { output.Append('"'); if (longlit) { output.Append("\"\""); } var value = l.Value; value = longlit ? Escape(value, _longLitMustEscape) : Escape(value, _litMustEscape); output.Append(value); output.Append('"'); if (longlit) { output.Append("\"\""); } if (!l.Language.Equals(string.Empty)) { output.Append('@'); output.Append(l.Language.ToLower()); } else if (l.DataType != null) { output.Append("^^"); if (_qnameMapper.ReduceToQName(l.DataType.AbsoluteUri, out var qname)) { if (TurtleSpecsHelper.IsValidQName(qname)) { output.Append(qname); } else { output.Append('<'); output.Append(FormatUri(l.DataType)); output.Append('>'); } } else { output.Append('<'); output.Append(FormatUri(l.DataType)); output.Append('>'); } } } return(output.ToString()); }
/// <summary> /// Formats a Literal Node as a String /// </summary> /// <param name="l">Literal Node</param> /// <param name="segment">Triple Segment</param> /// <returns></returns> protected override string FormatLiteralNode(ILiteralNode l, TripleSegment?segment) { StringBuilder output = new StringBuilder(); String value, qname; bool longlit = false, plainlit = false; longlit = TurtleSpecsHelper.IsLongLiteral(l.Value); plainlit = TurtleSpecsHelper.IsValidPlainLiteral(l.Value, l.DataType); if (plainlit) { if (TurtleSpecsHelper.IsValidDecimal(l.Value) && l.Value.EndsWith(".")) { //Ensure we strip the trailing dot of any xsd:decimal and add a datatype definition output.Append('"'); output.Append(l.Value.Substring(0, l.Value.Length - 1)); output.Append("\"^^<"); output.Append(this.FormatUri(XmlSpecsHelper.XmlSchemaDataTypeDecimal)); output.Append('>'); } else { //Otherwise just write out the value output.Append(l.Value); } //For integers ensure we insert a space after the literal to ensure it can't ever be confused with a decimal if (TurtleSpecsHelper.IsValidInteger(l.Value)) { output.Append(' '); } } else { output.Append('"'); if (longlit) { output.Append("\"\""); } value = l.Value; bool fullyEscaped = (longlit) ? value.IsFullyEscaped(this._validEscapes, this._longLitMustEscape) : value.IsFullyEscaped(this._validEscapes, this._litMustEscape); if (!fullyEscaped) { //This first replace escapes all back slashes for good measure value = value.EscapeBackslashes(this._validEscapes); //Then remove null character since it doesn't change the meaning of the Literal value = value.Replace("\0", ""); //Don't need all the other escapes for long literals as the characters that would be escaped are permitted in long literals //Need to escape " still value = value.Escape('"'); if (!longlit) { //Then if we're not a long literal we'll escape tabs value = value.Replace("\t", "\\t"); } } output.Append(value); output.Append('"'); if (longlit) { output.Append("\"\""); } if (!l.Language.Equals(String.Empty)) { output.Append('@'); output.Append(l.Language.ToLower()); } else if (l.DataType != null) { output.Append("^^"); if (this._qnameMapper.ReduceToQName(l.DataType.ToString(), out qname)) { if (TurtleSpecsHelper.IsValidQName(qname)) { output.Append(qname); } else { output.Append('<'); output.Append(this.FormatUri(l.DataType)); output.Append('>'); } } else { output.Append('<'); output.Append(this.FormatUri(l.DataType)); output.Append('>'); } } } return(output.ToString()); }