001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package javax.el;
019:
020: import java.beans.FeatureDescriptor;
021: import java.util.ArrayList;
022: import java.util.Enumeration;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.MissingResourceException;
026: import java.util.ResourceBundle;
027:
028: public class ResourceBundleELResolver extends ELResolver {
029:
030: public ResourceBundleELResolver() {
031: super ();
032: }
033:
034: public Object getValue(ELContext context, Object base,
035: Object property) throws NullPointerException,
036: PropertyNotFoundException, ELException {
037: if (context == null) {
038: throw new NullPointerException();
039: }
040:
041: if (base instanceof ResourceBundle) {
042: context.setPropertyResolved(true);
043:
044: if (property != null) {
045: try {
046: return ((ResourceBundle) base).getObject(property
047: .toString());
048: } catch (MissingResourceException mre) {
049: return "???" + property.toString() + "???";
050: }
051: }
052: }
053:
054: return null;
055: }
056:
057: public Class<?> getType(ELContext context, Object base,
058: Object property) throws NullPointerException,
059: PropertyNotFoundException, ELException {
060: if (context == null) {
061: throw new NullPointerException();
062: }
063:
064: if (base instanceof ResourceBundle) {
065: context.setPropertyResolved(true);
066: }
067:
068: return null;
069: }
070:
071: public void setValue(ELContext context, Object base,
072: Object property, Object value) throws NullPointerException,
073: PropertyNotFoundException, PropertyNotWritableException,
074: ELException {
075: if (context == null) {
076: throw new NullPointerException();
077: }
078:
079: if (base instanceof ResourceBundle) {
080: context.setPropertyResolved(true);
081: throw new PropertyNotWritableException(message(context,
082: "resolverNotWriteable", new Object[] { base
083: .getClass().getName() }));
084: }
085: }
086:
087: public boolean isReadOnly(ELContext context, Object base,
088: Object property) throws NullPointerException,
089: PropertyNotFoundException, ELException {
090: if (context == null) {
091: throw new NullPointerException();
092: }
093:
094: if (base instanceof ResourceBundle) {
095: context.setPropertyResolved(true);
096: }
097:
098: return true;
099: }
100:
101: public Iterator getFeatureDescriptors(ELContext context, Object base) {
102: if (base instanceof ResourceBundle) {
103: List<FeatureDescriptor> feats = new ArrayList<FeatureDescriptor>();
104: Enumeration e = ((ResourceBundle) base).getKeys();
105: FeatureDescriptor feat;
106: String key;
107: while (e.hasMoreElements()) {
108: key = (String) e.nextElement();
109: feat = new FeatureDescriptor();
110: feat.setDisplayName(key);
111: feat.setExpert(false);
112: feat.setHidden(false);
113: feat.setName(key);
114: feat.setPreferred(true);
115: feat.setValue(RESOLVABLE_AT_DESIGN_TIME, Boolean.TRUE);
116: feat.setValue(TYPE, String.class);
117: feats.add(feat);
118: }
119: return feats.iterator();
120: }
121: return null;
122: }
123:
124: public Class<?> getCommonPropertyType(ELContext context, Object base) {
125: if (base instanceof ResourceBundle) {
126: return String.class;
127: }
128: return null;
129: }
130:
131: }
|