001: /**
002: * MVEL (The MVFLEX Expression Language)
003: *
004: * Copyright (C) 2007 Christopher Brock, MVFLEX/Valhalla Project and the Codehaus
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: */package org.mvel;
019:
020: import static java.lang.Character.isWhitespace;
021: import static java.lang.String.copyValueOf;
022: import java.util.ArrayList;
023: import java.util.List;
024:
025: public class CompileException extends RuntimeException {
026: private char[] expr;
027: private int cursor;
028: private List<ErrorDetail> errors;
029:
030: public CompileException() {
031: super ();
032: }
033:
034: public CompileException(String message) {
035: super (message);
036: }
037:
038: public CompileException(String message, List<ErrorDetail> errors) {
039: super (message);
040:
041: this .errors = errors;
042: for (ErrorDetail error : errors) {
043: System.err.println("* " + error.toString());
044: }
045: System.err.flush();
046: }
047:
048: public CompileException(String message, char[] expr, int cursor,
049: Exception e) {
050: super ("Failed to compile:\n[Error: " + message
051: + "]\n[Near: { ... " + showCodeNearError(expr, cursor)
052: + " ... } ]", e);
053: this .expr = expr;
054: this .cursor = cursor;
055: }
056:
057: public CompileException(String message, char[] expr, int cursor) {
058: super ("Failed to compile:\n[Error: " + message
059: + "]\n[Near: { ... " + showCodeNearError(expr, cursor)
060: + " ... } ]");
061: this .expr = expr;
062: this .cursor = cursor;
063: }
064:
065: public CompileException(String message, char[] expr, int cursor,
066: boolean concatError) {
067: super (concatError ? "Failed to compile:\n[Error: " + message
068: + "]\n[Near: { ... " + showCodeNearError(expr, cursor)
069: + " ... } ]" : message);
070: this .expr = expr;
071: this .cursor = cursor;
072: }
073:
074: public CompileException(String message, char[] expr, int cursor,
075: boolean concatError, Exception e) {
076: super (concatError ? "Failed to compile:\n[Error: " + message
077: + "]\n[Near: { ... " + showCodeNearError(expr, cursor)
078: + " ... } ]" : message, e);
079: this .expr = expr;
080: this .cursor = cursor;
081: }
082:
083: public CompileException(String message, Throwable cause) {
084: super (message, cause);
085: }
086:
087: public CompileException(Throwable cause) {
088: super (cause);
089: }
090:
091: private static CharSequence showCodeNearError(char[] expr,
092: int cursor) {
093: if (expr == null)
094: return "Unknown";
095:
096: int start = cursor - 10;
097: int end = (cursor + 20);
098:
099: if (end > expr.length) {
100: end = expr.length - 1;
101: start -= 20;
102: }
103:
104: if (start < 0) {
105: start = 0;
106: }
107:
108: while (start < end && isWhitespace(expr[start]))
109: start++;
110: // while (end > start && isWhitespace(expr[end])) end--;
111:
112: return copyValueOf(expr, start, end - start);
113: }
114:
115: public char[] getExpr() {
116: return expr;
117: }
118:
119: public int getCursor() {
120: return cursor;
121: }
122:
123: public List<ErrorDetail> getErrors() {
124: return errors != null ? errors : new ArrayList(0);
125: }
126:
127: public void setErrors(List<ErrorDetail> errors) {
128: this.errors = errors;
129: }
130: }
|