KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > faces > taglib > LoadBundleTag


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.faces.taglib;
17
18 import org.apache.cocoon.taglib.TagSupport;
19 import org.apache.cocoon.util.ClassUtils;
20
21 import org.apache.cocoon.faces.FacesUtils;
22 import org.xml.sax.Attributes JavaDoc;
23 import org.xml.sax.SAXException JavaDoc;
24
25 import javax.faces.FacesException;
26 import javax.faces.context.FacesContext;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.Enumeration JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.HashSet JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.ResourceBundle JavaDoc;
34 import java.util.Set JavaDoc;
35
36 /**
37  * @version CVS $Id: LoadBundleTag.java 46253 2004-09-17 14:36:29Z vgritsenko $
38  */

39 public class LoadBundleTag extends TagSupport {
40
41     private String JavaDoc basename;
42     private String JavaDoc var;
43
44     public void setBasename(String JavaDoc basename) {
45         this.basename = basename;
46     }
47
48     public void setVar(String JavaDoc var) {
49         this.var = var;
50     }
51
52     public int doStartTag(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc atts) throws SAXException JavaDoc {
53         final FacesContext context = FacesUtils.getFacesContext(this, objectModel);
54
55         final String JavaDoc basename = (String JavaDoc) FacesUtils.evaluate(context, this.basename);
56
57         final ResourceBundle JavaDoc bundle = ResourceBundle.getBundle(basename,
58                                                                context.getViewRoot().getLocale(),
59                                                                ClassUtils.getClassLoader());
60
61         if (bundle == null) {
62             throw new FacesException("Tag <" + getClass().getName() + "> " +
63                                      "could not find ResourceBundle for <" + basename + ">");
64         }
65
66         context.getExternalContext().getRequestMap().put(this.var, new BundleMap(bundle));
67
68         return SKIP_BODY;
69     }
70
71     public void recycle() {
72         super.recycle();
73         this.basename = null;
74         this.var = null;
75     }
76 }
77
78 class BundleMap implements Map JavaDoc {
79     private ResourceBundle JavaDoc bundle;
80
81     public BundleMap (ResourceBundle JavaDoc bundle) {
82         this.bundle = bundle;
83     }
84
85     public void clear() {
86         throw new UnsupportedOperationException JavaDoc("BundleMap does not support clear()");
87     }
88
89     public boolean containsKey(Object JavaDoc key) {
90         if (key == null) {
91             return false;
92         }
93
94         try {
95             this.bundle.getObject(key.toString());
96             return true;
97         } catch (Exception JavaDoc e) {
98             return false;
99         }
100     }
101
102     public boolean containsValue(Object JavaDoc value) {
103         if (value == null) {
104             return false;
105         }
106
107         for (Enumeration JavaDoc i = this.bundle.getKeys(); i.hasMoreElements();) {
108             Object JavaDoc obj = bundle.getObject((String JavaDoc) i.nextElement());
109             if (value == obj || value.equals(obj)) {
110                 return true;
111             }
112         }
113
114         return false;
115     }
116
117     public Set JavaDoc entrySet() {
118         final HashMap JavaDoc entries = new HashMap JavaDoc();
119         for (Enumeration JavaDoc i = this.bundle.getKeys(); i.hasMoreElements();) {
120             String JavaDoc key = (String JavaDoc) i.nextElement();
121             entries.put(key, this.bundle.getObject(key));
122         }
123
124         return entries.entrySet();
125     }
126
127     public boolean equals(Object JavaDoc obj) {
128         if (obj == null || !(obj instanceof Map JavaDoc)) {
129             return false;
130         }
131
132         return entrySet().equals(((Map JavaDoc) obj).entrySet());
133     }
134
135     public Object JavaDoc get(Object JavaDoc key) {
136         if (key == null) {
137             return null;
138         }
139
140         return bundle.getObject(key.toString());
141     }
142
143     public int hashCode() {
144         return this.bundle.hashCode();
145     }
146
147     public boolean isEmpty() {
148         return !this.bundle.getKeys().hasMoreElements();
149     }
150
151     public Set JavaDoc keySet() {
152         final Set JavaDoc keys = new HashSet JavaDoc();
153         for (Enumeration JavaDoc i = this.bundle.getKeys(); i.hasMoreElements();) {
154             keys.add(i.nextElement());
155         }
156         return keys;
157     }
158
159     public Object JavaDoc put(Object JavaDoc k, Object JavaDoc v) {
160         throw new UnsupportedOperationException JavaDoc("BundleMap does not support put()");
161     }
162
163     public void putAll(Map JavaDoc t) {
164         throw new UnsupportedOperationException JavaDoc("BundleMap does not support putAll()");
165     }
166
167     public Object JavaDoc remove(Object JavaDoc k) {
168         throw new UnsupportedOperationException JavaDoc("BundleMap does not support remove()");
169     }
170
171     public int size() {
172         int result = 0;
173         for (Enumeration JavaDoc i = this.bundle.getKeys(); i.hasMoreElements();) {
174             i.nextElement();
175             result ++;
176         }
177
178         return result;
179     }
180
181     public Collection JavaDoc values() {
182         ArrayList JavaDoc values = new ArrayList JavaDoc();
183         for (Enumeration JavaDoc i = this.bundle.getKeys(); i.hasMoreElements();) {
184             values.add(this.bundle.getObject((String JavaDoc) i.nextElement()));
185         }
186         return values;
187     }
188 }
189
Popular Tags