Coverage Report - org.apache.tiles.ognl.AnyScopePropertyAccessor
 
Classes in this File Line Coverage Branch Coverage Complexity
AnyScopePropertyAccessor
100%
36/36
94%
17/18
4.25
 
 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  8
 public class AnyScopePropertyAccessor implements PropertyAccessor {
 36  
 
 37  
     @Override
 38  
     public Object getProperty(@SuppressWarnings("rawtypes") Map context, Object target, Object name) {
 39  11
         Request request = (Request) target;
 40  11
         String attributeName = (String) name;
 41  11
         for (String scopeName : request.getAvailableScopes()) {
 42  19
             Map<String, Object> scope = request.getContext(scopeName);
 43  19
             if (scope.containsKey(attributeName)) {
 44  10
                 return scope.get(attributeName);
 45  
             }
 46  9
         }
 47  1
         return null;
 48  
     }
 49  
 
 50  
     @Override
 51  
     public String getSourceAccessor(OgnlContext context, Object target,
 52  
             Object index) {
 53  3
         Request request = (Request) target;
 54  3
         String attributeName = (String) index;
 55  3
         for (String scopeName : request.getAvailableScopes()) {
 56  5
             Map<String, Object> scope = request.getContext(scopeName);
 57  5
             if (scope.containsKey(attributeName)) {
 58  2
                 return ".getContext(\"" + scopeName + "\").get(index)";
 59  
             }
 60  3
         }
 61  1
         return null;
 62  
     }
 63  
 
 64  
     @Override
 65  
     public String getSourceSetter(OgnlContext context, Object target,
 66  
             Object index) {
 67  3
         Request request = (Request) target;
 68  3
         String attributeName = (String) index;
 69  3
         String[] availableScopes = request.getAvailableScopes().toArray(new String[0]);
 70  6
         for (String scopeName : availableScopes) {
 71  5
             Map<String, Object> scope = request.getContext(scopeName);
 72  5
             if (scope.containsKey(attributeName)) {
 73  2
                 return ".getContext(\"" + scopeName + "\").put(index, target)";
 74  
             }
 75  
         }
 76  1
         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  3
         Request request = (Request) target;
 83  3
         String attributeName = (String) name;
 84  3
         String[] availableScopes = request.getAvailableScopes().toArray(new String[0]);
 85  6
         for (String scopeName : availableScopes) {
 86  5
             Map<String, Object> scope = request.getContext(scopeName);
 87  5
             if (scope.containsKey(attributeName)) {
 88  2
                 scope.put(attributeName, value);
 89  2
                 return;
 90  
             }
 91  
         }
 92  1
         if (availableScopes.length > 0) {
 93  1
             request.getContext(availableScopes[0]).put(attributeName, value);
 94  
         }
 95  1
     }
 96  
 
 97  
 }