1 18 package org.apache.beehive.netui.tags.javascript; 19 20 import org.apache.beehive.netui.util.internal.InternalStringBuilder; 21 22 import org.apache.beehive.netui.tags.AbstractClassicTag; 23 import org.apache.beehive.netui.tags.TagConfig; 24 import org.apache.beehive.netui.tags.RequestUtils; 25 import org.apache.beehive.netui.tags.rendering.AbstractRenderAppender; 26 import org.apache.beehive.netui.tags.rendering.ScriptTag; 27 import org.apache.beehive.netui.tags.rendering.TagRenderingBase; 28 import org.apache.beehive.netui.tags.rendering.WriteRenderAppender; 29 import org.apache.beehive.netui.pageflow.scoping.ScopedRequest; 30 import org.apache.beehive.netui.pageflow.scoping.ScopedServletUtils; 31 32 import javax.servlet.http.HttpServletRequest ; 33 import javax.servlet.jsp.JspException ; 34 import javax.servlet.jsp.tagext.SimpleTagSupport ; 35 import javax.servlet.jsp.tagext.Tag ; 36 import javax.servlet.ServletRequest ; 37 import java.util.ArrayList ; 38 import java.util.HashMap ; 39 import java.util.Iterator ; 40 41 74 public class ScriptContainer extends AbstractClassicTag 75 implements IScriptReporter 76 { 77 public final static String SCOPE_ID = "netui:scopeId"; 78 79 private String _idScope = null; 80 private ArrayList _funcBlocks; 81 private ArrayList _codeBefore; 82 private ArrayList _codeAfter; 83 private HashMap _idMap; 84 private HashMap _idToNameMap; 85 private boolean _genScope = false; 86 private boolean _writeScript = false; 87 private boolean _writeId = false; 88 89 92 public String getTagName() 93 { 94 return "ScriptContainer"; 95 } 96 97 99 104 public void addScriptFunction(ScriptPlacement placement, String script) 105 { 106 assert (script != null) : "The paramter 'script' must not be null"; 107 IScriptReporter sr = getParentScriptReporter(); 108 if (sr != null) { 109 sr.addScriptFunction(placement, script); 110 return; 111 } 112 113 if (placement == null || placement == ScriptPlacement.PLACE_INFRAMEWORK) { 115 if (_funcBlocks == null) { 116 _funcBlocks = new ArrayList (); 117 } 118 assert (_funcBlocks != null) : "_funcBlocks should not be null"; 119 _funcBlocks.add(script); 120 } 121 else if (placement == ScriptPlacement.PLACE_BEFORE) { 122 if (_codeBefore == null) 123 _codeBefore = new ArrayList (); 124 _codeBefore.add(script); 125 } 126 else if (placement == ScriptPlacement.PLACE_AFTER) { 127 if (_codeAfter == null) 128 _codeAfter = new ArrayList (); 129 _codeAfter.add(script); 130 } 131 else { 132 assert(false) : "unsupported placement:" + placement; 133 } 134 } 135 136 141 public void addLegacyTagIdMappings(String tagId, String tagName) 142 { 143 assert (tagId != null) : "The parameter 'tagId' must not be null"; 144 assert (tagName != null) : "The parameter 'tagName' must not be null"; 145 146 if (_idMap == null) { 147 _idMap = new HashMap (); 148 } 149 150 assert (_idMap != null) : "_idMap should not be null"; 151 _idMap.put(tagId, tagName); 152 } 153 154 160 public void addTagIdMappings(String tagId, String realId, String realName) 161 { 162 assert (tagId != null) : "The parameter 'tagId' must not be null"; 163 assert (realId != null) : "The parameter 'realId' must not be null"; 164 165 _writeId = true; 166 167 if (realName != null) { 168 if (_idToNameMap == null) 169 _idToNameMap = new HashMap (); 170 _idToNameMap.put(tagId, realName); 171 } 172 } 173 174 178 public void writeScript(AbstractRenderAppender sb) 179 { 180 assert(sb != null) : "The paramter 'sb' must not be null;"; 181 if (_writeScript) 182 return; 183 184 _writeScript = true; 185 IScriptReporter sr = getParentScriptReporter(); 186 if (sr != null) { 187 sr.writeScript(sb); 188 return; 189 } 190 191 writeBeforeBlocks(sb); 192 writeFrameworkScript(sb); 193 writeAfterBlocks(sb); 194 } 195 196 198 206 public void setIdScope(String idScope) 207 { 208 _idScope = idScope; 209 } 210 211 214 public String getIdScope() 215 { 216 return _idScope; 217 218 } 219 220 229 public void setGenerateIdScope(boolean genScopeValue) 230 { 231 _genScope = genScopeValue; 232 } 233 235 public int doStartTag() 236 throws JspException 237 { 238 String scope = getRealIdScope(); 239 pushIdScope(); 240 241 WriteRenderAppender writer = new WriteRenderAppender(pageContext); 242 writeBeforeBlocks(writer); 243 244 if (_idScope != null) { 246 write("<div"); 247 write(" netui:idScope=\""); 248 write(scope); 249 write("\" "); 250 write(">"); 251 } 252 253 return EVAL_BODY_INCLUDE; 254 } 255 256 260 public int doEndTag() 261 throws JspException 262 { 263 264 popIdScope(); 265 266 WriteRenderAppender writer = new WriteRenderAppender(pageContext); 268 269 if (_idScope != null) { 271 writer.append("</div>"); 272 } 273 274 writeFrameworkScript(writer); 275 writeAfterBlocks(writer); 276 localRelease(); 277 return EVAL_PAGE; 278 } 279 280 282 protected void pushIdScope() 283 { 284 if (_idScope != null) { 285 HttpServletRequest req = (HttpServletRequest ) pageContext.getRequest(); 286 ArrayList list = (ArrayList ) RequestUtils.getOuterAttribute(req,SCOPE_ID); 287 if (list == null) { 288 list = new ArrayList (); 289 RequestUtils.setOuterAttribute(req,SCOPE_ID,list); 290 } 291 list.add(_idScope); 292 293 } 294 } 295 296 protected void popIdScope() 297 { 298 if (_idScope != null) { 299 HttpServletRequest req = (HttpServletRequest ) pageContext.getRequest(); 300 ArrayList list = (ArrayList ) RequestUtils.getOuterAttribute(req,SCOPE_ID); 301 assert(list != null); 302 list.remove(list.size() -1); 303 } 304 } 305 306 310 protected String getRealIdScope() 311 { 312 ServletRequest request = pageContext.getRequest(); 313 314 String idScope = _idScope; 316 317 if (_idScope == null && _genScope) { 319 int id = getNextId(request); 320 idScope = "n" + Integer.toString(id); 321 _idScope = idScope; 322 } 323 324 if (_idScope == null) { 326 ScopedRequest scopedRequest = ScopedServletUtils.unwrapRequest(request); 327 if (scopedRequest != null) { 328 _idScope = scopedRequest.getScopeKey().toString(); 329 idScope = _idScope; 330 } 331 } 332 333 return idScope; 334 } 335 336 protected void writeBeforeBlocks(AbstractRenderAppender sb) 337 { 338 if (_codeBefore == null || _codeBefore.size() == 0) 339 return; 340 341 InternalStringBuilder s = new InternalStringBuilder(256); 342 for (Iterator i = _codeBefore.iterator(); i.hasNext();) 343 { 344 String code = ( String ) i.next(); 345 s.append(code); 346 s.append("\n"); 347 } 348 ScriptRequestState.writeScriptBlock(pageContext.getRequest(), sb, s.toString()); 349 350 } 351 352 protected void writeAfterBlocks(AbstractRenderAppender sb) 353 { 354 if (_codeAfter == null || _codeAfter.size() == 0) 355 return; 356 357 InternalStringBuilder s = new InternalStringBuilder(256); 358 for (Iterator i = _codeAfter.iterator(); i.hasNext();) 359 { 360 String code = ( String ) i.next(); 361 s.append(code); 362 s.append("\n"); 363 } 364 ScriptRequestState.writeScriptBlock(pageContext.getRequest(), sb, s.toString()); 365 } 366 367 370 protected void writeFrameworkScript(AbstractRenderAppender sb) 371 { 372 boolean script = false; 373 ScriptRequestState jsu = ScriptRequestState.getScriptRequestState((HttpServletRequest ) pageContext.getRequest()); 374 375 boolean writeLegacy = false; 376 boolean writeName = false; 377 String val; 378 379 if (TagConfig.isLegacyJavaScript()) { 381 val = processIdMap(_idMap, "idMappingEntry", _idScope); 382 if (val != null) { 383 writeIdMap(this, "idMappingTable", val); 384 writeLegacy = true; 385 } 386 } 387 388 if (TagConfig.isDefaultJavaScript()) { 390 String idScope = getJavaScriptId(); 391 if (idScope.equals("")) 392 idScope = null; 393 val = processIdMap(_idToNameMap, "tagIdNameMappingEntry", idScope); 394 if (val != null) { 395 writeIdMap(this, "tagIdNameMappingTable", val); 396 writeName = true; 397 } 398 } 399 400 if (writeLegacy || _writeId || writeName) 401 jsu.writeNetuiNameFunctions(this, writeLegacy, _writeId, writeName); 402 403 ScriptTag.State state = null; 404 ScriptTag br = null; 405 if (_funcBlocks != null && _funcBlocks.size() > 0) { 406 if (!script) { 407 state = new ScriptTag.State(); 408 state.suppressComments = false; 409 br = (ScriptTag) TagRenderingBase.Factory.getRendering(TagRenderingBase.SCRIPT_TAG, pageContext.getRequest()); 410 br.doStartTag(sb, state); 411 script = true; 412 } 413 String s = ScriptRequestState.getString("functionComment", null); 414 sb.append(s); 415 int cnt = _funcBlocks.size(); 416 for (int i = 0; i < cnt; i++) { 417 sb.append((String )_funcBlocks.get(i)); 418 if (i != cnt - 1) { 419 sb.append("\n"); 420 } 421 } 422 } 423 424 if (script) { 425 assert(br != null); 426 br.doEndTag(sb, false); 427 } 428 } 429 430 432 438 private String writeIdMap(IScriptReporter scriptRepoter, String mapObj, String entries) 439 { 440 String s = ScriptRequestState.getString(mapObj, new Object []{entries}); 441 if (scriptRepoter != null) { 442 scriptRepoter.addScriptFunction(null, s); 443 return null; 444 } 445 return s; 446 } 447 448 449 private String getJavaScriptId() 450 { 451 String idScope = ""; 452 Tag tag = this; 453 while (tag != null) { 454 if (tag instanceof ScriptContainer) { 455 String sid = ((ScriptContainer) tag).getIdScope(); 456 if (sid != null) { 457 idScope = sid + "_" + idScope; 458 } 459 } 460 tag = tag.getParent(); 461 } 462 return idScope; 463 } 464 465 private String processIdMap(HashMap map, String mapEntry, String idScope) 466 { 467 if (map == null || map.size() == 0) 469 return null; 470 471 InternalStringBuilder results = new InternalStringBuilder(128); 472 Iterator ids = map.keySet().iterator(); 473 while (ids.hasNext()) { 474 String id = (String ) ids.next(); 475 String value = (String ) map.get(id); 476 if (idScope != null) 477 id = idScope + "__" + id; 478 String entry = ScriptRequestState.getString(mapEntry, new Object []{id, value}); 479 results.append(entry); 480 } 481 return results.toString(); 482 } 483 484 private IScriptReporter getParentScriptReporter() 486 { 487 Tag parent = getParent(); 488 if (parent == null) 489 return null; 490 IScriptReporter sr = (IScriptReporter) SimpleTagSupport.findAncestorWithClass(parent, IScriptReporter.class); 491 return sr; 492 } 493 494 497 protected void localRelease() 498 { 499 super.localRelease(); 500 501 _idScope = null; 502 _writeScript = false; 503 _genScope = false; 504 _writeId = false; 505 506 if (_funcBlocks != null) 507 _funcBlocks.clear(); 508 if (_codeBefore != null) 509 _codeBefore.clear(); 510 if (_codeAfter != null) 511 _codeAfter.clear(); 512 513 if (_idMap != null) 514 _idMap.clear(); 515 if (_idToNameMap != null) 516 _idToNameMap.clear(); 517 } 518 519 } 520 | Popular Tags |