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
33      implements RecursionInterceptor
34  {
35  
36      private Stack expressions = new Stack();
37  
38      /**
39       * {@inheritDoc}
40       */
41      public void expressionResolutionFinished( String expression )
42      {
43          expressions.pop();
44      }
45  
46      /**
47       * {@inheritDoc}
48       */
49      public void expressionResolutionStarted( String expression )
50      {
51          expressions.push( expression );
52      }
53  
54      /**
55       * Check whether the current expression is already present in the in-process
56       * stack.
57       */
58      public boolean hasRecursiveExpression( String expression )
59      {
60          return expressions.contains( expression );
61      }
62  
63      /**
64       * When an expression is determined to be a recursive reference, this method
65       * returns the sublist of tracked expressions that participate in this cycle.
66       * Otherwise, if the expression isn't present in the in-process stack, return
67       * {@link Collections#EMPTY_LIST}.
68       */
69      public List getExpressionCycle( String expression )
70      {
71          int idx = expressions.indexOf( expression );
72          if ( idx < 0 )
73          {
74              return Collections.EMPTY_LIST;
75          }
76          else
77          {
78              return expressions.subList( idx, expressions.size() );
79          }
80      }
81  
82      public void clear(){
83          expressions.clear();
84      }
85  }