1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.peaseplate.locator;
21
22 import java.io.BufferedReader;
23 import java.io.CharArrayReader;
24 import java.io.IOException;
25
26 import org.peaseplate.TemplateException;
27
28 public class LocatorUtils {
29
30 private static final String LINE_SEPARATOR = System.getProperty("line.separator");
31
32 public static String highlight(TemplateLocator locator, String message, int line, int column) {
33 StringBuilder builder = new StringBuilder();
34 int currentLine = 1;
35 int lineNumberWidth = String.valueOf(line+2).length();
36
37 try {
38 BufferedReader reader = new BufferedReader(new CharArrayReader(locator.load()));
39
40 try {
41 String data = null;
42
43 while ((data = reader.readLine()) != null) {
44 if ((currentLine >= line-2) && (currentLine <= line+2)) {
45 String lineNumber = String.valueOf(currentLine);
46
47 while (lineNumber.length() < lineNumberWidth)
48 lineNumber = " " + lineNumber;
49
50 builder.append(lineNumber).append(" | ");
51 builder.append(data).append(LINE_SEPARATOR);
52
53 if (currentLine == line) {
54 for (int i=0; i<lineNumberWidth+3; i+=1)
55 builder.append(' ');
56
57 for (int i=0; (i<column-1) && (i<data.length()); i+=1) {
58 char c = data.charAt(i);
59
60 if (Character.isWhitespace(c))
61 builder.append(c);
62 else
63 builder.append(' ');
64 }
65
66 builder.append('^').append(LINE_SEPARATOR);
67 }
68 }
69
70 if (currentLine > line+2)
71 break;
72
73 currentLine += 1;
74 }
75 }
76 finally {
77 reader.close();
78 }
79 }
80 catch (IOException e) {
81
82 }
83 catch (TemplateException e) {
84
85 }
86
87 return builder.toString();
88 }
89
90 }