private string ExtractTypeNameFromSchema(JsonSchema jSchema) { string reference = jSchema.Ref(); if (reference != null) { return(ExtractTypeNameFromDefinitionReference(reference)); } TypeKeyword type = jSchema.Get <TypeKeyword>(); if (type != null) { if (type.Value == JsonSchemaType.Array) { List <JsonSchema> items = jSchema.Items(); return(ExtractTypeNameFromSchema(items[0])); } if (type.Value == JsonSchemaType.Object) { return(jSchema.Title()); } return(type.Value.ToString()); } return(null); }
private string FollowValueType(JsonSchema jSchema) { TypeKeyword topType = jSchema.Get <TypeKeyword>(); if (topType != null) { return(HandleJsonTypes(jSchema)); } string reference = jSchema.Ref(); if (reference != null) { JsonSchema nextSchema = definitions.GetValueOrDefault(ExtractTypeNameFromDefinitionReference(reference)); if (nextSchema != null) { TypeKeyword type = nextSchema.Get <TypeKeyword>(); if (type != null) { return(HandleJsonTypes(nextSchema)); } else { return(FollowValueType(nextSchema)); } } } return(null); }
public ValueKeyword(string name, TypeKeyword type) : base(name, type) { Name = name; Type = type; Number = Convert.ToInt32(name); }
public StructType(TypeKeyword type, List <IDataType>?typeArguments, CheckedClassDeclStatement structDecl, bool isExplicitPointer = false) { Type = type; TypeArguments = typeArguments; StructDecl = structDecl; IsExplicitPointer = isExplicitPointer; }
private XmlQualifiedName GetTypeName(JsonSchema jSchema) { string referencedType = GetterExtensions.Ref(jSchema); if (!string.IsNullOrEmpty(referencedType)) { return(new XmlQualifiedName(ExtractTypeFromDefinitionReference(referencedType))); } TypeKeyword type = jSchema.Get <TypeKeyword>(); if (type != null) { switch (type.Value) { case JsonSchemaType.String: { string format = GetterExtensions.Format(jSchema); if (format != null) { return(ExtractBaseTypeNameFromFormat(format)); } return(new XmlQualifiedName("string", XML_SCHEMA_NS)); } case JsonSchemaType.Integer: return(new XmlQualifiedName("integer", XML_SCHEMA_NS)); case JsonSchemaType.Number: return(new XmlQualifiedName("decimal", XML_SCHEMA_NS)); case JsonSchemaType.Boolean: return(new XmlQualifiedName("boolean", XML_SCHEMA_NS)); case JsonSchemaType.Array: { List <JsonSchema> itemsSchemas = GetterExtensions.Items(jSchema); JsonSchema itemSchema = itemsSchemas.ToArray()[0]; string itemsReferencedType = GetterExtensions.Ref(itemSchema); if (!string.IsNullOrEmpty(itemsReferencedType)) { return(new XmlQualifiedName(ExtractTypeFromDefinitionReference(itemsReferencedType))); } return(null); } } return(null); } return(null); }
private string HandleJsonTypes(JsonSchema jSchema) { TypeKeyword type = jSchema.Get <TypeKeyword>(); if (type != null) { switch (type.Value) { case JsonSchemaType.String: { FormatKeyword format = jSchema.Get <FormatKeyword>(); if (format != null && format.Value != null && !string.IsNullOrEmpty(format.Value)) { return(HandleFormatTypes(format.Value)); } return("string"); } case JsonSchemaType.Boolean: return("boolean"); case JsonSchemaType.Number: return("decimal"); case JsonSchemaType.Integer: { double?minimum = jSchema.Minimum(); if (minimum > 0.0) { return("positiveInteger"); } else if (minimum == 0.0) { return("nonNegativeInteger"); } else { return("integer"); } } } } return(null); }
private void TraverseModell(string parentPath, string parentTypeName, string propertyName, JsonSchema propertyType, bool isRequired, ISet <string> alreadyVisitedTypes, JsonSchema parentType, string parentXpath) { string sanitizedPropertyName = SanitizeName(propertyName); string xPath; string path; int index = 0; do { path = (string.IsNullOrEmpty(parentPath) ? string.Empty : parentPath + ".") + sanitizedPropertyName; xPath = (string.IsNullOrEmpty(parentXpath) ? "/" : parentXpath + "/") + propertyName; if (++index >= 2) { path += index.ToString(); xPath += index.ToString(); } }while (elements.ContainsKey(path)); // exclude elements that does not start with firstPropertyName if (!path.StartsWith(firstPropertyName)) { return; } string minItems = "0"; string maxItems = "1"; TypeKeyword type = propertyType.Get <TypeKeyword>(); if (type != null && type.Value == JsonSchemaType.Array) { List <JsonSchema> items = propertyType.Items(); path += multiplicityString; FollowRef(path, items[0], alreadyVisitedTypes, xPath); // TODO fix multiple item types. It now uses only the first double?minItemsValue = propertyType.MinItems(); double?maxItemsValue = propertyType.MaxItems(); if (minItemsValue.HasValue) { minItems = minItemsValue.ToString(); } maxItems = "*"; if (maxItemsValue.HasValue && maxItemsValue.Value != MagicNumberMaxOccurs) { maxItems = maxItemsValue.ToString(); } } else { FollowRef(path, propertyType, alreadyVisitedTypes, xPath); if (isRequired) { minItems = "1"; } } JsonObject result = new JsonObject(); string inputType = "Field"; string xsdType = propertyType.OtherData.TryGetString("@xsdType"); result.Add("ID", RemoveStarsFromPath(path)); string parentElement = ExtractParent(path); if (parentElement != null) { result.Add("ParentElement", RemoveStarsFromPath(parentElement)); } string xsdValueType = FollowValueType(propertyType); string typeName = ExtractTypeNameFromSchema(propertyType); if (typeName != null) { result.Add("TypeName", SanitizeName(typeName)); } result.Add("Name", sanitizedPropertyName); string fixedValue = null; JsonValue fixedValueJson = propertyType.Const(); if (fixedValueJson != null) { fixedValue = fixedValueJson.String; } if (xsdType != null && xsdType.Equals("XmlAttribute")) { inputType = "Attribute"; } else if ((type == null && xsdValueType == null) || (type != null && (type.Value == JsonSchemaType.Object || type.Value == JsonSchemaType.Array))) { inputType = "Group"; } int maxOccursParsed = maxItems.Equals("*") ? MagicNumberMaxOccurs : int.Parse(maxItems); if ((!inputType.Equals("Group") && string.IsNullOrEmpty(fixedValue)) || (inputType.Equals("Group") && maxOccursParsed > 1)) { string dataBindingNameWithoutFirstPropertyName = $"{parentXpath}/{propertyName}".Replace("/" + firstPropertyName + "/", string.Empty); string dataBindingName = dataBindingNameWithoutFirstPropertyName.Replace("/", "."); result.Add("DataBindingName", dataBindingName); } result.Add("XPath", xPath); result.Add("Restrictions", ExtractRestrictions(xsdValueType, propertyType)); result.Add("Type", inputType); if (!string.IsNullOrEmpty(xsdValueType)) { result.Add("XsdValueType", char.ToUpper(xsdValueType[0]) + xsdValueType.Substring(1)); // Uppercase first character } if (path.EndsWith(".value")) { result.Add("Texts", ExtractTexts(parentElement.Split(".").Last(), parentType)); result.Add("IsTagContent", true); } else { result.Add("Texts", ExtractTexts(propertyName, propertyType)); } result.Add("CustomProperties", new JsonObject()); // ?? result.Add("MaxOccurs", maxOccursParsed); result.Add("MinOccurs", int.Parse(minItems)); result.Add("XName", propertyName); if (fixedValue != null) { result.Add("FixedValue", fixedValue); } string jsonSchemaPointer = "#/properties/" + propertyName; if (parentElement != null) { jsonSchemaPointer = "#/definitions/" + parentTypeName + "/properties/" + propertyName; } result.Add("JsonSchemaPointer", jsonSchemaPointer); string cardinality = "[" + minItems + ".." + maxItems + "]"; string displayString = RemoveLastStar(path) + " : " + cardinality + " " + SanitizeName(typeName); result.Add("DisplayString", displayString); // TODO ..., XmlSchemaReference elements.Add(RemoveStarsFromPath(path), result); }
private static void KeywordEqual(TypeKeyword expected, TypeKeyword actual) { Assert.Equal(expected.Type, actual.Type); }
private static XmlSchemaSimpleTypeRestriction ExtractNumberAndIntegerFacets(JsonSchema jSchema, TypeKeyword type) { XmlSchemaSimpleTypeRestriction content = new XmlSchemaSimpleTypeRestriction(); double? minValue = GetterExtensions.Minimum(jSchema); double? minExclusiveValue = GetterExtensions.ExclusiveMinimum(jSchema); if (type.Value == JsonSchemaType.Number) { content.BaseTypeName = new XmlQualifiedName("decimal", XML_SCHEMA_NS); } else if (type.Value == JsonSchemaType.Integer) { if (minValue != null && minValue == 0.0) { content.BaseTypeName = new XmlQualifiedName("positiveInteger", XML_SCHEMA_NS); } else { content.BaseTypeName = new XmlQualifiedName("integer", XML_SCHEMA_NS); } } if (minValue != null || minExclusiveValue != null) { if (minValue != null) { XmlSchemaMinInclusiveFacet facet = new XmlSchemaMinInclusiveFacet { Value = FormatDouble((double)minValue), }; content.Facets.Add(facet); } else { XmlSchemaMinExclusiveFacet facet = new XmlSchemaMinExclusiveFacet { Value = FormatDouble((double)minExclusiveValue), }; content.Facets.Add(facet); } } double? maxValue = GetterExtensions.Maximum(jSchema); double? maxExclusiveValue = GetterExtensions.ExclusiveMaximum(jSchema); if (maxValue != null || maxExclusiveValue != null) { if (maxValue != null) { XmlSchemaMaxInclusiveFacet maxInclusiveFacet = new XmlSchemaMaxInclusiveFacet { Value = FormatDouble((double)maxValue), }; content.Facets.Add(maxInclusiveFacet); } else { XmlSchemaMaxExclusiveFacet maxExclusiveFacet = new XmlSchemaMaxExclusiveFacet { Value = FormatDouble((double)maxExclusiveValue), }; content.Facets.Add(maxExclusiveFacet); } } return content; }
public Keyword(string name, TypeKeyword type) { Name = name; Type = type; }
private static XmlSchemaSimpleTypeRestriction ExtractNumberAndIntegerFacets(JsonSchema jSchema, TypeKeyword type) { XmlSchemaSimpleTypeRestriction content = new XmlSchemaSimpleTypeRestriction(); double?minValue = GetterExtensions.Minimum(jSchema); double?minExclusiveValue = GetterExtensions.ExclusiveMinimum(jSchema); if (type.Value == JsonSchemaType.Number) { content.BaseTypeName = new XmlQualifiedName("decimal", XML_SCHEMA_NS); } else if (type.Value == JsonSchemaType.Integer) { if (minValue != null && minValue == 1) { content.BaseTypeName = new XmlQualifiedName("positiveInteger", XML_SCHEMA_NS); } else { content.BaseTypeName = new XmlQualifiedName("integer", XML_SCHEMA_NS); } } double?maxValue = GetterExtensions.Maximum(jSchema); double?maxExclusiveValue = GetterExtensions.ExclusiveMaximum(jSchema); Regex regex = new Regex("^[9]+$"); if ((minValue != null && maxValue != null) && Math.Abs((double)minValue).Equals(maxValue) && regex.IsMatch(maxValue.ToString())) { XmlSchemaTotalDigitsFacet facet = new XmlSchemaTotalDigitsFacet { Value = FormatDouble(maxValue.ToString().Length), }; content.Facets.Add(facet); } else { if (minValue != null || minExclusiveValue != null) { if (minValue != null) { XmlSchemaMinInclusiveFacet facet = new XmlSchemaMinInclusiveFacet { Value = FormatDouble((double)minValue), }; content.Facets.Add(facet); } else { XmlSchemaMinExclusiveFacet facet = new XmlSchemaMinExclusiveFacet { Value = FormatDouble((double)minExclusiveValue), }; content.Facets.Add(facet); } } if (maxValue != null || maxExclusiveValue != null) { if (maxValue != null) { XmlSchemaMaxInclusiveFacet maxInclusiveFacet = new XmlSchemaMaxInclusiveFacet { Value = FormatDouble((double)maxValue), }; content.Facets.Add(maxInclusiveFacet); } else { XmlSchemaMaxExclusiveFacet maxExclusiveFacet = new XmlSchemaMaxExclusiveFacet { Value = FormatDouble((double)maxExclusiveValue), }; content.Facets.Add(maxExclusiveFacet); } } } return(content); }
private static TypeKeywordAttr GetAttr(TypeKeyword p) { return (TypeKeywordAttr)Attribute.GetCustomAttribute(ForValue(p), typeof(TypeKeywordAttr)); }
private static MemberInfo ForValue(TypeKeyword p) { return typeof(TypeKeyword).GetField(Enum.GetName(typeof(TypeKeyword), p)); }
private readonly StringCache stringCache; // only used for validation that all strings are interned (disabled) public NodeQueryMatcher( string query, IEnumerable <string> stringTable, CancellationToken cancellationToken = default, StringCache stringCache = null // validation disabled in production ) { this.stringCache = stringCache; query = PreprocessQuery(query); this.Query = query; var rawWords = TextUtilities.Tokenize(query); this.Words = new List <Term>(rawWords.Count); foreach (var rawWord in rawWords) { var term = Term.Get(rawWord); if (term != default) { Words.Add(term); } } if (Words.Count == 1 && Words[0].Word is string potentialNodeIndex && potentialNodeIndex.Length > 1 && potentialNodeIndex[0] == '$') { var nodeIndexText = potentialNodeIndex.Substring(1); if (int.TryParse(nodeIndexText, out var nodeIndex)) { NodeIndex = nodeIndex; Words.RemoveAt(0); return; } } for (int i = Words.Count - 1; i >= 0; i--) { var word = Words[i].Word; if (word == "$time" || word == "$duration") { Words.RemoveAt(i); IncludeDuration = true; continue; } else if (word == "$start" || word == "$starttime") { Words.RemoveAt(i); IncludeStart = true; continue; } else if (word == "$end" || word == "$endtime") { Words.RemoveAt(i); IncludeEnd = true; continue; } if (word.Length > 2 && word[0] == '$' && word[1] != '(' && (TypeKeyword == null || !TypeKeyword.Contains(word.Substring(1).ToLowerInvariant()))) { Words.RemoveAt(i); TypeKeyword = word.Substring(1).ToLowerInvariant(); continue; } if (word.StartsWith("under(", StringComparison.OrdinalIgnoreCase) && word.EndsWith(")")) { word = word.Substring(6, word.Length - 7); Words.RemoveAt(i); var underMatcher = new NodeQueryMatcher(word, stringTable); IncludeMatchers.Add(underMatcher); continue; } if (word.StartsWith("notunder(", StringComparison.OrdinalIgnoreCase) && word.EndsWith(")")) { word = word.Substring(9, word.Length - 10); Words.RemoveAt(i); var underMatcher = new NodeQueryMatcher(word, stringTable); ExcludeMatchers.Add(underMatcher); continue; } if (word.StartsWith("project(", StringComparison.OrdinalIgnoreCase) && word.EndsWith(")")) { word = word.Substring(8, word.Length - 9); Words.RemoveAt(i); var underMatcher = new NodeQueryMatcher(word, stringTable); underMatcher.UnderProject = true; IncludeMatchers.Add(underMatcher); continue; } if (word.StartsWith("name=", StringComparison.OrdinalIgnoreCase) && word.Length > 5) { word = word.Substring(5, word.Length - 5); Words.RemoveAt(i); var term = Term.Get(word); if (term != default) { Words.Insert(i, term); nameToSearch = term; } continue; } if (word.StartsWith("value=", StringComparison.OrdinalIgnoreCase) && word.Length > 6) { word = word.Substring(6, word.Length - 6); Words.RemoveAt(i); var term = Term.Get(word); if (term != default) { Words.Insert(i, term); valueToSearch = term; } continue; } } PrecomputeMatchesInStrings(stringTable, cancellationToken); }
public NodeQueryMatcher(string query, IEnumerable <string> stringTable) { query = PreprocessQuery(query); this.Query = query; var rawWords = ParseIntoWords(query); this.Words = new List <Term>(rawWords.Count); foreach (var rawWord in rawWords) { var trimmed = TrimQuotes(rawWord); if (trimmed == rawWord) { Words.Add(new Term(rawWord)); } else if (!string.IsNullOrWhiteSpace(trimmed)) { Words.Add(new Term(trimmed, quotes: true)); } } if (Words.Count == 1 && Words[0].Word is string potentialNodeIndex && potentialNodeIndex.Length > 1 && potentialNodeIndex[0] == '$') { var nodeIndexText = potentialNodeIndex.Substring(1); if (int.TryParse(nodeIndexText, out var nodeIndex)) { NodeIndex = nodeIndex; Words.RemoveAt(0); return; } } for (int i = Words.Count - 1; i >= 0; i--) { var word = Words[i].Word; if (word == "$time" || word == "$duration") { Words.RemoveAt(i); IncludeDuration = true; continue; } else if (word == "$start" || word == "$starttime") { Words.RemoveAt(i); IncludeStart = true; continue; } else if (word == "$end" || word == "$endtime") { Words.RemoveAt(i); IncludeEnd = true; continue; } if (word.Length > 2 && word[0] == '$' && word[1] != '(' && (TypeKeyword == null || !TypeKeyword.Contains(word.Substring(1).ToLowerInvariant()))) { Words.RemoveAt(i); TypeKeyword = word.Substring(1).ToLowerInvariant(); continue; } if (word.StartsWith("under(", StringComparison.OrdinalIgnoreCase) && word.EndsWith(")")) { word = word.Substring(6, word.Length - 7); Words.RemoveAt(i); var underMatcher = new NodeQueryMatcher(word, stringTable); IncludeMatchers.Add(underMatcher); continue; } if (word.StartsWith("notunder(", StringComparison.OrdinalIgnoreCase) && word.EndsWith(")")) { word = word.Substring(9, word.Length - 10); Words.RemoveAt(i); var underMatcher = new NodeQueryMatcher(word, stringTable); ExcludeMatchers.Add(underMatcher); continue; } if (word.StartsWith("project(", StringComparison.OrdinalIgnoreCase) && word.EndsWith(")")) { word = word.Substring(8, word.Length - 9); Words.RemoveAt(i); var underMatcher = new NodeQueryMatcher(word, stringTable); underMatcher.UnderProject = true; IncludeMatchers.Add(underMatcher); continue; } if (word.StartsWith("name=", StringComparison.OrdinalIgnoreCase) && word.Length > 5) { word = word.Substring(5, word.Length - 5); Words.RemoveAt(i); Words.Insert(i, new Term(word)); nameToSearch = word; continue; } if (word.StartsWith("value=", StringComparison.OrdinalIgnoreCase) && word.Length > 6) { word = word.Substring(6, word.Length - 6); Words.RemoveAt(i); Words.Insert(i, new Term(word)); valueToSearch = word; continue; } } PrecomputeMatchesInStrings(stringTable); }
private XmlSchemaSimpleType ExtractSimpleType(string name, JsonSchema jSchema) { XmlSchemaSimpleType simpleType = new XmlSchemaSimpleType { Name = name, }; AddAnnotations(simpleType, jSchema); string reference = GetterExtensions.Ref(jSchema); if (reference != null) { XmlQualifiedName baseTypeName = new XmlQualifiedName(ExtractTypeFromDefinitionReference(reference)); XmlSchemaSimpleTypeRestriction simpleTypeRestriction = new XmlSchemaSimpleTypeRestriction { BaseTypeName = baseTypeName, }; XmlSchemaSimpleTypeRestriction stringFacets = ExtractStringFacets(jSchema); if (stringFacets.Facets.Count > 0) { foreach (XmlSchemaObject facet in stringFacets.Facets) { simpleTypeRestriction.Facets.Add(facet); } } XmlSchemaSimpleTypeRestriction numberFacets = ExtractNumberAndIntegerFacets(jSchema, new TypeKeyword(JsonSchemaType.Number)); if (numberFacets.Facets.Count > 0) { foreach (XmlSchemaObject facet in numberFacets.Facets) { simpleTypeRestriction.Facets.Add(facet); } } simpleType.Content = simpleTypeRestriction; return simpleType; } TypeKeyword type = jSchema.Get<TypeKeyword>(); if (type == null) { // assume string type if type is missing XmlSchemaSimpleTypeRestriction noTypeSimpleType = ExtractStringFacets(jSchema); noTypeSimpleType.BaseTypeName = null; simpleType.Content = noTypeSimpleType; return simpleType; } if (type.Value == JsonSchemaType.String) { simpleType.Content = ExtractStringFacets(jSchema); } else if (type.Value == JsonSchemaType.Number || type.Value == JsonSchemaType.Integer) { simpleType.Content = ExtractNumberAndIntegerFacets(jSchema, type); } else if (type.Value == JsonSchemaType.Boolean) { XmlSchemaSimpleTypeRestriction content = new XmlSchemaSimpleTypeRestriction { BaseTypeName = new XmlQualifiedName("boolean", XML_SCHEMA_NS), }; simpleType.Content = content; } else if (type.Value == JsonSchemaType.Array) { string xlist = jSchema.OtherData.TryGetString("@xsdType"); if (xlist.Equals("XmlList")) { XmlSchemaSimpleTypeList theList = new XmlSchemaSimpleTypeList(); List<JsonSchema> items = GetterExtensions.Items(jSchema); string typeRef = GetterExtensions.Ref(items[0]); theList.ItemTypeName = new XmlQualifiedName(ExtractTypeFromDefinitionReference(typeRef)); simpleType.Content = theList; } } return simpleType; }
public ValueDefenition(string typeName, double value, TypeKeyword type) { TypeName = typeName; Value = value; Type = type; }
public NodeQueryMatcher(string query, IEnumerable <string> stringTable) { this.Query = query; this.Words = ParseIntoWords(query); if (Words.Count == 1 && Words[0] is string potentialNodeIndex && potentialNodeIndex.Length > 1 && potentialNodeIndex[0] == '$') { var nodeIndexText = potentialNodeIndex.Substring(1); if (int.TryParse(nodeIndexText, out var nodeIndex)) { NodeIndex = nodeIndex; Words.RemoveAt(0); return; } } for (int i = Words.Count - 1; i >= 0; i--) { var word = Words[i]; if (word == "$time" || word == "$duration") { Words.RemoveAt(i); IncludeDuration = true; continue; } else if (word == "$start" || word == "$starttime") { Words.RemoveAt(i); IncludeStart = true; continue; } else if (word == "$end" || word == "$endtime") { Words.RemoveAt(i); IncludeEnd = true; continue; } if (word.Length > 2 && word[0] == '$' && word[1] != '(' && (TypeKeyword == null || !TypeKeyword.Contains(word.Substring(1).ToLowerInvariant()))) { Words.RemoveAt(i); TypeKeyword = word.Substring(1).ToLowerInvariant(); continue; } if (word.StartsWith("under(", StringComparison.OrdinalIgnoreCase) && word.EndsWith(")")) { word = word.Substring(6, word.Length - 7); Words.RemoveAt(i); var underMatcher = new NodeQueryMatcher(word, stringTable); IncludeMatchers.Add(underMatcher); continue; } if (word.StartsWith("notunder(", StringComparison.OrdinalIgnoreCase) && word.EndsWith(")")) { word = word.Substring(9, word.Length - 10); Words.RemoveAt(i); var underMatcher = new NodeQueryMatcher(word, stringTable); ExcludeMatchers.Add(underMatcher); continue; } if (word.StartsWith("name=", StringComparison.OrdinalIgnoreCase) && word.Length > 5) { word = word.Substring(5, word.Length - 5); Words.RemoveAt(i); Words.Insert(i, word); nameToSearch = word; continue; } if (word.StartsWith("value=", StringComparison.OrdinalIgnoreCase) && word.Length > 6) { word = word.Substring(6, word.Length - 6); Words.RemoveAt(i); Words.Insert(i, word); valueToSearch = word; continue; } } PrecomputeMatchesInStrings(stringTable); }
public ValueDefenition(string typeName, string romanValue, TypeKeyword type) { TypeName = typeName; RomanValue = romanValue; Type = type; }