-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ac5a58c
commit 81e3032
Showing
6 changed files
with
176 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/main/java/se/bjurr/violations/lib/parsers/StyleCopParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package se.bjurr.violations.lib.parsers; | ||
|
||
import static com.google.common.collect.Lists.newArrayList; | ||
import static javax.xml.stream.XMLStreamConstants.START_ELEMENT; | ||
import static se.bjurr.violations.lib.model.SEVERITY.INFO; | ||
import static se.bjurr.violations.lib.model.Violation.violationBuilder; | ||
import static se.bjurr.violations.lib.parsers.ViolationParserUtils.getAttribute; | ||
import static se.bjurr.violations.lib.parsers.ViolationParserUtils.getIntegerAttribute; | ||
import static se.bjurr.violations.lib.reports.Reporter.STYLECOP; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.InputStream; | ||
import java.util.List; | ||
|
||
import javax.xml.stream.XMLInputFactory; | ||
import javax.xml.stream.XMLStreamReader; | ||
|
||
import se.bjurr.violations.lib.model.SEVERITY; | ||
import se.bjurr.violations.lib.model.Violation; | ||
|
||
public class StyleCopParser implements ViolationsParser { | ||
|
||
@Override | ||
public List<Violation> parseFile(File file) throws Exception { | ||
List<Violation> violations = newArrayList(); | ||
|
||
try (InputStream input = new FileInputStream(file)) { | ||
|
||
XMLInputFactory factory = XMLInputFactory.newInstance(); | ||
XMLStreamReader xmlr = factory.createXMLStreamReader(input); | ||
|
||
while (xmlr.hasNext()) { | ||
int eventType = xmlr.next(); | ||
if (eventType == START_ELEMENT) { | ||
if (xmlr.getLocalName().equals("Violation")) { | ||
String section = getAttribute(xmlr, "Section"); | ||
String source = getAttribute(xmlr, "Source"); | ||
String ruleNamespace = getAttribute(xmlr, "RuleNamespace"); | ||
String rule = getAttribute(xmlr, "Rule"); | ||
String ruleId = getAttribute(xmlr, "RuleId"); | ||
Integer lineNumber = getIntegerAttribute(xmlr, "LineNumber"); | ||
String message = xmlr.getElementText().replaceAll("\\s+", " "); | ||
SEVERITY severity = INFO; | ||
String filename = toFile(source); | ||
violations.add(// | ||
violationBuilder()// | ||
.setReporter(STYLECOP)// | ||
.setMessage(message)// | ||
.setFile(filename)// | ||
.setStartLine(lineNumber)// | ||
.setRule(rule)// | ||
.setSeverity(severity)// | ||
.setSource(source)// | ||
.setSpecific("section", section)// | ||
.setSpecific("source", source)// | ||
.setSpecific("ruleNamespace", ruleNamespace)// | ||
.setSpecific("rule", rule)// | ||
.setSpecific("ruleId", ruleId)// | ||
.build()// | ||
); | ||
} | ||
} | ||
} | ||
} | ||
return violations; | ||
} | ||
|
||
private String toFile(String source) { | ||
return source.replaceAll("\\.", "/"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package se.bjurr.violations.lib; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static se.bjurr.violations.lib.TestUtils.getRootFolder; | ||
import static se.bjurr.violations.lib.ViolationsReporterApi.violationsReporterApi; | ||
import static se.bjurr.violations.lib.model.SEVERITY.INFO; | ||
import static se.bjurr.violations.lib.reports.Reporter.STYLECOP; | ||
|
||
import java.util.List; | ||
|
||
import org.junit.Test; | ||
|
||
import se.bjurr.violations.lib.model.Violation; | ||
|
||
public class StyleCopTest { | ||
|
||
@Test | ||
public void testThatViolationsCanBeParsed() { | ||
String rootFolder = getRootFolder(); | ||
|
||
List<Violation> actual = violationsReporterApi() // | ||
.withPattern(".*/stylecop/.*\\.xml$") // | ||
.inFolder(rootFolder) // | ||
.findAll(STYLECOP) // | ||
.violations(); | ||
|
||
assertThat(actual)// | ||
.hasSize(2); | ||
|
||
Violation actualViolationZero = actual.get(0); | ||
assertThat(actualViolationZero.getFile())// | ||
.isEqualTo("Form1/Designer/cs"); | ||
assertThat(actualViolationZero.getStartLine())// | ||
.isEqualTo(18); | ||
assertThat(actualViolationZero.getMessage())// | ||
.startsWith("The call to"); | ||
assertThat(actualViolationZero.getReporter())// | ||
.isEqualTo(STYLECOP); | ||
assertThat(actualViolationZero.getRule().orNull())// | ||
.isEqualTo("PrefixLocalCallsWithThis"); | ||
assertThat(actualViolationZero.getSeverity())// | ||
.isEqualTo(INFO); | ||
assertThat(actualViolationZero.getSource().orNull())// | ||
.isEqualTo("Form1.Designer.cs"); | ||
|
||
Violation actualViolationOne = actual.get(1); | ||
assertThat(actualViolationOne.getFile())// | ||
.isEqualTo("Form1/Designer/cs"); | ||
assertThat(actualViolationOne.getStartLine())// | ||
.isEqualTo(16); | ||
assertThat(actualViolationOne.getMessage())// | ||
.startsWith("The call to"); | ||
assertThat(actualViolationOne.getReporter())// | ||
.isEqualTo(STYLECOP); | ||
assertThat(actualViolationOne.getRule().orNull())// | ||
.isEqualTo("PrefixLocalCallsWithThis"); | ||
assertThat(actualViolationOne.getSeverity())// | ||
.isEqualTo(INFO); | ||
assertThat(actualViolationOne.getSource().orNull())// | ||
.isEqualTo("Form1.Designer.cs"); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<StyleCopViolations> | ||
<Violation Section="Root.ListViewProblem.Form1.Dispose%bool" | ||
LineNumber="18" Source="Form1.Designer.cs" RuleNamespace="Microsoft.StyleCop.CSharp.ReadabilityRules" | ||
Rule="PrefixLocalCallsWithThis" RuleId="SA1101">The call to components must | ||
begin with the 'this.' prefix to indicate that the item is a member of | ||
the class.</Violation> | ||
<Violation Section="Root.ListViewProblem.Form1.Dispose%bool" | ||
LineNumber="16" Source="Form1.Designer.cs" RuleNamespace="Microsoft.StyleCop.CSharp.ReadabilityRules" | ||
Rule="PrefixLocalCallsWithThis" RuleId="SA1101">The call to components must | ||
begin with the 'this.' prefix to indicate that the item is a member of | ||
the class.</Violation> | ||
</StyleCopViolations> |