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.awt.Color;
008: import java.io.StringWriter;
009: import java.util.Iterator;
010:
011: /**
012: *
013: * Converts the violations list into a text string for viewing.
014: *
015: * @author Donald A. Leckie
016: * @since August 27, 2002
017: * @version $Revision: 1.2 $, $Date: 2003/09/01 00:25:31 $
018: */
019: class HTMLResultRenderer {
020:
021: private boolean m_reportNoViolations;
022: private StringWriter m_writer;
023:
024: /**
025: *******************************************************************************
026: *
027: */
028: protected void beginRendering(boolean reportNoViolations) {
029: m_reportNoViolations = reportNoViolations;
030: m_writer = new StringWriter(25000);
031:
032: //
033: // Write HTML header.
034: //
035: m_writer
036: .write("<!doctype html public \"-//w3c//dtd html 4.0 transitional//en\">\n");
037: m_writer.write("<html>\n");
038: m_writer.write("<head>\n");
039: m_writer
040: .write("<meta content=\"text/html; charset=iso-8859-1\">\n");
041: m_writer.write("<title>PMD Analysis Results</title>\n");
042: m_writer.write("</head>\n");
043:
044: //
045: // Write the body.
046: //
047: m_writer.write("<body>\n");
048: }
049:
050: /**
051: *******************************************************************************
052: *
053: * @return HTML text.
054: */
055: protected String endRendering() {
056: m_writer.write("</body>\n");
057: m_writer.write("</html>\n");
058:
059: return m_writer.toString();
060: }
061:
062: /**
063: *******************************************************************************
064: *
065: * @param fileName
066: * @param report
067: */
068: public void render(String fileName, Report report) {
069: Iterator violations = report.iterator();
070:
071: if (violations.hasNext() == false) {
072: if (m_reportNoViolations) {
073: //
074: // Write the name of the file that was analyzed.
075: //
076: m_writer.write("<b>Source File:</b> ");
077: m_writer.write(fileName);
078: m_writer.write("\n<p>\n");
079: m_writer.write("No rule violations detected.<br>\n");
080: }
081:
082: return;
083: }
084:
085: //
086: // Write the name of the file that was analyzed.
087: //
088: m_writer.write("<b>Source File:</b> ");
089: m_writer.write(fileName);
090: m_writer.write("\n<br>\n");
091:
092: String hexValue = Integer.toHexString(Color.WHITE.getRGB());
093:
094: if (hexValue.startsWith("0x")) {
095: hexValue = hexValue.substring(2);
096: }
097:
098: if (hexValue.length() > 6) {
099: hexValue = hexValue.substring(hexValue.length() - 6);
100: }
101:
102: m_writer
103: .write("<table bgcolor=\"#" + hexValue + "\" border>\n");
104:
105: //
106: // Create the column headings.
107: //
108: m_writer.write("<tr>\n");
109: m_writer.write("<th><b>Line<br>No.</b></th>\n");
110: m_writer.write("<th><b>Rule</b></th>\n");
111: m_writer.write("<th><b>Priority</b></th>\n");
112: m_writer.write("<th><b>Description</b></th>\n");
113: m_writer.write("<th><b>Example</b></th>\n");
114: m_writer.write("</tr>\n");
115:
116: while (violations.hasNext()) {
117: RuleViolation ruleViolation = (RuleViolation) violations
118: .next();
119: Rule rule = ruleViolation.getRule();
120:
121: //
122: // Begin table row.
123: //
124: m_writer.write("<tr>\n");
125:
126: //
127: // Line Number
128: //
129: m_writer.write("<td align=\"center\" valign=\"top\">\n");
130: m_writer.write("<font size=\"3\">\n");
131: m_writer.write(String.valueOf(ruleViolation.getLine()));
132: m_writer.write("\n</font>\n");
133: m_writer.write("</td>\n");
134:
135: //
136: // Rule Message
137: //
138: String ruleMessage = ruleViolation.getDescription();
139:
140: if (ruleMessage == null) {
141: ruleMessage = "";
142: } else {
143: removeNewLineCharacters(ruleMessage);
144: }
145:
146: m_writer.write("<td align=\"left\" valign=\"top\">\n");
147: m_writer.write("<font size=\"3\">\n");
148: m_writer.write(ruleMessage);
149: m_writer.write("\n</font>\n");
150: m_writer.write("</td>\n");
151:
152: //
153: // Rule Priority
154: //
155: m_writer.write("<td align=\"left\" valign=\"top\">\n");
156: m_writer.write("<font size=\"3\">\n");
157: m_writer.write(rule.getPriorityName());
158: m_writer.write("\n</font>\n");
159: m_writer.write("</td>\n");
160:
161: //
162: // Rule Description
163: //
164: String description = rule.getDescription();
165:
166: if (description == null) {
167: description = "";
168: } else {
169: removeNewLineCharacters(description);
170: }
171:
172: m_writer.write("<td align=\"left\" valign=\"top\">\n");
173: m_writer.write("<font size=\"3\">\n");
174: m_writer.write(description);
175: m_writer.write("\n</font>\n");
176: m_writer.write("</td>\n");
177:
178: //
179: // Rule Example
180: //
181: String example = rule.getExample();
182:
183: if ((example != null) && (example.length() > 0)) {
184: StringBuffer buffer = new StringBuffer(example);
185:
186: for (int n = buffer.length() - 1; n >= 0; n--) {
187: if (buffer.charAt(n) == '\n') {
188: buffer.deleteCharAt(n);
189: } else {
190: break;
191: }
192: }
193:
194: example = buffer.toString();
195:
196: m_writer.write("<td align=\"left\" valign=\"top\">\n");
197: m_writer.write("<pre>\n");
198: m_writer.write("<font size=\"-1\">");
199: m_writer.write(example);
200: m_writer.write("</font>");
201: m_writer.write("\n</pre>\n");
202: m_writer.write("</td>\n");
203: }
204:
205: //
206: // End table row.
207: //
208: m_writer.write("</tr>\n");
209: }
210:
211: m_writer.write("</table>\n");
212: m_writer.write("<p><p>\n");
213: }
214:
215: /**
216: ******************************************************************************
217: *
218: * @param text
219: *
220: * @return
221: */
222: private String removeNewLineCharacters(String text) {
223: char[] chars = text.trim().toCharArray();
224: int startIndex = 0;
225:
226: for (int n = 0; n < chars.length; n++) {
227: if ((chars[n] != ' ') && (chars[n] != '\n')) {
228: startIndex = n;
229: break;
230: }
231: }
232:
233: int lastIndex = chars.length - 1;
234:
235: for (int n = lastIndex; n >= 0; n--) {
236: if ((chars[n] != ' ') && (chars[n] != '\n')) {
237: lastIndex = n;
238: break;
239: }
240: }
241:
242: return String.valueOf(chars, startIndex, lastIndex + 1);
243: }
244: }
|