Skip to content

Commit

Permalink
Getter for XmlNodeList objects
Browse files Browse the repository at this point in the history
- XmlNodeListGetter to pull from lists by index, similar to IList getter
- XmlNodeListGetterFactory to create above
- Tests for above.
  • Loading branch information
Martin Zarate authored and Romanx committed May 19, 2016
1 parent 1fd3f4e commit 31fab63
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
39 changes: 39 additions & 0 deletions Nustache.Core.Tests/Describe_ValueGetter.cs
Expand Up @@ -153,6 +153,45 @@ public void It_gets_XmlNode_multiple_child_element_values_as_a_list()
Assert.AreEqual("text2", elements[1].InnerText);
}

[Test]
public void It_gets_single_string_values_as_string_by_Index_from_XmlNodeList()
{
XmlDocument target = new XmlDocument();
target.LoadXml(@"<doc attr='val'>
<child>text1</child>
<child>text2</child>
<child>text3</child>
</doc>");
var value = (string)ValueGetter.GetValue(target.DocumentElement.ChildNodes, "1");
Assert.AreEqual("text2", value);
}

[Test]
public void It_gets_single_node_values_as_node_by_Index_from_XmlNodeList()
{
XmlDocument target = new XmlDocument();
target.LoadXml(@"<doc attr='val'>
<child>text1</child>
<child><grandchild>text2</grandchild></child>
<child>text3</child>
</doc>");
var value = (XmlNode)ValueGetter.GetValue(target.DocumentElement.ChildNodes, "1");
Assert.AreEqual("<grandchild>text2</grandchild>", value.InnerXml);
}

[Test]
public void It_gets_single_CDATA_values_as_string_by_Index_from_XmlNodeList()
{
XmlDocument target = new XmlDocument();
target.LoadXml(@"<doc attr='val'>
<child><![CDATA[text1]]></child>
<child><![CDATA[text2]]></child>
<child><![CDATA[text3]]></child>
</doc>");
var value = (string)ValueGetter.GetValue(target.DocumentElement.ChildNodes, "1");
Assert.AreEqual("text2", value);
}

[Test]
public void It_gets_ListValueByIndex_values_from_array()
{
Expand Down
32 changes: 32 additions & 0 deletions Nustache.Core/ValueGetter.cs
Expand Up @@ -122,6 +122,38 @@ private bool TryGetStringByAttributeName(string attributeName)
}
}

internal class XmlNodeListIndexGetter : ValueGetter
{
private readonly XmlNodeList _target;
private readonly int _index;
private object _foundSingleValue;

public XmlNodeListIndexGetter(XmlNodeList target, int index)
{
_target = target;
_index = index;
}

private object GetNodeValue(XmlNode node)
{
if (node.ChildNodes.Count == 1
&& (node.ChildNodes[0].NodeType == XmlNodeType.Text || node.ChildNodes[0].NodeType == XmlNodeType.CDATA)
)
{
return node.ChildNodes[0].Value;
}
else
{
return node;
}
}

public override object GetValue()
{
return GetNodeValue(_target[_index]);
}
}

internal class PropertyDescriptorValueGetter : ValueGetter
{
private readonly object _target;
Expand Down
27 changes: 27 additions & 0 deletions Nustache.Core/ValueGetterFactory.cs
Expand Up @@ -82,6 +82,7 @@ public static class ValueGetterFactories
private static readonly ValueGetterFactoryCollection _factories = new ValueGetterFactoryCollection
{
new XmlNodeValueGetterFactory(),
new XmlNodeListIndexGetterFactory(),
new PropertyDescriptorValueGetterFactory(),
new GenericDictionaryValueGetterFactory(),
new DataRowGetterFactory(),
Expand Down Expand Up @@ -112,6 +113,32 @@ public override ValueGetter GetValueGetter(object target, Type targetType, strin
}
}

internal class XmlNodeListIndexGetterFactory : ValueGetterFactory
{
public override ValueGetter GetValueGetter(object target, Type targetType, string name)
{
if (target is XmlNodeList)
{
var listTarget = target as XmlNodeList;
int arrayIndex;
bool parseSuccess = Int32.TryParse(name, out arrayIndex);

/*
* There is an index as per the success of the parse, it is not greater than the count
* (minus one since index is zero referenced) or less than zero.
*/
if (parseSuccess &&
!(arrayIndex > (listTarget.Count - 1)) &&
!(arrayIndex < 0))
{
return new XmlNodeListIndexGetter(listTarget, arrayIndex);
}
}

return null;
}
}

internal class PropertyDescriptorValueGetterFactory : ValueGetterFactory
{
public override ValueGetter GetValueGetter(object target, Type targetType, string name)
Expand Down

0 comments on commit 31fab63

Please sign in to comment.