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 | 43983 | jjdelcerro | package org.gvsig.expressionevaluator; |
---|---|---|---|
2 | |||
3 | 44379 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
4 | |||
5 | 43983 | jjdelcerro | /**
|
6 | *
|
||
7 | * @author jjdelcerro
|
||
8 | */
|
||
9 | public class ExpressionSyntaxException extends RuntimeException { |
||
10 | |||
11 | 44098 | jjdelcerro | private final int position; |
12 | 44379 | jjdelcerro | private final int line; |
13 | private final int column; |
||
14 | 44098 | jjdelcerro | private final String phrase; |
15 | private final String description; |
||
16 | 45935 | jjdelcerro | private final String tip; |
17 | 43983 | jjdelcerro | |
18 | public ExpressionSyntaxException() {
|
||
19 | super("Syntax error in expression."); |
||
20 | 44098 | jjdelcerro | this.phrase = null; |
21 | this.position = -1; |
||
22 | 44379 | jjdelcerro | this.line = -1; |
23 | this.column = -1; |
||
24 | 44098 | jjdelcerro | this.description = I18N.Syntax_error_in_expression();
|
25 | 44139 | jjdelcerro | this.tip = null; |
26 | 43983 | jjdelcerro | } |
27 | |||
28 | public ExpressionSyntaxException(LexicalAnalyzer lexer) {
|
||
29 | 45935 | jjdelcerro | this(null, lexer, null); |
30 | 43983 | jjdelcerro | } |
31 | |||
32 | public ExpressionSyntaxException(String msg, LexicalAnalyzer lexer) { |
||
33 | 45935 | jjdelcerro | this(msg, lexer, null); |
34 | 43983 | jjdelcerro | } |
35 | 44139 | jjdelcerro | |
36 | public ExpressionSyntaxException(String msg, LexicalAnalyzer lexer, String tip) { |
||
37 | 45935 | jjdelcerro | this(msg, lexer.getSource(), lexer.getMaxPosition(), lexer.getMaxLine(), lexer.getMaxColumn(), tip);
|
38 | 44139 | jjdelcerro | } |
39 | 43983 | jjdelcerro | |
40 | public ExpressionSyntaxException(String phrase, int position) { |
||
41 | 45935 | jjdelcerro | this(null, phrase, position, -1, -1, null); |
42 | 43983 | jjdelcerro | } |
43 | |||
44 | 45935 | jjdelcerro | 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 | 43983 | jjdelcerro | this.phrase = phrase;
|
51 | this.position = position;
|
||
52 | 45935 | jjdelcerro | 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 | 43983 | jjdelcerro | } |
61 | 45935 | jjdelcerro | |
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 | 43983 | jjdelcerro | |
69 | 44379 | jjdelcerro | 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 | 43983 | jjdelcerro | public String getPhrase() { |
78 | return this.phrase; |
||
79 | } |
||
80 | |||
81 | public int getPosition() { |
||
82 | return this.position; |
||
83 | } |
||
84 | 44098 | jjdelcerro | |
85 | 44379 | jjdelcerro | public int getLine() { |
86 | return this.line; |
||
87 | } |
||
88 | |||
89 | public int getColumn() { |
||
90 | return this.column; |
||
91 | } |
||
92 | |||
93 | 44098 | jjdelcerro | public String getDescription() { |
94 | return this.description; |
||
95 | } |
||
96 | 44139 | jjdelcerro | |
97 | public String getTip() { |
||
98 | return this.tip; |
||
99 | } |
||
100 | 43983 | jjdelcerro | } |