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.scxml2;
018
019import org.apache.commons.scxml2.env.xpath.XPathEvaluator;
020
021/**
022 * Implementation and support of Commons SCXML builtin predicates to support XPath based datamodel operations
023 * for non-XPath languages.
024 *
025 * These static builtin functions delegate to a static {@link }XPathEvaluator} instance.
026 */
027public class XPathBuiltin {
028
029    private static XPathEvaluator evaluator = new XPathEvaluator();
030
031    /**
032     * Optional static setter to change and override the default {@link XPathEvaluator}
033     * @param evaluator A custom evaluator to be used
034     */
035    public static void setEvaluator(XPathEvaluator evaluator) {
036        XPathBuiltin.evaluator = evaluator;
037    }
038
039    /**
040     * Evaluate an xpath expression returning a data value
041     *
042     * @param ctx variable context
043     * @param expression xpath expression
044     * @return the result of the evaluation
045     * @throws SCXMLExpressionException A malformed expression exception
046     * @see Evaluator#eval(Context, String)
047     */
048    public static Object eval(Context ctx, String expression) throws SCXMLExpressionException {
049        return evaluator.eval(ctx, expression);
050    }
051
052    /**
053     * Evaluate an xpath location that returns a data assignable reference or list of references.
054     * Manifests as "location" attributes of <assign> element.
055     *
056     * @param ctx variable context
057     * @param expression expression
058     * @return The location result.
059     * @throws SCXMLExpressionException A malformed expression exception
060     * @see Evaluator#evalLocation(Context, String)
061     */
062    public static Object evalLocation(Context ctx, String expression) throws SCXMLExpressionException {
063        return evaluator.evalLocation(ctx, expression);
064    }
065
066    /**
067     * Determine if an {@link Evaluator#evalLocation(Context, String)} returned result represents an XPath location
068     * @param ctx variable context
069     * @param data result data from {@link Evaluator#evalLocation(Context, String)}
070     * @return true if the data represents an XPath location
071     * @see XPathEvaluator#isXPathLocation(Context, Object)
072     */
073    public static boolean isXPathLocation(Context ctx, Object data) {
074        return evaluator.isXPathLocation(ctx, data);
075    }
076
077    /**
078     * Assigns data to a location
079     *
080     * @param ctx variable context
081     * @param location location expression
082     * @param data the data to assign.
083     * @param type the type of assignment to perform, null assumes {@link Evaluator.AssignType#REPLACE_CHILDREN}
084     * @param attr the name of the attribute to add when using type {@link Evaluator.AssignType#ADD_ATTRIBUTE}
085     * @throws SCXMLExpressionException A malformed expression exception
086     * @see Evaluator#evalAssign(Context, String, Object, Evaluator.AssignType, String)
087     * @see XPathEvaluator#assign(Context, Object, Object, Evaluator.AssignType, String)
088     */
089    public static void assign(Context ctx, Object location, Object data, Evaluator.AssignType type, String attr)
090            throws SCXMLExpressionException {
091        evaluator.assign(ctx, location, data, type, attr);
092    }
093}