KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > pmd > renderers > IDEAJRenderer


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 JavaDoc;
11 import java.io.Writer JavaDoc;
12 import java.util.HashSet JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.Set JavaDoc;
15 import java.util.StringTokenizer JavaDoc;
16
17 public class IDEAJRenderer extends AbstractRenderer {
18
19     private static final String JavaDoc FILE_SEPARATOR = System.getProperty("file.separator");
20     private static final String JavaDoc PATH_SEPARATOR = System.getProperty("path.separator");
21     
22     private static class SourcePath {
23
24         private Set JavaDoc paths = new HashSet JavaDoc();
25
26         public SourcePath(String JavaDoc sourcePathString) {
27             for (StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(sourcePathString, PATH_SEPARATOR); st.hasMoreTokens();) {
28                 paths.add(st.nextToken());
29             }
30         }
31
32         public String JavaDoc clipPath(String JavaDoc fullFilename) {
33             for (Iterator JavaDoc i = paths.iterator(); i.hasNext();) {
34                 String JavaDoc path = (String JavaDoc) i.next();
35                 if (fullFilename.startsWith(path)) {
36                     return fullFilename.substring(path.length() + 1);
37                 }
38             }
39             throw new RuntimeException JavaDoc("Couldn't find src path for " + fullFilename);
40         }
41     }
42
43     private String JavaDoc[] args;
44
45     public IDEAJRenderer(String JavaDoc[] args) {
46         this.args = args;
47     }
48
49     public void render(Writer JavaDoc writer, Report report) throws IOException JavaDoc {
50         if (args[4].equals(".method")) {
51             // working on a directory tree
52
String JavaDoc sourcePath = args[3];
53             render(writer, report, sourcePath);
54             return;
55         }
56         // working on one file
57
String JavaDoc classAndMethodName = args[4];
58         String JavaDoc singleFileName = args[5];
59         render(writer, report, classAndMethodName, singleFileName);
60     }
61
62     private void render(Writer JavaDoc writer, Report report, String JavaDoc sourcePathString) throws IOException JavaDoc {
63         SourcePath sourcePath = new SourcePath(sourcePathString);
64         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
65         for (Iterator JavaDoc 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 JavaDoc writer, Report report, String JavaDoc classAndMethod, String JavaDoc file) throws IOException JavaDoc {
76         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
77         for (Iterator JavaDoc 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 JavaDoc getFullyQualifiedClassName(String JavaDoc in, SourcePath sourcePath) {
87         String JavaDoc classNameWithSlashes = sourcePath.clipPath(in);
88         String JavaDoc className = classNameWithSlashes.replace(FILE_SEPARATOR.charAt(0), '.');
89         return className.substring(0, className.length() - 5);
90     }
91
92     private String JavaDoc getSimpleFileName(String JavaDoc in) {
93         return in.substring(in.lastIndexOf(FILE_SEPARATOR) + 1);
94     }
95 }
96
Popular Tags