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
package org.jbpm.pvm.internal.wire.binding;
 
import java.util.ArrayList;
import java.util.List;
 
import org.jbpm.pvm.internal.util.XmlUtil;
import org.jbpm.pvm.internal.wire.Descriptor;
import org.jbpm.pvm.internal.wire.descriptor.CollectionDescriptor;
import org.jbpm.pvm.internal.wire.xml.WireParser;
import org.jbpm.pvm.internal.xml.Parse;
import org.jbpm.pvm.internal.xml.Parser;
import org.w3c.dom.Element;
 
public abstract class AbstractCollectionBinding extends WireDescriptorBinding {
 
  public AbstractCollectionBinding(String tagName) {
    super(tagName);
  }
 
  public Object parse(Element element, Parse parse, Parser parser) {
    CollectionDescriptor descriptor = createDescriptor();
    
    String className = XmlUtil.attribute(element,"class");
    
    // verify if the given classname is specified and implements the collection interface
    if (verify(className, getCollectionInterface(), parse, parser)) {
      descriptor.setClassName(className);
    }
    
    Boolean isSynchronized = XmlUtil.attributeBoolean(element, "synchronized", false, parse);
    if (isSynchronized!=null) {
      descriptor.setSynchronized(isSynchronized.booleanValue());
    }
    
    List<Descriptor> valueDescriptors = new ArrayList<Descriptor>();
    List<Element> elements = XmlUtil.elements(element);
    for (Element valueElement: elements) {
      Descriptor valueDescriptor = (Descriptor) parser.parseElement(valueElement, parse, WireParser.CATEGORY_DESCRIPTOR);
      if (valueDescriptor!=null) {
        valueDescriptors.add(valueDescriptor);
      } else {
        parse.addProblem("unrecognized element: "+XmlUtil.toString(valueElement));
      }
    }
    descriptor.setValueDescriptors(valueDescriptors);
    return descriptor;
  }
 
  /** verifies if the given classname is specified and implements the collection interface */
  public static boolean verify(String className, Class< ? > collectionInterface, Parse parse, Parser parser) {
    if (className==null) {
      return false;
    }
 
    try {
      ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
      Class<?> collectionClass = Class.forName(className, true, classLoader);
      
      if (collectionInterface.isAssignableFrom(collectionClass)) {
        return true;
      } else {
        parse.addProblem("class "+ className+" is not a "+collectionInterface.getName());
      }
    } catch (Exception e) {
      parse.addProblem("class "+className+" could not be found");
    }
    return false;
  }
 
  protected abstract Class<?> getCollectionInterface();
  protected abstract CollectionDescriptor createDescriptor();
}