Exemple #1
0
        private string PerformEnumerableExpansionReplacement(string input, object model)
        {
            //Replace variables
            var replacedInput = EnumerableExpansionRegex.Replace(input, m =>
            {
                var properties  = ModelReflectionUtil.GetCaptureGroupValues(m, "ParameterName");
                var propertyVal = ModelReflectionUtil.GetPropertyValueFromParameterCollection(model, properties);

                if (!propertyVal.Item1) //Enumerable doesn't exist
                {
                    return(string.Empty);
                }

                var enumerable = propertyVal.Item2 as IEnumerable;
                if (enumerable == null) //The object wasn't an enumerable
                {
                    return(SubstitutionErrorString);
                }

                var expandedEnumerableBody = new StringBuilder();
                var enumerableContent      = m.Groups["Contents"].Value;
                var enumerableItemRegex    = new Regex(@"{\$element}", RegexOptions.Compiled);
                foreach (var item in enumerable)
                {
                    var replacedContent = enumerableItemRegex.Replace(enumerableContent, item.ToString());
                    expandedEnumerableBody.AppendLine(replacedContent);
                }

                var expandedEnumerableStr = expandedEnumerableBody.ToString();

                return(expandedEnumerableStr);
            });

            return(replacedInput);
        }
Exemple #2
0
        private string PerformConditionalSubstitutions(string input, object model)
        {
            //Process conditionals
            var replacedInput = ConditionalRegex.Replace(input, m =>
            {
                var properties       = ModelReflectionUtil.GetCaptureGroupValues(m, "ParameterName");
                var propertyVal      = ModelReflectionUtil.GetPropertyValueFromParameterCollection(model, properties);
                var allowNonexistent = m.Groups["AllowNonexistent"].Success;
                var negateResult     = m.Groups["Not"].Success;

                bool propertyExists = propertyVal.Item1;

                //If the property doesnt exist and nonexistent is not allowed, error
                if (!propertyExists && !allowNonexistent)
                {
                    return(SubstitutionErrorString);
                }

                //Check if the property isn't null
                bool evaluateResult = propertyVal.Item2 != null;

                //Check if property result is a BOOLEAN
                if (evaluateResult && propertyVal.Item2 is bool?) //if the property isn't null, and if it's a boolean
                {
                    //Get the actual boolean value
                    var booleanPropertyResult = propertyVal.Item2 as bool?;
                    evaluateResult            = (bool)booleanPropertyResult;
                }

                if (negateResult)                     //A NOT is present
                {
                    evaluateResult = !evaluateResult; //negate
                }

                var conditionalContent = m.Groups["Contents"].Value;

                if (evaluateResult)
                {
                    return(conditionalContent);
                }
                return(string.Empty);
            });

            return(replacedInput);
        }
Exemple #3
0
        private string PerformSingleSubstitutions(string input, object model)
        {
            //Replace variables
            var replacedInput = SingleVariableSubstitutionRegex.Replace(input, m =>
            {
                var properties   = ModelReflectionUtil.GetCaptureGroupValues(m, "ParameterName");
                var substitution = ModelReflectionUtil.GetPropertyValueFromParameterCollection(model, properties);
                if (!substitution.Item1)
                {
                    return(SubstitutionErrorString);
                }

                if (substitution.Item2 == null)
                {
                    return(string.Empty);
                }
                return(m.Groups["Encode"].Success ? XmlEncode(substitution.Item2.ToString()) : substitution.Item2.ToString());
            });

            return(replacedInput);
        }