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