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 org.codehaus.plexus.interpolation.util.ValueSourceUtils;
20  
21  import java.util.Collection;
22  import java.util.Collections;
23  import java.util.List;
24  import java.util.Stack;
25  
26  /**
27   * {@link RecursionInterceptor} implementation that provides support for expressions
28   * with multiple synonyms, such as project.build.directory == pom.build.directory ==
29   * build.directory in Maven's POM.
30   *
31   * @author jdcasey
32   */
33  public class PrefixAwareRecursionInterceptor
34      implements RecursionInterceptor
35  {
36  
37      public static final String DEFAULT_START_TOKEN = "\\$\\{";
38  
39      public static final String DEFAULT_END_TOKEN = "\\}";
40  
41      private Stack<String> nakedExpressions = new Stack<String>();
42  
43      private final String[] possiblePrefixes;
44  
45      private boolean watchUnprefixedExpressions = true;
46  
47      /**
48       * Use the specified expression prefixes to detect synonyms, and specify whether
49       * unprefixed expressions can be considered synonyms.
50       *
51       * @param possiblePrefixes           The collection of expression prefixes supported
52       * @param watchUnprefixedExpressions Whether to consider unprefixed expressions as synonyms
53       */
54      public PrefixAwareRecursionInterceptor( Collection<String> possiblePrefixes, boolean watchUnprefixedExpressions )
55      {
56          this.possiblePrefixes = possiblePrefixes.toArray( new String[possiblePrefixes.size()]) ;
57          this.watchUnprefixedExpressions = watchUnprefixedExpressions;
58      }
59  
60      /**
61       * Use the specified expression prefixes to detect synonyms. Consider
62       * unprefixed expressions synonyms as well.
63       *
64       * @param possiblePrefixes The collection of expression prefixes supported
65       */
66      public PrefixAwareRecursionInterceptor( Collection<String> possiblePrefixes )
67      {
68          this.possiblePrefixes = possiblePrefixes.toArray( new String[possiblePrefixes.size()]) ;
69      }
70  
71      public boolean hasRecursiveExpression( String expression )
72      {
73          String realExpr = ValueSourceUtils.trimPrefix( expression, possiblePrefixes, watchUnprefixedExpressions );
74          return realExpr != null && nakedExpressions.contains( realExpr );
75  
76      }
77  
78      public void expressionResolutionFinished( String expression )
79      {
80          nakedExpressions.pop();
81      }
82  
83      public void expressionResolutionStarted( String expression )
84      {
85          String realExpr = ValueSourceUtils.trimPrefix( expression, possiblePrefixes, watchUnprefixedExpressions );
86          nakedExpressions.push( realExpr );
87      }
88  
89      /**
90       * When an expression is determined to be a recursive reference, this method
91       * returns the sublist of tracked expressions that participate in this cycle.
92       * Otherwise, if the expression isn't present in the in-process stack, return
93       * {@link Collections#EMPTY_LIST}. Also, if the expression doesn't have a matched
94       * prefix from this interceptor's list, and unprefixed expressions aren't allowed
95       * then return {@link Collections#EMPTY_LIST}.
96       */
97      public List getExpressionCycle( String expression )
98      {
99          String expr = ValueSourceUtils.trimPrefix( expression, possiblePrefixes, watchUnprefixedExpressions );
100 
101         if ( expr == null )
102         {
103             return Collections.EMPTY_LIST;
104         }
105 
106         int idx = nakedExpressions.indexOf( expr );
107         if ( idx < 0 )
108         {
109             return Collections.EMPTY_LIST;
110         }
111         else
112         {
113             return nakedExpressions.subList( idx, nakedExpressions.size() );
114         }
115     }
116 
117     public void clear()
118     {
119         nakedExpressions.clear();
120     }
121 }