Skip to content

Commit

Permalink
Added namespace support to the XmlPoke task
Browse files Browse the repository at this point in the history
  • Loading branch information
steinjak committed Mar 8, 2012
1 parent 2e5fad5 commit 030dd87
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns="http://example.com/schemas/test">
<connectionStrings>
<add name="bob" connectionString="Data Source=(local);Initial Catalog=bob;Integrated Security=True;Pooling=false;Connection Timeout=600" providerName="System.Data.SqlClient" />
<add name="nancy" connectionString="Data Source=(local);Initial Catalog=Nancy;Integrated Security=True;Pooling=false;Connection Timeout=600" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
8 changes: 8 additions & 0 deletions product/dropkick.tests/Tasks/Xml/Context/XmlContextResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,13 @@ public static class XmlContextResult
</connectionStrings>
</configuration>";

public static string ResultWithNamespace = @"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration xmlns=""http://example.com/schemas/test"">
<connectionStrings>
<add name=""bob"" connectionString=""bob"" providerName=""System.Data.SqlClient"" />
<add name=""nancy"" connectionString=""Data Source=(local);Initial Catalog=Nancy;Integrated Security=True;Pooling=false;Connection Timeout=600"" providerName=""System.Data.SqlClient"" />
</connectionStrings>
</configuration>";

}
}
38 changes: 27 additions & 11 deletions product/dropkick.tests/Tasks/Xml/XmlPokeTaskSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,17 @@ public abstract class XmlPokeTaskSpecsBase : TinySpec
protected DeploymentResult result;
protected XmlPokeTask task;
protected string file_path = @".\Tasks\Xml\Context\Test.xml";
protected string file_path_with_namespace = @".\Tasks\Xml\Context\TestWithNamespace.xml";

public override void Context()
public override void Because()
{
result = new DeploymentResult();
result = task.Execute();
}

[Fact]
public void should_execute_successfully()
{
System.Console.WriteLine(result.ToString());
}
}

Expand All @@ -35,26 +42,35 @@ public override void Context()
task = new XmlPokeTask(file_path, replacement_items, new DotNetPath());
}

public override void Because()
[Fact]
public void should_have_updated_the_file()
{
result = task.Execute();
string updated_text = File.ReadAllText(file_path);
updated_text.ShouldBeEqualTo(XmlContextResult.Result);
}
}

[Fact]
public void should_execute_successfully()

[ConcernFor("Xml")]
[Category("Integration")]
public class when_updating_the_settings_in_an_xml_file_with_namespaces : XmlPokeTaskSpecsBase
{
protected IDictionary<string, string> replacement_items = new Dictionary<string, string>();
protected IDictionary<string, string> namespace_prefixes = new Dictionary<string, string> {{"test", "http://example.com/schemas/test"}};

public override void Context()
{
System.Console.WriteLine(result.ToString());
replacement_items.Add("//test:configuration/test:connectionStrings/test:add[@name='bob']/@connectionString", "bob");
task = new XmlPokeTask(file_path_with_namespace, replacement_items, new DotNetPath(), namespace_prefixes);
}

[Fact]
public void should_have_updated_the_file()
{
string updated_text = File.ReadAllText(file_path);
updated_text.ShouldBeEqualTo(XmlContextResult.Result);
string updated_text = File.ReadAllText(file_path_with_namespace);
updated_text.ShouldBeEqualTo(XmlContextResult.ResultWithNamespace);
}
}


}

}
3 changes: 3 additions & 0 deletions product/dropkick.tests/dropkick.tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@
<Content Include="Tasks\MsSql\test.sql">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Tasks\Xml\Context\TestWithNamespace.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Tasks\Xml\Context\Test.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
Expand Down
10 changes: 10 additions & 0 deletions product/dropkick/Configuration/Dsl/Xml/Extension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.

using System.Collections.Generic;

namespace dropkick.Configuration.Dsl.Xml
{
public static class Extension
Expand All @@ -20,5 +23,12 @@ public static XmlPokeOptions XmlPoke(this ProtoServer protoServer, string filePa
protoServer.RegisterProtoTask(proto);
return proto;
}

public static XmlPokeOptions XmlPoke(this ProtoServer protoServer, string filePath, IDictionary<string, string> namespacePrefixes)
{
var proto = new ProtoXmlPokeTask(filePath, namespacePrefixes);
protoServer.RegisterProtoTask(proto);
return proto;
}
}
}
9 changes: 8 additions & 1 deletion product/dropkick/Configuration/Dsl/Xml/ProtoXmlPokeTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,18 @@ public class ProtoXmlPokeTask :
XmlPokeOptions
{
private readonly string _filePath;
private IDictionary<string, string> _namespacePrefixes;
private IDictionary<string, string> _items = new Dictionary<string, string>();

public ProtoXmlPokeTask(string filePath)
: this(filePath, null)
{
}

public ProtoXmlPokeTask(string filePath, IDictionary<string, string> namespacePrefixes)
{
_filePath = ReplaceTokens(filePath);
_namespacePrefixes = namespacePrefixes ?? new Dictionary<string, string>();
}

public XmlPokeOptions Set(string xPath, string value)
Expand All @@ -50,7 +57,7 @@ public override void RegisterRealTasks(PhysicalServer site)
{
string filePath = site.MapPath(_filePath);

var o = new XmlPokeTask(filePath, _items, new DotNetPath());
var o = new XmlPokeTask(filePath, _items, new DotNetPath(), _namespacePrefixes);
site.AddTask(o);
}
}
Expand Down
24 changes: 18 additions & 6 deletions product/dropkick/Tasks/Xml/XmlPokeTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,19 @@ public class XmlPokeTask :

private string _filePath;
private readonly IDictionary<string, string> _replacementItems;
private readonly IDictionary<string, string> _namespacePrefixes;

public XmlPokeTask(string filePath, IDictionary<string, string> replacementItems, Path path)
: this(filePath, replacementItems, path, new Dictionary<string, string>())
{
}

public XmlPokeTask(string filePath, IDictionary<string, string> replacementItems, Path path, IDictionary<string, string> namespacePrefixes)
: base(path)
{
_filePath = filePath;
_replacementItems = replacementItems;
_namespacePrefixes = namespacePrefixes;
}

public override string Name
Expand All @@ -58,13 +65,13 @@ public override DeploymentResult Execute()
_filePath = _path.GetFullPath(_filePath);
ValidateIsFile(result, _filePath);

UpdateXmlFile(result, _filePath, _replacementItems);
UpdateXmlFile(result, _filePath, _replacementItems, _namespacePrefixes);

result.AddGood(Name);
return result;
}

private void UpdateXmlFile(DeploymentResult result, string filePath, IDictionary<string, string> replacementItems)
private void UpdateXmlFile(DeploymentResult result, string filePath, IDictionary<string, string> replacementItems, IDictionary<string, string> namespacePrefixes)
{
//XmlTextReader xmlReader = new XmlTextReader(_filePath);
//XPathDocument xmlDoc = new XPathDocument(xmlReader);
Expand All @@ -74,21 +81,26 @@ private void UpdateXmlFile(DeploymentResult result, string filePath, IDictionary

XmlDocument document = new XmlDocument();
document.Load(filePath);
XPathNavigator xpathNavigator = document.CreateNavigator();
var nsManager = new XmlNamespaceManager(document.NameTable);
foreach (var prefix in namespacePrefixes)
{
nsManager.AddNamespace(prefix.Key, prefix.Value);
}

XPathNavigator xpathNavigator = document.CreateNavigator();
foreach (KeyValuePair<string, string> item in replacementItems.OrEmptyListIfNull())
{
UpdateValueInFile(result, xpathNavigator, item.Key, item.Value);
UpdateValueInFile(result, xpathNavigator, item.Key, item.Value, nsManager);
}

LogFileChange("[xmlpoke] Completed changes to '{0}'.",filePath);

document.Save(filePath);
}

private void UpdateValueInFile(DeploymentResult result, XPathNavigator xpathNavigator, string xPath, string value)
private void UpdateValueInFile(DeploymentResult result, XPathNavigator xpathNavigator, string xPath, string value, XmlNamespaceManager nsManager)
{
foreach (XPathNavigator navigator in xpathNavigator.Select(xPath))
foreach (XPathNavigator navigator in xpathNavigator.Select(xPath, nsManager))
{
string original = navigator.Value;
navigator.SetValue(value);
Expand Down

0 comments on commit 030dd87

Please sign in to comment.