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  
18  package org.apache.commons.jxpath.servlet;
19  
20  import org.apache.commons.jxpath.Variables;
21  
22  /**
23   * Implementation of the Variables interface that provides access to a single object using a reserved name (keyword).
24   */
25  public class KeywordVariables implements Variables {
26  
27      private static final long serialVersionUID = 894145608741325442L;
28  
29      /**
30       * The keyword.
31       */
32      private final String keyword;
33  
34      /**
35       * The value.
36       */
37      private final Object value;
38  
39      /**
40       * Constructs a new KeywordVariables.
41       *
42       * @param keyword The keyword.
43       * @param value  The value.
44       */
45      public KeywordVariables(final String keyword, final Object value) {
46          if (keyword == null) {
47              throw new IllegalArgumentException("keyword cannot be null");
48          }
49          this.keyword = keyword;
50          this.value = value;
51      }
52  
53      @Override
54      public void declareVariable(final String variable, final Object value) {
55          throw new UnsupportedOperationException("Cannot declare new keyword variables.");
56      }
57  
58      @Override
59      public Object getVariable(final String variable) {
60          return isDeclaredVariable(variable) ? value : null;
61      }
62  
63      @Override
64      public boolean isDeclaredVariable(final String variable) {
65          return variable.equals(keyword);
66      }
67  
68      @Override
69      public void undeclareVariable(final String variable) {
70          throw new UnsupportedOperationException("Cannot undeclare keyword variables.");
71      }
72  }