svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.expressionevaluator / org.gvsig.expressionevaluator.lib / org.gvsig.expressionevaluator.lib.api / src / main / java / org / gvsig / expressionevaluator / ExpressionSyntaxException.java @ 47248
History | View | Annotate | Download (2.94 KB)
1 |
package org.gvsig.expressionevaluator; |
---|---|
2 |
|
3 |
import org.apache.commons.lang3.StringUtils; |
4 |
|
5 |
/**
|
6 |
*
|
7 |
* @author jjdelcerro
|
8 |
*/
|
9 |
public class ExpressionSyntaxException extends RuntimeException { |
10 |
|
11 |
private final int position; |
12 |
private final int line; |
13 |
private final int column; |
14 |
private final String phrase; |
15 |
private final String description; |
16 |
private final String tip; |
17 |
|
18 |
public ExpressionSyntaxException() {
|
19 |
super("Syntax error in expression."); |
20 |
this.phrase = null; |
21 |
this.position = -1; |
22 |
this.line = -1; |
23 |
this.column = -1; |
24 |
this.description = I18N.Syntax_error_in_expression();
|
25 |
this.tip = null; |
26 |
} |
27 |
|
28 |
public ExpressionSyntaxException(LexicalAnalyzer lexer) {
|
29 |
this(null, lexer, null); |
30 |
} |
31 |
|
32 |
public ExpressionSyntaxException(String msg, LexicalAnalyzer lexer) { |
33 |
this(msg, lexer, null); |
34 |
} |
35 |
|
36 |
public ExpressionSyntaxException(String msg, LexicalAnalyzer lexer, String tip) { |
37 |
this(msg, lexer.getSource(), lexer.getMaxPosition(), lexer.getMaxLine(), lexer.getMaxColumn(), tip);
|
38 |
} |
39 |
|
40 |
public ExpressionSyntaxException(String phrase, int position) { |
41 |
this(null, phrase, position, -1, -1, null); |
42 |
} |
43 |
|
44 |
public ExpressionSyntaxException(String msg, String phrase, int position, String tip) { |
45 |
this(msg, phrase, position, -1, -1, tip); |
46 |
} |
47 |
|
48 |
public ExpressionSyntaxException(String msg, String phrase, int position, int line, int column, String tip) { |
49 |
super("Syntax error in '"+getSource(phrase, position) +"' near character "+ formatPosition(position,line,column) + ". " + StringUtils.trimToEmpty(msg)); |
50 |
this.phrase = phrase;
|
51 |
this.position = position;
|
52 |
this.line = line;
|
53 |
this.column = column;
|
54 |
if( StringUtils.isBlank(msg) ) {
|
55 |
this.description = I18N.Syntax_error_near_character_XPositionX(position);
|
56 |
} else {
|
57 |
this.description = I18N.Syntax_error_near_character_XPositionX(position)+ " "+msg; |
58 |
} |
59 |
this.tip = tip;
|
60 |
} |
61 |
|
62 |
private static String formatPosition(int position, int line, int column) { |
63 |
if( line<0 || column<0 ) { |
64 |
return String.valueOf(position); |
65 |
} |
66 |
return position + " ("+line+":"+column+")"; |
67 |
} |
68 |
|
69 |
private static String getSource(String source, int position) { |
70 |
String s = StringUtils.left(source, position) + "[*]" + StringUtils.mid(source, position, 200); |
71 |
if( s.length()>200 ) { |
72 |
s = "..."+StringUtils.mid(s, position-100, 200)+"..."; |
73 |
} |
74 |
return s;
|
75 |
} |
76 |
|
77 |
public String getPhrase() { |
78 |
return this.phrase; |
79 |
} |
80 |
|
81 |
public int getPosition() { |
82 |
return this.position; |
83 |
} |
84 |
|
85 |
public int getLine() { |
86 |
return this.line; |
87 |
} |
88 |
|
89 |
public int getColumn() { |
90 |
return this.column; |
91 |
} |
92 |
|
93 |
public String getDescription() { |
94 |
return this.description; |
95 |
} |
96 |
|
97 |
public String getTip() { |
98 |
return this.tip; |
99 |
} |
100 |
} |