1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
5
using System.Collections.Specialized;
10
using ICSharpCode.SharpDevelop.Project;
12
namespace ICSharpCode.CodeCoverage
15
/// File that stores PartCover settings. This file has the same format as
16
/// PartCover requires, but is actually just used by the Code Coverage addin
17
/// as a place to store the include and exclude regular expressions that the
18
/// user may set up on a per project basis.
20
public class PartCoverSettings
22
static readonly string RootElementName = "PartCoverSettings";
23
static readonly string RuleElementName = "Rule";
24
StringCollection include = new StringCollection();
25
StringCollection exclude = new StringCollection();
27
public PartCoverSettings()
31
public PartCoverSettings(string fileName)
32
: this(new StreamReader(fileName, true))
36
public PartCoverSettings(XmlReader reader)
41
public PartCoverSettings(TextReader reader)
42
: this(new XmlTextReader(reader))
47
/// Gets the NCover settings filename for the specified project.
49
public static string GetFileName(IProject project)
51
return Path.ChangeExtension(project.FileName, "PartCover.Settings");
55
/// Gets the list of include regular expressions.
57
public StringCollection Include {
58
get { return include; }
62
/// Gets the list of exclude regular expressions.
64
public StringCollection Exclude {
65
get { return exclude; }
69
/// Writes the PartCover settings to the specified text writer.
71
public void Save(TextWriter writer)
73
Save(new XmlTextWriter(writer));
77
/// Saves the PartCover settings to the specified file.
79
public void Save(string fileName)
81
Save(new StreamWriter(fileName, false, Encoding.UTF8));
85
/// Writes the PartCover settings to the specified XmlTextWriter.
87
public void Save(XmlTextWriter writer)
89
writer.Formatting = Formatting.Indented;
92
writer.WriteStartElement(RootElementName);
93
WriteRuleElements(writer, "+", include);
94
WriteRuleElements(writer, "-", exclude);
95
writer.WriteEndElement();
100
/// Reads the include and exclude regular expressions from the
101
/// PartCover settings xml.
103
void ReadSettings(XmlReader reader)
106
while (reader.Read()) {
107
if (reader.NodeType == XmlNodeType.Element) {
108
if (reader.Name == RuleElementName) {
109
AddRule(reader.ReadString());
117
/// Writes the Rule elements to the writer. Each item in the collection will
118
/// have be prefixed with the specified prefix string.
120
void WriteRuleElements(XmlWriter writer, string prefix, StringCollection rules)
122
foreach (string rule in rules) {
123
writer.WriteElementString(RuleElementName, prefix + rule);
128
/// Adds an include or exclude regular expression. The rule starts with
129
/// a '+' if it is an include. It starts with a '-' if it is an exclude.
131
void AddRule(string rule)
133
if (rule.Length > 0) {
134
char firstCharacter = rule[0];
135
if (firstCharacter == '+') {
136
include.Add(rule.Substring(1));
137
} else if (firstCharacter == '-') {
138
exclude.Add(rule.Substring(1));