ludc
2025-01-16 986aa62ed00bee39363bab41b4eeb8259d446efd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jbpm.jpdl.internal.activity;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import org.jbpm.jpdl.internal.xml.JpdlParser;
import org.jbpm.pvm.internal.util.XmlUtil;
import org.jbpm.pvm.internal.wire.Descriptor;
import org.jbpm.pvm.internal.wire.WireContext;
import org.jbpm.pvm.internal.wire.xml.WireParser;
import org.jbpm.pvm.internal.xml.Parse;
import org.w3c.dom.Element;
 
 
/**
 * @author Tom Baeyens
 */
public class SubProcessBinding extends JpdlBinding {
 
  public SubProcessBinding() {
    super("sub-process");
  }
 
  public Object parseJpdl(Element element, Parse parse, JpdlParser parser) {
    SubProcessActivity subProcessActivity = new SubProcessActivity();
    
    String subProcessKey = XmlUtil.attribute(element, "sub-process-key");
    subProcessActivity.setSubProcessKey(subProcessKey);
    
    String subProcessId = XmlUtil.attribute(element, "sub-process-id");
    subProcessActivity.setSubProcessId(subProcessId);
    
    List<SubProcessInParameterImpl> inParameters = new ArrayList<SubProcessInParameterImpl>();
    for (Element inElement: XmlUtil.elements(element, "parameter-in")) {
      SubProcessInParameterImpl inParameter = new SubProcessInParameterImpl();
      parseParameter(inElement, inParameter);
      inParameters.add(inParameter);
 
      if (inParameter.getSubVariableName()==null) {
        parse.addProblem("no 'subvar' specified for parameter-in", element);
      }
      if ( (inParameter.getExpression()==null)
           && (inParameter.getVariableName()==null)
         ) {
        parse.addProblem("no 'expr' or 'variable' specified for parameter-in '"+inParameter.getSubVariableName()+"'", element);
      }
      if ( (inParameter.getExpression()!=null)
           && (inParameter.getVariableName()!=null)
         ) {
        parse.addProblem("attributes 'expr' and 'variable' are mutually exclusive on parameter-in", element);
      }
    }
    subProcessActivity.setInParameters(inParameters);
 
    List<SubProcessOutParameterImpl> outParameters = new ArrayList<SubProcessOutParameterImpl>();
    for (Element outElement: XmlUtil.elements(element, "parameter-out")) {
      SubProcessOutParameterImpl outParameter = new SubProcessOutParameterImpl();
      parseParameter(outElement, outParameter);
      outParameters.add(outParameter);
      
      if (outParameter.getVariableName()==null) {
        parse.addProblem("no 'variable' specified for parameter-in", element);
      }
      if ( (outParameter.getExpression()==null)
           && (outParameter.getSubVariableName()==null)
         ) {
        parse.addProblem("no 'expr' or 'subvar' specified for parameter-out '"+outParameter.getVariableName()+"'", element);
      }
      if ( (outParameter.getExpression()!=null)
           && (outParameter.getSubVariableName()!=null)
         ) {
        parse.addProblem("attributes 'expr' and 'subvar' are mutually exclusive on parameter-out '"+outParameter.getVariableName()+"'", element);
      }
    }
    subProcessActivity.setOutParameters(outParameters);
 
    Map<String, String> swimlaneMappings = parseSwimlaneMappings(element, parse);
    subProcessActivity.setSwimlaneMappings(swimlaneMappings);
 
    Map<Object, String> outcomeVariableMappings = new HashMap<Object, String>();
 
    String outcomeExpression = XmlUtil.attribute(element, "outcome");
    if (outcomeExpression!=null) {
      subProcessActivity.setOutcomeExpression(outcomeExpression);
      
      for (Element transitionElement: XmlUtil.elements(element, "transition")) {
        Element outcomeValueElement = XmlUtil.element(transitionElement, "outcome-value");
        if (outcomeValueElement!=null) {
          String transitionName = XmlUtil.attribute(transitionElement, "name");
          if (transitionName==null) {
            parse.addProblem("transitions with an outcome-value must have a name", transitionElement);
          }
          Element valueElement = XmlUtil.element(outcomeValueElement);
          if (valueElement!=null) {
            Descriptor descriptor = (Descriptor) WireParser.getInstance().parseElement(valueElement, parse);
            Object value = WireContext.create(descriptor);
            outcomeVariableMappings.put(value, transitionName);
          } else {
            parse.addProblem("outcome-value must contain exactly one element", outcomeValueElement);
          }
        }
      }
 
    }
 
    return subProcessActivity;
  }
 
  void parseParameter(Element element, SubProcessParameterImpl parameter) {
    String name = XmlUtil.attribute(element, "subvar");
    parameter.setSubVariableName(name);
    
    String expr = XmlUtil.attribute(element, "expr");
    if (expr!=null) {
      parameter.setExpression(expr);
    }
 
    String language = XmlUtil.attribute(element, "lang");
    if (language!=null) {
      parameter.setLanguage(language);
    }
 
    String variable = XmlUtil.attribute(element, "var");
    if (variable!=null) {
      parameter.setVariableName(variable);
    }
  }
 
  public static Map<String, String> parseSwimlaneMappings(Element element, Parse parse) {
    Map<String, String> swimlaneMappings = new HashMap<String, String>();
    
    for (Element inElement: XmlUtil.elements(element, "swimlane-mapping")) {
      String swimlane = XmlUtil.attribute(inElement, "swimlane", true, parse);
      String subSwimlane = XmlUtil.attribute(inElement, "sub-swimlane", true, parse);
      
      swimlaneMappings.put(swimlane, subSwimlane);
    }
 
    return swimlaneMappings;
  }
}