View Javadoc
1   package org.codehaus.plexus.interpolation;
2   
3   /*
4    * Copyright 2001-2008 Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.util.Collections;
20  import java.util.List;
21  import java.util.Stack;
22  
23  /**
24   * Simplest implementation of a {@link RecursionInterceptor}, which checks whether
25   * the existing interpolation effort is already attempting to resolve an exact
26   * expression, but has not finished. This will not catch synonym expressions, as
27   * are found in Maven (${project.build.directory}, ${pom.build.directory}, and
28   * ${build.directory} are synonyms).
29   *
30   * @author jdcasey
31   */
32  public class SimpleRecursionInterceptor implements RecursionInterceptor {
33  
34      private Stack expressions = new Stack();
35  
36      /**
37       * {@inheritDoc}
38       */
39      public void expressionResolutionFinished(String expression) {
40          expressions.pop();
41      }
42  
43      /**
44       * {@inheritDoc}
45       */
46      public void expressionResolutionStarted(String expression) {
47          expressions.push(expression);
48      }
49  
50      /**
51       * Check whether the current expression is already present in the in-process
52       * stack.
53       */
54      public boolean hasRecursiveExpression(String expression) {
55          return expressions.contains(expression);
56      }
57  
58      /**
59       * When an expression is determined to be a recursive reference, this method
60       * returns the sublist of tracked expressions that participate in this cycle.
61       * Otherwise, if the expression isn't present in the in-process stack, return
62       * {@link Collections#EMPTY_LIST}.
63       */
64      public List getExpressionCycle(String expression) {
65          int idx = expressions.indexOf(expression);
66          if (idx < 0) {
67              return Collections.EMPTY_LIST;
68          } else {
69              return expressions.subList(idx, expressions.size());
70          }
71      }
72  
73      public void clear() {
74          expressions.clear();
75      }
76  }