Skip to content
This repository was archived by the owner on Feb 12, 2025. It is now read-only.

Commit 4ab8bcf

Browse files
obonessavornicesei
authored andcommitted
IConfigurationSectionHandler is deprecated, a section handler should inherit from ConfigurationSection and implement the hiearchy via collections and elements
1 parent 988b042 commit 4ab8bcf

3 files changed

Lines changed: 81 additions & 26 deletions

File tree

project/UnitTests/Core/Config/XslFilesSectionHandlerTest.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Collections;
22
using System.Configuration;
33
using NUnit.Framework;
4+
using ThoughtWorks.CruiseControl.Core.Config;
45

56
namespace ThoughtWorks.CruiseControl.UnitTests.Core.Config
67
{
@@ -10,8 +11,10 @@ public class XslFilesSectionHandlerTest : CustomAssertion
1011
[Test]
1112
public void GetConfig()
1213
{
13-
IList list = (IList) ConfigurationManager.GetSection("xslFiles");
14-
Assert.IsNotNull(list);
14+
var section = ConfigurationManager.GetSection("xslFiles") as XslFilesSectionHandler;
15+
Assert.IsNotNull(section, "section should not be null");
16+
IList list = section.FileNames;
17+
Assert.IsNotNull(list, "file names list should not be null");
1518
Assert.AreEqual(5, list.Count);
1619
Assert.AreEqual(@"xsl\header.xsl", list[0]);
1720
Assert.AreEqual(@"xsl\compile.xsl", list[1]);
Lines changed: 73 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
using System;
12
using System.Collections;
3+
using System.Collections.Generic;
24
using System.Configuration;
35
using System.Xml;
46

@@ -7,29 +9,78 @@ namespace ThoughtWorks.CruiseControl.Core.Config
79
/// <summary>
810
///
911
/// </summary>
10-
public class XslFilesSectionHandler : IConfigurationSectionHandler
12+
public class XslFilesSectionHandler : ConfigurationSection
1113
{
12-
/// <summary>
13-
/// Creates the specified parent.
14-
/// </summary>
15-
/// <param name="parent">The parent.</param>
16-
/// <param name="configContext">The config context.</param>
17-
/// <param name="section">The section.</param>
18-
/// <returns></returns>
19-
/// <remarks></remarks>
20-
public object Create(object parent, object configContext, XmlNode section)
21-
{
22-
ArrayList files = new ArrayList();
14+
[ConfigurationProperty("", IsDefaultCollection = true)]
15+
[ConfigurationCollection(typeof(XslFilesCollection), AddItemName = "file")]
16+
public XslFilesCollection XslFiles
17+
{
18+
get
19+
{
20+
return (XslFilesCollection)base[""];
21+
}
22+
}
23+
24+
public List<string> FileNames
25+
{
26+
get
27+
{
28+
var result = new List<string>();
29+
foreach (var file in XslFiles)
30+
result.Add(((XslFileElement)file).Name);
31+
32+
return result;
33+
}
34+
}
35+
}
2336

24-
foreach (XmlNode node in section.ChildNodes)
25-
{
26-
if (node.NodeType == System.Xml.XmlNodeType.Element)
27-
{
28-
files.Add(node.Attributes["name"].Value);
29-
}
30-
}
37+
public class XslFilesCollection : ConfigurationElementCollection
38+
{
39+
protected override ConfigurationElement CreateNewElement()
40+
{
41+
return new XslFileElement();
42+
}
43+
44+
protected override Object GetElementKey(ConfigurationElement element)
45+
{
46+
return ((XslFileElement)element).Name;
47+
}
48+
49+
public XslFileElement this[int index]
50+
{
51+
get
52+
{
53+
return (XslFileElement)BaseGet(index);
54+
}
55+
set
56+
{
57+
if (BaseGet(index) != null)
58+
BaseRemoveAt(index);
3159

32-
return files;
33-
}
34-
}
60+
BaseAdd(index, value);
61+
}
62+
}
63+
64+
protected override void BaseAdd(ConfigurationElement element)
65+
{
66+
BaseAdd(element, false);
67+
}
68+
}
69+
70+
public class XslFileElement : ConfigurationElement
71+
{
72+
[ConfigurationProperty("name", IsRequired = true, IsKey = true)]
73+
public string Name
74+
{
75+
get
76+
{
77+
return (string)this["name"];
78+
}
79+
set
80+
{
81+
this["name"] = value;
82+
}
83+
}
84+
}
3585
}
86+

project/core/publishers/BuildLogTransformer.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Configuration;
33
using System.Text;
44
using System.Xml.XPath;
5+
using ThoughtWorks.CruiseControl.Core.Config;
56
using ThoughtWorks.CruiseControl.Core.Util;
67

78
namespace ThoughtWorks.CruiseControl.Core.Publishers
@@ -20,8 +21,8 @@ public class BuildLogTransformer
2021
/// <returns></returns>
2122
public string TransformResultsWithAllStyleSheets(XPathDocument document)
2223
{
23-
IList list = (IList) ConfigurationManager.GetSection("xslFiles");
24-
return TransformResults(list, document);
24+
var section = ConfigurationManager.GetSection("xslFiles") as XslFilesSectionHandler;
25+
return TransformResults(section?.FileNames, document);
2526
}
2627

2728
/// <summary>

0 commit comments

Comments
 (0)