/// <summary> /// Convert EmotionType to string used in script file. /// </summary> /// <param name="emotionType">EmotionType.</param> /// <returns> String representation of EmotionType.</returns> public static string EmotionTypeToString(EmotionCategory emotionType) { string emotionStr = string.Empty; switch (emotionType) { case EmotionCategory.Abashed: emotionStr = "abashed"; break; case EmotionCategory.Angry: emotionStr = "angry"; break; case EmotionCategory.Bouncy: emotionStr = "bouncy"; break; case EmotionCategory.Calm: emotionStr = "calm"; break; case EmotionCategory.Confident: emotionStr = "confident"; break; case EmotionCategory.Considerate: emotionStr = "considerate"; break; case EmotionCategory.Disappointed: emotionStr = "disappointed"; break; case EmotionCategory.Disgust: emotionStr = "disgust"; break; case EmotionCategory.Elated: emotionStr = "elated"; break; case EmotionCategory.Happy: emotionStr = "happy"; break; case EmotionCategory.Neutral: break; case EmotionCategory.Optimistic: emotionStr = "optimistic"; break; case EmotionCategory.PraseUnknown: emotionStr = "praseUnknown"; break; case EmotionCategory.Sad: emotionStr = "sad"; break; case EmotionCategory.Satisfied: emotionStr = "satisfied"; break; case EmotionCategory.Sensitive: emotionStr = "sensitive"; break; case EmotionCategory.Sorry: emotionStr = "sorry"; break; case EmotionCategory.Worry: emotionStr = "worry"; break; default: string message = Helper.NeutralFormat("Unrecognized emotion type: \"{0}\"!", Convert.ToString(emotionType)); throw new InvalidDataException(message); } return emotionStr; }
/// <summary> /// 将所有表情写入集合中供下面的转换 /// </summary> /// <param name="category">表情分类</param> private void PrepareTransformsInfo(EmotionCategory category) { if (category == null || category.Emotions == null) { return; } foreach (Emotion emotion in category.Emotions) { string smileyPattern = emotion.FormatedCode; string replacePattern = string.Format("<img src=\"{0}\" title=\"{1}\" alt=\"{1}\" />", emotion.ImageUrl, emotion.Description); maxWordLength = Math.Max(maxWordLength, smileyPattern.Length); minWordLength = Math.Min(minWordLength, smileyPattern.Length); foreach (char c in smileyPattern) { allCharCheck[c] = true; } emoctionDictionary[smileyPattern] = replacePattern; } }
/// <summary> /// 更新表情分类 /// </summary> /// <param name="emotionCategory">表情分类</param> public void UpdateEmotionCategory(EmotionCategory emotionCategory) { emotionRepository.Update(emotionCategory); //加载表情 if (emotionCategory.IsEnabled && (emotionCategory.Emotions == null || emotionCategory.Emotions.Count() == 0)) LoadEmoticons(emotionCategory); }
/// <summary> /// 组装表情实体 /// </summary> /// <param name="category">表情分类</param> /// <param name="categoryElement">表情分类配置节点</param> private void PopulateEmotion(EmotionCategory category, XElement categoryElement) { string emoticonPath = WebUtility.ResolveUrl(_emotionSettings.EmoticonPath); IEnumerable<XElement> emotionsElements = categoryElement.Elements(); foreach (var emotionElement in emotionsElements) { Emotion emotion = Emotion.New(emotionElement); emotion.FormatedCode = string.Format("[{0}{1}]", !string.IsNullOrEmpty(category.CodePrefix) ? category.CodePrefix + ":" : string.Empty, emotion.Code); emotion.ImageUrl = string.Format("{0}/{1}/{2}", emoticonPath, category.DirectoryName, emotion.FileName); if (category.Emotions.Where(n => n.Code == emotion.Code).Count() == 0) category.Emotions.Add(emotion); PrepareTransformsInfo(category); } }
/// <summary> /// 加载表情分类下的表情 /// </summary> /// <param name="emotionCategory">表情分类</param> public void LoadEmoticons(EmotionCategory emotionCategory) { if (emotionCategory == null) return; XElement document = XElement.Load(emotionCategory.PhysicalDirectoryPath + "\\" + _emoticonConfigName); if (document != null) { //为启用的分类加载表情 PopulateEmotion(emotionCategory, document); } }