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.ri;
019
020import java.util.Iterator;
021
022import org.apache.commons.jxpath.CompiledExpression;
023import org.apache.commons.jxpath.JXPathContext;
024import org.apache.commons.jxpath.Pointer;
025import org.apache.commons.jxpath.ri.compiler.Expression;
026
027/**
028 * RI of CompiledExpression.
029 */
030public class JXPathCompiledExpression implements CompiledExpression {
031
032    private final String xpath;
033    private final Expression expression;
034
035    /**
036     * Constructs a new JXPathCompiledExpression.
037     *
038     * @param xpath      source
039     * @param expression compiled
040     */
041    public JXPathCompiledExpression(final String xpath, final Expression expression) {
042        this.xpath = xpath;
043        this.expression = expression;
044    }
045
046    @Override
047    public Pointer createPath(final JXPathContext context) {
048        return ((JXPathContextReferenceImpl) context).createPath(xpath, expression);
049    }
050
051    @Override
052    public Pointer createPathAndSetValue(final JXPathContext context, final Object value) {
053        return ((JXPathContextReferenceImpl) context).createPathAndSetValue(xpath, expression, value);
054    }
055
056    /**
057     * Gets the compiled expression.
058     *
059     * @return Expression
060     */
061    protected Expression getExpression() {
062        return expression;
063    }
064
065    @Override
066    public Pointer getPointer(final JXPathContext context, final String xpath) {
067        return ((JXPathContextReferenceImpl) context).getPointer(xpath, expression);
068    }
069
070    @Override
071    public Object getValue(final JXPathContext context) {
072        return ((JXPathContextReferenceImpl) context).getValue(xpath, expression);
073    }
074
075    @Override
076    public Object getValue(final JXPathContext context, final Class requiredType) {
077        return ((JXPathContextReferenceImpl) context).getValue(xpath, expression, requiredType);
078    }
079
080    /**
081     * Gets the source expression.
082     *
083     * @return String
084     */
085    protected String getXPath() {
086        return xpath;
087    }
088
089    @Override
090    public Iterator iterate(final JXPathContext context) {
091        return ((JXPathContextReferenceImpl) context).iterate(xpath, expression);
092    }
093
094    @Override
095    public Iterator<Pointer> iteratePointers(final JXPathContext context) {
096        return ((JXPathContextReferenceImpl) context).iteratePointers(xpath, expression);
097    }
098
099    @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}