View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     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.commons.jxpath.servlet;
18  
19  import java.util.Enumeration;
20  import java.util.HashSet;
21  
22  import javax.servlet.jsp.PageContext;
23  
24  import org.apache.commons.jxpath.DynamicPropertyHandler;
25  
26  /**
27   * Implementation of the {@link DynamicPropertyHandler} interface that provides
28   * access to attributes of a {@link PageContext} in all scopes.
29   *
30   * @author Dmitri Plotnikov
31   * @version $Revision: 668329 $ $Date: 2008-06-16 17:59:48 -0400 (Mon, 16 Jun 2008) $
32   */
33  public class PageContextHandler implements DynamicPropertyHandler {
34  
35      public String[] getPropertyNames(Object pageContext) {
36          HashSet list = new HashSet();
37          Enumeration e =
38              ((PageContext) pageContext).getAttributeNamesInScope(
39                  PageContext.PAGE_SCOPE);
40          while (e.hasMoreElements()) {
41              list.add(e.nextElement());
42          }
43          e =
44              ((PageContext) pageContext).getAttributeNamesInScope(
45                  PageContext.REQUEST_SCOPE);
46          while (e.hasMoreElements()) {
47              list.add(e.nextElement());
48          }
49          e =
50              ((PageContext) pageContext).getAttributeNamesInScope(
51                  PageContext.SESSION_SCOPE);
52          while (e.hasMoreElements()) {
53              list.add(e.nextElement());
54          }
55          e =
56              ((PageContext) pageContext).getAttributeNamesInScope(
57                  PageContext.APPLICATION_SCOPE);
58          while (e.hasMoreElements()) {
59              list.add(e.nextElement());
60          }
61          return (String[]) list.toArray(new String[list.size()]);
62      }
63  
64      /**
65       * Returns <code>pageContext.findAttribute(property)</code>.
66       * @param pageContext to search
67       * @param property name
68       * @return Object value
69       */
70      public Object getProperty(Object pageContext, String property) {
71          return ((PageContext) pageContext).findAttribute(property);
72      }
73  
74      public void setProperty(Object pageContext, String property, Object value) {
75          ((PageContext) pageContext).setAttribute(
76              property,
77              value,
78              PageContext.PAGE_SCOPE);
79      }
80  }