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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
/*
 * 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.xml;
 
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;
 
import org.jbpm.api.JbpmException;
import org.jbpm.api.activity.ActivityBehaviour;
import org.jbpm.api.listener.EventListener;
import org.jbpm.api.model.Event;
import org.jbpm.internal.log.Log;
import org.jbpm.jpdl.internal.activity.JpdlBinding;
import org.jbpm.jpdl.internal.activity.MailListener;
import org.jbpm.jpdl.internal.model.JpdlProcessDefinition;
import org.jbpm.pvm.internal.email.impl.MailProducerImpl;
import org.jbpm.pvm.internal.email.impl.MailTemplate;
import org.jbpm.pvm.internal.email.impl.MailTemplateRegistry;
import org.jbpm.pvm.internal.email.spi.MailProducer;
import org.jbpm.pvm.internal.env.EnvironmentImpl;
import org.jbpm.pvm.internal.model.ActivityCoordinatesImpl;
import org.jbpm.pvm.internal.model.ActivityImpl;
import org.jbpm.pvm.internal.model.CompositeElementImpl;
import org.jbpm.pvm.internal.model.Continuation;
import org.jbpm.pvm.internal.model.EventImpl;
import org.jbpm.pvm.internal.model.EventListenerReference;
import org.jbpm.pvm.internal.model.ObservableElementImpl;
import org.jbpm.pvm.internal.model.ProcessDefinitionImpl;
import org.jbpm.pvm.internal.model.ScopeElementImpl;
import org.jbpm.pvm.internal.model.TimerDefinitionImpl;
import org.jbpm.pvm.internal.model.TransitionImpl;
import org.jbpm.pvm.internal.model.VariableDefinitionImpl;
import org.jbpm.pvm.internal.model.VariableOutDefinitionImpl;
import org.jbpm.pvm.internal.model.VariableOutDefinitionSet;
import org.jbpm.pvm.internal.repository.DeploymentImpl;
import org.jbpm.pvm.internal.task.AssignableDefinitionImpl;
import org.jbpm.pvm.internal.task.SwimlaneDefinitionImpl;
import org.jbpm.pvm.internal.task.TaskDefinitionImpl;
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.binding.MailTemplateBinding;
import org.jbpm.pvm.internal.wire.binding.ObjectBinding;
import org.jbpm.pvm.internal.wire.descriptor.ObjectDescriptor;
import org.jbpm.pvm.internal.wire.usercode.UserCodeReference;
import org.jbpm.pvm.internal.wire.xml.WireParser;
import org.jbpm.pvm.internal.xml.Bindings;
import org.jbpm.pvm.internal.xml.Parse;
import org.jbpm.pvm.internal.xml.Parser;
import org.jbpm.pvm.internal.xml.ProblemImpl;
import org.w3c.dom.Element;
 
/**
 * @author Tom Baeyens
 */
public class JpdlParser extends Parser {
  
  private static final Log log = Log.getLog(JpdlParser.class.getName());
 
  public static final String NAMESPACE_JPDL_40 = "http://jbpm.org/4.0/jpdl";
  public static final String NAMESPACE_JPDL_42 = "http://jbpm.org/4.2/jpdl";
  public static final String NAMESPACE_JPDL_43 = "http://jbpm.org/4.3/jpdl";
 
  public static final String CURRENT_VERSION_JBPM = "4.3";
  public static final String CURRENT_VERSION_NAMESPACE = "http://jbpm.org/"+CURRENT_VERSION_JBPM+"/jpdl";
  public static final String CURRENT_VERSION_PROCESS_LANGUAGE_ID = "jpdl-"+CURRENT_VERSION_JBPM;
  
  public static final List<String> SCHEMA_RESOURCES = new ArrayList<String>();
  static {
    SCHEMA_RESOURCES.add("jpdl-4.0.xsd");
    SCHEMA_RESOURCES.add("jpdl-4.2.xsd");
    SCHEMA_RESOURCES.add("jpdl-4.3.xsd");
  }
 
  public static final WireParser wireParser = WireParser.getInstance();
  public static final ObjectBinding objectBinding = ObjectBinding.INSTANCE;
 
  // array elements are mutable, even when final
  // never make a static array public
  static final String[] DEFAULT_BINDING_RESOURCES = {
    "jbpm.jpdl.bindings.xml",
    "jbpm.user.bindings.xml"
  }; 
 
  static JpdlBindingsParser jpdlBindingsParser = new JpdlBindingsParser();
 
  public static final String CATEGORY_ACTIVITY = "activity";
  public static final String CATEGORY_EVENT_LISTENER = "eventlistener";
 
  public JpdlParser() {
    initialize(); 
    parseBindings();
    setSchemaResources(SCHEMA_RESOURCES);
  }
 
  protected void parseBindings() {
    this.bindings = new Bindings();
 
    for (String activityResource: DEFAULT_BINDING_RESOURCES) {
      Enumeration<URL> resourceUrls = getResources(activityResource);
      if (resourceUrls.hasMoreElements()) {
        while (resourceUrls.hasMoreElements()) {
          URL resourceUrl = resourceUrls.nextElement();
          log.trace("loading jpdl bindings from resource: "+resourceUrl);
          jpdlBindingsParser.createParse()
            .setUrl(resourceUrl)
            .contextMapPut(Parse.CONTEXT_KEY_BINDINGS, bindings)
            .execute()
            .checkErrors("jpdl bindings from "+resourceUrl.toString());
        }
      } else {
        log.trace("skipping unavailable jpdl activities resource: "+activityResource);
      }
    }
  }
 
  protected Enumeration<URL> getResources(String resourceName) {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    Enumeration<URL> resourceUrls;
    try {
      resourceUrls = classLoader.getResources(resourceName);
    } catch (Exception e) {
      throw new JbpmException("couldn't get resource urls for "+resourceName, e);
    }
    return resourceUrls;
  }
 
  public Object parseDocumentElement(Element documentElement, Parse parse) {
    List<ProcessDefinitionImpl> processDefinitions = new ArrayList<ProcessDefinitionImpl>();
    
    JpdlProcessDefinition processDefinition = instantiateNewJpdlProcessDefinition();
    
    processDefinitions.add(processDefinition);
    
    parse.contextStackPush(processDefinition);
    try {
      // process attribues
      String name = XmlUtil.attribute(documentElement, "name", true, parse);
      processDefinition.setName(name);
 
      // make the process language version available for bindings
      // to allow for specific parsing behaviour per version
      
      // first check if the langid is available as a deployment property
      DeploymentImpl deployment = (DeploymentImpl) parse.contextMapGet(Parse.CONTEXT_KEY_DEPLOYMENT);
      if (deployment!=null) {
        String processLanguageId = deployment.getProcessLanguageId(name);
        if (processLanguageId==null) {
          // if it is not available as a deployment property, check if the 
          // jpdlparser attribute specifies a specific jpdl version.
          // this is the case for certain compatibility tests in our test suite
          String jpdlParser = XmlUtil.attribute(documentElement, "jpdlparser");
          if (jpdlParser!=null) {
            processLanguageId = "jpdl-"+jpdlParser;
 
          } else {
            // if none of the above, check if this is a parser test run for a specific verion
            // specify the jpdltestversion with "mvn -Djpdlparser=jpdl-4.3 clean install"
            // that way, the whole test suite will be use the specified parser
            jpdlParser = System.getProperty("jpdlparser");
            if (jpdlParser!=null) {
              processLanguageId = "jpdl-"+jpdlParser;
 
            } else {
              // if this process has a namespace, then use the namespace 
              // to see what jpdl parser version should be used
              String namespaceUri = documentElement.getNamespaceURI();
              if (namespaceUri!=null) {
                processLanguageId = "jpdl-"+namespaceUri.substring(16, 19);
 
              } else {
                // if none of the above, just deploy it as the current library version
                processLanguageId = CURRENT_VERSION_PROCESS_LANGUAGE_ID;
              }
            }
          }
          // saving the process language will make sure that  
          // the right parser version is used after an upgrade of jbpm
          // as the old format xml will still be in the db
          deployment.setProcessLanguageId(name, processLanguageId);
        }
        parse.contextMapPut(Parse.CONTEXT_KEY_PROCESS_LANGUAGE_ID, processLanguageId);
      }
 
      String packageName = XmlUtil.attribute(documentElement, "package");
      processDefinition.setPackageName(packageName);
 
      Integer version = XmlUtil.attributeInteger(documentElement, "version", false, parse);
      if (version!=null) {
        processDefinition.setVersion(version);
      }
 
      String key = XmlUtil.attribute(documentElement, "key", false, parse);
      if (key!=null) {
        processDefinition.setKey(key);
      }
 
      Element descriptionElement = XmlUtil.element(documentElement, "description");
      if (descriptionElement!=null) {
        String description = XmlUtil.getContentText(descriptionElement);
        processDefinition.setDescription(description);
      }
      
      UnresolvedTransitions unresolvedTransitions = new UnresolvedTransitions();
      parse.contextStackPush(unresolvedTransitions);
      
      // swimlanes
      List<Element> swimlaneElements = XmlUtil.elements(documentElement, "swimlane");
      for (Element swimlaneElement: swimlaneElements) {
        String swimlaneName = XmlUtil.attribute(swimlaneElement, "name", true, parse);
        if (swimlaneName!=null) {
          SwimlaneDefinitionImpl swimlaneDefinition = 
              processDefinition.createSwimlaneDefinition(swimlaneName);
          parseAssignmentAttributes(swimlaneElement, swimlaneDefinition, parse);
        }
      }
 
      // on events
      parseOnEvents(documentElement, parse, processDefinition);
      
      // activities
      parseActivities(documentElement, parse, processDefinition);
 
      // bind activities to their destinations
      resolveTransitionDestinations(parse, processDefinition, unresolvedTransitions);
 
      // process migration information
      Element migrationElement = XmlUtil.element(documentElement, "migrate-instances");
      if (migrationElement != null) {
        MigrationHelper.parseMigrationDescriptor(migrationElement, parse, processDefinition);
      }
 
    } finally {
      parse.contextStackPop();
    }
 
    if (processDefinition.getInitial()==null) {
      parse.addProblem("no start activity in process", documentElement);
    }
    
    return processDefinitions;
  }
 
  protected JpdlProcessDefinition instantiateNewJpdlProcessDefinition() {
    return new JpdlProcessDefinition();
  }
 
  protected void resolveTransitionDestinations(Parse parse, JpdlProcessDefinition processDefinition, UnresolvedTransitions unresolvedTransitions) {
    for (UnresolvedTransition unresolvedTransition: unresolvedTransitions.list) {
      unresolvedTransition.resolve(processDefinition, parse);
    }
  }
 
  public void parseActivities(Element documentElement, Parse parse, CompositeElementImpl compositeElement) {
    List<Element> elements = XmlUtil.elements(documentElement);
    for (Element nestedElement : elements) {
      String tagName = XmlUtil.getTagLocalName(nestedElement);
      if ("on".equals(tagName) 
          || "timer".equals(tagName)
          || "swimlane".equals(tagName) 
          || "migrate-instances".equals(tagName)) continue;
 
      JpdlBinding activityBinding = (JpdlBinding) getBinding(nestedElement, CATEGORY_ACTIVITY);
      if (activityBinding == null) {
        log.debug("unrecognized activity: " + tagName);
        continue;
      }
 
      ActivityImpl activity = compositeElement.createActivity();
      parse.contextStackPush(activity);
      try {
        activity.setType(activityBinding.getTagName());
        activityBinding.parseName(nestedElement, activity, parse);
        parseTransitions(nestedElement, activity, parse);
 
        Element descriptionElement = XmlUtil.element(documentElement, "description");
        if (descriptionElement!=null) {
          String description = XmlUtil.getContentText(descriptionElement);
          activity.setDescription(description);
        }
 
        String continuationText = XmlUtil.attribute(nestedElement, "continue");
        if (continuationText!=null) {
          if ("async".equals(continuationText)) {
            activity.setContinuation(Continuation.ASYNCHRONOUS);
          } else if ("exclusive".equals(continuationText)) {
            activity.setContinuation(Continuation.EXCLUSIVE);
          }
        }
 
        Object parseResult = activityBinding.parse(nestedElement, parse, this);
        if (parseResult instanceof ActivityBehaviour) {
          ActivityBehaviour activityBehaviour = (ActivityBehaviour) parseResult;
          activity.setActivityBehaviour(activityBehaviour);
        } else {
          Descriptor activityBehaviourDescriptor = (Descriptor) parseResult;
          activity.setActivityBehaviourDescriptor(activityBehaviourDescriptor);
        }
 
        parseOnEvents(nestedElement, parse, activity);
 
        String g = XmlUtil.attribute(nestedElement, "g");
        if (g == null) continue;
 
        StringTokenizer stringTokenizer = new StringTokenizer(g, ",");
        ActivityCoordinatesImpl coordinates = null;
        if (stringTokenizer.countTokens() == 4) {
          try {
            int x = Integer.parseInt(stringTokenizer.nextToken());
            int y = Integer.parseInt(stringTokenizer.nextToken());
            int width = Integer.parseInt(stringTokenizer.nextToken());
            int height = Integer.parseInt(stringTokenizer.nextToken());
            coordinates = new ActivityCoordinatesImpl(x, y, width, height);
          } catch (NumberFormatException e) {
            coordinates = null;
          }
        }
        if (coordinates != null) {
          activity.setCoordinates(coordinates);
        } else {
          parse.addProblem("invalid coordinates g=\"" + g + "\" in " + activity, nestedElement);
        }
      } finally {
        parse.contextStackPop();
      }
    }
  }
  
  public TimerDefinitionImpl parseTimerDefinition(Element timerElement, Parse parse, ScopeElementImpl scopeElement) {
    TimerDefinitionImpl timerDefinition = scopeElement.createTimerDefinition();
 
    String duedate = XmlUtil.attribute(timerElement, "duedate");
    String duedatetime = XmlUtil.attribute(timerElement, "duedatetime");
 
    if (duedate!=null) {
      timerDefinition.setDueDateDescription(duedate);
      
    } else if (duedatetime!=null) {
      String dueDateTimeFormatText = (String) EnvironmentImpl.getFromCurrent("jbpm.duedatetime.format");
      if (dueDateTimeFormatText==null) {
        dueDateTimeFormatText = "HH:mm dd/MM/yyyy";
      }
      SimpleDateFormat dateFormat = new SimpleDateFormat(dueDateTimeFormatText);
      try {
        Date duedatetimeDate = dateFormat.parse(duedatetime);
        timerDefinition.setDueDate(duedatetimeDate);
      } catch (ParseException e) {
        parse.addProblem("couldn't parse duedatetime "+duedatetime, e);
      }
    } else {
      parse.addProblem("either duedate or duedatetime is required in timer", timerElement);
    }
    
    String repeat = XmlUtil.attribute(timerElement, "repeat");
    timerDefinition.setRepeat(repeat);
    
    return timerDefinition;
  }
 
  public void parseOnEvents(Element element, Parse parse, ScopeElementImpl scopeElement) {
    // event listeners
    List<Element> onElements = XmlUtil.elements(element, "on");
    for (Element onElement: onElements) {
      String eventName = XmlUtil.attribute(onElement, "event", true, parse);
      parseOnEvent(onElement, parse, scopeElement, eventName);
 
      Element timerElement = XmlUtil.element(onElement, "timer");
      if (timerElement!=null) {
        TimerDefinitionImpl timerDefinitionImpl = parseTimerDefinition(timerElement, parse, scopeElement);
        timerDefinitionImpl.setEventName(eventName);
      }
    }
  }
 
  public void parseOnEvent(Element element, Parse parse, ObservableElementImpl observableElement, String eventName) {
    if (eventName!=null) {
      EventImpl event = observableElement.getEvent(eventName);
      if (event==null) {
        event = observableElement.createEvent(eventName);
      }
      
      String continuationText = XmlUtil.attribute(element, "continue");
      if (continuationText!=null) {
        if ("async".equals(continuationText)) {
          event.setContinuation(Continuation.ASYNCHRONOUS);
        } else if ("exclusive".equals(continuationText)) {
          event.setContinuation(Continuation.EXCLUSIVE);
        }
      }
 
      for (Element eventListenerElement: XmlUtil.elements(element)) {
        JpdlBinding eventBinding = (JpdlBinding) getBinding(eventListenerElement, CATEGORY_EVENT_LISTENER);
        if (eventBinding!=null) {
          EventListenerReference eventListenerReference = null;
          Object parseResult = eventBinding.parse(eventListenerElement, parse, this);
          if (parseResult instanceof EventListener) {
            EventListener eventListener = (EventListener) parseResult;
            eventListenerReference = event.createEventListenerReference(eventListener);
          } else {
            Descriptor eventListenerDescriptor = (Descriptor) parseResult;
            eventListenerReference = event.createEventListenerReference(eventListenerDescriptor);
          }
          
          if (XmlUtil.attributeBoolean(eventListenerElement, "propagation", false, parse, false)) {
            eventListenerReference.setPropagationEnabled(true);
          }
          
          continuationText = XmlUtil.attribute(eventListenerElement, "continue");
          if (continuationText!=null) {
            if (observableElement instanceof ActivityImpl) {
              if (observableElement.getName()==null) {
                parse.addProblem("async continuation on event listener requires activity name", eventListenerElement);
              }
            } else if (observableElement instanceof TransitionImpl) {
              TransitionImpl transition = (TransitionImpl) observableElement;
              if (transition.getSource().getName()==null) {
                parse.addProblem("async continuation on event listener requires name in the transition source activity", eventListenerElement);
              }
            }
            if ("async".equals(continuationText)) {
              eventListenerReference.setContinuation(Continuation.ASYNCHRONOUS);
            } else if ("exclusive".equals(continuationText)) {
              eventListenerReference.setContinuation(Continuation.EXCLUSIVE);
            }
          }
 
        } else {
          String tagName = XmlUtil.getTagLocalName(eventListenerElement);
          if ( ! ( (observableElement instanceof TransitionImpl)
                   && ( "condition".equals(tagName)
                        || "timer".equals(tagName)
                      )
                 )
             ) {
            parse.addProblem("unrecognized event listener: "+tagName, null, ProblemImpl.TYPE_WARNING, eventListenerElement);
          }
        }
      }
    }
  }
 
  public void parseTransitions(Element element, ActivityImpl activity, Parse parse) {
    List<Element> transitionElements = XmlUtil.elements(element, "transition");
    UnresolvedTransitions unresolvedTransitions = parse.contextStackFind(UnresolvedTransitions.class);
    for (Element transitionElement: transitionElements) {
      String transitionName = XmlUtil.attribute(transitionElement, "name", false, parse);
 
      Element timerElement = XmlUtil.element(transitionElement, "timer");
      if (timerElement!=null) {
        TimerDefinitionImpl timerDefinitionImpl = parseTimerDefinition(timerElement, parse, activity);
        timerDefinitionImpl.setSignalName(transitionName);
      }
  
      TransitionImpl transition = activity.createOutgoingTransition();
      transition.setName(transitionName);
  
      unresolvedTransitions.add(transition, transitionElement);
      
      parseOnEvent(transitionElement, parse, transition, Event.TAKE);
    }
  }
 
  public void parseAssignmentAttributes(Element element, AssignableDefinitionImpl assignableDefinition, Parse parse) {
    Element descriptionElement = XmlUtil.element(element, "description");
    if (descriptionElement!=null) {
      String description = XmlUtil.getContentText(descriptionElement);
      assignableDefinition.setDescription(description);
    }
  
    Element assignmentHandlerElement = XmlUtil.element(element, "assignment-handler");
    if (assignmentHandlerElement!=null) {
      UserCodeReference assignmentHandlerReference = parseUserCodeReference(assignmentHandlerElement, parse);
      assignableDefinition.setAssignmentHandlerReference(assignmentHandlerReference);
    }
  
    String assigneeExpression = XmlUtil.attribute(element, "assignee");
    assignableDefinition.setAssigneeExpression(assigneeExpression);
    
    String assigneeExpressionLanguage = XmlUtil.attribute(element, "assignee-lang");
    assignableDefinition.setAssigneeExpressionLanguage(assigneeExpressionLanguage);
    
    String candidateUsersExpression = XmlUtil.attribute(element, "candidate-users");
    assignableDefinition.setCandidateUsersExpression(candidateUsersExpression);
    
    String candidateUsersExpressionLanguage = XmlUtil.attribute(element, "candidate-users-lang");
    assignableDefinition.setCandidateUsersExpressionLanguage(candidateUsersExpressionLanguage);
    
    String candidateGroupsExpression = XmlUtil.attribute(element, "candidate-groups");
    assignableDefinition.setCandidateGroupsExpression(candidateGroupsExpression);
    
    String candidateGroupsExpressionLanguage = XmlUtil.attribute(element, "candidate-groups-lang");
    assignableDefinition.setCandidateGroupsExpressionLanguage(candidateGroupsExpressionLanguage);
  }
 
  public TaskDefinitionImpl parseTaskDefinition(Element element, Parse parse, ScopeElementImpl scopeElement) {
    TaskDefinitionImpl taskDefinition = new TaskDefinitionImpl();
  
    String taskName = XmlUtil.attribute(element, "name");
    taskDefinition.setName(taskName);
 
    String form = XmlUtil.attribute(element, "form");
    taskDefinition.setFormResourceName(form);
    
    ProcessDefinitionImpl processDefinition = parse.contextStackFind(ProcessDefinitionImpl.class);
    if (processDefinition.getTaskDefinition(taskName)!=null) {
      parse.addProblem("duplicate task name "+taskName, element);
    } else {
      processDefinition.addTaskDefinitionImpl(taskDefinition);
    }
 
    String swimlaneName = XmlUtil.attribute(element, "swimlane");
    if (swimlaneName!=null) {
      JpdlProcessDefinition jpdlProcessDefinition = parse.contextStackFind(JpdlProcessDefinition.class);
      SwimlaneDefinitionImpl swimlaneDefinition = jpdlProcessDefinition.getSwimlaneDefinition(swimlaneName);
      if (swimlaneDefinition!=null) {
        taskDefinition.setSwimlaneDefinition(swimlaneDefinition);
      } else {
        parse.addProblem("swimlane "+swimlaneName+" not declared", element);
      }
    }
    
    parseAssignmentAttributes(element, taskDefinition, parse);
    
    // parse notification mail producer
    Element notificationElement = XmlUtil.element(element, "notification");
    if (notificationElement != null) {
      parseMailEvent(notificationElement, parse, scopeElement, Event.ASSIGN);
    }
 
    Element reminderElement = XmlUtil.element(element, "reminder");
    if (reminderElement != null) {
      parseMailEvent(reminderElement, parse, scopeElement, Event.REMIND);
      // associate timer to event
      TimerDefinitionImpl timerDefinition = parseTimerDefinition(reminderElement, parse, scopeElement);
      timerDefinition.setEventName(Event.REMIND);
    }
 
    return taskDefinition;
  }
 
  public List<VariableDefinitionImpl> parseVariableDefinitions(Element element, Parse parse, boolean initRequired) {
    List<VariableDefinitionImpl> variableDefinitions = new ArrayList<VariableDefinitionImpl>();
    
    for (Element inElement: XmlUtil.elements(element, "variable")) {
      VariableDefinitionImpl variableDefinition = new VariableDefinitionImpl();
 
      String name = XmlUtil.attribute(inElement, "name", true, parse);
      variableDefinition.setName(name);
      
      int sources = 0;
      
      String initExpr = XmlUtil.attribute(inElement, "init");
      if (initExpr!=null) {
        variableDefinition.setInitExpression(initExpr);
        sources++;
      }
      
      Element valueElement = XmlUtil.element(inElement);
      if (valueElement!=null) {
        Descriptor initValueDescriptor = (Descriptor) WireParser.getInstance().parseElement(valueElement, parse);
        variableDefinition.setInitDescriptor(initValueDescriptor);
        sources++;
      }
 
      if (initRequired && sources==0) {
        parse.addProblem("no init specified", inElement);
      }
      if (sources>1) {
        parse.addProblem("init attribute and init element are mutually exclusive on element variable", inElement);
      }
      
      variableDefinitions.add(variableDefinition);
    }
 
    return variableDefinitions;
  }
 
  public VariableOutDefinitionSet parseVariableOutDefinitionSet(Element element, Parse parse) {
    VariableOutDefinitionSet variableOutDefinitionSet = new VariableOutDefinitionSet();
    
    for (Element inElement: XmlUtil.elements(element, "out-variable")) {
      VariableOutDefinitionImpl variableOutDefinition = variableOutDefinitionSet.createVariableOutDefinition();
 
      String name = XmlUtil.attribute(inElement, "name", true, parse);
      variableOutDefinition.setName(name);
      
      String expression = XmlUtil.attribute(inElement, "init");
      if (expression!=null) {
        variableOutDefinition.setExpression(expression);
      }
    }
 
    return variableOutDefinitionSet;
  }
 
  public void parseMailEvent(Element element, Parse parse,
      ObservableElementImpl observableElement, String eventName) {
    // obtain assign event
    EventImpl event = observableElement.getEvent(eventName);
    if (event == null) {
      event = observableElement.createEvent(eventName);
    }
    // register event listener
    MailListener eventListener = new MailListener();
    EventListenerReference eventListenerRef = event.createEventListenerReference(eventListener);
    // set continuation mode
    String continuationText = XmlUtil.attribute(element, "continue");
    if ("async".equals(continuationText)) {
      eventListenerRef.setContinuation(Continuation.ASYNCHRONOUS);
    }
    else if ("exclusive".equals(continuationText)) {
      eventListenerRef.setContinuation(Continuation.EXCLUSIVE);
    }
 
    //https://jira.jboss.org/jira/browse/JBPM-2466
    String mailTemplateName = eventName;
    if (Event.ASSIGN.equals(eventName)) {
        mailTemplateName = "task-notification";
    } else if (Event.REMIND.equals(eventName)) {
        mailTemplateName = "task-reminder";
    }
    
    // associate mail producer to event listener
    MailProducer mailProducer = parseMailProducer(element, parse, mailTemplateName);
    eventListener.setMailProducer(mailProducer);
  }
 
  public MailProducer parseMailProducer(Element element, Parse parse, String defaultTemplateName) {
    // check whether the element is a generic object descriptor
    if (ObjectBinding.isObjectDescriptor(element)) {
      // TODO test custom mail producer
      ObjectDescriptor objectDescriptor = parseObjectDescriptor(element, parse);
      return (MailProducer) WireContext.create(objectDescriptor);
    }
 
    // parse the default producer
    MailProducerImpl mailProducer = new MailProducerImpl();
    mailProducer.setTemplate(parseMailTemplate(element, parse, defaultTemplateName));
    return mailProducer;
  }
 
  private MailTemplate parseMailTemplate(Element element, Parse parse,
      String defaultTemplateName) {
    if (element.hasAttribute("template")) {
      // fetch template from configuration
      return findTemplate(element, parse, element.getAttribute("template"));
    }
    if (!XmlUtil.isTextOnly(element)) {
      // parse inline template
      return MailTemplateBinding.parseMailTemplate(element, parse);
    }
    if (defaultTemplateName != null) {
      // fetch default template
      return findTemplate(element, parse, defaultTemplateName);
    }
    parse.addProblem("mail template must be referenced in the 'template' attribute "
        + "or specified inline", element);
    return null;
  }
 
  private MailTemplate findTemplate(Element element, Parse parse, String templateName) {
    MailTemplateRegistry templateRegistry = EnvironmentImpl.getFromCurrent(MailTemplateRegistry.class);
    if (templateRegistry != null) {
      MailTemplate template = templateRegistry.getTemplate(templateName);
      if (template != null) return template;
    }
    parse.addProblem("mail template not found: " + templateName, element);
    return null;
  }
 
  public UserCodeReference parseUserCodeReference(Element element, Parse parse) {
    UserCodeReference userCodeReference = new UserCodeReference();
 
    ObjectDescriptor objectDescriptor = parseObjectDescriptor(element, parse);
    userCodeReference.setDescriptor(objectDescriptor);
    
    if (objectDescriptor.getExpr()!=null) {
      // expressions are not cached by default
      userCodeReference.setCached(false);
    }
 
    Boolean isCached = XmlUtil.attributeBoolean(element, "cache", false, parse, null);
    if (isCached!=null) {
      userCodeReference.setCached(isCached.booleanValue());
    }
 
    return userCodeReference;
  }
 
  public ObjectDescriptor parseObjectDescriptor(Element element, Parse parse) {
    return (ObjectDescriptor) objectBinding.parse(element, parse, wireParser);
  }
 
  public Descriptor parseDescriptor(Element element, Parse parse) {
    return (Descriptor) wireParser.parseElement(element, parse);
  }
 
  public Set<String> getActivityTagNames() {
    return getBindings().getTagNames(CATEGORY_ACTIVITY);
  }
}