1 33 34 package com.icesoft.faces.renderkit.dom_html_basic; 35 36 import com.icesoft.faces.context.DOMContext; 37 import com.icesoft.faces.util.DOMUtils; 38 import org.w3c.dom.Document ; 39 import org.w3c.dom.Element ; 40 import org.w3c.dom.Node ; 41 import org.w3c.dom.Text ; 42 43 import javax.faces.component.UIComponent; 44 import javax.faces.component.UIParameter; 45 import javax.faces.component.ValueHolder; 46 import javax.faces.context.FacesContext; 47 import java.io.IOException ; 48 import java.text.MessageFormat ; 49 import java.util.ArrayList ; 50 import java.util.Iterator ; 51 52 public class OutputMessageRenderer extends DomBasicRenderer { 53 54 public void encodeBegin(FacesContext facesContext, UIComponent uiComponent) 55 throws IOException { 56 validateParameters(facesContext, uiComponent, null); 57 } 58 59 public void encodeChildren(FacesContext facesContext, 60 UIComponent uiComponent) { 61 validateParameters(facesContext, uiComponent, null); 62 } 63 64 public void encodeEnd(FacesContext facesContext, UIComponent uiComponent) 65 throws IOException { 66 validateParameters(facesContext, uiComponent, null); 67 68 boolean spanIsRequired = false; 70 String style = (String ) uiComponent.getAttributes().get("style"); 71 String styleClass = 72 (String ) uiComponent.getAttributes().get("styleClass"); 73 if (style != null 74 || styleClass != null 75 || idNotNull(uiComponent) 76 || 77 PassThruAttributeRenderer.passThruAttributeExists(uiComponent)) { 78 spanIsRequired = true; 79 } 80 81 ArrayList uiParameterChildren = new ArrayList (); 83 Iterator allChildren = uiComponent.getChildren().iterator(); 84 while (allChildren.hasNext()) { 85 UIComponent nextChild = (UIComponent) allChildren.next(); 86 if (nextChild instanceof UIParameter) { 87 uiParameterChildren.add(((UIParameter) nextChild).getValue()); 88 } 89 } 90 91 String uiComponentValue = null; 93 if (uiComponent instanceof ValueHolder) { 94 Object uiComponentValueObject = null; 95 if ((uiComponentValueObject = 96 ((ValueHolder) uiComponent).getValue()) != null) { 97 if (uiComponentValueObject instanceof String ) { 98 uiComponentValue = (String ) uiComponentValueObject; 99 } else { 100 uiComponentValue = uiComponentValueObject.toString(); 101 } 102 } 103 } 104 105 int numberOfParameters = uiParameterChildren.size(); 108 if (numberOfParameters > 0) { 109 Object [] parameters = uiParameterChildren.toArray(); 110 uiComponentValue = 111 MessageFormat.format(uiComponentValue, parameters); 112 } 113 114 boolean escape = DOMUtils.escapeIsRequired(uiComponent); 116 if (escape && uiComponentValue != null) { 117 uiComponentValue = DOMUtils.escapeAnsi(uiComponentValue); 118 } 119 120 DOMContext domContext = 121 DOMContext.attachDOMContext(facesContext, uiComponent); 122 123 if (!domContext.isInitialized()) { 124 Document document = domContext.getDocument(); 126 Text textNode = (Text ) document.createTextNode(uiComponentValue); 127 if (spanIsRequired) { 130 Element rootSpan = domContext.createElement(HTML.SPAN_ELEM); 131 domContext.setRootNode(rootSpan); 132 if (styleClass != null) { 133 rootSpan.setAttribute("class", styleClass); 134 } 135 PassThruAttributeRenderer 136 .renderAttributes(facesContext, uiComponent, null); 137 rootSpan.appendChild(textNode); 138 } else { 139 domContext.setRootNode(textNode); 140 } 141 } else { 142 Node rootSpanOrText = domContext.getRootNode(); 145 if (rootSpanOrText instanceof Text ) { 146 ((Text ) rootSpanOrText).setData(uiComponentValue); 147 } else { 148 Node textNode = null; 149 if ((textNode = rootSpanOrText.getFirstChild()) instanceof Text ) 150 { 151 ((Text ) textNode).setData(uiComponentValue); 152 } 153 } 154 } 155 domContext.stepOver(); 156 } 157 } 158 | Popular Tags |