View Javadoc

    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.PMD;
    7   import net.sourceforge.pmd.Report;
    8   import net.sourceforge.pmd.IRuleViolation;
    9
    10  import java.util.HashSet;
    11  import java.util.Iterator;
    12  import java.util.Set;
    13  import java.util.StringTokenizer;
    14
    15  public class IDEAJRenderer extends AbstractRenderer implements Renderer {
    16
    17      private static class SourcePath {
    18
    19          private Set paths = new HashSet();
    20
    21          public SourcePath(String sourcePathString) {
    22              for (StringTokenizer st = new StringTokenizer(sourcePathString, System.getProperty("path.separator")); st.hasMoreTokens();) {
    23                  paths.add(st.nextToken());
    24              }
    25          }
    26
    27          public String clipPath(String fullFilename) {
    28              for (Iterator i = paths.iterator(); i.hasNext();) {
    29                  String path = (String) i.next();
    30                  if (fullFilename.startsWith(path)) {
    31                      return fullFilename.substring(path.length() + 1);
    32                  }
    33              }
    34              throw new RuntimeException("Couldn't find src path for " + 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              IRuleViolation rv = (IRuleViolation) i.next();
    61              buf.append(rv.getDescription() + PMD.EOL);
    62              buf.append(" at " + getFullyQualifiedClassName(rv.getFilename(), sourcePath) + ".method(" + getSimpleFileName(rv.getFilename()) + ":" + rv.getBeginLine() + ")" + PMD.EOL);
    63          }
    64          return buf.toString();
    65      }
    66
    67      private String render(Report report, String classAndMethod, String file) {
    68          StringBuffer buf = new StringBuffer();
    69          for (Iterator i = report.iterator(); i.hasNext();) {
    70              IRuleViolation rv = (IRuleViolation) i.next();
    71              buf.append(rv.getDescription() + PMD.EOL);
    72              buf.append(" at " + classAndMethod + "(" + file + ":" + rv.getBeginLine() + ")" + PMD.EOL);
    73          }
    74          return buf.toString();
    75      }
    76
    77      private String getFullyQualifiedClassName(String in, SourcePath sourcePath) {
    78          String classNameWithSlashes = sourcePath.clipPath(in);
    79          String className = classNameWithSlashes.replace(System.getProperty("file.separator").charAt(0), '.');
    80          return className.substring(0, className.length() - 5);
    81      }
    82
    83      private String getSimpleFileName(String in) {
    84          return in.substring(in.lastIndexOf(System.getProperty("file.separator")) + 1);
    85      }
    86  }