KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tags > javascript > ScriptRequestState


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

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 JavaDoc;
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31 import java.io.Serializable JavaDoc;
32 import java.text.MessageFormat JavaDoc;
33 import java.util.ResourceBundle JavaDoc;
34
35 /**
36  * Provides tag specific support for the HTML tags so they can create JavaScript. There is a bundle
37  * which contains all of the Java Script. This bundle will be cached inside this class. THe properties
38  * file does not get i18n support.
39  */

40 public class ScriptRequestState implements Serializable JavaDoc
41 {
42     /**
43      * This is the name of a request scoped attribute that contains the status of what JavaScript processing
44      * has taken place.
45      */

46     public static final String JavaDoc JAVASCRIPT_STATUS = "netui.javascript.status";
47
48     private static final String JavaDoc BUNDLE_NAME = "org.apache.beehive.netui.tags.javascript.javaScript";
49
50     private int _javaScriptFeatures; // this is a integer bitmap indicating various feature have been written out or not
51
private static ResourceBundle JavaDoc _bundle; // This points to the bundle
52
private ServletRequest JavaDoc _req;
53
54     //*************************** PUBLIC STATIC METHODS *****************************************
55

56     /**
57      * This method will return the <code>javaScriptUtils</code> that is assocated
58      * with this request. If this doesn't exist, it will be created before it is
59      * returned.
60      * @param request the HttpServletRequest associated with this request
61      * @return a <code>ScriptRequestState</code> assocated with the request
62      */

63     static public ScriptRequestState getScriptRequestState(HttpServletRequest JavaDoc 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     /**
77      * Returns the string specified by aKey from the errors.properties bundle.
78      * @param aKey The key for the message pattern in the bundle.
79      * @param args The args to use in the message format.
80      */

81     public static String JavaDoc getString(String JavaDoc aKey, Object JavaDoc[] args)
82     {
83         assert (aKey != null);
84
85         String JavaDoc pattern = getBundle().getString(aKey);
86         if (args == null)
87             return pattern;
88
89         MessageFormat JavaDoc format = new MessageFormat JavaDoc(pattern);
90         String JavaDoc result = format.format(args).toString();
91         return result;
92     }
93
94     ////************************** MEMBER FUNCTIONS **************************************
95

96     /**
97      * A ScriptRequestState is obtained through the factory method ScriptRequestState.getScriptRequestState()
98      * The constructor is private so that these cannot be created outside of the factory.
99      */

100     private ScriptRequestState()
101     {
102     }
103
104     public boolean isFeatureWritten(CoreScriptFeature feature)
105     {
106         return ((_javaScriptFeatures & feature.value) != 0);
107     }
108
109     /**
110      * @param scriptReporter
111      * @param results
112      * @param featureKey
113      * @param args
114      */

115     public void writeFeature(IScriptReporter scriptReporter, AbstractRenderAppender results,
116                              String JavaDoc featureKey, Object JavaDoc[] args)
117     {
118         String JavaDoc 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     /**
129      * @param scriptReporter
130      * @param results
131      * @param feature
132      * @param singleInstance
133      * @param inline
134      * @param args
135      */

136     public void writeFeature(IScriptReporter scriptReporter, AbstractRenderAppender results,
137                              CoreScriptFeature feature, boolean singleInstance, boolean inline,
138                              Object JavaDoc[] args)
139     {
140         if (singleInstance) {
141             if ((_javaScriptFeatures & feature.value) != 0)
142                 return;
143             _javaScriptFeatures |= feature.value;
144         }
145
146         // get the JavaScript to write out
147
String JavaDoc jsKey = getFeatureKey(feature);
148         String JavaDoc 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     /**
159      * @param scriptReporter
160      * @param tagId
161      * @param realId
162      * @param realName
163      * @return String
164      */

165     public String JavaDoc mapTagId(IScriptReporter scriptReporter, String JavaDoc tagId, String JavaDoc realId, String JavaDoc realName)
166     {
167         if (scriptReporter != null) {
168             scriptReporter.addTagIdMappings(tagId, realId, realName);
169             return null;
170         }
171
172         // without a scripRepoter we need to create the actual JavaScript that will be written out
173
InternalStringBuilder sb = new InternalStringBuilder(128);
174         StringBuilderRenderAppender writer = new StringBuilderRenderAppender(sb);
175         getTagIdMapping(tagId, realId, realName, writer);
176         return sb.toString();
177     }
178
179     /**
180      * This method will add a tagId and value to the ScriptRepoter TagId map.
181      * The a ScriptContainer tag will create a JavaScript table that allows
182      * the container, such as a portal, to rewrite the id so it's unique.
183      * The real name may be looked up based upon the tagId.
184      *
185      * If the no ScriptReporter is found, a script string will be returned
186      * to the caller so they can output the script block.
187      * @param tagId
188      * @param value
189      * @return String
190      */

191     public String JavaDoc mapLegacyTagId(IScriptReporter scriptReporter, String JavaDoc tagId, String JavaDoc value)
192     {
193         if (scriptReporter != null) {
194             scriptReporter.addLegacyTagIdMappings(tagId, value);
195             return null;
196         }
197
198         // without a scripRepoter we need to create the actual JavaScript that will be written out
199
InternalStringBuilder sb = new InternalStringBuilder(64);
200         StringBuilderRenderAppender writer = new StringBuilderRenderAppender(sb);
201         getTagIdMapping(tagId, value, writer);
202         return sb.toString();
203     }
204
205     /**
206      * @param scriptReporter
207      * @param writeLegacy
208      * @param writeId
209      * @param writeName
210      * @return String
211      */

212     public String JavaDoc writeNetuiNameFunctions(IScriptReporter scriptReporter, boolean writeLegacy, boolean writeId, boolean writeName)
213     {
214         // allocate a String Buffer only if there is no script reporter
215
InternalStringBuilder sb = null;
216         if (scriptReporter == null)
217             sb = new InternalStringBuilder(256);
218
219         // if we are supporting legacy javascript then output the original javascript method
220
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 we are supporting the default javascript then output the lookup methods for id and name
226
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     /**
241      * This is a static method that will write a consistent look/feel to the
242      * tags and comment markup that appears around the JavaScript.
243      * @param results the InternalStringBuilder that will have the &lt;script>
244      * tag written into
245      * @param script the JavaScript block
246      */

247     public static void writeScriptBlock(ServletRequest JavaDoc req, AbstractRenderAppender results, String JavaDoc 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     /////************************* Private Methods *******************************************
262

263     /**
264      * The ScriptRequestState is stored in a request. This is a back pointer to that request.
265      * When this is created it the pointer will be stored.
266      * @param req the Outer Request that this object is stored in
267      */

268     private void setRequest(ServletRequest JavaDoc req)
269     {
270         _req = req;
271     }
272
273     /**
274      * This will map the Features into their keys
275      * @param feature
276      * @return String
277      */

278     private String JavaDoc 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     /**
306      * Returns the resource bundle named Bundle[].properties in the
307      * package of the specified class.
308      * This is ok to cache because we don't localize the JavaScript resources.
309      */

310     private static ResourceBundle JavaDoc getBundle()
311     {
312         if (_bundle == null)
313             _bundle = ResourceBundle.getBundle(BUNDLE_NAME);
314         return _bundle;
315     }
316
317     /**
318      * This method will write out a tagId map entry for when there
319      * isn't a ScriptContainer defined.
320      * @param tagId the tagId value
321      * @param value the "real" value of the written out
322      * @param results the JavaScript that will be output
323      */

324     private void getTagIdMapping(String JavaDoc tagId, String JavaDoc value, AbstractRenderAppender results)
325     {
326         if ((_javaScriptFeatures & CoreScriptFeature.ALLOCATE_LEGACY.value) == 0) {
327             _javaScriptFeatures |= CoreScriptFeature.ALLOCATE_LEGACY.value;
328             String JavaDoc s = getString("singleIdMappingTable", new Object JavaDoc[]{tagId, value});
329             String JavaDoc meths = writeNetuiNameFunctions(null, true, false, false);
330             if (meths != null)
331                 s += meths;
332             writeScriptBlock(_req, results, s);
333         }
334         else {
335             String JavaDoc s = getString("idMappingEntry", new Object JavaDoc[]{tagId, value});
336             writeScriptBlock(_req, results, s);
337         }
338     }
339
340     /**
341      * @param tagId
342      * @param realId
343      * @param realName
344      * @param results
345      */

346     private void getTagIdMapping(String JavaDoc tagId, String JavaDoc realId, String JavaDoc 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 JavaDoc 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 JavaDoc s = getString("singleIdToNameMappingTable", new Object JavaDoc[]{tagId, realName});
362                 String JavaDoc meths = writeNetuiNameFunctions(null, false, false, true);
363                 if (meths != null)
364                     s += meths;
365                 sb.append(s);
366             }
367             else {
368                 String JavaDoc s = getString("tagIdNameMappingEntry", new Object JavaDoc[]{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 JavaDoc bundleString, int feature)
376     {
377         if ((_javaScriptFeatures & feature) != 0)
378             return;
379         _javaScriptFeatures |= feature;
380
381         String JavaDoc 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