Ejemplo n.º 1
0
            /// <summary>
            /// initializes <see cref="SettingsContainer"/> with data from existing
            /// corresponding data file.
            /// </summary>
            /// <exception cref="FileReadException">
            /// thrown if reading from <see cref="SettingsContainer"/> data file failed
            /// </exception>
            /// <exception cref="JsonSerializationException">
            /// thrown if <see cref="SettingsContainer"/> data file content is not in a valid
            /// JSON format.
            /// </exception>
            /// <exception cref="SettingsContainerJsonObjectParseException">
            /// thrown if parsing <see cref="SettingsContainer"/> from <see cref="SettingsContainer"/>
            /// data file content failed
            /// </exception>
            private void initializeSettingsContainerFromExistingDataFile()
            {
                // initialize settings container with values from settings file
                string settingsContainerJsonString = FileIOUtils.ReadTextFromFile(SETTINGS_FILE_PATH);
                object settingsContainerJsonObject = JsonUtils.DeserializeObject <object>(
                    settingsContainerJsonString);

                this.settingsContainer = SettingsContainer.Parse(settingsContainerJsonObject);
            }
Ejemplo n.º 2
0
            /// <summary>
            /// initializes <see cref="UserDefinedCommandContainer"/> and corresponding data file.
            /// </summary>
            /// <exception cref="FileReadUserDefinedCommandsInitializationException">
            /// thrown if reading data from existing <see cref="UserDefinedCommandContainer"/>
            /// data file failed
            /// </exception>
            /// <exception cref="CorruptFileUserDefinedCommandsInitializationException">
            /// thrown if data in existing <see cref="UserDefinedCommandContainer"/> data file
            /// is invalid
            /// </exception>
            private void initializeUserDefinedCommands()
            {
                if (FileIOUtils.FileExists(USER_DEFINED_COMMANDS_FILE_PATH)) // saved commands file exists
                {
                    try
                    {
                        // initialize UserDefinedCommandsContainer with data from file
                        string userDefinedCommandsContainerJsonString = FileIOUtils.ReadTextFromFile(
                            USER_DEFINED_COMMANDS_FILE_PATH);
                        this.userDefinedCommandContainer = JsonUtils.DeserializeObject <UserDefinedCommandContainer>(
                            userDefinedCommandsContainerJsonString);
                    }
                    catch (Exception exception)
                    {
                        // reading from user defined commands file failed
                        if (exception is FileReadException)
                        {
                            throw new FileReadUserDefinedCommandsInitializationException(
                                      USER_DEFINED_COMMANDS_FILE_PATH,
                                      exception);
                        }
                        // corrupt user defined commands file
                        else if (exception is JsonSerializationException)
                        {
                            throw new CorruptFileUserDefinedCommandsInitializationException(
                                      USER_DEFINED_COMMANDS_FILE_PATH,
                                      exception);
                        }
                        else // unhandled exception
                        {
                            throw exception;
                        }
                    }
                }
                else // saved commands file does not exist
                {
                    // initialize an empty UserDefinedCommandContainer
                    this.userDefinedCommandContainer = new UserDefinedCommandContainer();

                    // write empty UserDefinedCommandContainer data to file
                    writeUserDefinedCommandContainerDataToFile();
                }
            }
Ejemplo n.º 3
0
 public string ReadTextFromFile(string filePath)
 {
     return(FileIOUtils.ReadTextFromFile(filePath));
 }