1 /*
2 * $Id: ScopePropertyAccessor.java 1049696 2010-12-15 20:30:10Z apetrelli $
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 a scope.
32 *
33 * @version $Rev: 1049696 $ $Date: 2010-12-16 07:30:10 +1100 (Thu, 16 Dec 2010) $
34 */
35 public class ScopePropertyAccessor implements PropertyAccessor {
36
37 /**
38 * The length of the scope suffix: "Scope".
39 */
40 static final int SCOPE_SUFFIX_LENGTH = 5;
41
42 @Override
43 public Object getProperty(@SuppressWarnings("rawtypes") Map context, Object target, Object name) {
44 Request request = (Request) target;
45 String scope = (String) name;
46 if (scope.endsWith("Scope")) {
47 String scopeName = scope.substring(0, scope.length() - SCOPE_SUFFIX_LENGTH);
48 return request.getContext(scopeName);
49 }
50 return null;
51 }
52
53 @Override
54 public String getSourceAccessor(OgnlContext context, Object target,
55 Object index) {
56 String scope = (String) index;
57 if (scope.endsWith("Scope")) {
58 String scopeName = scope.substring(0, scope.length() - SCOPE_SUFFIX_LENGTH);
59 return ".getContext(\"" + scopeName + "\")";
60 }
61 return null;
62 }
63
64 @Override
65 public String getSourceSetter(OgnlContext context, Object target,
66 Object index) {
67 return null;
68 }
69
70 @Override
71 public void setProperty(@SuppressWarnings("rawtypes") Map context, Object target, Object name,
72 Object value) {
73 // Does nothing.
74 }
75
76 }