private void CreateTermDefinition(string term, Context context, JObject localContext, IDictionary<string, bool> defined) { if (defined.ContainsKey(term)) { if (!defined[term]) { throw new InvalidOperationException("Cyclic IRI mapping detected."); } return; } if (IsKeyWord(term)) { throw new InvalidOperationException("Keyword redefinition detected."); } defined[term] = false; context.Remove(term); JToken value = ((localContext != null) && (localContext.Property(term) != null) ? localContext[term].DeepClone() : null); if ((value == null) || ((value is JValue) && (((JValue)value).Value == null)) || ((value is JObject) && (((JObject)value).Property(Id) != null) && (((JObject)value).Property(Id).ValueEquals(null)))) { context[term] = null; return; } else if ((value is JValue) && (((JValue)value).Type == JTokenType.String)) { JObject newObject = new JObject(); newObject[Id] = value; value = newObject; } else if (!(value is JObject)) { throw new InvalidOperationException("Invalid term definition."); } TermDefinition definition = new TermDefinition() { Original = value }; if (((JObject)value).Property(Type) != null) { if (!((JObject)value).Property(Type).ValueIs<string>()) { throw new InvalidOperationException("Invalid type mapping."); } string result = ((JObject)value).Property(Type).ValueAs<string>(); result = ExpandIri(result, context, localContext, false, true, defined); if ((result != Id) && (result != Vocab) && (!Regex.IsMatch(result, "[a-zA-Z0-9_]+:.+"))) { throw new InvalidOperationException("Invalid type mapping."); } definition.Type = result; } if (((JObject)value).Property(Reverse) != null) { JProperty reverse = ((JObject)value).Property(Reverse); if (((JObject)value).Property(Id) != null) { throw new InvalidOperationException("Invalid reverse property."); } if (!reverse.ValueIs<string>()) { throw new InvalidOperationException("Invalid IRI mapping."); } definition.Iri = ExpandIri(reverse.ValueAs<string>(), context, localContext, false, true, defined); if (((JObject)value).Property(Container) != null) { definition.Container = (string)((JObject)value)[Container]; } definition.IsReverse = true; context[term] = definition; defined[term] = true; return; } definition.IsReverse = false; if ((value is JObject) && (((JObject)value).Property(Id) != null) && (!((JObject)value).Property(Id).ValueEquals(term))) { if ((!(((JObject)value).Property(Id).Value is JValue)) || (((JValue)((JObject)value).Property(Id).Value).Type != JTokenType.String)) { throw new InvalidOperationException("Invalid IRI mapping."); } string iri = ExpandIri((string)((JValue)((JObject)value).Property(Id).Value).Value, context, localContext, false, true, defined).ToString(); if ((!IsKeyWord((string)iri)) && (!Regex.IsMatch(iri, "[a-zA-Z0-9_]+:.+"))) { throw new InvalidOperationException("Invalid IRI mapping."); } if ((string)iri == Context) { throw new InvalidOperationException("Invalid keyword alias."); } definition.Iri = iri; } else if (term.IndexOf(':') != -1) { string prefix = null; if (Regex.IsMatch(term, "[a-zA-Z0-9_]+:[a-zA-Z0-9_]+")) { prefix = term.Substring(0, term.IndexOf(':')); if (localContext.Property(prefix) != null) { CreateTermDefinition(prefix, context, localContext, defined); } } if ((prefix != null) && (context.ContainsKey(prefix))) { definition.Iri = MakeAbsoluteUri(context[prefix].Iri, term.Substring(prefix.Length + 1)); } else { definition.Iri = term; } } else if (context.Vocabulary != null) { definition.Iri = MakeAbsoluteUri(context.Vocabulary, term); } else { throw new InvalidOperationException("Invalid IRI mapping."); } if (((JObject)value).Property(Container) != null) { if ((!((JObject)value).Property(Container).ValueEquals(List)) && (!((JObject)value).Property(Container).ValueEquals(Set)) && (!((JObject)value).Property(Container).ValueEquals(Index)) && (!((JObject)value).Property(Container).ValueEquals(Language))) { throw new InvalidOperationException("Invalid container mapping."); } definition.Container = (string)((JValue)((JObject)value).Property(Container).Value).Value; } if ((((JObject)value).Property(Language) != null) && ((((JObject)value).Property(Type) == null) || (((JObject)value).Property(Type).ValueEquals(null)))) { if ((((JObject)value).Property(Language).Value is JValue) && ((!((JObject)value).Property(Language).ValueEquals(null)) && (!((JObject)value).Property(Language).ValueIs<string>()))) { throw new InvalidOperationException("Invalid lanuage mapping."); } definition.Language = (!((JObject)value).Property(Language).ValueEquals(null) ? ((JObject)value).Property(Language).ValueAs<string>().ToLower() : "iv"); } context[term] = definition; defined[term] = true; }
private void CreateTermDefinition(string term, Context context, JObject localContext, IDictionary <string, bool> defined) { if (defined.ContainsKey(term)) { if (!defined[term]) { throw new InvalidOperationException("Cyclic IRI mapping detected."); } return; } if (IsKeyWord(term)) { throw new InvalidOperationException("Keyword redefinition detected."); } defined[term] = false; context.Remove(term); JToken value = ((localContext != null) && (localContext.Property(term) != null) ? localContext[term].DeepClone() : null); if ((value == null) || ((value is JValue) && (((JValue)value).Value == null)) || ((value is JObject) && (((JObject)value).Property(Id) != null) && (((JObject)value).Property(Id).ValueEquals(null)))) { context[term] = null; return; } else if ((value is JValue) && (((JValue)value).Type == JTokenType.String)) { JObject newObject = new JObject(); newObject[Id] = value; value = newObject; } else if (!(value is JObject)) { throw new InvalidOperationException("Invalid term definition."); } TermDefinition definition = new TermDefinition() { Original = value }; if (((JObject)value).Property(Type) != null) { if (!((JObject)value).Property(Type).ValueIs <string>()) { throw new InvalidOperationException("Invalid type mapping."); } string result = ((JObject)value).Property(Type).ValueAs <string>(); result = ExpandIri(result, context, localContext, false, true, defined); if ((result != Id) && (result != Vocab) && (!Regex.IsMatch(result, "[a-zA-Z0-9_]+:.+"))) { throw new InvalidOperationException("Invalid type mapping."); } definition.Type = result; } if (((JObject)value).Property(Reverse) != null) { JProperty reverse = ((JObject)value).Property(Reverse); if (((JObject)value).Property(Id) != null) { throw new InvalidOperationException("Invalid reverse property."); } if (!reverse.ValueIs <string>()) { throw new InvalidOperationException("Invalid IRI mapping."); } definition.Iri = ExpandIri(reverse.ValueAs <string>(), context, localContext, false, true, defined); if (((JObject)value).Property(Container) != null) { definition.Container = (string)((JObject)value)[Container]; } definition.IsReverse = true; context[term] = definition; defined[term] = true; return; } definition.IsReverse = false; if ((value is JObject) && (((JObject)value).Property(Id) != null) && (!((JObject)value).Property(Id).ValueEquals(term))) { if ((!(((JObject)value).Property(Id).Value is JValue)) || (((JValue)((JObject)value).Property(Id).Value).Type != JTokenType.String)) { throw new InvalidOperationException("Invalid IRI mapping."); } string iri = ExpandIri((string)((JValue)((JObject)value).Property(Id).Value).Value, context, localContext, false, true, defined).ToString(); if ((!IsKeyWord((string)iri)) && (!Regex.IsMatch(iri, "[a-zA-Z0-9_]+:.+"))) { throw new InvalidOperationException("Invalid IRI mapping."); } if ((string)iri == Context) { throw new InvalidOperationException("Invalid keyword alias."); } definition.Iri = iri; } else if (term.IndexOf(':') != -1) { string prefix = null; if (Regex.IsMatch(term, "[a-zA-Z0-9_]+:[a-zA-Z0-9_]+")) { prefix = term.Substring(0, term.IndexOf(':')); if (localContext.Property(prefix) != null) { CreateTermDefinition(prefix, context, localContext, defined); } } if ((prefix != null) && (context.ContainsKey(prefix))) { definition.Iri = MakeAbsoluteUri(context[prefix].Iri, term.Substring(prefix.Length + 1)); } else { definition.Iri = term; } } else if (context.Vocabulary != null) { definition.Iri = MakeAbsoluteUri(context.Vocabulary, term); } else { throw new InvalidOperationException("Invalid IRI mapping."); } if (((JObject)value).Property(Container) != null) { if ((!((JObject)value).Property(Container).ValueEquals(List)) && (!((JObject)value).Property(Container).ValueEquals(Set)) && (!((JObject)value).Property(Container).ValueEquals(Index)) && (!((JObject)value).Property(Container).ValueEquals(Language))) { throw new InvalidOperationException("Invalid container mapping."); } definition.Container = (string)((JValue)((JObject)value).Property(Container).Value).Value; } if ((((JObject)value).Property(Language) != null) && ((((JObject)value).Property(Type) == null) || (((JObject)value).Property(Type).ValueEquals(null)))) { if ((((JObject)value).Property(Language).Value is JValue) && ((!((JObject)value).Property(Language).ValueEquals(null)) && (!((JObject)value).Property(Language).ValueIs <string>()))) { throw new InvalidOperationException("Invalid lanuage mapping."); } definition.Language = (!((JObject)value).Property(Language).ValueEquals(null) ? ((JObject)value).Property(Language).ValueAs <string>().ToLower() : "iv"); } context[term] = definition; defined[term] = true; }