1 10 package org.mmbase.bridge.jsp.taglib; 11 12 import org.mmbase.bridge.jsp.taglib.util.Attribute; 13 import org.mmbase.bridge.Cloud; 14 15 import java.io.IOException ; 16 import javax.servlet.jsp.JspTagException ; 17 import javax.servlet.jsp.PageContext ; 18 import java.util.*; 19 20 import org.mmbase.util.logging.Logger; 21 import org.mmbase.util.logging.Logging; 22 23 29 30 public class LocaleTag extends CloudReferrerTag { 31 private static final Logger log = Logging.getLoggerInstance(LocaleTag.class); 32 33 public static final String KEY = "javax.servlet.jsp.jstl.fmt.locale.request"; 34 public static final String TZ_KEY = "org.mmbase.timezone"; 35 public static final int SCOPE = PageContext.REQUEST_SCOPE; 36 private Attribute language = Attribute.NULL; 37 private Attribute country = Attribute.NULL; 38 private Attribute variant = Attribute.NULL; 39 40 private Attribute timezone = Attribute.NULL; 41 42 protected Locale locale; 43 protected Locale prevLocale = null; 44 protected Cloud cloud; 45 private String jspvar = null; 46 47 50 public void setLanguage(String lang) throws JspTagException { 51 language = getAttribute(lang); 52 } 53 54 public void setCountry(String c) throws JspTagException { 55 country = getAttribute(c); 56 } 57 58 public void setVariant(String v) throws JspTagException { 59 variant = getAttribute(v); 60 } 61 64 public void setTimezone(String t) throws JspTagException { 65 timezone = getAttribute(t); 66 } 67 68 71 public Locale getLocale() { 72 return locale; 76 } 77 78 public void setJspvar(String j) { 79 jspvar = j; 80 } 81 82 83 public int doStartTag() throws JspTagException { 84 determineLocale(); 85 if (locale != null) { 86 if (jspvar != null) { 87 pageContext.setAttribute(jspvar, locale); 88 } 89 prevLocale = (Locale) pageContext.findAttribute(KEY); 92 pageContext.setAttribute(KEY, locale, SCOPE); 93 CloudProvider cloudProvider = findCloudProvider(false); 94 if (cloudProvider != null) { 95 cloud = cloudProvider.getCloudVar(); 96 prevLocale = cloud.getLocale(); 97 cloud.setLocale(locale); 98 } else { 99 cloud = null; 100 } 101 } 102 String tz = timezone.getString(this); 103 if (timezone != null && ! tz.equals("")) { 104 pageContext.setAttribute(TZ_KEY, TimeZone.getTimeZone(tz), SCOPE); 105 } else { 106 if (pageContext.getAttribute(TZ_KEY, SCOPE) == null) { 107 pageContext.setAttribute(TZ_KEY, org.mmbase.bridge.ContextProvider.getDefaultCloudContext().getDefaultTimeZone(), SCOPE); 108 } 109 } 110 return EVAL_BODY; 111 } 112 113 116 protected void determineLocale() throws JspTagException { 117 determineLocaleFromAttributes(); 118 if (locale == null) { 119 determineFromCloudProvider(); 120 } 121 if (locale == null) { 122 locale = org.mmbase.bridge.ContextProvider.getDefaultCloudContext().getDefaultLocale(); 123 } 124 } 125 126 129 protected void determineFromCloudProvider() throws JspTagException { 130 CloudProvider cloudProvider = findCloudProvider(false); 131 if (cloudProvider != null) { 132 locale = cloudProvider.getCloudVar().getLocale(); 133 } 134 } 135 136 139 protected void determineLocaleFromAttributes() throws JspTagException { 140 String l = language.getString(this); 141 if (! l.equals("")) { 142 if (l.equalsIgnoreCase("client")) { 143 locale = pageContext.getRequest().getLocale(); 144 } else { 145 String c = country.getString(this); 146 if ("".equals(c)) { 147 locale = org.mmbase.util.LocalizedString.getLocale(l); 148 } else { 149 locale = new Locale(l, c, variant.getString(this)); 150 } 151 } 152 } 153 } 154 155 public int doAfterBody() throws JspTagException { 156 if (EVAL_BODY == EVAL_BODY_BUFFERED) { 157 if (bodyContent != null) { 158 try { 159 bodyContent.writeOut(bodyContent.getEnclosingWriter()); 160 } catch (IOException ioe){ 161 throw new TaglibException(ioe); 162 } 163 } 164 } 165 return SKIP_BODY; 166 } 167 168 public int doEndTag() throws JspTagException { 169 if (prevLocale != null) { 170 pageContext.setAttribute(KEY, prevLocale, SCOPE); 171 if (cloud != null) { 172 cloud.setLocale(prevLocale); 173 } 174 } else { 175 pageContext.removeAttribute(KEY, SCOPE); 176 } 177 cloud = null; 178 return super.doEndTag(); 179 } 180 181 public void doFinally() { 182 cloud = null; 183 locale = null; 184 prevLocale = null; 185 jspvar = null; 186 super.doFinally(); 187 } 188 } 189 190 | Popular Tags |