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.internal;
21  
22  import org.peaseplate.TemplateException;
23  import org.peaseplate.locator.TemplateLocator;
24  
25  
26  /**
27   * The abstract implementation of a template exception that is
28   * linked to some tempalte source.
29   * 
30   * @author Manfred HANTSCHEL
31   */
32  public abstract class AbstractLocatedTemplateException extends TemplateException {
33  
34      private static final long serialVersionUID = 1L;
35  	private static final String LINE_SEPARATOR = System.getProperty("line.separator");
36      
37  	private final TemplateLocator locator;
38  	private final int line;
39  	private final int column;
40  	
41  	/**
42  	 * Creates the exception using the specified locator, line and column number and the message.
43  	 * @param locator the locator
44  	 * @param line the line
45  	 * @param column the column
46  	 * @param message the message
47  	 */
48  	public AbstractLocatedTemplateException(TemplateLocator locator, int line, int column, String message) {
49  		this(locator, line, column, message, null);
50      }
51  
52  	/**
53  	 * Creates the exception using the specified locator, line and column number and the message.
54  	 * Additionally it gets the cause for the exception.
55  	 * @param locator the locator
56  	 * @param line the line
57  	 * @param column the column
58  	 * @param message the message
59  	 * @param cause the cause
60  	 */
61  	public AbstractLocatedTemplateException(TemplateLocator locator, int line, int column, String message, Throwable cause) {
62  	    super(addHighlight(locator, message, line, column), cause);
63  	    
64  	    this.locator = locator;
65  	    this.line = line;
66  	    this.column = column;
67      }
68  
69  	/**
70  	 * Returns the locator of the template where the exception occurred 
71  	 * @return the locator
72  	 */
73  	public TemplateLocator getLocator() {
74      	return locator;
75      }
76  
77  	/**
78  	 * Returns the line of the template where the exception occurred
79  	 * @return the line
80  	 */
81  	public int getLine() {
82      	return line;
83      }
84  
85  	/**
86  	 * Returns the column of the template where the exception occurred
87  	 * @return the column
88  	 */
89  	public int getColumn() {
90      	return column;
91      }
92  
93  	private static String addHighlight(TemplateLocator locator, String message, int line, int column) {
94  		StringBuilder builder = new StringBuilder(message);
95  		
96  		if (locator != null) {
97  			String highlight = locator.highlight(message, line, column);
98  			
99  			if (highlight != null) {
100 				builder.append(LINE_SEPARATOR).append(LINE_SEPARATOR);
101 				builder.append(locator).append(" [").append(line).append(", ").append(column).append("]:").append(LINE_SEPARATOR);
102 				builder.append(highlight);
103 			}
104 			else {
105 				builder.append(" (").append(locator).append(" [").append(line).append(", ").append(column).append("])");
106 			}
107 		}
108 		
109 	    return builder.toString();
110     }
111 
112 }