package org.jbpm.wire.binding;
import java.util.ArrayList;
import java.util.List;
import org.jbpm.wire.Descriptor;
import org.jbpm.wire.descriptor.CollectionDescriptor;
import org.jbpm.wire.xml.WireParser;
import org.jbpm.xml.Binding;
import org.jbpm.xml.Parse;
import org.jbpm.xml.Parser;
import org.jbpm.xml.XmlUtil;
import org.w3c.dom.Element;
public abstract class AbstractCollectionBinding implements Binding {
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);
}
String synchronizedText = XmlUtil.attribute(element, "synchronized");
if (synchronizedText!=null) {
Boolean isSynchronized = XmlUtil.booleanEquals(synchronizedText, Boolean.FALSE);
descriptor.setSynchronized(isSynchronized);
}
List<Descriptor> valueDescriptors = new ArrayList<Descriptor>();
List<Element> elements = XmlUtil.elements(element);
if (elements!=null) {
for (Element valueElement: elements) {
Descriptor valueDescriptor = (Descriptor) parser.parseElement(valueElement, parse, WireParser.CATEGORY_DESCRIPTOR);
if (valueDescriptor!=null) {
valueDescriptors.add(valueDescriptor);
}
}
}
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 {
Class<?> collectionClass = parser.getClassLoader().loadClass(className);
if (collectionInterface.isAssignableFrom(collectionClass)) {
return true;
} else {
parse.addProblem("class "+ className+" is not a "+collectionInterface.getName());
}
} catch (ClassNotFoundException e) {
parse.addProblem("class "+className+" could not be found");
}
return false;
}
protected abstract Class<?> getCollectionInterface();
protected abstract CollectionDescriptor createDescriptor();
}