View Javadoc

1   /*
2    * Copyright 2004-2008 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.cubby.dxo.impl;
17  
18  import java.lang.reflect.Array;
19  import java.lang.reflect.Method;
20  import java.util.LinkedHashMap;
21  import java.util.Map;
22  import java.util.Map.Entry;
23  
24  import org.seasar.extension.dxo.annotation.AnnotationReader;
25  import org.seasar.extension.dxo.command.impl.BeanToMapDxoCommand;
26  import org.seasar.extension.dxo.converter.ConversionContext;
27  import org.seasar.extension.dxo.converter.Converter;
28  import org.seasar.extension.dxo.converter.ConverterFactory;
29  
30  /**
31   * Beanから{@link String}をキー、{@link String}の配列を値にもつ{@link Map}に変換するコマンドです。
32   * 
33   * @author baba
34   * @since 1.0.0
35   */
36  class BeanToStringArrayMapDxoCommand extends BeanToMapDxoCommand {
37  
38  	/**
39  	 * インスタンスを構築します。
40  	 * 
41  	 * @param dxoClass
42  	 *            Dxoのインターフェースまたはクラス
43  	 * @param method
44  	 *            Dxoのメソッド
45  	 * @param converterFactory
46  	 *            {@link Converter}のファクトリ
47  	 * @param annotationReader
48  	 *            {@link AnnotationReader}のファクトリ
49  	 * @param expression
50  	 *            変換ルールを表すOGNL式
51  	 */
52  	@SuppressWarnings("unchecked")
53  	public BeanToStringArrayMapDxoCommand(final Class dxoClass,
54  			final Method method, final ConverterFactory converterFactory,
55  			final AnnotationReader annotationReader, final String expression) {
56  		super(dxoClass, method, converterFactory, annotationReader, expression);
57  		valueType = String[].class;
58  	}
59  
60  	@Override
61  	@SuppressWarnings("unchecked")
62  	protected Map convertValueType(final Map from,
63  			final ConversionContext context) {
64  		final Map<String, String[]> to = new LinkedHashMap<String, String[]>();
65  		for (final Entry<String, Object> entry : castToObjectMap(from)
66  				.entrySet()) {
67  			final String key = entry.getKey();
68  			final Object value = entry.getValue();
69  			if (value == null || value instanceof String[]) {
70  				to.put(key, (String[]) value);
71  			} else {
72  				if (value.getClass().isArray()) {
73  					final Converter converter = converterFactory.getConverter(
74  							value.getClass(), String[].class);
75  					final String[] convertedValue = (String[]) converter
76  							.convert(value, String[].class, context);
77  					to.put(key, convertedValue);
78  				} else {
79  					final Converter converter = converterFactory.getConverter(
80  							value.getClass(), String.class);
81  					final String[] array = (String[]) Array.newInstance(
82  							String.class, 1);
83  					Array.set(array, 0, converter.convert(value, String.class,
84  							context));
85  					to.put(key, array);
86  				}
87  			}
88  		}
89  		return to;
90  	}
91  
92  	@SuppressWarnings("unchecked")
93  	private Map<String, Object> castToObjectMap(final Map from) {
94  		return from;
95  	}
96  
97  }