private void CompilePlaceHolder(string[] templates, string template)
        {
            // First element in placeholder has two tastes name or index.
            string[] fields      = Utils.TrimByLength(template, 1).Split(new char[] { ':' });
            string   inlinecalls = string.Empty;
            int      pointer     = -1;

            // {0}
            // {0:Guid}
            // {Guid}
            if (fields.Length == 1)
            {
                if (Utils.IsNumeric(fields[0]) || fields[0] == "X" || fields[0] == "Y")
                {
                    // index based
                    pointer = ConvertUtility.ToInt32(fields[0], 0);

                    if (fields[0] == "X")
                    {
                        pointer = -2;
                    }

                    if (fields[0] == "Y")
                    {
                        pointer = -3;
                    }

                    PlaceHolderMethods phmIndexOnly = new PlaceHolderMethods();
                    phmIndexOnly.Methods     = new List <MethodBase>();
                    phmIndexOnly.Templates   = templates;
                    phmIndexOnly.Text        = template;
                    phmIndexOnly.ValuesIndex = pointer;
                    this.PlaceHolders.Add(phmIndexOnly);
                    return;
                }
                else
                {
                    // Method based
                    inlinecalls = fields[0];
                }
            }
            if (fields.Length == 2)
            {
                // index based
                pointer = ConvertUtility.ToInt32(fields[0], 0);
                if (fields[0] == "X")
                {
                    pointer = -2;
                }

                if (fields[0] == "Y")
                {
                    pointer = -3;
                }

                inlinecalls = fields[1];
            }

            // find out which methods to use.
            string[] methodcalls = Utils.SplitEscaped(inlinecalls, ",", "(", ")");

            PlaceHolderMethods phm = new PlaceHolderMethods();

            phm.Text        = template;
            phm.Templates   = templates;
            phm.ValuesIndex = pointer;

            foreach (string call in methodcalls)
            {
                MethodInstance clone = MethodInstance.GetMethodInstance(getCallName(call));
                if (clone != null)
                {
                    // can be Guid, Guid() PadLeft(3,0) etc
                    // parameters
                    string   parametersString = getCallParameters(call).Trim(new char[] { '(', ')' });
                    string[] parameters       = Utils.SplitEscaped(parametersString, ',', '"');

                    for (int ii = 0; ii < parameters.Length; ii++)
                    {
                        parameters[ii] = parameters[ii].Trim();
                        if (parameters[ii].Length > 1)
                        {
                            parameters[ii] = parameters[ii].Trim(new char[] { '"' });
                        }

                        Type t = typeof(object);
                        if (clone.Properties.Count > 0)
                        {
                            t = clone.Properties[Utils.Bound(ii, 0, clone.Properties.Count)].PropertyReference.PropertyType;
                        }
                        if (ii < clone.Properties.Count)
                        {
                            if (StringTemplateItem.PropertyTypeSupported(t))
                            {
                                StringTemplateItem.SetPropertyValue(clone.Properties[ii].PropertyReference, clone.Formatter, parameters[ii]);
                            }

                            if (StringTemplateItem.PropertyArrayTypeSupported(t))
                            {
                                // if next property to be set is an array, all remaining parameters will be asigned to this property.
                                StringTemplateItem.SetPropertyValueArray(clone.Properties[ii].PropertyReference, clone.Formatter, parameters, ii);
                                break;
                            }
                        }
                    }
                    phm.Methods.Add(clone.Formatter);
                }
            }
            this.PlaceHolders.Add(phm);
        }
Esempio n. 2
0
        /// <summary>
        /// Data consists of lines based on CSV rules (http://en.wikipedia.org/wiki/Comma-separated_values</para>)
        /// <para>Separator will be auto detected.</para>
        /// </summary>
        /// <param name="data"></param>
        private void initCommands(string data, bool handleDataAsOne)
        {
            if (data != prev_data || (data == string.Empty && prev_data == string.Empty))
            {
                datacommands.Clear();
                string   callname   = string.Empty;
                string   callparams = string.Empty;
                string[] dataLines  = new string[1] {
                    string.Empty
                };
                if (!string.IsNullOrEmpty(data))
                {
                    dataLines = Utils.SplitCsvLines(data, separator);
                }
                if (handleDataAsOne)
                {
                    dataLines    = new string[1];
                    dataLines[0] = data;
                }

                int             templateNumber = 0;
                DataCommandBase dc             = new DataCommandBase();
                string          line           = string.Empty;
                for (int xx = 0; xx < dataLines.Length; xx++)
                {
                    line           = dataLines[xx];
                    templateNumber = templateNumberFromLine(line);
                    dc             = new DataCommandBase();

                    string[] lineValues = Utils.SplitEscaped(lineFromTemplateLine(line), separator, '"');

                    if (lineValues.Length == 1)
                    {
                        callname = getCallName(line);
                        if (availableCommandNames.Contains(callname))
                        {
                            dc = GetCommandInstance(callname, commandTypes);
                            if (dc.GetType() != typeof(DataCommandBase))
                            {
                                dc.StringTemplate = this;
                                // set dc parameters
                                callparams = getCallParameters(line);
                                List <DataCommandParameterAttribute> attribs = DataCommandParameterAttribute.GetAttributes(dc.GetType());

                                string   parametersString = callparams.Trim(new char[] { '(', ')' });
                                string[] parameters       = Utils.SplitEscaped(parametersString, ',', '"');
                                for (int ii = 0; ii < parameters.Length; ii++)
                                {
                                    Type t = typeof(object);
                                    if (attribs.Count > 0)
                                    {
                                        t = attribs[Utils.Bound(ii, 0, attribs.Count)].PropertyReference.PropertyType;
                                    }
                                    if (ii < attribs.Count)
                                    {
                                        if (StringTemplateItem.PropertyTypeSupported(t))
                                        {
                                            StringTemplateItem.SetPropertyValue(attribs[ii].PropertyReference, dc, parameters[ii]);
                                        }
                                        if (StringTemplateItem.PropertyArrayTypeSupported(t))
                                        {
                                            // if next property to be set is an array, all remaining parameters will be asigned to this property.
                                            StringTemplateItem.SetPropertyValueArray(attribs[ii].PropertyReference, dc, parameters, ii);
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    dc.IsLastCommand  = (xx == dataLines.Length - 1);
                    dc.LineValues     = lineValues;
                    dc.Line           = line;
                    dc.Data           = data;
                    dc.TemplateNumber = templateNumber;
                    datacommands.Add(dc);
                }
                dc.IsLastCommand = true;
            }
        }