1 package org.exoplatform.portlet.faces.taglib; 2 3 import java.io.IOException ; 4 import java.util.Locale ; 5 import javax.faces.component.UIComponent; 6 import javax.faces.component.UIViewRoot; 7 import javax.faces.context.*; 8 import javax.faces.el.ValueBinding; 9 import javax.faces.webapp.UIComponentBodyTag; 10 import javax.faces.webapp.UIComponentTag; 11 import javax.servlet.ServletRequest ; 12 import javax.servlet.jsp.JspException ; 13 import javax.servlet.jsp.jstl.core.Config; 14 import javax.portlet.PortletSession ; 15 import javax.portlet.RenderResponse ; 16 17 import org.apache.commons.logging.Log; 18 import org.exoplatform.services.log.LogUtil; 19 20 import com.sun.faces.util.Util; 21 22 23 public class ExoViewTag extends UIComponentBodyTag { 24 private static Log log_ = LogUtil.getLog("org.exoplatform.portlet.faces.faces"); 25 26 protected String locale; 27 28 public void setLocale(String newLocale) { locale = newLocale; } 29 30 public ExoViewTag() { locale = null; } 31 32 protected UIComponent findComponent(FacesContext context) throws JspException { 33 UIComponent comp = super.findComponent(context) ; 34 return comp ; 35 } 36 37 protected int getDoStartValue() throws JspException { 38 return 2; 39 } 40 41 public int doStartTag() throws JspException { 42 int rc = 0; 43 try { 44 rc = super.doStartTag(); 45 } catch(JspException e) { 46 throw e; 47 } catch(Throwable t) { 48 throw new JspException (t); 49 } 50 return rc; 51 } 52 53 public void doInitBody() throws javax.servlet.jsp.JspException { 54 FacesContext facesContext = FacesContext.getCurrentInstance(); 55 ResponseWriter facesWriter = facesContext.getResponseWriter(); 56 facesWriter = facesWriter.cloneWithWriter(getBodyContent()) ; 57 facesContext.setResponseWriter(facesWriter) ; 58 super.doInitBody() ; 59 } 60 61 public int doAfterBody() throws JspException { 62 String content = null; 63 try { 64 if(null == (bodyContent = getBodyContent())) { 65 Object params[] = { getClass().getName() }; 66 throw new JspException ("Body content is nulll" + params[0].toString()); 67 } 68 content = bodyContent.getString(); 69 getPreviousOut().write(content); 70 } catch(IOException iox) { 71 Object params[] = { "session", iox.getMessage() }; 72 throw new JspException ("SAVING STATE ERROR" + params[0].toString()); 73 } 74 return 6; 75 } 76 77 public int doEndTag() throws JspException { 78 int rc = super.doEndTag(); 79 FacesContext context = FacesContext.getCurrentInstance(); 80 88 PortletSession session = null; 89 if(null != (session = (PortletSession)context.getExternalContext().getSession(false))) { 90 RenderResponse response = (RenderResponse)context.getExternalContext().getResponse(); 91 session.setAttribute("javax.faces.request.charset", response.getCharacterEncoding()); 92 } 93 return rc; 94 } 95 96 public String getComponentType() { 97 throw new IllegalStateException (); 98 } 99 100 public String getRendererType() { 101 return null; 102 } 103 104 protected int getDoEndValue() throws JspException { 105 return 6; 106 } 107 108 protected void setProperties(UIComponent component) { 109 super.setProperties(component); 110 Locale viewLocale = null; 111 ValueBinding vb = null; 112 if(null != locale) { 113 if(UIComponentTag.isValueReference(locale)) { 114 component.setValueBinding("locale", vb = Util.getValueBinding(locale)); 115 Object resultLocale = vb.getValue(FacesContext.getCurrentInstance()); 116 if(resultLocale instanceof Locale ) 117 viewLocale = (Locale )resultLocale; 118 else 119 if(resultLocale instanceof String ) 120 viewLocale = getLocaleFromString((String )resultLocale); 121 } else { 122 viewLocale = getLocaleFromString(locale); 123 } 124 ((UIViewRoot)component).setLocale(viewLocale); 125 Config.set((ServletRequest )getFacesContext().getExternalContext().getRequest(), "javax.servlet.jsp.jstl.fmt.locale", viewLocale); 126 } 127 } 128 129 protected Locale getLocaleFromString(String localeExpr) { 130 Locale result = Locale.getDefault(); 131 if(localeExpr.indexOf("_") == -1 && localeExpr.indexOf("-") == -1) { 132 if(localeExpr.length() == 2) 133 result = new Locale (localeExpr, ""); 134 } else 135 if(localeExpr.length() == 5) { 136 String language = localeExpr.substring(0, 2); 137 String country = localeExpr.substring(3, localeExpr.length()); 138 result = new Locale (language, country); 139 } 140 return result; 141 } 142 } 143 | Popular Tags |