001: /*
002: * $Id: MessageRenderer.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: package org.apache.struts.faces.renderer;
023:
024: import java.util.ArrayList;
025: import java.util.Iterator;
026:
027: import javax.faces.component.UIComponent;
028: import javax.faces.component.UIParameter;
029: import javax.faces.component.ValueHolder;
030: import javax.faces.context.FacesContext;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.apache.struts.Globals;
035: import org.apache.struts.util.MessageResources;
036: import org.apache.struts.util.ResponseUtils;
037:
038: /**
039: * <p><code>Renderer</code> implementation for the <code>message</code> tag
040: * from the <em>Struts-Faces Integration Library</em>.</p>
041: *
042: * @version $Rev: 471754 $ $Date: 2006-11-06 08:55:09 -0600 (Mon, 06 Nov 2006) $
043: */
044:
045: public class MessageRenderer extends WriteRenderer {
046:
047: // -------------------------------------------------------- Static Variables
048:
049: /**
050: * <p>The <code>Log</code> instance for this class.</p>
051: */
052: private static Log log = LogFactory.getLog(MessageRenderer.class);
053:
054: // ---------------------------------------------------------- Public Methods
055:
056: // ------------------------------------------------------- Protected Methods
057:
058: /**
059: * <p>Return the message format String to be processed for this message.
060: * </p>
061: *
062: * @param context FacesContext for the response we are creating
063: * @param component Component to be rendered
064: *
065: * @exception IllegalArgumentException if no MessageResources bundle
066: * can be found
067: * @exception IllegalArgumentException if no message key can be found
068: */
069: protected String getText(FacesContext context, UIComponent component) {
070:
071: // Look up the MessageResources bundle to be used
072: String bundle = (String) component.getAttributes()
073: .get("bundle");
074: if (bundle == null) {
075: bundle = Globals.MESSAGES_KEY;
076: }
077: MessageResources resources = (MessageResources) context
078: .getExternalContext().getApplicationMap().get(bundle);
079: if (resources == null) { // FIXME - i18n
080: throw new IllegalArgumentException(
081: "MessageResources bundle " + bundle + " not found");
082: }
083:
084: // Look up the message key to be used
085: Object value = component.getAttributes().get("key");
086: if (value == null) {
087: value = ((ValueHolder) component).getValue();
088: }
089: if (value == null) { // FIXME - i18n
090: throw new NullPointerException("Component '"
091: + component.getClientId(context)
092: + "' has no current value");
093: }
094: String key = value.toString();
095:
096: // Build the substitution arguments list
097: ArrayList list = new ArrayList();
098: Iterator kids = component.getChildren().iterator();
099: while (kids.hasNext()) {
100: UIComponent kid = (UIComponent) kids.next();
101: if (!(kid instanceof UIParameter)) {
102: continue;
103: }
104: list.add(((UIParameter) kid).getValue());
105: }
106: Object args[] = list.toArray(new Object[list.size()]);
107:
108: // Look up the requested message
109: String text = resources.getMessage(context.getViewRoot()
110: .getLocale(), key, args);
111: Boolean filter = (Boolean) component.getAttributes().get(
112: "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: }
|