001 package edu.nrao.sss.webapp.faces.renderer; 002 003 import javax.faces.component.UIComponent; 004 import javax.faces.context.FacesContext; 005 import javax.faces.context.ResponseWriter; 006 import java.io.IOException; 007 import java.util.Iterator; 008 009 public class RendererUtils 010 { 011 private static final String LINE_SEPARATOR = System.getProperty("line.separator", "\r\n"); 012 013 /** 014 * Renders your children recursively. Calls renderChild() on all of your children. 015 */ 016 public static void renderChildren(FacesContext facesContext, UIComponent component) throws IOException 017 { 018 ResponseWriter writer = facesContext.getResponseWriter(); 019 if (component.getChildCount() > 0) 020 { 021 for (Iterator it = component.getChildren().iterator(); it.hasNext(); ) 022 { 023 UIComponent child = (UIComponent)it.next(); 024 renderChild(facesContext, child); 025 writer.write('\n'); 026 } 027 } 028 } 029 030 031 /** 032 * Renders <code>child</code> (calls encode* methods) and if the child 033 * renders its own child, calls child.encodeChildren, other wise calls 034 * renderChildren(). 035 */ 036 public static void renderChild(FacesContext facesContext, UIComponent child) throws IOException 037 { 038 if (!child.isRendered()) 039 { 040 return; 041 } 042 043 child.encodeBegin(facesContext); 044 if (child.getRendersChildren()) 045 { 046 child.encodeChildren(facesContext); 047 } 048 else 049 { 050 renderChildren(facesContext, child); 051 } 052 child.encodeEnd(facesContext); 053 } 054 055 public static void writeLineSeparator(FacesContext context) 056 throws IOException 057 { 058 writeLineSeparator(context.getResponseWriter()); 059 } 060 061 public static void writeLineSeparator(ResponseWriter w) 062 throws IOException 063 { 064 w.write(LINE_SEPARATOR); 065 } 066 }