View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.scxml2;
18  
19  import org.apache.commons.scxml2.env.xpath.XPathEvaluator;
20  
21  /**
22   * Implementation and support of Commons SCXML builtin predicates to support XPath based datamodel operations
23   * for non-XPath languages.
24   *
25   * These static builtin functions delegate to a static {@link }XPathEvaluator} instance.
26   */
27  public class XPathBuiltin {
28  
29      private static XPathEvaluator evaluator = new XPathEvaluator();
30  
31      /**
32       * Optional static setter to change and override the default {@link XPathEvaluator}
33       * @param evaluator A custom evaluator to be used
34       */
35      public static void setEvaluator(XPathEvaluator evaluator) {
36          XPathBuiltin.evaluator = evaluator;
37      }
38  
39      /**
40       * Evaluate an xpath expression returning a data value
41       *
42       * @param ctx variable context
43       * @param expression xpath expression
44       * @return the result of the evaluation
45       * @throws SCXMLExpressionException A malformed expression exception
46       * @see Evaluator#eval(Context, String)
47       */
48      public static Object eval(Context ctx, String expression) throws SCXMLExpressionException {
49          return evaluator.eval(ctx, expression);
50      }
51  
52      /**
53       * Evaluate an xpath location that returns a data assignable reference or list of references.
54       * Manifests as "location" attributes of <assign> element.
55       *
56       * @param ctx variable context
57       * @param expression expression
58       * @return The location result.
59       * @throws SCXMLExpressionException A malformed expression exception
60       * @see Evaluator#evalLocation(Context, String)
61       */
62      public static Object evalLocation(Context ctx, String expression) throws SCXMLExpressionException {
63          return evaluator.evalLocation(ctx, expression);
64      }
65  
66      /**
67       * Determine if an {@link Evaluator#evalLocation(Context, String)} returned result represents an XPath location
68       * @param ctx variable context
69       * @param data result data from {@link Evaluator#evalLocation(Context, String)}
70       * @return true if the data represents an XPath location
71       * @see XPathEvaluator#isXPathLocation(Context, Object)
72       */
73      public static boolean isXPathLocation(Context ctx, Object data) {
74          return evaluator.isXPathLocation(ctx, data);
75      }
76  
77      /**
78       * Assigns data to a location
79       *
80       * @param ctx variable context
81       * @param location location expression
82       * @param data the data to assign.
83       * @param type the type of assignment to perform, null assumes {@link Evaluator.AssignType#REPLACE_CHILDREN}
84       * @param attr the name of the attribute to add when using type {@link Evaluator.AssignType#ADD_ATTRIBUTE}
85       * @throws SCXMLExpressionException A malformed expression exception
86       * @see Evaluator#evalAssign(Context, String, Object, Evaluator.AssignType, String)
87       * @see XPathEvaluator#assign(Context, Object, Object, Evaluator.AssignType, String)
88       */
89      public static void assign(Context ctx, Object location, Object data, Evaluator.AssignType type, String attr)
90              throws SCXMLExpressionException {
91          evaluator.assign(ctx, location, data, type, attr);
92      }
93  }