Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
ConverterHelper |
|
| 5.0;5 |
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 | ||
18 | package org.apache.shale.util; |
|
19 | ||
20 | import javax.faces.FacesException; |
|
21 | import javax.faces.context.FacesContext; |
|
22 | import javax.faces.convert.Converter; |
|
23 | import javax.faces.convert.ConverterException; |
|
24 | ||
25 | /** |
|
26 | * <p>Helper class to provide access to by-type JavaServer Faces |
|
27 | * <code>Converter</code> capabilities. This implementation is stateless |
|
28 | * and maintains no cached information, so instances may be freely created |
|
29 | * and destroyed with no side effects.</p> |
|
30 | * |
|
31 | * $Id: ConverterHelper.java 464373 2006-10-16 04:21:54Z rahul $ |
|
32 | * |
|
33 | * @since 1.0.1 |
|
34 | */ |
|
35 | 0 | public class ConverterHelper { |
36 | ||
37 | ||
38 | // -------------------------------------------------------- Static Variables |
|
39 | ||
40 | ||
41 | /** |
|
42 | * <p>Messages for this class.</p> |
|
43 | */ |
|
44 | 0 | private static Messages messages = |
45 | new Messages("org.apache.shale.resources.Bundle", |
|
46 | 0 | ConverterHelper.class.getClassLoader()); |
47 | ||
48 | ||
49 | // ---------------------------------------------------------- Public Methods |
|
50 | ||
51 | ||
52 | /** |
|
53 | * <p>Use the registered by-type <code>Converter</code> to convert the |
|
54 | * specified String value to a corresponding Object value.</p> |
|
55 | * |
|
56 | * @param context <code>FacesContext</code> for the current request |
|
57 | * @param type Type to which this value should be converted |
|
58 | * (used to select the appropriate by-type converter) |
|
59 | * @param value Value to be converted |
|
60 | * |
|
61 | * @exception ConverterException if a conversion error occurs |
|
62 | */ |
|
63 | public Object asObject(FacesContext context, Class type, String value) { |
|
64 | ||
65 | 0 | if (String.class == type) { |
66 | 0 | return value; |
67 | } |
|
68 | 0 | return converter(context, type).getAsObject(context, context.getViewRoot(), value); |
69 | ||
70 | } |
|
71 | ||
72 | ||
73 | /** |
|
74 | * <p>Use the registered by-type <code>Converter</code> to convert the |
|
75 | * specified Object value to a corresponding String value.</p> |
|
76 | * |
|
77 | * @param context <code>FacesContext</code> for the current request |
|
78 | * @param type Type from which this value should be converted |
|
79 | * (used to select the appropriate by-type converter) |
|
80 | * @param value Value to be converted |
|
81 | * |
|
82 | * @exception ConverterException if a conversion error occurs |
|
83 | */ |
|
84 | public String asString(FacesContext context, Class type, Object value) { |
|
85 | ||
86 | 0 | if (value == null) { |
87 | 0 | return null; |
88 | 0 | } else if ((String.class == type) && (value instanceof String)) { |
89 | 0 | return (String) value; |
90 | } |
|
91 | 0 | return converter(context, type).getAsString(context, context.getViewRoot(), value); |
92 | ||
93 | } |
|
94 | ||
95 | ||
96 | // --------------------------------------------------------- Private Methods |
|
97 | ||
98 | ||
99 | /** |
|
100 | * <p>Return an appropriate <code>Converter</code> instance for the |
|
101 | * specified type.</p> |
|
102 | * |
|
103 | * @param context <code>FacesContext</code> for the current request |
|
104 | * @param type Type for which to retrieve a <code>Converter</code> |
|
105 | * |
|
106 | * @exception ConverterException if no <code>Converter</code> has been |
|
107 | * registered for the specified type |
|
108 | */ |
|
109 | private Converter converter(FacesContext context, Class type) { |
|
110 | ||
111 | 0 | if (type == null) { |
112 | 0 | throw new ConverterException(messages.getMessage("convHelper.missing", |
113 | context.getViewRoot().getLocale())); |
|
114 | } |
|
115 | ||
116 | 0 | Converter converter = null; |
117 | try { |
|
118 | 0 | converter = context.getApplication().createConverter(type); |
119 | 0 | } catch (FacesException e) { |
120 | 0 | throw new ConverterException(e); |
121 | 0 | } |
122 | 0 | if (converter == null) { |
123 | 0 | throw new ConverterException(messages.getMessage("convHelper.noConverter", |
124 | context.getViewRoot().getLocale(), |
|
125 | new Object[] { type.getName() })); |
|
126 | } |
|
127 | 0 | return converter; |
128 | ||
129 | } |
|
130 | ||
131 | ||
132 | } |