Ejemplo n.º 1
0
			public NodeMapNode(JsonLdApi _enclosing, string id) : base()
			{
				this._enclosing = _enclosing;
				this["@id"] = id;
			}
Ejemplo n.º 2
0
		/// <summary>
		/// Embeds values for the given subject and property into the given output
		/// during the framing algorithm.
		/// </summary>
		/// <remarks>
		/// Embeds values for the given subject and property into the given output
		/// during the framing algorithm.
		/// </remarks>
		/// <param name="state">the current framing state.</param>
		/// <param name="element">the subject.</param>
		/// <param name="property">the property.</param>
		/// <param name="output">the output.</param>
		private void EmbedValues(JsonLdApi.FramingContext state, JObject element, string property, JToken output)
		{
			// embed subject properties in output
			JArray objects = (JArray)element[property];
			foreach (JToken o in objects)
			{
                var eachObj = o;

                if (eachObj is JObject && ((JObject)eachObj).ContainsKey("@list"))
                {
                    JObject list = new JObject { { "@list", new JArray() } };
                    if (output is JArray)
                    {
                        ((JArray)output).Add(list);
                    }
                    else
                    {
                        output[property] = new JArray(list);
                    }
                    EmbedValues(state, (JObject)eachObj, "@list", list["@list"]);
                }
                // handle subject reference
                else if (JsonLdUtils.IsNodeReference(eachObj))
				{
					string sid = (string)((JObject)eachObj)["@id"];
					// embed full subject if isn't already embedded
					if (!state.embeds.ContainsKey(sid))
					{
						// add embed
						JsonLdApi.EmbedNode embed = new JsonLdApi.EmbedNode(this);
						embed.parent = output;
						embed.property = property;
						state.embeds[sid] = embed;
						// recurse into subject
						eachObj = new JObject();
						JObject s = (JObject)this.nodeMap[sid];
						if (s == null)
						{
							s = new JObject();
							s["@id"] = sid;
						}
						foreach (string prop in s.GetKeys())
						{
							// copy keywords
							if (JsonLdUtils.IsKeyword(prop))
							{
								((JObject)eachObj)[prop] = JsonLdUtils.Clone(s[prop]);
								continue;
							}
							EmbedValues(state, s, prop, eachObj);
						}
					}
					AddFrameOutput(state, output, property, eachObj);
				}
				else
				{
					// copy non-subject value
					AddFrameOutput(state, output, property, JsonLdUtils.Clone(eachObj));
				}
			}
		}
Ejemplo n.º 3
0
			public UsagesNode(JsonLdApi _enclosing, JsonLdApi.NodeMapNode node, string property
				, JObject value)
			{
				this._enclosing = _enclosing;
				this.node = node;
				this.property = property;
				this.value = value;
			}
Ejemplo n.º 4
0
		/// <exception cref="JsonLD.Core.JsonLdError"></exception>
		private bool FilterNode(JsonLdApi.FramingContext state, JObject node, JObject frame)
		{
			JToken types = frame["@type"];
			if (!types.IsNull())
			{
				if (!(types is JArray))
				{
					throw new JsonLdError(JsonLdError.Error.SyntaxError, "frame @type must be an array"
						);
				}
                JToken nodeTypes = node["@type"];
				if (nodeTypes.IsNull())
				{
					nodeTypes = new JArray();
				}
				else
				{
					if (!(nodeTypes is JArray))
					{
						throw new JsonLdError(JsonLdError.Error.SyntaxError, "node @type must be an array"
							);
					}
				}
				if (((JArray)types).Count == 1 && ((JArray)types)[0] is JObject
					 && ((JObject)((JArray)types)[0]).Count == 0)
				{
					return !((JArray)nodeTypes).IsEmpty();
				}
				else
				{
					foreach (JToken i in (JArray)nodeTypes)
					{
                        foreach (JToken j in (JArray)types)
						{
							if (JsonLdUtils.DeepCompare(i, j))
							{
								return true;
							}
						}
					}
					return false;
				}
			}
			else
			{
				foreach (string key in frame.GetKeys())
				{
					if ("@id".Equals(key) || !JsonLdUtils.IsKeyword(key) && !(node.ContainsKey(key)))
					{
						return false;
					}
				}
				return true;
			}
		}
Ejemplo n.º 5
0
		/// <summary>Adds framing output to the given parent.</summary>
		/// <remarks>Adds framing output to the given parent.</remarks>
		/// <param name="state">the current framing state.</param>
		/// <param name="parent">the parent to add to.</param>
		/// <param name="property">the parent property.</param>
		/// <param name="output">the output to add.</param>
		private static void AddFrameOutput(JsonLdApi.FramingContext state, JToken parent, 
			string property, JToken output)
		{
            if (parent is JObject)
			{
				JArray prop = (JArray)((JObject)parent)[property];
				if (prop == null)
				{
					prop = new JArray();
					((JObject)parent)[property] = prop;
				}
				prop.Add(output);
			}
			else
			{
				((JArray)parent).Add(output);
			}
		}
Ejemplo n.º 6
0
		/// <exception cref="JsonLD.Core.JsonLdError"></exception>
		private JObject FilterNodes(JsonLdApi.FramingContext state, JObject nodes, JObject frame)
		{
			JObject rval = new JObject();
			foreach (string id in nodes.GetKeys())
			{
				JObject element = (JObject)nodes[id];
				if (element != null && FilterNode(state, element, frame))
				{
					rval[id] = element;
				}
			}
			return rval;
		}
Ejemplo n.º 7
0
		/// <summary>Removes an existing embed.</summary>
		/// <remarks>Removes an existing embed.</remarks>
		/// <param name="state">the current framing state.</param>
		/// <param name="id">the @id of the embed to remove.</param>
		private static void RemoveEmbed(JsonLdApi.FramingContext state, string id)
		{
			// get existing embed
			IDictionary<string, JsonLdApi.EmbedNode> embeds = state.embeds;
			JsonLdApi.EmbedNode embed = embeds[id];
            JToken parent = embed.parent;
			string property = embed.property;
			// create reference to replace embed
			JObject node = new JObject();
			node["@id"] = id;
			// remove existing embed
			if (JsonLdUtils.IsNode(parent))
			{
				// replace subject with reference
				JArray newvals = new JArray();
				JArray oldvals = (JArray)((JObject)parent)[property
					];
				foreach (JToken v in oldvals)
				{
					if (v is JObject && ((JObject)v)["@id"].SafeCompare(id))
					{
						newvals.Add(node);
					}
					else
					{
						newvals.Add(v);
					}
				}
				((JObject)parent)[property] = newvals;
			}
			// recursively remove dependent dangling embeds
			RemoveDependents(embeds, id);
		}
Ejemplo n.º 8
0
		/// <summary>Frames subjects according to the given frame.</summary>
		/// <remarks>Frames subjects according to the given frame.</remarks>
		/// <param name="state">the current framing state.</param>
		/// <param name="subjects">the subjects to filter.</param>
		/// <param name="frame">the frame.</param>
		/// <param name="parent">the parent subject or top-level array.</param>
		/// <param name="property">the parent property, initialized to null.</param>
		/// <exception cref="JSONLDProcessingError">JSONLDProcessingError</exception>
		/// <exception cref="JsonLD.Core.JsonLdError"></exception>
		private void Frame(JsonLdApi.FramingContext state, JObject nodes
			, JObject frame, JToken parent, string property)
		{
			// filter out subjects that match the frame
			JObject matches = FilterNodes(state, nodes, frame);
			// get flags for current frame
			bool embedOn = GetFrameFlag(frame, "@embed", state.embed);
			bool explicitOn = GetFrameFlag(frame, "@explicit", state.@explicit);
			// add matches to output
			JArray ids = new JArray(matches.GetKeys());
			ids.SortInPlace();
			foreach (string id in ids)
			{
				if (property == null)
				{
					state.embeds = new Dictionary<string, JsonLdApi.EmbedNode>();
				}
				// start output
				JObject output = new JObject();
				output["@id"] = id;
				// prepare embed meta info
				JsonLdApi.EmbedNode embeddedNode = new JsonLdApi.EmbedNode(this);
				embeddedNode.parent = parent;
				embeddedNode.property = property;
				// if embed is on and there is an existing embed
				if (embedOn && state.embeds.ContainsKey(id))
				{
					JsonLdApi.EmbedNode existing = state.embeds[id];
					embedOn = false;
					if (existing.parent is JArray)
					{
						foreach (JToken p in (JArray)(existing.parent))
						{
							if (JsonLdUtils.CompareValues(output, p))
							{
								embedOn = true;
								break;
							}
						}
					}
					else
					{
						// existing embed's parent is an object
						if (((JObject)existing.parent).ContainsKey(existing.property))
						{
							foreach (JToken v in (JArray)((JObject)existing.parent)[existing.property])
							{
								if (v is JObject && ((JObject)v)["@id"].SafeCompare(id))
								{
									embedOn = true;
									break;
								}
							}
						}
					}
					// existing embed has already been added, so allow an overwrite
					if (embedOn)
					{
						RemoveEmbed(state, id);
					}
				}
				// not embedding, add output without any other properties
				if (!embedOn)
				{
					AddFrameOutput(state, parent, property, output);
				}
				else
				{
					// add embed meta info
					state.embeds[id] = embeddedNode;
					// iterate over subject properties
					JObject element = (JObject)matches[id];
					JArray props = new JArray(element.GetKeys());
					props.SortInPlace();
					foreach (string prop in props)
					{
						// copy keywords to output
						if (JsonLdUtils.IsKeyword(prop))
						{
							output[prop] = JsonLdUtils.Clone(element[prop]);
							continue;
						}
						// if property isn't in the frame
						if (!frame.ContainsKey(prop))
						{
							// if explicit is off, embed values
							if (!explicitOn)
							{
								EmbedValues(state, element, prop, output);
							}
							continue;
						}
						// add objects
						JArray value = (JArray)element[prop];
						foreach (JToken item in value)
						{
							// recurse into list
                            if ((item is JObject) && ((JObject)item).ContainsKey("@list"))
							{
								// add empty list
								JObject list = new JObject();
								list["@list"] = new JArray();
								AddFrameOutput(state, output, prop, list);
								// add list objects
                                foreach (JToken listitem in (JArray)((JObject)item)["@list"
									])
								{
									// recurse into subject reference
									if (JsonLdUtils.IsNodeReference(listitem))
									{
										JObject tmp = new JObject();
                                        string itemid = (string)((IDictionary<string, JToken>)listitem)["@id"];
										// TODO: nodes may need to be node_map,
										// which is global
										tmp[itemid] = this.nodeMap[itemid];
                                        Frame(state, tmp, (JObject)((JArray)frame[prop])[0], list
											, "@list");
									}
									else
									{
										// include other values automatcially (TODO:
										// may need JsonLdUtils.clone(n))
										AddFrameOutput(state, list, "@list", listitem);
									}
								}
							}
							else
							{
								// recurse into subject reference
								if (JsonLdUtils.IsNodeReference(item))
								{
                                    JObject tmp = new JObject();
                                    string itemid = (string)((JObject)item)["@id"];
									// TODO: nodes may need to be node_map, which is
									// global
									tmp[itemid] = this.nodeMap[itemid];
									Frame(state, tmp, (JObject)((JArray)frame[prop])[0], output
										, prop);
								}
								else
								{
									// include other values automatically (TODO: may
									// need JsonLdUtils.clone(o))
									AddFrameOutput(state, output, prop, item);
								}
							}
						}
					}
					// handle defaults
					props = new JArray(frame.GetKeys());
					props.SortInPlace();
					foreach (string prop_1 in props)
					{
						// skip keywords
						if (JsonLdUtils.IsKeyword(prop_1))
						{
							continue;
						}
						JArray pf = (JArray)frame[prop_1];
						JObject propertyFrame = pf.Count > 0 ? (JObject)pf[0] : null;
						if (propertyFrame == null)
						{
							propertyFrame = new JObject();
						}
						bool omitDefaultOn = GetFrameFlag(propertyFrame, "@omitDefault", state.omitDefault
							);
						if (!omitDefaultOn && !output.ContainsKey(prop_1))
						{
							JToken def = "@null";
							if (propertyFrame.ContainsKey("@default"))
							{
								def = JsonLdUtils.Clone(propertyFrame["@default"]);
							}
							if (!(def is JArray))
							{
								JArray tmp = new JArray();
								tmp.Add(def);
								def = tmp;
							}
							JObject tmp1 = new JObject();
							tmp1["@preserve"] = def;
							JArray tmp2 = new JArray();
							tmp2.Add(tmp1);
							output[prop_1] = tmp2;
						}
					}
					// add output to parent
					AddFrameOutput(state, parent, property, output);
				}
			}
		}
Ejemplo n.º 9
0
			internal EmbedNode(JsonLdApi _enclosing)
			{
				this._enclosing = _enclosing;
			}
Ejemplo n.º 10
0
			public FramingContext(JsonLdApi _enclosing)
			{
				this._enclosing = _enclosing;
				this.embed = true;
				this.@explicit = false;
				this.omitDefault = false;
				this.embeds = null;
			}