1 /*
2 * Title: PageDecoratorMapper
3 * Description:
4 *
5 * This software is published under the terms of the OpenSymphony Software
6 * License version 1.1, of which a copy has been included with this
7 * distribution in the LICENSE.txt file.
8 */
9
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, DecoratorMapper parent) throws InstantiationException {
45 super.init(config, properties, parent);
46 pageProps = new ArrayList();
47 Iterator i = properties.entrySet().iterator();
48 while (i.hasNext()) {
49 Map.Entry entry = (Map.Entry) i.next();
50 String key = (String) entry.getKey();
51 if (key.startsWith("property")) {
52 pageProps.add(entry.getValue());
53 }
54 }
55 }
56
57 public Decorator getDecorator(HttpServletRequest request, Page page) {
58 Decorator result = null;
59 Iterator i = pageProps.iterator();
60 while (i.hasNext()) {
61 String propName = (String)i.next();
62 result = getByProperty(request, page, propName);
63 if (result != null) break;
64 }
65 return result == null ? super.getDecorator(request, page) : result;
66 }
67
68 private Decorator getByProperty(HttpServletRequest request, Page p, String name) {
69 if (p.isPropertySet(name)) {
70 return getNamedDecorator(request, p.getProperty(name));
71 }
72 return null;
73 }
74 }