public void query2(MdoQuery mq) { string request = ""; string xpath = ""; getRequestIdentifiers(mq, ref xpath, ref request); XmlDocument doc = new XmlDocument(); doc.Load(Path.Combine(XmlResourcePath, "MockConnection" + siteId + ".xml")); XmlNodeList list = doc.SelectNodes("//query"); foreach (XmlNode node in list) { string value = node.SelectSingleNode("value").InnerText; XmlNode responseNode = node.SelectSingleNode("response"); if (value.Equals(request)) { String response = responseNode.InnerText; if (null != responseNode.Attributes["type"]) { response = FileIOUtils.readFromFile(Path.Combine(DataResourcePath, response)); } addRequest(mq, response, false); return; } } }
/// <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); }
/// <summary> /// initializes <see cref="SettingsContainer"/> and corresponding data file. /// </summary> /// <seealso cref="initializeSettingsContainerFromExistingDataFile"/> /// <seealso cref="initializeDefaultSettingsContainerAndDataFile"/> /// <exception cref="FileReadSettingsInitializationException"> /// thrown if reading data from <see cref="SettingsContainer"/> file failed /// </exception> /// <exception cref="CorruptFileSettingsInitializationException"> /// thrown if <see cref="SettingsContainer"/> file data is not in a valid JSON format, /// or <see cref="SettingsContainer"/> parsing from data failed /// </exception> /// <exception cref="FileCreateSettingsInitializationException"> /// thrown if creating a <see cref="SettingsContainer"/> data file with default settings /// failed /// </exception> private void initializeSettings() { if (FileIOUtils.FileExists(SETTINGS_FILE_PATH)) // settings file exists { ConsoleIOManager.Instance.LogNotice( "Settings file found. Using existing file.", ConsoleIOManager.eOutputReportType.System); try { initializeSettingsContainerFromExistingDataFile(); } catch (Exception exception) { if (exception is FileReadException) // settings file read failed { throw new FileReadSettingsInitializationException( SETTINGS_FILE_PATH, exception); } // corrupt settings file else if ( exception is JsonSerializationException || exception is SettingsContainerJsonObjectParseException) { throw new CorruptFileSettingsInitializationException( SETTINGS_FILE_PATH, exception); } else // unhandled exception { throw exception; } } } else // settings file does not exist { ConsoleIOManager.Instance.LogNotice( "Settings file not found. Creating new settings file with default values ..", ConsoleIOManager.eOutputReportType.System); try { initializeDefaultSettingsContainerAndDataFile(); ConsoleIOManager.Instance.LogNotice( "New Settings file created successfully.", ConsoleIOManager.eOutputReportType.System); } catch (FileWriteException fileWriteException) { throw new FileCreateSettingsInitializationException( SETTINGS_FILE_PATH, fileWriteException); } } }
/// <summary> /// initializes a <see cref="SettingsContainer"/> with default values, /// and creates a corresponding <see cref="SettingsContainer"/> data file. /// </summary> /// <exception cref="FileWriteException"> /// <seealso cref="FileIOUtils.WriteTextToFile(string, string)"/> /// </exception> private void initializeDefaultSettingsContainerAndDataFile() { // initialize settings container with default values this.settingsContainer = new SettingsContainer(); // create settings file having default values string settingsContainerJsonString = JsonUtils.SerializeObject(this.settingsContainer); FileIOUtils.WriteTextToFile(SETTINGS_FILE_PATH, settingsContainerJsonString); }
/// <summary> /// writes <see cref="SettingsContainer"/> object JSON serialization to /// data file. /// </summary> /// <exception cref="BackupFileCreateException"> /// <seealso cref="FileIOUtils.WriteTextToFileWithBackup(string, string, string)"/> /// </exception> /// <exception cref="BackupFileRenameException"> /// <seealso cref="FileIOUtils.WriteTextToFileWithBackup(string, string, string)"/> /// </exception> /// <exception cref="BackupFileDeleteException"> /// <seealso cref="FileIOUtils.WriteTextToFileWithBackup(string, string, string)"/> /// </exception> private void writeSettingsContainerDataToFile() { // get SettingsContainer JSON string string settingsContainerJsonString = JsonUtils.SerializeObject(this.settingsContainer); // write SettingsContainer JSON string to data file FileIOUtils.WriteTextToFileWithBackup( SETTINGS_FILE_PATH, settingsContainerJsonString, BACKUP_SETTINGS_FILE_PATH); }
/// <summary> /// writes <see cref="UserDefinedCommandContainer"/> object JSON serialization to /// data file. /// </summary> /// <exception cref="BackupFileCreateException"> /// <seealso cref="FileIOUtils.WriteTextToFileWithBackup(string, string, string)"/> /// </exception> /// <exception cref="BackupFileRenameException"> /// <seealso cref="FileIOUtils.WriteTextToFileWithBackup(string, string, string)"/> /// </exception> /// <exception cref="BackupFileDeleteException"> /// <seealso cref="FileIOUtils.WriteTextToFileWithBackup(string, string, string)"/> /// </exception> private void writeUserDefinedCommandContainerDataToFile() { // get UserDefinedCommandContainer JSON string string userDefinedCommandContainerJsonString = JsonUtils.SerializeObject( this.userDefinedCommandContainer); // write UserDefinedCommandContainer JSON string to fiel FileIOUtils.WriteTextToFileWithBackup( USER_DEFINED_COMMANDS_FILE_PATH, userDefinedCommandContainerJsonString, BACKUP_USER_DEFINED_COMMANDS_FILE_PATH); }
/// <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(); } }
/// <summary> /// Get the response from the selected query node /// </summary> /// <param name="query">a valid query node</param> /// <returns>the response string</returns> private string getResponse(XmlNode query) { string rpcName = query.Attributes.GetNamedItem(RPC_ATTR).Value; XmlNode result = query.SelectSingleNode(RESPONSE_NODE); if (null != result) { if (null == result.Attributes.GetNamedItem("type", "")) { return(result.InnerText); } else { return(FileIOUtils.readFromFile(Path.Combine(DataResourcePath, result.InnerText))); } } throw new DataException(String.Format("response for rpc: {0} is missing from the mock data", rpcName)); }
/// <summary> /// synchronously writes <paramref name="content"/> to file at location <paramref name="filePath"/>. /// if file at <paramref name="filePath"/> does not exist, /// creates a new file containing <paramref name="content"/>. /// if file exists, overrwrites existing file safely. /// </summary> /// <seealso cref="FileIOUtils.WriteTextToFile(string, string, string)"/> /// <param name="filePathWithoutExtension"></param> /// <param name="fileExtension"></param> /// <param name="content"></param> /// <exception cref="BackupFileCreateException"> /// <seealso cref="FileIOUtils.WriteTextToFile(string, string, string)"/> /// </exception> /// <exception cref="FileRenameException"> /// <seealso cref="FileIOUtils.WriteTextToFile(string, string, string)"/> /// </exception> /// <exception cref="BackupFileDeleteException"> /// <seealso cref="FileIOUtils.WriteTextToFile(string, string, string)"/> /// </exception> public void WriteTextToFile( string filePathWithoutExtension, string fileExtension, string content) { string filePath = filePathWithoutExtension + fileExtension; string backupFilePath = getBackupFilePath(filePathWithoutExtension); try { FileIOUtils.WriteTextToFileWithBackup(filePath, content, backupFilePath); } catch (BackupFileWriteException backupFileWriteException) { // backup file could not be renamed to requested file path if (backupFileWriteException is BackupFileRenameException) { BackupFileRenameException backupFileRenameException = backupFileWriteException as BackupFileRenameException; try { // try renaming backup file to to requested file path FileIOUtils.RenameFile(backupFileRenameException.BackupFilePath, filePath); } catch (FileRenameException) // renaming failed again { throw backupFileWriteException; // throw original exception } } else { throw backupFileWriteException; // throw original exception } } }
/// <summary> /// Adds or updates an existing query node in the mock connection file /// </summary> /// <param name="node"></param> /// <param name="request"></param> /// <param name="response"></param> private void addUpdateResponseNode(XmlNode node, string request, string response) { if (isFileWorthy(response)) { // save response to dat file string dataFileName = ""; XmlAttribute type = node.Attributes["type"]; if (null == type) { type = siteDoc.CreateAttribute("type"); type.Value = "file"; node.Attributes.Append(type); dataFileName = (siteId + request).GetHashCode().ToString() + ".dat"; } else { dataFileName = node.InnerText; } node.InnerText = dataFileName; FileIOUtils.writeToFile( Path.Combine(DataResourcePath, dataFileName), response); } else { node.RemoveAll(); // save response directly to the xml file XmlCDataSection cdata = siteDoc.CreateCDataSection(response); node.AppendChild(cdata); node.Attributes.RemoveAll(); } }
public void DeleteFile(string filePath) { FileIOUtils.DeleteFile(filePath); }
public string ReadTextFromFile(string filePath) { return(FileIOUtils.ReadTextFromFile(filePath)); }
public void AppendTextToFile(string filePath, string text) { FileIOUtils.AppendTextToFile(filePath, text); }