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.model; 018 019import java.util.ArrayList; 020import java.util.List; 021import java.util.Map; 022 023import org.apache.commons.scxml2.ActionExecutionContext; 024import org.apache.commons.scxml2.Context; 025import org.apache.commons.scxml2.Evaluator; 026import org.apache.commons.scxml2.SCXMLExpressionException; 027 028/** 029 * A <code>ParamsContainer</code> represents an element in the SCXML 030 * document that may have one or more <param/> children which are used to 031 * produce payload for events or external communication. 032 */ 033public abstract class ParamsContainer extends PayloadProvider { 034 035 /** 036 * The List of the params to be sent 037 */ 038 private final List<Param> paramsList = new ArrayList<Param>(); 039 040 /** 041 * Get the list of {@link Param}s. 042 * 043 * @return List The params list. 044 */ 045 public List<Param> getParams() { 046 return paramsList; 047 } 048 049 /** 050 * Adds data to the payload data map based on the {@link Param}s of this {@link ParamsContainer} 051 * @param exctx The ActionExecutionContext 052 * @param payload the payload data map to be updated 053 * @throws ModelException if this action has not an EnterableState as parent 054 * @throws SCXMLExpressionException if a malformed or invalid expression is evaluated 055 * @see PayloadProvider#addToPayload(String, Object, java.util.Map) 056 */ 057 protected void addParamsToPayload(ActionExecutionContext exctx, Map<String, Object> payload) 058 throws ModelException, SCXMLExpressionException { 059 if (!paramsList.isEmpty()) { 060 EnterableState parentState = getParentEnterableState(); 061 Context ctx = exctx.getContext(parentState); 062 try { 063 ctx.setLocal(getNamespacesKey(), getNamespaces()); 064 Evaluator evaluator = exctx.getEvaluator(); 065 Object paramValue; 066 for (Param p : paramsList) { 067 if (p.getExpr() != null) { 068 paramValue = evaluator.eval(ctx, p.getExpr()); 069 } 070 else if (p.getLocation() != null) { 071 paramValue = evaluator.eval(ctx, p.getLocation()); 072 } 073 else { 074 // ignore invalid param definition 075 continue; 076 } 077 addToPayload(p.getName(), paramValue, payload); 078 } 079 } 080 finally { 081 ctx.setLocal(getNamespacesKey(), null); 082 } 083 } 084 } 085}