Exemple #1
0
        private string GenerateFactoryMethod(TypeReference tr, string ownerNs)
        {
            StringWriter sw = new StringWriter();

            Dictionary <string, string[]> cfm = ClassRenderer.CreateFactoryMethod(tr, "retVal", true);


            if (tr.Class != null && tr.Class.IsAbstract)
            {
                foreach (var chld in tr.Class.SpecializedBy)
                {
                    sw.WriteLine(GenerateFactoryMethod(chld, ownerNs));
                }
            }
            else
            {
                string factoryName = String.Format("Create{0}", tr.Class.Name), tfact = factoryName;
                if (generatedFactoryMethods.Contains(factoryName))
                {
                    factoryName = String.Format("Create{0}", Util.Util.MakeFriendly(tr.Name));
                }
                generatedFactoryMethods.Add(factoryName);

                // Iterate and create shortcuts
                List <String> wroteParms = new List <string>(); // Keep track of the parameters used
                foreach (KeyValuePair <String, String[]> kv in cfm)
                {
                    if (kv.Value[0].Length == 0 || wroteParms.Contains(kv.Value[0]))
                    {
                        continue;
                    }

                    wroteParms.Add(kv.Value[0]);

                    // Correct generic children
                    // TODO: Turn these into regexes
                    //if (tr.Class.TypeParameters != null)
                    //    for (int i = 0; i < tr.Class.TypeParameters.Count; i++)
                    //    {
                    //        List<TypeReference> filler = tr.GenericSupplier == null ? new List<TypeReference>() : tr.GenericSupplier.FindAll(o => (o as TypeParameter).ParameterName == tr.Class.TypeParameters[i].ParameterName);

                    //        for(int f = 0; f< 2; f++)
                    //            if (filler.Count == 1)
                    //                foreach(string replacement in replaceMe)
                    //                    kv.Value[f] = kv.Value[f].Replace(String.Format(replacement, tr.Class.TypeParameters[i].ParameterName), String.Format(replacement, filler[0].Name));
                    //            else
                    //                foreach (string replacement in replaceMe)
                    //                    kv.Value[f] = kv.Value[f].Replace(String.Format(replacement, tr.Class.TypeParameters[i].ParameterName), String.Format(replacement, "System.Object"));
                    //    }

                    // Generate method(s)
                    Class cls = tr.Class;
                    sw.WriteLine("\t\t/// <summary> Create a new instance of {0} to populate {1} </summary>", tr.Name, tr is TypeParameter ? (tr as TypeParameter).ParameterName : "");
                    sw.Write(kv.Value[2]);
                    sw.WriteLine("\t\tpublic static {0} {1} ({2}) {{ \r\n\t\t\t{0} retVal = new {0}();\r\n{3}", ClassRenderer.CreateDatatypeRef(tr, new Property()), factoryName,
                                 kv.Value[0].Substring(0, kv.Value[0].Length - 1), kv.Value[1]);
                    sw.WriteLine("\t\t\treturn retVal;\r\n\t\t}");

                    if (!wroteParms.Contains(""))
                    {
                        wroteParms.Add("");
                        sw.WriteLine("\t\t/// <summary> Create a new instance of {0} to populate {1} </summary>", tr.Name, tr.Class.Name);
                        sw.WriteLine("\t\tpublic static {0} {1} () {{ \r\n\t\t\treturn new {0}();\r\n\t\t}}", ClassRenderer.CreateDatatypeRef(tr, new Property()), factoryName);
                    }
                }


                foreach (TypeReference tpr in tr.GenericSupplier ?? new List <TypeReference>())
                {
                    sw.WriteLine(GenerateFactoryMethod(tpr, ownerNs));
                }
            }

            return(sw.ToString());
        }
Exemple #2
0
        /// <summary>
        /// Get overridden setters
        /// </summary>
        public static SetterOverrideData[] GetOverrideSetters(TypeReference tr, Property p, string ownerPackage)
        {
            // Not a datatype so we won't need overrides
            if (tr.Class != null)
            {
                return(new SetterOverrideData[0]);
            }

            // Get all the setter overrides
            var dataType = s_heuristicData.Datatypes.Find(o => o.MifDatatype == tr.Name);

            // Sanity check
            if (dataType == null || dataType.SetterOverride.Count == 0)
            {
                return(new SetterOverrideData[0]);
            }

            // Set generic paramaters
            Dictionary <String, String> bind = new Dictionary <string, string>();
            string fillParameter             = dataType.TemplateParameter;

            if (!String.IsNullOrEmpty(dataType.TemplateParameter))
            {
                int i = 0;
                foreach (string s in dataType.TemplateParameter.Split(','))
                {
                    TypeReference bindTypeRef = null;
                    if (tr.GenericSupplier != null) // Generic Supplier
                    {
                        bindTypeRef = tr.GenericSupplier.Find(o => o is TypeParameter && (o as TypeParameter).ParameterName == s);
                        if (bindTypeRef == null)
                        {
                            bindTypeRef = tr.GenericSupplier[i++];
                        }
                    }
                    else if (p != null && p.SupplierDomain != null && !String.IsNullOrEmpty(EnumerationRenderer.WillRender(p.SupplierDomain)))
                    {
                        string vocabName     = Util.Util.MakeFriendly(EnumerationRenderer.WillRender(p.SupplierDomain));
                        string containerName = p.Container is Choice ? (p.Container as Choice).Container.Name : p.Container.Name;
                        if (vocabName.Equals(containerName))
                        {
                            vocabName = String.Format("{0}.vocabulary.{1}", ownerPackage, vocabName);
                        }
                        bindTypeRef = new TypeReference()
                        {
                            Name = String.Format("{0}", vocabName)
                        };
                    }
                    else
                    {
                        bindTypeRef = new TypeReference()
                        {
                            Name = dataType.DefaultBind
                        }
                    };

                    // Add Binding
                    bind.Add(s, ClassRenderer.CreateDatatypeRef(bindTypeRef ?? new TypeReference()
                    {
                        Name = null
                    }, null, ownerPackage));
                    fillParameter = fillParameter.Replace(s, bind[s]);
                }
            }

            // Create setter override and return
            List <SetterOverrideData> overrides = new List <SetterOverrideData>(dataType.SetterOverride.Count);

            foreach (var sod in dataType.SetterOverride)
            {
                // Correct parameters
                SetterOverrideData templatedSod = new SetterOverrideData();
                templatedSod.Parameters = new List <PropertyInfoData>();
                foreach (var parameterData in sod.Parameters)
                {
                    string dt = String.Empty;
                    if (!bind.TryGetValue(parameterData.DataType, out dt))
                    {
                        dt = parameterData.DataType;
                    }


                    templatedSod.Parameters.Add(new PropertyInfoData()
                    {
                        Name = parameterData.Name, DataType = dt.Replace(string.Format("<{0}>", dataType.TemplateParameter), String.Format("<{0}>", fillParameter))
                    });
                }

                // Correct Body
                templatedSod.Throws        = new List <ThrowsData>(sod.Throws);
                templatedSod.SetterText    = sod.SetterText.Replace(string.Format("<{0}>", dataType.TemplateParameter), String.Format("<{0}>", fillParameter));
                templatedSod.ValueInstance = sod.ValueInstance;
                overrides.Add(templatedSod);
            }

            return(overrides.ToArray());
        }