Coverage Report - org.apache.myfaces.el.unified.resolver.ResourceBundleResolver
 
Classes in this File Line Coverage Branch Coverage Complexity
ResourceBundleResolver
0%
0/60
0%
0/42
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.Map;
 25  
 import java.util.ResourceBundle;
 26  
 import javax.el.ELContext;
 27  
 import javax.el.ELException;
 28  
 import javax.el.ELResolver;
 29  
 import javax.el.PropertyNotFoundException;
 30  
 import javax.el.PropertyNotWritableException;
 31  
 import javax.faces.application.Application;
 32  
 import javax.faces.context.FacesContext;
 33  
 
 34  
 import org.apache.myfaces.config.RuntimeConfig;
 35  
 
 36  
 /**
 37  
  * See JSF 1.2 spec section 5.6.1.4
 38  
  *
 39  
  * @author Stan Silvert
 40  
  */
 41  
 public final class ResourceBundleResolver extends ELResolver {
 42  
     
 43  
     /**
 44  
      * RuntimeConfig is instantiated once per servlet and never changes--we can
 45  
      * safely cache it
 46  
      */
 47  
     private RuntimeConfig runtimeConfig;
 48  
     
 49  
     /** Creates a new instance of ResourceBundleResolver */
 50  0
     public ResourceBundleResolver() {
 51  0
     }
 52  
 
 53  
     public void setValue(final ELContext context, final Object base, final Object property, final Object value)
 54  
         throws NullPointerException, PropertyNotFoundException, PropertyNotWritableException, ELException {
 55  
         
 56  0
         if ((base == null) && (property == null)) throw new PropertyNotFoundException();
 57  
 
 58  0
         if (!(property instanceof String)) return;
 59  
         
 60  0
         final ResourceBundle bundle = getResourceBundle(context, (String)property);
 61  
         
 62  0
         if (bundle != null) {
 63  0
             throw new PropertyNotWritableException("ResourceBundles are read-only");
 64  
         }
 65  0
     }
 66  
 
 67  
     public boolean isReadOnly(final ELContext context, final Object base, final Object property)
 68  
         throws NullPointerException, PropertyNotFoundException, ELException {
 69  
         
 70  0
         if (base != null) return false;
 71  0
         if (property == null) throw new PropertyNotFoundException();
 72  0
         if (!(property instanceof String)) return false;
 73  
         
 74  0
         final ResourceBundle bundle = getResourceBundle(context, (String)property);
 75  
         
 76  0
         if (bundle != null) {
 77  0
             context.setPropertyResolved(true);
 78  0
             return true;
 79  
         }
 80  
         
 81  0
         return false;
 82  
     }
 83  
 
 84  
     public Object getValue(final ELContext context, final Object base, final Object property)
 85  
         throws NullPointerException, PropertyNotFoundException, ELException {
 86  
         
 87  0
         if (base != null) return null;
 88  0
         if (property == null) throw new PropertyNotFoundException();
 89  0
         if (!(property instanceof String)) return null;
 90  
         
 91  0
         final ResourceBundle bundle = getResourceBundle(context, (String)property);
 92  
         
 93  0
         if (bundle != null) {
 94  0
             context.setPropertyResolved(true);
 95  0
             return bundle;
 96  
         }
 97  
         
 98  0
         return null;
 99  
     }
 100  
     
 101  
     public Class<?> getType(final ELContext context, final Object base, final Object property)
 102  
         throws NullPointerException, PropertyNotFoundException, ELException {
 103  
         
 104  0
         if (base != null) return null;
 105  0
         if (property == null) throw new PropertyNotFoundException();
 106  0
         if (!(property instanceof String)) return null;
 107  
         
 108  0
         final ResourceBundle bundle = getResourceBundle(context, (String)property);
 109  
         
 110  0
         if (bundle != null) {
 111  0
             context.setPropertyResolved(true);
 112  0
             return ResourceBundle.class;
 113  
         }
 114  
         
 115  0
         return null;
 116  
     }
 117  
 
 118  
     public Iterator<FeatureDescriptor> getFeatureDescriptors(final ELContext context, final Object base) {
 119  
        
 120  0
         if (base != null) return null;
 121  
         
 122  0
         final ArrayList<FeatureDescriptor> descriptors = new ArrayList<FeatureDescriptor>();
 123  
         
 124  0
         final Map<String, org.apache.myfaces.config.impl.digester.elements.ResourceBundle> resourceBundles = runtimeConfig(context).getResourceBundles();
 125  
         
 126  0
         for (org.apache.myfaces.config.impl.digester.elements.ResourceBundle resourceBundle : resourceBundles.values()) {
 127  0
             descriptors.add(makeDescriptor(resourceBundle));
 128  
         }
 129  
         
 130  0
         return descriptors.iterator();
 131  
     }
 132  
 
 133  
     public Class<?> getCommonPropertyType(final ELContext context, final Object base) {
 134  
         
 135  0
         if (base != null) return null;
 136  
         
 137  0
         return String.class;
 138  
     }
 139  
     
 140  
     // get the FacesContext from the ELContext
 141  
     private static FacesContext facesContext(final ELContext context) {
 142  0
         return (FacesContext)context.getContext(FacesContext.class);
 143  
     }
 144  
 
 145  
     private static ResourceBundle getResourceBundle(final ELContext context, final String property) {
 146  0
         final FacesContext facesContext = facesContext(context);
 147  0
         if (facesContext != null) {
 148  0
             final Application application = facesContext.getApplication();
 149  0
             return application.getResourceBundle(facesContext, property);
 150  
         }
 151  
         
 152  0
         return null;
 153  
     }
 154  
     
 155  
     protected RuntimeConfig runtimeConfig(ELContext context) {
 156  0
         final FacesContext facesContext = facesContext(context);
 157  
         
 158  
         // application-level singleton - we can safely cache this
 159  0
         if (this.runtimeConfig == null) {
 160  0
             this.runtimeConfig = RuntimeConfig.getCurrentInstance(facesContext.getExternalContext());
 161  
         }
 162  
         
 163  0
         return runtimeConfig;
 164  
     }
 165  
 
 166  
     private static FeatureDescriptor makeDescriptor(org.apache.myfaces.config.impl.digester.elements.ResourceBundle bundle) {
 167  0
         final FeatureDescriptor fd = new FeatureDescriptor();
 168  0
         fd.setValue(ELResolver.RESOLVABLE_AT_DESIGN_TIME, Boolean.TRUE);
 169  0
         fd.setName(bundle.getVar());
 170  0
         fd.setDisplayName(bundle.getDisplayName());
 171  0
         fd.setValue(ELResolver.TYPE, ResourceBundle.class);
 172  0
         fd.setShortDescription("");
 173  0
         fd.setExpert(false);
 174  0
         fd.setHidden(false);
 175  0
         fd.setPreferred(true);
 176  0
         return fd;
 177  
     }
 178  
 }