1 package org.incava.analysis; 2 3 import java.io.*; 4 import java.util.*; 5 import net.sourceforge.pmd.ast.Token; 6 import org.incava.lang.StringExt; 7 import org.incava.util.ANSI; 8 9 10 14 public class ContextReport extends Report 15 { 16 19 public static int tabWidth = 4; 20 21 24 private final static String EOLN = System.getProperty("line.separator"); 25 26 30 private Reader sourceReader; 31 32 35 private boolean wroteFileName = false; 36 37 41 private String [] contents; 42 43 48 public ContextReport(Writer writer) 49 { 50 super(writer); 51 } 52 53 58 public ContextReport(OutputStream os) 59 { 60 super(os); 61 } 62 63 69 public ContextReport(Writer writer, String source) 70 { 71 super(writer, source); 72 } 73 74 80 public ContextReport(Writer writer, File file) 81 { 82 super(writer, file); 83 } 84 85 91 public ContextReport(OutputStream os, String source) 92 { 93 super(os, source); 94 } 95 96 102 public ContextReport(OutputStream os, File file) 103 { 104 super(os, file); 105 } 106 107 113 public void reset(File file) 114 { 115 super.reset(file); 116 wroteFileName = false; 117 contents = null; 118 119 try { 120 sourceReader = new FileReader(file); 121 } 122 catch (IOException ioe) { 123 tr.Ace.log("error reading file: " + file); 124 } 125 } 126 127 133 public void reset(String source) 134 { 135 super.reset(source); 136 wroteFileName = false; 137 contents = null; 138 139 sourceReader = new StringReader(source); 140 } 141 142 148 protected String toString(Violation violation) 149 { 150 StringBuffer buf = new StringBuffer (); 151 if (!wroteFileName) { 152 buf.append("In " + ANSI.BOLD + ANSI.REVERSE + fileName + ANSI.RESET + ":" + EOLN + EOLN); 153 wroteFileName = true; 154 } 155 156 if (contents == null) { 157 tr.Ace.log("opening reader for " + sourceReader); 158 159 try { 160 List cont = new ArrayList(); 161 BufferedReader br = new BufferedReader(sourceReader); 162 163 String line = br.readLine(); 164 while (line != null) { 165 cont.add(line); 166 line = br.readLine(); 167 } 168 169 contents = (String [])cont.toArray(new String [0]); 170 171 for (int i = 0; i < contents.length; ++i) { 172 tr.Ace.log("contents[" + i + "]: " + contents[i]); 173 } 174 } 175 catch (IOException ioe) { 176 tr.Ace.log("error reading source: " + ioe); 177 } 178 } 179 180 tr.Ace.log("writing: " + violation); 181 182 int beginLine = violation.getBeginLine() - 1; 183 int endLine = violation.getEndLine() - 1; 184 int beginColumn = violation.getBeginColumn() - 1; 185 int endColumn = violation.getEndColumn() - 1; 186 187 if (beginLine == endLine) { 188 writeLine(buf, beginLine); 189 underline(buf, beginLine, beginColumn, endColumn); 190 } 191 else { 192 markToEndOfLine(buf, beginLine, beginColumn); 193 for (int lnum = beginLine; lnum <= endLine; ++lnum) { 194 writeLine(buf, lnum); 195 } 196 markToStartPosition(buf, endLine, endColumn); 197 } 198 199 buf.append("*** " + violation.getMessage() + EOLN); 200 buf.append(EOLN); 201 202 return buf.toString(); 203 } 204 205 214 protected void indent(StringBuffer buf, int line, int column, char ch) 215 { 216 buf.append(" "); 217 218 buf.append(StringExt.repeat(ch, column)); 220 } 221 222 231 protected void markToEndOfLine(StringBuffer buf, int line, int column) 232 { 233 indent(buf, line, column, ' '); 234 235 int len = contents[line].length(); 236 237 buf.append('<'); 238 for (int i = column + 1; i < len; ++i) { 239 buf.append('-'); 240 } 241 buf.append(EOLN); 242 } 243 244 252 protected void markToStartPosition(StringBuffer buf, int line, int column) 253 { 254 indent(buf, line, column, '-'); 255 256 buf.append('>'); 257 buf.append(EOLN); 258 } 259 260 270 protected void underline(StringBuffer buf, int line, int beginColumn, int endColumn) 271 { 272 indent(buf, line, beginColumn, ' '); 273 274 if (beginColumn == endColumn) { 275 buf.append('^'); 276 } 277 else { 278 buf.append('<'); 279 for (int i = beginColumn + 1; i < endColumn; ++i) { 280 buf.append('-'); 281 } 282 buf.append('>'); 283 } 284 buf.append(EOLN); 285 } 286 287 294 protected void writeLine(StringBuffer buf, int line) 295 { 296 StringBuffer lnBuf = new StringBuffer ("" + (1 + line)); 297 while (lnBuf.length() < 6) { 298 lnBuf.insert(0, ' '); 299 } 300 301 buf.append(lnBuf); 302 buf.append(". "); 303 buf.append(contents[line]); 304 buf.append(EOLN); 305 } 306 307 } 308 | Popular Tags |