2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shale.test.el;
19  
20  import java.util.ArrayList;
21  import java.util.Iterator;
22  import java.util.List;
23  import java.util.ResourceBundle;
24  import java.util.Map.Entry;
25  
26  import javax.el.ELContext;
27  import javax.el.PropertyNotFoundException;
28  import javax.el.PropertyNotWritableException;
29  import javax.faces.context.FacesContext;
30  
31  import org.apache.shale.test.mock.MockApplication12;
32  
33  /***
34   * <p><code>ELResolver</code> implementation that accesses resource bundles
35   * in the current application.  See the JSF 1.2 Specification, section
36   * 5.6.1.3, for requirements implemented by this class.</p>
37   *
38   * @since 1.0.4
39   */
40  public class FacesResourceBundleELResolver extends AbstractELResolver {
41      
42  
43      /***
44       * <p>Return the most general type this resolver accepts for the
45       * <code>property</code> argument.</p>
46       */
47      public Class getCommonPropertyType(ELContext context, Object base) {
48  
49          if (base != null) {
50              return null;
51          } else {
52              return String.class;
53          }
54  
55      }
56  
57  
58      /***
59       * <p>Return an <code>Iterator</code> over the attributes that this
60       * resolver knows how to deal with.</p>
61       *
62       * @param context <code>ELContext</code> for evaluating this value
63       * @param base Base object against which this evaluation occurs
64       */
65      public Iterator getFeatureDescriptors(ELContext context, Object base) {
66  
67          if (base != null) {
68              return null;
69          }
70  
71          // Create the variables we will need
72          List descriptors = new ArrayList();
73          FacesContext fcontext = (FacesContext) context.getContext(FacesContext.class);
74          MockApplication12 application =
75                  (MockApplication12) fcontext.getApplication();
76          String key = null;
77          Object value = null;
78  
79          // Create a feature descriptor for each configured resource bundle
80          Iterator entries = application.getResourceBundles().entrySet().iterator();
81          while (entries.hasNext()) {
82              Entry entry = (Entry) entries.next();
83              key = (String) entry.getKey();
84              value = entry.getValue();
85              descriptors.add(descriptor(key, key, "Resource Bundle " + key,
86                                         false, false, true,
87                                         ResourceBundle.class, true));
88          }
89  
90          // Return the accumulated descriptors
91          return descriptors.iterator();
92  
93      }
94  
95  
96      /***
97       * <p>Return the Java type of the specified property.</p>
98       *
99       * @param context <code>ELContext</code> for evaluating this value
100      * @param base Base object against which this evaluation occurs
101      *  (must be null because we are evaluating a top level variable)
102      * @param property Property name to be accessed
103      */
104     public Class getType(ELContext context, Object base, Object property) {
105 
106         if (base != null) {
107             return null;
108         }
109         if (property == null) {
110             throw new PropertyNotFoundException("No property specified");
111         }
112         FacesContext fcontext = (FacesContext) context.getContext(FacesContext.class);
113         ResourceBundle bundle =
114           fcontext.getApplication().getResourceBundle(fcontext, property.toString());
115         if (bundle != null) {
116             context.setPropertyResolved(true);
117             return ResourceBundle.class;
118         }
119         return null;
120 
121     }
122 
123 
124     /***
125      * <p>Return a resource bundle for the specified name (if any);
126      * otherwise, return <code>null</code>.</p>
127      *
128      * @param context <code>ELContext</code> for evaluating this value
129      * @param base Base object against which this evaluation occurs
130      *  (must be null because we are evaluating a top level variable)
131      * @param property Property name to be accessed
132      */
133     public Object getValue(ELContext context, Object base, Object property) {
134 
135         if (base != null) {
136             return null;
137         }
138         if (property == null) {
139             throw new PropertyNotFoundException("No property specified");
140         }
141 
142         FacesContext fcontext = (FacesContext) context.getContext(FacesContext.class);
143         ResourceBundle bundle =
144           fcontext.getApplication().getResourceBundle(fcontext, property.toString());
145         if (bundle != null) {
146             context.setPropertyResolved(true);
147             return bundle;
148         }
149         return null;
150 
151     }
152 
153 
154     /***
155      * <p>Return <code>true</code> if the specified property is read only.</p>
156      *
157      * @param context <code>ELContext</code> for evaluating this value
158      * @param base Base object against which this evaluation occurs
159      *  (must be null because we are evaluating a top level variable)
160      * @param property Property name to be accessed
161      */
162     public boolean isReadOnly(ELContext context, Object base, Object property) {
163 
164         if (base != null) {
165             return false;
166         }
167         if (property == null) {
168             throw new PropertyNotFoundException("No property specified");
169         }
170         FacesContext fcontext = (FacesContext) context.getContext(FacesContext.class);
171         ResourceBundle bundle =
172           fcontext.getApplication().getResourceBundle(fcontext, property.toString());
173         if (bundle != null) {
174             context.setPropertyResolved(true);
175             return true;
176         }
177         return false;
178 
179     }
180 
181 
182 
183     /***
184      * <p>Set the value of a scoped object for the specified name.</p>
185      *
186      * @param context <code>ELContext</code> for evaluating this value
187      * @param base Base object against which this evaluation occurs
188      *  (must be null because we are evaluating a top level variable)
189      * @param property Property name to be accessed
190      * @param value New value to be set
191      */
192     public void setValue(ELContext context, Object base, Object property, Object value) {
193 
194         if (base != null) {
195             return;
196         }
197         if (property == null) {
198             throw new PropertyNotFoundException("No property specified");
199         }
200 
201         FacesContext fcontext = (FacesContext) context.getContext(FacesContext.class);
202         ResourceBundle bundle =
203           fcontext.getApplication().getResourceBundle(fcontext, property.toString());
204         if (bundle != null) {
205             context.setPropertyResolved(true);
206             throw new PropertyNotWritableException(property.toString());
207         }
208 
209     }
210 
211 
212 }