Пример #1
0
        public static string ExpandProperties(object target, string inString, char startSeperator, char endSeperator, char formatSeperator,
                                              IChoPropertyReplacer[] propertyReplacers)
        {
            if (inString.IsNullOrEmpty())
            {
                return(inString);
            }

            ChoGuard.ArgumentNotNull(propertyReplacers, "Property Replacers");

            string msg = inString;

            if (inString.IndexOf(startSeperator) != -1)
            {
                int           index      = -1;
                bool          hasChoChar = false;
                StringBuilder message    = new StringBuilder();
                StringBuilder token      = new StringBuilder();
                while (++index < inString.Length)
                {
                    if (!hasChoChar && inString[index] == startSeperator &&
                        index + 1 < inString.Length && inString[index + 1] == startSeperator)
                    {
                        hasChoChar = true;
                    }
                    else if (inString[index] == startSeperator)
                    {
                        if (hasChoChar)
                        {
                            bool hadEndChoChar = false;
                            do
                            {
                                if (inString[index] == endSeperator && inString[index - 1] == endSeperator)
                                {
                                    if (!hadEndChoChar)
                                    {
                                        hadEndChoChar = true;
                                    }
                                    else
                                    {
                                        message.Append(inString[index]);
                                    }

                                    continue;
                                }
                                message.Append(inString[index]);
                            }while (++index < inString.Length && inString[index] != startSeperator);

                            index--;
                            hasChoChar = false;
                        }
                        else
                        {
                            token.Remove(0, token.Length);
                            index++;
                            do
                            {
                                if (!hasChoChar && inString[index] == endSeperator &&
                                    index + 1 < inString.Length && inString[index + 1] == endSeperator)
                                {
                                    hasChoChar = true;
                                }
                                else if (inString[index] == endSeperator)
                                {
                                    if (hasChoChar)
                                    {
                                        message.Append(startSeperator);
                                        message.Append(token);
                                        message.Append(inString[index]);
                                        bool hadEndChoChar = false;
                                        do
                                        {
                                            if (inString[index] == endSeperator && inString[index - 1] == endSeperator)
                                            {
                                                if (!hadEndChoChar)
                                                {
                                                    hadEndChoChar = true;
                                                }
                                                else
                                                {
                                                    message.Append(inString[index]);
                                                }

                                                continue;
                                            }
                                            message.Append(inString[index]);
                                        }while (++index < inString.Length && inString[index] == endSeperator);
                                    }
                                    else
                                    {
                                        if (token.Length > 0)
                                        {
                                            string[] propertyNameNFormat = token.ToString().SplitNTrim(formatSeperator);
                                            if (!(propertyNameNFormat.Length >= 1 &&
                                                  ReplaceToken(propertyReplacers, message, propertyNameNFormat[0],
                                                               propertyNameNFormat.Length == 2 ? propertyNameNFormat[1] : null)))
                                            {
                                                message.AppendFormat("{0}{1}{2}", startSeperator, token, endSeperator);
                                            }
                                        }
                                    }

                                    break;
                                }
                                else
                                {
                                    token.Append(inString[index]);
                                }
                            }while (++index < inString.Length);
                        }
                    }
                    else
                    {
                        message.Append(inString[index]);
                    }
                }
                msg = message.ToString();
            }

            foreach (IChoPropertyReplacer propertyReplacer in propertyReplacers)
            {
                if (!(propertyReplacer is IChoCustomPropertyReplacer))
                {
                    continue;
                }

                IChoCustomPropertyReplacer customPropertyReplacer = propertyReplacer as IChoCustomPropertyReplacer;
                string formattedMsg = msg;
                bool   retVal       = customPropertyReplacer.Format(target, ref formattedMsg);

                if (!String.IsNullOrEmpty(formattedMsg))
                {
                    msg = formattedMsg;
                }

                if (!retVal)
                {
                    break;
                }
            }

            return(msg);
        }
Пример #2
0
        private static string ExpandProperties(object target, string inString, char startSeparator, char endSeparator, char formatSeparator,
                                               IChoPropertyReplacer[] propertyReplacers, bool rec, ref int innerIndex, bool internalCall)
        {
            if (inString.IsNullOrEmpty())
            {
                return(inString);
            }

            ChoGuard.ArgumentNotNull(propertyReplacers, "Property Replacers");

            string msg = inString;

            if (inString.IndexOfEx(startSeparator) != -1 ||
                inString.IndexOfEx(endSeparator) != -1)
            {
                int           index         = -1;
                bool          hasEscapeChar = false;
                StringBuilder message       = new StringBuilder();

                while (++index < inString.Length)
                {
                    if (inString[index] == ChoChar.Backslash)
                    {
                        if (!hasEscapeChar) //if first slash char found, flag and ignore
                        {
                            hasEscapeChar = true;
                        }
                        else  //if second slash char found, just append the char
                        {
                            hasEscapeChar = false;
                            message.Append(ChoChar.Backslash);
                            message.Append(inString[index]);
                        }
                        continue;
                    }

                    if (hasEscapeChar) //if backslash char preceded by it, add and continue
                    {
                        hasEscapeChar = false;
                        message.Append(ChoChar.Backslash);
                        message.Append(inString[index]);
                        continue;
                    }

                    if (startSeparator == endSeparator)
                    {
                        #region Both Start and End Seperator are same

                        if (inString[index] == startSeparator)
                        {
                            if (hasEscapeChar) //if slash char found, just append the char
                            {
                                hasEscapeChar = false;
                                message.Append(ChoChar.Backslash);
                                message.Append(inString[index]);
                                continue;
                            }

                            if (index + 1 < inString.Length && inString[index + 1] == startSeparator)
                            {
                                index++;

                                bool          hasInnerEscapeChar = false;
                                bool          endCharFound       = false;
                                StringBuilder token = new StringBuilder();
                                while (++index < inString.Length)
                                {
                                    endCharFound = false;

                                    if (inString[index] == ChoChar.Backslash)
                                    {
                                        if (!hasInnerEscapeChar) //if first slash char found, flag and ignore
                                        {
                                            hasInnerEscapeChar = true;
                                        }
                                        else  //if second slash char found, just append the char
                                        {
                                            hasInnerEscapeChar = false;
                                            message.Append(ChoChar.Backslash);
                                            token.Append(inString[index]);
                                        }
                                        continue;
                                    }

                                    if (hasInnerEscapeChar) //if backslash char preceded by it, add and continue
                                    {
                                        hasInnerEscapeChar = false;
                                        message.Append(ChoChar.Backslash);
                                        token.Append(inString[index]);
                                        continue;
                                    }

                                    if (inString[index] == endSeparator &&
                                        (index + 1 < inString.Length && inString[index + 1] == endSeparator))
                                    {
                                        index++;

                                        endCharFound = true;
                                        if (token.Length > 0)
                                        {
                                            string[] propertyNameNFormat = token.ToString().SplitNTrimEx(formatSeparator).ToArray();

                                            StringBuilder message1 = new StringBuilder(message.ToString());
                                            if (!(propertyNameNFormat.Length >= 1 &&
                                                  ReplaceToken(propertyReplacers, message1, propertyNameNFormat[0],
                                                               propertyNameNFormat.Length == 2 ? propertyNameNFormat[1] : null, target)))
                                            {
                                                message.AppendFormat("{0}{1}{2}", startSeparator, token, endSeparator);
                                            }
                                            else
                                            {
                                                message.Clear();
                                                message.Append(message1.ToString());
                                            }
                                            token.Remove(0, token.Length);
                                        }
                                        break;
                                    }
                                    else
                                    {
                                        token.Append(inString[index]);
                                    }
                                }
                                if (!endCharFound)
                                {
                                    message.AppendFormat("{0}{0}{1}", startSeparator, token);
                                }
                            }
                            else
                            {
                                message.Append(inString[index]);
                            }
                        }
                        else
                        {
                            message.Append(inString[index]);
                        }

                        #endregion Both Start and End Seperator are same
                    }
                    else
                    {
                        #region Start and End Seperators are different

                        if (inString[index] == startSeparator)
                        {
                            if (hasEscapeChar) //if slash char found, just append the char
                            {
                                hasEscapeChar = false;
                                message.Append(ChoChar.Backslash);
                                message.Append(inString[index]);
                                continue;
                            }

                            if (index + 1 < inString.Length && inString[index + 1] == startSeparator)
                            {
                                ++index;
                                ++index;
                                if (index < inString.Length)
                                {
                                    int    inIndex = 0;
                                    string subStr  = ExpandProperties(target, inString.Substring(index), startSeparator, endSeparator, formatSeparator, propertyReplacers, true, ref inIndex, internalCall);
                                    if (inIndex == 0)
                                    {
                                        message.AppendFormat("{0}{0}{1}", startSeparator, subStr);
                                        break;
                                    }
                                    else
                                    {
                                        message.Append(subStr);
                                        index += inIndex;
                                    }
                                    continue;
                                }
                            }
                            else
                            {
                                message.Append(inString[index]);
                            }
                        }
                        else if (inString[index] == endSeparator)
                        {
                            if (index + 1 < inString.Length && inString[index + 1] == endSeparator)
                            {
                                if (rec)
                                {
                                    index++;
                                    if (message.Length > 0)
                                    {
                                        string[] propertyNameNFormat = message.ToString().SplitNTrimEx(formatSeparator).ToArray();

                                        StringBuilder message1 = new StringBuilder();
                                        if (!(propertyNameNFormat.Length >= 1 &&
                                              ReplaceToken(propertyReplacers, message1, propertyNameNFormat[0],
                                                           propertyNameNFormat.Length == 2 ? propertyNameNFormat[1] : null, target)))
                                        {
                                            string token = message.ToString();
                                            message.Remove(0, message.Length);
                                            message.AppendFormat("{0}{0}{1}{2}{2}", startSeparator, token, endSeparator);
                                        }
                                        else
                                        {
                                            message.Clear();
                                            message.Append(message1.ToString());
                                        }
                                    }
                                    innerIndex = index;
                                    return(message.ToString());
                                }
                                else
                                {
                                    message.Append(inString[index]);
                                }
                            }
                            else
                            {
                                message.Append(inString[index]);
                            }
                        }
                        else
                        {
                            message.Append(inString[index]);
                        }

                        #endregion Start and End Seperators are different
                    }
                }
                msg = message.ToString();
            }

            if (!internalCall)
            {
                foreach (IChoPropertyReplacer propertyReplacer in propertyReplacers)
                {
                    if (!(propertyReplacer is IChoCustomPropertyReplacer))
                    {
                        continue;
                    }

                    IChoCustomPropertyReplacer customPropertyReplacer = propertyReplacer as IChoCustomPropertyReplacer;
                    string formattedMsg = msg;
                    bool   retVal       = customPropertyReplacer.Format(target, ref formattedMsg);

                    if (!String.IsNullOrEmpty(formattedMsg))
                    {
                        msg = formattedMsg;
                    }

                    if (!retVal)
                    {
                        break;
                    }
                }
            }

            return(msg);
        }