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.request.freemarker.extractor; |
22 | |
|
23 | |
import java.util.Collections; |
24 | |
import java.util.Enumeration; |
25 | |
|
26 | |
import org.apache.tiles.request.attribute.AttributeExtractor; |
27 | |
import org.apache.tiles.request.freemarker.FreemarkerRequestException; |
28 | |
|
29 | |
import freemarker.core.Environment; |
30 | |
import freemarker.template.TemplateModel; |
31 | |
import freemarker.template.TemplateModelException; |
32 | |
import freemarker.template.utility.DeepUnwrap; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
public class EnvironmentScopeExtractor implements AttributeExtractor { |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
private Environment request; |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | 11 | public EnvironmentScopeExtractor(Environment request) { |
52 | 11 | this.request = request; |
53 | 11 | } |
54 | |
|
55 | |
@Override |
56 | |
public void removeValue(String name) { |
57 | 1 | request.setVariable(name, null); |
58 | 1 | } |
59 | |
|
60 | |
@SuppressWarnings("unchecked") |
61 | |
@Override |
62 | |
public Enumeration<String> getKeys() { |
63 | |
try { |
64 | 2 | return Collections.<String> enumeration(request.getKnownVariableNames()); |
65 | 1 | } catch (TemplateModelException e) { |
66 | 1 | throw new FreemarkerRequestException("Cannot iterate variable names correctly", e); |
67 | |
} |
68 | |
} |
69 | |
|
70 | |
@Override |
71 | |
public Object getValue(String key) { |
72 | |
try { |
73 | 3 | TemplateModel variable = request.getVariable(key); |
74 | 2 | if (variable != null) { |
75 | 1 | return DeepUnwrap.unwrap(variable); |
76 | |
} |
77 | 1 | return null; |
78 | 1 | } catch (TemplateModelException e) { |
79 | 1 | throw new FreemarkerRequestException("Cannot get attribute with name '" + key + "'", e); |
80 | |
} |
81 | |
} |
82 | |
|
83 | |
@Override |
84 | |
public void setValue(String key, Object value) { |
85 | |
try { |
86 | 2 | TemplateModel model = request.getObjectWrapper().wrap(value); |
87 | 1 | request.setVariable(key, model); |
88 | 1 | } catch (TemplateModelException e) { |
89 | 1 | throw new FreemarkerRequestException("Error when wrapping an object setting the '" + key + "' attribute", e); |
90 | 1 | } |
91 | 1 | } |
92 | |
} |