001: /*
002: * Copyright (c) 1998-2008 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 com.caucho.jsf.el;
031:
032: import com.caucho.el.AbstractVariableResolver;
033: import com.caucho.jsp.el.*;
034: import com.caucho.jsp.BundleManager;
035: import com.caucho.jsf.cfg.*;
036:
037: import javax.el.*;
038: import javax.faces.context.*;
039: import javax.servlet.jsp.jstl.fmt.LocalizationContext;
040: import java.beans.FeatureDescriptor;
041: import java.util.*;
042:
043: /**
044: * Variable resolution for JSF variables
045: */
046: public class JsfResourceBundleELResolver extends ELResolver {
047: private BundleManager _bundleManager = BundleManager.create();
048:
049: private HashMap<String, ResourceBundleConfig> _bundleMap = new HashMap<String, ResourceBundleConfig>();
050:
051: public JsfResourceBundleELResolver() {
052: }
053:
054: public void addBundle(String var, ResourceBundleConfig bundle) {
055: _bundleMap.put(var, bundle);
056: }
057:
058: @Override
059: public Class<?> getCommonPropertyType(ELContext env, Object base) {
060: if (base != null)
061: return null;
062: else
063: return ResourceBundle.class;
064: }
065:
066: @Override
067: public Iterator<FeatureDescriptor> getFeatureDescriptors(
068: ELContext env, Object base) {
069: if (base != null)
070: return null;
071:
072: ArrayList<FeatureDescriptor> descriptors = new ArrayList<FeatureDescriptor>();
073:
074: for (ResourceBundleConfig bundle : _bundleMap.values()) {
075: FeatureDescriptor desc = new FeatureDescriptor();
076: desc.setName(bundle.getVar());
077:
078: if (bundle.getDisplayName() != null)
079: desc.setDisplayName(bundle.getDisplayName());
080: else
081: desc.setDisplayName(bundle.getVar());
082:
083: desc.setExpert(false);
084: desc.setHidden(false);
085: desc.setPreferred(true);
086: desc.setValue(ELResolver.RESOLVABLE_AT_DESIGN_TIME,
087: Boolean.TRUE);
088: desc.setValue(ELResolver.TYPE, ResourceBundle.class);
089:
090: descriptors.add(desc);
091: }
092:
093: return descriptors.iterator();
094: }
095:
096: @Override
097: public Class getType(ELContext env, Object base, Object property) {
098: if (base == null && property instanceof String) {
099: String var = (String) property;
100:
101: ResourceBundleConfig bundle = _bundleMap.get(var);
102:
103: if (bundle != null) {
104: env.setPropertyResolved(true);
105:
106: return ResourceBundle.class;
107: }
108: }
109:
110: return null;
111: }
112:
113: @Override
114: public Object getValue(ELContext env, Object base, Object property) {
115: if (base == null && property instanceof String) {
116: String var = (String) property;
117:
118: ResourceBundleConfig bundle = _bundleMap.get(var);
119:
120: if (bundle != null) {
121: env.setPropertyResolved(true);
122:
123: FacesContext facesContext = FacesContext
124: .getCurrentInstance();
125: Locale locale = null;
126:
127: if (facesContext != null)
128: locale = facesContext.getViewRoot().getLocale();
129:
130: LocalizationContext lc;
131:
132: lc = _bundleManager.getBundle(bundle.getBaseName(),
133: locale);
134:
135: if (lc == null)
136: lc = _bundleManager.getBundle(bundle.getBaseName());
137:
138: if (lc != null)
139: return lc.getResourceBundle();
140: else
141: return null;
142: }
143: }
144:
145: return null;
146: }
147:
148: @Override
149: public boolean isReadOnly(ELContext env, Object base,
150: Object property) {
151: if (base == null && property instanceof String) {
152: String var = (String) property;
153:
154: ResourceBundleConfig bundle = _bundleMap.get(var);
155:
156: if (bundle != null) {
157: env.setPropertyResolved(true);
158:
159: return true;
160: }
161: }
162:
163: return false;
164: }
165:
166: public void setValue(ELContext env, Object base, Object property,
167: Object value) {
168: if (base == null && property instanceof String) {
169: String var = (String) property;
170:
171: ResourceBundleConfig bundle = _bundleMap.get(var);
172:
173: if (bundle != null) {
174: env.setPropertyResolved(true);
175:
176: throw new PropertyNotWritableException();
177: }
178: }
179: }
180: }
|