    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    package org.apache.commons.jxpath.servlet;
    018
    019    import org.apache.commons.jxpath.Variables;
    020
    021    /**
    022     * Implementation of the Variables interface that provides access
    023     * to a single object using a reserved name (keyword).
    024     *
    025     * @author Dmitri Plotnikov
    026     * @version $Revision: 652925 $ $Date: 2008-05-02 17:05:41 -0500 (Fri, 02 May 2008) $
    027     */
    028    public class KeywordVariables implements Variables {
    029        private static final long serialVersionUID = 894145608741325442L;
    030
    031        private String keyword;
    032        private Object object;
    033
    034        /**
    035         * Create a new KeywordVariables.
    036         * @param keyword String
    037         * @param object value
    038         */
    039        public KeywordVariables(String keyword, Object object) {
    040            if (keyword == null) {
    041                throw new IllegalArgumentException("keyword cannot be null");
    042            }
    043            this.keyword = keyword;
    044            this.object = object;
    045        }
    046
    047        public boolean isDeclaredVariable(String variable) {
    048            return variable.equals(keyword);
    049        }
    050
    051        public Object getVariable(String variable) {
    052            return isDeclaredVariable(variable) ? object : null;
    053        }
    054
    055        public void declareVariable(String variable, Object value) {
    056            throw new UnsupportedOperationException(
    057                "Cannot declare new keyword variables.");
    058        }
    059
    060        public void undeclareVariable(String variable) {
    061            throw new UnsupportedOperationException(
    062                "Cannot undeclare keyword variables.");
    063        }
    064    }