PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/Trunk/WebConfigModifier/WebConfigUpdater.cs

#
C# | 130 lines | 116 code | 14 blank | 0 comment | 13 complexity | 49405dd93b57d35a16501105fc7c4fd2 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml.Linq;
  6. using System.IO;
  7. using System.Xml;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. namespace WebConfigModifier
  13. {
  14. public class WebConfigUpdater
  15. {
  16. public void HandleRequest(Stack<string> argStack, string value)
  17. {
  18. try
  19. {
  20. if (argStack == null) throw new ArgumentNullException("argStack");
  21. if (argStack.Count < 3) throw new ArgumentException();
  22. if (value == null) throw new ArgumentNullException("value");
  23. string webConfigFileName = argStack.Pop();
  24. string action = argStack.Pop();
  25. string xPathExpression = argStack.Pop();
  26. UpdateWebConfig(argStack, value, webConfigFileName, action, xPathExpression);
  27. }
  28. catch (ArgumentException exc)
  29. {
  30. Console.WriteLine(exc.Message);
  31. Console.WriteLine();
  32. DisplayUsage();
  33. throw new UsageException("Incorrect usage.", exc);
  34. }
  35. catch (UsageException exc)
  36. {
  37. Console.WriteLine(exc.Message);
  38. Console.WriteLine();
  39. DisplayUsage();
  40. throw;
  41. }
  42. catch (Exception exc)
  43. {
  44. Console.WriteLine(exc.Message);
  45. throw;
  46. }
  47. }
  48. private static void UpdateWebConfig(Stack<string> argStack, string value, string webConfigFileName, string action, string xPathExpression)
  49. {
  50. XDocument doc = LoadWebConfig(webConfigFileName);
  51. DocumentModifier modifier = new DocumentModifier(doc);
  52. if (action == "/element")
  53. {
  54. modifier.ReplaceElementValue(xPathExpression, value);
  55. }
  56. else if (action == "/attribute")
  57. {
  58. if (!argStack.Any()) throw new UsageException();
  59. string attributeName = argStack.Pop();
  60. modifier.ReplaceAttributeValue(xPathExpression, attributeName, value);
  61. }
  62. else
  63. {
  64. throw new UsageException(string.Format("Unknown action '{0}'", action));
  65. }
  66. SaveWebConfig(webConfigFileName, doc);
  67. }
  68. private static XDocument LoadWebConfig(string webConfigFileName)
  69. {
  70. string xml = File.ReadAllText(webConfigFileName);
  71. XDocument doc = XDocument.Parse(xml);
  72. return doc;
  73. }
  74. private static void SaveWebConfig(string webConfigFileName, XDocument doc)
  75. {
  76. XmlWriterSettings settings = new XmlWriterSettings
  77. {
  78. Indent = true,
  79. NewLineChars = Environment.NewLine,
  80. NewLineHandling = System.Xml.NewLineHandling.Replace,
  81. NewLineOnAttributes = false,
  82. };
  83. using (var writer = XmlWriter.Create(webConfigFileName, settings))
  84. {
  85. doc.WriteTo(writer);
  86. }
  87. }
  88. private static bool ParseCommandLineArguments(string[] args, out string webConfigFilename, out string action, out string key)
  89. {
  90. webConfigFilename = null;
  91. action = null;
  92. key = null;
  93. if (args.Length != 3) return false;
  94. webConfigFilename = args[0];
  95. action = args[1];
  96. key = args[2];
  97. return true;
  98. }
  99. private static void DisplayUsage()
  100. {
  101. Console.WriteLine("usage:");
  102. Console.WriteLine(" echo <value> | WebConfigModifier.exe <path to web.config> /element <xPathToElement>");
  103. Console.WriteLine(" type <filename> | WebConfigModifier.exe <path to web.config> /element <xPathToElement>");
  104. Console.WriteLine(" echo <value> | WebConfigModifier.exe <path to web.config> /attribute <xPathToElement> <attributeName>");
  105. Console.WriteLine();
  106. Console.WriteLine("e.g.:");
  107. Console.WriteLine();
  108. Console.WriteLine(" To replace individual connection strings, replace the providerName and connectionString attributes:");
  109. Console.WriteLine(" echo System.Data.SqlClient | WebConfigModifier.exe Samples\\Mvc2.Vanilla.Web.config /attribute /configuration/connectionStrings/add[@name='ApplicationServices'] providerName");
  110. Console.WriteLine(" echo \"data source=.\\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|different_aspnetdb.mdf;User Instance=true\" | WebConfigModifier.exe Samples\\Mvc2.Vanilla.Web.config /attribute /configuration/connectionStrings/add[@name='ApplicationServices'] connectionString");
  111. Console.WriteLine();
  112. Console.WriteLine(" To replace the entire connectionStrings document element from a file:");
  113. Console.WriteLine(" type Samples\\connectionStrings.xml | WebConfigModifier.exe Samples\\Mvc2.Vanilla.Web.config /element /configuration/connectionStrings");
  114. }
  115. }
  116. }