protected override async Task <Action <AsyncCodeActivityContext> > ExecuteAsync(AsyncCodeActivityContext context, CancellationToken cancellationToken)
        {
            // Object Container: Use objectContainer.Get<T>() to retrieve objects from the scope
            var objectContainer = context.GetFromContext <IObjectContainer>(TextApplicationScope.ParentContainerPropertyTag);

            // Inputs
            string inputText = objectContainer.Get <string>();

            // Inputs
            var searchWordsCol = SearchWord.Get(context);
            var replacedWord   = ReplacedWord.Get(context);
            var textOccurrance = TextOccurrance.Get(context);
            var indexOccurence = IndexOccurence.Get(context);
            var displayLog     = DisplayLog;

            //Convert Collection to Array
            string[] searchWords = Utils.ConvertCollectionToArray(searchWordsCol);

            ///////////////////////////
            // Add execution logic HERE
            //Replace word from text
            string OutputString = Utils.ReplaceWordsFromText(inputText, searchWords, replacedWord, textOccurrance, indexOccurence, displayLog);

            ///////////////////////////

            // Outputs
            return((ctx) =>
            {
                AdjustedText.Set(ctx, OutputString);
            });
        }
        /// <summary>
        /// 对传入的字符串进行关键字替换,并返回还原特征字符串(如果传入的字符串为空,那么特征字符串也会为空,因为不需要对其进行版本控制)
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public string Replace(string value, out string newVersion, out string newRevertInfo)
        {
            InitRegex();

#if DEBUG
            //System.Web.HttpContext.Current.Response.Write("<font style=\"size:26px;color:red\">注意,发生了关键字替换,请确认这不是BUG</font><br />");
#endif

            if (string.IsNullOrEmpty(value) || m_ReplaceKeyWordRegex == null || m_ReplaceKeywords == null || m_ReplaceKeywords.Count == 0)
            {
                newVersion    = Version;
                newRevertInfo = string.Empty;
                return(value);
            }


            int indexOffset = 0;
            ReplacedWordCollection replacedWords = new ReplacedWordCollection();

            string result = m_ReplaceKeyWordRegex.Replace(value, delegate(Match match)
            {
                object obj = m_ReplaceKeywords[match.Value];

                if (obj != null)
                {
                    string newWord = (string)obj;
                    int newLength  = newWord.Length;
                    int newIndex   = match.Index + indexOffset;
                    indexOffset   += newLength - match.Length;

                    ReplacedWord word = new ReplacedWord();
                    word.Index        = newIndex;
                    word.Length       = newLength;
                    word.OriginalWord = match.Value;
                    replacedWords.Add(word);

                    return(newWord);
                }

                return(match.Value);
            });

            if (replacedWords.Count > 0)
            {
                newRevertInfo = replacedWords.ToString();
            }
            else
            {
                newRevertInfo = string.Empty;
            }

            newVersion = this.Version;

            return(result);
        }
Esempio n. 3
0
 /// <summary>
 ///     This is the method that is responsible for notifying
 ///     receivers that the event occurred
 /// </summary>
 protected virtual void OnReplacedWord(ReplaceWordEventArgs e)
 {
     ReplacedWord?.Invoke(this, e);
 }
        protected String CapitalizeWords(String OriginalString, String WordsToUse)
        {
            if (OriginalString == null)
            {
                return(null);
            }

            String s = OriginalString.ToUpper();

            if (WordsToUse != null)
            {
                String[]  words            = WordsToUse.Split((" .,").ToCharArray());
                ArrayList CapitalizedWords = new ArrayList();

                foreach (String word in words)
                {
                    String sw    = "";
                    bool   First = true;
                    foreach (char ch in word)
                    {
                        if (First)
                        {
                            First = false;
                            sw   += char.ToUpper(ch);
                        }
                        else
                        {
                            sw += char.ToLower(ch);
                        }
                    }
                    if (!First)
                    {
                        CapitalizedWords.Add(sw);
                    }
                }

                CapitalizedWords.Sort(new CompareStringLength());

                ArrayList ReplacedWords = new ArrayList();

                foreach (String word in CapitalizedWords)
                {
                    bool DontReplace = false;
                    foreach (String ReplacedWord in ReplacedWords)
                    {
                        if (ReplacedWord.IndexOf(word) >= 0)
                        {
                            DontReplace = true;
                            break;
                        }
                    }

                    if (DontReplace)
                    {
                        break;
                    }
                    s = s.Replace(word.ToUpper(), word);
                    ReplacedWords.Add(word.ToUpper());
                }
            }

            char[] charr       = s.ToCharArray();
            char[] charr_build = s.ToCharArray();

            for (int i = 1; i < charr.Length; i++)
            {
                if (char.IsUpper(charr[i]))
                {
                    if (i < (charr.Length - 1))
                    {
                        if (char.IsLower(charr[i + 1]))
                        {
                            continue;
                        }
                    }

                    if (char.IsLower(charr[i - 1]))
                    {
                        continue;
                    }

                    charr_build[i] = char.ToLower(charr[i]);
                }
            }

            s = "";
            foreach (char ch in charr_build)
            {
                s += ch;
            }

            return(s);
        }