/* * Copyright 2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.myfaces.renderkit.html; import org.apache.myfaces.shared_impl.renderkit.JSFAttr; import org.apache.myfaces.shared_impl.renderkit.RendererUtils; import org.apache.myfaces.shared_impl.renderkit.html.HtmlRenderer; import org.apache.myfaces.shared_impl.renderkit.html.HtmlTextRendererBase; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import javax.faces.component.UIComponent; import javax.faces.component.UIOutput; import javax.faces.component.UIParameter; import javax.faces.component.html.HtmlOutputFormat; import javax.faces.context.FacesContext; import java.io.IOException; import java.text.MessageFormat; import java.util.ArrayList; import java.util.Iterator; import java.util.List; /** * @author Manfred Geiler (latest modification by $Author$) * @author Thomas Spiegl * @version $Revision$ $Date$ */ public class HtmlFormatRenderer extends HtmlRenderer { private static final Log log = LogFactory.getLog(HtmlFormatRenderer.class); private static final Object[] EMPTY_ARGS = new Object[0]; public void encodeBegin(FacesContext facesContext, UIComponent uiComponent) throws IOException { } public void encodeChildren(FacesContext facescontext, UIComponent uicomponent) throws IOException { } public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException { RendererUtils.checkParamValidity(facesContext, component, UIOutput.class); String text = getOutputFormatText(facesContext, component); boolean isEscape; if (component instanceof HtmlOutputFormat) { isEscape = ((HtmlOutputFormat)component).isEscape(); } else { isEscape = RendererUtils.getBooleanAttribute(component, JSFAttr.ESCAPE_ATTR, true); } HtmlTextRendererBase.renderOutputText(facesContext, component, text, isEscape); } private String getOutputFormatText(FacesContext facesContext, UIComponent htmlOutputFormat) { String pattern = RendererUtils.getStringValue(facesContext, htmlOutputFormat); Object[] args; if (htmlOutputFormat.getChildCount() == 0) { args = EMPTY_ARGS; } else { List argsList = new ArrayList(); for (Iterator it = htmlOutputFormat.getChildren().iterator(); it.hasNext(); ) { UIComponent child = (UIComponent)it.next(); if (child instanceof UIParameter) { argsList.add(((UIParameter)child).getValue()); } } args = argsList.toArray(new Object[argsList.size()]); } MessageFormat format = new MessageFormat(pattern, facesContext.getViewRoot().getLocale()); try { return format.format(args); } catch (Exception e) { log.error("Error formatting message of component " + htmlOutputFormat.getClientId(facesContext)); return ""; } } }