Permalink
Browse files

Use XmlReader in TemplateDictionary. Read child xml as `property` val…

…ue when `value` attribute not present
  • Loading branch information...
Jordan Wallwork
Jordan Wallwork committed Nov 25, 2015
1 parent d18dbbb commit d3b88aca351a33da61ab708c64cf8f1db5589690
@@ -376,6 +376,12 @@ public void should_have_replaced_quote_with_quote_values()
{
Assert.AreEqual(@"""somevalue""", result["${settings.with.quote}"]);
}
[Observation]
public void should_read_property_xml()
{
Assert.AreEqual(@"<xmlParent><child num=""1"" /><child num=""2"" /></xmlParent>", result[@"${settings.with.xml}"]);
}
}
@@ -12,6 +12,13 @@ public class DevTokenSettings
<property name=""settings.no.separation"" value=""${server.files}${environment}"" />
<property name=""settings.with.nonexisting"" value=""${server.files}\${nonexistant.setting}"" />
<property name=""settings.with.quote"" value=""${quote}somevalue${quote}"" />
<property name=""settings.with.xml"">
<xmlParent>
<child num=""1"" />
<child num=""2"" />
</xmlParent>
</property>
</project>";
}
@@ -1,3 +1,6 @@
using System.IO;
using System.Xml;
namespace uppercut.template.builder
{
using System.Collections.Generic;
@@ -10,13 +13,27 @@ public sealed class TemplateDictionary
IDictionary<string, string> token_replacement_dictionary = new Dictionary<string, string>();
token_replacement_dictionary.Add("${quote}", "\"");
foreach (Match name_value_match in get_matches(settings_text))
using (var text = new StringReader(settings_text))
using (var reader = XmlReader.Create(text, new XmlReaderSettings { IgnoreWhitespace = true }))
{
string pair_key_text = get_name_value(name_value_match.ToString());
string pair_value_text = get_pair_value(name_value_match.ToString(), token_replacement_dictionary);
pair_key_text = @"${" + pair_key_text + @"}";
if (!token_replacement_dictionary.ContainsKey(pair_key_text))
token_replacement_dictionary.Add(pair_key_text, pair_value_text);
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
if (reader.Name == "property")
{
var name = reader["name"];
var value = reader["value"] ?? reader.ReadInnerXml();
if (!string.IsNullOrEmpty(value))
{
var replaced = replace_tokens_with_other_dictionary_values(value, token_replacement_dictionary);
token_replacement_dictionary.Add("${" + name + "}", replaced);
}
}
break;
}
}
}
return token_replacement_dictionary;
@@ -37,26 +54,6 @@ public static IList<Match> get_matches(string settings_text)
return matches;
}
public static string get_name_value(string input)
{
if (string.IsNullOrEmpty(input)) return string.Empty;
const string token_key_start = "name=\"";
const string token_key_end = "\"";
return get_token_value(input, token_key_start, token_key_end);
}
public static string get_pair_value(string input, IDictionary<string, string> tokens)
{
if (string.IsNullOrEmpty(input)) return string.Empty;
const string token_value_start = "value=\"";
const string token_value_end = "\"";
return replace_tokens_with_other_dictionary_values(get_token_value(input, token_value_start, token_value_end), tokens);
}
public static IList<Match> get_value_token_matches(string value_text)
{
IList<Match> matches = new List<Match>();

0 comments on commit d3b88ac

Please sign in to comment.