~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/Analysis/CodeCoverage/Project/Src/PartCoverSettings.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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)
 
3
 
 
4
using System;
 
5
using System.Collections.Specialized;
 
6
using System.IO;
 
7
using System.Text;
 
8
using System.Xml;
 
9
 
 
10
using ICSharpCode.SharpDevelop.Project;
 
11
 
 
12
namespace ICSharpCode.CodeCoverage
 
13
{
 
14
        /// <summary>
 
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.
 
19
        /// </summary>
 
20
        public class PartCoverSettings
 
21
        {
 
22
                static readonly string RootElementName = "PartCoverSettings";
 
23
                static readonly string RuleElementName = "Rule";
 
24
                StringCollection include = new StringCollection();
 
25
                StringCollection exclude = new StringCollection();
 
26
 
 
27
                public PartCoverSettings()
 
28
                {
 
29
                }
 
30
                
 
31
                public PartCoverSettings(string fileName) 
 
32
                        : this(new StreamReader(fileName, true))
 
33
                {
 
34
                }
 
35
                
 
36
                public PartCoverSettings(XmlReader reader)
 
37
                {
 
38
                        ReadSettings(reader);
 
39
                }
 
40
                
 
41
                public PartCoverSettings(TextReader reader) 
 
42
                        : this(new XmlTextReader(reader))
 
43
                {
 
44
                }
 
45
                
 
46
                /// <summary>
 
47
                /// Gets the NCover settings filename for the specified project.
 
48
                /// </summary>
 
49
                public static string GetFileName(IProject project)
 
50
                {
 
51
                        return Path.ChangeExtension(project.FileName, "PartCover.Settings");
 
52
                }
 
53
                
 
54
                /// <summary>
 
55
                /// Gets the list of include regular expressions.
 
56
                /// </summary>
 
57
                public StringCollection Include {
 
58
                        get { return include; }
 
59
                }
 
60
 
 
61
                /// <summary>
 
62
                /// Gets the list of exclude regular expressions.
 
63
                /// </summary>
 
64
                public StringCollection Exclude {
 
65
                        get { return exclude; }
 
66
                }
 
67
        
 
68
                /// <summary>
 
69
                /// Writes the PartCover settings to the specified text writer.
 
70
                /// </summary>
 
71
                public void Save(TextWriter writer)
 
72
                {
 
73
                        Save(new XmlTextWriter(writer));
 
74
                }
 
75
                
 
76
                /// <summary>
 
77
                /// Saves the PartCover settings to the specified file.
 
78
                /// </summary>
 
79
                public void Save(string fileName)
 
80
                {
 
81
                        Save(new StreamWriter(fileName, false, Encoding.UTF8));
 
82
                }
 
83
                
 
84
                /// <summary>
 
85
                /// Writes the PartCover settings to the specified XmlTextWriter.
 
86
                /// </summary>
 
87
                public void Save(XmlTextWriter writer)
 
88
                {
 
89
                        writer.Formatting = Formatting.Indented;
 
90
 
 
91
                        using (writer) {
 
92
                                writer.WriteStartElement(RootElementName);
 
93
                                WriteRuleElements(writer, "+", include);
 
94
                                WriteRuleElements(writer, "-", exclude);
 
95
                                writer.WriteEndElement();
 
96
                        }
 
97
                }
 
98
                
 
99
                /// <summary>
 
100
                /// Reads the include and exclude regular expressions from the
 
101
                /// PartCover settings xml.
 
102
                /// </summary>
 
103
                void ReadSettings(XmlReader reader)
 
104
                {
 
105
                        using (reader) {
 
106
                                while (reader.Read()) {
 
107
                                        if (reader.NodeType == XmlNodeType.Element) {
 
108
                                                if (reader.Name == RuleElementName) {
 
109
                                                        AddRule(reader.ReadString());
 
110
                                                } 
 
111
                                        }
 
112
                                }
 
113
                        }
 
114
                }               
 
115
                
 
116
                /// <summary>
 
117
                /// Writes the Rule elements to the writer. Each item in the collection will
 
118
                /// have be prefixed with the specified prefix string.
 
119
                /// </summary>
 
120
                void WriteRuleElements(XmlWriter writer, string prefix, StringCollection rules)
 
121
                {
 
122
                        foreach (string rule in rules) {
 
123
                                writer.WriteElementString(RuleElementName, prefix + rule);
 
124
                        }
 
125
                }
 
126
                
 
127
                /// <summary>
 
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.
 
130
                /// </summary>
 
131
                void AddRule(string rule)
 
132
                {
 
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));
 
139
                                }
 
140
                        }
 
141
                }
 
142
        }
 
143
}