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.matching;
18:
19: import org.apache.avalon.framework.parameters.Parameters;
20: import org.apache.avalon.framework.thread.ThreadSafe;
21: import org.apache.cocoon.environment.ObjectModelHelper;
22:
23: import java.util.HashMap;
24: import java.util.Map;
25:
26: /**
27: * This class allows for matching based on a session attribute.
28: * If the specified session attribute exists, its string representation
29: * is retrieved for later sitemap substitution.
30: *
31: * <p><b>Example:</b></p>
32: * <pre>
33: * <map:match type="session-attribute" pattern="style">
34: * <map:read src="{1}"/>
35: * </map:match>
36: * </pre>
37: *
38: * @author <a href="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
39: * @version CVS $Id: SessionAttributeMatcher.java 433543 2006-08-22 06:22:54Z crossley $
40: */
41: public class SessionAttributeMatcher implements Matcher, ThreadSafe {
42: /**
43: * Match method to see if the request attribute exists. If it does
44: * have a value the string represenation of attribute is added to
45: * the array list for later sitemap substitution.
46: *
47: * @param pattern name of session attribute to find
48: * @param objectModel environment passed through via cocoon
49: * @return null or map containing value of session attribute 'pattern'
50: */
51: public Map match(String pattern, Map objectModel,
52: Parameters parameters) {
53:
54: Object attribute = ObjectModelHelper.getRequest(objectModel)
55: .getSession().getAttribute(pattern);
56: if (attribute == null) {
57: return null;
58: } else {
59: Map map = new HashMap();
60: map.put("1", attribute.toString());
61: return map;
62: }
63: }
64: }
|