KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Map JavaDoc;
19
20 import org.apache.avalon.framework.configuration.Configuration;
21 import org.apache.avalon.framework.configuration.ConfigurationException;
22 import org.apache.avalon.framework.parameters.Parameters;
23 import org.apache.cocoon.environment.ObjectModelHelper;
24
25 /**
26  * <p>The {@link RegexpRequestParameterSelector} class defines a selector matching
27  * specific request parameters to configured regular-expression patterns.</p>
28  *
29  * <p>The configuration of an {@link RegexpRequestParameterSelector} follows exactly
30  * what has been outlined in {@link AbstractRegexpSelector} regarting regular
31  * expression patterns, and additionally it requires an extra configuration element
32  * specifying the request parameter whose value needs to be matched:</p>
33  *
34  * <pre>
35  * &lt;map:components&gt;
36  * ...
37  * &lt;map:selectors default="..."&gt;
38  * &lt;map:selector name="..." SRC="org.apache.cocoon.selection...."&gt;
39  * &lt;pattern name="empty"&gt;^$&lt;/pattern&gt;
40  * &lt;pattern name="number"&gt;^[0-9]+$&lt;/pattern&gt;
41  * &lt;pattern name="string"&gt;^.+$&lt;/pattern&gt;
42  * &lt;parameter-name&gt;...&lt;/parameter-name&gt;
43  * &lt;/map:selector&gt;
44  * &lt;/map:selectors&gt;
45  * &lt;/map:components&gt;
46  * </pre>
47  *
48  * <p>If not configured, or if it needs to be overriddent, the parameter name can
49  * also be specified as a <code>&lt;map:parameter&nbsp;.../&gt;</code> inside the
50  * pipeline itself.</p>
51  *
52  * @version CVS $Id: RegexpRequestParameterSelector.java 30941 2004-07-29 19:56:58Z vgritsenko $
53  * @author <a HREF="mailto:pier@apache.org">Pier Fumagalli</a>
54  */

55 public class RegexpRequestParameterSelector extends AbstractRegexpSelector {
56
57     /** <p>The name of the parameter to work on.</p> */
58     protected String JavaDoc parameterName;
59
60     /**
61      * <p>Create a new {@link RegexpRequestParameterSelector} instance.</p>
62      */

63     public RegexpRequestParameterSelector() {
64         super();
65     }
66
67     /**
68      * <p>Configure this instance parsing all regular expression patterns and
69      * storing the parameter name upon which selection occurs.</p>
70      *
71      * @param configuration the {@link Configuration} instance where configured
72      * patterns are defined.
73      * @throws ConfigurationException if one of the regular-expression to configure
74      * could not be compiled.
75      */

76     public void configure(Configuration configuration)
77     throws ConfigurationException {
78         super.configure(configuration);
79         this.parameterName = configuration.getChild("parameter-name").getValue(null);
80     }
81
82     /**
83      * <p>Return the value of the parameter identified by the configured parameter
84      * name, if any.</p>
85      *
86      * @param objectModel the Cocoon object model.
87      * @param parameters the {@link Parameters} associated with the pipeline.
88      * @return the value of the configured request parameter or <b>null</b>.
89      */

90     public Object JavaDoc getSelectorContext(Map JavaDoc objectModel, Parameters parameters) {
91         String JavaDoc name = parameters.getParameter("parameter-name", this.parameterName);
92         if (name == null) {
93             this.getLogger().warn("No parameter name given -- failing.");
94             return null;
95         }
96         return ObjectModelHelper.getRequest(objectModel).getParameter(name);
97     }
98 }
99
Popular Tags