01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.selection;
18:
19: import org.apache.avalon.framework.configuration.Configurable;
20: import org.apache.avalon.framework.configuration.Configuration;
21: import org.apache.avalon.framework.configuration.ConfigurationException;
22: import org.apache.avalon.framework.logger.AbstractLogEnabled;
23: import org.apache.avalon.framework.parameters.Parameters;
24: import org.apache.avalon.framework.thread.ThreadSafe;
25: import org.apache.cocoon.environment.ObjectModelHelper;
26:
27: import java.util.Map;
28:
29: /**
30: * A <code>Selector</code> that matches a string against a configurable request
31: * attribute's string represenation.
32: *
33: * <p><b>Global and local configuration</b></p>
34: * <table border="1">
35: * <tr><td><code>attribute-name</code></td><td>String identifying the request attribute.</td></tr>
36: * </table>
37: *
38: * @author <a href="mailto:haul@apache.org">Christian Haul</a>
39: * @author <a href="mailto:sylvain@apache.org">Sylvain Wallez</a>
40: * @author <a href="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
41: * @version CVS $Id: RequestAttributeSelector.java 433543 2006-08-22 06:22:54Z crossley $
42: */
43: public class RequestAttributeSelector extends AbstractLogEnabled
44: implements Configurable, ThreadSafe, Selector {
45:
46: protected String defaultName;
47:
48: public void configure(Configuration config)
49: throws ConfigurationException {
50: this .defaultName = config.getChild("attribute-name").getValue(
51: null);
52: }
53:
54: public boolean select(String expression, Map objectModel,
55: Parameters parameters) {
56: String name = parameters.getParameter("attribute-name",
57: this .defaultName);
58:
59: if (name == null) {
60: getLogger().warn("No attribute name given -- failing.");
61: return false;
62: }
63:
64: Object value = ObjectModelHelper.getRequest(objectModel)
65: .getAttribute(name);
66: if (value == null) {
67: getLogger().debug(
68: "Request attribute '" + name
69: + "' not set -- failing.");
70: return false;
71: }
72:
73: return value.toString().equals(expression);
74: }
75: }
|