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