This ActionMappingStrategy runs the element type (singular property type) through the configured TypeBindingStrategy to determine whether it is a COMPLEX or PRIMITIVE type. If it is a PRIMITIVE type, then a SimpleTypeBindAction is returned, which enables the correct unmarshalling of elements with simple type content. Otherwise, it defers to the default ActionMappingStrategy, which is by default an instance of DefaultActionMappingStrategy but can be overridden with a constructor argument.

import org.apache.commons.betwixt.*;
import org.apache.commons.betwixt.io.read.*;
import org.apache.commons.betwixt.strategy.*;
import org.apache.commons.logging.*;
import org.xml.sax.*;

/**
 *
 * @author Jesse Sweetland
 */
public class TypeBindingMappingStrategy extends ActionMappingStrategy {
    private Log _log = LogFactory.getLog(getClass());
    private ActionMappingStrategy _defaultActionMappingStrategy = new DefaultActionMappingStrategy();
    
    public TypeBindingMappingStrategy() {
    }
    
    public TypeBindingMappingStrategy(ActionMappingStrategy defaultActionMappingStrategy) {
        if(defaultActionMappingStrategy != null) {
            _defaultActionMappingStrategy = defaultActionMappingStrategy;
        }
    }
    
    public MappingAction getMappingAction(String ns, String name, Attributes attributes, ReadContext context) throws Exception {
        XMLIntrospector intro = context.getXMLIntrospector();
        IntrospectionConfiguration cfg = intro.getConfiguration();
        TypeBindingStrategy typeStrat = cfg.getTypeBindingStrategy();
        
        ElementDescriptor descr = context.getCurrentDescriptor();
        Class type = null;
        if(descr == null) {
            if(_log.isDebugEnabled()) _log.debug("Element {" + ns + "}:" + name + " has null descriptor");
            type = null;
        } else {
            type = descr.getSingularPropertyType();
            if(_log.isDebugEnabled()) _log.debug("Element {" + ns + "}:" + name + " is a " + type);
        }
        
        MappingAction action = null;
        if((type != null) && (TypeBindingStrategy.BindingType.PRIMITIVE.equals(typeStrat.bindingType(type)))) {
            action = new SimpleTypeBindAction();
        } else {
            action = _defaultActionMappingStrategy.getMappingAction(ns, name, attributes, context);
        }
        _log.debug("Element {" + ns + "}:" + name + " (" + type + ") mapped to " + action.getClass());
        return action;
    }
}
  • No labels