View Javadoc
1   package net.sourceforge.pmd.lang.vm.ast;
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 net.sourceforge.pmd.lang.vm.util.LogUtil;
23  
24  /**
25   * Exception to indicate problem happened while constructing #macro()
26   * 
27   * For internal use in parser - not to be passed to app level
28   * 
29   * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
30   * @author <a href="hps@intermeta.de">Henning P. Schmiedehausen</a>
31   * @version $Id: MacroParseException.java 735709 2009-01-19 14:30:03Z byron $
32   */
33  public class MacroParseException extends ParseException {
34      private final String templateName;
35  
36      /**
37       * Version Id for serializable
38       */
39      private static final long serialVersionUID = -4985224672336070689L;
40  
41      /**
42       * @param msg
43       * @param templateName
44       * @param currentToken
45       */
46      public MacroParseException(final String msg, final String templateName, final Token currentToken) {
47          super(msg + " at ");
48          this.currentToken = currentToken;
49          this.templateName = templateName;
50      }
51  
52      /**
53       * returns the Template name where this exception occured.
54       * 
55       * @return The Template name where this exception occured.
56       * @since 1.5
57       */
58      public String getTemplateName() {
59          return templateName;
60      }
61  
62      /**
63       * returns the line number where this exception occured.
64       * 
65       * @return The line number where this exception occured.
66       * @since 1.5
67       */
68      public int getLineNumber() {
69          if (currentToken != null && currentToken.next != null) {
70              return currentToken.next.beginLine;
71          }
72          else if (currentToken != null) {
73              return currentToken.beginLine;
74          }
75          else {
76              return -1;
77          }
78      }
79  
80      /**
81       * returns the column number where this exception occured.
82       * 
83       * @return The column number where this exception occured.
84       * @since 1.5
85       */
86      public int getColumnNumber() {
87          if (currentToken != null && currentToken.next != null) {
88              return currentToken.next.beginColumn;
89          }
90          else if (currentToken != null) {
91              return currentToken.beginColumn;
92          }
93          else {
94              return -1;
95          }
96      }
97  
98      /**
99       * This method has the standard behavior when this object has been created using the standard constructors.
100      * Otherwise, it uses "currentToken" and "expectedTokenSequences" to generate a parse error message and returns it.
101      * If this object has been created due to a parse error, and you do not catch it (it gets thrown from the parser),
102      * then this method is called during the printing of the final stack trace, and hence the correct error message gets
103      * displayed.
104      * 
105      * @return the current message.
106      * @since 1.5
107      */
108     @Override
109     public String getMessage() {
110         final StringBuffer sb = new StringBuffer(super.getMessage());
111         appendTemplateInfo(sb);
112         return sb.toString();
113     }
114 
115     /**
116      * @param sb
117      * @since 1.5
118      */
119     protected void appendTemplateInfo(final StringBuffer sb) {
120         sb.append(LogUtil.formatFileString(getTemplateName(), getLineNumber(), getColumnNumber()));
121         sb.append(eol);
122     }
123 }