1 18 package org.apache.beehive.netui.tags.html; 19 20 import org.apache.beehive.netui.tags.AbstractPageError; 21 import org.apache.beehive.netui.tags.ErrorHandling; 22 import org.apache.beehive.netui.tags.IErrorReporter; 23 import org.apache.beehive.netui.tags.javascript.ScriptContainer; 24 import org.apache.beehive.netui.tags.rendering.AbstractHtmlState; 25 import org.apache.beehive.netui.tags.rendering.HtmlTag; 26 import org.apache.beehive.netui.tags.rendering.TagRenderingBase; 27 import org.apache.beehive.netui.tags.rendering.WriteRenderAppender; 28 import org.apache.struts.Globals; 29 30 import javax.servlet.ServletRequest ; 31 import javax.servlet.http.HttpServletRequest ; 32 import javax.servlet.http.HttpSession ; 33 import javax.servlet.jsp.JspException ; 34 import javax.servlet.jsp.tagext.Tag ; 35 import java.util.ArrayList ; 36 import java.util.Locale ; 37 38 49 public class Html extends ScriptContainer 50 implements IErrorReporter 51 { 52 56 public static final String HTML_TAG_ID = "netui:html"; 57 58 61 public static final String DOC_TYPE_OVERRIDE = "netui:doctype"; 62 63 66 public static final String HTML_401 = "html4-loose"; 67 68 71 public static final String HTML_401_QUIRKS = "html4-loose-quirks"; 72 73 76 public static final String XHTML_10 = "xhtml1-transitional"; 77 78 private HtmlTag.State _state = new HtmlTag.State(); 79 private TagRenderingBase _br; 80 private WriteRenderAppender _writer; 81 82 private boolean _useLocale = false; private boolean _scopeEnded = false; 84 private ArrayList _errors; private IErrorReporter _containerErrors; 87 private String _docType; 89 private int _rendering = TagRenderingBase.UNKNOWN_RENDERING; 90 91 94 public String getTagName() 95 { 96 return "Html"; 97 } 98 99 104 public int getTargetDocumentType() 105 { 106 if (_rendering != TagRenderingBase.UNKNOWN_RENDERING) 107 return _rendering; 108 109 if (_docType != null) { 110 if (_docType.equals(HTML_401)) 111 _rendering = TagRenderingBase.HTML_RENDERING; 112 else if (_docType.equals(HTML_401_QUIRKS)) 113 _rendering = TagRenderingBase.HTML_RENDERING_QUIRKS; 114 else if (_docType.equals(XHTML_10)) 115 _rendering = TagRenderingBase.XHTML_RENDERING; 116 else 117 _rendering = TagRenderingBase.getDefaultDocType(); 118 } 119 else { 120 _rendering = TagRenderingBase.getDefaultDocType(); 121 } 122 return _rendering; 123 } 124 125 127 136 public void setDir(String dir) 137 { 138 _state.dir = dir; 139 } 140 141 145 public boolean isUseLocale() 146 { 147 return _useLocale; 148 } 149 150 159 public void setUseLocale(boolean locale) 160 { 161 _useLocale = locale; 162 } 163 164 173 public void setDocumentType(String docType) 174 { 175 _docType = docType; 176 } 177 178 179 public int doStartTag() throws JspException 180 { 181 ServletRequest req = pageContext.getRequest(); 183 _containerErrors = (org.apache.beehive.netui.tags.IErrorReporter) req.getAttribute(CONTAINER_ERRORS); 184 185 req.setAttribute(HTML_TAG_ID, this); 187 188 Locale currentLocale = currentLocale(); 190 if (currentLocale == null) { 191 currentLocale = Locale.getDefault(); 192 } 193 194 _state.lang = currentLocale.getLanguage(); 196 String idScope = getRealIdScope(); 197 pushIdScope(); 198 if (idScope != null) { 199 _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL, "netui:idScope", idScope); 200 } 201 202 _writer = new WriteRenderAppender(pageContext); 203 _br = TagRenderingBase.Factory.getRendering(TagRenderingBase.HTML_TAG, req); 204 _br.doStartTag(_writer, _state); 205 _writer.append("\n"); 206 207 return EVAL_BODY_INCLUDE; 208 } 209 210 214 public int doEndTag() throws JspException 215 { 216 popIdScope(); 217 218 ErrorHandling.reportCollectedErrors(pageContext, this); 220 221 226 writeScript(_writer); 228 229 if (!_scopeEnded) 231 _writer.append("\n"); 232 _br.doEndTag(_writer); 233 localRelease(); 234 return EVAL_PAGE; 235 } 236 237 242 public void endScope(WriteRenderAppender _writer) 243 { 244 _scopeEnded = true; 247 } 248 249 253 public void addError(AbstractPageError ape) 254 { 255 assert (ape != null); 256 257 if (_containerErrors == null && _errors == null) { 259 Tag par = getParent(); 260 while (par != null) { 261 if (par instanceof org.apache.beehive.netui.tags.IErrorReporter) { 262 _containerErrors = (org.apache.beehive.netui.tags.IErrorReporter) par; 263 break; 264 } 265 par = par.getParent(); 266 } 267 } 268 269 if (_containerErrors != null) { 271 _containerErrors.addError(ape); 272 return; 273 } 274 275 if (_errors == null) { 277 _errors = new ArrayList (); 278 } 279 280 _errors.add(ape); 282 ape.errorNo = _errors.size(); 283 } 284 285 289 public ArrayList returnErrors() 290 { 291 if (_containerErrors != null) 292 return _containerErrors.returnErrors(); 293 ArrayList e = _errors; 294 _errors = null; 295 return e; 296 } 297 298 304 public boolean isReporting() 305 { 306 return true; 307 } 308 309 310 315 protected Locale currentLocale() 316 { 317 ServletRequest req = pageContext.getRequest(); 318 319 HttpSession session = pageContext.getSession(); 321 if ((_useLocale) && (session == null)) 322 session = ((HttpServletRequest ) req).getSession(); 323 if (session == null) 324 return null; 325 326 Locale current = (Locale ) session.getAttribute(Globals.LOCALE_KEY); 328 if (current != null) 329 return current; 330 331 if (!_useLocale) 333 return (null); 334 current = req.getLocale(); 335 if (current != null) 336 session.setAttribute(Globals.LOCALE_KEY, current); 337 return current; 338 } 339 340 343 protected void localRelease() 344 { 345 super.localRelease(); 346 347 _state.clear(); 348 _br = null; 349 _writer = null; 350 351 _useLocale = false; 352 _scopeEnded = false; 353 _errors = null; 354 _containerErrors = null; 355 pageContext.getRequest().removeAttribute(HTML_TAG_ID); 356 } 357 } 358 | Popular Tags |