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      /** line number. */
29      private final int line;
30  
31      /** column number. */
32      private final int column;
33  
34      /** name. */
35      private final String name;
36  
37      /**
38       * @return the detailed information in case of an error
39       */
40      public Detail getDetail() {
41          return null;
42      }
43  
44      /**
45       * Describes errors more precisely.
46       */
47      public interface Detail {
48          /**
49           * @return the start column on the line that triggered the error
50           */
51          int start();
52  
53          /**
54           * @return the end column on the line that triggered the error
55           */
56          int end();
57  
58          /**
59           * @return the actual part of code that triggered the error
60           */
61  
62          @Override
63          String toString();
64      }
65  
66      /**
67       * Create info.
68       *
69       * @param source source name
70       * @param l line number
71       * @param c column number
72       */
73      public JexlInfo(final String source, final int l, final int c) {
74          name = source;
75          line = l <= 0? 1: l;
76          column = c <= 0? 1 : c;
77      }
78  
79      /**
80       * Create an information structure for dynamic set/get/invoke/new.
81       * <p>This gathers the class, method and line number of the first calling method
82       * outside of o.a.c.jexl3.</p>
83       */
84      public JexlInfo() {
85          final StackTraceElement[] stack = new Throwable().getStackTrace();
86          String cname = getClass().getName();
87          final String pkgname = getClass().getPackage().getName();
88          StackTraceElement se = null;
89          for (int s = 1; s < stack.length; ++s) {
90              se = stack[s];
91              final String className = se.getClassName();
92              if (!className.equals(cname)) {
93                  // go deeper if called from jexl implementation classes
94                  if (!className.startsWith(pkgname + ".internal.") && !className.startsWith(pkgname + ".Jexl")
95                      && !className.startsWith(pkgname + ".parser")) {
96                      break;
97                  }
98                  cname = className;
99              }
100         }
101         this.name = se != null ? se.getClassName() + "." + se.getMethodName() + ":" + se.getLineNumber() : "?";
102         this.line = 1;
103         this.column = 1;
104     }
105 
106     /**
107      * Creates info reusing the name.
108      *
109      * @param l the line
110      * @param c the column
111      * @return a new info instance
112      */
113     public JexlInfo at(final int l, final int c) {
114         return new JexlInfo(name, l, c);
115     }
116 
117     /**
118      * The copy constructor.
119      *
120      * @param copy the instance to copy
121      */
122     protected JexlInfo(final JexlInfo copy) {
123         this(copy.getName(), copy.getLine(), copy.getColumn());
124     }
125 
126     /**
127      * Formats this info in the form 'name&#064;line:column'.
128      *
129      * @return the formatted info
130      */
131     @Override
132     public String toString() {
133         final StringBuilder sb = new StringBuilder(name != null? name : "");
134         sb.append("@");
135         sb.append(line);
136         sb.append(":");
137         sb.append(column);
138         final JexlInfo.Detail dbg = getDetail();
139         if (dbg!= null) {
140             sb.append("![");
141             sb.append(dbg.start());
142             sb.append(",");
143             sb.append(dbg.end());
144             sb.append("]: '");
145             sb.append(dbg.toString());
146             sb.append("'");
147         }
148         return sb.toString();
149     }
150 
151     /**
152      * Gets the file/script/url name.
153      *
154      * @return template name
155      */
156     public final String getName() {
157         return name;
158     }
159 
160     /**
161      * Gets the line number.
162      *
163      * @return line number.
164      */
165     public final int getLine() {
166         return line;
167     }
168 
169     /**
170      * Gets the column number.
171      *
172      * @return the column.
173      */
174     public final int getColumn() {
175         return column;
176     }
177 
178     /**
179      * @return this instance or a copy without any decorations
180      */
181     public JexlInfo detach() {
182         return this;
183     }
184 
185     /**
186      * Gets the info from a script.
187      * @param script the script
188      * @return the info
189      */
190     public static JexlInfo from(final JexlScript script) {
191         return script instanceof Script? ((Script) script).getInfo() :  null;
192     }
193 }
194