예제 #1
0
        /// <summary>
        /// Processes the message.
        /// </summary>
        /// <param name="textMessage">The text message.</param>
        /// <param name="rootModel">The root model.</param>
        public void ProcessMessage([NotNull] TextMessage textMessage, [NotNull] RootModel rootModel)
        {
            Assert.ArgumentNotNull(textMessage, "textMessage");
            Assert.ArgumentNotNull(rootModel, "rootModel");

            string text = textMessage.InnerText;

            if (string.IsNullOrEmpty(textMessage.InnerText))
            {
                return;
            }

            if (IsRegExp)
            {
                Regex rExp;
                if (_compiledRegex != null)
                {
                    rExp = _compiledRegex;
                }
                else
                {
                    var varReplace = rootModel.ReplaceVariables(TextToHighlight);
                    if (!varReplace.IsAllVariables)
                    {
                        return;
                    }

                    rExp = new Regex(varReplace.Value);
                }

                Match match = rExp.Match(textMessage.InnerText);
                while (match.Success)
                {
                    textMessage.HighlightText(match.Index, match.Length, ForegroundColor, BackgroundColor);
                    match = rExp.Match(textMessage.InnerText, match.Index + match.Length);
                }
            }
            else
            {
                int position = 0;
                ClearMatchingResults();

                var res = GetRootPatternToken(rootModel).Match(text, position, _matchingResults);

                while (res.IsSuccess)
                {
                    textMessage.HighlightText(res.StartPosition, res.EndPosition - res.StartPosition, ForegroundColor,
                                              BackgroundColor);
                    position = res.EndPosition;
                    ClearMatchingResults();
                    res = GetRootPatternToken(rootModel).Match(text, position, _matchingResults);
                }
            }
        }
예제 #2
0
        public bool MatchMessage(TextMessage textMessage, RootModel rootModel)
        {
            ClearMatchingResults();

            if (IsRegExp)
            {
                Match match;
                if (_compiledRegex != null)
                {
                    match = _compiledRegex.Match(textMessage.InnerText);
                }
                else
                {
                    var varReplace = rootModel.ReplaceVariables(MatchingPattern);
                    if (!varReplace.IsAllVariables)
                    {
                        return(false);
                    }

                    Regex rExp = new Regex(varReplace.Value);
                    match = rExp.Match(textMessage.InnerText);
                }

                if (!match.Success)
                {
                    return(false);
                }

                for (int i = 0; i < 10; i++)
                {
                    if (i + 1 < match.Groups.Count)
                    {
                        _matchingResults[i] = match.Groups[i + 1].ToString();
                    }
                }

                return(true);
            }

            var res = GetRootPatternToken(rootModel).Match(textMessage.InnerText, 0, _matchingResults);

            return(res.IsSuccess);
        }
예제 #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="textMessage"></param>
        /// <param name="rootModel"></param>
        public void HandleMessage([NotNull] TextMessage textMessage, [NotNull] RootModel rootModel)
        {
            Assert.ArgumentNotNull(textMessage, "message");
            Assert.ArgumentNotNull(rootModel, "rootModel");

            ClearMatchingResults();
            string text = textMessage.InnerText;

            if (string.IsNullOrEmpty(textMessage.InnerText))
            {
                return;
            }

            if (IsRegExp)
            {
                Regex rExp;
                if (_compiledRegex != null)
                {
                    rExp = _compiledRegex;
                }
                else
                {
                    var varReplace = rootModel.ReplaceVariables(_pattern);
                    if (!varReplace.IsAllVariables)
                    {
                        return;
                    }

                    rExp = new Regex(varReplace.Value);
                }

                StringBuilder sb     = new StringBuilder(text.Length);
                int           offset = 0;
                Match         match  = rExp.Match(textMessage.InnerText);
                if (match.Success)
                {
                    do
                    {
                        if (offset < match.Index)
                        {
                            sb.Append(text, offset, match.Index - offset);
                        }

                        offset = match.Index + match.Length;

                        sb.Append(_wildRegex.Replace(SubstituteWith,
                                                     m =>
                        {
                            return(match.Groups[int.Parse(m.Value.Substring(1))].Value);
                        }));

                        match = rExp.Match(textMessage.InnerText, match.Index + match.Length);
                    } while (match.Success);

                    if (offset < textMessage.InnerText.Length)
                    {
                        sb.Append(text, offset, textMessage.InnerText.Length - offset);
                    }

                    //TODO: We have cleared information about color :( Need to do something with this
                    textMessage.Clear();
                    textMessage.AppendText(sb.ToString());
                }
            }
            else
            {
                int position = 0;
                ClearMatchingResults();

                var res = GetRootPatternToken(rootModel).Match(text, position, _matchingResults);

                while (res.IsSuccess)
                {
                    //Actually method TextMessage.Substitute() too heavy if we are substituting more than one substitution in one string
                    //TODO: Best way build string right here
                    textMessage.Substitute(res.StartPosition, res.EndPosition - res.StartPosition, GetSubstituteWithPatternToken(rootModel).GetValue(_matchingResults));

                    position = res.StartPosition + GetSubstituteWithPatternToken(rootModel).GetValue(_matchingResults).Length;
                    ClearMatchingResults();

                    if (position > text.Length)
                    {
                        break;
                    }

                    res = GetRootPatternToken(rootModel).Match(text, position, _matchingResults);
                }
            }
        }