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.model;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  import java.util.Map;
22  
23  import org.apache.commons.scxml2.ActionExecutionContext;
24  import org.apache.commons.scxml2.Context;
25  import org.apache.commons.scxml2.Evaluator;
26  import org.apache.commons.scxml2.SCXMLExpressionException;
27  
28  /**
29   * A <code>ParamsContainer</code> represents an element in the SCXML
30   * document that may have one or more &lt;param/&gt; children which are used to
31   * produce payload for events or external communication.
32   */
33  public abstract class ParamsContainer extends PayloadProvider {
34  
35      /**
36       * The List of the params to be sent
37       */
38      private final List<Param> paramsList = new ArrayList<Param>();
39  
40      /**
41       * Get the list of {@link Param}s.
42       *
43       * @return List The params list.
44       */
45      public List<Param> getParams() {
46          return paramsList;
47      }
48  
49      /**
50       * Adds data to the payload data map based on the {@link Param}s of this {@link ParamsContainer}
51       * @param exctx The ActionExecutionContext
52       * @param payload the payload data map to be updated
53       * @throws ModelException if this action has not an EnterableState as parent
54       * @throws SCXMLExpressionException if a malformed or invalid expression is evaluated
55       * @see PayloadProvider#addToPayload(String, Object, java.util.Map)
56       */
57      protected void addParamsToPayload(ActionExecutionContext exctx, Map<String, Object> payload)
58              throws ModelException, SCXMLExpressionException {
59          if (!paramsList.isEmpty()) {
60              EnterableState parentState = getParentEnterableState();
61              Context ctx = exctx.getContext(parentState);
62              try {
63                  ctx.setLocal(getNamespacesKey(), getNamespaces());
64                  Evaluator evaluator = exctx.getEvaluator();
65                  Object paramValue;
66                  for (Param p : paramsList) {
67                      if (p.getExpr() != null) {
68                          paramValue = evaluator.eval(ctx, p.getExpr());
69                      }
70                      else if (p.getLocation() != null) {
71                          paramValue = evaluator.eval(ctx, p.getLocation());
72                      }
73                      else {
74                          // ignore invalid param definition
75                          continue;
76                      }
77                      addToPayload(p.getName(), paramValue, payload);
78                  }
79              }
80              finally {
81                  ctx.setLocal(getNamespacesKey(), null);
82              }
83          }
84      }
85  }