public ProcessNodeResults ProcessNode(
            [NotNull] XElement element,
            [NotNull] IConfiguration configuration)
        {
            if (element == null)
            {
                throw new ArgumentNullException(nameof(element));
            }
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            var attributeName = XName.Get("applyWhen", XmlTemplate.ConfigGenXmlNamespace);
            var attribute     = element.Attribute(attributeName);

            if (attribute == null)
            {
                throw new ArgumentException("Supplied element should contain an attribute with name 'applyWhen' in the configgen namespace.", nameof(element));
            }

            var expression = attribute.Value;

            if (expression.IsNullOrEmpty())
            {
                attribute.Remove();
                return(ProcessNodeResults.CreateErrorResult(XmlTemplateErrorCodes.ConditionProcessingError, "Condition error: and empty condition was encountered"));
            }

            ExpressionEvaluationResults evaluationResults = _evaluator.Evaluate(configuration.ConfigurationName, expression, element.Name);

            foreach (var usedToken in evaluationResults.UsedTokens)
            {
                if (configuration.Contains(usedToken))
                {
                    _tokenUsageTracker.OnTokenUsed(configuration.ConfigurationName, usedToken);
                }
                else
                {
                    _tokenUsageTracker.OnTokenNotRecognised(configuration.ConfigurationName, usedToken);
                }
            }

            if (evaluationResults.Result)
            {
                attribute.Remove();
            }
            else
            {
                element.Remove();
            }

            if (evaluationResults.ErrorCode != null)
            {
                return(ProcessNodeResults.CreateErrorResult(evaluationResults.ErrorCode, evaluationResults.ErrorMessage));
            }

            return(ProcessNodeResults.CreateSuccessResult());
        }
Exemplo n.º 2
0
        protected ExpressionEvaluationResults EvaluateCondition(
            [NotNull] XElement element,
            [NotNull] IConfiguration configuration,
            [NotNull] IConfigurationExpressionEvaluator configurationExpressionEvaluator)
        {
            if (element == null)
            {
                throw new ArgumentNullException(nameof(element));
            }
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }
            if (configurationExpressionEvaluator == null)
            {
                throw new ArgumentNullException(nameof(configurationExpressionEvaluator));
            }

            string expression         = null;
            var    conditionAttribute = element.Attribute(XName.Get(ConditionAttributeName, string.Empty));

            if (conditionAttribute != null)
            {
                expression = conditionAttribute.Value;
            }

            ExpressionEvaluationResults evaluationResults = configurationExpressionEvaluator.Evaluate(configuration.ConfigurationName, expression, element.Name);

            foreach (var usedToken in evaluationResults.UsedTokens)
            {
                if (configuration.Contains(usedToken))
                {
                    _tokenUsageTracker.OnTokenUsed(configuration.ConfigurationName, usedToken);
                }
                else
                {
                    _tokenUsageTracker.OnTokenNotRecognised(configuration.ConfigurationName, usedToken);
                }
            }

            return(evaluationResults);
        }