1 16 17 package org.apache.struts.faces.renderer; 18 19 20 import java.io.IOException ; 21 import java.util.Locale ; 22 import javax.faces.component.UIComponent; 23 import javax.faces.context.FacesContext; 24 import javax.faces.context.ResponseWriter; 25 import javax.servlet.http.HttpSession ; 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 import org.apache.struts.Globals; 29 30 31 37 38 public class HtmlRenderer extends AbstractRenderer { 39 40 41 43 44 47 private static Log log = LogFactory.getLog(HtmlRenderer.class); 48 49 50 52 53 63 public void encodeBegin(FacesContext context, UIComponent component) 64 throws IOException { 65 66 if ((context == null) || (component == null)) { 67 throw new NullPointerException (); 68 } 69 70 Locale currentLocale = getCurrentLocale(context, component); 71 String lang = currentLocale.getLanguage(); 72 boolean validLanguage = ((lang != null) && (lang.length() > 0)); 73 74 ResponseWriter writer = context.getResponseWriter(); 75 writer.startElement("html", component); 76 if (isXhtml(component)) { 77 writer.writeAttribute("xmlns", 79 "http://www.w3.org/1999/xhtml", null); 80 } 81 if ((isLocale(component) || isXhtml(component)) && validLanguage) { 82 writer.writeAttribute("lang", lang, null); 83 } 84 if (isXhtml(component) && validLanguage) { 85 writer.writeAttribute("xml:lang", lang, null); 86 } 87 writer.writeText("\n", null); 88 89 } 90 91 92 102 public void encodeEnd(FacesContext context, UIComponent component) 103 throws IOException { 104 105 if ((context == null) || (component == null)) { 106 throw new NullPointerException (); 107 } 108 109 ResponseWriter writer = context.getResponseWriter(); 110 writer.endElement("html"); 111 112 } 113 114 115 116 118 119 126 protected Locale getCurrentLocale 127 (FacesContext context, UIComponent component) { 128 129 if (!isLocale(component)) { 131 return (context.getExternalContext().getRequestLocale()); 132 } 133 134 HttpSession session = (HttpSession ) 136 context.getExternalContext().getSession(true); 137 138 Locale current = (Locale ) session.getAttribute(Globals.LOCALE_KEY); 140 if (current != null) { 141 return (current); 142 } 143 current = context.getExternalContext().getRequestLocale(); 144 session.setAttribute(Globals.LOCALE_KEY, current); 145 return (current); 146 147 } 148 149 150 151 156 protected boolean isLocale(UIComponent component) { 157 158 Boolean locale = (Boolean ) component.getAttributes().get("locale"); 159 if (locale != null) { 160 return locale.booleanValue(); 161 } else { 162 return (false); 163 } 164 165 } 166 167 168 173 protected boolean isXhtml(UIComponent component) { 174 175 Boolean xhtml = (Boolean ) component.getAttributes().get("xhtml"); 176 if (xhtml != null) { 177 return xhtml.booleanValue(); 178 } else { 179 return (false); 180 } 181 182 } 183 184 185 } 186 | Popular Tags |