001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.selection;
018:
019: import java.util.Map;
020:
021: import org.apache.avalon.framework.configuration.Configuration;
022: import org.apache.avalon.framework.configuration.ConfigurationException;
023: import org.apache.avalon.framework.parameters.Parameters;
024: import org.apache.cocoon.environment.ObjectModelHelper;
025:
026: /**
027: * <p>The {@link RegexpRequestParameterSelector} class defines a selector matching
028: * specific request parameters to configured regular-expression patterns.</p>
029: *
030: * <p>The configuration of an {@link RegexpRequestParameterSelector} follows exactly
031: * what has been outlined in {@link AbstractRegexpSelector} regarting regular
032: * expression patterns, and additionally it requires an extra configuration element
033: * specifying the request parameter whose value needs to be matched:</p>
034: *
035: * <pre>
036: * <map:components>
037: * ...
038: * <map:selectors default="...">
039: * <map:selector name="..." src="org.apache.cocoon.selection....">
040: * <pattern name="empty">^$</pattern>
041: * <pattern name="number">^[0-9]+$</pattern>
042: * <pattern name="string">^.+$</pattern>
043: * <parameter-name>...</parameter-name>
044: * </map:selector>
045: * </map:selectors>
046: * </map:components>
047: * </pre>
048: *
049: * <p>If not configured, or if it needs to be overriddent, the parameter name can
050: * also be specified as a <code><map:parameter .../></code> inside the
051: * pipeline itself.</p>
052: *
053: * @version CVS $Id: RegexpRequestParameterSelector.java 30941 2004-07-29 19:56:58Z vgritsenko $
054: * @author <a href="mailto:pier@apache.org">Pier Fumagalli</a>
055: */
056: public class RegexpRequestParameterSelector extends
057: AbstractRegexpSelector {
058:
059: /** <p>The name of the parameter to work on.</p> */
060: protected String parameterName;
061:
062: /**
063: * <p>Create a new {@link RegexpRequestParameterSelector} instance.</p>
064: */
065: public RegexpRequestParameterSelector() {
066: super ();
067: }
068:
069: /**
070: * <p>Configure this instance parsing all regular expression patterns and
071: * storing the parameter name upon which selection occurs.</p>
072: *
073: * @param configuration the {@link Configuration} instance where configured
074: * patterns are defined.
075: * @throws ConfigurationException if one of the regular-expression to configure
076: * could not be compiled.
077: */
078: public void configure(Configuration configuration)
079: throws ConfigurationException {
080: super .configure(configuration);
081: this .parameterName = configuration.getChild("parameter-name")
082: .getValue(null);
083: }
084:
085: /**
086: * <p>Return the value of the parameter identified by the configured parameter
087: * name, if any.</p>
088: *
089: * @param objectModel the Cocoon object model.
090: * @param parameters the {@link Parameters} associated with the pipeline.
091: * @return the value of the configured request parameter or <b>null</b>.
092: */
093: public Object getSelectorContext(Map objectModel,
094: Parameters parameters) {
095: String name = parameters.getParameter("parameter-name",
096: this .parameterName);
097: if (name == null) {
098: this .getLogger()
099: .warn("No parameter name given -- failing.");
100: return null;
101: }
102: return ObjectModelHelper.getRequest(objectModel).getParameter(
103: name);
104: }
105: }
|