IsKeyword() static private method

Returns whether or not the given value is a keyword (or a keyword alias).
Returns whether or not the given value is a keyword (or a keyword alias).
static private IsKeyword ( JToken key ) : bool
key JToken
return bool
 /// <summary>parses a @context object and sets any namespaces found within it</summary>
 /// <param name="context"></param>
 public virtual void ParseContext(JObject context)
 {
     foreach (string key in context.GetKeys())
     {
         JToken val = context[key];
         if ("@vocab".Equals(key))
         {
             if (val.IsNull() || JsonLdUtils.IsString(val))
             {
                 SetNamespace(string.Empty, (string)val);
             }
         }
         else
         {
             // TODO: the context is actually invalid, should we throw an
             // exception?
             if ("@context".Equals(key))
             {
                 // go deeper!
                 ParseContext((JObject)context["@context"]);
             }
             else
             {
                 if (!JsonLdUtils.IsKeyword(key))
                 {
                     // TODO: should we make sure val is a valid URI prefix (i.e. it
                     // ends with /# or ?)
                     // or is it ok that full URIs for terms are used?
                     if (val.Type == JTokenType.String)
                     {
                         SetNamespace(key, (string)context[key]);
                     }
                     else
                     {
                         if (JsonLdUtils.IsObject(val) && ((JObject)val).ContainsKey("@id"
                                                                                     ))
                         {
                             SetNamespace(key, (string)((JObject)val)["@id"]);
                         }
                     }
                 }
             }
         }
     }
 }
Example #2
0
 /// <summary>parses a @context object and sets any namespaces found within it</summary>
 /// <param name="context"></param>
 public virtual void ParseContext(JObject context)
 {
     foreach (var key in context.GetKeys())
     {
         var val = context[key];
         if ("@vocab".Equals(key))
         {
             if (val.IsNull() || JsonLdUtils.IsString(val))
             {
                 SetNamespace(string.Empty, (string)val);
             }
         }
         else
         {
             // TODO: the context is actually invalid, should we throw an
             // exception?
             if ("@context".Equals(key))
             {
                 // go deeper!
                 ParseContext((JObject)context["@context"]);
             }
             else
             {
                 if (!JsonLdUtils.IsKeyword(key))
                 {
                     if (val.Type == JTokenType.String)
                     {
                         SetNamespace(key, (string)context[key]);
                     }
                     else
                     {
                         if (JsonLdUtils.IsObject(val) && ((JObject)val).ContainsKey("@id"
                                                                                     ))
                         {
                             SetNamespace(key, (string)((JObject)val)["@id"]);
                         }
                     }
                 }
             }
         }
     }
 }
Example #3
0
        internal static JArray GraphToRDF(JObject graph, UniqueNamer
                                          namer)
        {
            // use RDFDataset.graphToRDF
            JArray rval = new JArray();

            foreach (string id in graph.GetKeys())
            {
                JObject node       = (JObject)graph[id];
                JArray  properties = new JArray(node.GetKeys());
                properties.SortInPlace();
                foreach (string property in properties)
                {
                    var    eachProperty = property;
                    JToken items        = node[eachProperty];
                    if ("@type".Equals(eachProperty))
                    {
                        eachProperty = JSONLDConsts.RdfType;
                    }
                    else
                    {
                        if (JsonLdUtils.IsKeyword(eachProperty))
                        {
                            continue;
                        }
                    }
                    foreach (JToken item in (JArray)items)
                    {
                        // RDF subjects
                        JObject subject = new JObject();
                        if (id.IndexOf("_:") == 0)
                        {
                            subject["type"]  = "blank node";
                            subject["value"] = namer.GetName(id);
                        }
                        else
                        {
                            subject["type"]  = "IRI";
                            subject["value"] = id;
                        }
                        // RDF predicates
                        JObject predicate = new JObject();
                        predicate["type"]  = "IRI";
                        predicate["value"] = eachProperty;
                        // convert @list to triples
                        if (JsonLdUtils.IsList(item))
                        {
                            ListToRDF((JArray)((JObject)item)["@list"], namer, subject
                                      , predicate, rval);
                        }
                        else
                        {
                            // convert value or node object to triple
                            object @object = ObjectToRDF(item, namer);
                            IDictionary <string, object> tmp = new Dictionary <string, object>();
                            tmp["subject"]   = subject;
                            tmp["predicate"] = predicate;
                            tmp["object"]    = @object;
                            rval.Add(tmp);
                        }
                    }
                }
            }
            return(rval);
        }
        /// <summary>Creates an array of RDF triples for the given graph.</summary>
        /// <remarks>Creates an array of RDF triples for the given graph.</remarks>
        /// <param name="graph">the graph to create RDF triples for.</param>
        internal virtual void GraphToRDF(string graphName, JObject graph
                                         )
        {
            // 4.2)
            IList <RDFDataset.Quad> triples = new List <RDFDataset.Quad>();
            // 4.3)
            IEnumerable <string> subjects = graph.GetKeys();

            // Collections.sort(subjects);
            foreach (string id in subjects)
            {
                if (JsonLdUtils.IsRelativeIri(id))
                {
                    continue;
                }
                JObject node       = (JObject)graph[id];
                JArray  properties = new JArray(node.GetKeys());
                properties.SortInPlace();
                foreach (string property in properties)
                {
                    var    localProperty = property;
                    JArray values;
                    // 4.3.2.1)
                    if ("@type".Equals(localProperty))
                    {
                        values        = (JArray)node["@type"];
                        localProperty = JSONLDConsts.RdfType;
                    }
                    else
                    {
                        // 4.3.2.2)
                        if (JsonLdUtils.IsKeyword(localProperty))
                        {
                            continue;
                        }
                        else
                        {
                            // 4.3.2.3)
                            if (localProperty.StartsWith("_:") && !api.opts.GetProduceGeneralizedRdf())
                            {
                                continue;
                            }
                            else
                            {
                                // 4.3.2.4)
                                if (JsonLdUtils.IsRelativeIri(localProperty))
                                {
                                    continue;
                                }
                                else
                                {
                                    values = (JArray)node[localProperty];
                                }
                            }
                        }
                    }
                    RDFDataset.Node subject;
                    if (id.IndexOf("_:") == 0)
                    {
                        // NOTE: don't rename, just set it as a blank node
                        subject = new RDFDataset.BlankNode(id);
                    }
                    else
                    {
                        subject = new RDFDataset.IRI(id);
                    }
                    // RDF predicates
                    RDFDataset.Node predicate;
                    if (localProperty.StartsWith("_:"))
                    {
                        predicate = new RDFDataset.BlankNode(localProperty);
                    }
                    else
                    {
                        predicate = new RDFDataset.IRI(localProperty);
                    }
                    foreach (JToken item in values)
                    {
                        // convert @list to triples
                        if (JsonLdUtils.IsList(item))
                        {
                            JArray          list       = (JArray)((JObject)item)["@list"];
                            RDFDataset.Node last       = null;
                            RDFDataset.Node firstBNode = nil;
                            if (!list.IsEmpty())
                            {
                                last       = ObjectToRDF(list[list.Count - 1]);
                                firstBNode = new RDFDataset.BlankNode(api.GenerateBlankNodeIdentifier());
                            }
                            triples.Add(new RDFDataset.Quad(subject, predicate, firstBNode, graphName));
                            for (int i = 0; i < list.Count - 1; i++)
                            {
                                RDFDataset.Node @object = ObjectToRDF(list[i]);
                                triples.Add(new RDFDataset.Quad(firstBNode, first, @object, graphName));
                                RDFDataset.Node restBNode = new RDFDataset.BlankNode(api.GenerateBlankNodeIdentifier
                                                                                         ());
                                triples.Add(new RDFDataset.Quad(firstBNode, rest, restBNode, graphName));
                                firstBNode = restBNode;
                            }
                            if (last != null)
                            {
                                triples.Add(new RDFDataset.Quad(firstBNode, first, last, graphName));
                                triples.Add(new RDFDataset.Quad(firstBNode, rest, nil, graphName));
                            }
                        }
                        else
                        {
                            // convert value or node object to triple
                            RDFDataset.Node @object = ObjectToRDF(item);
                            if (@object != null)
                            {
                                triples.Add(new RDFDataset.Quad(subject, predicate, @object, graphName));
                            }
                        }
                    }
                }
            }
            this[graphName] = triples;
        }