This project has retired. For details please refer to its
Attic page.
EnvironmentScopeExtractor xref
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 public EnvironmentScopeExtractor(Environment request) {
52 this.request = request;
53 }
54
55 @Override
56 public void removeValue(String name) {
57 request.setVariable(name, null);
58 }
59
60 @SuppressWarnings("unchecked")
61 @Override
62 public Enumeration<String> getKeys() {
63 try {
64 return Collections.<String> enumeration(request.getKnownVariableNames());
65 } catch (TemplateModelException e) {
66 throw new FreemarkerRequestException("Cannot iterate variable names correctly", e);
67 }
68 }
69
70 @Override
71 public Object getValue(String key) {
72 try {
73 TemplateModel variable = request.getVariable(key);
74 if (variable != null) {
75 return DeepUnwrap.unwrap(variable);
76 }
77 return null;
78 } catch (TemplateModelException e) {
79 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 TemplateModel model = request.getObjectWrapper().wrap(value);
87 request.setVariable(key, model);
88 } catch (TemplateModelException e) {
89 throw new FreemarkerRequestException("Error when wrapping an object setting the '" + key + "' attribute", e);
90 }
91 }
92 }