Coverage Report - org.apache.tiles.el.ScopeELResolver
 
Classes in this File Line Coverage Branch Coverage Complexity
ScopeELResolver
100%
39/39
83%
20/24
3.833
 
 1  
 /*
 2  
  * $Id: ScopeELResolver.java 1049676 2010-12-15 19:38:54Z 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.el;
 22  
 
 23  
 import java.beans.FeatureDescriptor;
 24  
 import java.util.ArrayList;
 25  
 import java.util.Collections;
 26  
 import java.util.Iterator;
 27  
 import java.util.List;
 28  
 import java.util.Map;
 29  
 
 30  
 import javax.el.ELContext;
 31  
 import javax.el.ELResolver;
 32  
 
 33  
 import org.apache.tiles.request.Request;
 34  
 
 35  
 /**
 36  
  * Resolves beans in request, session and application scope.
 37  
  *
 38  
  * @version $Rev: 1049676 $ $Date: 2010-12-16 06:38:54 +1100 (Thu, 16 Dec 2010) $
 39  
  * @since 2.2.1
 40  
  */
 41  9
 public class ScopeELResolver extends ELResolver {
 42  
 
 43  
     /**
 44  
      * The length of the suffix: "Scope".
 45  
      */
 46  
     private static final int SUFFIX_LENGTH = 5;
 47  
 
 48  
     /** {@inheritDoc} */
 49  
     @Override
 50  
     public Class<?> getCommonPropertyType(ELContext context, Object base) {
 51  
         // only resolve at the root of the context
 52  2
         if (base != null) {
 53  1
             return null;
 54  
         }
 55  
 
 56  1
         return Map.class;
 57  
     }
 58  
 
 59  
     /** {@inheritDoc} */
 60  
     @Override
 61  
     public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context,
 62  
             Object base) {
 63  2
         if (base != null) {
 64  1
             List<FeatureDescriptor> retValue = Collections.emptyList();
 65  1
             return retValue.iterator();
 66  
         }
 67  
 
 68  1
         List<FeatureDescriptor> list = new ArrayList<FeatureDescriptor>();
 69  
 
 70  1
         Request request = (Request) context
 71  
                 .getContext(Request.class);
 72  1
         for (String scope : request.getAvailableScopes()) {
 73  2
             FeatureDescriptor descriptor = new FeatureDescriptor();
 74  2
             descriptor.setDisplayName(scope + "Scope");
 75  2
             descriptor.setExpert(false);
 76  2
             descriptor.setHidden(false);
 77  2
             descriptor.setName(scope + "Scope");
 78  2
             descriptor.setPreferred(true);
 79  2
             descriptor.setShortDescription("");
 80  2
             descriptor.setValue("type", Map.class);
 81  2
             descriptor.setValue("resolvableAtDesignTime", Boolean.FALSE);
 82  2
             list.add(descriptor);
 83  2
         }
 84  
 
 85  1
         return list.iterator();
 86  
     }
 87  
 
 88  
     /** {@inheritDoc} */
 89  
     @Override
 90  
     public Class<?> getType(ELContext context, Object base, Object property) {
 91  4
         if (base != null || property == null || !(property instanceof String)
 92  
                 || !((String) property).endsWith("Scope")) {
 93  1
             return null;
 94  
         }
 95  
 
 96  3
         return Map.class;
 97  
     }
 98  
 
 99  
     /** {@inheritDoc} */
 100  
     @Override
 101  
     public Object getValue(ELContext context, Object base, Object property) {
 102  26
         if (base != null) {
 103  9
             return null;
 104  
         }
 105  
 
 106  17
         Object retValue = null;
 107  17
         String propertyString = (String) property;
 108  17
         if (property instanceof String && propertyString.endsWith("Scope")) {
 109  9
             Request request = (Request) context
 110  
                     .getContext(Request.class);
 111  9
             retValue = request.getContext(propertyString.substring(0,
 112  
                     propertyString.length() - SUFFIX_LENGTH));
 113  
         }
 114  
 
 115  17
         if (retValue != null) {
 116  9
             context.setPropertyResolved(true);
 117  
         }
 118  
 
 119  17
         return retValue;
 120  
     }
 121  
 
 122  
     /** {@inheritDoc} */
 123  
     @Override
 124  
     public boolean isReadOnly(ELContext context, Object base, Object property) {
 125  2
         if (context == null) {
 126  1
             throw new NullPointerException();
 127  
         }
 128  
 
 129  1
         return true;
 130  
     }
 131  
 
 132  
     /** {@inheritDoc} */
 133  
     @Override
 134  
     public void setValue(ELContext context, Object base, Object property,
 135  
             Object value) {
 136  
         // Does nothing for the moment.
 137  1
     }
 138  
 }