01: /*
02: * Title: PageDecoratorMapper
03: * Description:
04: *
05: * This software is published under the terms of the OpenSymphony Software
06: * License version 1.1, of which a copy has been included with this
07: * distribution in the LICENSE.txt file.
08: */
09:
10: package com.opensymphony.module.sitemesh.mapper;
11:
12: import com.opensymphony.module.sitemesh.Config;
13: import com.opensymphony.module.sitemesh.Decorator;
14: import com.opensymphony.module.sitemesh.DecoratorMapper;
15: import com.opensymphony.module.sitemesh.Page;
16:
17: import javax.servlet.http.HttpServletRequest;
18: import java.util.*;
19:
20: /**
21: * The PageDecoratorMapper allows the actual Page to determine the Decorator to be
22: * used.
23: *
24: * <p>The 'meta.decorator' and 'decorator' properties of the page are accessed
25: * and if any of them contain the name of a valid Decorator, that Decorator shall
26: * be applied.</p>
27: *
28: * <p>As an example, if HTML is being used, the Decorator could be chosen by using
29: * a <code><html decorator="mydecorator"></code> root tag <i>or</i> by using a
30: * <code><meta name="decorator" content="mydecorator"></code> tag in the header.</p>
31: *
32: * <p>The actual properties to query are specified by passing properties to the mapper using the
33: * <code>property.?</code> prefix. As the properties are stored in a Map, each key has to be unique.
34: * Example: property.1=decorator, property.2=meta.decorator .</p>
35: *
36: * @author <a href="mailto:joe@truemesh.com">Joe Walnes</a>
37: * @version $Revision: 1.2 $
38: *
39: * @see com.opensymphony.module.sitemesh.DecoratorMapper
40: */
41: public class PageDecoratorMapper extends AbstractDecoratorMapper {
42: private List pageProps = null;
43:
44: public void init(Config config, Properties properties,
45: DecoratorMapper parent) throws InstantiationException {
46: super .init(config, properties, parent);
47: pageProps = new ArrayList();
48: Iterator i = properties.entrySet().iterator();
49: while (i.hasNext()) {
50: Map.Entry entry = (Map.Entry) i.next();
51: String key = (String) entry.getKey();
52: if (key.startsWith("property")) {
53: pageProps.add(entry.getValue());
54: }
55: }
56: }
57:
58: public Decorator getDecorator(HttpServletRequest request, Page page) {
59: Decorator result = null;
60: Iterator i = pageProps.iterator();
61: while (i.hasNext()) {
62: String propName = (String) i.next();
63: result = getByProperty(request, page, propName);
64: if (result != null)
65: break;
66: }
67: return result == null ? super .getDecorator(request, page)
68: : result;
69: }
70:
71: private Decorator getByProperty(HttpServletRequest request, Page p,
72: String name) {
73: if (p.isPropertySet(name)) {
74: return getNamedDecorator(request, p.getProperty(name));
75: }
76: return null;
77: }
78: }
|