Package com.thoughtworks.xstream.mapper

Source Code of com.thoughtworks.xstream.mapper.AttributeMapper

/*
* Copyright (C) 2006 Joe Walnes.
* Copyright (C) 2006, 2007, 2008, 2009, 2010, 2013, 2014 XStream Committers.
* All rights reserved.
*
* The software in this package is published under the terms of the BSD
* style license a copy of which has been included with this distribution in
* the LICENSE.txt file.
*
* Created on 20. February 2006 by Mauro Talevi
*/
package com.thoughtworks.xstream.mapper;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.ConverterLookup;
import com.thoughtworks.xstream.converters.SingleValueConverter;
import com.thoughtworks.xstream.converters.reflection.ReflectionProvider;


/**
* Mapper that allows the usage of attributes for fields and corresponding types or specified arbitrary types. It is
* responsible for the lookup of the {@link SingleValueConverter} for item types and attribute names.
*
* @author Paul Hammant
* @author Ian Cartwright
* @author Jörg Schaible
* @author Mauro Talevi
* @author Guilherme Silveira
* @since 1.2
*/
public class AttributeMapper extends MapperWrapper {

    private final Map<String, Class<?>> fieldNameToTypeMap = new HashMap<String, Class<?>>();
    private final Set<Class<?>> typeSet = new HashSet<Class<?>>();
    private final ConverterLookup converterLookup;
    private final ReflectionProvider reflectionProvider;
    private final Set<Field> fieldToUseAsAttribute = new HashSet<Field>();

    public AttributeMapper(
            final Mapper wrapped, final ConverterLookup converterLookup, final ReflectionProvider refProvider) {
        super(wrapped);
        this.converterLookup = converterLookup;
        reflectionProvider = refProvider;
    }

    public void addAttributeFor(final String fieldName, final Class<?> type) {
        fieldNameToTypeMap.put(fieldName, type);
    }

    public void addAttributeFor(final Class<?> type) {
        typeSet.add(type);
    }

    private SingleValueConverter getLocalConverterFromItemType(final Class<?> type) {
        final Converter converter = converterLookup.lookupConverterForType(type);
        if (converter != null && converter instanceof SingleValueConverter) {
            return (SingleValueConverter)converter;
        } else {
            return null;
        }
    }

    @Override
    public SingleValueConverter getConverterFromItemType(final String fieldName, final Class<?> type,
            final Class<?> definedIn) {
        if (shouldLookForSingleValueConverter(fieldName, type, definedIn)) {
            final SingleValueConverter converter = getLocalConverterFromItemType(type);
            if (converter != null) {
                return converter;
            }
        }
        return super.getConverterFromItemType(fieldName, type, definedIn);
    }

    public boolean shouldLookForSingleValueConverter(final String fieldName, final Class<?> type,
            final Class<?> definedIn) {
        if (typeSet.contains(type)) {
            return true;
        } else if (fieldNameToTypeMap.get(fieldName) == type) {
            return true;
        } else if (fieldName != null && definedIn != null) {
            final Field field = reflectionProvider.getField(definedIn, fieldName);
            return fieldToUseAsAttribute.contains(field);
        }
        return false;
    }

    /**
     * @deprecated As of 1.3.1, use {@link #getConverterFromAttribute(Class, String, Class)}
     */
    @Deprecated
    @Override
    public SingleValueConverter getConverterFromAttribute(final Class<?> definedIn, final String attribute) {
        final Field field = reflectionProvider.getField(definedIn, attribute);
        return getConverterFromAttribute(definedIn, attribute, field.getType());
    }

    @Override
    public SingleValueConverter getConverterFromAttribute(final Class<?> definedIn, final String attribute,
            final Class<?> type) {
        if (shouldLookForSingleValueConverter(attribute, type, definedIn)) {
            final SingleValueConverter converter = getLocalConverterFromItemType(type);
            if (converter != null) {
                return converter;
            }
        }
        return super.getConverterFromAttribute(definedIn, attribute, type);
    }

    /**
     * Tells this mapper to use an attribute for this field.
     *
     * @param field the field itself
     * @since 1.2.2
     */
    public void addAttributeFor(final Field field) {
        fieldToUseAsAttribute.add(field);
    }

    /**
     * Tells this mapper to use an attribute for this field.
     *
     * @param definedIn the declaring class of the field
     * @param fieldName the name of the field
     * @throws IllegalArgumentException if the field does not exist
     * @since 1.3
     */
    public void addAttributeFor(final Class<?> definedIn, final String fieldName) {
        fieldToUseAsAttribute.add(reflectionProvider.getField(definedIn, fieldName));
    }
}
TOP

Related Classes of com.thoughtworks.xstream.mapper.AttributeMapper

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.