1 /*
2 * Title: PrintableDecoratorMapper
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.Properties;
19
20 /**
21 * The PrintableDecoratorMapper is a sample DecoratorMapper that will
22 * check to see whether 'printable=true' is supplied as a request parameter
23 * and if so, use the specified decorator instead. The name of this decorator
24 * should be supplied in the <code>decorator</code> property.
25 *
26 * <p>The exact 'printable=true' request criteria can be overriden with the
27 * <code>parameter.name</code> and <code>parameter.value</code> properties.</p>
28 *
29 * <p>Although this DecoratorMapper was designed for creating printable versions
30 * of a page, it can be used for much more imaginative purposes.</p>
31 *
32 * @author <a href="mailto:joe@truemesh.com">Joe Walnes</a>
33 * @version $Revision: 1.1 $
34 *
35 * @see com.opensymphony.module.sitemesh.DecoratorMapper
36 */
37 public final class PrintableDecoratorMapper extends AbstractDecoratorMapper {
38 private String decorator, paramName, paramValue;
39
40 public void init(Config config, Properties properties, DecoratorMapper parent) throws InstantiationException {
41 super.init(config, properties, parent);
42 decorator = properties.getProperty("decorator");
43 paramName = properties.getProperty("parameter.name", "printable");
44 paramValue = properties.getProperty("parameter.value", "true");
45 }
46
47 public Decorator getDecorator(HttpServletRequest request, Page page) {
48 Decorator result = null;
49 if (decorator != null && paramValue.equalsIgnoreCase(request.getParameter(paramName))) {
50 result = getNamedDecorator(request, decorator);
51 }
52 return result == null ? super.getDecorator(request, page) : result;
53 }
54 }