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 */
017package org.apache.commons.jxpath;
018
019import java.util.Iterator;
020
021/**
022 * Represents a compiled XPath. The interpretation of compiled XPaths
023 * may be faster, because it bypasses the compilation step. The reference
024 * implementation of {@link JXPathContext} also globally caches some of the
025 * results of compilation, so the direct use of JXPathContext is not
026 * always less efficient than the use of CompiledExpression.
027 * <p>
028 * Use CompiledExpression only when there is a need to evaluate the
029 * same expression multiple times and the CompiledExpression can be
030 * conveniently cached.
031 * <p>
032 * To acquire a CompiledExpression, call {@link JXPathContext#compile
033 * JXPathContext.compile}
034 *
035 * @author Dmitri Plotnikov
036 * @version $Revision: 652845 $ $Date: 2008-05-02 13:46:46 -0400 (Fri, 02 May 2008) $
037 */
038public interface CompiledExpression {
039
040    /**
041     * Evaluates the xpath and returns the resulting object. Primitive
042     * types are wrapped into objects.
043     * @param context to evaluate
044     * @return Object
045     */
046    Object getValue(JXPathContext context);
047
048    /**
049     * Evaluates the xpath, converts the result to the specified class and
050     * returns the resulting object.
051     * @param context to evaluate
052     * @param requiredType return type
053     * @return Object
054     */
055    Object getValue(JXPathContext context, Class requiredType);
056
057    /**
058     * Modifies the value of the property described by the supplied xpath.
059     * Will throw an exception if one of the following conditions occurs:
060     * <ul>
061     * <li>The xpath does not in fact describe an existing property
062     * <li>The property is not writable (no public, non-static set method)
063     * </ul>
064     * @param context base
065     * @param value to set
066     */
067    void setValue(JXPathContext context, Object value);
068
069    /**
070     * Creates intermediate elements of
071     * the path by invoking an {@link AbstractFactory}, which should first be
072     * installed on the context by calling {@link JXPathContext#setFactory}.
073     * @param context base
074     * @return Pointer created
075     */
076    Pointer createPath(JXPathContext context);
077
078    /**
079     * The same as setValue, except it creates intermediate elements of
080     * the path by invoking an {@link AbstractFactory}, which should first be
081     * installed on the context by calling {@link JXPathContext#setFactory}.
082     * <p>
083     * Will throw an exception if one of the following conditions occurs:
084     * <ul>
085     * <li>Elements of the xpath aleady exist, by the path does not in
086     *  fact describe an existing property
087     * <li>The AbstractFactory fails to create an instance for an intermediate
088     * element.
089     * <li>The property is not writable (no public, non-static set method)
090     * </ul>
091     * @param context base
092     * @param value to set
093     * @return Pointer created
094     */
095    Pointer createPathAndSetValue(JXPathContext context, Object value);
096
097    /**
098     * Traverses the xpath and returns a Iterator of all results found
099     * for the path. If the xpath matches no properties
100     * in the graph, the Iterator will not be null.
101     * @param context base
102     * @return Iterator
103     */
104    Iterator iterate(JXPathContext context);
105
106    /**
107     * Traverses the xpath and returns a Pointer.
108     * A Pointer provides easy access to a property.
109     * If the xpath matches no properties
110     * in the graph, the pointer will be null.
111     * @param context base
112     * @param xpath string
113     * @return Pointer found
114     */
115    Pointer getPointer(JXPathContext context, String xpath);
116
117    /**
118     * Traverses the xpath and returns an Iterator of Pointers.
119     * A Pointer provides easy access to a property.
120     * If the xpath matches no properties
121     * in the graph, the Iterator be empty, but not null.
122     * @param context to iterate
123     * @return Iterator<Pointer>
124     */
125    Iterator iteratePointers(JXPathContext context);
126
127    /**
128     * Remove the graph element described by this expression.
129     * @param context base
130     */
131    void removePath(JXPathContext context);
132
133    /**
134     * Remove all graph elements described by this expression.
135     * @param context base
136     */
137    void removeAll(JXPathContext context);
138}