Example #1
0
        private static Condition BuildConditionInternal(string s)
        {
            s = s.Trim();
            Stack <Condition> condStack = new Stack <Condition>();
            Stack <int>       opStack   = new Stack <int>();

            for (int i = 0; i < s.Length;)
            {
                if (s[i] == '(')
                {
                    string p = BraceHelper.SelectBracesContent(s, ref i, '(', ')');
                    condStack.Push(BuildConditionInternal(p));
                    continue;
                }
                if (char.IsLetterOrDigit(s[i]) || s[i] == '^' || s[i] == '$')
                {
                    string p = "";
                    while (i < s.Length && !"<>=!&|".Contains(s[i]))
                    {
                        p += s[i];
                        i++;
                    }
                    condStack.Push(new FakeCondition(p.Trim()));
                    continue;
                }
                string ops = "";
                while (i < s.Length && "<>=!&|".Contains(s[i]))
                {
                    ops += s[i];
                    i++;
                }
                if (ops.Length > 0)
                {
                    int op = GetOperationPriority(ops);
                    while (opStack.Count > 0 && opStack.Peek() > op)
                    {
                        condStack.Push(MakeCondition(condStack, opStack));
                    }
                    opStack.Push(op);
                    continue;
                }
                i++;
            }
            while (opStack.Count > 0)
            {
                condStack.Push(MakeCondition(condStack, opStack));
            }
            if (condStack.Count != 1)
            {
                throw new Exception("Invalid condition statement: [" + s + "]");
            }
            return(condStack.Pop());
        }
Example #2
0
 private string SelectObjectConditions(string template, ref int i)
 {
     return(BraceHelper.SelectBracesContent(template, ref i, '[', ']'));
 }
Example #3
0
        private string SelectObjectBody(string template, ref int i)
        {
            string result = BraceHelper.SelectBracesContent(template, ref i, '{', '}');

            return(result);
        }
Example #4
0
        public static object ReadObject(string template, ref int i, object model)
        {
            if (i >= template.Length)
            {
                return(null);
            }
            string name = "";
            object current;

            if (template[i] == '^')
            {
                current = model;
                i++;
            }
            else
            {
                while (i < template.Length && char.IsLetterOrDigit(template[i]))
                {
                    name += template[i];
                    i++;
                }
                if (name.Length == 0)
                {
                    throw new Exception("Incorect syntax in [" + template + "] at position " + i);
                }

                Type t = model.GetType();


                bool hasParams = i < template.Length && template[i] == '(';
                if (hasParams)
                {
                    string        parameters = BraceHelper.SelectBracesContent(template, ref i, '(', ')');
                    List <string> list       = ParametersHelper.GetParametersList(parameters);
                    object[]      ps         = new object[list.Count];
                    int           k          = 0;
                    foreach (string l in list)
                    {
                        if (l == "^")
                        {
                            ps[k] = model;
                        }
                        else if (l.StartsWith("^"))
                        {
                            int index = 0;
                            ps[k] = ReadObject(l, ref index, model);
                        }
                        else
                        {
                            ps[k] = l;
                        }
                        k++;
                    }
                    Type[] types = new Type[ps.Length];
                    for (int j = 0; j < ps.Length; j++)
                    {
                        types[j] = ps[j].GetType();
                    }
                    MethodInfo mi = t.GetMethod(name, types);
                    if (mi != null)
                    {
                        current = mi.Invoke(model, ps);
                    }
                    else
                    {
                        throw new Exception("Can not find method:[" + name + "] in object type [" + t + "]");
                    }
                }
                else
                {
                    FieldInfo fi = t.GetField(name);
                    if (fi != null)
                    {
                        current = fi.GetValue(model);
                    }
                    else
                    {
                        PropertyInfo pi = t.GetProperty(name,
                                                        BindingFlags.Instance | BindingFlags.Public |
                                                        BindingFlags.GetProperty);
                        if (pi != null)
                        {
                            current = pi.GetValue(model, null);
                        }
                        else
                        {
                            throw new Exception("Can not find property or field:[" + name + "] in object type [" + t + "]");
                        }
                    }
                }
            }
            if (i >= template.Length || template[i] != '.')
            {
                return(current);
            }
            i++;
            return(ReadObject(template, ref i, current));
        }