01: package org.acm.seguin.pmd.renderers;
02:
03: import org.acm.seguin.pmd.PMD;
04: import org.acm.seguin.pmd.Report;
05: import org.acm.seguin.pmd.RuleViolation;
06:
07: import java.util.HashSet;
08: import java.util.Iterator;
09: import java.util.Set;
10: import java.util.StringTokenizer;
11:
12: public class IDEAJRenderer implements Renderer {
13: private static class SourcePath {
14:
15: private Set paths = new HashSet();
16:
17: public SourcePath(String sourcePathString) {
18: for (StringTokenizer st = new StringTokenizer(
19: sourcePathString, System
20: .getProperty("path.separator")); st
21: .hasMoreTokens();) {
22: paths.add(st.nextToken());
23: }
24: }
25:
26: public String clipPath(String fullFilename) {
27: for (Iterator i = paths.iterator(); i.hasNext();) {
28: String path = (String) i.next();
29: if (fullFilename.startsWith(path)) {
30: return fullFilename.substring(path.length() + 1);
31: }
32: }
33: throw new RuntimeException("Couldn't find src path for "
34: + fullFilename);
35: }
36: }
37:
38: private String[] args;
39:
40: public IDEAJRenderer(String[] args) {
41: this .args = args;
42: }
43:
44: public String render(Report report) {
45: if (args[4].equals(".method")) {
46: // working on a directory tree
47: String sourcePath = args[3];
48: return render(report, sourcePath);
49: }
50: // working on one file
51: String classAndMethodName = args[4];
52: String singleFileName = args[5];
53: return render(report, classAndMethodName, singleFileName);
54: }
55:
56: private String render(Report report, String sourcePathString) {
57: SourcePath sourcePath = new SourcePath(sourcePathString);
58: StringBuffer buf = new StringBuffer();
59: for (Iterator i = report.iterator(); i.hasNext();) {
60: RuleViolation rv = (RuleViolation) i.next();
61: buf.append(rv.getDescription() + PMD.EOL);
62: buf.append(" at "
63: + getFullyQualifiedClassName(rv.getFilename(),
64: sourcePath) + ".method("
65: + getSimpleFileName(rv.getFilename()) + ":"
66: + rv.getLine() + ")" + PMD.EOL);
67: }
68: return buf.toString();
69: }
70:
71: private String render(Report report, String classAndMethod,
72: String file) {
73: StringBuffer buf = new StringBuffer();
74: for (Iterator i = report.iterator(); i.hasNext();) {
75: RuleViolation rv = (RuleViolation) i.next();
76: buf.append(rv.getDescription() + PMD.EOL);
77: buf.append(" at " + classAndMethod + "(" + file + ":"
78: + rv.getLine() + ")" + PMD.EOL);
79: }
80: return buf.toString();
81: }
82:
83: private String getFullyQualifiedClassName(String in,
84: SourcePath sourcePath) {
85: String classNameWithSlashes = sourcePath.clipPath(in);
86: String className = classNameWithSlashes.replace(System
87: .getProperty("file.separator").charAt(0), '.');
88: return className.substring(0, className.length() - 5);
89: }
90:
91: private String getSimpleFileName(String in) {
92: return in.substring(in.lastIndexOf(System
93: .getProperty("file.separator")) + 1);
94: }
95: }
|