1 /***
2 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3 */
4 package net.sourceforge.pmd.renderers;
5
6 import net.sourceforge.pmd.IRuleViolation;
7 import net.sourceforge.pmd.PMD;
8 import net.sourceforge.pmd.Report;
9
10 import java.io.IOException;
11 import java.io.Writer;
12 import java.util.HashSet;
13 import java.util.Iterator;
14 import java.util.Set;
15 import java.util.StringTokenizer;
16
17 public class IDEAJRenderer extends AbstractRenderer {
18
19 private static final String FILE_SEPARATOR = System.getProperty("file.separator");
20 private static final String PATH_SEPARATOR = System.getProperty("path.separator");
21
22 private static class SourcePath {
23
24 private Set paths = new HashSet();
25
26 public SourcePath(String sourcePathString) {
27 for (StringTokenizer st = new StringTokenizer(sourcePathString, PATH_SEPARATOR); st.hasMoreTokens();) {
28 paths.add(st.nextToken());
29 }
30 }
31
32 public String clipPath(String fullFilename) {
33 for (Iterator i = paths.iterator(); i.hasNext();) {
34 String path = (String) i.next();
35 if (fullFilename.startsWith(path)) {
36 return fullFilename.substring(path.length() + 1);
37 }
38 }
39 throw new RuntimeException("Couldn't find src path for " + fullFilename);
40 }
41 }
42
43 private String[] args;
44
45 public IDEAJRenderer(String[] args) {
46 this.args = args;
47 }
48
49 public void render(Writer writer, Report report) throws IOException {
50 if (args[4].equals(".method")) {
51
52 String sourcePath = args[3];
53 render(writer, report, sourcePath);
54 return;
55 }
56
57 String classAndMethodName = args[4];
58 String singleFileName = args[5];
59 render(writer, report, classAndMethodName, singleFileName);
60 }
61
62 private void render(Writer writer, Report report, String sourcePathString) throws IOException {
63 SourcePath sourcePath = new SourcePath(sourcePathString);
64 StringBuffer buf = new StringBuffer();
65 for (Iterator i = report.iterator(); i.hasNext();) {
66 buf.setLength(0);
67 IRuleViolation rv = (IRuleViolation) i.next();
68 buf.append(rv.getDescription() + PMD.EOL);
69 buf.append(" at ").append(getFullyQualifiedClassName(rv.getFilename(), sourcePath)).append(".method(");
70 buf.append(getSimpleFileName(rv.getFilename())).append(':').append(rv.getBeginLine()).append(')').append(PMD.EOL);
71 writer.write(buf.toString());
72 }
73 }
74
75 private void render(Writer writer, Report report, String classAndMethod, String file) throws IOException {
76 StringBuffer buf = new StringBuffer();
77 for (Iterator i = report.iterator(); i.hasNext();) {
78 buf.setLength(0);
79 IRuleViolation rv = (IRuleViolation) i.next();
80 buf.append(rv.getDescription()).append(PMD.EOL);
81 buf.append(" at ").append(classAndMethod).append('(').append(file).append(':').append(rv.getBeginLine()).append(')').append(PMD.EOL);
82 writer.write(buf.toString());
83 }
84 }
85
86 private String getFullyQualifiedClassName(String in, SourcePath sourcePath) {
87 String classNameWithSlashes = sourcePath.clipPath(in);
88 String className = classNameWithSlashes.replace(FILE_SEPARATOR.charAt(0), '.');
89 return className.substring(0, className.length() - 5);
90 }
91
92 private String getSimpleFileName(String in) {
93 return in.substring(in.lastIndexOf(FILE_SEPARATOR) + 1);
94 }
95 }