1 18 19 package org.apache.struts.taglib.html; 20 21 import java.util.Locale ; 22 23 import javax.servlet.http.HttpServletRequest ; 24 import javax.servlet.http.HttpSession ; 25 import javax.servlet.jsp.JspException ; 26 import javax.servlet.jsp.PageContext ; 27 import javax.servlet.jsp.tagext.TagSupport ; 28 29 import org.apache.struts.Globals; 30 import org.apache.struts.taglib.TagUtils; 31 import org.apache.struts.util.MessageResources; 32 33 39 public class HtmlTag extends TagSupport { 40 41 42 44 45 48 protected static MessageResources messages = 49 MessageResources.getMessageResources(Constants.Package + ".LocalStrings"); 50 51 52 56 protected boolean locale = false; 57 58 61 public boolean getLocale() { 62 return (locale); 63 } 64 65 68 public void setLocale(boolean locale) { 69 this.locale = locale; 70 } 71 72 75 protected boolean xhtml = false; 76 77 81 protected boolean lang = false; 82 83 public boolean getXhtml() { 84 return this.xhtml; 85 } 86 87 public void setXhtml(boolean xhtml) { 88 this.xhtml = xhtml; 89 } 90 91 95 public boolean getLang() { 96 return this.lang; 97 } 98 99 103 public void setLang(boolean lang) { 104 this.lang = lang; 105 } 106 107 112 public int doStartTag() throws JspException { 113 114 TagUtils.getInstance().write(this.pageContext, this.renderHtmlStartElement()); 115 116 return EVAL_BODY_INCLUDE; 117 } 118 119 123 protected String renderHtmlStartElement() { 124 StringBuffer sb = new StringBuffer ("<html"); 125 126 String language = null; 127 String country = ""; 128 129 if (this.locale) { 130 language = this.getCurrentLocale().getLanguage(); 132 } else { 133 Locale currentLocale = 134 TagUtils.getInstance().getUserLocale(pageContext, Globals.LOCALE_KEY); 135 136 language = currentLocale.getLanguage(); 137 country = currentLocale.getCountry(); 138 } 139 140 boolean validLanguage = ((language != null) && (language.length() > 0)); 141 boolean validCountry = country.length() > 0; 142 143 if (this.xhtml) { 144 this.pageContext.setAttribute( 145 Globals.XHTML_KEY, 146 "true", 147 PageContext.PAGE_SCOPE); 148 149 sb.append(" xmlns=\"http://www.w3.org/1999/xhtml\""); 150 } 151 152 if ((this.lang || this.locale || this.xhtml) && validLanguage) { 153 sb.append(" lang=\""); 154 sb.append(language); 155 if (validCountry) { 156 sb.append("-"); 157 sb.append(country); 158 } 159 sb.append("\""); 160 } 161 162 if (this.xhtml && validLanguage) { 163 sb.append(" xml:lang=\""); 164 sb.append(language); 165 if (validCountry) { 166 sb.append("-"); 167 sb.append(country); 168 } 169 sb.append("\""); 170 } 171 172 sb.append(">"); 173 174 return sb.toString(); 175 } 176 177 178 183 public int doEndTag() throws JspException { 184 185 TagUtils.getInstance().write(pageContext, "</html>"); 186 187 return (EVAL_PAGE); 189 190 } 191 192 195 public void release() { 196 this.locale = false; 197 this.xhtml = false; 198 this.lang=false; 199 } 200 201 202 204 205 213 protected Locale getCurrentLocale() { 214 215 Locale userLocale = TagUtils.getInstance().getUserLocale(pageContext, Globals.LOCALE_KEY); 216 217 if (this.locale) { 219 HttpSession session = ((HttpServletRequest ) this.pageContext.getRequest()).getSession(); 220 session.setAttribute(Globals.LOCALE_KEY, userLocale); 221 } 222 223 return userLocale; 224 } 225 226 227 228 } 229 | Popular Tags |