static public void RegexMatch(String sourceFile, String destinationFile, SettingFileType transformFile, List <SettingType> settings, bool isDryRun = false) { String[] lines = System.IO.File.ReadAllLines(sourceFile); String[] xformLines = null; if (transformFile != null) { xformLines = System.IO.File.ReadAllLines(transformFile.Value); } for (int i = 0; i < lines.Length; i++) { // Apply Settings From Transform File if (xformLines != null) { foreach (String xformLine in xformLines) { char[] delims = { ',' }; String[] values = xformLine.Split(delims); String key = null; String value = null; if (values.Length >= 2) { key = values[0].Trim(); value = values[1].Trim(); } else { continue; } if (Regex.IsMatch(lines[i], key)) { if (transformFile._HasEncryptedValues && !isDryRun) { Cipher cipher = new Cipher(config.Default.PassPhrase, config.Default.SaltValue, config.Default.InitVector); String newValue = cipher.Decrypt(value); if (!newValue.StartsWith("UNABLE TO DECRYPT")) { value = newValue; } } lines[i] = DoRegexReplaceWithAutoSave(lines[i], key, value, RegexOptions.IgnoreCase); } } } // Apply Settings From Package foreach (SettingType setting in settings) { if (Regex.IsMatch(lines[i], setting.Key)) { if (setting.Value != null) { String localValue = setting.Value.Value; if (setting.Value._IsEncrypted && !isDryRun) { Cipher cipher = new Cipher(config.Default.PassPhrase, config.Default.SaltValue, config.Default.InitVector); String newValue = cipher.Decrypt(localValue); if (!newValue.StartsWith("UNABLE TO DECRYPT")) { localValue = newValue; } } lines[i] = DoRegexReplaceWithAutoSave(lines[i], setting.Key, localValue, RegexOptions.IgnoreCase); } else { lines[i] = DoRegexReplaceWithAutoSave(lines[i], setting.Key, "", RegexOptions.IgnoreCase); } } } } if (!isDryRun) { System.IO.File.WriteAllLines(destinationFile, lines); } }
static public void KeyValue(PropertyFile.Type type, String sourceFile, String destinationFile, SettingFileType transformFile, List <SettingType> settings, bool isDryRun = false) { PropertyFile props = new PropertyFile(type, sourceFile); if (transformFile != null) { if (!String.IsNullOrWhiteSpace(transformFile.Value)) { String[] lines = File.ReadAllLines(transformFile.Value); foreach (String line in lines) { char[] delims = { ',' }; String[] values = line.Split(delims); String section = null; String key = null; String value = null; if (values.Length == 2) { key = values[0].Trim(); value = values[1].Trim(); } else if (values.Length >= 3) { section = values[0].Trim(); key = values[1].Trim(); value = values[2].Trim(); } else { continue; } if (!String.IsNullOrWhiteSpace(section)) { if (section.StartsWith(@"""")) { section = section.Substring(1, section.Length - 2); } } if (!String.IsNullOrWhiteSpace(key)) { if (key.StartsWith(@"""")) { key = key.Substring(1, key.Length - 2); } } if (!String.IsNullOrWhiteSpace(value)) { if (value.Trim().StartsWith(@"""")) { value = value.Substring(1, value.Length - 2); } } if (transformFile._HasEncryptedValues && !isDryRun) { Cipher cipher = new Cipher(config.Default.PassPhrase, config.Default.SaltValue, config.Default.InitVector); String newValue = cipher.Decrypt(value); if (!newValue.StartsWith("UNABLE TO DECRYPT")) { value = newValue; } } if (props.Exists(section, key)) { props.SetProperty(section, key, value); } else if (transformFile._CreateIfNotFound) { props.AddProperty(section, key, value); } } } } if (settings != null) { foreach (SettingType setting in settings) { String value = setting.Value.Value; if (setting.Value._IsEncrypted && !isDryRun) { Cipher cipher = new Cipher(config.Default.PassPhrase, config.Default.SaltValue, config.Default.InitVector); String newValue = cipher.Decrypt(value); if (!newValue.StartsWith("UNABLE TO DECRYPT")) { value = newValue; } } if (props.Exists(setting.Section, setting.Key)) { props.SetProperty(setting.Section, setting.Key, value); } else if (setting._CreateIfNotFound) { props.AddProperty(setting.Section, setting.Key, value); } } } if (!isDryRun) { props.Save(destinationFile); } }
static public void XPath(String sourceFile, String destinationFile, SettingFileType transformFile, List <SettingType> settings, bool isDryRun = false) { XmlDocument doc = new XmlDocument(); doc.XmlResolver = null; doc.Load(sourceFile); if (transformFile != null) { if (!String.IsNullOrWhiteSpace(transformFile.Value)) { String[] lines = File.ReadAllLines(transformFile.Value); foreach (String line in lines) { char[] delims = { ',' }; String[] values = line.Split(delims); String key = null; String value = null; if (values.Length >= 2) { key = values[0].Trim(); value = values[1].Trim(); } else { continue; } if (!String.IsNullOrWhiteSpace(key)) { if (key.StartsWith(@"""")) { key = key.Substring(1, key.Length - 2); } } if (!String.IsNullOrWhiteSpace(value)) { if (value.Trim().StartsWith(@"""")) { value = value.Substring(1, value.Length - 2); } } if (transformFile._HasEncryptedValues && !isDryRun) { Cipher cipher = new Cipher(config.Default.PassPhrase, config.Default.SaltValue, config.Default.InitVector); String newValue = cipher.Decrypt(value); if (!newValue.StartsWith("UNABLE TO DECRYPT")) { value = newValue; } } XmlNodeList nodes = doc.SelectNodes(key); foreach (XmlNode node in nodes) { node.InnerText = value; } } } } foreach (SettingType setting in settings) { String localValue = ""; if (setting.Value != null) { localValue = setting.Value.Value; if (setting.Value._IsEncrypted && !isDryRun) { Cipher cipher = new Cipher(config.Default.PassPhrase, config.Default.SaltValue, config.Default.InitVector); String newValue = cipher.Decrypt(localValue); if (!newValue.StartsWith("UNABLE TO DECRYPT")) { localValue = newValue; } } } if (setting.Key != null) { XmlNodeList nodes = doc.SelectNodes(setting.Key); foreach (XmlNode node in nodes) { node.InnerText = localValue; } } } if (!isDryRun) { doc.Save(destinationFile); } }