1 package org.apache.velocity.runtime.parser;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.velocity.exception.ExtendedParseException;
23
24
25
26
27
28
29
30
31
32
33
34 public class TemplateParseException
35 extends ParseException
36 implements ExtendedParseException
37 {
38 private static final long serialVersionUID = -3146323135623083918L;
39
40
41
42
43
44 private final String templateName;
45
46
47
48
49
50
51
52
53
54 public TemplateParseException(Token currentTokenVal, int [][] expectedTokenSequencesVal, String [] tokenImageVal,
55 String templateNameVal)
56 {
57 super(currentTokenVal, expectedTokenSequencesVal, tokenImageVal);
58 this.templateName = templateNameVal;
59 }
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76 public TemplateParseException(Token currentTokenVal, int [][] expectedTokenSequencesVal, String [] tokenImageVal)
77 {
78 super(currentTokenVal, expectedTokenSequencesVal, tokenImageVal);
79 templateName = "*unset*";
80 }
81
82
83
84
85
86
87
88
89
90
91 public TemplateParseException()
92 {
93 super();
94 templateName = "*unset*";
95 }
96
97
98
99
100
101
102 public TemplateParseException(String message)
103 {
104 super(message);
105 templateName = "*unset*";
106 }
107
108
109
110
111
112 public String getTemplateName()
113 {
114 return templateName;
115 }
116
117
118
119
120
121 public int getLineNumber()
122 {
123 if ((currentToken != null) && (currentToken.next != null))
124 {
125 return currentToken.next.beginLine;
126 }
127 else
128 {
129 return -1;
130 }
131 }
132
133
134
135
136
137 public int getColumnNumber()
138 {
139 if ((currentToken != null) && (currentToken.next != null))
140 {
141 return currentToken.next.beginColumn;
142 }
143 else
144 {
145 return -1;
146 }
147 }
148
149
150
151
152
153
154
155
156
157
158
159
160 public String getMessage()
161 {
162 if (!specialConstructor)
163 {
164 StringBuffer sb = new StringBuffer(super.getMessage());
165 appendTemplateInfo(sb);
166 return sb.toString();
167 }
168
169 int maxSize = 0;
170
171 StringBuffer expected = new StringBuffer();
172
173 for (int i = 0; i < expectedTokenSequences.length; i++)
174 {
175 if (maxSize < expectedTokenSequences[i].length)
176 {
177 maxSize = expectedTokenSequences[i].length;
178 }
179
180 for (int j = 0; j < expectedTokenSequences[i].length; j++)
181 {
182 expected.append(tokenImage[expectedTokenSequences[i][j]]).append(" ");
183 }
184
185 if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0)
186 {
187 expected.append("...");
188 }
189
190 expected.append(eol).append(" ");
191 }
192
193 StringBuffer retval = new StringBuffer("Encountered \"");
194 Token tok = currentToken.next;
195
196 for (int i = 0; i < maxSize; i++)
197 {
198 if (i != 0)
199 {
200 retval.append(" ");
201 }
202
203 if (tok.kind == 0)
204 {
205 retval.append(tokenImage[0]);
206 break;
207 }
208
209 retval.append(add_escapes(tok.image));
210 tok = tok.next;
211 }
212
213 retval.append("\"");
214 appendTemplateInfo(retval);
215
216 if (expectedTokenSequences.length == 1)
217 {
218 retval.append("Was expecting:").append(eol).append(" ");
219 }
220 else
221 {
222 retval.append("Was expecting one of:").append(eol).append(" ");
223 }
224
225
226 retval.append(expected.toString());
227 return retval.toString();
228 }
229
230
231
232
233 protected void appendTemplateInfo(final StringBuffer sb)
234 {
235 sb.append(" at line ").append(getLineNumber())
236 .append(", column ").append(getColumnNumber());
237
238 if (getTemplateName() != null)
239 {
240 sb.append(" of ").append(getTemplateName());
241 }
242 else
243 {
244 sb.append(".");
245 }
246 sb.append(eol);
247 }
248 }