001: package org.apache.velocity.exception;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import org.apache.velocity.runtime.parser.ParseException;
023: import org.apache.velocity.util.introspection.Info;
024:
025: /**
026: * Application-level exception thrown when a resource of any type
027: * has a syntax or other error which prevents it from being parsed.
028: * <br>
029: * When this resource is thrown, a best effort will be made to have
030: * useful information in the exception's message. For complete
031: * information, consult the runtime log.
032: *
033: * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
034: * @author <a href="hps@intermeta.de">Henning P. Schmiedehausen</a>
035: * @version $Id: ParseErrorException.java 471381 2006-11-05 08:56:58Z wglass $
036: */
037: public class ParseErrorException extends VelocityException {
038: /**
039: * Version Id for serializable
040: */
041: private static final long serialVersionUID = -6665197935086306472L;
042:
043: /**
044: * The column number of the parsing error, or -1 if not defined.
045: */
046: private int columnNumber = -1;
047:
048: /**
049: * The line number of the parsing error, or -1 if not defined.
050: */
051: private int lineNumber = -1;
052:
053: /**
054: * The name of the template containing the error, or null if not defined.
055: */
056: private String templateName = "*unset*";
057:
058: /**
059: * If applicable, contains the invalid syntax or reference that triggered this exception
060: */
061: private String invalidSyntax;
062:
063: /**
064: * Create a ParseErrorException with the given message.
065: *
066: * @param exceptionMessage the error exception message
067: */
068: public ParseErrorException(String exceptionMessage) {
069: super (exceptionMessage);
070: }
071:
072: /**
073: * Create a ParseErrorException with the given ParseException.
074: *
075: * @param pex the parsing exception
076: */
077: public ParseErrorException(ParseException pex) {
078: super (pex.getMessage());
079:
080: // Don't use a second C'tor, TemplateParseException is a subclass of
081: // ParseException...
082: if (pex instanceof ExtendedParseException) {
083: ExtendedParseException xpex = (ExtendedParseException) pex;
084:
085: columnNumber = xpex.getColumnNumber();
086: lineNumber = xpex.getLineNumber();
087: templateName = xpex.getTemplateName();
088: } else {
089: // ugly, ugly, ugly...
090:
091: if (pex.currentToken != null
092: && pex.currentToken.next != null) {
093: columnNumber = pex.currentToken.next.beginColumn;
094: lineNumber = pex.currentToken.next.beginLine;
095: }
096: }
097: }
098:
099: /**
100: * Create a ParseErrorException with the given ParseException.
101: *
102: * @param pex the parsing exception
103: */
104: public ParseErrorException(VelocityException pex) {
105: super (pex.getMessage());
106:
107: // Don't use a second C'tor, TemplateParseException is a subclass of
108: // ParseException...
109: if (pex instanceof ExtendedParseException) {
110: ExtendedParseException xpex = (ExtendedParseException) pex;
111:
112: columnNumber = xpex.getColumnNumber();
113: lineNumber = xpex.getLineNumber();
114: templateName = xpex.getTemplateName();
115: } else if (pex.getWrappedThrowable() instanceof ParseException) {
116: ParseException pex2 = (ParseException) pex
117: .getWrappedThrowable();
118:
119: if (pex2.currentToken != null
120: && pex2.currentToken.next != null) {
121: columnNumber = pex2.currentToken.next.beginColumn;
122: lineNumber = pex2.currentToken.next.beginLine;
123: }
124: }
125: }
126:
127: /**
128: * Create a ParseErrorRuntimeException with the given message and info
129: *
130: * @param exceptionMessage the error exception message
131: * @param info an Info object with the current template info
132: */
133: public ParseErrorException(String exceptionMessage, Info info) {
134: super (exceptionMessage);
135: columnNumber = info.getColumn();
136: lineNumber = info.getLine();
137: templateName = info.getTemplateName();
138: }
139:
140: /**
141: * Create a ParseErrorRuntimeException with the given message and info
142: *
143: * @param exceptionMessage the error exception message
144: * @param info an Info object with the current template info
145: * @param invalidSyntax the invalid syntax or reference triggering this exception
146: */
147: public ParseErrorException(String exceptionMessage, Info info,
148: String invalidSyntax) {
149: super (exceptionMessage);
150: columnNumber = info.getColumn();
151: lineNumber = info.getLine();
152: templateName = info.getTemplateName();
153: this .invalidSyntax = invalidSyntax;
154: }
155:
156: /**
157: * Return the column number of the parsing error, or -1 if not defined.
158: *
159: * @return column number of the parsing error, or -1 if not defined
160: */
161: public int getColumnNumber() {
162: return columnNumber;
163: }
164:
165: /**
166: * Return the line number of the parsing error, or -1 if not defined.
167: *
168: * @return line number of the parsing error, or -1 if not defined
169: */
170: public int getLineNumber() {
171: return lineNumber;
172: }
173:
174: /**
175: * Return the name of the template containing the error, or null if not
176: * defined.
177: *
178: * @return the name of the template containing the parsing error, or null
179: * if not defined
180: */
181: public String getTemplateName() {
182: return templateName;
183: }
184:
185: /**
186: * Return the invalid syntax or reference that triggered this error, or null
187: * if not defined.
188: *
189: * @return Return the invalid syntax or reference that triggered this error, or null
190: * if not defined
191: */
192: public String getInvalidSyntax() {
193: return invalidSyntax;
194: }
195:
196: }
|