View Javadoc

1   /*
2    * Copyright 2002,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  
17  package org.apache.struts.faces.renderer;
18  
19  
20  import java.util.ArrayList;
21  import java.util.Iterator;
22  
23  import javax.faces.component.UIComponent;
24  import javax.faces.component.UIParameter;
25  import javax.faces.component.ValueHolder;
26  import javax.faces.context.FacesContext;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.apache.struts.Globals;
31  import org.apache.struts.util.MessageResources;
32  import org.apache.struts.util.ResponseUtils;
33  
34  
35  /***
36   * <p><code>Renderer</code> implementation for the <code>message</code> tag
37   * from the <em>Struts-Faces Integration Library</em>.</p>
38   *
39   * @version $Rev: 421138 $ $Date: 2006-07-11 22:41:40 -0700 (Tue, 11 Jul 2006) $
40   */
41  
42  public class MessageRenderer extends WriteRenderer {
43  
44  
45      // -------------------------------------------------------- Static Variables
46  
47  
48      /***
49       * <p>The <code>Log</code> instance for this class.</p>
50       */
51      private static Log log = LogFactory.getLog(MessageRenderer.class);
52  
53  
54      // ---------------------------------------------------------- Public Methods
55  
56  
57      // ------------------------------------------------------- Protected Methods
58  
59  
60      /***
61       * <p>Return the message format String to be processed for this message.
62       * </p>
63       *
64       * @param context FacesContext for the response we are creating
65       * @param component Component to be rendered
66       *
67       * @exception IllegalArgumentException if no MessageResources bundle
68       *  can be found
69       * @exception IllegalArgumentException if no message key can be found
70       */
71      protected String getText(FacesContext context, UIComponent component) {
72  
73          // Look up the MessageResources bundle to be used
74          String bundle = (String) component.getAttributes().get("bundle");
75          if (bundle == null) {
76              bundle = Globals.MESSAGES_KEY;
77          }
78          MessageResources resources = (MessageResources)
79              context.getExternalContext().getApplicationMap().get(bundle);
80          if (resources == null) { // FIXME - i18n
81              throw new IllegalArgumentException("MessageResources bundle " +
82                                                 bundle + " not found");
83          }
84  
85          // Look up the message key to be used
86          Object value = component.getAttributes().get("key");
87          if (value == null) {
88              value = ((ValueHolder) component).getValue();
89          }
90          if (value == null) { // FIXME - i18n
91              throw new NullPointerException("Component '" +
92                                             component.getClientId(context) +
93                                             "' has no current value");
94          }
95          String key = value.toString();
96  
97          // Build the substitution arguments list
98          ArrayList list = new ArrayList();
99          Iterator kids = component.getChildren().iterator();
100         while (kids.hasNext()) {
101             UIComponent kid = (UIComponent) kids.next();
102             if (!(kid instanceof UIParameter)) {
103                 continue;
104             }
105             list.add(((UIParameter) kid).getValue());
106         }
107         Object args[] = list.toArray(new Object[list.size()]);
108 
109         // Look up the requested message
110         String text = resources.getMessage(context.getViewRoot().getLocale(),
111                                            key, args);
112         Boolean filter = (Boolean) component.getAttributes().get("filter");
113         if (filter == null) {
114             filter = Boolean.FALSE;
115         }
116         if (filter.booleanValue()) {
117             return (ResponseUtils.filter(text));
118         } else {
119             return (text);
120         }
121 
122     }
123 
124 
125 }