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.controller.impl;
17  
18  import java.lang.reflect.Array;
19  import java.util.Collection;
20  
21  import org.seasar.cubby.controller.FormWrapper;
22  import org.seasar.cubby.controller.FormWrapperFactory;
23  import org.seasar.cubby.converter.ConversionHelper;
24  import org.seasar.cubby.converter.Converter;
25  import org.seasar.cubby.converter.ConverterFactory;
26  import org.seasar.framework.beans.BeanDesc;
27  import org.seasar.framework.beans.PropertyDesc;
28  import org.seasar.framework.beans.factory.BeanDescFactory;
29  
30  /**
31   * フォームオブジェクトのラッパーファクトリの実装です。
32   * 
33   * @author baba
34   * @since 1.1.0
35   */
36  public class FormWrapperFactoryImpl implements FormWrapperFactory {
37  
38  	/** コンバータのファクトリクラス。 */
39  	private ConverterFactory converterFactory;
40  
41  	/** 変換のヘルパクラス。 */
42  	private ConversionHelper conversionHelper;
43  
44  	/**
45  	 * コンバータのファクトリクラスを設定します。
46  	 * 
47  	 * @param converterFactory
48  	 *            コンバータのファクトリクラス
49  	 */
50  	public void setConverterFactory(final ConverterFactory converterFactory) {
51  		this.converterFactory = converterFactory;
52  	}
53  
54  	/**
55  	 * 変換のヘルパクラスを設定します。
56  	 * 
57  	 * @param conversionHelper
58  	 *            変換のヘルパクラス
59  	 */
60  	public void setConversionHelper(final ConversionHelper conversionHelper) {
61  		this.conversionHelper = conversionHelper;
62  	}
63  
64  	/**
65  	 * {@inheritDoc}
66  	 */
67  	public FormWrapper create(final Object form) {
68  		final FormWrapper formObject = new FormWrapperImpl(form);
69  		return formObject;
70  	}
71  
72  	/**
73  	 * フォームオブジェクトのラッパーの実装です。
74  	 * 
75  	 * @author baba
76  	 * @since 1.1.0
77  	 */
78  	private class FormWrapperImpl implements FormWrapper {
79  
80  		/** フォームオブジェクト */
81  		private final Object form;
82  
83  		/**
84  		 * インスタンス化します。
85  		 * 
86  		 * @param form
87  		 *            フォームオブジェクト
88  		 * @param context
89  		 *            変換中のコンテキスト
90  		 */
91  		private FormWrapperImpl(final Object form) {
92  			this.form = form;
93  		}
94  
95  		/**
96  		 * {@inheritDoc}
97  		 */
98  		public String[] getValues(final String name) {
99  			if (this.form == null) {
100 				return null;
101 			}
102 			final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(this.form
103 					.getClass());
104 			if (!beanDesc.hasPropertyDesc(name)) {
105 				return null;
106 			}
107 			final PropertyDesc propertyDesc = beanDesc.getPropertyDesc(name);
108 			final Object value = propertyDesc.getValue(this.form);
109 			if (value == null) {
110 				return null;
111 			} else if (value instanceof String[]) {
112 				return (String[]) value;
113 			} else {
114 				if (value.getClass().isArray()) {
115 					final int length = Array.getLength(value);
116 					final String[] array = (String[]) Array.newInstance(
117 							String.class, length);
118 					for (int i = 0; i < length; i++) {
119 						final Object element = Array.get(value, i);
120 						final String converted = convert(element);
121 						Array.set(array, i, converted);
122 					}
123 					return array;
124 				} else if (value instanceof Collection) {
125 					final Collection<?> collection = (Collection<?>) value;
126 					final String[] array = (String[]) Array.newInstance(
127 							String.class, collection.size());
128 					int i = 0;
129 					for (final Object element : collection) {
130 						final String converted = convert(element);
131 						Array.set(array, i++, converted);
132 					}
133 					return array;
134 				} else {
135 					final String[] array = (String[]) Array.newInstance(
136 							String.class, 1);
137 					final String converted = convert(value);
138 					Array.set(array, 0, converted);
139 					return array;
140 				}
141 			}
142 		}
143 
144 		/**
145 		 * 指定されたオブジェクトを文字列に変換します。
146 		 * 
147 		 * @param value
148 		 *            値
149 		 * @return <code>value</code>を変換した文字列
150 		 */
151 		private String convert(final Object value) {
152 			if (value == null) {
153 				return null;
154 			}
155 			final Converter converter = converterFactory.getConverter(null,
156 					value.getClass());
157 			if (converter == null) {
158 				if (value == null) {
159 					return null;
160 				} else {
161 					return value.toString();
162 				}
163 			} else {
164 				return converter.convertToString(value, conversionHelper);
165 			}
166 		}
167 
168 	}
169 }