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.ajax;
18:
19: import java.util.Map;
20:
21: import org.apache.avalon.framework.parameters.Parameters;
22: import org.apache.cocoon.environment.ObjectModelHelper;
23: import org.apache.cocoon.environment.Request;
24: import org.apache.cocoon.selection.AbstractSwitchSelector;
25: import org.apache.commons.lang.BooleanUtils;
26:
27: /**
28: * Choose a select branch depending on if the current request is an Ajax request.
29: * The test value can be either "<code>true</code>" or "<code>false</code>".
30: *
31: * <pre>
32: * <map:select type="ajax-request">
33: * <map:when test="true">
34: * ... ajax request ...
35: * </map:when>
36: * <map:otherwise>
37: * ... non ajax request ...
38: * </map:otherwise>
39: * </map:select>
40: * </pre>
41: *
42: * @since 2.1.8
43: * @version $Id: AjaxRequestSelector.java 448479 2006-09-21 07:33:36Z crossley $
44: */
45: public class AjaxRequestSelector extends AbstractSwitchSelector {
46:
47: public Object getSelectorContext(Map objectModel,
48: Parameters parameters) {
49: Request req = ObjectModelHelper.getRequest(objectModel);
50: return BooleanUtils.toBooleanObject(AjaxHelper
51: .isAjaxRequest(req));
52: }
53:
54: public boolean select(String expression, Object selectorContext) {
55: boolean test = BooleanUtils.toBoolean(expression);
56: return test == ((Boolean) selectorContext).booleanValue();
57: }
58: }
|