01: /*
02: * Title: ParameterDecoratorMapper
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.Properties;
19:
20: /**
21: * The ParameterDecoratorMapper will map a suitable decorator based on request
22: * parameters.
23: *
24: * <p>The ParameterDecoratorMapper is configured via three properties.</p>
25: *
26: * <p><code>decorator.parameter</code> - the parameter which contains the name of the decorator which will be mapped.
27: * The default is "decorator".</p>
28: *
29: * <p>For example if <code>decorator.parameter</code> is "foobar" then
30: * myurl.jsp?foobar=mydecorator will map to the decorator named "mydecorator".</p>
31: *
32: * <p>You can also supply an optional 'confirmation parameter'.
33: * The decorator will only be mapped if the parameter named <code>parameter.name</code> is
34: * in the request URI and the value of that parameter is equal to the
35: * <code>parameter.value</code> property.</p>
36: *
37: * <p>For example assuming parameter.name=confirm and parameter.value=true
38: * the URI myurl.jsp?decorator=mydecorator&confirm=true will map the decorator mydecorator.
39: * where as the URIs myurl.jsp?decorator=mydecorator and myurl.jsp?decorator=mydecorator&confirm=false will
40: * not return any decorator.</p>
41: *
42: * @author <a href="mailto:mcannon@internet.com">Mike Cannon-Brookes</a>
43: * @version $Revision: 1.3 $
44: *
45: * @see com.opensymphony.module.sitemesh.DecoratorMapper
46: */
47: public class ParameterDecoratorMapper extends AbstractDecoratorMapper {
48: private String decoratorParameter = null, paramName = null,
49: paramValue = null;
50:
51: public void init(Config config, Properties properties,
52: DecoratorMapper parent) throws InstantiationException {
53: super .init(config, properties, parent);
54: decoratorParameter = properties.getProperty(
55: "decorator.parameter", "decorator");
56: paramName = properties.getProperty("parameter.name", null);
57: paramValue = properties.getProperty("parameter.value", null);
58: }
59:
60: public Decorator getDecorator(HttpServletRequest request, Page page) {
61: Decorator result = null;
62: String decoratorParamValue = request
63: .getParameter(decoratorParameter);
64:
65: if ((paramName == null || paramValue.equals(request
66: .getParameter(paramName)))
67: && decoratorParamValue != null
68: && !decoratorParamValue.trim().equals("")) {
69: result = getNamedDecorator(request, decoratorParamValue);
70: }
71: return result == null ? super.getDecorator(request, page)
72: : result;
73: }
74: }
|