KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tags > html > Html


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.html;
19
20 import org.apache.beehive.netui.tags.AbstractPageError;
21 import org.apache.beehive.netui.tags.ErrorHandling;
22 import org.apache.beehive.netui.tags.IErrorReporter;
23 import org.apache.beehive.netui.tags.javascript.ScriptContainer;
24 import org.apache.beehive.netui.tags.rendering.AbstractHtmlState;
25 import org.apache.beehive.netui.tags.rendering.HtmlTag;
26 import org.apache.beehive.netui.tags.rendering.TagRenderingBase;
27 import org.apache.beehive.netui.tags.rendering.WriteRenderAppender;
28 import org.apache.struts.Globals;
29
30 import javax.servlet.ServletRequest JavaDoc;
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpSession JavaDoc;
33 import javax.servlet.jsp.JspException JavaDoc;
34 import javax.servlet.jsp.tagext.Tag JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.Locale JavaDoc;
37
38 /**
39  * Generates the html element and performs error handling within its body.
40  * @jsptagref.tagdescription <p>
41  * Renders an &lt;html> tag.
42  * </p>
43  * @example In this sample, the &lt;netui:html> tag uses the default locale and the direction of the HTML is
44  * left-to-right (LTR).
45  * <pre>&lt;netui:html dir="LTR" useLocale="true" /></pre>
46  *
47  * @netui:tag name="html" description="Generates the html element and performs error handling within its body."
48  */

49 public class Html extends ScriptContainer
50         implements IErrorReporter
51 {
52     /**
53      * The HTML tag is registered into the request with this name. This allows tags reporting
54      * errors to find the top level <code>ErrorReporter</code>.
55      */

56     public static final String JavaDoc HTML_TAG_ID = "netui:html";
57
58     /**
59      * This is an override of the Document type set in the request
60      */

61     public static final String JavaDoc DOC_TYPE_OVERRIDE = "netui:doctype";
62
63     /**
64      * Constant representing the document type html 4.01
65      */

66     public static final String JavaDoc HTML_401 = "html4-loose";
67
68     /**
69      * Constant representing the document type html 4.01
70      */

71     public static final String JavaDoc HTML_401_QUIRKS = "html4-loose-quirks";
72
73     /**
74      * Constant representing the document type XHTML 1.0 Transitional.
75      */

76     public static final String JavaDoc XHTML_10 = "xhtml1-transitional";
77
78     private HtmlTag.State _state = new HtmlTag.State();
79     private TagRenderingBase _br;
80     private WriteRenderAppender _writer;
81
82     private boolean _useLocale = false; // include xml:lang=defaultLocale.getLanguage()
83
private boolean _scopeEnded = false;
84     private ArrayList JavaDoc _errors; // errors
85
private IErrorReporter _containerErrors; //Check to see if there is a parent error reporter
86

87     private String JavaDoc _docType; // The document type
88

89     private int _rendering = TagRenderingBase.UNKNOWN_RENDERING;
90
91     /**
92      * Returns the name of the Tag.
93      */

94     public String JavaDoc getTagName()
95     {
96         return "Html";
97     }
98
99     /**
100      * This method will return the TagRenderBase enum value for the document type. The default
101      * value is HTML 4.01.
102      * @return int
103      */

104     public int getTargetDocumentType()
105     {
106         if (_rendering != TagRenderingBase.UNKNOWN_RENDERING)
107             return _rendering;
108
109         if (_docType != null) {
110             if (_docType.equals(HTML_401))
111                 _rendering = TagRenderingBase.HTML_RENDERING;
112             else if (_docType.equals(HTML_401_QUIRKS))
113                 _rendering = TagRenderingBase.HTML_RENDERING_QUIRKS;
114             else if (_docType.equals(XHTML_10))
115                 _rendering = TagRenderingBase.XHTML_RENDERING;
116             else
117                 _rendering = TagRenderingBase.getDefaultDocType();
118         }
119         else {
120             _rendering = TagRenderingBase.getDefaultDocType();
121         }
122         return _rendering;
123     }
124
125     /////////////////////////// Attributes ////////////////////////////
126

127     /**
128      * Sets the dir value of the html.
129      * @param dir the direction of text, "LTR" or "RTL"
130      * @jsptagref.attributedescription Specifies the direction of text. (<code>LTR | RTL</code>)
131      * @jsptagref.databindable false
132      * @jsptagref.attributesyntaxvalue <i>string_dir</i>
133      * @netui:attribute required="false" rtexprvalue="true"
134      * description="Sets the dir value (ltr or rtl) of the html."
135      */

136     public void setDir(String JavaDoc dir)
137     {
138         _state.dir = dir;
139     }
140
141     /**
142      * Gets whether the default locale's language should be used.
143      * @return true or false
144      */

145     public boolean isUseLocale()
146     {
147         return _useLocale;
148     }
149
150     /**
151      * Sets whether the default locale's language should be used.
152      * @param locale true or false
153      * @jsptagref.attributedescription Sets whether the default locale's language should be used.
154      * @jsptagref.databindable false
155      * @jsptagref.attributesyntaxvalue <i>boolean_locale</i>
156      * @netui:attribute required="false" rtexprvalue="true" type="boolean"
157      * description="Sets whether the default locale's language should be used."
158      */

159     public void setUseLocale(boolean locale)
160     {
161         _useLocale = locale;
162     }
163
164     /**
165      * Set the document type (html4-loose or xhtml1-transitional) of the document.
166      * @jsptagref.attributedescription Set the document type (html4-loose or xhtml1-transitional) of the document.
167      * The default is html4-loose-quirks.
168      * @jsptagref.databindable false
169      * @jsptagref.attributesyntaxvalue <i>string_doctype</i>
170      * @netui:attribute required="false" rtexprvalue="true"
171      * description="Set the document type (html4-loose or xhtml1-transitional) of the document."
172      */

173     public void setDocumentType(String JavaDoc docType)
174     {
175         _docType = docType;
176     }
177
178
179     public int doStartTag() throws JspException JavaDoc
180     {
181         // check to see if there is a scope id
182
ServletRequest JavaDoc req = pageContext.getRequest();
183         _containerErrors = (org.apache.beehive.netui.tags.IErrorReporter) req.getAttribute(CONTAINER_ERRORS);
184
185         //Make this tag available to child tags
186
req.setAttribute(HTML_TAG_ID, this);
187
188         // set the local
189
Locale JavaDoc currentLocale = currentLocale();
190         if (currentLocale == null) {
191             currentLocale = Locale.getDefault();
192         }
193
194         // write out the html...
195
_state.lang = currentLocale.getLanguage();
196         String JavaDoc idScope = getRealIdScope();
197         pushIdScope();
198         if (idScope != null) {
199             _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL, "netui:idScope", idScope);
200         }
201
202         _writer = new WriteRenderAppender(pageContext);
203         _br = TagRenderingBase.Factory.getRendering(TagRenderingBase.HTML_TAG, req);
204         _br.doStartTag(_writer, _state);
205         _writer.append("\n");
206
207         return EVAL_BODY_INCLUDE;
208     }
209
210     /**
211      * Write out the body content and report any errors that occured.
212      * @throws JspException if a JSP exception has occurred
213      */

214     public int doEndTag() throws JspException JavaDoc
215     {
216         popIdScope();
217         
218         // if there are errors then we should output the error table
219
ErrorHandling.reportCollectedErrors(pageContext, this);
220
221         // The body tag may cause the scope Div to be closed, if not we must do it now
222
//if (getIdScope() != null && !_scopeEnded) {
223
// _writer.append("</div>");
224
//}
225

226         // the script can be written out by another tag, typically this would be the <body> tag.
227
writeScript(_writer);
228
229         // close the html tag
230
if (!_scopeEnded)
231             _writer.append("\n");
232         _br.doEndTag(_writer);
233         localRelease();
234         return EVAL_PAGE;
235     }
236
237     /**
238      * This will close the HTML div associated with the idScope. This may be
239      * called by the Body tag so the div ends before the body ends.
240      * @param _writer a writer to write the close tag into
241      */

242     public void endScope(WriteRenderAppender _writer)
243     {
244         // write the close tag and mark the fact that it is now closed.
245
//_writer.append("</div>\n");
246
_scopeEnded = true;
247     }
248
249     /**
250      * Add an error to the errors being reported by this tag.
251      * @param ape The AbstractPageError to add
252      */

253     public void addError(AbstractPageError ape)
254     {
255         assert (ape != null);
256
257         // if this is not the error reporter, then find an error reporter above this one.
258
if (_containerErrors == null && _errors == null) {
259             Tag par = getParent();
260             while (par != null) {
261                 if (par instanceof org.apache.beehive.netui.tags.IErrorReporter) {
262                     _containerErrors = (org.apache.beehive.netui.tags.IErrorReporter) par;
263                     break;
264                 }
265                 par = par.getParent();
266             }
267         }
268
269         // if there is an error reporter, add the error and return
270
if (_containerErrors != null) {
271             _containerErrors.addError(ape);
272             return;
273         }
274
275         // This is the error reporter.
276
if (_errors == null) {
277             _errors = new ArrayList JavaDoc();
278         }
279
280         // add the error and update it
281
_errors.add(ape);
282         ape.errorNo = _errors.size();
283     }
284
285     /**
286      * Return an ArrayList of the errors
287      * @return an <code>ArrayList</code> of all errors.
288      */

289     public ArrayList JavaDoc returnErrors()
290     {
291         if (_containerErrors != null)
292             return _containerErrors.returnErrors();
293         ArrayList JavaDoc e = _errors;
294         _errors = null;
295         return e;
296     }
297
298     /**
299      * This boolean indicates if an ErrorReporter is reporting errors
300      * or not. The caller should check this before calling addError
301      * because the ErrorReporter may be off for some reason.
302      * @return a boolean indicating if the tag is reporting errors or not.
303      */

304     public boolean isReporting()
305     {
306         return true;
307     }
308
309
310     /**
311      * Return the current Locale for this request, creating a new one if
312      * necessary. If there is no current Locale, and locale support is not
313      * requested, return <code>null</code>.
314      */

315     protected Locale JavaDoc currentLocale()
316     {
317         ServletRequest JavaDoc req = pageContext.getRequest();
318
319         // Create a new session if necessary
320
HttpSession JavaDoc session = pageContext.getSession();
321         if ((_useLocale) && (session == null))
322             session = ((HttpServletRequest JavaDoc) req).getSession();
323         if (session == null)
324             return null;
325
326         // Return any currently set Locale in our session
327
Locale JavaDoc current = (Locale JavaDoc) session.getAttribute(Globals.LOCALE_KEY);
328         if (current != null)
329             return current;
330
331         // Configure a new current Locale, if requested
332
if (!_useLocale)
333             return (null);
334         current = req.getLocale();
335         if (current != null)
336             session.setAttribute(Globals.LOCALE_KEY, current);
337         return current;
338     }
339
340     /**
341      * Release any acquired resources.
342      */

343     protected void localRelease()
344     {
345         super.localRelease();
346
347         _state.clear();
348         _br = null;
349         _writer = null;
350
351         _useLocale = false;
352         _scopeEnded = false;
353         _errors = null;
354         _containerErrors = null;
355         pageContext.getRequest().removeAttribute(HTML_TAG_ID);
356     }
357 }
358
Popular Tags