001: /*
002: * Copyright (c) 1998-2003 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package javax.el;
031:
032: import java.beans.FeatureDescriptor;
033: import java.util.ArrayList;
034: import java.util.Enumeration;
035: import java.util.Iterator;
036: import java.util.ResourceBundle;
037: import java.util.logging.Logger;
038:
039: /**
040: * Resolves properties based on resourceBundles.
041: */
042: public class ResourceBundleELResolver extends ELResolver {
043: private final static Logger log = Logger
044: .getLogger(ResourceBundleELResolver.class.getName());
045:
046: public ResourceBundleELResolver() {
047: }
048:
049: @Override
050: public Class<?> getCommonPropertyType(ELContext context, Object base) {
051: if (base instanceof ResourceBundle)
052: return String.class;
053: else
054: return null;
055: }
056:
057: @Override
058: public Iterator<FeatureDescriptor> getFeatureDescriptors(
059: ELContext context, Object base) {
060: if (!(base instanceof ResourceBundle))
061: return null;
062:
063: ResourceBundle bundle = (ResourceBundle) base;
064:
065: ArrayList<FeatureDescriptor> list = new ArrayList<FeatureDescriptor>();
066:
067: Enumeration<String> e = bundle.getKeys();
068: while (e.hasMoreElements()) {
069: String key = e.nextElement();
070:
071: FeatureDescriptor desc = new FeatureDescriptor();
072: desc.setName(key);
073: desc.setDisplayName(key);
074: desc.setShortDescription("");
075: desc.setExpert(false);
076: desc.setHidden(false);
077: desc.setPreferred(true);
078:
079: desc.setValue(ELResolver.TYPE, String.class);
080: desc.setValue(ELResolver.RESOLVABLE_AT_DESIGN_TIME,
081: Boolean.TRUE);
082:
083: list.add(desc);
084: }
085:
086: return list.iterator();
087: }
088:
089: @Override
090: public Class<?> getType(ELContext context, Object base,
091: Object property) {
092: if (base instanceof ResourceBundle) {
093: context.setPropertyResolved(true);
094:
095: return null;
096: } else
097: return null;
098: }
099:
100: @Override
101: public Object getValue(ELContext context, Object base,
102: Object property) {
103: if (base instanceof ResourceBundle) {
104: context.setPropertyResolved(true);
105:
106: ResourceBundle bundle = (ResourceBundle) base;
107:
108: String key = String.valueOf(property);
109:
110: String value = bundle.getString(key);
111:
112: if (value != null)
113: return value;
114: else
115: return "???" + key + "???";
116: }
117:
118: return null;
119: }
120:
121: @Override
122: public boolean isReadOnly(ELContext context, Object base,
123: Object property) {
124: if (base instanceof ResourceBundle) {
125: context.setPropertyResolved(true);
126:
127: return true;
128: }
129:
130: return true;
131: }
132:
133: @Override
134: public void setValue(ELContext context, Object base,
135: Object property, Object value) {
136: if (base instanceof ResourceBundle) {
137: context.setPropertyResolved(true);
138:
139: throw new PropertyNotWritableException(String.valueOf(base));
140: }
141: }
142: }
|