KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > core > theme > tag > ThemeTagHandler


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9
10 package org.jboss.portal.core.theme.tag;
11
12 import org.apache.log4j.Logger;
13 import org.jboss.portal.server.ThemeServer;
14 import org.jboss.portal.server.theme.PortalTheme;
15 import org.jboss.portal.server.theme.ThemeConstants;
16 import org.jboss.portal.server.theme.ThemeLink;
17 import org.jboss.portal.server.theme.ThemeScript;
18
19 import javax.servlet.http.HttpServletRequest JavaDoc;
20 import javax.servlet.jsp.JspException JavaDoc;
21 import javax.servlet.jsp.JspWriter JavaDoc;
22 import javax.servlet.jsp.PageContext JavaDoc;
23 import javax.servlet.jsp.tagext.SimpleTagSupport JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27
28 /**
29  * Tag handler for the theme tag.
30  * <p>The theme tag injects theme resource links into the page. Themes are CSS files, and their resources (images).
31  * The theme tag can inject only the resources of one theme at a time. The href and src attributes of the link and
32  * script tags that are being injected are automatically adjusted to the war context they reside in. A theme is defined
33  * in the /WEB-INF/portal-themes.xml</p>
34  *
35  * @author <a HREF="mailto:mholzner@novell.com>Martin Holzner</a>
36  * @version $LastChangedRevision$, $LastChangedDate$
37  * @see PortalTheme
38  * @see ThemeLink
39  * @see ThemeScript
40  */

41 public class ThemeTagHandler
42    extends SimpleTagSupport JavaDoc
43 {
44    private static Logger log = Logger.getLogger(ThemeTagHandler.class);
45    private String JavaDoc themeName;
46
47    /**
48     * Render the markup of this tag.
49     * <p>This tag expects that the theme name is provided via a request attribute, keyed by
50     * <code>ThemeConstants.ATTR_THEME</code>. The theme server (registry of all available themes) also
51     * has to be available via a request attribute (ThemeConstants.ATTR_THEMESERVER). The attribute values are
52     * provided in <code>org.jboss.portal.core.servlet.CoreServlet</code>.</p>
53     *
54     * @throws JspException
55     * @throws IOException
56     * @see ThemeConstants#ATTR_THEME
57     * @see ThemeConstants#ATTR_THEMESERVER
58     * @see org.jboss.portal.core.servlet.CoreServlet
59     */

60    public void doTag() throws JspException JavaDoc, IOException JavaDoc
61    {
62       log.debug("rendering theme ...");
63
64       // get page and region
65
PageContext JavaDoc app = (PageContext JavaDoc)getJspContext();
66       HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc)app.getRequest();
67
68       JspWriter JavaDoc out = this.getJspContext().getOut();
69
70       ThemeServer themeServer = (ThemeServer)request.getAttribute(ThemeConstants.ATTR_THEMESERVER);
71       // if the theme server isn't available, then we're done
72
if (themeServer == null)
73       {
74          log.warn("No theme server available; themes are disabled");
75          return;
76       }
77
78       String JavaDoc themeName = (String JavaDoc)request.getAttribute(ThemeConstants.ATTR_THEME);
79
80       log.debug("got theme to render: " + themeName);
81
82       PortalTheme requestedTheme = null;
83       if (themeName != null && !"".equals(themeName))
84       {
85          requestedTheme = themeServer.getTheme(themeName, false);
86       }
87       if (requestedTheme == null)
88       {
89          // if there is no theme in the request, see if we have one via a tag attribute
90
// which we treat as the default
91
log.debug("requested theme not found. Is there a theme attribute in the tag ? " + getThemeName());
92          requestedTheme = themeServer.getTheme(getThemeName(), false);
93       }
94
95       if (requestedTheme == null)
96       {
97          log.debug("No valid theme to render: " + themeName);
98          return;
99       }
100
101       log.debug("Found valid theme to render: " + themeName);
102
103       List JavaDoc links = requestedTheme.getLinks();
104       for (Iterator JavaDoc i = links.iterator(); i.hasNext();)
105       {
106          ThemeLink link = (ThemeLink)i.next();
107          log.debug("writing theme link: " + link.getLink());
108          out.println(link.getLink().toString());
109       }
110
111       List JavaDoc scripts = requestedTheme.getScripts();
112       for (Iterator JavaDoc s = scripts.iterator(); s.hasNext();)
113       {
114          ThemeScript script = (ThemeScript)s.next();
115          log.debug("writing theme script: " + script.getScript());
116          out.println(script.getScript().toString());
117       }
118       log.debug("done rendering theme");
119    }
120
121    // ----- attribute handlers
122

123    public String JavaDoc getThemeName()
124    {
125       return themeName;
126    }
127
128    public void setThemeName(String JavaDoc name)
129    {
130       themeName = name;
131    }
132 }
133
Popular Tags