コード例 #1
0
        /// <summary>
        /// Helper method to read the conversation from <paramref name="inputFilePath"/>.
        /// </summary>
        /// <param name="inputFilePath">
        /// The input file path.
        /// </param>
        /// <param name="userName">
        /// The string representing the username to filter messages with.
        /// </param>
        /// <param name="keyword">
        /// The string used in to match message content by keyword.
        /// </param>
        /// <param name="blacklist">
        /// The strings to be blacklisted from message content.
        /// </param>
        /// <param name="encryptUsernames">
        /// Boolean flag indicating whether to encrypt usernames or not.
        /// </param>
        /// <param name="hideNumbers">
        /// Boolean flag indicating whether to hide numbers or not.
        /// </param>
        /// <returns>
        /// A <see cref="Conversation"/> model representing the conversation.
        /// </returns>
        /// <exception cref="ArgumentException">
        /// Thrown when the input file could not be found.
        /// </exception>
        /// <exception cref="Exception">
        /// Thrown when something else went wrong.
        /// </exception>
        public Conversation ReadConversation(string inputFilePath, string userName, string keyword, string blacklist)
        {
            try
            {
                FileStream stream = new FileStream(inputFilePath, FileMode.Open, FileAccess.Read);
                // using statement to properly dispose reader at end of scope.
                using (StreamReader reader = new StreamReader(stream, Encoding.ASCII))
                {
                    string conversationName = reader.ReadLine();
                    var    messages         = new List <Message>();

                    string line;

                    while ((line = reader.ReadLine()) != null)
                    {
                        Message msg = MessageGenerator.CreateNewMessage(line, blacklist);

                        if (MessageFilter.Filter(msg, userName, keyword))
                        {
                            // add the newly instantiated message in List.
                            messages.Add(msg);
                        }
                    }

                    return(new Conversation(conversationName, messages));
                }
            }
            catch (FileNotFoundException)
            {
                throw new FileNotFoundException("The file was not found.");
            }
            catch (IOException)
            {
                throw new IOException("Something went wrong in the IO.");
            }
        }