KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > ajax > AjaxRequestSelector


1 /*
2  * Copyright 1999-2005 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.ajax;
17
18 import java.util.Map JavaDoc;
19
20 import org.apache.avalon.framework.parameters.Parameters;
21 import org.apache.avalon.framework.thread.ThreadSafe;
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  * &lt;map:select type="ajax-request"&gt;
33  * &lt;map:when test="true"&gt;
34  * ... ajax request ...
35  * &lt;/map:when&gt;
36  * &lt;map:otherwise&gt;
37  * ... non ajax request ...
38  * &lt;/map:otherwise&gt;
39  * &lt;/map:select&gt;
40  * </pre>
41  *
42  * @since 2.1.8
43  * @version $Id: AjaxRequestSelector.java 292158 2005-09-28 10:24:51Z sylvain $
44  */

45 public class AjaxRequestSelector extends AbstractSwitchSelector implements ThreadSafe {
46     
47     public Object JavaDoc getSelectorContext(Map JavaDoc objectModel, Parameters parameters) {
48         Request req = ObjectModelHelper.getRequest(objectModel);
49         return BooleanUtils.toBooleanObject(AjaxHelper.isAjaxRequest(req));
50     }
51
52     public boolean select(String JavaDoc expression, Object JavaDoc selectorContext) {
53         boolean test = BooleanUtils.toBoolean(expression);
54
55         return test == ((Boolean JavaDoc)selectorContext).booleanValue();
56     }
57 }
58
Popular Tags