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