예제 #1
0
        private void AddInfo(XmlSchema xSchema, JsonSchema jSchema)
        {
            InfoKeyword info = jSchema.Get<InfoKeyword>();
            if (info != null)
            {
                XmlSchemaAnnotation annotation = new XmlSchemaAnnotation();
                XmlSchemaDocumentation documentation = new XmlSchemaDocumentation();
                annotation.Items.Add(documentation);

                List<XmlElement> elements = new List<XmlElement>();
                JsonObject infoObject = info.ToJson(new JsonSerializer()).Object;
                foreach (string attributeName in infoObject.Keys)
                {
                    XmlElement attrElement = xmlDocument.CreateElement("xs", "attribute", XML_SCHEMA_NS);
                    attrElement.SetAttribute("name", attributeName);
                    attrElement.SetAttribute("fixed", infoObject.TryGetString(attributeName));

                    elements.Add(attrElement);
                }

                documentation.Markup = elements.ToArray();

                xSchema.Items.Add(annotation);
            }
        }
예제 #2
0
        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);
        }
예제 #3
0
        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);
        }
예제 #4
0
        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);
        }
예제 #5
0
        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);
        }
예제 #6
0
        private JsonValue ExtractTexts(string propertyName, JsonSchema propertyType)
        {
            JsonObject result = new JsonObject();

            JsonValue otherData = propertyType.OtherData;

            if (otherData == null)
            {
                return(result);
            }

            JsonValue texts = otherData.Object.GetValueOrDefault("texts");

            if (texts == null)
            {
                // For some unknown reason Manatee sometimes needs to explicit use this method to extract other data texts
                TextsKeyword texts2 = propertyType.Get <TextsKeyword>();
                if (texts2 != null)
                {
                    texts = texts2.ToJson(new JsonSerializer());
                }
            }

            if (texts != null)
            {
                foreach (string textType in texts.Object.Keys)
                {
                    JsonValue language = texts.Object.GetValueOrDefault(textType);

                    string textKey = TextTypeFormat(propertyName, textType);

                    result.Add(TextTypeFormat(textType), textKey);

                    Dictionary <string, TextResourceElement> languageWithTextResource = new Dictionary <string, TextResourceElement>();

                    foreach (string lang in language.Object.Keys)
                    {
                        string textMessage = language.Object.TryGetString(lang);
                        languageWithTextResource.Add(LanguageCode(lang), new TextResourceElement {
                            Id = textKey, Value = textMessage
                        });
                    }

                    if (!modelTexts.ContainsKey(textKey))
                    {
                        modelTexts.Add(textKey, languageWithTextResource);
                    }
                }
            }

            return(result);
        }
예제 #7
0
        private void AddAnnotations(XmlSchemaAnnotated element, JsonSchema jSchema)
        {
            XmlSchemaAnnotation annotation = new XmlSchemaAnnotation();

            TextsKeyword text = jSchema.Get <TextsKeyword>();

            if (text != null)
            {
                JsonValue textObject = text.ToJson(new JsonSerializer());

                foreach (string textType in textObject.Object.Keys)
                {
                    JsonValue language = textObject.Object.GetValueOrDefault(textType);

                    foreach (string lang in language.Object.Keys)
                    {
                        string textMessage = language.Object.TryGetString(lang);

                        XmlElement p = xmlDocument.CreateElement("p");
                        p.AppendChild(xmlDocument.CreateTextNode(textMessage));

                        XmlElement brregTekst = xmlDocument.CreateElement("brreg", "tekst", BRREG_NS);
                        brregTekst.SetAttribute("lang", BRREG_NS, lang);
                        brregTekst.SetAttribute("teksttype", BRREG_NS, textType);
                        brregTekst.AppendChild(p);

                        XmlNode[] nodes = { brregTekst };
                        XmlSchemaDocumentation documentation = new XmlSchemaDocumentation
                        {
                            Markup = nodes,
                        };

                        annotation.Items.Add(documentation);
                    }
                }
            }

            AddTitleAndDescriptionAnnotations(jSchema, annotation);

            if (annotation.Items.Count > 0)
            {
                element.Annotation = annotation;
            }
        }
예제 #8
0
        private XmlSchemaSimpleTypeRestriction ExtractStringFacets(JsonSchema jSchema)
        {
            XmlSchemaSimpleTypeRestriction content = new XmlSchemaSimpleTypeRestriction
            {
                BaseTypeName = new XmlQualifiedName("string", XML_SCHEMA_NS),
            };

            EnumKeyword enumKeyword = jSchema.Get<EnumKeyword>();
            if (enumKeyword != null)
            {
                foreach (JsonValue enumValue in GetterExtensions.Enum(jSchema))
                {
                    XmlSchemaEnumerationFacet enumFacet = new XmlSchemaEnumerationFacet
                    {
                        Value = enumValue.String,
                    };
                    content.Facets.Add(enumFacet);
                }
            }

            MinLengthKeyword minLength = jSchema.Get<MinLengthKeyword>();
            MaxLengthKeyword maxLength = jSchema.Get<MaxLengthKeyword>();

            if (minLength != null && maxLength != null && minLength.Value == maxLength.Value)
            {
                // special rule that maps equal min and max lengths to xsd length facet
                XmlSchemaLengthFacet lengthFacet = new XmlSchemaLengthFacet
                {
                    Value = minLength.Value.ToString(),
                };
                content.Facets.Add(lengthFacet);
            }
            else
            {
                if (minLength != null)
                {
                    XmlSchemaMinLengthFacet minLengthFacet = new XmlSchemaMinLengthFacet
                    {
                        Value = minLength.Value.ToString(),
                    };
                    content.Facets.Add(minLengthFacet);
                }

                if (maxLength != null)
                {
                    XmlSchemaMaxLengthFacet maxLengthFacet = new XmlSchemaMaxLengthFacet
                    {
                        Value = maxLength.Value.ToString(),
                    };
                    content.Facets.Add(maxLengthFacet);
                }
            }

            PatternKeyword pattern = jSchema.Get<PatternKeyword>();
            if (pattern != null)
            {
                XmlSchemaPatternFacet patternFacet = new XmlSchemaPatternFacet
                {
                    Value = pattern.Value.ToString(),
                };

                content.Facets.Add(patternFacet);
            }

            FormatKeyword format = jSchema.Get<FormatKeyword>();
            if (format != null && format.Value != null && !string.IsNullOrEmpty(format.Value.Key))
            {
                content.BaseTypeName = ExtractBaseTypeNameFromFormat(format.Value.Key);
            }

            return content;
        }
예제 #9
0
        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;
        }
예제 #10
0
        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);
        }