Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
PropertyEditorTypeConverter |
|
| 0.0;0 |
1 | /** |
|
2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
|
3 | * contributor license agreements. See the NOTICE file distributed with |
|
4 | * this work for additional information regarding copyright ownership. |
|
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
|
6 | * (the "License"); you may not use this file except in compliance with |
|
7 | * the License. You may obtain a copy of the License at |
|
8 | * |
|
9 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
10 | * |
|
11 | * Unless required by applicable law or agreed to in writing, software |
|
12 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14 | * See the License for the specific language governing permissions and |
|
15 | * limitations under the License. |
|
16 | */ |
|
17 | package org.apache.camel.impl.converter; |
|
18 | ||
19 | import java.beans.PropertyEditor; |
|
20 | import java.beans.PropertyEditorManager; |
|
21 | ||
22 | import org.apache.camel.TypeConverter; |
|
23 | ||
24 | /** |
|
25 | * Uses the java.beans.PropertyEditor conversion system to convert Objects to |
|
26 | * and from String values. |
|
27 | * |
|
28 | * @version $Revision: 523731 $ |
|
29 | */ |
|
30 | 351 | public class PropertyEditorTypeConverter implements TypeConverter { |
31 | ||
32 | public <T> T convertTo(Class<T> toType, Object value) { |
|
33 | ||
34 | // We can't convert null values since we can't figure out a property |
|
35 | // editor for it. |
|
36 | 87 | if (value == null) { |
37 | 54 | return null; |
38 | } |
|
39 | ||
40 | 33 | if (value.getClass() == String.class) { |
41 | ||
42 | // No conversion needed. |
|
43 | 3 | if (toType == String.class) { |
44 | 0 | return toType.cast(value); |
45 | } |
|
46 | ||
47 | 3 | PropertyEditor editor = PropertyEditorManager.findEditor(toType); |
48 | 3 | if (editor != null) { |
49 | 3 | editor.setAsText(value.toString()); |
50 | 3 | return toType.cast(editor.getValue()); |
51 | } |
|
52 | ||
53 | 0 | } else if (toType == String.class) { |
54 | ||
55 | 12 | PropertyEditor editor = PropertyEditorManager.findEditor(value.getClass()); |
56 | 12 | if (editor != null) { |
57 | 9 | editor.setValue(value); |
58 | 9 | return toType.cast(editor.getAsText()); |
59 | } |
|
60 | ||
61 | } |
|
62 | 21 | return null; |
63 | } |
|
64 | ||
65 | } |