public void TestMalformedTags(string tag)
 {
     Assert.False(LanguageTag.IsWellFormed(tag),
                  $"Expected language tag {tag} to not parse as well-formed");
 }
Exemplo n.º 2
0
        private INode MakeNode(IRdfHandler handler, JToken token, Uri graphIri, bool allowRelativeIri = false)
        {
            if (token is JValue)
            {
                var stringValue = token.Value <string>();
                if (JsonLdUtils.IsBlankNodeIdentifier(stringValue))
                {
                    return(handler.CreateBlankNode(stringValue.Substring(2)));
                }
                if (Uri.TryCreate(stringValue, allowRelativeIri ? UriKind.RelativeOrAbsolute : UriKind.Absolute, out Uri iri))
                {
                    if (!Uri.IsWellFormedUriString(stringValue, allowRelativeIri ? UriKind.RelativeOrAbsolute : UriKind.Absolute))
                    {
                        return(null);
                    }
                    return(handler.CreateUriNode(iri));
                }
                return(null);
            }

            if (JsonLdUtils.IsValueObject(token))
            {
                string literalValue = null;
                var    valueObject  = token as JObject;
                var    value        = valueObject["@value"];
                var    datatype     = valueObject.Property("@type")?.Value.Value <string>();
                var    language     = valueObject.Property("@language")?.Value.Value <string>();
                if (datatype == "@json")
                {
                    datatype = RdfNs + "JSON";
                    var serializer = new JsonLiteralSerializer();
                    literalValue = serializer.Serialize(value);
                }
                else if (value.Type == JTokenType.Boolean)
                {
                    literalValue = value.Value <bool>() ? "true" : "false";
                    if (datatype == null)
                    {
                        datatype = XsdNs + "boolean";
                    }
                }
                else if (value.Type == JTokenType.Float ||
                         value.Type == JTokenType.Integer && datatype != null && datatype.Equals(XsdNs + "double"))
                {
                    var doubleValue  = value.Value <double>();
                    var roundedValue = Math.Round(doubleValue);
                    if (doubleValue.Equals(roundedValue) && doubleValue < 1000000000000000000000.0 && datatype == null)
                    {
                        // Integer values up to 10^21 should be rendered as a fixed-point integer
                        literalValue = roundedValue.ToString("F0");
                        datatype     = XsdNs + "integer";
                    }
                    else
                    {
                        literalValue = value.Value <double>().ToString("E15", CultureInfo.InvariantCulture);
                        literalValue = ExponentialFormatMatcher.Replace(literalValue, "$1E");
                        if (literalValue.EndsWith("E"))
                        {
                            literalValue = literalValue + "0";
                        }
                        if (datatype == null)
                        {
                            datatype = XsdNs + "double";
                        }
                    }
                }
                else if (value.Type == JTokenType.Integer ||
                         value.Type == JTokenType.Float && datatype != null && datatype.Equals(XsdNs + "integer"))
                {
                    literalValue = value.Value <long>().ToString("D", CultureInfo.InvariantCulture);
                    if (datatype == null)
                    {
                        datatype = XsdNs + "integer";
                    }
                }
                else if (valueObject.ContainsKey("@direction") && ParserOptions.RdfDirection.HasValue)
                {
                    literalValue = value.Value <string>();
                    var direction = valueObject["@direction"].Value <string>();
                    language = valueObject.ContainsKey("@language")
                        ? valueObject["@language"].Value <string>().ToLowerInvariant()
                        : string.Empty;
                    if (ParserOptions.RdfDirection == JsonLdRdfDirectionMode.I18NDatatype)
                    {
                        datatype = "https://www.w3.org/ns/i18n#" + language + "_" + direction;
                        return(handler.CreateLiteralNode(literalValue, new Uri(datatype)));
                    }
                    // Otherwise direction mode is CompoundLiteral
                    var literalNode = handler.CreateBlankNode();
                    var xsdString   =
                        UriFactory.Create(XmlSpecsHelper.XmlSchemaDataTypeString);
                    handler.HandleTriple(new Triple(
                                             literalNode,
                                             handler.CreateUriNode(UriFactory.Create(RdfSpecsHelper.RdfValue)),
                                             handler.CreateLiteralNode(literalValue, xsdString),
                                             graphIri));
                    if (!string.IsNullOrEmpty(language))
                    {
                        handler.HandleTriple(new Triple(
                                                 literalNode,
                                                 handler.CreateUriNode(UriFactory.Create(RdfSpecsHelper.RdfLanguage)),
                                                 handler.CreateLiteralNode(language, xsdString),
                                                 graphIri));
                    }

                    handler.HandleTriple(new Triple(
                                             literalNode,
                                             handler.CreateUriNode(UriFactory.Create(RdfSpecsHelper.RdfDirection)),
                                             handler.CreateLiteralNode(direction, xsdString),
                                             graphIri));

                    return(literalNode);
                }
                else
                {
                    literalValue = value.Value <string>();
                    if (datatype == null && language == null)
                    {
                        datatype = XsdNs + "string";
                    }
                }

                if (language != null)
                {
                    return(LanguageTag.IsWellFormed(language) ? handler.CreateLiteralNode(literalValue, language) : null);
                }
                return(handler.CreateLiteralNode(literalValue, new Uri(datatype)));
            }
            if (JsonLdUtils.IsListObject(token))
            {
                var listArray = token["@list"] as JArray;
                return(MakeRdfList(handler, listArray, graphIri));
            }

            if ((token as JObject)?.Property("@id") != null)
            {
                // Must be a node object
                var nodeObject = (JObject)token;
                return(MakeNode(handler, nodeObject["@id"], graphIri));
            }

            return(null);
        }
 public void TestWellFormedTags(string tag)
 {
     Assert.True(LanguageTag.IsWellFormed(tag),
                 $"Expected language tag {tag} to parse as well-formed");
 }