This project has retired. For details please refer to its Attic page.
EnvironmentScopeExtractor xref
View Javadoc

1   /*
2    * $Id: EnvironmentScopeExtractor.java 1294456 2012-02-28 04:44:55Z nlebas $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Extract attributes from {@link Environment} objects as a scope.
36   *
37   * @version $Rev: 1294456 $ $Date: 2012-02-28 15:44:55 +1100 (Tue, 28 Feb 2012) $
38   */
39  public class EnvironmentScopeExtractor implements AttributeExtractor {
40  
41      /**
42       * The environment.
43       */
44      private Environment request;
45  
46      /**
47       * Constructor.
48       *
49       * @param request The environment.
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  }