1 package org.codehaus.plexus.interpolation;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.util.Collection;
20 import java.util.Collections;
21 import java.util.List;
22 import java.util.Stack;
23
24 import org.codehaus.plexus.interpolation.util.ValueSourceUtils;
25
26
27
28
29
30
31
32
33 public class PrefixAwareRecursionInterceptor implements RecursionInterceptor {
34
35 public static final String DEFAULT_START_TOKEN = "\\$\\{";
36
37 public static final String DEFAULT_END_TOKEN = "\\}";
38
39 private Stack<String> nakedExpressions = new Stack<String>();
40
41 private final String[] possiblePrefixes;
42
43 private boolean watchUnprefixedExpressions = true;
44
45
46
47
48
49
50
51
52 public PrefixAwareRecursionInterceptor(Collection<String> possiblePrefixes, boolean watchUnprefixedExpressions) {
53 this.possiblePrefixes = possiblePrefixes.toArray(new String[possiblePrefixes.size()]);
54 this.watchUnprefixedExpressions = watchUnprefixedExpressions;
55 }
56
57
58
59
60
61
62
63 public PrefixAwareRecursionInterceptor(Collection<String> possiblePrefixes) {
64 this.possiblePrefixes = possiblePrefixes.toArray(new String[possiblePrefixes.size()]);
65 }
66
67 public boolean hasRecursiveExpression(String expression) {
68 String realExpr = ValueSourceUtils.trimPrefix(expression, possiblePrefixes, watchUnprefixedExpressions);
69 return realExpr != null && nakedExpressions.contains(realExpr);
70 }
71
72 public void expressionResolutionFinished(String expression) {
73 nakedExpressions.pop();
74 }
75
76 public void expressionResolutionStarted(String expression) {
77 String realExpr = ValueSourceUtils.trimPrefix(expression, possiblePrefixes, watchUnprefixedExpressions);
78 nakedExpressions.push(realExpr);
79 }
80
81
82
83
84
85
86
87
88
89 public List getExpressionCycle(String expression) {
90 String expr = ValueSourceUtils.trimPrefix(expression, possiblePrefixes, watchUnprefixedExpressions);
91
92 if (expr == null) {
93 return Collections.EMPTY_LIST;
94 }
95
96 int idx = nakedExpressions.indexOf(expr);
97 if (idx < 0) {
98 return Collections.EMPTY_LIST;
99 } else {
100 return nakedExpressions.subList(idx, nakedExpressions.size());
101 }
102 }
103
104 public void clear() {
105 nakedExpressions.clear();
106 }
107 }