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.RequestUtils; 23 import org.apache.beehive.netui.tags.TagConfig; 24 import org.apache.beehive.netui.tags.rendering.AbstractRenderAppender; 25 import org.apache.beehive.netui.tags.rendering.ScriptTag; 26 import org.apache.beehive.netui.tags.rendering.StringBuilderRenderAppender; 27 import org.apache.beehive.netui.tags.rendering.TagRenderingBase; 28 29 import javax.servlet.ServletRequest ; 30 import javax.servlet.http.HttpServletRequest ; 31 import java.io.Serializable ; 32 import java.text.MessageFormat ; 33 import java.util.ResourceBundle ; 34 35 40 public class ScriptRequestState implements Serializable  41 { 42 46 public static final String JAVASCRIPT_STATUS = "netui.javascript.status"; 47 48 private static final String BUNDLE_NAME = "org.apache.beehive.netui.tags.javascript.javaScript"; 49 50 private int _javaScriptFeatures; private static ResourceBundle _bundle; private ServletRequest _req; 53 54 56 63 static public ScriptRequestState getScriptRequestState(HttpServletRequest request) 64 { 65 assert (request != null); 66 ScriptRequestState srs = (ScriptRequestState) RequestUtils.getOuterAttribute(request, JAVASCRIPT_STATUS); 67 if (srs == null) { 68 srs = new ScriptRequestState(); 69 srs.setRequest(request); 70 RequestUtils.setOuterAttribute(request, JAVASCRIPT_STATUS, srs); 71 } 72 assert (srs != null); 73 return srs; 74 } 75 76 81 public static String getString(String aKey, Object [] args) 82 { 83 assert (aKey != null); 84 85 String pattern = getBundle().getString(aKey); 86 if (args == null) 87 return pattern; 88 89 MessageFormat format = new MessageFormat (pattern); 90 String result = format.format(args).toString(); 91 return result; 92 } 93 94 96 100 private ScriptRequestState() 101 { 102 } 103 104 public boolean isFeatureWritten(CoreScriptFeature feature) 105 { 106 return ((_javaScriptFeatures & feature.value) != 0); 107 } 108 109 115 public void writeFeature(IScriptReporter scriptReporter, AbstractRenderAppender results, 116 String featureKey, Object [] args) 117 { 118 String s = getString(featureKey, args); 119 120 if (scriptReporter != null) { 121 scriptReporter.addScriptFunction(null, s); 122 } 123 else { 124 writeScriptBlock(_req, results, s); 125 } 126 } 127 128 136 public void writeFeature(IScriptReporter scriptReporter, AbstractRenderAppender results, 137 CoreScriptFeature feature, boolean singleInstance, boolean inline, 138 Object [] args) 139 { 140 if (singleInstance) { 141 if ((_javaScriptFeatures & feature.value) != 0) 142 return; 143 _javaScriptFeatures |= feature.value; 144 } 145 146 String jsKey = getFeatureKey(feature); 148 String s = getString(jsKey, args); 149 150 if (inline || scriptReporter == null) { 151 writeScriptBlock(_req, results, s); 152 return; 153 } 154 155 scriptReporter.addScriptFunction(null, s); 156 } 157 158 165 public String mapTagId(IScriptReporter scriptReporter, String tagId, String realId, String realName) 166 { 167 if (scriptReporter != null) { 168 scriptReporter.addTagIdMappings(tagId, realId, realName); 169 return null; 170 } 171 172 InternalStringBuilder sb = new InternalStringBuilder(128); 174 StringBuilderRenderAppender writer = new StringBuilderRenderAppender(sb); 175 getTagIdMapping(tagId, realId, realName, writer); 176 return sb.toString(); 177 } 178 179 191 public String mapLegacyTagId(IScriptReporter scriptReporter, String tagId, String value) 192 { 193 if (scriptReporter != null) { 194 scriptReporter.addLegacyTagIdMappings(tagId, value); 195 return null; 196 } 197 198 InternalStringBuilder sb = new InternalStringBuilder(64); 200 StringBuilderRenderAppender writer = new StringBuilderRenderAppender(sb); 201 getTagIdMapping(tagId, value, writer); 202 return sb.toString(); 203 } 204 205 212 public String writeNetuiNameFunctions(IScriptReporter scriptReporter, boolean writeLegacy, boolean writeId, boolean writeName) 213 { 214 InternalStringBuilder sb = null; 216 if (scriptReporter == null) 217 sb = new InternalStringBuilder(256); 218 219 if (TagConfig.isLegacyJavaScript() && writeLegacy) { 221 writeLookupMethod(scriptReporter, sb, "getNetuiTagNameAdvanced", CoreScriptFeature.LEGACY_LOOKUP.value); 222 writeLookupMethod(scriptReporter, sb, "getScopeId", CoreScriptFeature.LEGACY_SCOPE_LOOKUP.value); 223 } 224 225 if (TagConfig.isDefaultJavaScript()) { 227 if (writeId) 228 writeLookupMethod(scriptReporter, sb, "lookupIdByTagId", CoreScriptFeature.ID_LOOKUP.value); 229 230 if (writeName) 231 writeLookupMethod(scriptReporter, sb, "lookupNameByTagId", CoreScriptFeature.NAME_LOOKUP.value); 232 233 if (writeId || writeName) 234 writeLookupMethod(scriptReporter, sb, "lookupScopeId", CoreScriptFeature.SCOPE_LOOKUP.value); 235 } 236 237 return (sb != null) ? sb.toString() : null; 238 } 239 240 247 public static void writeScriptBlock(ServletRequest req, AbstractRenderAppender results, String script) 248 { 249 assert(results != null); 250 ScriptTag.State state = new ScriptTag.State(); 251 state.suppressComments = false; 252 ScriptTag br = (ScriptTag) TagRenderingBase.Factory.getRendering(TagRenderingBase.SCRIPT_TAG, req); 253 254 results.append("\n"); 255 br.doStartTag(results, state); 256 results.append(script); 257 br.doEndTag(results, false); 258 results.append("\n"); 259 } 260 261 263 268 private void setRequest(ServletRequest req) 269 { 270 _req = req; 271 } 272 273 278 private String getFeatureKey(CoreScriptFeature feature) 279 { 280 switch (feature.getIntValue()) { 281 case CoreScriptFeature.INT_ANCHOR_SUBMIT: 282 return "anchorFormSubmit"; 283 case CoreScriptFeature.INT_SET_FOCUS: 284 return "setFocus"; 285 case CoreScriptFeature.INT_POPUP_DONE: 286 return "popupDone"; 287 case CoreScriptFeature.INT_ROLLOVER: 288 return "rollover"; 289 case CoreScriptFeature.INT_TREE_INIT: 290 return "initTree"; 291 case CoreScriptFeature.INT_DIVPANEL_INIT: 292 return "initDivPanel"; 293 case CoreScriptFeature.INT_DYNAMIC_INIT: 294 return "writeWebAppName"; 295 case CoreScriptFeature.INT_BUTTON_DISABLE_AND_SUBMIT: 296 return "buttonDisableAndSubmitForm"; 297 case CoreScriptFeature.INT_BUTTON_DISABLE: 298 return "buttonDisable"; 299 } 300 assert(false) : "getFeature fell through on feature:" + feature; 301 return null; 302 } 303 304 305 310 private static ResourceBundle getBundle() 311 { 312 if (_bundle == null) 313 _bundle = ResourceBundle.getBundle(BUNDLE_NAME); 314 return _bundle; 315 } 316 317 324 private void getTagIdMapping(String tagId, String value, AbstractRenderAppender results) 325 { 326 if ((_javaScriptFeatures & CoreScriptFeature.ALLOCATE_LEGACY.value) == 0) { 327 _javaScriptFeatures |= CoreScriptFeature.ALLOCATE_LEGACY.value; 328 String s = getString("singleIdMappingTable", new Object []{tagId, value}); 329 String meths = writeNetuiNameFunctions(null, true, false, false); 330 if (meths != null) 331 s += meths; 332 writeScriptBlock(_req, results, s); 333 } 334 else { 335 String s = getString("idMappingEntry", new Object []{tagId, value}); 336 writeScriptBlock(_req, results, s); 337 } 338 } 339 340 346 private void getTagIdMapping(String tagId, String realId, String realName, AbstractRenderAppender results) 347 { 348 InternalStringBuilder sb = new InternalStringBuilder(128); 349 if (realId != null) { 350 if ((_javaScriptFeatures & CoreScriptFeature.ALLOCATE_ID.value) == 0) { 351 _javaScriptFeatures |= CoreScriptFeature.ALLOCATE_ID.value; 352 String meths = writeNetuiNameFunctions(null, false, true, false); 353 if (meths != null) 354 sb.append(meths); 355 } 356 } 357 358 if (realName != null) { 359 if ((_javaScriptFeatures & CoreScriptFeature.ALLOCATE_NAME.value) == 0) { 360 _javaScriptFeatures |= CoreScriptFeature.ALLOCATE_NAME.value; 361 String s = getString("singleIdToNameMappingTable", new Object []{tagId, realName}); 362 String meths = writeNetuiNameFunctions(null, false, false, true); 363 if (meths != null) 364 s += meths; 365 sb.append(s); 366 } 367 else { 368 String s = getString("tagIdNameMappingEntry", new Object []{tagId, realName}); 369 sb.append(s); 370 } 371 } 372 writeScriptBlock(_req, results, sb.toString()); 373 } 374 375 private void writeLookupMethod(IScriptReporter scriptReporter, InternalStringBuilder sb, String bundleString, int feature) 376 { 377 if ((_javaScriptFeatures & feature) != 0) 378 return; 379 _javaScriptFeatures |= feature; 380 381 String s = getString(bundleString, null); 382 if (scriptReporter != null) 383 scriptReporter.addScriptFunction(null, s); 384 else { 385 sb.append(s); 386 sb.append("\n"); 387 } 388 } 389 } 390 | Popular Tags |