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.ArrayList;
021import java.util.Enumeration;
022
023import org.apache.commons.jxpath.DynamicPropertyHandler;
024
025/**
026 * Implementation of the {@link DynamicPropertyHandler} interface that provides access to attributes of a {@link PageScopeContext}.
027 */
028public class PageScopeContextHandler implements DynamicPropertyHandler {
029
030    private static final int DEFAULT_LIST_SIZE = 16;
031
032    /**
033     * Constructs a new instance.
034     */
035    public PageScopeContextHandler() {
036        // empty
037    }
038
039    @Override
040    public Object getProperty(final Object pageScope, final String property) {
041        return ((PageScopeContext) pageScope).getAttribute(property);
042    }
043
044    @Override
045    public String[] getPropertyNames(final Object pageScope) {
046        final Enumeration<String> e = ((PageScopeContext) pageScope).getAttributeNames();
047        final ArrayList<String> list = new ArrayList<>(DEFAULT_LIST_SIZE);
048        while (e.hasMoreElements()) {
049            list.add(e.nextElement());
050        }
051        return list.toArray(new String[list.size()]);
052    }
053
054    @Override
055    public void setProperty(final Object pageScope, final String property, final Object value) {
056        ((PageScopeContext) pageScope).setAttribute(property, value);
057    }
058}