View Javadoc

1   /*
2    * This file is part of Pease Plate Template Engine.
3    * 
4    * Pease Plate Template Engine is free software: you can redistribute
5    * it and/or modify it under the terms of the GNU Lesser General 
6    * Public License as published by the Free Software Foundation, 
7    * either version 3 of the License, or any later version.
8    * 
9    * Pease Plate Template Engine is distributed in the hope that it 
10   * will be useful, but WITHOUT ANY WARRANTY; without even the implied
11   * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12   * See the GNU Lesser General Public License for more details.
13   * 
14   * You should have received a copy of the GNU Lesser General Public 
15   * License along with Pease Plate Template Engine. If not, see 
16   * <http://www.gnu.org/licenses/>.
17   * 
18   * Copyright (c) 2008 Manfred HANTSCHEL
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      		// ignore
82      	}
83      	catch (TemplateException e) {
84      		// ignore
85      	}
86      	
87  		return builder.toString();
88  	}
89  
90  }