KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tags > rendering > TagRenderingBase


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.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 JavaDoc;
30 import javax.servlet.jsp.JspException JavaDoc;
31 import javax.servlet.jsp.PageContext JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Iterator JavaDoc;
34
35 /**
36  *
37  */

38 public abstract class TagRenderingBase
39 {
40     private static final Logger logger = Logger.getInstance(TagRenderingBase.class);
41
42     /**
43      * Unknown Rendering
44      */

45     public static final int UNKNOWN_RENDERING = 0;
46
47     /**
48      * Identifier for HTML 4.01 Rendering
49      */

50     public static final int HTML_RENDERING = 1;
51
52     /**
53      * Identifier for XHTML Transitional Rendering
54      */

55     public static final int XHTML_RENDERING = 2;
56
57     /**
58      * Identifier for HTML 4.01 Rendering in Quirks mode
59      */

60     public static final int HTML_RENDERING_QUIRKS = 3;
61
62     //////////////////////////////////// Supported Rendering Constants ////////////////////////////
63

64     /**
65      * The static initializer will read the netui config file and set the doc type of there is
66      * one specified.
67      */

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     /**
99      * Token identifying the Anchor Renderer <a>
100      */

101     public static final Object JavaDoc ANCHOR_TAG = new Object JavaDoc();
102     public static final Object JavaDoc AREA_TAG = new Object JavaDoc();
103     public static final Object JavaDoc BASE_TAG = new Object JavaDoc();
104     public static final Object JavaDoc BODY_TAG = new Object JavaDoc();
105     public static final Object JavaDoc BR_TAG = new Object JavaDoc();
106     public static final Object JavaDoc CAPTION_TAG = new Object JavaDoc();
107     public static final Object JavaDoc FORM_TAG = new Object JavaDoc();
108     public static final Object JavaDoc IMAGE_TAG = new Object JavaDoc();
109     public static final Object JavaDoc INPUT_BOOLEAN_TAG = new Object JavaDoc();
110     public static final Object JavaDoc INPUT_FILE_TAG = new Object JavaDoc();
111     public static final Object JavaDoc INPUT_HIDDEN_TAG = new Object JavaDoc();
112     public static final Object JavaDoc INPUT_IMAGE_TAG = new Object JavaDoc();
113     public static final Object JavaDoc INPUT_SUBMIT_TAG = new Object JavaDoc();
114     public static final Object JavaDoc INPUT_TEXT_TAG = new Object JavaDoc();
115     public static final Object JavaDoc HTML_TAG = new Object JavaDoc();
116     public static final Object JavaDoc LABEL_TAG = new Object JavaDoc();
117     public static final Object JavaDoc OPTION_TAG = new Object JavaDoc();
118     public static final Object JavaDoc SELECT_TAG = new Object JavaDoc();
119     public static final Object JavaDoc SPAN_TAG = new Object JavaDoc();
120     public static final Object JavaDoc DIV_TAG = new Object JavaDoc();
121     public static final Object JavaDoc TABLE_TAG = new Object JavaDoc();
122     public static final Object JavaDoc TBODY_TAG = new Object JavaDoc();
123     public static final Object JavaDoc TD_TAG = new Object JavaDoc();
124     public static final Object JavaDoc TEXT_AREA_TAG = new Object JavaDoc();
125     public static final Object JavaDoc TH_TAG = new Object JavaDoc();
126     public static final Object JavaDoc THEAD_TAG = new Object JavaDoc();
127     public static final Object JavaDoc TFOOT_TAG = new Object JavaDoc();
128     public static final Object JavaDoc TR_TAG = new Object JavaDoc();
129     public static final Object JavaDoc SCRIPT_TAG = new Object JavaDoc();
130
131     //////////////////////////////////// Abstract Methods ////////////////////////////
132

133     /**
134      * Render the start tag for an element. The element will render the tag and all of it's
135      * attributes into a InternalStringBuilder.
136      * @param sb A InternalStringBuilder where the element start tag is appended.
137      * @param renderState The state assocated with the element.
138      */

139     abstract public void doStartTag(AbstractRenderAppender sb, AbstractTagState renderState);
140
141     /**
142      * Render the end tag for an element. The end tag will be rendered if the tag requires an end tag.
143      * @param sb A InternalStringBuilder where the element end tag may be appended.
144      */

145     abstract public void doEndTag(AbstractRenderAppender sb);
146
147     /**
148      * @param buf
149      * @param name
150      */

151     protected final void renderTag(AbstractRenderAppender buf, String JavaDoc 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     /**
161      * @param buf
162      * @param name
163      */

164     protected final void renderEndTag(AbstractRenderAppender buf, String JavaDoc name)
165     {
166         buf.append("</");
167         buf.append(name);
168         buf.append(">");
169     }
170
171     /**
172      * This method will append an attribute value to a InternalStringBuilder.
173      * The method assumes that the attr is not <code>null</code>. If the
174      * <code>value</code> attribute is <code>null</code> the attribute will not be appended to the
175      * <code>InternalStringBuilder</code>.
176      * @param buf The InternalStringBuilder to append the attribute into.
177      * @param name The name of the attribute
178      * @param value The value of teh attribute. If this is <code>null</code> the attribute will not be written.
179      */

180     protected final void renderAttribute(AbstractRenderAppender buf, String JavaDoc name, String JavaDoc value)
181     {
182         assert (buf != null) : "Parameter 'buf' must not be null.";
183         assert (name != null) : "Parameter 'name' must not be null";
184
185         // without a value lets skip writting this out
186
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     /**
197      * @param buf
198      * @param name
199      * @param value
200      */

201     protected final void renderAttributeSingleQuotes(AbstractRenderAppender buf, String JavaDoc name, String JavaDoc value)
202     {
203         assert (buf != null) : "Parameter 'buf' must not be null.";
204         assert (name != null) : "Parameter 'name' must not be null";
205
206         // without a value lets skip writting this out
207
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     /**
218      * Render all of the attributes defined in a map and return the string value. The attributes
219      * are rendered with in a name="value" style supported by XML.
220      * @param type an integer key indentifying the map
221      */

222     protected void renderAttributes(int type, AbstractRenderAppender sb, AbstractAttributeState state, boolean doubleQuote)
223     {
224         HashMap JavaDoc map = null;
225         switch (type) {
226             case AbstractAttributeState.ATTR_GENERAL:
227                 map = state.getGeneralAttributeMap();
228                 break;
229             default:
230                 String JavaDoc s = Bundle.getString("Tags_ParameterRenderError",
231                         new Object JavaDoc[]{new Integer JavaDoc(type)});
232                 logger.error(s);
233                 throw new IllegalStateException JavaDoc(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     /**
244      * This method will render all of the general attributes.
245      */

246     protected void renderGeneral(HashMap JavaDoc map, AbstractRenderAppender sb, boolean doubleQuote)
247     {
248         if (map == null)
249             return;
250
251         Iterator JavaDoc iterator = map.keySet().iterator();
252         for (; iterator.hasNext();) {
253             String JavaDoc key = (String JavaDoc) iterator.next();
254             if (key == null)
255                 continue;
256
257             String JavaDoc value = (String JavaDoc) 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 JavaDoc pc, String JavaDoc string)
266     {
267         try {
268             ResponseUtils.write(pc, string);
269         }
270         catch (JspException JavaDoc e) {
271             logger.error(Bundle.getString("Tags_WriteException"), e);
272         }
273     }
274
275     /**
276      * This is the factory for obtaining a Tag Rendering object. The factory supports to types
277      * of renderings HTML 4.01 and XHTML. The factory is responsible for creating the rendering objects and
278      * passing them out. The target encoding may be specified on a page by page basis within a WebApp. The
279      * <code>getRendering</code> method will return a <code>TagRenderingBase</code> object. This object is always
280      * a stateless object. The state needed to render the tag will be passed into the tag.
281      */

282     public static class Factory
283     {
284         private static HashMap JavaDoc _xhtml; // The XHTML TagRenderingBase objects
285
private static HashMap JavaDoc _html; // The HTML TagRenderingBase objects
286
private static HashMap JavaDoc _htmlQuirks; // THe HTML Quirks TagRenderingBase object
287

288         private static ConstantRendering _xhtmlConstants;
289         private static ConstantRendering _htmlConstants;
290
291         // create the HashMaps and their static renderings.
292
static
293         {
294             _xhtml = new HashMap JavaDoc(37);
295             _html = new HashMap JavaDoc(37);
296             _htmlQuirks = new HashMap JavaDoc(37);
297
298             // build the supported renderers.
299
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         /**
330          * Factory method for getting a TagRenderingBase for a tag. The default rendering is HTML 4.01.
331          * @param token The type of TagRenderingBase to retrieve.
332          * @param req The <code>ServletRequest</code> used to see what type of rendering is being done.
333          * @return A <code>TagRenderingBase</code>
334          */

335         public static TagRenderingBase getRendering(Object JavaDoc token, ServletRequest JavaDoc req)
336         {
337             int renderingType = _defaultDocType;
338             Integer JavaDoc reqRender = (Integer JavaDoc) req.getAttribute(Html.DOC_TYPE_OVERRIDE);
339             if (reqRender != null) {
340                 renderingType = ((Integer JavaDoc) reqRender).intValue();
341             }
342             else {
343                 Html html = (Html) req.getAttribute(Html.HTML_TAG_ID);
344                 // the default is html 4.0
345
if (html != null) {
346                     renderingType = html.getTargetDocumentType();
347                 }
348             }
349
350             // pick the map of renderers
351
HashMap JavaDoc 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             // return the renderer
367
Object JavaDoc o = h.get(token);
368             assert(o != null) : "Renderer was not found for [" + token + "] rendering:" + renderingType;
369             return (TagRenderingBase) o;
370         }
371
372         /**
373          * Return true if the current document is XHTML
374          * @param req
375          * @return boolean
376          */

377         public static boolean isXHTML(ServletRequest JavaDoc req)
378         {
379             Html html = (Html) req.getAttribute(Html.HTML_TAG_ID);
380
381             // the default is html 4.0
382
int renderingType = _defaultDocType;
383             if (html != null) {
384                 renderingType = html.getTargetDocumentType();
385             }
386
387             return (renderingType == XHTML_RENDERING);
388         }
389
390         /**
391          * @param req
392          * @return ConstantRendering
393          */

394         public static ConstantRendering getConstantRendering(ServletRequest JavaDoc 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             // the default is html 4.0
404
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     /**
414      * @param req
415      * @return String
416      */

417     public static String JavaDoc getAmp(ServletRequest JavaDoc req)
418     {
419         Html html = (Html) req.getAttribute(Html.HTML_TAG_ID);
420
421         // the default is html 4.0
422
int renderingType = HTML_RENDERING;
423         if (html != null) {
424             renderingType = html.getTargetDocumentType();
425         }
426
427         // pick the map of renderers
428
return (renderingType == XHTML_RENDERING) ? "&amp;" : "&";
429     }
430 }
431
Popular Tags