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  
21  import javax.servlet.jsp.PageContext;
22  
23  /**
24   * A lightweight wrapper for {@link PageContext} that restricts access
25   * to attributes of the "page" scope.  This object is needed so that
26   * XPath "foo" would lookup the attribute "foo" in all scopes, while
27   * "$page/foo" would only look in the "page" scope.
28   *
29   * @author Dmitri Plotnikov
30   * @version $Revision: 652845 $ $Date: 2008-05-02 19:46:46 +0200 (Fr, 02 Mai 2008) $
31   */
32  public class PageScopeContext {
33      private PageContext pageContext;
34  
35      /**
36       * Create a new PageScopeContext.
37       * @param pageContext base
38       */
39      public PageScopeContext(PageContext pageContext) {
40          this.pageContext = pageContext;
41      }
42  
43      /**
44       * Returns attributes of the pageContext declared in the "page" scope.
45       * @return Enumeration of attribute names
46       */
47      public Enumeration getAttributeNames() {
48          return pageContext.getAttributeNamesInScope(PageContext.PAGE_SCOPE);
49      }
50  
51      /**
52       * Get the value of the specified attribute.
53       * @param attribute name
54       * @return Object
55       */
56      public Object getAttribute(String attribute) {
57          return pageContext.getAttribute(attribute, PageContext.PAGE_SCOPE);
58      }
59  
60      /**
61       * Set the specified attribute.
62       * @param attribute to set
63       * @param value to set
64       */
65      public void setAttribute(String attribute, Object value) {
66          pageContext.setAttribute(attribute, value, PageContext.PAGE_SCOPE);
67      }
68  }