This repository has been archived by the owner on Feb 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathXmlExporter.cs
283 lines (216 loc) · 7 KB
/
XmlExporter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
using System;
using System.Collections;
using System.Text;
using System.IO;
using System.Xml;
namespace MonoCov {
public class XmlExporter {
public class ProgressEventArgs {
public CoverageItem item;
public string fileName;
public int pos;
public int itemCount;
public ProgressEventArgs (CoverageItem item, string fileName,
int pos, int itemCount) {
this.item = item;
this.fileName = fileName;
this.pos = pos;
this.itemCount = itemCount;
}
}
public delegate void ProgressEventHandler (Object sender,
ProgressEventArgs e);
public string DestinationDir;
public string StyleSheet;
public event ProgressEventHandler Progress;
private XmlTextWriter writer;
private CoverageModel model;
private static string DefaultStyleSheet = "style.xsl";
private int itemCount;
private int itemsProcessed;
public void Export (CoverageModel model) {
this.model = model;
if (model.hit + model.missed == 0)
return;
if (StyleSheet == null) {
// Use default stylesheet
using (StreamReader sr = new StreamReader (typeof (XmlExporter).Assembly.GetManifestResourceStream ("style.xsl"))) {
using (StreamWriter sw = new StreamWriter (Path.Combine (DestinationDir, "style.xsl"))) {
string line;
while ((line = sr.ReadLine ()) != null)
sw.WriteLine (line);
}
}
using (Stream s = typeof (XmlExporter).Assembly.GetManifestResourceStream ("trans.gif")) {
using (FileStream fs = new FileStream (Path.Combine (DestinationDir, "trans.gif"), FileMode.Create)) {
byte[] buf = new byte[1024];
int len = s.Read (buf, 0, buf.Length);
fs.Write (buf, 0, len);
}
}
StyleSheet = DefaultStyleSheet;
}
// Count items
itemCount = 1 + model.Classes.Count + model.Namespaces.Count;
itemsProcessed = 0;
WriteProject ();
WriteNamespaces ();
WriteClasses ();
}
private void WriteStyleSheet () {
// The standard says text/xml, while IE6 only understands text/xsl
writer.WriteProcessingInstruction ("xml-stylesheet", "href=\"" + StyleSheet + "\" type=\"text/xsl\"");
}
private void WriteProject () {
string fileName = Path.Combine (DestinationDir, "project.xml");
// If I use Encoding.UTF8 here, the file will start with strange
// characters
writer = new XmlTextWriter (fileName, Encoding.ASCII);
writer.Formatting = Formatting.Indented;
writer.WriteStartDocument ();
WriteStyleSheet ();
WriteItem (model, typeof (ClassCoverageItem), 999);
writer.WriteEndDocument ();
writer.WriteRaw ("\n");
writer.Close ();
itemsProcessed ++;
if (Progress != null)
Progress (this, new ProgressEventArgs (model, fileName, itemsProcessed, itemCount));
}
private void WriteItem (CoverageItem item, Type stopLevel, int level) {
if (item.filtered)
return;
if (item.hit + item.missed == 0)
// Filtered
return;
if (level == 0)
return;
if (item.GetType () == stopLevel)
return;
if (item is CoverageModel) {
writer.WriteStartElement ("project");
writer.WriteAttributeString ("name", "Project");
}
else
if (item is NamespaceCoverageItem) {
NamespaceCoverageItem ns = (NamespaceCoverageItem)item;
writer.WriteStartElement ("namespace");
if (ns.ns == "<GLOBAL>")
writer.WriteAttributeString ("name", "GLOBAL");
else
writer.WriteAttributeString ("name", ns.ns);
}
else
if (item is ClassCoverageItem) {
ClassCoverageItem klass = (ClassCoverageItem)item;
writer.WriteStartElement ("class");
writer.WriteAttributeString ("name", klass.name);
writer.WriteAttributeString ("fullname", klass.FullName.Replace('/', '.'));
}
WriteCoverage (item);
if (item.ChildCount > 0)
foreach (CoverageItem child in item.children)
WriteItem (child, stopLevel, level - 1);
writer.WriteEndElement ();
}
private void WriteNamespaces () {
foreach (NamespaceCoverageItem ns in model.Namespaces.Values) {
bool filtered = false;
string fileSuffix = ns.ns;
if (ns.ns == "<GLOBAL>")
fileSuffix = "GLOBAL";
string fileName =
Path.Combine (DestinationDir, String.Format ("namespace-{0}.xml", fileSuffix));
if (ns.hit + ns.missed == 0)
// Filtered
filtered = true;
if (!filtered) {
writer = new XmlTextWriter (fileName, Encoding.ASCII);
writer.Formatting = Formatting.Indented;
writer.WriteStartDocument ();
WriteStyleSheet ();
WriteItem (ns, typeof (MethodCoverageItem), 2);
writer.WriteEndDocument ();
writer.WriteRaw ("\n");
writer.Close ();
}
else
fileName = null;
itemsProcessed ++;
if (Progress != null)
Progress (this, new ProgressEventArgs (ns, fileName, itemsProcessed, itemCount));
}
}
private void WriteClasses () {
foreach (ClassCoverageItem item in model.Classes.Values) {
bool filtered = false;
string fileName = Path.Combine (DestinationDir, String.Format ("class-{0}.xml", item.FullName.Replace('/', '.')));
if (item.filtered)
filtered = true;
if (item.hit + item.missed == 0)
// Filtered
filtered = true;
if (!filtered) {
writer = new XmlTextWriter (fileName, Encoding.ASCII);
writer.Formatting = Formatting.Indented;
writer.WriteStartDocument ();
WriteStyleSheet ();
WriteClass (item);
writer.WriteEndDocument ();
writer.WriteRaw ("\n");
writer.Close ();
}
else
fileName = null;
itemsProcessed ++;
if (Progress != null)
Progress (this, new ProgressEventArgs (item, fileName, itemsProcessed, itemCount));
}
}
private void WriteClass (ClassCoverageItem item) {
if (item.filtered)
return;
writer.WriteStartElement ("class");
writer.WriteAttributeString ("name", item.name);
writer.WriteAttributeString ("fullname", item.FullName.Replace('/', '.'));
writer.WriteAttributeString ("namespace", item.Namespace);
WriteCoverage (item);
writer.WriteStartElement ("source");
if (item.sourceFile != null) {
writer.WriteAttributeString ("sourceFile", item.sourceFile.sourceFile);
StreamReader infile = new StreamReader (item.sourceFile.sourceFile, Encoding.ASCII);
int[] coverage = item.sourceFile.Coverage;
int pos = 1;
while (infile.Peek () > -1) {
int count;
if ((coverage != null) && (pos < coverage.Length))
count = coverage [pos];
else
count = -1;
writer.WriteStartElement ("l");
writer.WriteAttributeString ("line", "" + pos);
writer.WriteAttributeString ("count", "" + count);
string line = infile.ReadLine ();
writer.WriteString (line);
writer.WriteEndElement ();
pos ++;
}
}
writer.WriteEndElement ();
}
private void WriteCoverage (CoverageItem item) {
double coverage;
if (item.hit + item.missed == 0)
coverage = 1.0;
else
coverage = (double)item.hit / (item.hit + item.missed);
string coveragePercent
= String.Format ("{0:###0}", coverage * 100);
writer.WriteStartElement ("coverage");
writer.WriteAttributeString ("hit", item.hit.ToString ());
writer.WriteAttributeString ("missed", item.missed.ToString ());
writer.WriteAttributeString ("coverage", coveragePercent);
writer.WriteEndElement ();
}
}
}