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