/// <summary> /// Adds a <see cref="IExpressionHandler" /> to be used when handling expressions. /// </summary> /// <param name="expressionType"> /// The <see cref="ExpressionType" /> handled by the given <see cref="IExpressionHandler" /> /// </param> /// <param name="handler"> /// The <see cref="IExpressionHandler" />. /// </param> /// <param name="isDefaultHandler"> /// A value indicating whether the handler is added internally (true) or externally using /// <see cref="FlowQueryHelper.AddExpressionHandler" /> (false). /// </param> /// <exception cref="ArgumentNullException"> /// <paramref name="handler" /> is null. /// </exception> private static void AddHandler ( ExpressionType expressionType, IExpressionHandler handler, bool isDefaultHandler = true ) { lock (ExpressionHandlerLock) { if (handler == null) { throw new ArgumentNullException("handler"); } Dictionary <ExpressionType, HashSet <IExpressionHandler> > collection = isDefaultHandler ? DefaultExpressionHandlers : CustomExpressionHandlers; HashSet <IExpressionHandler> handlers; bool found = collection.TryGetValue(expressionType, out handlers); if (found) { handlers.Add(handler); } else { collection.Add(expressionType, new HashSet <IExpressionHandler> { handler }); } } }
/// <summary> /// Adds a <see cref="IMethodCallExpressionHandler" /> to be used when handling method calls. /// </summary> /// <param name="expressionType"> /// The <see cref="ExpressionType" /> handled by the given <see cref="IExpressionHandler" /> /// </param> /// <param name="handler"> /// The <see cref="IExpressionHandler" />. /// </param> /// <exception cref="ArgumentNullException"> /// <paramref name="handler" /> is null. /// </exception> public static void AddExpressionHandler ( ExpressionType expressionType, IExpressionHandler handler ) { AddHandler(expressionType, handler, false); }
/// <summary> /// Adds a <see cref="IExpressionHandler" /> to be used when handling method call expressions. /// </summary> /// <param name="handler"> /// The <see cref="IExpressionHandler" />. /// </param> /// <param name="isDefaultHandler"> /// A value indicating whether the handler is added internally (true) or externally using /// <see cref="FlowQueryHelper.AddExpressionHandler" /> (false). /// </param> /// <exception cref="ArgumentNullException"> /// <paramref name="handler" /> is null. /// </exception> private static void AddCallHandler ( IExpressionHandler handler, bool isDefaultHandler = true ) { AddHandler(ExpressionType.Call, handler, isDefaultHandler); }
/// <summary> /// Evaluates an expression and applies the results to an existing object /// </summary> /// <param name="Expression">the expression to evaluate</param> /// <param name="existingObject">the object to apply to</param> /// <returns>the evaluated object</returns> public object Evaluate(Expression expression, object existingObject) { Expression oldExpr = Current; _currentExpression = expression; IExpressionHandler handler = Settings.ExpressionHandlers.GetHandler(expression); object result = handler.Evaluate(expression, existingObject, this); _currentExpression = oldExpr ?? _currentExpression; return(result); }
/// <summary> /// Sets the current model. /// </summary> /// <param name="model"></param> public void SetModel(Model.Model model) { this.model = model; ExpressionHandler = new ExpressionHandler(model); }
public override IResult Parse(object source) { if (null == source) { throw new ArgumentNullException("source"); } ContainerWebPart webPart = source as ContainerWebPart; if (null == webPart) { throw new ArgumentException("source is not an ContainerWebPart"); } IConfiguration config = webPart.Configuration; if (null == config) { throw new ArgumentException("missing configuration"); } IValidationManager validationManager = webPart.ValidationManager; if (null == validationManager) { throw new ArgumentException("missing validation manager"); } base.Parse(source); if (config.Sections.ContainsKey("Template")) { IConfigurationSection templateConfig = config.GetConfigurationSectionReference("Template"); webPart.TemplateConfig = templateConfig; } else { webPart.TemplateConfig = config.NewConfigurationSectionInstance("Template"); } if (config.Sections.ContainsKey("Validation")) { foreach (IConfigurationElement element in config.Sections["Validation"].Elements.Values) { IValidationTask task = validationManager.NewValidationTaskInstance(); if (element.Attributes.ContainsKey("target")) { task.Target = element.GetAttributeReference("target").Value; } if (element.Attributes.ContainsKey("member")) { task.Member = element.GetAttributeReference("member").Value.ToString(); } foreach (IConfigurationElement handlerElement in element.Elements.Values) { if (handlerElement.Attributes.ContainsKey("handlerKey")) { string handlerKey = handlerElement.GetAttributeReference("handlerKey").Value.ToString(); IValidationHandler handler = validationManager.Handler(handlerKey); if ("expression" == handlerKey) { IExpressionHandler expressionHandler = handler as IExpressionHandler; IEnumerator <IConfigurationElement> enumerator = handlerElement.Elements.Values.GetEnumerator(); if (enumerator.MoveNext()) { IConfigurationElement expressionElement = enumerator.Current; expressionHandler.Expression = (validationManager.ExpressionsManager.Token(expressionElement.GetAttributeReference("type").Value.ToString()) as IExpression); if (null == expressionHandler.Expression) { throw new InvalidOperationException("Token is not an IExpression"); } expressionHandler.Expression.Make(expressionElement, validationManager.ExpressionsManager); } } task.Handlers.Add(handler); } } validationManager.RegisterTask(task); } } if (config.Sections.ContainsKey("Checks")) { foreach (IConfigurationElement element in config.Sections["Checks"].Elements.Values) { if (element.Attributes.ContainsKey("command")) { string command = element.GetAttributeReference("command").Value.ToString(); webPart.Checks.Add(command, new List <Container.CheckDefinition>()); foreach (IConfigurationElement checkElement in element.Elements.Values) { Container.CheckDefinition check = new Container.CheckDefinition(); if (checkElement.Attributes.ContainsKey("error")) { check.Error = checkElement.GetAttributeReference("error").Value.ToString(); } if (checkElement.Attributes.ContainsKey("milestone")) { check.Milestone = checkElement.GetAttributeReference("milestone").Value.ToString(); } IEnumerator <IConfigurationElement> enumerator = checkElement.Elements.Values.GetEnumerator(); if (enumerator.MoveNext()) { IConfigurationElement expressionElement = enumerator.Current; IExpression expression = webPart.ExpressionsManager.Token(expressionElement.GetAttributeReference("type").Value.ToString()) as IExpression; if (null == expression) { throw new InvalidOperationException("Token is not an IExpression"); } expression.Make(expressionElement, webPart.ExpressionsManager); check.Expression = expression; } webPart.Checks[command].Add(check); } } } } return(null); }
public object Evaluate(Expression Expression, object existingObject) { IExpressionHandler handler = _context.ExpressionHandlers.GetHandler(Expression); return(handler.Evaluate(Expression, existingObject, this)); }