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.env.jexl;
018
019import org.apache.commons.scxml2.Builtin;
020import org.apache.commons.scxml2.SCXMLExpressionException;
021import org.apache.commons.scxml2.XPathBuiltin;
022
023/**
024 * Global JEXL namespace functor, providing the standard SCXML In() operator and the Commons SCXML extensions
025 * for Data() and Location() to support XPath datamodel access.
026 */
027public final class JexlBuiltin {
028    /**
029     * The context currently in use for evaluation.
030     */
031    private final JexlContext context;
032
033    /**
034     * Creates a new instance, wraps the context.
035     * @param ctxt the context in use
036     */
037    public JexlBuiltin(final JexlContext ctxt) {
038        context = ctxt;
039    }
040
041    /**
042     * Provides the SCXML standard In() predicate for SCXML documents.
043     * @param state The State ID to compare with
044     * @return true if this state is currently active
045     */
046    public boolean In(final String state) {
047        return Builtin.isMember(context, state);
048    }
049
050    /**
051     * Provides the Commons SCXML Data() predicate extension for SCXML documents.
052     * @param expression the XPath expression
053     * @return the data matching the expression
054     */
055    public Object Data(final String expression) throws SCXMLExpressionException {
056        return XPathBuiltin.eval(context, expression);
057    }
058
059    /**
060     * Provides the Commons SCXML Location() predicate extension for SCXML documents.
061     * @param expression the XPath expression
062     * @return the location matching the expression
063     */
064    public Object Location(final String expression) throws SCXMLExpressionException {
065        return XPathBuiltin.evalLocation(context, expression);
066    }
067}