se.cambio.cds.gdl.parser.GDLBinding Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gdl-parser Show documentation
Show all versions of gdl-parser Show documentation
Guide definition language parser
package se.cambio.cds.gdl.parser;
import lombok.extern.slf4j.Slf4j;
import org.openehr.am.parser.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
@Slf4j
public class GDLBinding {
private static final String MODEL_PACKAGE = "se.cambio.cds.gdl.model.";
Object createModelClass(String name, Map valueMap)
throws ClassNotFoundException, SecurityException,
NoSuchMethodException, IllegalArgumentException,
InstantiationException, IllegalAccessException,
InvocationTargetException {
log.debug("class: " + name + ", valueMap: " + valueMap);
String className = MODEL_PACKAGE + toCamelCase(name);
Class klass = Class.forName(className);
Constructor constructor = klass.getConstructor();
Object obj = constructor.newInstance();
Method[] methods = klass.getMethods();
for (Method method : methods) {
String methodName = method.getName();
if (methodName.startsWith("set")) {
for (Map.Entry entry : valueMap.entrySet()) {
String attribute = entry.getKey();
String setter = "set" + toCamelCase(attribute);
if (methodName.equals(setter)) {
log.debug("setter: " + setter);
method.invoke(obj, entry.getValue());
break;
}
}
}
}
log.debug("class: " + name + " created !! ");
return obj;
}
private String toCamelCase(String underscoreSeparated) {
StringTokenizer tokens = new StringTokenizer(underscoreSeparated, "_");
StringBuilder buf = new StringBuilder();
while (tokens.hasMoreTokens()) {
String word = tokens.nextToken();
buf.append(word.substring(0, 1).toUpperCase());
buf.append(word.substring(1).toLowerCase());
}
return buf.toString();
}
public GDLBinding() {
}
public Object bind(ContentObject co) throws BindingException {
if (co.getAttributeValues() != null) {
return bindAttributes(null, co.getAttributeValues());
} else {
ComplexObjectBlock complexObj = co.getComplexObjectBlock();
return bindComplexBlock(complexObj);
}
}
private Object bindAttributes(String type, List attributes)
throws BindingException {
log.debug("bind attributes for type: " + type);
Map values = new HashMap();
for (AttributeValue attr : attributes) {
String id = attr.getId();
Object value = bindObjectBlock(attr.getValue());
values.put(id, value);
}
try {
return createModelClass(type, values);
} catch (Exception ex) {
throw new BindingException("failed to create instance of " + type
+ ", with values: " + values, ex);
}
}
private Object bindObjectBlock(ObjectBlock block) throws BindingException {
if (block instanceof PrimitiveObjectBlock) {
return bindPrimitiveBlock((PrimitiveObjectBlock) block);
} else {
return bindComplexBlock((ComplexObjectBlock) block);
}
}
private Object bindPrimitiveBlock(PrimitiveObjectBlock block)
throws BindingException {
if (block.getSimpleValue() != null) {
return block.getSimpleValue().getValue();
} else if (block.getSimpleListValue() != null) {
List values = block.getSimpleListValue();
List list = new ArrayList(values.size());
for (SimpleValue sv : values) {
list.add(sv.getValue());
}
return list;
} else if (block.getSimpleIntervalValue() != null) {
// TODO
return null;
} else if (block.getTermCode() != null) {
return block.getTermCode();
} else if (block.getTermCodeListValue() != null) {
return block.getTermCodeListValue();
} else {
throw new BindingException("empty block");
}
}
private Object bindComplexBlock(ComplexObjectBlock block) throws BindingException {
if (block instanceof SingleAttributeObjectBlock) {
SingleAttributeObjectBlock singleBlock = (SingleAttributeObjectBlock) block;
// a special case to deal with empty attribute list
if ("LIST".equalsIgnoreCase(singleBlock.getTypeIdentifier())
&& singleBlock.getAttributeValues().isEmpty()) {
return new ArrayList();
}
return bindAttributes(singleBlock.getTypeIdentifier(),
singleBlock.getAttributeValues());
} else {
MultipleAttributeObjectBlock multiBlock = (MultipleAttributeObjectBlock) block;
List list = multiBlock.getKeyObjects();
// can't tell list or map
if (list.size() == 0) {
return null;
}
// list
if (isNumericKey(list.get(0))) {
List