All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sourceforge.stripes.util.bean.MapPropertyAccessor Maven / Gradle / Ivy

There is a newer version: 1.7.0-async-beta
Show newest version
/* Copyright 2005-2006 Tim Fennell
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.sourceforge.stripes.util.bean;

import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.validation.ValidationError;
import net.sourceforge.stripes.validation.TypeConverter;
import net.sourceforge.stripes.controller.StripesFilter;
import net.sourceforge.stripes.util.Log;

import java.util.Map;
import java.util.Locale;
import java.util.Collection;
import java.util.ArrayList;

/**
 * Implementation of {@link PropertyAccessor} for interacting with Maps. Uses information
 * stored in the node evaluation to determine the correct type for the Map key and converts
 * the key using Stripes' type conversion system.
 *
 * @author Tim Fennell
 * @since Stripes 1.4
 */
public class MapPropertyAccessor implements PropertyAccessor> {
    private static final Log log = Log.getInstance(MapPropertyAccessor.class);

    /**
     * Gets the value stored in the Map under the key specified by the current node.
     * @param evaluation the current node evaluation
     * @param map the target Map
     * @return the value stored in the map under the specified key
     */
    public Object getValue(NodeEvaluation evaluation, Map map) {
        Object key = getKey(evaluation);
        return map.get(key);
    }

    /**
     * Sets the value stored in the Map under the key specified by the current node.
     * @param evaluation the current node evaluation
     * @param map the target Map
     * @param value the value to be stored in the map under the specified key
     */
    @SuppressWarnings("unchecked")
    public void setValue(NodeEvaluation evaluation, Map map, Object value) {
        Object key = getKey(evaluation);
        if (key != null && !evaluation.getKeyType().isAssignableFrom(key.getClass())) {
            String exprString = evaluation.getExpressionEvaluation().getExpression().getSource();
            String nodeString = evaluation.getNode().getStringValue();
            String declTypeName = evaluation.getKeyType().getName();
            String evalTypeName = key.getClass().getName();
            log.warn("Unable to bind ", exprString, " because the string \"", nodeString,
                    "\" evaluates to a ", evalTypeName,
                    ", which is not assignable to the map's key type of ", declTypeName,
                    ". This likely means type conversion failed and there is no constructor ",
                    declTypeName, "(String).");
        }
        else {
            map.put(key, value);
        }
    }

    /**
     * Attempts to convert the key to from the expression node to the correct type
     * as determined by reflection (using generics to find the Map key type). If generics
     * information is not present then the type found in the expression is used (String, int etc.).
     *
     * @param evaluation the current node evaluation
     * @return the key to use when interacting with the Map
     */
    @SuppressWarnings("unchecked")
	protected Object getKey(NodeEvaluation evaluation) {
        Class declaredType = evaluation.getKeyType();
        Class nodeType     = evaluation.getNode().getTypedValue().getClass();

        if (nodeType.equals(declaredType) || declaredType == null) {
            return evaluation.getNode().getTypedValue();
        }
        else {
            try {
                // Collect the things needed to grab a type converter
                String stringKey = evaluation.getNode().getStringValue();
                ActionBean bean = (ActionBean) evaluation.getExpressionEvaluation().getBean();
                Locale locale = bean.getContext().getLocale();
                Collection errors = new ArrayList();

                TypeConverter tc = StripesFilter.getConfiguration()
                        .getTypeConverterFactory().getTypeConverter(declaredType, locale);

                // If there is a type converter, try using it!
                if (tc != null) {
                    Object retval = tc.convert(stringKey, declaredType, errors);
                    if (errors.size() == 0) return retval;
                }
                // Otherwise look for a String constructor
                else {
                    return StripesFilter.getConfiguration().getObjectFactory()
                            .constructor(declaredType, String.class).newInstance(stringKey);
                }
            }
            catch (Exception e) {
                log.warn("Exception while converting Map key to appropriate type. Key: ",
                         evaluation.getNode().getStringValue());
            }

            // Return the original key if we couldn't type convert it
            return evaluation.getNode().getTypedValue();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy