Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
FormWrapperFactoryImpl |
|
| 0.0;0 | ||||
FormWrapperFactoryImpl$1 |
|
| 0.0;0 | ||||
FormWrapperFactoryImpl$FormWrapperImpl |
|
| 0.0;0 |
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 | 87 | 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 | 73 | this.converterFactory = converterFactory; |
52 | 73 | } |
53 | ||
54 | /** | |
55 | * 変換のヘルパクラスを設定します。 | |
56 | * | |
57 | * @param conversionHelper | |
58 | * 変換のヘルパクラス | |
59 | */ | |
60 | public void setConversionHelper(final ConversionHelper conversionHelper) { | |
61 | 73 | this.conversionHelper = conversionHelper; |
62 | 73 | } |
63 | ||
64 | /** | |
65 | * {@inheritDoc} | |
66 | */ | |
67 | public FormWrapper create(final Object form) { | |
68 | 7 | final FormWrapper formObject = new FormWrapperImpl(form); |
69 | 7 | return formObject; |
70 | } | |
71 | ||
72 | /** | |
73 | * フォームオブジェクトのラッパーの実装です。 | |
74 | * | |
75 | * @author baba | |
76 | * @since 1.1.0 | |
77 | */ | |
78 | 7 | 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 | 7 | private FormWrapperImpl(final Object form) { |
92 | 7 | this.form = form; |
93 | 7 | } |
94 | ||
95 | /** | |
96 | * {@inheritDoc} | |
97 | */ | |
98 | public String[] getValues(final String name) { | |
99 | 7 | if (this.form == null) { |
100 | 0 | return null; |
101 | } | |
102 | 7 | final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(this.form |
103 | .getClass()); | |
104 | 7 | if (!beanDesc.hasPropertyDesc(name)) { |
105 | 1 | return null; |
106 | } | |
107 | 6 | final PropertyDesc propertyDesc = beanDesc.getPropertyDesc(name); |
108 | 6 | final Object value = propertyDesc.getValue(this.form); |
109 | 6 | if (value == null) { |
110 | 0 | return null; |
111 | 6 | } else if (value instanceof String[]) { |
112 | 0 | return (String[]) value; |
113 | } else { | |
114 | 6 | if (value.getClass().isArray()) { |
115 | 1 | final int length = Array.getLength(value); |
116 | 1 | final String[] array = (String[]) Array.newInstance( |
117 | String.class, length); | |
118 | 4 | for (int i = 0; i < length; i++) { |
119 | 3 | final Object element = Array.get(value, i); |
120 | 3 | final String converted = convert(element); |
121 | 3 | Array.set(array, i, converted); |
122 | } | |
123 | 1 | return array; |
124 | 5 | } else if (value instanceof Collection) { |
125 | 1 | final Collection<?> collection = (Collection<?>) value; |
126 | 1 | final String[] array = (String[]) Array.newInstance( |
127 | String.class, collection.size()); | |
128 | 1 | int i = 0; |
129 | 1 | for (final Object element : collection) { |
130 | 2 | final String converted = convert(element); |
131 | 2 | Array.set(array, i++, converted); |
132 | 2 | } |
133 | 1 | return array; |
134 | } else { | |
135 | 4 | final String[] array = (String[]) Array.newInstance( |
136 | String.class, 1); | |
137 | 4 | final String converted = convert(value); |
138 | 4 | Array.set(array, 0, converted); |
139 | 4 | 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 | 9 | final Converter converter = converterFactory.getConverter(null, |
153 | value.getClass()); | |
154 | 9 | if (converter == null) { |
155 | 4 | return value.toString(); |
156 | } else { | |
157 | 5 | return converter.convertToString(value, conversionHelper); |
158 | } | |
159 | } | |
160 | ||
161 | } | |
162 | } |