예제 #1
0
        private void AddCustomNameSpaces(XmlSchema xsdSchema, JsonSchema jsonSchema)
        {
            Dictionary <string, JsonSchema> definitions = GetterExtensions.Definitions(jsonSchema);

            if (definitions != null)
            {
                foreach (KeyValuePair <string, JsonSchema> def in definitions)
                {
                    JsonSchema jSchema = def.Value;

                    if (jSchema != JsonSchema.Empty && jSchema.Properties() != null)
                    {
                        Dictionary <string, JsonSchema> props = jSchema.Properties();
                        foreach (KeyValuePair <string, JsonSchema> property in props)
                        {
                            JsonSchema schemaProp = property.Value;
                            string     nameProp   = property.Key;

                            if (nameProp.Equals("dataFormatProvider") && schemaProp.Const().String.Equals("SERES"))
                            {
                                xsdSchema.Namespaces.Add("seres", SERES_NS);
                            }
                        }
                    }
                }
            }
        }
예제 #2
0
        private XmlSchemaAttribute ExtractAttribute(string propertyName, JsonSchema propertyType)
        {
            XmlSchemaSimpleType simpleType = ExtractSimpleType("example", propertyType);

            XmlSchemaAttribute attribute = new XmlSchemaAttribute
            {
                Name           = propertyName,
                SchemaTypeName = ((XmlSchemaSimpleTypeRestriction)simpleType.Content).BaseTypeName,
            };

            if (simpleType.Content != null && simpleType.Content is XmlSchemaSimpleTypeRestriction && ((XmlSchemaSimpleTypeRestriction)simpleType.Content).Facets.Count > 0)
            {
                bool hasEnumeration = false;
                XmlSchemaSimpleTypeRestriction simpleTypeRestriction = (XmlSchemaSimpleTypeRestriction)simpleType.Content;
                foreach (XmlSchemaObject facet in simpleTypeRestriction.Facets)
                {
                    if (facet is XmlSchemaEnumerationFacet)
                    {
                        hasEnumeration = true;
                        break;
                    }
                }

                if (hasEnumeration)
                {
                    // add anonymous type
                    simpleType.Name          = null;
                    attribute.SchemaType     = simpleType;
                    attribute.SchemaTypeName = null;
                }
            }

            AddAnnotations(attribute, propertyType);

            JsonValue constant = propertyType.Const();

            if (constant != null)
            {
                if (constant.String != null)
                {
                    attribute.FixedValue = constant.String;
                }
            }

            return(attribute);
        }
예제 #3
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);
        }