    1   /*
    2    * Copyright 1999-2004 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.commons.jxpath.servlet;
    17
    18  import java.util.Enumeration;
    19  import java.util.HashSet;
    20
    21  import javax.servlet.jsp.PageContext;
    22
    23  import org.apache.commons.jxpath.DynamicPropertyHandler;
    24
    25  /***
    26   * Implementation of the DynamicPropertyHandler interface that provides
    27   * access to attributes of a PageContext in all scopes.
    28   *
    29   * @author Dmitri Plotnikov
    30   * @version $Revision: 1.6 $ $Date: 2004/05/08 15:10:49 $
    31   */
    32  public class PageContextHandler implements DynamicPropertyHandler {
    33
    34      public String[] getPropertyNames(Object pageContext) {
    35          HashSet list = new HashSet();
    36          Enumeration e =
    37              ((PageContext) pageContext).getAttributeNamesInScope(
    38                  PageContext.PAGE_SCOPE);
    39          while (e.hasMoreElements()) {
    40              list.add(e.nextElement());
    41          }
    42          e =
    43              ((PageContext) pageContext).getAttributeNamesInScope(
    44                  PageContext.REQUEST_SCOPE);
    45          while (e.hasMoreElements()) {
    46              list.add(e.nextElement());
    47          }
    48          e =
    49              ((PageContext) pageContext).getAttributeNamesInScope(
    50                  PageContext.SESSION_SCOPE);
    51          while (e.hasMoreElements()) {
    52              list.add(e.nextElement());
    53          }
    54          e =
    55              ((PageContext) pageContext).getAttributeNamesInScope(
    56                  PageContext.APPLICATION_SCOPE);
    57          while (e.hasMoreElements()) {
    58              list.add(e.nextElement());
    59          }
    60          return (String[]) list.toArray(new String[0]);
    61      }
    62
    63      /***
    64       * Returns <code>pageContext.findAttribute(property)</code>.
    65       */
    66      public Object getProperty(Object pageContext, String property) {
    67          return ((PageContext) pageContext).findAttribute(property);
    68      }
    69
    70      public void setProperty(
    71          Object pageContext,
    72          String property,
    73          Object value)
    74      {
    75          ((PageContext) pageContext).setAttribute(
    76              property,
    77              value,
    78              PageContext.PAGE_SCOPE);
    79      }
    80  }