Skip to content

Commit

Permalink
Excaped message and filename
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Jul 4, 2018
1 parent 990f4d6 commit 0431394
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 16 deletions.
24 changes: 24 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@
Changelog of Violations lib.

## Unreleased
### No issue

**Issue template**


[990f4d6da1000b6](https://github.com/tomasbjerre/violations-lib/commit/990f4d6da1000b6) Tomas Bjerre *2018-07-03 14:12:54*

**Updating docs on PyLint**


[4016805bf281f1f](https://github.com/tomasbjerre/violations-lib/commit/4016805bf281f1f) Tomas Bjerre *2018-05-03 17:08:10*


## 1.55
### GitHub [#33](https://github.com/tomasbjerre/violations-lib/issues/33) Add support for NullAway

**NullAway**
Expand All @@ -22,6 +36,16 @@ Changelog of Violations lib.

### No issue

**Avoiding Optional in model**


[a2c03571ee99246](https://github.com/tomasbjerre/violations-lib/commit/a2c03571ee99246) Tomas Bjerre *2018-05-01 04:58:35*

**TSLint**


[9df7684f4fba7e8](https://github.com/tomasbjerre/violations-lib/commit/9df7684f4fba7e8) Tomas Bjerre *2018-04-20 10:06:18*

**Adding another PMD test**


Expand Down
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/bin/bash
./gradlew clean gitChangelogTask build -i --refresh-dependencies -Dhttp.socketTimeout=60000 -Dhttp.connectionTimeout=60000
./gradlew clean gitChangelogTask build -i -Dhttp.socketTimeout=60000 -Dhttp.connectionTimeout=60000
15 changes: 15 additions & 0 deletions src/main/java/se/bjurr/violations/lib/model/Violation.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package se.bjurr.violations.lib.model;

import static se.bjurr.violations.lib.util.StringUtils.escapeHTML;
import static se.bjurr.violations.lib.util.Utils.checkNotNull;
import static se.bjurr.violations.lib.util.Utils.emptyToNull;
import static se.bjurr.violations.lib.util.Utils.firstNonNull;
Expand Down Expand Up @@ -107,6 +108,7 @@ public static ViolationBuilder violationBuilder() {
private final Integer endLine;
private final String file;
private final String message;
private final String messageEscaped;
/** The algorithm, the format, used. */
private final Parser parser;
/**
Expand All @@ -120,12 +122,14 @@ public static ViolationBuilder violationBuilder() {
private final String source;
private final Map<String, String> specifics;
private final Integer startLine;
private String fileName;

public Violation() {
startLine = null;
endLine = null;
severity = null;
message = null;
messageEscaped = null;
file = null;
source = null;
rule = null;
Expand All @@ -146,7 +150,10 @@ public Violation(final ViolationBuilder vb) {
column = vb.column;
severity = checkNotNull(vb.severity, "severity");
message = checkNotNull(emptyToNull(vb.message), "message");
messageEscaped = escapeHTML(message);
file = checkNotNull(emptyToNull(vb.file), "file").replaceAll("\\\\", "/");
final String[] fileParts = file.split("\\/");
fileName = fileParts[fileParts.length - 1];
source = emptyToNull(vb.source);
rule = emptyToNull(vb.rule);
specifics = vb.specifics;
Expand Down Expand Up @@ -248,10 +255,18 @@ public String getFile() {
return file;
}

public String getFileName() {
return fileName;
}

public String getMessage() {
return message;
}

public String getMessageEscaped() {
return messageEscaped;
}

public Parser getParser() {
return parser;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import java.util.ArrayList;
import java.util.List;

import se.bjurr.violations.lib.model.SEVERITY;
import se.bjurr.violations.lib.model.Violation;

Expand Down
15 changes: 15 additions & 0 deletions src/main/java/se/bjurr/violations/lib/util/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,19 @@ public static String xmlDecode(final String input) {
}
return result.toString();
}

public static String escapeHTML(final String s) {
final StringBuilder out = new StringBuilder(Math.max(16, s.length()));
for (int i = 0; i < s.length(); i++) {
final char c = s.charAt(i);
if (c > 127 || c == '"' || c == '<' || c == '>' || c == '&') {
out.append("&#");
out.append((int) c);
out.append(';');
} else {
out.append(c);
}
}
return out.toString();
}
}
52 changes: 38 additions & 14 deletions src/test/java/se/bjurr/violations/lib/model/ViolationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,42 @@

public class ViolationTest {

@Test
public void testThatFilePathsAreAlwaysFronSlashes() {
assertThat( //
violationBuilder() //
.setParser(CHECKSTYLE) //
.setFile("c:\\path\\to\\file.xml") //
.setMessage("message") //
.setSeverity(ERROR) //
.setStartLine(1) //
.build() //
.getFile() //
) //
.isEqualTo("c:/path/to/file.xml");
}
@Test
public void testThatFilePathsAreAlwaysFronSlashes() {
final Violation violation = violationBuilder() //
.setParser(CHECKSTYLE) //
.setFile("c:\\path\\to\\file.xml") //
.setMessage("message") //
.setSeverity(ERROR) //
.setStartLine(1) //
.build();
assertThat(violation.getFile()) //
.isEqualTo("c:/path/to/file.xml");
}

@Test
public void testThatFileNameCanBeExtracted() {
final Violation violation = violationBuilder() //
.setParser(CHECKSTYLE) //
.setFile("c:\\path\\to\\file1.xml") //
.setMessage("message") //
.setSeverity(ERROR) //
.setStartLine(1) //
.build();
assertThat(violation.getFileName()) //
.isEqualTo("file1.xml");
}

@Test
public void testThatFileNameCanBeExtractedWhenNoSlashes() {
final Violation violation = violationBuilder() //
.setParser(CHECKSTYLE) //
.setFile("file2.xml") //
.setMessage("message") //
.setSeverity(ERROR) //
.setStartLine(1) //
.build();
assertThat(violation.getFileName()) //
.isEqualTo("file2.xml");
}
}

0 comments on commit 0431394

Please sign in to comment.