View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.jexl3;
19  
20  import org.apache.commons.jexl3.internal.Script;
21  
22  /**
23   * Helper class to carry information such as a url/file name, line and column for
24   * debugging information reporting.
25   */
26  public class JexlInfo {
27  
28      /**
29       * Describes errors more precisely.
30       */
31      public interface Detail {
32          /**
33           * @return the end column on the line that triggered the error
34           */
35          int end();
36  
37          /**
38           * @return the start column on the line that triggered the error
39           */
40          int start();
41  
42          /**
43           * @return the actual part of code that triggered the error
44           */
45  
46          @Override
47          String toString();
48      }
49  
50      /**
51       * Gets the info from a script.
52       * @param script the script
53       * @return the info
54       */
55      public static JexlInfo from(final JexlScript script) {
56          return script instanceof Script? ((Script) script).getInfo() :  null;
57      }
58  
59      /** Line number. */
60      private final int line;
61  
62      /** Column number. */
63      private final int column;
64  
65      /** Name. */
66      private final String name;
67  
68      /**
69       * Create an information structure for dynamic set/get/invoke/new.
70       * <p>This gathers the class, method and line number of the first calling method
71       * outside of o.a.c.jexl3.</p>
72       */
73      public JexlInfo() {
74          final StackTraceElement[] stack = new Throwable().getStackTrace();
75          String cname = getClass().getName();
76          final String pkgname = getClass().getPackage().getName();
77          StackTraceElement se = null;
78          for (int s = 1; s < stack.length; ++s) {
79              se = stack[s];
80              final String className = se.getClassName();
81              if (!className.equals(cname)) {
82                  // go deeper if called from jexl implementation classes
83                  if (!className.startsWith(pkgname + ".internal.") && !className.startsWith(pkgname + ".Jexl")
84                      && !className.startsWith(pkgname + ".parser")) {
85                      break;
86                  }
87                  cname = className;
88              }
89          }
90          this.name = se != null ? se.getClassName() + "." + se.getMethodName() + ":" + se.getLineNumber() : "?";
91          this.line = 1;
92          this.column = 1;
93      }
94  
95      /**
96       * The copy constructor.
97       *
98       * @param copy the instance to copy
99       */
100     protected JexlInfo(final JexlInfo copy) {
101         this(copy.getName(), copy.getLine(), copy.getColumn());
102     }
103 
104     /**
105      * Create info.
106      *
107      * @param source source name
108      * @param l line number
109      * @param c column number
110      */
111     public JexlInfo(final String source, final int l, final int c) {
112         name = source;
113         line = l <= 0? 1: l;
114         column = c <= 0? 1 : c;
115     }
116 
117     /**
118      * Creates info reusing the name.
119      *
120      * @param l the line
121      * @param c the column
122      * @return a new info instance
123      */
124     public JexlInfo at(final int l, final int c) {
125         return new JexlInfo(name, l, c);
126     }
127 
128     /**
129      * @return this instance or a copy without any decorations
130      */
131     public JexlInfo detach() {
132         return this;
133     }
134 
135     /**
136      * Gets the column number.
137      *
138      * @return the column.
139      */
140     public final int getColumn() {
141         return column;
142     }
143 
144     /**
145      * @return the detailed information in case of an error
146      */
147     public Detail getDetail() {
148         return null;
149     }
150 
151     /**
152      * Gets the line number.
153      *
154      * @return line number.
155      */
156     public final int getLine() {
157         return line;
158     }
159 
160     /**
161      * Gets the file/script/url name.
162      *
163      * @return template name
164      */
165     public final String getName() {
166         return name;
167     }
168 
169     /**
170      * Formats this info in the form 'name&#064;line:column'.
171      *
172      * @return the formatted info
173      */
174     @Override
175     public String toString() {
176         final StringBuilder sb = new StringBuilder(name != null ? name : "");
177         sb.append("@");
178         sb.append(line);
179         sb.append(":");
180         sb.append(column);
181         final JexlInfo.Detail dbg = getDetail();
182         if (dbg!= null) {
183             sb.append("![");
184             sb.append(dbg.start());
185             sb.append(",");
186             sb.append(dbg.end());
187             sb.append("]: '");
188             sb.append(dbg.toString());
189             sb.append("'");
190         }
191         return sb.toString();
192     }
193 }
194