Skip to content

Commit

Permalink
Using a Supplier for loading FileModel
Browse files Browse the repository at this point in the history
* Avoids NPE in FileModelProxy.java line 475 #71
  • Loading branch information
tomasbjerre committed Aug 14, 2015
1 parent b811511 commit 56722bc
Showing 1 changed file with 32 additions and 28 deletions.
60 changes: 32 additions & 28 deletions src/main/java/hudson/plugins/violations/render/FileModelProxy.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hudson.plugins.violations.render;

import static com.google.common.collect.Lists.newArrayList;
import static java.util.logging.Level.WARNING;
import hudson.Functions;
import hudson.model.AbstractBuild;
import hudson.plugins.violations.generate.XMLUtil;
Expand All @@ -16,9 +17,10 @@
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.google.common.base.Supplier;

/**
* A proxy class for FileModel used to allow lazy loading of FileModel. This
* class is also used to render the FileModel.
Expand All @@ -27,7 +29,24 @@ public class FileModelProxy {
private static final Logger LOG = Logger.getLogger(FileModelProxy.class.getName());

private final File xmlFile;
private FileModel fileModel;
private final Supplier<FileModel> fileModel = new Supplier<FileModel>() {
@Override
public FileModel get() {
if (!xmlFile.exists()) {
LOG.log(WARNING, "The file " + xmlFile + " does not exist");
return null;
}
try {
FileModel t = new FileModel();
ParseXML.parse(xmlFile, new FileModelParser().fileModel(t));
return t;
} catch (Exception ex) {
LOG.log(WARNING, "Unable to parse " + xmlFile, ex);
return null;
}

}
};
private String contextPath;
private AbstractBuild<?, ?> build;

Expand Down Expand Up @@ -83,7 +102,7 @@ public FileModelProxy contextPath(String contextPath) {
* number of suppressed violations.
*/
public String typeLine(String type) {
FileModel.LimitType l = fileModel.getLimitTypeMap().get(type);
FileModel.LimitType l = fileModel.get().getLimitTypeMap().get(type);
StringBuilder b = new StringBuilder();
if (l == null) {
return type + " ?number?";
Expand Down Expand Up @@ -137,23 +156,23 @@ public List<BlockData> getFileContent() {
int startLine = 0;
int currentLine = -1;

for (Map.Entry<Integer, String> e : fileModel.getLines().entrySet()) {
for (Map.Entry<Integer, String> e : fileModel.get().getLines().entrySet()) {
currentLine = e.getKey();
String line = e.getValue();
// Check if at start of block
if (currentLine != (previousLine + 1)) {
// Start of block
// Check if need to write previous block
if (b.length() > 0) {
blockDataList.add(new BlockData(startLine, previousLine, b.toString(), new File(fileModel
blockDataList.add(new BlockData(startLine, previousLine, b.toString(), new File(fileModel.get()
.getDisplayName()).getName()));
}
b = new StringBuilder();
startLine = currentLine;
}
previousLine = currentLine;

Set<Violation> v = fileModel.getLineViolationMap().get(currentLine);
Set<Violation> v = fileModel.get().getLineViolationMap().get(currentLine);

// TR
b.append("<tr " + (v != null ? "class='violation'" : "") + ">");
Expand Down Expand Up @@ -195,15 +214,15 @@ public List<BlockData> getFileContent() {
b.append("</tr>\n");
}
if (b.length() > 0) {
blockDataList.add(new BlockData(startLine, previousLine, b.toString(), new File(fileModel.getDisplayName())
.getName()));
blockDataList.add(new BlockData(startLine, previousLine, b.toString(), new File(fileModel.get()
.getDisplayName()).getName()));
}
return blockDataList;
}

public String getVisualStudioLink(Violation v) {
StringBuilder ret = new StringBuilder();
String uriBase = "devenv:?file=" + this.fileModel.getDisplayName();
String uriBase = "devenv:?file=" + this.fileModel.get().getDisplayName();

ret.append("<a href=\"");
String uri = String.valueOf(uriBase + "&line=" + v.getLine());
Expand All @@ -215,7 +234,7 @@ public String getVisualStudioLink(Violation v) {
public String getViolationsSummary() {
StringBuilder ret = new StringBuilder();

for (Entry<String, TreeSet<Violation>> t : this.fileModel.getTypeMap().entrySet()) {
for (Entry<String, TreeSet<Violation>> t : this.fileModel.get().getTypeMap().entrySet()) {
ret.append("<table class=\"pane\">");
ret.append("<tbody>");
ret.append("<tr><td class=\"pane-header\" colspan=\"5\">");
Expand Down Expand Up @@ -270,22 +289,7 @@ public String getViolationsSummary() {
* @return the file model or null if unable to parse.
*/
public FileModel getFileModel() {
if (fileModel != null) {
return fileModel;
}
if (!xmlFile.exists()) {
LOG.log(Level.WARNING, "The file " + xmlFile + " does not exist");
return null;
}
try {
FileModel t = new FileModel();
ParseXML.parse(xmlFile, new FileModelParser().fileModel(t));
fileModel = t;
return fileModel;
} catch (Exception ex) {
LOG.log(Level.WARNING, "Unable to parse " + xmlFile, ex);
return null;
}
return this.fileModel.get();
}

private String getSeverityIcon(int level) {
Expand Down Expand Up @@ -410,7 +414,7 @@ private void showDiv(StringBuilder b, Set<Violation> violations) {
}

public String getFileNameAlt() {
return new File(fileModel.getDisplayName()).getName();
return new File(fileModel.get().getDisplayName()).getName();
}

public String getSummaryTable() {
Expand All @@ -427,7 +431,7 @@ public String getSummaryTable() {
gst.append(" <td class='violations-header'> Description</td>\n");
gst.append(" </tr>\n");

Set<Violation> violations = fileModel.getLineViolationMap().get(0);
Set<Violation> violations = fileModel.get().getLineViolationMap().get(0);

for (Violation v : violations) {
++count;
Expand Down

0 comments on commit 56722bc

Please sign in to comment.