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: package org.apache.cocoon.faces.taglib;
018:
019: import org.apache.cocoon.taglib.TagSupport;
020: import org.apache.cocoon.util.ClassUtils;
021:
022: import org.apache.cocoon.faces.FacesUtils;
023: import org.xml.sax.Attributes;
024: import org.xml.sax.SAXException;
025:
026: import javax.faces.FacesException;
027: import javax.faces.context.FacesContext;
028: import java.util.ArrayList;
029: import java.util.Collection;
030: import java.util.Enumeration;
031: import java.util.HashMap;
032: import java.util.HashSet;
033: import java.util.Map;
034: import java.util.ResourceBundle;
035: import java.util.Set;
036:
037: /**
038: * @version CVS $Id: LoadBundleTag.java 433543 2006-08-22 06:22:54Z crossley $
039: */
040: public class LoadBundleTag extends TagSupport {
041:
042: private String basename;
043: private String var;
044:
045: public void setBasename(String basename) {
046: this .basename = basename;
047: }
048:
049: public void setVar(String var) {
050: this .var = var;
051: }
052:
053: public int doStartTag(String namespaceURI, String localName,
054: String qName, Attributes atts) throws SAXException {
055: final FacesContext context = FacesUtils.getFacesContext(this ,
056: objectModel);
057:
058: final String basename = (String) FacesUtils.evaluate(context,
059: this .basename);
060:
061: final ResourceBundle bundle = ResourceBundle.getBundle(
062: basename, context.getViewRoot().getLocale(), ClassUtils
063: .getClassLoader());
064:
065: if (bundle == null) {
066: throw new FacesException("Tag <" + getClass().getName()
067: + "> " + "could not find ResourceBundle for <"
068: + basename + ">");
069: }
070:
071: context.getExternalContext().getRequestMap().put(this .var,
072: new BundleMap(bundle));
073:
074: return SKIP_BODY;
075: }
076:
077: public void recycle() {
078: super .recycle();
079: this .basename = null;
080: this .var = null;
081: }
082: }
083:
084: class BundleMap implements Map {
085: private ResourceBundle bundle;
086:
087: public BundleMap(ResourceBundle bundle) {
088: this .bundle = bundle;
089: }
090:
091: public void clear() {
092: throw new UnsupportedOperationException(
093: "BundleMap does not support clear()");
094: }
095:
096: public boolean containsKey(Object key) {
097: if (key == null) {
098: return false;
099: }
100:
101: try {
102: this .bundle.getObject(key.toString());
103: return true;
104: } catch (Exception e) {
105: return false;
106: }
107: }
108:
109: public boolean containsValue(Object value) {
110: if (value == null) {
111: return false;
112: }
113:
114: for (Enumeration i = this .bundle.getKeys(); i.hasMoreElements();) {
115: Object obj = bundle.getObject((String) i.nextElement());
116: if (value == obj || value.equals(obj)) {
117: return true;
118: }
119: }
120:
121: return false;
122: }
123:
124: public Set entrySet() {
125: final HashMap entries = new HashMap();
126: for (Enumeration i = this .bundle.getKeys(); i.hasMoreElements();) {
127: String key = (String) i.nextElement();
128: entries.put(key, this .bundle.getObject(key));
129: }
130:
131: return entries.entrySet();
132: }
133:
134: public boolean equals(Object obj) {
135: if (obj == null || !(obj instanceof Map)) {
136: return false;
137: }
138:
139: return entrySet().equals(((Map) obj).entrySet());
140: }
141:
142: public Object get(Object key) {
143: if (key == null) {
144: return null;
145: }
146:
147: return bundle.getObject(key.toString());
148: }
149:
150: public int hashCode() {
151: return this .bundle.hashCode();
152: }
153:
154: public boolean isEmpty() {
155: return !this .bundle.getKeys().hasMoreElements();
156: }
157:
158: public Set keySet() {
159: final Set keys = new HashSet();
160: for (Enumeration i = this .bundle.getKeys(); i.hasMoreElements();) {
161: keys.add(i.nextElement());
162: }
163: return keys;
164: }
165:
166: public Object put(Object k, Object v) {
167: throw new UnsupportedOperationException(
168: "BundleMap does not support put()");
169: }
170:
171: public void putAll(Map t) {
172: throw new UnsupportedOperationException(
173: "BundleMap does not support putAll()");
174: }
175:
176: public Object remove(Object k) {
177: throw new UnsupportedOperationException(
178: "BundleMap does not support remove()");
179: }
180:
181: public int size() {
182: int result = 0;
183: for (Enumeration i = this .bundle.getKeys(); i.hasMoreElements();) {
184: i.nextElement();
185: result++;
186: }
187:
188: return result;
189: }
190:
191: public Collection values() {
192: ArrayList values = new ArrayList();
193: for (Enumeration i = this .bundle.getKeys(); i.hasMoreElements();) {
194: values.add(this .bundle.getObject((String) i.nextElement()));
195: }
196: return values;
197: }
198: }
|