Skip to content

Commit

Permalink
made XmlReporter output threadsafe (#389)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexHaxe authored Apr 22, 2018
1 parent 8c4326b commit c63f714
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- Added CHANGES.md
- Added a reset function for checks ([#279](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/279))
- Added unittest for [#78](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/78)
- Fixed XMLReporter output after introducing multithreading in 2.2.0
- Updated formula for number of pre-parsed files [#386](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/386)
- Removed conditional section for unittest [#181](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/181)

Expand Down
22 changes: 16 additions & 6 deletions src/checkstyle/reporter/XMLReporter.hx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ class XMLReporter extends BaseReporter {

var style:String;

var messageCache:Map<String, Array<CheckMessage>>;

/*
* Solution from mustache.js
* https://github.com/janl/mustache.js/blob/master/mustache.js#L49
Expand All @@ -21,6 +23,7 @@ class XMLReporter extends BaseReporter {

public function new(numFiles:Int, checkCount:Int, usedCheckCount:Int, path:String, s:String, ns:Bool) {
super(numFiles, checkCount, usedCheckCount, path, ns);
messageCache = new Map<String, Array<CheckMessage>>();
style = s;
}

Expand Down Expand Up @@ -49,15 +52,17 @@ class XMLReporter extends BaseReporter {
}

override public function fileStart(f:CheckFile) {
var sb = new StringBuf();
sb.add("\t<file name=\"");
sb.add(encode(f.name));
sb.add("\">\n");
if (file != null) report.add(sb.toString());
messageCache.set(f.name, []);
}

override public function fileFinish(f:CheckFile) {
var sb = new StringBuf();
sb.add("\t<file name=\"");
sb.add(encode(f.name));
sb.add("\">\n");
var messages:Array<CheckMessage> = messageCache.get(f.name);
for (m in messages) sb.add(formatMessage(m));
messageCache.remove(f.name);
sb.add("\t</file>\n");
if (file != null) report.add(sb.toString());
}
Expand All @@ -73,6 +78,11 @@ class XMLReporter extends BaseReporter {
}

override public function addMessage(m:CheckMessage) {
if (!messageCache.exists(m.fileName)) messageCache.set(m.fileName, [m]);
else messageCache.get(m.fileName).push(m);
}

function formatMessage(m:CheckMessage):String {
var sb:StringBuf = new StringBuf();

sb.add("\t\t<error line=\"");
Expand Down Expand Up @@ -102,6 +112,6 @@ class XMLReporter extends BaseReporter {

Sys.print(applyColour(getMessage(m).toString(), m.severity));

if (file != null) report.add(sb.toString());
return sb.toString();
}
}

0 comments on commit c63f714

Please sign in to comment.