001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.jxpath.servlet;
018
019import java.util.Enumeration;
020import java.util.HashSet;
021
022import javax.servlet.jsp.PageContext;
023
024import org.apache.commons.jxpath.DynamicPropertyHandler;
025
026/**
027 * Implementation of the {@link DynamicPropertyHandler} interface that provides
028 * access to attributes of a {@link PageContext} in all scopes.
029 *
030 * @author Dmitri Plotnikov
031 * @version $Revision: 668329 $ $Date: 2008-06-16 23:59:48 +0200 (Mo, 16 Jun 2008) $
032 */
033public class PageContextHandler implements DynamicPropertyHandler {
034
035    public String[] getPropertyNames(Object pageContext) {
036        HashSet list = new HashSet();
037        Enumeration e =
038            ((PageContext) pageContext).getAttributeNamesInScope(
039                PageContext.PAGE_SCOPE);
040        while (e.hasMoreElements()) {
041            list.add(e.nextElement());
042        }
043        e =
044            ((PageContext) pageContext).getAttributeNamesInScope(
045                PageContext.REQUEST_SCOPE);
046        while (e.hasMoreElements()) {
047            list.add(e.nextElement());
048        }
049        e =
050            ((PageContext) pageContext).getAttributeNamesInScope(
051                PageContext.SESSION_SCOPE);
052        while (e.hasMoreElements()) {
053            list.add(e.nextElement());
054        }
055        e =
056            ((PageContext) pageContext).getAttributeNamesInScope(
057                PageContext.APPLICATION_SCOPE);
058        while (e.hasMoreElements()) {
059            list.add(e.nextElement());
060        }
061        return (String[]) list.toArray(new String[list.size()]);
062    }
063
064    /**
065     * Returns <code>pageContext.findAttribute(property)</code>.
066     * @param pageContext to search
067     * @param property name
068     * @return Object value
069     */
070    public Object getProperty(Object pageContext, String property) {
071        return ((PageContext) pageContext).findAttribute(property);
072    }
073
074    public void setProperty(Object pageContext, String property, Object value) {
075        ((PageContext) pageContext).setAttribute(
076            property,
077            value,
078            PageContext.PAGE_SCOPE);
079    }
080}