Beispiel #1
0
        /// <inheritdoc/>
        public void Load(IRdfHandler handler, TextReader input)
        {
            handler.StartRdf();
            var rdfTypeNode = handler.CreateUriNode(new Uri(RdfNs + "type"));

            try
            {
                JToken element;
                using (var reader = new JsonTextReader(input)
                {
                    DateParseHandling = DateParseHandling.None
                })
                {
                    element = JToken.ReadFrom(reader);
                }
                var expandedElement = JsonLdProcessor.Expand(element, ParserOptions);
                var nodeMap         = JsonLdProcessor.GenerateNodeMap(expandedElement);
                foreach (var p in nodeMap.Properties())
                {
                    var graphName = p.Name;
                    var graph     = p.Value as JObject;
                    if (graph == null)
                    {
                        continue;
                    }
                    Uri graphIri;
                    if (graphName == "@default")
                    {
                        graphIri = null;
                    }
                    else
                    {
                        if (!Uri.TryCreate(graphName, UriKind.Absolute, out graphIri))
                        {
                            continue;
                        }
                    }
                    foreach (var gp in graph.Properties())
                    {
                        var   subject = gp.Name;
                        var   node    = gp.Value as JObject;
                        INode subjectNode;
                        if (IsBlankNodeIdentifier(subject))
                        {
                            subjectNode = handler.CreateBlankNode(subject.Substring(2));
                        }
                        else
                        {
                            Uri subjectIri;
                            if (!Uri.TryCreate(subject, UriKind.Absolute, out subjectIri))
                            {
                                continue;
                            }
                            subjectNode = handler.CreateUriNode(subjectIri);
                        }
                        foreach (var np in node.Properties())
                        {
                            var property = np.Name;
                            var values   = np.Value as JArray;
                            if (property.Equals("@type"))
                            {
                                foreach (var type in values)
                                {
                                    var typeNode = MakeNode(handler, type);
                                    handler.HandleTriple(new Triple(subjectNode, rdfTypeNode, typeNode, graphIri));
                                }
                            }
                            else if (JsonLdProcessor.IsKeyword(property))
                            {
                                continue;
                            }
                            else if (JsonLdProcessor.IsBlankNodeIdentifier(property) && !ParserOptions.ProduceGeneralizedRdf)
                            {
                                continue;
                            }
                            else if (JsonLdProcessor.IsRelativeIri(property))
                            {
                                continue;
                            }
                            else
                            {
                                foreach (var item in values)
                                {
                                    var predicateNode = MakeNode(handler, property);
                                    var objectNode    = MakeNode(handler, item);
                                    if (objectNode != null)
                                    {
                                        handler.HandleTriple(new Triple(subjectNode, predicateNode, objectNode,
                                                                        graphIri));
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
                handler.EndRdf(false);
                throw;
            }
            handler.EndRdf(true);
        }