1 16 package org.apache.myfaces.renderkit.html; 17 18 import org.apache.myfaces.renderkit.JSFAttr; 19 import org.apache.myfaces.renderkit.RendererUtils; 20 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 24 import javax.faces.component.UIComponent; 25 import javax.faces.component.UIOutput; 26 import javax.faces.component.UIParameter; 27 import javax.faces.component.html.HtmlOutputFormat; 28 import javax.faces.context.FacesContext; 29 import java.io.IOException ; 30 import java.text.MessageFormat ; 31 import java.util.ArrayList ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 35 40 public class HtmlFormatRenderer 41 extends HtmlRenderer 42 { 43 private static final Log log = LogFactory.getLog(HtmlFormatRenderer.class); 44 45 private static final Object [] EMPTY_ARGS = new Object [0]; 46 47 public void encodeBegin(FacesContext facesContext, UIComponent uiComponent) 48 throws IOException 49 { 50 } 51 52 public void encodeChildren(FacesContext facescontext, UIComponent uicomponent) 53 throws IOException 54 { 55 } 56 57 public void encodeEnd(FacesContext facesContext, UIComponent component) 58 throws IOException 59 { 60 RendererUtils.checkParamValidity(facesContext, component, UIOutput.class); 61 62 String text = getOutputFormatText(facesContext, component); 63 boolean isEscape; 64 if (component instanceof HtmlOutputFormat) 65 { 66 isEscape = ((HtmlOutputFormat)component).isEscape(); 67 } 68 else 69 { 70 isEscape = RendererUtils.getBooleanAttribute(component, JSFAttr.ESCAPE_ATTR, true); 71 } 72 HtmlTextRendererBase.renderOutputText(facesContext, component, text, isEscape); 73 } 74 75 private String getOutputFormatText(FacesContext facesContext, 76 UIComponent htmlOutputFormat) 77 { 78 String pattern = RendererUtils.getStringValue(facesContext, htmlOutputFormat); 79 Object [] args; 80 if (htmlOutputFormat.getChildCount() == 0) 81 { 82 args = EMPTY_ARGS; 83 } 84 else 85 { 86 List argsList = new ArrayList (); 87 for (Iterator it = htmlOutputFormat.getChildren().iterator(); it.hasNext(); ) 88 { 89 UIComponent child = (UIComponent)it.next(); 90 if (child instanceof UIParameter) 91 { 92 argsList.add(((UIParameter)child).getValue()); 93 } 94 } 95 args = argsList.toArray(new Object [argsList.size()]); 96 } 97 98 MessageFormat format = new MessageFormat (pattern, facesContext.getViewRoot().getLocale()); 99 try 100 { 101 return format.format(args); 102 } 103 catch (Exception e) 104 { 105 log.error("Error formatting message of component " + htmlOutputFormat.getClientId(facesContext)); 106 return ""; 107 } 108 } 109 110 } 111 | Popular Tags |