001: /**
002: * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
003: */package net.sourceforge.pmd.renderers;
004:
005: import net.sourceforge.pmd.IRuleViolation;
006: import net.sourceforge.pmd.PMD;
007:
008: import java.io.IOException;
009: import java.io.Writer;
010: import java.util.HashSet;
011: import java.util.Iterator;
012: import java.util.Set;
013: import java.util.StringTokenizer;
014:
015: public class IDEAJRenderer extends OnTheFlyRenderer {
016:
017: private static final String FILE_SEPARATOR = System
018: .getProperty("file.separator");
019: private static final String PATH_SEPARATOR = System
020: .getProperty("path.separator");
021:
022: private static class SourcePath {
023:
024: private Set<String> paths = new HashSet<String>();
025:
026: public SourcePath(String sourcePathString) {
027: for (StringTokenizer st = new StringTokenizer(
028: sourcePathString, PATH_SEPARATOR); st
029: .hasMoreTokens();) {
030: paths.add(st.nextToken());
031: }
032: }
033:
034: public String clipPath(String fullFilename) {
035: for (String path : paths) {
036: if (fullFilename.startsWith(path)) {
037: return fullFilename.substring(path.length() + 1);
038: }
039: }
040: throw new RuntimeException("Couldn't find src path for "
041: + fullFilename);
042: }
043: }
044:
045: private String[] args;
046:
047: public IDEAJRenderer(String[] args) {
048: this .args = args;
049: }
050:
051: public void start() throws IOException {
052: }
053:
054: public void renderFileViolations(Iterator<IRuleViolation> violations)
055: throws IOException {
056: Writer writer = getWriter();
057: if (args[4].equals(".method")) {
058: // working on a directory tree
059: String sourcePath = args[3];
060: render(writer, violations, sourcePath);
061: return;
062: }
063: // working on one file
064: String classAndMethodName = args[4];
065: String singleFileName = args[5];
066: render(writer, violations, classAndMethodName, singleFileName);
067: }
068:
069: public void end() throws IOException {
070: }
071:
072: private void render(Writer writer,
073: Iterator<IRuleViolation> violations, String sourcePathString)
074: throws IOException {
075: SourcePath sourcePath = new SourcePath(sourcePathString);
076: StringBuffer buf = new StringBuffer();
077: while (violations.hasNext()) {
078: buf.setLength(0);
079: IRuleViolation rv = violations.next();
080: buf.append(rv.getDescription() + PMD.EOL);
081: buf.append(" at ").append(
082: getFullyQualifiedClassName(rv.getFilename(),
083: sourcePath)).append(".method(");
084: buf.append(getSimpleFileName(rv.getFilename())).append(':')
085: .append(rv.getBeginLine()).append(')').append(
086: PMD.EOL);
087: writer.write(buf.toString());
088: }
089: }
090:
091: private void render(Writer writer,
092: Iterator<IRuleViolation> violations, String classAndMethod,
093: String file) throws IOException {
094: StringBuffer buf = new StringBuffer();
095: while (violations.hasNext()) {
096: buf.setLength(0);
097: IRuleViolation rv = violations.next();
098: buf.append(rv.getDescription()).append(PMD.EOL);
099: buf.append(" at ").append(classAndMethod).append('(')
100: .append(file).append(':').append(rv.getBeginLine())
101: .append(')').append(PMD.EOL);
102: writer.write(buf.toString());
103: }
104: }
105:
106: private String getFullyQualifiedClassName(String in,
107: SourcePath sourcePath) {
108: String classNameWithSlashes = sourcePath.clipPath(in);
109: String className = classNameWithSlashes.replace(FILE_SEPARATOR
110: .charAt(0), '.');
111: return className.substring(0, className.length() - 5);
112: }
113:
114: private String getSimpleFileName(String in) {
115: return in.substring(in.lastIndexOf(FILE_SEPARATOR) + 1);
116: }
117: }
|