private void AnalyseChildNodes(SyntaxNode node, ObjectModel parent)
        {
            foreach (SyntaxNode child in node.ChildNodes())
            {
                if (child.Kind() == SyntaxKind.ClassDeclaration)
                {
                    ObjectModel subParent = new ObjectModel();
                    foreach (SyntaxToken item in child.ChildTokens())
                    {
                        if (item.Kind() == SyntaxKind.IdentifierToken)
                        {
                            subParent.TokenType  = SyntaxKind.ClassDeclaration;
                            subParent.SyntaxName = item.Value.ToString();
                            break;
                        }
                    }

                    if (child.ChildNodes().Count() > 0)
                    {
                        AnalyseChildNodes(child, subParent);
                    }

                    parent.Children.Add(subParent);
                }
                else if (child.Kind() == SyntaxKind.PropertyDeclaration)
                {
                    ObjectModel property = new ObjectModel();
                    foreach (SyntaxToken item in child.ChildTokens())
                    {
                        if (item.Kind() == SyntaxKind.IdentifierToken)
                        {
                            property.TokenType  = SyntaxKind.PropertyDeclaration;
                            property.SyntaxName = item.Value.ToString();
                            break;
                        }
                    }

                    foreach (SyntaxNode item in child.ChildNodes())
                    {
                        if (item.Kind() == SyntaxKind.PredefinedType || // int, bool, string
                            item.Kind() == SyntaxKind.NullableType || // Nullable kinds
                            item.Kind() == SyntaxKind.IdentifierName)    // Classes, Guid, Boolean, String
                        {
                            property.PropertyType = item.ToString();
                            property.NodeType     = item.Kind();
                            break;
                        }

                        if (item.Kind() == SyntaxKind.GenericName || // Lists, Dictionaries
                            item.Kind() == SyntaxKind.ArrayType)    // Arrays
                        {
                            // get the generic type inside the array, or list
                            foreach (var genericItem in item.ChildNodes())
                            {
                                if (genericItem.Kind() == SyntaxKind.PredefinedType)
                                {
                                    property.GenericType = genericItem.ToString();
                                    break;
                                }
                            }
                            property.PropertyType = item.ToString();
                            property.NodeType     = item.Kind();
                            break;
                        }
                    }
                    parent.Children.Add(property);
                }
            }
        }
Esempio n. 2
0
        private void WritePropertyMember(ObjectModel propertyModel, List <string> accessorList, ObjectModel propertyClassObject = null)
        {
            if (propertyClassObject != null && propertyClassObject.TokenType == SyntaxKind.ClassDeclaration) // Handle class properties
            {
                bool hasChildren = propertyClassObject.Children != null && propertyClassObject.Children.Any();
                BuilderWriteClassMember(propertyModel.SyntaxName, propertyModel.PropertyType, hasChildren, accessorList);
                if (hasChildren)
                {
                    accessorList.Add(propertyModel.SyntaxName);
                    foreach (ObjectModel child in propertyClassObject.Children)
                    {
                        bool checkIfTypeExist = this.objectModels.Exists(p => p.SyntaxName == child.PropertyType);
                        var  propertyClass    = this.objectModels.FirstOrDefault(p => p.SyntaxName == child.PropertyType);
                        WritePropertyMember(child, accessorList, propertyClass);
                    }

                    BuilderWriteChildClassClosing();
                }
            }
            else if (propertyModel.TokenType == SyntaxKind.PropertyDeclaration)
            {
                try
                {
                    switch (propertyModel.NodeType)
                    {
                    case SyntaxKind.ArrayType:     // Handle Arrays
                        BuilderWriteMember(propertyModel.SyntaxName, CSharpTypesMapping.MapArray(propertyModel), accessorList);
                        break;

                    case SyntaxKind.GenericName:     // Handle Lists
                        BuilderWriteMember(propertyModel.SyntaxName, CSharpTypesMapping.MapObject(propertyModel), accessorList);
                        break;

                    case SyntaxKind.PredefinedType:   // Any Non Generic Property : bool
                    case SyntaxKind.NullableType:     // Any Nullabe Type : Datetime?
                        BuilderWriteMember(propertyModel.SyntaxName, CSharpTypesMapping.Map(propertyModel.PropertyType), accessorList);
                        break;

                    case SyntaxKind.IdentifierName: // Classes, Guid, Boolean, String
                    {
                        try                         // Boolean, Guid, String classes
                        {
                            BuilderWriteMember(propertyModel.SyntaxName, CSharpTypesMapping.Map(propertyModel.PropertyType), accessorList);
                        }
                        catch (Exception)         // Custom Classes
                        {
                            bool checkIfTypeExist = this.objectModels.Exists(p => p.SyntaxName == propertyModel.PropertyType);
                            var  propertyClass    = this.objectModels.FirstOrDefault(p => p.SyntaxName == propertyModel.PropertyType);
                            if (checkIfTypeExist)
                            {
                                WritePropertyMember(propertyModel, accessorList, propertyClass);
                            }
                            else
                            {
                                BuilderWriteMember(propertyModel.SyntaxName, CSharpTypesMapping.MapObject(propertyModel), accessorList);
                            }
                        }
                    }
                    break;

                    default:
                        throw new NotImplementedException(string.Format("propertyModel.NodeType {0} not handled in WritePropertyMember, CSharpWriter", propertyModel.NodeType));
                    }
                }
                catch (Exception ex)
                {
                    builder.AppendFormat("// Unknown Property : {0} {1}", propertyModel.SyntaxName, Environment.NewLine);
                }
            }
        }
Esempio n. 3
0
 static internal string MapArray(ObjectModel type)
 {
     return(string.Format("new {0}[]{{{1}}}", type.GenericType, Mappings[type.GenericType]));
 }