KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ivata > groupware > web > tag > HeadTag


1  /*
2  * Copyright (c) 2001 - 2005 ivata limited.
3  * All rights reserved.
4  * -----------------------------------------------------------------------------
5  * ivata groupware may be redistributed under the GNU General Public
6  * License as published by the Free Software Foundation;
7  * version 2 of the License.
8  *
9  * These programs are free software; you can redistribute them and/or
10  * modify them under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; version 2 of the License.
12  *
13  * These programs are distributed in the hope that they will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  * See the GNU General Public License in the file LICENSE.txt for more
18  * details.
19  *
20  * If you would like a copy of the GNU General Public License write to
21  *
22  * Free Software Foundation, Inc.
23  * 59 Temple Place - Suite 330
24  * Boston, MA 02111-1307, USA.
25  *
26  *
27  * To arrange commercial support and licensing, contact ivata at
28  * http://www.ivata.com/contact.jsp
29  * -----------------------------------------------------------------------------
30  * $Log: HeadTag.java,v $
31  * Revision 1.7 2005/04/28 18:47:05 colinmacleod
32  * Fixed XHMTL, styles and resin compatibility.
33  * Added support for URL rewriting.
34  *
35  * Revision 1.6 2005/04/27 15:14:36 colinmacleod
36  * Added media print stylesheets.
37  *
38  * Revision 1.5 2005/04/22 10:30:02 colinmacleod
39  * Updated stylesheet include routine
40  * to pickup stylsheets anywhere along
41  * the servlet context's path, so you have
42  * an override for all library pages and
43  * another for all calendar pages, for
44  * example.
45  *
46  * Revision 1.4 2005/04/10 20:10:06 colinmacleod
47  * Added new themes.
48  * Changed id type to String.
49  * Changed i tag to em and b tag to strong.
50  * Improved PicoContainerFactory with NanoContainer scripts.
51  *
52  * Revision 1.3 2005/04/09 17:19:59 colinmacleod
53  * Changed copyright text to GPL v2 explicitly.
54  *
55  * Revision 1.2 2005/03/16 18:41:45 colinmacleod
56  * Added warning when title is null.
57  *
58  * Revision 1.1.1.1 2005/03/10 17:51:35 colinmacleod
59  * Restructured ivata op around Hibernate/PicoContainer.
60  * Renamed ivata groupware.
61  *
62  * Revision 1.4 2004/12/23 21:01:30 colinmacleod
63  * Updated Struts to v1.2.4.
64  * Changed base classes to use ivata masks.
65  *
66  * Revision 1.3 2004/11/12 15:57:20 colinmacleod
67  * Removed dependencies on SSLEXT.
68  * Moved Persistence classes to ivata masks.
69  *
70  * Revision 1.2 2004/11/03 16:10:11 colinmacleod
71  * Changed todo comments to TODO: all caps.
72  *
73  * Revision 1.1 2004/09/30 15:16:02 colinmacleod
74  * Split off addressbook elements into security subproject.
75  *
76  * Revision 1.4 2004/07/13 19:41:15 colinmacleod
77  * Moved project to POJOs from EJBs.
78  * Applied PicoContainer to services layer (replacing session EJBs).
79  * Applied Hibernate to persistence layer (replacing entity EJBs).
80  *
81  * Revision 1.3 2004/03/21 21:16:08 colinmacleod
82  * Shortened name to ivata op.
83  *
84  * Revision 1.2 2004/02/01 22:00:34 colinmacleod
85  * Added full names to author tags
86  *
87  * Revision 1.1.1.1 2004/01/27 20:57:56 colinmacleod
88  * Moved ivata openportal to SourceForge..
89  *
90  * Revision 1.4 2004/01/20 15:11:46 jano
91  * fixing problems with new sslext
92  *
93  * Revision 1.3 2003/11/18 09:56:25 jano
94  * commiting all forgoten staff
95  *
96  * Revision 1.2 2003/10/17 12:36:13 jano
97  * fixing problems with building
98  * converting intranet -> portal
99  * Eclipse building
100  *
101  * Revision 1.1.1.1 2003/10/13 20:50:10 colin
102  * Restructured portal into subprojects
103  *
104  * Revision 1.8 2003/06/16 09:09:06 peter
105  * return afer sendRedirect
106  *
107  * Revision 1.7 2003/06/05 11:00:49 peter
108  * fixed apostrophe error in favorite title
109  *
110  * Revision 1.6 2003/04/01 11:32:01 jano
111  * if we set title use it otherwise use titleKey
112  *
113  * Revision 1.5 2003/03/27 20:41:12 peter
114  * favorites javascript changed, works with https now
115  *
116  * Revision 1.4 2003/03/06 17:32:36 peter
117  * added js exception catching for favorites
118  *
119  * Revision 1.3 2003/03/04 19:07:15 colin
120  * moved charset stuff from head to checkLogin
121  *
122  * Revision 1.2 2003/03/04 14:21:46 colin
123  * added width/height of popups & favorite stuff
124  *
125  * Revision 1.1 2003/02/24 19:33:33 colin
126  * moved to jsp
127  *
128  * Revision 1.18 2003/02/04 17:43:50 colin
129  * copyright notice
130  *
131  * Revision 1.17 2003/01/31 10:09:29 colin
132  * Added no cache pragma stuff
133  *
134  * Revision 1.16 2003/01/27 07:29:32 colin
135  * added titleKey and titleArg attributes
136  *
137  * Revision 1.15 2003/01/23 17:37:03 colin
138  * fix silly bug in response rewriting
139  *
140  * Revision 1.14 2003/01/23 13:58:06 colin
141  * modifications to allow URL rewriting sessions (instead of
142  * cookie)
143  *
144  * Revision 1.11 2002/09/30 15:33:08 colin
145  * added keepOnTop handling
146  *
147  * Revision 1.10 2002/09/30 14:21:24 colin
148  * added topLevel attribute
149  *
150  * Revision 1.9 2002/09/04 08:09:29 colin
151  * fixed bug in getSetting call
152  *
153  * Revision 1.8 2002/08/19 12:24:38 colin
154  * tidied documentation; removed Hungarian notation
155  *
156  * Revision 1.7 2002/06/13 07:44:07 colin
157  * first version of rose model: code tidied up/added javadoc
158  *
159  * Revision 1.6 2002/05/11 18:32:41 colin
160  * changed the name of the remote settings instance in the
161  * session from "SettingsRemote" to "settings" for easier
162  * usage in JSP files
163  *
164  * Revision 1.5 2002/05/11 18:07:29 colin
165  * cosmetic improvements to javadoc
166  *
167  * Revision 1.4 2002/05/11 08:53:36 colin
168  * updated and extended javadoc
169  *
170  * Revision 1.3 2002/04/30 15:22:16 colin
171  * first functional version of the mail subproject in JBuilder
172  *
173  * Revision 1.2 2002/04/27 17:40:47 colin
174  * first compliling version in EJB/JBuilder project
175  *
176  * Revision 1.1 2002/04/26 13:36:16 colin
177  * move to EJB/JBuilder project
178  *
179  * Revision 1.7 2002/02/03 19:54:25 colin
180  * linked settings to the database, rather than hard-coding in
181  * com.ivata.groupware.admin.settings
182  *
183  * Revision 1.6 2002/02/03 17:09:55 colin
184  * moved intialization of settings object from HeadTag to
185  * LoginTag
186  *
187  * Revision 1.5 2002/02/03 16:28:35 colin
188  * changed User to PersonUser
189  *
190  * Revision 1.4 2002/02/02 21:23:01 colin
191  * major restructuring to make the Settings class more generic
192  * all default settings are now taken from the database rather
193  * than being
194  * hard coded in the settings class
195  * settings are stored in a HashMap in settings
196  *
197  * Revision 1.3 2002/01/20 19:28:25 colin
198  * added tab and tree tags
199  * implemented address book functionality
200  *
201  * Revision 1.2 2001/12/12 20:29:29 colin
202  * initial version of the core intranet site
203  * fixed width attribute of window tag
204  *
205  * Revision 1.1 2001/10/06 17:20:06 colin
206  * first version after compiling in forte
207  * -----------------------------------------------------------------------------
208  */

209 package com.ivata.groupware.web.tag;
210
211 import java.io.File JavaDoc;
212 import java.io.IOException JavaDoc;
213 import java.io.UnsupportedEncodingException JavaDoc;
214 import java.util.List JavaDoc;
215 import java.util.Locale JavaDoc;
216 import java.util.Vector JavaDoc;
217
218 import javax.servlet.http.HttpServletRequest JavaDoc;
219 import javax.servlet.http.HttpSession JavaDoc;
220 import javax.servlet.jsp.JspException JavaDoc;
221 import javax.servlet.jsp.JspWriter JavaDoc;
222 import javax.servlet.jsp.tagext.BodyContent JavaDoc;
223 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
224
225 import org.apache.log4j.Logger;
226 import org.apache.struts.Globals;
227 import org.apache.struts.taglib.TagUtils;
228 import org.picocontainer.PicoContainer;
229
230 import com.ivata.groupware.admin.security.Security;
231 import com.ivata.groupware.admin.security.server.SecuritySession;
232 import com.ivata.groupware.admin.security.user.UserDO;
233 import com.ivata.groupware.admin.setting.Settings;
234 import com.ivata.groupware.container.PicoContainerFactory;
235 import com.ivata.mask.persistence.FinderException;
236 import com.ivata.mask.util.StringHandling;
237 import com.ivata.mask.util.SystemException;
238 import com.ivata.mask.web.RewriteHandling;
239 import com.ivata.mask.web.format.URLFormat;
240 import com.ivata.mask.web.servlet.GenerateCSS;
241 import com.ivata.mask.web.tag.theme.ThemeConstants;
242
243
244 /**
245  * <p>Creates standard header HTML for all of our pages. It is
246  * important to note that the stylesheet gets set here.</p>
247  *
248  * <p>This tag is also responsible for ensuring that the user is
249  * logged into the system. This is done by checking the session
250  * attribute 'userName' was set and, if not, the system is redirected
251  * to the login page.</p>
252  *
253  * <p><strong>Tag attributes:</strong><br/>
254  * <table cellpadding='2' cellspacing='5' border='0' align='center'
255  * width='85%'>
256  * <tr class='TableHeadingColor'>
257  * <th>attribute</th>
258  * <th>reqd.</th>
259  * <th>param. class</th>
260  * <th width='100%'>description</th>
261  * </tr>
262  * <tr class='TableRowColor'>
263  * <td>bundle</td>
264  * <td>false</td>
265  * <td><code>String</code></td>
266  * <td>Bundle used to localize the <code>titleKey</code>.</td>
267  * </tr>
268  * <tr class='TableRowColor'>
269  * <td>login</td>
270  * <td>false</td>
271  * <td><code>boolean</code></td>
272  * <td>If specified, this indicates that the system is trying to
273  * login.
274  * In this case, the tag does not verify the user's
275  * authenticity.</td>
276  * </tr>
277  * <tr class='TableRowColor'>
278  * <td>title</td>
279  * <td>false</td>
280  * <td><code>String</code></td>
281  * <td>Optional non-localized title information which will be appended
282  * to the
283  * title information from the system settings.</td>
284  * </tr>
285  * <tr class='TableRowColor'>
286  * <td>titleArgs</td>
287  * <td>false</td>
288  * <td><code>Object []</code></td>
289  * <td>Optional argument to use with <code>titleKey</cocde> (see below).</td>
290  * </tr>
291  * </tr>
292  * <tr class='TableRowColor'>
293  * <td>titleArg1</td>
294  * <td>false</td>
295  * <td><code>String</code></td>
296  * <td>Optional argument to use with <code>titleKey</cocde> (see below).</td>
297  * </tr>
298  * <tr class='TableRowColor'>
299  * <td>titleArg2</td>
300  * <td>false</td>
301  * <td><code>String</code></td>
302  * <td>Optional argument to use with <code>titleKey</cocde> (see below).</td>
303  * </tr>
304  * <tr class='TableRowColor'>
305  * <td>titleArg3</td>
306  * <td>false</td>
307  * <td><code>String</code></td>
308  * <td>Optional argument to use with <code>titleKey</cocde> (see below).</td>
309  * </tr>
310  * <tr class='TableRowColor'>
311  * <td>titleKey</td>
312  * <td>false</td>
313  * <td><code>String</code></td>
314  * <td>Optional localized title information which will be appended
315  * to the
316  * title information from the system settings.</td>
317  * </tr>
318  * <tr class='TableRowColor'>
319  * <td>javaScript</td>
320  * <td>false</td>
321  * <td><code>String</code></td>
322  * <td>Optional javascript to execute in the head section of the
323  * page.</td>
324  * </tr>
325  * <tr class='TableRowColor'>
326  * <td>topLevel</td>
327  * <td>false</td>
328  * <td><code>boolean</code></td>
329  * <td>Dictates whether or not this JSP page should always appear
330  * in a top-level frame. This is only used if the attribute
331  * <code>topLevel</code> is <code>true</code>.</td>
332  * </tr>
333  * <tr class='TableRowColor'>
334  * <td>topLevel</td>
335  * <td>false</td>
336  * <td><code>boolean</code></td>
337  * <td>Dictates whether or not this JSP page should appear in a
338  * top-level browser window. The default is <code>false</code>,
339  * meaning
340  * <em>JavaScript</em> is inserted to ensure this browser window appears
341  * within the main<em>ivata groupware</em> frames.</td>
342  * </tr>
343  * </table>
344  * </p>
345  *
346  * @since 2001-09-08
347  * @author Colin MacLeod
348  * <a HREF='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
349  * @version $Revision: 1.7 $
350  */

351 public class HeadTag extends BodyTagSupport JavaDoc {
352
353     /**
354      * This is the default style sheet - used for all themes, if present.
355      */

356     private static final String JavaDoc DEFAULT_CSS_NAME = "default.css";
357     /**
358      * Logger for this class.
359      */

360     private static final Logger logger = Logger.getLogger(HeadTag.class);
361     /**
362      * This is the ivata main style sheet - for the main frame.
363      */

364     private static final String JavaDoc MAIN_CSS_NAME = "ivataMain.css";
365     /**
366      * Special stylesheet just for IE.
367      */

368     private static final String JavaDoc MSIE_CSS_NAME = "internetExplorer.css";
369     /**
370      * <p>The bundle to use when setting the attribute
371      * <code>titleKey</code>.</p>
372      */

373     private String JavaDoc bundle = null;
374     /**
375      * Refer to {@link #isFrames}.
376      */

377     private boolean frames = false;
378     /**
379      * <p>Property decaration for tag attribute: javaScript.</p>
380      */

381     private String JavaDoc javaScript = null;
382     /**
383      * <p>Only used when <code>topLevel</code> is set to
384      * <code>true</code>, if this is set to <code>true</code> then
385      * javascript is inserted to keep the jsp page on the top level.</p>
386      */

387     private boolean keepOnTop = true;
388     /**
389      * <p>Property declaration for tag attribute: login.</p>
390      */

391     private boolean login = false;
392     /**
393      * <p>Stores the value of the tag attribute 'title'. </p>
394      *
395      * @see org.apache.struts.util.RequestUtils#message
396      */

397     private String JavaDoc title = null;
398     /**
399      * <p>Arguments used in localized title string. See <em>Struts<em>
400      * documentation for details.</p>
401      *
402      * @see org.apache.struts.util.RequestUtils#message
403      */

404     private List JavaDoc titleArgs = new Vector JavaDoc();
405     /**
406      * <p>Use a <em>Struts</em> localize key string to set the current value
407      * of <code>title</code>. </p>
408      *
409      * @see org.apache.struts.util.RequestUtils#message
410      */

411     private String JavaDoc titleKey = null;
412     /**
413      * <p>If <code>false</code> (the default), then javascript is inserted
414      * to ensure this page is displayed within the main ivata groupware
415      * frames.<p>
416      *
417      * <p>Set this to <code>true</code> for a pop-up or other top-level
418      * window frame.</p>
419      */

420     private boolean topLevel = false;
421     /**
422      * <p>This method is called after the JSP engine processes the body
423      * content of the tag.</p>
424      * @exception JspException encapsulates any exception when calling
425      * {@link #writeTagBodyContent writeTagBodyContent}.
426      * @return <code>SKIP_BODY</code> since we only ever want to evaluate
427      * this tag once.
428      * @see #writeTagBodyContent( JspWriter out, BodyContent bodyContent
429      * )
430      *
431      */

432     public int doAfterBody() throws JspException JavaDoc {
433         BodyContent JavaDoc bodyContent = getBodyContent();
434         if(bodyContent != null) {
435           try {
436               JspWriter JavaDoc out = getPreviousOut();
437               writeTagBodyContent(out, bodyContent);
438           } catch(Exception JavaDoc ex) {
439               throw new JspException JavaDoc(ex);
440           }
441         }
442
443         // for this tag, we only want to evaluate the body one time
444
return SKIP_BODY;
445     }
446     public int doEndTag() throws JspException JavaDoc {
447         HttpSession JavaDoc session = pageContext.getSession();
448         TagUtils tagUtils = TagUtils.getInstance();
449
450         HttpServletRequest JavaDoc request =
451             (HttpServletRequest JavaDoc) pageContext.getRequest();
452         SecuritySession securitySession = (SecuritySession)
453             session.getAttribute("securitySession");
454         boolean print = (request.getParameter("print") != null);
455         if (securitySession == null) {
456             try {
457                 PicoContainer container = PicoContainerFactory.getInstance()
458                     .getGlobalContainer();
459                 Security security =
460                     (Security)container.getComponentInstance(Security.class);
461                 securitySession = security.loginGuest();
462             } catch (SystemException e) {
463                 throw new JspException JavaDoc(e);
464             }
465             session.setAttribute("securitySession", securitySession);
466         }
467         PicoContainer container = securitySession.getContainer();
468         Settings settings =
469             (Settings) container.getComponentInstance(Settings.class);
470         UserDO user = securitySession.getUser();
471
472         // find the theme - if there is no theme, use the default from the
473
// settings
474
String JavaDoc themeName = (String JavaDoc) pageContext.findAttribute(
475                 ThemeConstants.ATTRIBUTE_THEME_NAME);
476         if (StringHandling.isNullOrEmpty(themeName)) {
477             try {
478                 // use the site default theme for this user
479
themeName = settings.getStringSetting(securitySession,
480                         "siteTheme", user);
481             } catch (SystemException e) {
482                 throw new JspException JavaDoc(e);
483             }
484             session.setAttribute(ThemeConstants.ATTRIBUTE_THEME_NAME,
485                     themeName);
486         }
487
488         String JavaDoc encoding;
489         try {
490             Object JavaDoc localeObject = session.getAttribute(Globals.LOCALE_KEY);
491             if (localeObject == null) {
492                 String JavaDoc localeLanguage;
493                 try {
494                     localeLanguage = settings.getStringSetting(securitySession,
495                             "i18nLocaleLanguage", null);
496                 } catch (FinderException e) {
497                     if (logger.isDebugEnabled()) {
498                         logger.warn("Could not retrieve locale language from "
499                                 + "setting 'i18nLocaleLanguage'. Defaulting "
500                                 + "to en.", e);
501                     } else {
502                         logger.warn("Could not retrieve locale language from "
503                                 + "setting 'i18nLocaleLanguage'. Defaulting "
504                                 + "to en.");
505                     }
506                     localeLanguage = "en";
507                 }
508
509                 session.setAttribute(Globals.LOCALE_KEY,
510                     new Locale JavaDoc(localeLanguage));
511             }
512             // set the response character encoding
513
try {
514                 encoding = settings.getStringSetting(securitySession,
515                     "i18nEncoding",
516                     user);
517             } catch (FinderException e) {
518                 if (logger.isDebugEnabled()) {
519                     logger.warn("Could not retrieve encoding from setting "
520                             + "'i18nEncoding'. Defaulting to utf-8.", e);
521                 } else {
522                     logger.warn("Could not retrieve encoding from setting "
523                             + "'i18nEncoding'. Defaulting to utf-8.");
524                 }
525                 encoding = "utf-8";
526             }
527             pageContext.getRequest().setCharacterEncoding(encoding);
528             pageContext.getResponse().setContentType("text/HTML; charset="
529                     + encoding);
530
531         } catch(UnsupportedEncodingException JavaDoc e) {
532             throw new JspException JavaDoc(e);
533
534         } catch(SystemException e) {
535             throw new JspException JavaDoc(e);
536         }
537
538         // if we want to set up just title in jsp page we will not do titleKey
539
// functionality
540
String JavaDoc myTitle = title;
541         if (myTitle==null) {
542             // window title
543
try {
544                 myTitle = "";
545                 if (titleKey != null) {
546                     // Construct the optional arguments array we will be using
547
myTitle = tagUtils.message(pageContext, bundle,
548                             Globals.LOCALE_KEY, titleKey, titleArgs.toArray());
549                     if (myTitle == null) {
550                         myTitle = tagUtils.message(pageContext, null,
551                                 Globals.LOCALE_KEY, titleKey,
552                                 titleArgs.toArray());
553                     }
554                     if (myTitle == null) {
555                         StringBuffer JavaDoc warning =
556                            new StringBuffer JavaDoc("Could not find a title for key '");
557                         warning.append(titleKey);
558                         warning.append("'. I checked ");
559                         if (bundle != null) {
560                             warning.append(" bundle '");
561                             warning.append(bundle);
562                             warning.append("' and ");
563                         }
564                         warning.append("the default (null) bundle.");
565                         logger.warn(warning.toString());
566                         myTitle = "";
567                     }
568                 }
569
570                 if (!"".equals(myTitle)) {
571                      myTitle += " - ";
572                 }
573                 try {
574                     myTitle += settings.getStringSetting(securitySession,
575                             "siteTitle",
576                             user);
577                 } catch(FinderException e) {
578                     if (logger.isDebugEnabled()) {
579                         logger.warn("Could locate value for setting "
580                                 + "'siteTitle'.", e);
581
582                     } else {
583                         logger.warn("Could locate value for setting "
584                                 + "'siteTitle'.");
585                     }
586                 }
587             } catch(SystemException e) {
588                 throw new JspException JavaDoc(e);
589             }
590         }
591         if (print) {
592             String JavaDoc printLabel = StringHandling.getNotNull(tagUtils
593                     .message(pageContext, null,
594                             Globals.LOCALE_KEY, "print.label.title",
595                             null),
596                     "Printing ");
597             myTitle = printLabel + myTitle;
598         }
599
600
601         // if this is not a top level window, make sure there is javascript to
602
// return this window to a frame
603
StringBuffer JavaDoc javaScriptBuffer = new StringBuffer JavaDoc();
604         // append any user-requested javascript
605
if(javaScript != null) {
606             javaScriptBuffer.append(javaScript);
607             javaScriptBuffer.append('\n');
608         }
609         // if this is a top level window, make sure it stays on the top
610
if (topLevel) {
611             // see if the width and height are set
612
Integer JavaDoc width = StringHandling.integerValue(request.getParameter(
613                     "width"));
614             Integer JavaDoc height = StringHandling.integerValue(request.getParameter(
615                     "height"));
616             if ((width != null)
617                     && (height != null)){
618                 javaScriptBuffer.append("window.resizeTo(");
619                 javaScriptBuffer.append(width);
620                 javaScriptBuffer.append(",");
621                 javaScriptBuffer.append(height);
622                 javaScriptBuffer.append(");\n");
623             }
624
625             // only keep on the top level if we were told to
626
if(keepOnTop && !print) {
627                 javaScriptBuffer.append(
628                         "if (window != top) {\n top.location.href "
629                         + "= location.href ;\n}\nwindow.focus();\n\n");
630             }
631         } else if (!print) {
632             URLFormat URLFormat = new URLFormat();
633
634             // make sure the index page is on the top
635
// escape is JavaScript method to URL encode
636
javaScriptBuffer.append(
637                     "if (window == top) {\n top.location.href = \"");
638             javaScriptBuffer.append(
639                     RewriteHandling.getContextPath(request));
640             javaScriptBuffer.append("/index.jsp?uri=\" + escape(\"");
641             
642             
643             String JavaDoc queryString = request.getQueryString();
644             StringBuffer JavaDoc uri = new StringBuffer JavaDoc();
645             uri.append(request.getServletPath());
646             if(queryString != null) {
647                 uri.append(URLFormat.format("?"));
648                 uri.append(URLFormat.format(queryString));
649             }
650             javaScriptBuffer.append(RewriteHandling.rewriteURL(uri.toString(),
651                     request.getContextPath()));
652             javaScriptBuffer.append("\");\n}\n");
653         }
654         String JavaDoc generatedJavaScript;
655         if (javaScriptBuffer.length() > 0) {
656             generatedJavaScript = javaScriptBuffer.toString();
657         } else {
658             generatedJavaScript = null;
659         }
660         try {
661             JspWriter JavaDoc out = pageContext.getOut();
662             out.println("<meta http-equiv='Content-Type' content='text/HTML; "
663                     + "charset="
664                     + encoding
665                     + "'/>");
666             out.println("<meta http-equiv='Content-Style-Type' "
667                     + "content='text/css' />");
668
669             // make links for style-sheets in the /style directory which
670
// match this theme
671
// non-top level pages share a common style sheet
672
String JavaDoc servletPath = request.getServletPath();
673             if (!topLevel && !servletPath.startsWith("/util/iframeContent")) {
674                 printStyleSheetLink(request,
675                         out,
676                         GenerateCSS.CSS_PATH
677                         + "/"
678                         + MAIN_CSS_NAME,
679                         null);
680             }
681
682             // go through each element of the path and see if there are matching
683
// stylesheets. start with root and work up to the request path
684
// and file name (minus extension) for the current path. in each
685
// case see if there is matching directory in the style directory
686
// with stylesheets in
687
if (!StringHandling.isNullOrEmpty(servletPath)) {
688                 String JavaDoc fullPath = servletPath;
689                 int dotPos = fullPath.indexOf('.');
690                 // remove the file extension, so person.action and person.jsp
691
// mean the same thing
692
if (dotPos != 0) {
693                     fullPath = fullPath.substring(0, dotPos);
694                 }
695                 // start with just /
696
int pos = -1;
697                 do {
698                     // go forward one directory
699
pos = fullPath.indexOf('/', pos + 1);
700                     // go round once for the main stylesheet, followed by once
701
// for the print stylesheet (if such exists)
702
String JavaDoc[] mediaNames = new String JavaDoc[] {null, "print"};
703                     for (int i = 0; i < mediaNames.length; ++i) {
704                         String JavaDoc mediaName = mediaNames[i];
705                         String JavaDoc path;
706                         // if we got to the end, use the full path
707
if (pos == -1) {
708                             path = fullPath + "/";
709                         } else {
710                             path = fullPath.substring(0, pos + 1);
711                         }
712                         // print stylesheet is in separate subdir
713
if (mediaName != null) {
714                             path += mediaName + "/";
715                         }
716                         String JavaDoc specificCSSName = GenerateCSS.CSS_PATH
717                             + path
718                             + DEFAULT_CSS_NAME;
719                         // see if there is a default override
720
File JavaDoc file = new File JavaDoc(pageContext.getServletContext()
721                                 .getRealPath(specificCSSName));
722                         if (file.exists()) {
723                             printStyleSheetLink(request,
724                                     out,
725                                     specificCSSName,
726                                     mediaName);
727                         }
728                         // see if there is a theme-specific override
729
specificCSSName = GenerateCSS.CSS_PATH
730                             + path
731                             + themeName
732                             + ".css";
733                         file = new File JavaDoc(pageContext.getServletContext()
734                                 .getRealPath(specificCSSName));
735                         if (file.exists()) {
736                             printStyleSheetLink(request,
737                                     out,
738                                     specificCSSName,
739                                     mediaName);
740                         }
741                         // see if there is an internet deplorer override too
742
specificCSSName = GenerateCSS.CSS_PATH
743                             + path
744                             + MSIE_CSS_NAME;
745                         file = new File JavaDoc(pageContext.getServletContext()
746                                 .getRealPath(specificCSSName));
747                         if (file.exists()) {
748                             out.println("<!--[if IE]>");
749                             printStyleSheetLink(request,
750                                     out,
751                                     specificCSSName,
752                                     mediaName);
753                             out.println("<![endif]-->");
754                         }
755                     }
756                 } while (pos != -1);
757             }
758
759             out.println("<title>" + myTitle + "</title>");
760
761             // if JavaScript was specified, use it now.
762
if ((generatedJavaScript != null)
763                 && !generatedJavaScript.trim().equals("")) {
764                 out.println("<script "
765                         + "type='text/javascript'>");
766                 out.println(" <!--");
767                 out.println(generatedJavaScript);
768                 out.println(" // -->");
769                 out.println("</script>");
770             }
771             out.println("</head>");
772         } catch(IOException JavaDoc e) {
773             throw new JspException JavaDoc(e);
774         }
775         return EVAL_PAGE;
776     }
777     /**
778      * <p>This method is called when the JSP engine encounters the start
779      * tag, after the attributes are processed.<p>
780      *
781      * <p>Scripting variables (if any) have their values set here.</p>
782      *
783      * @return <code>EVAL_BODY_BUFFERED</code> since we always want to
784      * evaluate the tag body once.
785      * @throws JspException if there is a <code>NamingExcpetion</code>
786      * getting the <code>InitialContext</code>
787      * @throws com.ivata.groupware.admin.setting.SettingsInitializationException
788      * if there is no {@link com.ivata.groupware.admin.setting.Settings
789      * Settings} object in the session and one cannot be created
790      * @throws JspException encapsulates any exception when calling
791      * <code>out.println</code>
792      */

793     public int doStartTag() throws JspException JavaDoc {
794         try {
795             JspWriter JavaDoc out = pageContext.getOut();
796             out.println("<head>");
797             out.println("<meta http-equiv='Content-Style-Type' "
798                     + "content='text/css' />");
799             // Force Refresh HTTP1.1
800
out.println("<meta http-equiv='cache-control' content='no-cache'/>");
801             // Force Refresh HTTP1.0
802
out.println("<meta http-equiv='pragma' content='no-cache'/>");
803         } catch(IOException JavaDoc e) {
804             throw new JspException JavaDoc(e);
805         }
806         BodyContent JavaDoc bodyContent = getBodyContent();
807         return EVAL_BODY_BUFFERED;
808     }
809     /**
810      * <p>Get the bundle to use when setting the attribute
811      * <code>titleKey</code>.</p>
812      *
813      * @return the current value of bundle, or <code>null</code> if there
814      * is none.
815      */

816     public final String JavaDoc getBundle() {
817         return bundle;
818     }
819
820     /**
821      * <p>Set the value supplied to the tag attribute 'javaScript'</p>
822      *
823      * <p>If specified, this attribute contains javascript to execute in
824      * the head section of the page.</p>
825      *
826      * @return the value for the javaScript tag attribute.
827      */

828     public final String JavaDoc getJavaScript() {
829         return javaScript;
830     }
831     /**
832      * <p>Only used when <code>topLevel</code> is set to
833      * <code>true</code>, if this is set to <code>true</code> then
834      * javascript is inserted to keep the jsp page on the top level.</p>
835      *
836      * @return <code>true</code> if
837      * javascript will be inserted to keep the jsp page on the top level,
838      * otherwise <code>false</code>.
839      */

840     public final boolean getKeepOnTop() {
841         return keepOnTop;
842     }
843
844     /**
845      * <p>Return the value supplied to the attribute 'login'.</p>
846      *
847      * <p>If specified, this attribute indicates that the system is trying
848      * to login. In this case, the tag does not verify the user's
849      * authenticity.</p>
850      *
851      * @return the value supplied to the tag attribute 'login'
852      */

853     public final boolean getLogin() {
854         return login;
855     }
856
857     /**
858      * <p>Get the value supplied to the attribute 'titleKey'.</p>
859      *
860      * <p>This attribute represents the title of this window. This sets a
861      * custom
862      * property 'title' to be used in
863      * {@link
864      * com.ivata.groupware.web.theme.Theme#parseSection(String
865      * sName,
866      * java.util.Properties properties)
867      * Theme.parseSection(String sName, java.util.Properties
868      * properties)}.
869      * The actual meaning of this attribute depends on the implementation
870      * of the
871      * theme.</p>
872      *
873      * @return the value supplied to the tag attribute 'title'
874      *
875      */

876     public final String JavaDoc getTitle() {
877         return title;
878     }
879
880     /**
881      * <p>Arguments used in localized title string. See <em>Struts<em>
882      * documentation for details.</p>
883      *
884      * @see org.apache.struts.util.RequestUtils#message
885      * @return the current value of titleArgs.
886      */

887     public List JavaDoc getTitleArgs() {
888         return titleArgs;
889     }
890
891     /**
892      * <p>Use a <em>Struts</em> localize key string to set the current value
893      * of <code>title</code>. </p>
894      *
895      * @see org.apache.struts.util.RequestUtils#message
896      *
897      * @return the current value of titleKey.
898      */

899     public final String JavaDoc getTitleKey() {
900         return titleKey;
901     }
902
903     /**
904      * <p>Get whether or not this JSP page should appear in a top-level
905      * browser window.</p>
906      *
907      * <p>If <code>false</code> (the default), then javascript is inserted
908      * to ensure this page is displayed within the main ivata groupware
909      * frames.<p>
910      *
911      * <p>This is set to <code>true</code> for a pop-up or other top-level
912      * window frame.</p>
913      *
914      * @return <code>true</code> if this browser frame should appear in a
915      * top-level window, otherwise <code>false</code>.
916      */

917     public final boolean getTopLevel() {
918         return topLevel;
919     }
920     /**
921      * Get whether or not this page contains frames.
922      * @return Returns <code>true</code> if this page contains frames.
923      */

924     public boolean isFrames() {
925         return frames;
926     }
927     /**
928      * Prints out a single link tag for a stylesheet.
929      *
930      * @param request servlet request we are processing.
931      * @param out does the writing.
932      * @param filePath web-app relative path of the style-sheet, must start with
933      * '/'
934      * @param mediaName is this a media-specific link (e.g. 'print'). If so,
935      * this is the name of the media attribute generated, otherwise
936      * <code>null</code>.
937      */

938     private void printStyleSheetLink(final HttpServletRequest JavaDoc request,
939             final JspWriter JavaDoc out,
940             final String JavaDoc filePath,
941             final String JavaDoc mediaName)
942             throws IOException JavaDoc {
943         out.print("<link rel='stylesheet' HREF='");
944         out.print(RewriteHandling.rewriteURL(RewriteHandling.getContextPath(request) + filePath,
945                 RewriteHandling.getContextPath(request)));
946         if (mediaName != null) {
947             out.print("' media='");
948             out.print(mediaName);
949         }
950         out.println("' type='text/css' />");
951
952     }
953
954     /**
955      * <p>Set the bundle to use when setting the attribute
956      * <code>titleKey</code>.</p>
957      *
958      * @param bundle the new value of bundle to use, or <code>null</code>
959      * if there is none.
960      */

961     public final void setBundle(final String JavaDoc bundle) {
962         this.bundle = bundle;
963     }
964     /**
965      * Refer to {@link #isFrames}.
966      * @param framesParam Refer to {@link #isFrames}.
967      */

968     public void setFrames(boolean framesParam) {
969         if (logger.isDebugEnabled()) {
970             logger.debug("Setting frames. Before '" + frames + "', after '"
971                     + framesParam + "'");
972         }
973         frames = framesParam;
974     }
975
976     /**
977      * <p>Set the value supplied to the tag attribute 'javaScript'</p>
978      *
979      * <p>If specified, this attribute contains javascript to execute in
980      * the head section of the page.</p>
981      *
982      * @param javaScript the new value for the javaScript tag attribute.
983      */

984     public final void setJavaScript(final String JavaDoc javaScript) {
985         this.javaScript = javaScript;
986     }
987
988     /**
989      * <p>Only used when <code>topLevel</code> is set to
990      * <code>true</code>, this attribute decides whether or not the
991      * <em>JSP</em> page should only appear in a top-level frame.</p>
992      *
993      * @param keepOnTop if <code>true</code> then
994      * javascript is inserted to keep the jsp page on the top level.
995      */

996     public final void setKeepOnTop(final boolean keepOnTop) {
997         this.keepOnTop = keepOnTop;
998     }
999
1000    /**
1001     * <p>Set the value supplied to the tag attribute 'login'</p>
1002     *
1003     * <p>If specified, this attribute indicates that the system is trying
1004     * to login. In this case, the tag does not verify the user's
1005     * authenticity.</p>
1006     *
1007     * @param isLogin new value for the login tag attribute.
1008     */

1009    public final void setLogin(final boolean login) {
1010        this.login = login;
1011    }
1012
1013    /**
1014     * <p>Set the value supplied to the attribute 'title'.</p>
1015     *
1016     * <p>This attribute represents the title of this window. This sets a
1017     * custom property 'title' to be used in
1018     * {@link
1019     * com.ivata.groupware.web.theme.Theme#parseSection(String
1020     * sName, java.util.Properties properties)
1021     * Theme.parseSection(String sName, java.util.Properties
1022     * properties)}.
1023     * The actual meaning of this attribute depends on the implementation
1024     * of the
1025     * theme.</p>
1026     *
1027     * @param title the new value of the tag attribute 'title'
1028     *
1029     */

1030    public final void setTitle(final String JavaDoc title) {
1031        this.title = title;
1032    }
1033
1034    /**
1035     * <p>Argument used in localized title string. See <em>Struts<em>
1036     * documentation for details.</p>
1037     *
1038     * @see org.apache.struts.util.RequestUtils#message
1039     * @param titleArgs the new value of titleArgs.
1040     */

1041    public final void setTitleArgs(final List JavaDoc titleArgsParam) {
1042        this.titleArgs = titleArgsParam;
1043    }
1044
1045    /**
1046     * <p>Use a <em>Struts</em> localize key string to set the current value
1047     * of <code>title</code>. </p>
1048     *
1049     * @see org.apache.struts.util.RequestUtils#message
1050     *
1051     * @param titleKey the new value of titleKey.
1052     */

1053    public final void setTitleKey(final String JavaDoc titleKey) {
1054        this.titleKey = titleKey;
1055    }
1056
1057    /**
1058     * <p>Set whether or not this JSP page should appear in a top-level
1059     * browser window.</p>
1060     *
1061     * <p>If <code>false</code> (the default), then javascript is inserted
1062     * to ensure this page is displayed within the main ivata groupware
1063     * frames.<p>
1064     *
1065     * <p>Set this to <code>true</code> for a pop-up or other top-level
1066     * window frame.</p>
1067     *
1068     * @param topLevel set to <code>true</code> if this browser frame
1069     * should appear
1070     * in a top-level window, otherwise <code>false</code>.
1071     */

1072    public final void setTopLevel(final boolean topLevel) {
1073        this.topLevel = topLevel;
1074    }
1075
1076    /**
1077     * <p>This method contains all the functionality to process the body
1078     * of the tag. Not needed for empty tags!</p>
1079     *
1080     * @exception IOException thrown by JspWriter.println( String )
1081     * @param out used to write the body output to
1082     * @param bodyContent returned by getBodyContent( )
1083     */

1084    public void writeTagBodyContent(final JspWriter JavaDoc out,
1085            final BodyContent JavaDoc bodyContent) throws IOException JavaDoc {
1086        HttpSession JavaDoc session = pageContext.getSession();
1087        out.println(bodyContent.getString());
1088        // clear the body content for the next time through.
1089
bodyContent.clearBody();
1090    }
1091}
1092
Popular Tags