1 package org.apache.velocity.exception;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import org.apache.velocity.runtime.parser.ParseException;
23 import org.apache.velocity.util.introspection.Info;
24
25 /**
26 * Application-level exception thrown when a resource of any type
27 * has a syntax or other error which prevents it from being parsed.
28 * <br>
29 * When this resource is thrown, a best effort will be made to have
30 * useful information in the exception's message. For complete
31 * information, consult the runtime log.
32 *
33 * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
34 * @author <a href="hps@intermeta.de">Henning P. Schmiedehausen</a>
35 * @version $Id: ParseErrorException.java 471381 2006-11-05 08:56:58Z wglass $
36 */
37 public class ParseErrorException extends VelocityException
38 {
39 /**
40 * Version Id for serializable
41 */
42 private static final long serialVersionUID = -6665197935086306472L;
43
44 /**
45 * The column number of the parsing error, or -1 if not defined.
46 */
47 private int columnNumber = -1;
48
49 /**
50 * The line number of the parsing error, or -1 if not defined.
51 */
52 private int lineNumber = -1;
53
54 /**
55 * The name of the template containing the error, or null if not defined.
56 */
57 private String templateName = "*unset*";
58
59 /**
60 * If applicable, contains the invalid syntax or reference that triggered this exception
61 */
62 private String invalidSyntax;
63
64 /**
65 * Create a ParseErrorException with the given message.
66 *
67 * @param exceptionMessage the error exception message
68 */
69 public ParseErrorException(String exceptionMessage)
70 {
71 super(exceptionMessage);
72 }
73
74 /**
75 * Create a ParseErrorException with the given ParseException.
76 *
77 * @param pex the parsing exception
78 */
79 public ParseErrorException(ParseException pex)
80 {
81 super(pex.getMessage());
82
83 // Don't use a second C'tor, TemplateParseException is a subclass of
84 // ParseException...
85 if (pex instanceof ExtendedParseException)
86 {
87 ExtendedParseException xpex = (ExtendedParseException) pex;
88
89 columnNumber = xpex.getColumnNumber();
90 lineNumber = xpex.getLineNumber();
91 templateName = xpex.getTemplateName();
92 }
93 else
94 {
95 // ugly, ugly, ugly...
96
97 if (pex.currentToken != null && pex.currentToken.next != null)
98 {
99 columnNumber = pex.currentToken.next.beginColumn;
100 lineNumber = pex.currentToken.next.beginLine;
101 }
102 }
103 }
104
105 /**
106 * Create a ParseErrorException with the given ParseException.
107 *
108 * @param pex the parsing exception
109 */
110 public ParseErrorException(VelocityException pex)
111 {
112 super(pex.getMessage());
113
114 // Don't use a second C'tor, TemplateParseException is a subclass of
115 // ParseException...
116 if (pex instanceof ExtendedParseException)
117 {
118 ExtendedParseException xpex = (ExtendedParseException) pex;
119
120 columnNumber = xpex.getColumnNumber();
121 lineNumber = xpex.getLineNumber();
122 templateName = xpex.getTemplateName();
123 }
124 else if (pex.getWrappedThrowable() instanceof ParseException)
125 {
126 ParseException pex2 = (ParseException) pex.getWrappedThrowable();
127
128 if (pex2.currentToken != null && pex2.currentToken.next != null)
129 {
130 columnNumber = pex2.currentToken.next.beginColumn;
131 lineNumber = pex2.currentToken.next.beginLine;
132 }
133 }
134 }
135
136
137 /**
138 * Create a ParseErrorRuntimeException with the given message and info
139 *
140 * @param exceptionMessage the error exception message
141 * @param info an Info object with the current template info
142 */
143 public ParseErrorException(String exceptionMessage, Info info)
144 {
145 super(exceptionMessage);
146 columnNumber = info.getColumn();
147 lineNumber = info.getLine();
148 templateName = info.getTemplateName();
149 }
150
151 /**
152 * Create a ParseErrorRuntimeException with the given message and info
153 *
154 * @param exceptionMessage the error exception message
155 * @param info an Info object with the current template info
156 * @param invalidSyntax the invalid syntax or reference triggering this exception
157 */
158 public ParseErrorException(String exceptionMessage,
159 Info info, String invalidSyntax)
160 {
161 super(exceptionMessage);
162 columnNumber = info.getColumn();
163 lineNumber = info.getLine();
164 templateName = info.getTemplateName();
165 this.invalidSyntax = invalidSyntax;
166 }
167
168
169 /**
170 * Return the column number of the parsing error, or -1 if not defined.
171 *
172 * @return column number of the parsing error, or -1 if not defined
173 */
174 public int getColumnNumber()
175 {
176 return columnNumber;
177 }
178
179 /**
180 * Return the line number of the parsing error, or -1 if not defined.
181 *
182 * @return line number of the parsing error, or -1 if not defined
183 */
184 public int getLineNumber()
185 {
186 return lineNumber;
187 }
188
189 /**
190 * Return the name of the template containing the error, or null if not
191 * defined.
192 *
193 * @return the name of the template containing the parsing error, or null
194 * if not defined
195 */
196 public String getTemplateName()
197 {
198 return templateName;
199 }
200
201 /**
202 * Return the invalid syntax or reference that triggered this error, or null
203 * if not defined.
204 *
205 * @return Return the invalid syntax or reference that triggered this error, or null
206 * if not defined
207 */
208 public String getInvalidSyntax()
209 {
210 return invalidSyntax;
211 }
212
213 }