Skip to content

Commit 754541b

Browse files
committed
#56 Added better escape method 2
1 parent 9a8069b commit 754541b

File tree

2 files changed

+36
-38
lines changed

2 files changed

+36
-38
lines changed

src/main/scala/scoverage/IOUtils.scala

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,37 @@ object IOUtils {
3030
override def accept(pathname: File): Boolean = pathname.getName.startsWith(MeasurementsPrefix)
3131
})
3232

33+
/**
34+
* This method ensures that the output String has only
35+
* valid XML unicode characters as specified by the
36+
* XML 1.0 standard. For reference, please see
37+
* <a href="http://www.w3.org/TR/2000/REC-xml-20001006#NT-Char">the
38+
* standard</a>. This method will return an empty
39+
* String if the input is null or empty.
40+
*
41+
* @param in The String whose non-valid characters we want to remove.
42+
* @return The in String, stripped of non-valid characters.
43+
* @see http://blog.mark-mclaren.info/2007/02/invalid-xml-characters-when-valid-utf8_5873.html
44+
*
45+
*/
46+
def escape(in: String): String = {
47+
val out = new StringBuilder()
48+
for ( current <- Option(in).getOrElse("").toCharArray ) {
49+
if ((current == 0x9) || (current == 0xA) || (current == 0xD) ||
50+
((current >= 0x20) && (current <= 0xD7FF)) ||
51+
((current >= 0xE000) && (current <= 0xFFFD)) ||
52+
((current >= 0x10000) && (current <= 0x10FFFF)))
53+
out.append(current)
54+
}
55+
out.mkString
56+
}
57+
3358
// loads all the invoked statement ids from the given files
3459
def invoked(files: Seq[File]): Set[Int] = {
3560
val acc = mutable.Set[Int]()
3661
files.foreach { file =>
3762
val reader = Source.fromFile(file)
38-
for (line <- reader.getLines()) {
63+
for ( line <- reader.getLines() ) {
3964
if (!line.isEmpty) {
4065
acc += line.toInt
4166
}
@@ -86,13 +111,13 @@ object IOUtils {
86111
{stmt.line.toString}
87112
</line>
88113
<description>
89-
{stmt.desc}
114+
{escape(stmt.desc)}
90115
</description>
91116
<symbolName>
92-
{stmt.symbolName}
117+
{escape(stmt.symbolName)}
93118
</symbolName>
94119
<treeName>
95-
{stmt.treeName}
120+
{escape(stmt.treeName)}
96121
</treeName>
97122
<branch>
98123
{stmt.branch.toString}

src/main/scala/scoverage/report/ScoverageXmlWriter.scala

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package scoverage.report
22

3-
import scala.xml.{Utility, PrettyPrinter, Node}
43
import java.io.File
4+
5+
import scala.xml.{Node, PrettyPrinter}
6+
57
import org.apache.commons.io.FileUtils
8+
69
import scoverage._
7-
import scoverage.MeasuredStatement
8-
import scoverage.MeasuredClass
9-
import scoverage.MeasuredMethod
1010

1111
/** @author Stephen Samuel */
1212
class ScoverageXmlWriter(sourceDir: File, outputDir: File, debug: Boolean) {
@@ -19,31 +19,6 @@ class ScoverageXmlWriter(sourceDir: File, outputDir: File, debug: Boolean) {
1919
FileUtils.write(file, new PrettyPrinter(120, 4).format(xml(coverage)))
2020
}
2121

22-
/**
23-
* This method ensures that the output String has only
24-
* valid XML unicode characters as specified by the
25-
* XML 1.0 standard. For reference, please see
26-
* <a href="http://www.w3.org/TR/2000/REC-xml-20001006#NT-Char">the
27-
* standard</a>. This method will return an empty
28-
* String if the input is null or empty.
29-
*
30-
* @param in The String whose non-valid characters we want to remove.
31-
* @return The in String, stripped of non-valid characters.
32-
* @see http://blog.mark-mclaren.info/2007/02/invalid-xml-characters-when-valid-utf8_5873.html
33-
*
34-
*/
35-
private def escape(in: String): String = {
36-
val out = new StringBuilder()
37-
for ( current <- Option(in).getOrElse("").toCharArray ) {
38-
if ((current == 0x9) || (current == 0xA) || (current == 0xD) ||
39-
((current >= 0x20) && (current <= 0xD7FF)) ||
40-
((current >= 0xE000) && (current <= 0xFFFD)) ||
41-
((current >= 0x10000) && (current <= 0x10FFFF)))
42-
out.append(current)
43-
}
44-
out.mkString
45-
}
46-
4722
def statement(stmt: MeasuredStatement): Node = {
4823
debug match {
4924
case true =>
@@ -52,20 +27,18 @@ class ScoverageXmlWriter(sourceDir: File, outputDir: File, debug: Boolean) {
5227
method={stmt.location.method}
5328
start={stmt.start.toString}
5429
line={stmt.line.toString}
55-
symbol={escape(stmt.symbolName)}
56-
tree={stmt.treeName}
30+
symbol={IOUtils.escape(stmt.symbolName)}
31+
tree={IOUtils.escape(stmt.treeName)}
5732
branch={stmt.branch.toString}
5833
invocation-count={stmt.count.toString}>
59-
{escape(stmt.desc)}
34+
{IOUtils.escape(stmt.desc)}
6035
</statement>
6136
case false =>
6237
<statement package={stmt.location._package}
6338
class={stmt.location._class}
6439
method={stmt.location.method}
6540
start={stmt.start.toString}
6641
line={stmt.line.toString}
67-
symbol={escape(stmt.symbolName)}
68-
tree={stmt.treeName}
6942
branch={stmt.branch.toString}
7043
invocation-count={stmt.count.toString}/>
7144
}

0 commit comments

Comments
 (0)