1 18 package org.apache.beehive.netui.tags.rendering; 19 20 import org.apache.beehive.netui.util.internal.InternalStringBuilder; 21 22 import org.apache.beehive.netui.tags.html.Html; 23 import org.apache.beehive.netui.util.Bundle; 24 import org.apache.beehive.netui.util.config.ConfigUtil; 25 import org.apache.beehive.netui.util.config.bean.JspTagConfig; 26 import org.apache.beehive.netui.util.logging.Logger; 27 import org.apache.struts.util.ResponseUtils; 28 29 import javax.servlet.ServletRequest ; 30 import javax.servlet.jsp.JspException ; 31 import javax.servlet.jsp.PageContext ; 32 import java.util.HashMap ; 33 import java.util.Iterator ; 34 35 38 public abstract class TagRenderingBase 39 { 40 private static final Logger logger = Logger.getInstance(TagRenderingBase.class); 41 42 45 public static final int UNKNOWN_RENDERING = 0; 46 47 50 public static final int HTML_RENDERING = 1; 51 52 55 public static final int XHTML_RENDERING = 2; 56 57 60 public static final int HTML_RENDERING_QUIRKS = 3; 61 62 64 68 69 private static int _defaultDocType; 70 71 static 72 { 73 _defaultDocType = HTML_RENDERING_QUIRKS; 74 JspTagConfig tagConfig = ConfigUtil.getConfig().getJspTagConfig(); 75 if (tagConfig != null) { 76 JspTagConfig.Doctype.Enum docType = tagConfig.getDoctype(); 77 setDefaultDocType(docType); 78 } 79 } 80 81 public static int getDefaultDocType() 82 { 83 return _defaultDocType; 84 } 85 86 public static void setDefaultDocType(JspTagConfig.Doctype.Enum docType) 87 { 88 if (docType != null) { 89 if (docType == JspTagConfig.Doctype.HTML_4_LOOSE) 90 _defaultDocType = TagRenderingBase.HTML_RENDERING; 91 else if (docType == JspTagConfig.Doctype.HTML_4_LOOSE_QUIRKS) 92 _defaultDocType = TagRenderingBase.HTML_RENDERING_QUIRKS; 93 else if (docType== JspTagConfig.Doctype.XHTML_1_TRANSITIONAL) 94 _defaultDocType = TagRenderingBase.XHTML_RENDERING; 95 } 96 } 97 98 101 public static final Object ANCHOR_TAG = new Object (); 102 public static final Object AREA_TAG = new Object (); 103 public static final Object BASE_TAG = new Object (); 104 public static final Object BODY_TAG = new Object (); 105 public static final Object BR_TAG = new Object (); 106 public static final Object CAPTION_TAG = new Object (); 107 public static final Object FORM_TAG = new Object (); 108 public static final Object IMAGE_TAG = new Object (); 109 public static final Object INPUT_BOOLEAN_TAG = new Object (); 110 public static final Object INPUT_FILE_TAG = new Object (); 111 public static final Object INPUT_HIDDEN_TAG = new Object (); 112 public static final Object INPUT_IMAGE_TAG = new Object (); 113 public static final Object INPUT_SUBMIT_TAG = new Object (); 114 public static final Object INPUT_TEXT_TAG = new Object (); 115 public static final Object HTML_TAG = new Object (); 116 public static final Object LABEL_TAG = new Object (); 117 public static final Object OPTION_TAG = new Object (); 118 public static final Object SELECT_TAG = new Object (); 119 public static final Object SPAN_TAG = new Object (); 120 public static final Object DIV_TAG = new Object (); 121 public static final Object TABLE_TAG = new Object (); 122 public static final Object TBODY_TAG = new Object (); 123 public static final Object TD_TAG = new Object (); 124 public static final Object TEXT_AREA_TAG = new Object (); 125 public static final Object TH_TAG = new Object (); 126 public static final Object THEAD_TAG = new Object (); 127 public static final Object TFOOT_TAG = new Object (); 128 public static final Object TR_TAG = new Object (); 129 public static final Object SCRIPT_TAG = new Object (); 130 131 133 139 abstract public void doStartTag(AbstractRenderAppender sb, AbstractTagState renderState); 140 141 145 abstract public void doEndTag(AbstractRenderAppender sb); 146 147 151 protected final void renderTag(AbstractRenderAppender buf, String name) 152 { 153 assert (buf != null) : "Parameter 'buf' must not be null."; 154 assert (name != null) : "Parameter 'name' must not be null"; 155 156 buf.append("<"); 157 buf.append(name); 158 } 159 160 164 protected final void renderEndTag(AbstractRenderAppender buf, String name) 165 { 166 buf.append("</"); 167 buf.append(name); 168 buf.append(">"); 169 } 170 171 180 protected final void renderAttribute(AbstractRenderAppender buf, String name, String value) 181 { 182 assert (buf != null) : "Parameter 'buf' must not be null."; 183 assert (name != null) : "Parameter 'name' must not be null"; 184 185 if (value == null) 187 return; 188 189 buf.append(" "); 190 buf.append(name); 191 buf.append("=\""); 192 buf.append(value); 193 buf.append("\""); 194 } 195 196 201 protected final void renderAttributeSingleQuotes(AbstractRenderAppender buf, String name, String value) 202 { 203 assert (buf != null) : "Parameter 'buf' must not be null."; 204 assert (name != null) : "Parameter 'name' must not be null"; 205 206 if (value == null) 208 return; 209 210 buf.append(" "); 211 buf.append(name); 212 buf.append("='"); 213 buf.append(value); 214 buf.append("'"); 215 } 216 217 222 protected void renderAttributes(int type, AbstractRenderAppender sb, AbstractAttributeState state, boolean doubleQuote) 223 { 224 HashMap map = null; 225 switch (type) { 226 case AbstractAttributeState.ATTR_GENERAL: 227 map = state.getGeneralAttributeMap(); 228 break; 229 default: 230 String s = Bundle.getString("Tags_ParameterRenderError", 231 new Object []{new Integer (type)}); 232 logger.error(s); 233 throw new IllegalStateException (s); 234 } 235 renderGeneral(map, sb, doubleQuote); 236 } 237 238 final protected void renderAttributes(int type, AbstractRenderAppender sb, AbstractAttributeState state) 239 { 240 renderAttributes(type, sb, state, true); 241 } 242 243 246 protected void renderGeneral(HashMap map, AbstractRenderAppender sb, boolean doubleQuote) 247 { 248 if (map == null) 249 return; 250 251 Iterator iterator = map.keySet().iterator(); 252 for (; iterator.hasNext();) { 253 String key = (String ) iterator.next(); 254 if (key == null) 255 continue; 256 257 String value = (String ) map.get(key); 258 if (doubleQuote) 259 renderAttribute(sb, key, value); 260 else 261 renderAttributeSingleQuotes(sb, key, value); 262 } 263 } 264 265 protected final void write(PageContext pc, String string) 266 { 267 try { 268 ResponseUtils.write(pc, string); 269 } 270 catch (JspException e) { 271 logger.error(Bundle.getString("Tags_WriteException"), e); 272 } 273 } 274 275 282 public static class Factory 283 { 284 private static HashMap _xhtml; private static HashMap _html; private static HashMap _htmlQuirks; 288 private static ConstantRendering _xhtmlConstants; 289 private static ConstantRendering _htmlConstants; 290 291 static 293 { 294 _xhtml = new HashMap (37); 295 _html = new HashMap (37); 296 _htmlQuirks = new HashMap (37); 297 298 AnchorTag.add(_html, _htmlQuirks, _xhtml); 300 AreaTag.add(_html, _htmlQuirks, _xhtml); 301 BaseTag.add(_html, _htmlQuirks, _xhtml); 302 BodyTag.add(_html, _htmlQuirks, _xhtml); 303 CaptionTag.add(_html, _htmlQuirks, _xhtml); 304 DivTag.add(_html, _htmlQuirks, _xhtml); 305 FormTag.add(_html, _htmlQuirks, _xhtml); 306 ImageTag.add(_html, _htmlQuirks, _xhtml); 307 InputBooleanTag.add(_html, _htmlQuirks, _xhtml); 308 InputFileTag.add(_html, _htmlQuirks, _xhtml); 309 InputHiddenTag.add(_html, _htmlQuirks, _xhtml); 310 InputImageTag.add(_html, _htmlQuirks, _xhtml); 311 InputSubmitTag.add(_html, _htmlQuirks, _xhtml); 312 InputTextTag.add(_html, _htmlQuirks, _xhtml); 313 HtmlTag.add(_html, _htmlQuirks, _xhtml); 314 LabelTag.add(_html, _htmlQuirks, _xhtml); 315 OptionTag.add(_html, _htmlQuirks, _xhtml); 316 SelectTag.add(_html, _htmlQuirks, _xhtml); 317 SpanTag.add(_html, _htmlQuirks, _xhtml); 318 TableTag.add(_html, _htmlQuirks, _xhtml); 319 TBodyTag.add(_html, _htmlQuirks, _xhtml); 320 TdTag.add(_html, _htmlQuirks, _xhtml); 321 TextAreaTag.add(_html, _htmlQuirks, _xhtml); 322 ThTag.add(_html, _htmlQuirks, _xhtml); 323 THeadTag.add(_html, _htmlQuirks, _xhtml); 324 TFootTag.add(_html, _htmlQuirks, _xhtml); 325 TrTag.add(_html, _htmlQuirks, _xhtml); 326 ScriptTag.add(_html, _htmlQuirks, _xhtml); 327 } 328 329 335 public static TagRenderingBase getRendering(Object token, ServletRequest req) 336 { 337 int renderingType = _defaultDocType; 338 Integer reqRender = (Integer ) req.getAttribute(Html.DOC_TYPE_OVERRIDE); 339 if (reqRender != null) { 340 renderingType = ((Integer ) reqRender).intValue(); 341 } 342 else { 343 Html html = (Html) req.getAttribute(Html.HTML_TAG_ID); 344 if (html != null) { 346 renderingType = html.getTargetDocumentType(); 347 } 348 } 349 350 HashMap h = null; 352 switch (renderingType) { 353 case HTML_RENDERING: 354 h = _html; 355 break; 356 case HTML_RENDERING_QUIRKS: 357 h = _htmlQuirks; 358 break; 359 case XHTML_RENDERING: 360 h = _xhtml; 361 break; 362 default: 363 assert(true) : "Didn't find the map for rendering type:" + renderingType; 364 } 365 366 Object o = h.get(token); 368 assert(o != null) : "Renderer was not found for [" + token + "] rendering:" + renderingType; 369 return (TagRenderingBase) o; 370 } 371 372 377 public static boolean isXHTML(ServletRequest req) 378 { 379 Html html = (Html) req.getAttribute(Html.HTML_TAG_ID); 380 381 int renderingType = _defaultDocType; 383 if (html != null) { 384 renderingType = html.getTargetDocumentType(); 385 } 386 387 return (renderingType == XHTML_RENDERING); 388 } 389 390 394 public static ConstantRendering getConstantRendering(ServletRequest req) 395 { 396 Html html = (Html) req.getAttribute(Html.HTML_TAG_ID); 397 398 if (_htmlConstants == null) { 399 _htmlConstants = ConstantRendering.getRendering(HTML_RENDERING); 400 _xhtmlConstants = ConstantRendering.getRendering(XHTML_RENDERING); 401 } 402 403 int renderingType = TagRenderingBase.getDefaultDocType(); 405 if (html != null) { 406 renderingType = html.getTargetDocumentType(); 407 } 408 return (renderingType == XHTML_RENDERING) ? _xhtmlConstants : _htmlConstants; 409 410 } 411 } 412 413 417 public static String getAmp(ServletRequest req) 418 { 419 Html html = (Html) req.getAttribute(Html.HTML_TAG_ID); 420 421 int renderingType = HTML_RENDERING; 423 if (html != null) { 424 renderingType = html.getTargetDocumentType(); 425 } 426 427 return (renderingType == XHTML_RENDERING) ? "&" : "&"; 429 } 430 } 431 | Popular Tags |