001: package org.acm.seguin.pmd.swingui;
002:
003: import org.acm.seguin.pmd.Report;
004: import org.acm.seguin.pmd.Rule;
005: import org.acm.seguin.pmd.RuleViolation;
006:
007: import java.io.StringWriter;
008: import java.util.Iterator;
009:
010: /**
011: *
012: * Converts the violations list into a text string for viewing.
013: *
014: * @author Donald A. Leckie
015: * @since August 17, 2002
016: * @version $Revision: 1.1 $, $Date: 2003/07/29 20:51:59 $
017: */
018: class TextRenderer {
019:
020: private boolean m_reportNoViolations;
021: private StringWriter m_writer;
022:
023: /**
024: *******************************************************************************
025: *
026: */
027: protected void beginRendering(boolean reportNoViolations) {
028: m_reportNoViolations = reportNoViolations;
029: m_writer = new StringWriter(25000);
030: }
031:
032: /**
033: *******************************************************************************
034: *
035: * @return Results text.
036: */
037: protected String endRendering() {
038: return m_writer.toString();
039: }
040:
041: /**
042: *******************************************************************************
043: *
044: * @param report
045: */
046: public void render(String fileName, Report report) {
047: Iterator violations = report.iterator();
048:
049: if (violations.hasNext() == false) {
050: if (m_reportNoViolations) {
051: m_writer.write("Source File: ");
052: m_writer.write(fileName);
053: m_writer.write('\n');
054: m_writer.write("\nNo rule violations detected.");
055: }
056:
057: return;
058: }
059:
060: m_writer.write("Source File: ");
061: m_writer.write(fileName);
062: m_writer.write('\n');
063:
064: while (violations.hasNext()) {
065: RuleViolation ruleViolation = (RuleViolation) violations
066: .next();
067: Rule rule = ruleViolation.getRule();
068:
069: //
070: // Line Number
071: //
072: m_writer.write('\n');
073: m_writer.write("Line: ");
074: m_writer.write(ruleViolation.getLine());
075: m_writer.write('\n');
076:
077: //
078: // Rule Message
079: //
080: String ruleMessage = ruleViolation.getDescription();
081:
082: if (ruleMessage == null) {
083: ruleMessage = "";
084: } else {
085: ruleMessage = ruleMessage.replace('\n', ' ').trim();
086: }
087:
088: m_writer.write("Rule: ");
089: m_writer.write(ruleMessage);
090: m_writer.write('\n');
091:
092: //
093: // Rule Priority
094: //
095: m_writer.write("Rule Priority: ");
096: m_writer.write(rule.getPriorityName());
097: m_writer.write('\n');
098:
099: //
100: // Rule Description
101: //
102: String description = rule.getDescription();
103:
104: if (description == null) {
105: description = "";
106: } else {
107: description = description.replace('\n', ' ').trim();
108: }
109:
110: m_writer.write("Description: ");
111: m_writer.write(description);
112: m_writer.write('\n');
113:
114: //
115: // Rule Example
116: //
117: String example = rule.getExample();
118:
119: if ((example != null) && (example.length() > 0)) {
120: m_writer.write("Example: ");
121: m_writer.write(example);
122: m_writer.write('\n');
123: }
124: }
125:
126: //
127: // Space separation between rules.
128: //
129: m_writer.write("\n\n\n");
130: }
131: }
|