View Javadoc

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 685685 2008-08-13 21:43:27Z nbubna $
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       * @since 1.5
79       */
80      public ParseErrorException(ParseException pex)
81      {
82          super(pex.getMessage());
83  
84          // Don't use a second C'tor, TemplateParseException is a subclass of
85          // ParseException...
86          if (pex instanceof ExtendedParseException)
87          {
88              ExtendedParseException xpex = (ExtendedParseException) pex;
89  
90              columnNumber = xpex.getColumnNumber();
91              lineNumber = xpex.getLineNumber();
92              templateName = xpex.getTemplateName();
93          }
94          else
95          {
96              //  ugly, ugly, ugly...
97  
98              if (pex.currentToken != null && pex.currentToken.next != null)
99              {
100                 columnNumber = pex.currentToken.next.beginColumn;
101                 lineNumber = pex.currentToken.next.beginLine;
102             }
103         }
104     }
105 
106     /**
107      * Create a ParseErrorException with the given ParseException.
108      *
109      * @param pex the parsing exception
110      * @since 1.5
111      */
112     public ParseErrorException(VelocityException pex)
113     {
114         super(pex.getMessage());
115 
116         // Don't use a second C'tor, TemplateParseException is a subclass of
117         // ParseException...
118         if (pex instanceof ExtendedParseException)
119         {
120             ExtendedParseException xpex = (ExtendedParseException) pex;
121 
122             columnNumber = xpex.getColumnNumber();
123             lineNumber = xpex.getLineNumber();
124             templateName = xpex.getTemplateName();
125         }
126         else if (pex.getWrappedThrowable() instanceof ParseException)
127         {
128             ParseException pex2 = (ParseException) pex.getWrappedThrowable();
129 
130             if (pex2.currentToken != null && pex2.currentToken.next != null)
131             {
132                 columnNumber = pex2.currentToken.next.beginColumn;
133                 lineNumber = pex2.currentToken.next.beginLine;
134             }
135         }
136     }
137 
138 
139     /**
140      * Create a ParseErrorRuntimeException with the given message and info
141      * 
142      * @param exceptionMessage the error exception message
143      * @param info an Info object with the current template info
144      * @since 1.5
145      */
146     public ParseErrorException(String exceptionMessage, Info info)
147     {
148         super(exceptionMessage);
149         columnNumber = info.getColumn();
150         lineNumber = info.getLine();
151         templateName = info.getTemplateName();        
152     }    
153 
154     /**
155      * Create a ParseErrorRuntimeException with the given message and info
156      * 
157      * @param exceptionMessage the error exception message
158      * @param info an Info object with the current template info
159      * @param invalidSyntax the invalid syntax or reference triggering this exception
160      * @since 1.5
161      */
162     public ParseErrorException(String exceptionMessage, 
163             Info info, String invalidSyntax)
164     {
165         super(exceptionMessage);
166         columnNumber = info.getColumn();
167         lineNumber = info.getLine();
168         templateName = info.getTemplateName();  
169         this.invalidSyntax = invalidSyntax;       
170     }    
171 
172 
173     /**
174      * Return the column number of the parsing error, or -1 if not defined.
175      *
176      * @return column number of the parsing error, or -1 if not defined
177      * @since 1.5
178      */
179     public int getColumnNumber()
180     {
181         return columnNumber;
182     }
183 
184     /**
185      * Return the line number of the parsing error, or -1 if not defined.
186      *
187      * @return line number of the parsing error, or -1 if not defined
188      * @since 1.5
189      */
190     public int getLineNumber()
191     {
192         return lineNumber;
193     }
194 
195     /**
196      * Return the name of the template containing the error, or null if not
197      * defined.
198      *
199      * @return the name of the template containing the parsing error, or null
200      *      if not defined
201      * @since 1.5
202      */
203     public String getTemplateName()
204     {
205         return templateName;
206     }
207 
208     /**
209      * Return the invalid syntax or reference that triggered this error, or null
210      * if not defined.
211      * 
212      * @return Return the invalid syntax or reference that triggered this error, or null
213      * if not defined
214      * @since 1.5
215      */
216     public String getInvalidSyntax()
217     {
218         return invalidSyntax;
219     }
220 
221 }