Project Tiles has retired. For details please refer to its Attic page.
AnyScopePropertyAccessor xref
View Javadoc

1   /*
2    * $Id: AnyScopePropertyAccessor.java 1291847 2012-02-21 15:09:30Z 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.ognl;
22  
23  import java.util.Map;
24  
25  import ognl.OgnlContext;
26  import ognl.PropertyAccessor;
27  
28  import org.apache.tiles.request.Request;
29  
30  /**
31   * Accesses attributes in any scope.
32   *
33   * @version $Rev: 1291847 $ $Date: 2012-02-22 02:09:30 +1100 (Wed, 22 Feb 2012) $
34   */
35  public class AnyScopePropertyAccessor implements PropertyAccessor {
36  
37      @Override
38      public Object getProperty(@SuppressWarnings("rawtypes") Map context, Object target, Object name) {
39          Request request = (Request) target;
40          String attributeName = (String) name;
41          for (String scopeName : request.getAvailableScopes()) {
42              Map<String, Object> scope = request.getContext(scopeName);
43              if (scope.containsKey(attributeName)) {
44                  return scope.get(attributeName);
45              }
46          }
47          return null;
48      }
49  
50      @Override
51      public String getSourceAccessor(OgnlContext context, Object target,
52              Object index) {
53          Request request = (Request) target;
54          String attributeName = (String) index;
55          for (String scopeName : request.getAvailableScopes()) {
56              Map<String, Object> scope = request.getContext(scopeName);
57              if (scope.containsKey(attributeName)) {
58                  return ".getContext(\"" + scopeName + "\").get(index)";
59              }
60          }
61          return null;
62      }
63  
64      @Override
65      public String getSourceSetter(OgnlContext context, Object target,
66              Object index) {
67          Request request = (Request) target;
68          String attributeName = (String) index;
69          String[] availableScopes = request.getAvailableScopes().toArray(new String[0]);
70          for (String scopeName : availableScopes) {
71              Map<String, Object> scope = request.getContext(scopeName);
72              if (scope.containsKey(attributeName)) {
73                  return ".getContext(\"" + scopeName + "\").put(index, target)";
74              }
75          }
76          return ".getContext(\"" + availableScopes[0] + "\").put(index, target)";
77      }
78  
79      @Override
80      public void setProperty(@SuppressWarnings("rawtypes") Map context, Object target, Object name,
81              Object value) {
82          Request request = (Request) target;
83          String attributeName = (String) name;
84          String[] availableScopes = request.getAvailableScopes().toArray(new String[0]);
85          for (String scopeName : availableScopes) {
86              Map<String, Object> scope = request.getContext(scopeName);
87              if (scope.containsKey(attributeName)) {
88                  scope.put(attributeName, value);
89                  return;
90              }
91          }
92          if (availableScopes.length > 0) {
93              request.getContext(availableScopes[0]).put(attributeName, value);
94          }
95      }
96  
97  }