1 /*
2  * Copyright 2002-2009 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package org.springframework.expression;
18 
19 
20 /**
21  * Super class for exceptions that can occur whilst processing expressions
22  *
23  * @author Andy Clement
24  * @since 3.0
25  */
26 public class ExpressionException extends RuntimeException {
27 
28 	protected String expressionString;
29 	protected int position; // -1 if not known - but should be known in all reasonable cases
30 
31 	/**
32 	 * Creates a new expression exception.
33 	 * @param expressionString the expression string
34 	 * @param message a descriptive message
35 	 */
ExpressionException(String expressionString, String message)36 	public ExpressionException(String expressionString, String message) {
37 		super(message);
38 		this.position = -1;
39 		this.expressionString = expressionString;
40 	}
41 
42 	/**
43 	 * Creates a new expression exception.
44 	 * @param expressionString the expression string
45 	 * @param position the position in the expression string where the problem occurred
46 	 * @param message a descriptive message
47 	 */
ExpressionException(String expressionString, int position, String message)48 	public ExpressionException(String expressionString, int position, String message) {
49 		super(message);
50 		this.position = position;
51 		this.expressionString = expressionString;
52 	}
53 
54 	/**
55 	 * Creates a new expression exception.
56 	 * @param position the position in the expression string where the problem occurred
57 	 * @param message a descriptive message
58 	 */
ExpressionException(int position, String message)59 	public ExpressionException(int position, String message) {
60 		super(message);
61 		this.position = position;
62 	}
63 
64 	/**
65 	 * Creates a new expression exception.
66 	 * @param position the position in the expression string where the problem occurred
67 	 * @param message a descriptive message
68 	 * @param cause the underlying cause of this exception
69 	 */
ExpressionException(int position, String message, Throwable cause)70 	public ExpressionException(int position, String message, Throwable cause) {
71 		super(message,cause);
72 		this.position = position;
73 	}
74 
75 	/**
76 	 * Creates a new expression exception.
77 	 * @param message a descriptive message
78 	 */
ExpressionException(String message)79 	public ExpressionException(String message) {
80 		super(message);
81 	}
82 
ExpressionException(String message, Throwable cause)83 	public ExpressionException(String message, Throwable cause) {
84 		super(message,cause);
85 	}
86 
toDetailedString()87 	public String toDetailedString() {
88 		StringBuilder output = new StringBuilder();
89 		if (expressionString!=null) {
90 			output.append("Expression '");
91 			output.append(expressionString);
92 			output.append("'");
93 			if (position!=-1) {
94 				output.append(" @ ");
95 				output.append(position);
96 			}
97 			output.append(": ");
98 		}
99 		output.append(getMessage());
100 		return output.toString();
101 	}
102 
getExpressionString()103 	public final String getExpressionString() {
104 		return this.expressionString;
105 	}
106 
getPosition()107 	public final int getPosition() {
108 		return position;
109 	}
110 
111 }
112