KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > core > theme > render > RendererFactory


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.render;
11
12 import org.apache.log4j.Logger;
13 import org.apache.xpath.CachedXPathAPI;
14 import org.jboss.portal.common.MediaType;
15 import org.jboss.portal.core.theme.render.impl.RenderContextImpl;
16 import org.jboss.portal.core.theme.render.impl.RenderSetImpl;
17 import org.jboss.portal.server.theme.PortalRenderSet;
18 import org.jboss.portal.server.theme.RegionOrientation;
19 import org.jboss.portal.server.theme.render.RenderContext;
20 import org.jboss.portal.server.theme.strategy.AbstractLayoutStrategy;
21 import org.jboss.portal.server.theme.strategy.LayoutStrategy;
22 import org.w3c.dom.Node JavaDoc;
23
24 import javax.activation.MimeTypeParseException JavaDoc;
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.xml.transform.TransformerException JavaDoc;
27
28 /**
29  * Factory to instantiate implementations of the render set and layout strategy.
30  *
31  * @author <a HREF="mailto:mholzner@novell.com>Martin Holzner</a>
32  * @version $LastChangedRevision$, $LastChangedDate$
33  */

34 public class RendererFactory
35 {
36    private static final Logger log = Logger.getLogger(RendererFactory.class);
37
38    private RendererFactory()
39    {
40    }
41
42    /**
43     * Get the defined layout strategy from the provided layout node
44     *
45     * @param loader the classloader of the portal web application that contains the descriptor
46     * to which the provided strategyNode belongs
47     * @param xpath a CachedXPathAPI instance to use to xpath to the needed sub elements of the strategyNode
48     * @param strategyNode the Node in the layout descriptor that represents the strategy
49     * @return an implementation of the LayoutStrategy interface, populated with the values
50     * provided in strategyNode, and sub elements
51     * @throws TransformerException
52     * @throws ClassNotFoundException
53     * @throws MimeTypeParseException
54     * @throws IllegalAccessException
55     * @throws InstantiationException
56     * @see LayoutStrategy
57     */

58    public static AbstractLayoutStrategy getLayoutStrategy(ClassLoader JavaDoc loader, CachedXPathAPI xpath, Node JavaDoc strategyNode)
59       throws TransformerException JavaDoc, ClassNotFoundException JavaDoc, MimeTypeParseException JavaDoc, IllegalAccessException JavaDoc, InstantiationException JavaDoc
60    {
61       AbstractLayoutStrategy strategy = null;
62       Node JavaDoc impl = xpath.selectSingleNode(strategyNode, "./implementation/text()");
63       Node JavaDoc type = xpath.selectSingleNode(strategyNode, "@content-type");
64
65       if (impl != null && impl.getNodeValue() != null && type != null && type.getNodeValue() != null)
66       {
67          strategy = (AbstractLayoutStrategy)loader.loadClass(impl.getNodeValue()).newInstance();
68          MediaType mediaType = MediaType.parseMimeType(type.getNodeValue());
69          strategy.setMediaType(mediaType);
70       }
71
72       return strategy;
73    }
74
75    /**
76     * Get a portal render set based on the provided information in the renderSet node, and its sub elements.
77     * <p>A render set consists of implementations for the 4 renderer interfaces (Regionrenderer, WindowRenderer,
78     * PortletRenderer, DecorationRenderer). A render set is defined either as part of a layout descriptor
79     * (see portal-layouts.xml), or in case of the default renderer set,in WEB-INF/portal-renderSet.xml.</p>
80     *
81     * @param xpath a CachedXPathAPI instance to provide xpath capability to this method
82     * @param renderSet the 'set' Node of the xml definition of the render set
83     * @return a PortalRenderSet containing the the 4 renderer implementations
84     * @throws TransformerException if parsing the set node for the implementation classes fails due to xpath problems
85     * @throws ClassNotFoundException
86     * @throws MimeTypeParseException
87     * @throws IllegalAccessException
88     * @throws InstantiationException
89     * @see PortalRenderSet
90     */

91    public static PortalRenderSet getRenderSet(String JavaDoc appName, String JavaDoc name, ClassLoader JavaDoc loader, CachedXPathAPI xpath, Node JavaDoc renderSet)
92       throws TransformerException JavaDoc, MimeTypeParseException JavaDoc, IllegalAccessException JavaDoc, InstantiationException JavaDoc, ClassNotFoundException JavaDoc
93    {
94       log.debug("getting the renderSet...");
95       String JavaDoc regionRenderer = getRendererImpl(xpath, renderSet, "./region-renderer/text()");
96       String JavaDoc windowRenderer = getRendererImpl(xpath, renderSet, "./window-renderer/text()");
97       String JavaDoc portletRenderer = getRendererImpl(xpath, renderSet, "./portlet-renderer/text()");
98       String JavaDoc decorationRenderer = getRendererImpl(xpath, renderSet, "./decoration-renderer/text()");
99       String JavaDoc contentType = xpath.selectSingleNode(renderSet, "@content-type").getNodeValue();
100
101       return (PortalRenderSet)new RenderSetImpl(appName, name, loader, contentType, regionRenderer,
102          windowRenderer, portletRenderer, decorationRenderer);
103    }
104
105    /**
106     * Get a new instance of a render context, based on the provided values.
107     * <p>The render context is needed for a render set to produce its markup. The render set is used by the region tag
108     * in a layout jsp.</p>
109     *
110     * @param renderSet the render set to use for this request and tag
111     * @param contentType the content/media/mime type of the current request
112     * @param orientation the orientation of the region render tag
113     * @param request the current Servlet request that is being serviced
114     * @return a new instance of a render context
115     * @see RenderContext
116     * @see PortalRenderSet
117     * @see MediaType
118     * @see RegionOrientation
119     */

120    public static RenderContext getRenderContext(PortalRenderSet renderSet,
121                                                 MediaType contentType,
122                                                 RegionOrientation orientation,
123                                                 HttpServletRequest JavaDoc request)
124    {
125       return new RenderContextImpl(renderSet, contentType, orientation, request);
126    }
127
128    private static String JavaDoc getRendererImpl(CachedXPathAPI xpath, Node JavaDoc rendererSet, String JavaDoc expr)
129       throws TransformerException JavaDoc
130    {
131       log.debug("looking for renderer implementation with: " + expr);
132       Node JavaDoc impl = xpath.selectSingleNode(rendererSet, expr);
133       if (impl != null)
134       {
135          log.debug("found " + impl.getNodeValue());
136          return impl.getNodeValue();
137       }
138       return null;
139    }
140 }
141
Popular Tags