1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
package org.apache.tiles.mvel; |
22 | |
|
23 | |
import java.util.HashMap; |
24 | |
|
25 | |
import org.apache.tiles.context.TilesRequestContextHolder; |
26 | |
import org.mvel2.UnresolveablePropertyException; |
27 | |
import org.mvel2.integration.VariableResolver; |
28 | |
import org.mvel2.integration.impl.BaseVariableResolverFactory; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
public abstract class ReadOnlyVariableResolverFactory extends |
36 | |
BaseVariableResolverFactory { |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
protected TilesRequestContextHolder requestHolder; |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | 22 | public ReadOnlyVariableResolverFactory(TilesRequestContextHolder requestHolder) { |
50 | 22 | this.requestHolder = requestHolder; |
51 | 22 | variableResolvers = new HashMap<String, VariableResolver>(); |
52 | 22 | } |
53 | |
|
54 | |
|
55 | |
public VariableResolver createVariable(String name, Object value) { |
56 | 2 | if (nextFactory != null) { |
57 | 1 | return nextFactory.createVariable(name, value); |
58 | |
} |
59 | 1 | throw new UnsupportedOperationException("This variable resolver factory is read only"); |
60 | |
} |
61 | |
|
62 | |
|
63 | |
public VariableResolver createVariable(String name, Object value, |
64 | |
Class<?> type) { |
65 | 2 | variableResolvers = new HashMap<String, VariableResolver>(); |
66 | 2 | if (nextFactory != null) { |
67 | 1 | return nextFactory.createVariable(name, value, type); |
68 | |
} |
69 | 1 | throw new UnsupportedOperationException("This variable resolver factory is read only"); |
70 | |
} |
71 | |
|
72 | |
|
73 | |
public boolean isResolveable(String name) { |
74 | 58 | return isTarget(name) || isNextResolveable(name); |
75 | |
} |
76 | |
|
77 | |
|
78 | |
@Override |
79 | |
public VariableResolver getVariableResolver(String name) { |
80 | 27 | if (isResolveable(name)) { |
81 | 26 | if (variableResolvers != null && variableResolvers.containsKey(name)) { |
82 | 1 | return variableResolvers.get(name); |
83 | 25 | } else if (isTarget(name)) { |
84 | 16 | VariableResolver variableResolver = createVariableResolver(name); |
85 | 16 | variableResolvers.put(name, variableResolver); |
86 | 16 | return variableResolver; |
87 | 9 | } else if (nextFactory != null) { |
88 | 9 | return nextFactory.getVariableResolver(name); |
89 | |
} |
90 | |
} |
91 | |
|
92 | 1 | throw new UnresolveablePropertyException("unable to resolve variable '" + name + "'"); |
93 | |
} |
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
public abstract VariableResolver createVariableResolver(String name); |
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
public abstract static class ReadOnlyVariableResolver implements VariableResolver { |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
protected String name; |
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | 27 | public ReadOnlyVariableResolver(String name) { |
124 | 27 | this.name = name; |
125 | 27 | } |
126 | |
|
127 | |
|
128 | |
public int getFlags() { |
129 | 2 | return 0; |
130 | |
} |
131 | |
|
132 | |
|
133 | |
public String getName() { |
134 | 2 | return name; |
135 | |
} |
136 | |
|
137 | |
|
138 | |
public void setStaticType(@SuppressWarnings("rawtypes") Class type) { |
139 | |
|
140 | 2 | } |
141 | |
|
142 | |
|
143 | |
public void setValue(Object value) { |
144 | 1 | throw new UnsupportedOperationException("This resolver is read-only"); |
145 | |
} |
146 | |
} |
147 | |
} |