KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > selection > RequestParameterSelector


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.selection;
17
18 import org.apache.avalon.framework.configuration.Configurable;
19 import org.apache.avalon.framework.configuration.Configuration;
20 import org.apache.avalon.framework.configuration.ConfigurationException;
21 import org.apache.avalon.framework.parameters.Parameters;
22 import org.apache.avalon.framework.thread.ThreadSafe;
23 import org.apache.cocoon.environment.ObjectModelHelper;
24
25 import java.util.Map JavaDoc;
26
27 /**
28  * A <code>Selector</code> that matches a string against a configurable request parameter's value.
29  *
30  * <p><b>Global and local configuration</b></p>
31  * <table border="1">
32  * <tr><td><code>parameter-name</code></td><td>Name of the request
33  * parameter whose value to match against</td></tr>
34  * </table>
35  *
36  * @author <a HREF="mailto:haul@apache.org">Christian Haul</a>
37  * @author <a HREF="mailto:sylvain@apache.org">Sylvain Wallez</a>
38  * @author <a HREF="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
39  * @author <a HREF="mailto:crafterm@apache.org">Marcus Crafter</a>
40  * @version CVS $Id: RequestParameterSelector.java 30932 2004-07-29 17:35:38Z vgritsenko $
41  */

42 public class RequestParameterSelector extends AbstractSwitchSelector
43   implements Configurable, ThreadSafe {
44
45     protected String JavaDoc defaultName;
46
47     public void configure(Configuration config) throws ConfigurationException {
48         this.defaultName = config.getChild("parameter-name").getValue(null);
49     }
50
51     public Object JavaDoc getSelectorContext(Map JavaDoc objectModel, Parameters parameters) {
52         
53         String JavaDoc name = parameters.getParameter("parameter-name", this.defaultName);
54
55         if (name == null) {
56             getLogger().warn("No parameter name given -- failing.");
57             return null;
58         }
59
60         return ObjectModelHelper.getRequest(objectModel).getParameter(name);
61     }
62
63     public boolean select(String JavaDoc expression, Object JavaDoc selectorContext) {
64         if (selectorContext == null) {
65             getLogger().debug("Request parameter '" + selectorContext + "' not set -- failing.");
66             return false;
67         }
68
69         return selectorContext.equals(expression);
70     }
71 }
72
Popular Tags