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 */
017
018package org.apache.commons.jxpath.servlet;
019
020import java.util.Enumeration;
021
022import javax.servlet.jsp.PageContext;
023
024/**
025 * A lightweight wrapper for {@link PageContext} that restricts access to attributes of the "page" scope. This object is needed so that XPath "foo" would lookup
026 * the attribute "foo" in all scopes, while "$page/foo" would only look in the "page" scope.
027 */
028public class PageScopeContext {
029
030    private final PageContext pageContext;
031
032    /**
033     * Constructs a new PageScopeContext.
034     *
035     * @param pageContext base
036     */
037    public PageScopeContext(final PageContext pageContext) {
038        this.pageContext = pageContext;
039    }
040
041    /**
042     * Gets the value of the specified attribute.
043     *
044     * @param attribute name
045     * @return Object
046     */
047    public Object getAttribute(final String attribute) {
048        return pageContext.getAttribute(attribute, PageContext.PAGE_SCOPE);
049    }
050
051    /**
052     * Returns attributes of the pageContext declared in the "page" scope.
053     *
054     * @return Enumeration of attribute names
055     */
056    public Enumeration<String> getAttributeNames() {
057        return pageContext.getAttributeNamesInScope(PageContext.PAGE_SCOPE);
058    }
059
060    /**
061     * Sets the specified attribute.
062     *
063     * @param attribute to set
064     * @param value     to set
065     */
066    public void setAttribute(final String attribute, final Object value) {
067        pageContext.setAttribute(attribute, value, PageContext.PAGE_SCOPE);
068    }
069}