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;
020
021import javax.servlet.jsp.PageContext;
022
023/**
024 * A lightweight wrapper for {@link PageContext} that restricts access
025 * to attributes of the "page" scope.  This object is needed so that
026 * XPath "foo" would lookup the attribute "foo" in all scopes, while
027 * "$page/foo" would only look in the "page" scope.
028 *
029 * @author Dmitri Plotnikov
030 * @version $Revision: 652845 $ $Date: 2008-05-02 19:46:46 +0200 (Fr, 02 Mai 2008) $
031 */
032public class PageScopeContext {
033    private PageContext pageContext;
034
035    /**
036     * Create a new PageScopeContext.
037     * @param pageContext base
038     */
039    public PageScopeContext(PageContext pageContext) {
040        this.pageContext = pageContext;
041    }
042
043    /**
044     * Returns attributes of the pageContext declared in the "page" scope.
045     * @return Enumeration of attribute names
046     */
047    public Enumeration getAttributeNames() {
048        return pageContext.getAttributeNamesInScope(PageContext.PAGE_SCOPE);
049    }
050
051    /**
052     * Get the value of the specified attribute.
053     * @param attribute name
054     * @return Object
055     */
056    public Object getAttribute(String attribute) {
057        return pageContext.getAttribute(attribute, PageContext.PAGE_SCOPE);
058    }
059
060    /**
061     * Set the specified attribute.
062     * @param attribute to set
063     * @param value to set
064     */
065    public void setAttribute(String attribute, Object value) {
066        pageContext.setAttribute(attribute, value, PageContext.PAGE_SCOPE);
067    }
068}