KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > ext > taglib > StyleSheetTag


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.ext.taglib;
66
67 import com.jcorporate.expresso.core.misc.CurrentLogin;
68 import com.jcorporate.expresso.kernel.util.FastStringBuffer;
69 import com.jcorporate.expresso.services.dbobj.Setup;
70 import org.apache.log4j.Logger;
71
72 import javax.servlet.http.HttpServletRequest JavaDoc;
73 import javax.servlet.jsp.JspWriter JavaDoc;
74 import javax.servlet.jsp.PageContext JavaDoc;
75 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
76
77 /**
78  * This tag provides automatic Cascading Stylesheet output. What it does is
79  * it checks the setup table for any tag extensions. If it finds it, then it
80  * tries to output the appropriate stylesheet. If the extension can't be found
81  * then it will attempt to output the default component.css name without any
82  * extension.
83  * <p/>
84  * <p>Attributes:<br />
85  * component - The name of the component that this stylesheet is used. If it
86  * is ommitted then we look for default-*.css<br />
87  *
88  * @author Michael Rimov
89  */

90 public class StyleSheetTag extends TagSupport JavaDoc {
91     /**
92      * The component to use for the stylesheet.
93      */

94     protected String JavaDoc component = null;
95     protected String JavaDoc componentToUse = null;
96
97     private static Logger log = Logger.getLogger(StyleSheetTag.class);
98
99
100     /**
101      *
102      */

103     public StyleSheetTag() {
104
105     }
106
107
108     /**
109      * Do nothing until end tag.
110      *
111      * @return int
112      * @throws javax.servlet.jsp.JspTagException
113      * The exception description.
114      */

115     public int doStartTag()
116             throws javax.servlet.jsp.JspTagException JavaDoc {
117         return SKIP_BODY;
118     }
119
120     /**
121      * This part of the tag writes out the appropriate CSS directive to the
122      * page
123      *
124      * @return int as specified by the tag spec
125      * @throws JspTagException upon error
126      */

127     public int doEndTag() throws javax.servlet.jsp.JspTagException JavaDoc {
128         componentToUse = component;
129         String JavaDoc styleSheetName = getComponent();
130
131         if (componentToUse == null || componentToUse.length() == 0) {
132             styleSheetName = "default";
133         }
134
135         String JavaDoc dbName = "default";
136         CurrentLogin cl = (CurrentLogin) pageContext.getAttribute(CurrentLogin.LOGIN_KEY,
137                 PageContext.PAGE_SCOPE);
138
139         if (cl != null) {
140             dbName = cl.getDBName();
141         }
142
143         String JavaDoc contextPath = ((HttpServletRequest JavaDoc) pageContext.getRequest()).getContextPath();
144         String JavaDoc cssSuffix = null;
145         try {
146             cssSuffix = Setup.getValue(dbName, "defaultCSS");
147         } catch (com.jcorporate.expresso.core.db.DBException dbe) {
148             log.warn("Unable to get setup value 'defaultCSS'. You may need to re-run DBCreate get the entry added."
149                     , dbe);
150         }
151
152         FastStringBuffer styleURL = FastStringBuffer.getInstance();
153         try {
154             styleURL.append("<link rel=\"stylesheet\" type=\"text/css\" HREF=\"");
155             styleURL.append(contextPath);
156             styleURL.append("/expresso");
157             styleURL.append("/style/");
158             styleURL.append(styleSheetName);
159
160             if (cssSuffix != null && cssSuffix.length() > 0) {
161                 styleURL.append("-");
162                 styleURL.append(cssSuffix);
163             }
164             styleURL.append(".css");
165
166             styleURL.append("\">");
167
168             JspWriter JavaDoc writer = pageContext.getOut();
169             try {
170                 writer.println(styleURL.toString());
171             } catch (java.io.IOException JavaDoc ioe) {
172                 log.error("Unable to write tag entry: ", ioe);
173             }
174         } finally {
175             styleURL.release();
176         }
177         return EVAL_PAGE;
178     }
179
180
181     /**
182      * Setter method for Name.
183      *
184      * @return java.lang.String
185      */

186     public String JavaDoc getComponent() {
187         return componentToUse;
188     } /* getName() */
189
190     /**
191      * Insert the method's description here.
192      * Creation date: (12/29/2000 %r)
193      *
194      * @param newValue java.lang.String
195      */

196     public void setComponent(java.lang.String JavaDoc newValue) {
197         component = newValue;
198     } /* setName(java.lang.String) */
199
200
201 }
Popular Tags