Coverage Report - org.apache.myfaces.el.unified.resolver.ScopedAttributeResolver
 
Classes in this File Line Coverage Branch Coverage Complexity
ScopedAttributeResolver
0%
0/61
0%
0/34
0
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements.  See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership.  The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License.  You may obtain a copy of the License at
 9  
  *
 10  
  *   http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied.  See the License for the
 16  
  * specific language governing permissions and limitations
 17  
  * under the License.
 18  
  */
 19  
 package org.apache.myfaces.el.unified.resolver;
 20  
 
 21  
 import java.beans.FeatureDescriptor;
 22  
 import java.util.ArrayList;
 23  
 import java.util.Iterator;
 24  
 import java.util.List;
 25  
 import java.util.Map;
 26  
 
 27  
 import javax.el.ELContext;
 28  
 import javax.el.ELException;
 29  
 import javax.el.ELResolver;
 30  
 import javax.el.PropertyNotFoundException;
 31  
 import javax.el.PropertyNotWritableException;
 32  
 import javax.faces.context.ExternalContext;
 33  
 import javax.faces.context.FacesContext;
 34  
 
 35  
 /**
 36  
  * See JSF 1.2 spec section 5.6.2.7
 37  
  *
 38  
  * @author Stan Silvert
 39  
  */
 40  
 public final class ScopedAttributeResolver extends ELResolver {
 41  
     
 42  
     /**
 43  
      * Creates a new instance of ScopedAttributeResolver
 44  
      */
 45  0
     public ScopedAttributeResolver() {
 46  0
     }
 47  
 
 48  
     public void setValue(final ELContext context, final Object base, final Object property, final Object value)
 49  
         throws NullPointerException, PropertyNotFoundException, PropertyNotWritableException, ELException {
 50  
         
 51  0
         if (base != null) return;
 52  0
         if (property == null) throw new PropertyNotFoundException();
 53  
         
 54  0
         final Map<String, Object> scopedMap = findScopedMap(externalContext(context), property);
 55  0
         if (scopedMap != null) {
 56  0
             scopedMap.put((String)property, value);
 57  
         } else {
 58  0
             externalContext(context).getRequestMap().put((String)property, value);
 59  
         }
 60  
         
 61  0
         context.setPropertyResolved(true);
 62  0
     }
 63  
 
 64  
     public boolean isReadOnly(final ELContext context, final Object base, final Object property)
 65  
         throws NullPointerException, PropertyNotFoundException, ELException {
 66  
         
 67  0
         if (base == null) context.setPropertyResolved(true);
 68  
         
 69  0
         return false;
 70  
     }
 71  
 
 72  
     public Object getValue(final ELContext context, final Object base, final Object property)
 73  
         throws NullPointerException, PropertyNotFoundException, ELException {
 74  
         
 75  0
         if (base != null) return null;
 76  0
         if (property == null) throw new PropertyNotFoundException();
 77  
 
 78  0
         context.setPropertyResolved(true);
 79  
 
 80  0
         final Map scopedMap = findScopedMap(externalContext(context), property);
 81  0
         if (scopedMap != null) {
 82  0
             return scopedMap.get(property);
 83  
         }
 84  
         
 85  0
         return null;
 86  
     }
 87  
 
 88  
     public Class<?> getType(final ELContext context, final Object base, final Object property)
 89  
         throws NullPointerException, PropertyNotFoundException, ELException {
 90  
         
 91  0
         if (base != null) return null;
 92  0
         if (property == null) throw new PropertyNotFoundException();
 93  
         
 94  0
         context.setPropertyResolved(true);
 95  0
         return Object.class;
 96  
     }
 97  
 
 98  
     public Iterator<FeatureDescriptor> getFeatureDescriptors(final ELContext context, final Object base) {
 99  
         
 100  0
         if (base != null) return null;
 101  
         
 102  0
         final List<FeatureDescriptor> descriptorList = new ArrayList<FeatureDescriptor>();
 103  0
         final ExternalContext extContext = externalContext(context);
 104  0
         addDescriptorsToList(descriptorList, extContext.getRequestMap());
 105  0
         addDescriptorsToList(descriptorList, extContext.getSessionMap());
 106  0
         addDescriptorsToList(descriptorList, extContext.getApplicationMap());
 107  
         
 108  0
         return descriptorList.iterator();
 109  
     }
 110  
 
 111  
     public Class<?> getCommonPropertyType(final ELContext context, final Object base) {
 112  
         
 113  0
         if (base != null) return null;
 114  
         
 115  0
         return String.class;
 116  
     }
 117  
     
 118  
     // side effect: modifies the list
 119  
     private static void addDescriptorsToList(final List<FeatureDescriptor> descriptorList, final Map scopeMap) {
 120  0
         for (Object name: scopeMap.keySet()) {
 121  0
             String strName = (String)name;
 122  0
             Class runtimeType = scopeMap.get(strName).getClass();
 123  0
             descriptorList.add(makeDescriptor(strName, runtimeType));
 124  0
         }
 125  0
     }
 126  
     
 127  
     private static FeatureDescriptor makeDescriptor(final String name, final Class runtimeType) {
 128  0
         FeatureDescriptor fd = new FeatureDescriptor();
 129  0
         fd.setValue(ELResolver.RESOLVABLE_AT_DESIGN_TIME, Boolean.TRUE);
 130  0
         fd.setValue(ELResolver.TYPE, runtimeType);
 131  0
         fd.setName(name);
 132  0
         fd.setDisplayName(name);
 133  0
         fd.setShortDescription(name);
 134  0
         fd.setExpert(false);
 135  0
         fd.setHidden(false);
 136  0
         fd.setPreferred(true);
 137  0
         return fd;
 138  
     }
 139  
     
 140  
     // returns null if not found
 141  
     private static Map<String, Object> findScopedMap(final ExternalContext extContext, final Object property) {
 142  
         
 143  0
         if (extContext == null) return null;
 144  
 
 145  0
         Map<String, Object> scopedMap = extContext.getRequestMap();
 146  0
         if (scopedMap.containsKey(property)) return scopedMap;
 147  
         
 148  0
         scopedMap = extContext.getSessionMap();
 149  0
         if (scopedMap.containsKey(property)) return scopedMap;
 150  
 
 151  0
         scopedMap = extContext.getApplicationMap();
 152  0
         if (scopedMap.containsKey(property)) return scopedMap;
 153  
         
 154  0
         return null;
 155  
     }
 156  
     
 157  
     // get the FacesContext from the ELContext
 158  
     private static FacesContext facesContext(final ELContext context) {
 159  0
         return (FacesContext)context.getContext(FacesContext.class);
 160  
     }
 161  
     
 162  
     private static ExternalContext externalContext(final ELContext context) {
 163  0
         FacesContext facesContext = facesContext(context);
 164  0
         if (facesContext != null) {
 165  0
             return facesContext(context).getExternalContext();
 166  
         }
 167  
         
 168  0
         return null;
 169  
     }
 170  
     
 171  
 }