Exemplo n.º 1
0
        private static bool TryGetPropertyValue(object formatArgument, 
                                                string argumentName,
                                                string formatOption,
                                                out string argumentValue,
                                                ParseOptions parseOptions)
        {
            argumentValue = null;
            var argumentPath = argumentName.Split('.');

            PropertyInfo property = null;
            //Get nested property value
            foreach (var pathItem in argumentPath)
            {
                var type = formatArgument.GetType();
                property = type.GetProperty(pathItem);
                if (property == null) break;

                formatArgument = property.GetValue(formatArgument, null);
                if (formatArgument == null) break;
            }

            if (property == null) return false;
            if (formatArgument == null) return false;

            if (formatArgument is DateTime)
            {
                //Convert date if necessary
                if (parseOptions.CustomDateConverter != null)
                {
                    formatArgument = parseOptions.CustomDateConverter((DateTime)formatArgument);
                }

                if (!formatOption.IsNullOrEmpty() && allowedDateFormats.Contains(formatOption))
                {
                    argumentValue = ((DateTime)formatArgument).ToString(formatOption);
                }
                else
                {
                    argumentValue = formatArgument.ToString();
                }
            }
            else
            {
                argumentValue = formatArgument.ToString();
            }

            return true;
        }
Exemplo n.º 2
0
        private static string ProcessParameters(string text, ParseOptions parseOptions, params object[] args)
        {
            var matches = parameter.Matches(text);

            foreach (Match match in matches)
            {
                string argument = match.Groups["argument"].Value;
                string replacementText = argument;
                string textToReplace = match.Groups["parameter"].Value;

                if (HasProperties(argument))
                {
                    string bindedText = BindProperties(argument, parseOptions, args);

                    if (bindedText == null && parseOptions.SwallowNewlineOnEmptyParameter)
                    {
                        textToReplace = match.Value;
                    }

                    replacementText = bindedText;
                }

                text = text.Replace(textToReplace, replacementText);
            }

            return text;
        }
Exemplo n.º 3
0
        private static string BindProperties(string argument, ParseOptions parseOptions, object[] args)
        {
            while (HasProperties(argument))
            {
                var firstMatch = propertyBinding.Matches(argument)[0];
                var replaceValue = GetArgumentValue(GetPropertyPath(firstMatch.Value), parseOptions, GetPropertyFormatOptions(firstMatch.Value), args);
                string textBefore = argument.Substring(0, firstMatch.Index);
                string textAfter = argument.Substring(firstMatch.Index + firstMatch.Length);

                if (replaceValue == null) return null;
                argument = textBefore + replaceValue + textAfter;
            }

            return argument;
        }
Exemplo n.º 4
0
        private static string GetArgumentValue(string argumentName, ParseOptions parseOptions, string formatOption, object[] args)
        {
            if (args == null || args.Length == 0) return null;

            string argumentValue = null;
            foreach (var formatAgument in args)
            {
                if (formatAgument == null) continue;
                if (TryGetPropertyValue(formatAgument, argumentName, formatOption, out argumentValue, parseOptions))
                {
                    if (parseOptions.ConsiderAsNull.Contains(argumentValue)) return null;
                    return (parseOptions.Trim && argumentValue != null) ? argumentValue.Trim() : argumentValue;
                }
            }

            return null;
        }
Exemplo n.º 5
0
        /// <summary>
        /// Parses the specified text with the specified options.
        /// </summary>
        /// <param name="text">The text.</param>
        /// <param name="parseOptions">The parse options.</param>
        /// <param name="args">The args.</param>
        /// <returns></returns>
        public static string Parse(string text, ParseOptions parseOptions, params object[] args)
        {
            if (parseOptions == null) throw new ArgumentNullException("parseOptions", "parseOptions is null.");

            if (HasParameters(text)) return ProcessParameters(text, parseOptions, args);
            return text;
        }