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.ri;
19  
20  import java.util.Iterator;
21  
22  import org.apache.commons.jxpath.CompiledExpression;
23  import org.apache.commons.jxpath.JXPathContext;
24  import org.apache.commons.jxpath.Pointer;
25  import org.apache.commons.jxpath.ri.compiler.Expression;
26  
27  /**
28   * RI of CompiledExpression.
29   */
30  public class JXPathCompiledExpression implements CompiledExpression {
31  
32      private final String xpath;
33      private final Expression expression;
34  
35      /**
36       * Constructs a new JXPathCompiledExpression.
37       *
38       * @param xpath      source
39       * @param expression compiled
40       */
41      public JXPathCompiledExpression(final String xpath, final Expression expression) {
42          this.xpath = xpath;
43          this.expression = expression;
44      }
45  
46      @Override
47      public Pointer createPath(final JXPathContext context) {
48          return ((JXPathContextReferenceImpl) context).createPath(xpath, expression);
49      }
50  
51      @Override
52      public Pointer createPathAndSetValue(final JXPathContext context, final Object value) {
53          return ((JXPathContextReferenceImpl) context).createPathAndSetValue(xpath, expression, value);
54      }
55  
56      /**
57       * Gets the compiled expression.
58       *
59       * @return Expression
60       */
61      protected Expression getExpression() {
62          return expression;
63      }
64  
65      @Override
66      public Pointer getPointer(final JXPathContext context, final String xpath) {
67          return ((JXPathContextReferenceImpl) context).getPointer(xpath, expression);
68      }
69  
70      @Override
71      public Object getValue(final JXPathContext context) {
72          return ((JXPathContextReferenceImpl) context).getValue(xpath, expression);
73      }
74  
75      @Override
76      public Object getValue(final JXPathContext context, final Class requiredType) {
77          return ((JXPathContextReferenceImpl) context).getValue(xpath, expression, requiredType);
78      }
79  
80      /**
81       * Gets the source expression.
82       *
83       * @return String
84       */
85      protected String getXPath() {
86          return xpath;
87      }
88  
89      @Override
90      public Iterator iterate(final JXPathContext context) {
91          return ((JXPathContextReferenceImpl) context).iterate(xpath, expression);
92      }
93  
94      @Override
95      public Iterator<Pointer> iteratePointers(final JXPathContext context) {
96          return ((JXPathContextReferenceImpl) context).iteratePointers(xpath, expression);
97      }
98  
99      @Override
100     public void removeAll(final JXPathContext context) {
101         ((JXPathContextReferenceImpl) context).removeAll(xpath, expression);
102     }
103 
104     @Override
105     public void removePath(final JXPathContext context) {
106         ((JXPathContextReferenceImpl) context).removePath(xpath, expression);
107     }
108 
109     @Override
110     public void setValue(final JXPathContext context, final Object value) {
111         ((JXPathContextReferenceImpl) context).setValue(xpath, expression, value);
112     }
113 
114     @Override
115     public String toString() {
116         return xpath;
117     }
118 }