예제 #1
0
        private void FillFilterContext(
            FilterDescriptor filter,
            TDataItem item,
            ExpressionContext context)
        {
            CompositeFilterDescriptor filterDescriptor1 = filter as CompositeFilterDescriptor;

            if (filterDescriptor1 != null)
            {
                foreach (FilterDescriptor filterDescriptor2 in (Collection <FilterDescriptor>)filterDescriptor1.FilterDescriptors)
                {
                    this.FillFilterContext(filterDescriptor2, item, context);
                }
            }
            else
            {
                object obj = this.GetFieldValue(item, filter.PropertyName);
                if (obj is Enum)
                {
                    obj = (object)Convert.ToInt32(obj);
                }
                if (context.ContainsKey(filter.PropertyName))
                {
                    context[filter.PropertyName] = obj;
                }
                else
                {
                    context.Add(filter.PropertyName, obj);
                }
            }
        }
예제 #2
0
        public object Evaluate(string expression)
        {
            ExpressionNode node = ExpressionParser.Parse(expression, false);

            if (this.Items.Count == 0 || ExpressionNode.GetNodes <AggregateNode>(node).Count == 0)
            {
                return((object)null);
            }
            List <NameNode>  nodes            = ExpressionNode.GetNodes <NameNode>(node);
            StringCollection stringCollection = new StringCollection();

            foreach (NameNode nameNode in nodes)
            {
                if (!stringCollection.Contains(nameNode.Name))
                {
                    stringCollection.Add(nameNode.Name);
                }
            }
            ExpressionContext context = ExpressionContext.GetContext(Thread.CurrentThread.ManagedThreadId);

            context.Clear();
            for (int index = 0; index < stringCollection.Count; ++index)
            {
                if (context.ContainsKey(stringCollection[index]))
                {
                    context[stringCollection[index]] = this.Items[0][stringCollection[index]];
                }
                else
                {
                    context.Add(stringCollection[index], this.Items[0][stringCollection[index]]);
                }
            }
            return(node.Eval((object)new AggregateItems <T>((IEnumerable <T>) this.Items), (object)context));
        }
예제 #3
0
        protected virtual void Filter(FilterDescriptorCollection descriptors)
        {
            FilterDescriptor[] currentDescriptors = descriptors.ToArray();
            ExpressionNode     node = ExpressionParser.Parse(descriptors.Expression, this.masterTemplate.CaseSensitive);

            if (this.CurrentOperation != ItemSourceOperation.None || node == null || currentDescriptors.Length == 0)
            {
                this.view             = this.dataSource;
                this.CurrentOperation = ItemSourceOperation.None;
                return;
            }

            this.CurrentOperation = ItemSourceOperation.Filtering;
            List <object> newView      = new List <object>();
            IList         filteredView = this.dataSource;

            for (int i = 0; i < filteredView.Count; i++)
            {
                if (!this.perform)
                {
                    this.CurrentOperation = ItemSourceOperation.None;
                    return;
                }

                object            entry   = filteredView[i];
                ExpressionContext context = new ExpressionContext();

                for (int j = 0; j < currentDescriptors.Length; j++)
                {
                    string member = currentDescriptors[j].PropertyName;
                    if (!context.ContainsKey(member))
                    {
                        context.Add(member, this.GetValue(entry, member));
                    }
                    else
                    {
                        context[member] = this.GetValue(entry, member);
                    }
                }

                object evalResult = node.Eval(null, context);

                if (evalResult is bool && (bool)evalResult)
                {
                    newView.Add(entry);
                }
            }

            this.view             = newView;
            this.CurrentOperation = ItemSourceOperation.None;
        }
예제 #4
0
 private bool PerformExpressionFilter(TDataItem item)
 {
     if (this.filterContext.Count == 0)
     {
         return(true);
     }
     try
     {
         lock (this.syncobjs)
         {
             int managedThreadId = Thread.CurrentThread.ManagedThreadId;
             ExpressionContext expressionContext = managedThreadId != this.mainThreadId ? ExpressionContext.GetContext(managedThreadId) : ExpressionContext.Context;
             expressionContext.Clear();
             expressionContext.CaseSensitive = this.CaseSensitive;
             for (int index1 = 0; index1 < this.filterContext.Count; ++index1)
             {
                 string index2 = this.filterContext[index1];
                 object obj    = this.GetFieldValue(item, index2);
                 if (obj is Enum)
                 {
                     obj = (object)Convert.ToInt32(obj);
                 }
                 if (expressionContext.ContainsKey(index2))
                 {
                     expressionContext[index2] = obj;
                 }
                 else
                 {
                     expressionContext.Add(index2, obj);
                 }
             }
             object obj1 = (managedThreadId != this.mainThreadId ? this.GetFilterNode(managedThreadId) : this.filterNode).Eval((object)null, (object)expressionContext);
             if (obj1 is bool)
             {
                 return((bool)obj1);
             }
         }
     }
     catch (Exception ex)
     {
         throw new FilterExpressionException("Invalid filter expression.", ex);
     }
     return(false);
 }
예제 #5
0
        /// <summary>
        /// Evaluates the specified expression.
        /// </summary>
        /// <param name="expression">The expression.</param>
        /// <param name="items">The items.</param>
        /// <returns></returns>
        public object Evaluate(string expression, IEnumerable <TDataItem> items)
        {
            ExpressionNode  node      = ExpressionParser.Parse(expression, this.CaseSensitive);
            List <NameNode> nameNodes = ExpressionNode.GetNodes <NameNode>(node);

            StringCollection contextProperties = new StringCollection();

            foreach (NameNode nameNode in nameNodes)
            {
                if (!contextProperties.Contains(nameNode.Name))
                {
                    contextProperties.Add(nameNode.Name);
                }
            }

            IEnumerator <TDataItem> e = items.GetEnumerator();

            e.MoveNext();

            ExpressionContext context = ExpressionContext.Context;

            context.Clear();
            for (int i = 0; i < contextProperties.Count; i++)
            {
                if (context.ContainsKey(contextProperties[i]))
                {
                    context[contextProperties[i]] = e.Current[contextProperties[i]];
                }
                else
                {
                    context.Add(contextProperties[i], e.Current[contextProperties[i]]);
                }
            }

            if (ExpressionNode.GetNodes <AggregateNode>(node).Count > 0)
            {
                return(node.Eval(new AggregateItems <TDataItem>(items), context));
            }

            return(node.Eval(null, context));
        }
예제 #6
0
        private bool PerformExpressionFilter(TDataItem item)
        {
            if (this.filterContext.Count == 0)
            {
                return(false);
            }

            try
            {
                ExpressionContext context = ExpressionContext.Context;
                context.Clear();
                for (int i = 0; i < this.filterContext.Count; i++)
                {
                    string fieldName = this.filterContext[i];
                    if (context.ContainsKey(fieldName))
                    {
                        context[fieldName] = item[fieldName];
                    }
                    else
                    {
                        context.Add(fieldName, item[fieldName]);
                    }
                }

                object result = this.filterNode.Eval(null, context);
                if (result is bool)
                {
                    return((bool)result);
                }
            }
            catch (Exception ex)
            {
                throw new FilterExpressionException("Invalid filter expression.", ex);
            }

            return(false);
        }
예제 #7
0
        /// <summary>
        /// Evaluates the specified expression.
        /// </summary>
        /// <param name="expression">The expression.</param>
        /// <returns></returns>
        public object Evaluate(string expression)
        {
            ExpressionNode node = ExpressionParser.Parse(expression, false);

            if (this.Items.Count == 0 || ExpressionNode.GetNodes <AggregateNode>(node).Count == 0)
            {
                return(null);
            }

            List <NameNode>  nameNodes         = ExpressionNode.GetNodes <NameNode>(node);
            StringCollection contextProperties = new StringCollection();

            foreach (NameNode nameNode in nameNodes)
            {
                if (!contextProperties.Contains(nameNode.Name))
                {
                    contextProperties.Add(nameNode.Name);
                }
            }

            ExpressionContext context = ExpressionContext.Context;

            context.Clear();
            for (int i = 0; i < contextProperties.Count; i++)
            {
                if (context.ContainsKey(contextProperties[i]))
                {
                    context[contextProperties[i]] = this.Items[0][contextProperties[i]];
                }
                else
                {
                    context.Add(contextProperties[i], this.Items[0][contextProperties[i]]);
                }
            }

            return(node.Eval(new AggregateItems <T>(this.Items), context));
        }
예제 #8
0
        /// <summary>
        /// Try to evaluate the specified expression.
        /// </summary>
        /// <param name="expression">The expression.</param>
        /// <param name="items">The items.</param>
        /// <param name="index">Index of item, which the result will be calculated for</param>
        /// <param name="result">Expression result</param>
        /// <returns></returns>
        internal bool TryEvaluate(string expression, IEnumerable <TDataItem> items, int index, out object result)
        {
            result = null;

            ExpressionNode node = null;

            if (!ExpressionParser.TryParse(expression, this.CaseSensitive, out node))
            {
                return(false);
            }

            List <NameNode> nameNodes = ExpressionNode.GetNodes <NameNode>(node);

            StringCollection contextProperties = new StringCollection();

            foreach (NameNode nameNode in nameNodes)
            {
                if (!contextProperties.Contains(nameNode.Name))
                {
                    contextProperties.Add(nameNode.Name);
                }
            }

            IEnumerator <TDataItem> e = items.GetEnumerator();

            while (index >= 0)
            {
                if (!e.MoveNext())
                {
                    throw new IndexOutOfRangeException();
                }
                index--;
            }

            if (e.Current == null)
            {
                return(false);
            }

            ExpressionContext context = ExpressionContext.Context;

            context.Clear();

            for (int i = 0; i < contextProperties.Count; i++)
            {
                if (e.Current.IndexOf(contextProperties[i]) < 0)
                {
                    return(false);
                }
                if (context.ContainsKey(contextProperties[i]))
                {
                    context[contextProperties[i]] = e.Current[contextProperties[i]];
                }
                else
                {
                    context.Add(contextProperties[i], e.Current[contextProperties[i]]);
                }
            }

            try
            {
                if (ExpressionNode.GetNodes <AggregateNode>(node).Count > 0)
                {
                    result = node.Eval(new AggregateItems <TDataItem>(items), context);
                }
                else
                {
                    result = node.Eval(null, context);
                }
            }
            catch
            {
                return(false);
            }

            return(true);
        }
예제 #9
0
        private object EvaluateExpression(GridViewRowInfo row, GridViewColumn column)
        {
            if (row.ViewTemplate.MasterTemplate.Owner.InvokeRequired)
            {
                object result = (object)null;
                row.ViewTemplate.MasterTemplate.Owner.Invoke((Delegate)(() => result = this.EvaluateExpression(row, column)));
                return(result);
            }
            ExpressionNode expressionNode;

            if (DataUtils.TryParse(column.Expression, row.ViewTemplate.CaseSensitive, out expressionNode))
            {
                ExpressionContext context = ExpressionContext.Context;
                bool containsAggregate    = false;
                List <GridViewColumn> columnsForExpression = this.GetColumnsForExpression(row.ViewTemplate, expressionNode, out containsAggregate);
                for (int index1 = 0; index1 < columnsForExpression.Count; ++index1)
                {
                    if (columnsForExpression[index1] != column)
                    {
                        GridViewColumn index2   = columnsForExpression[index1];
                        Accessor       accessor = index2.Accessor;
                        object         obj      = !(accessor is ExpressionAccessor) || !row.Cache.ContainsKey((object)index2) ? accessor[row] : row.Cache[index2];
                        if (context.ContainsKey(index2.Name))
                        {
                            context[index2.Name] = obj;
                        }
                        else
                        {
                            context.Add(index2.Name, obj);
                        }
                    }
                }
                AggregateData aggregateData = (AggregateData)null;
                if (containsAggregate)
                {
                    GridViewGroupRowInfo parent = row.Parent as GridViewGroupRowInfo;
                    aggregateData = parent == null ? new AggregateData((IEnumerable <GridViewRowInfo>)row.ViewTemplate.ChildRows) : new AggregateData((IEnumerable <GridViewRowInfo>)parent.ChildRows);
                }
                try
                {
                    object obj1 = expressionNode.Eval((object)aggregateData, (object)context);
                    object obj2 = RadDataConverter.Instance.Parse(column as IDataConversionInfoProvider, obj1);
                    row.Cache[column] = obj2;
                }
                catch (InvalidExpressionException ex)
                {
                    if (!ExpressionAccessor.ExpressionErrorRaised)
                    {
                        string message = string.Format("Invalid Expression: \n\"{0}\"", (object)ex.Message);
                        row.ViewTemplate.SetError(new GridViewCellCancelEventArgs(row, column, (IInputEditor)null), DataUtils.CreateInvalidExpressionException(message));
                        ExpressionAccessor.ExpressionErrorRaised = true;
                    }
                }
            }
            else if (!ExpressionAccessor.ExpressionErrorRaised)
            {
                if (!string.IsNullOrEmpty(column.HeaderText))
                {
                    string headerText = column.HeaderText;
                }
                else if (!string.IsNullOrEmpty(column.FieldName))
                {
                    string fieldName = column.FieldName;
                }
                else if (!string.IsNullOrEmpty(column.Name))
                {
                    string name = column.Name;
                }
                else
                {
                    string empty = string.Empty;
                }
                string message = string.Format("Invalid expression value for [{0}] column!", (object)column.Name);
                row.ViewTemplate.SetError(new GridViewCellCancelEventArgs(row, column, (IInputEditor)null), DataUtils.CreateInvalidExpressionException(message));
                ExpressionAccessor.ExpressionErrorRaised = true;
            }
            return(row.Cache[column]);
        }
예제 #10
0
        public bool TryEvaluate(
            string expression,
            IEnumerable <TDataItem> items,
            int index,
            out object result)
        {
            result = (object)null;
            ExpressionNode expressionNode = (ExpressionNode)null;

            if (!ExpressionParser.TryParse(expression, this.CaseSensitive, out expressionNode))
            {
                return(false);
            }
            List <NameNode>  nodes            = ExpressionNode.GetNodes <NameNode>(expressionNode);
            StringCollection stringCollection = new StringCollection();

            foreach (NameNode nameNode in nodes)
            {
                if (!stringCollection.Contains(nameNode.Name))
                {
                    stringCollection.Add(nameNode.Name);
                }
            }
            IEnumerator <TDataItem> enumerator = items.GetEnumerator();

            for (; index >= 0; --index)
            {
                if (!enumerator.MoveNext())
                {
                    throw new IndexOutOfRangeException();
                }
            }
            if ((object)enumerator.Current == null)
            {
                return(false);
            }
            ExpressionContext context = ExpressionContext.Context;

            context.Clear();
            for (int index1 = 0; index1 < stringCollection.Count; ++index1)
            {
                if (enumerator.Current.IndexOf(stringCollection[index1]) < 0)
                {
                    return(false);
                }
                if (context.ContainsKey(stringCollection[index1]))
                {
                    context[stringCollection[index1]] = enumerator.Current[stringCollection[index1]];
                }
                else
                {
                    context.Add(stringCollection[index1], enumerator.Current[stringCollection[index1]]);
                }
            }
            try
            {
                result = ExpressionNode.GetNodes <AggregateNode>(expressionNode).Count <= 0 ? expressionNode.Eval((object)null, (object)context) : expressionNode.Eval((object)new AggregateItems <TDataItem>(items), (object)context);
            }
            catch
            {
                return(false);
            }
            return(true);
        }
예제 #11
0
        public object Evaluate(string expression, IEnumerable <TDataItem> items)
        {
            ExpressionNode   node             = ExpressionParser.Parse(expression, this.CaseSensitive);
            List <NameNode>  nodes            = ExpressionNode.GetNodes <NameNode>(node);
            StringCollection stringCollection = new StringCollection();

            foreach (NameNode nameNode in nodes)
            {
                if (!stringCollection.Contains(nameNode.Name))
                {
                    stringCollection.Add(nameNode.Name);
                }
            }
            IEnumerator <TDataItem> enumerator = items.GetEnumerator();

            enumerator.MoveNext();
            ExpressionContext context = ExpressionContext.Context;

            context.Clear();
            for (int index = 0; index < stringCollection.Count; ++index)
            {
                if (!RadCollectionView <TDataItem> .isDesignMode.HasValue)
                {
                    Process currentProcess = Process.GetCurrentProcess();
                    RadCollectionView <TDataItem> .isDesignMode = new bool?(currentProcess.ProcessName == "devenv");
                    currentProcess.Dispose();
                }
                if (RadCollectionView <TDataItem> .isDesignMode.HasValue)
                {
                    if (RadCollectionView <TDataItem> .isDesignMode.Value)
                    {
                        try
                        {
                            if (context.ContainsKey(stringCollection[index]))
                            {
                                context[stringCollection[index]] = enumerator.Current[stringCollection[index]];
                                continue;
                            }
                            context.Add(stringCollection[index], enumerator.Current[stringCollection[index]]);
                            continue;
                        }
                        catch (Exception ex)
                        {
                            int num = (int)MessageBox.Show(string.Format("Error evaluating expression: {0}", (object)ex.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                            return((object)null);
                        }
                    }
                }
                if (context.ContainsKey(stringCollection[index]))
                {
                    context[stringCollection[index]] = enumerator.Current[stringCollection[index]];
                }
                else
                {
                    context.Add(stringCollection[index], enumerator.Current[stringCollection[index]]);
                }
            }
            if (ExpressionNode.GetNodes <AggregateNode>(node).Count > 0)
            {
                return(node.Eval((object)new AggregateItems <TDataItem>(items), (object)context));
            }
            return(node.Eval((object)null, (object)context));
        }