Skip to content

Commit 7d90ff5

Browse files
committed
Refactored parameter conversion code into new ParameterConverter class
1 parent 9225906 commit 7d90ff5

3 files changed

Lines changed: 168 additions & 140 deletions

File tree

‎core/src/main/java/com/ocpsoft/pretty/faces/beans/ExtractedValuesURLBuilder.java‎

Lines changed: 5 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,15 @@
2020

2121
import javax.el.ELException;
2222
import javax.faces.context.FacesContext;
23-
import javax.faces.convert.Converter;
2423

2524
import com.ocpsoft.pretty.PrettyException;
26-
import com.ocpsoft.pretty.faces.config.mapping.PathConverter;
2725
import com.ocpsoft.pretty.faces.config.mapping.PathParameter;
2826
import com.ocpsoft.pretty.faces.config.mapping.QueryParameter;
2927
import com.ocpsoft.pretty.faces.config.mapping.UrlMapping;
3028
import com.ocpsoft.pretty.faces.url.QueryString;
3129
import com.ocpsoft.pretty.faces.url.URL;
3230
import com.ocpsoft.pretty.faces.url.URLPatternParser;
3331
import com.ocpsoft.pretty.faces.util.FacesElUtils;
34-
import com.ocpsoft.pretty.faces.util.NullComponent;
35-
import com.ocpsoft.pretty.faces.util.StringUtils;
3632

3733
/**
3834
* @author Lincoln Baxter, III <lincoln@ocpsoft.com>
@@ -72,15 +68,9 @@ public URL buildURL(final UrlMapping mapping)
7268
+ mapping.getId() + " >, Required value " + " < " + expression + " > was null");
7369
}
7470

75-
// search for a custom converter
76-
String customConverterId = null;
77-
PathConverter pathConverter = mapping.getPathConverterForPathParam(injection);
78-
if(pathConverter != null) {
79-
customConverterId = StringUtils.trimToNull(pathConverter.getConverterId());
80-
}
81-
8271
// convert the value to the corresponding string
83-
String valueAsString = convertObjectToString(context, value, customConverterId);
72+
ParameterConverter converter = new ParameterConverter(context, mapping, injection);
73+
String valueAsString = converter.getAsString(value);
8474
if (valueAsString == null)
8575
{
8676
throw new PrettyException("PrettyFaces: The converter returned null while converting the object <"
@@ -119,7 +109,7 @@ public QueryString buildQueryString(final UrlMapping mapping)
119109
for (QueryParameter injection : queryParams)
120110
{
121111
String name = injection.getName();
122-
String customConverterId = StringUtils.trimToNull(injection.getConverterId());
112+
ParameterConverter converter = new ParameterConverter(context, injection);
123113

124114
expression = injection.getExpression().getELExpression();
125115
value = elUtils.getValue(context, expression);
@@ -131,16 +121,12 @@ public QueryString buildQueryString(final UrlMapping mapping)
131121
Object[] values = (Object[]) value;
132122
for (Object temp : values)
133123
{
134-
// convert the object to a string and add the query parameter
135-
String valueAsString = convertObjectToString(context, temp, customConverterId);
136-
queryParameterValues.add(new QueryParameter(name, valueAsString));
124+
queryParameterValues.add(new QueryParameter(name, converter.getAsString(temp)));
137125
}
138126
}
139127
else
140128
{
141-
// convert the object to a string and add the query parameter
142-
String valueAsString = convertObjectToString(context, value, customConverterId);
143-
queryParameterValues.add(new QueryParameter(name, valueAsString));
129+
queryParameterValues.add(new QueryParameter(name, converter.getAsString(value)));
144130
}
145131
}
146132
}
@@ -156,52 +142,5 @@ public QueryString buildQueryString(final UrlMapping mapping)
156142

157143
return result;
158144
}
159-
160-
/**
161-
* This method will convert the supplied object to its string representation. The method will either use the JSF
162-
* default converter for the type or use the custom converter (if supplied).
163-
*
164-
* @param context the current {@link FacesContext}
165-
* @param value The object to convert
166-
* @param customConverterId An optional custom converter
167-
* @return the string representation or <code>null</code> if value is <code>null</code>
168-
*/
169-
private String convertObjectToString(FacesContext context, Object value, String customConverterId)
170-
{
171-
172-
// return null for null values
173-
if (value == null) {
174-
return null;
175-
}
176-
177-
// use this converter for creating the string representation of the object
178-
Converter converter = null;
179-
180-
// check for a custom converter
181-
if (StringUtils.isNotBlank(customConverterId)) {
182-
183-
converter = context.getApplication().createConverter(customConverterId);
184-
185-
// fail if the converter does not exist
186-
if (converter == null) {
187-
throw new IllegalStateException("Cannot find JSF converter: " + customConverterId);
188-
}
189-
190-
}
191-
192-
// use the default converter
193-
else {
194-
converter = context.getApplication().createConverter(value.getClass());
195-
}
196-
197-
// return the converted value
198-
if (converter != null) {
199-
return converter.getAsString(context, new NullComponent(), value);
200-
}
201-
else {
202-
return value.toString();
203-
}
204-
205-
}
206145

207146
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/*
2+
* Copyright 2010 Lincoln Baxter, III
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, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.ocpsoft.pretty.faces.beans;
17+
18+
import javax.faces.context.FacesContext;
19+
import javax.faces.convert.Converter;
20+
21+
import com.ocpsoft.pretty.faces.config.mapping.PathConverter;
22+
import com.ocpsoft.pretty.faces.config.mapping.PathParameter;
23+
import com.ocpsoft.pretty.faces.config.mapping.QueryParameter;
24+
import com.ocpsoft.pretty.faces.config.mapping.UrlMapping;
25+
import com.ocpsoft.pretty.faces.util.NullComponent;
26+
import com.ocpsoft.pretty.faces.util.StringUtils;
27+
28+
/**
29+
*
30+
* This class is used to perform the conversion for path and query parameters. The JSF converter used for the conversion
31+
* will be either the default converter for the type or the user-specified converter defined for the
32+
* {@link QueryParameter} or {@link PathParameter}.
33+
*
34+
* @author Christian Kaltepoth <christian@kaltepoth.de>
35+
*
36+
*/
37+
public class ParameterConverter
38+
{
39+
40+
private final FacesContext facesContext;
41+
42+
private final String customConverterId;
43+
44+
/**
45+
* Creates a {@link ParameterConverter} for the supplied query parameter.
46+
*/
47+
public ParameterConverter(FacesContext facesContext, QueryParameter queryParameter)
48+
{
49+
this.facesContext = facesContext;
50+
this.customConverterId = StringUtils.trimToNull(queryParameter.getConverterId());
51+
}
52+
53+
/**
54+
* Creates a {@link ParameterConverter} for the supplied path parameter.
55+
*/
56+
public ParameterConverter(FacesContext facesContext, UrlMapping mapping, PathParameter pathParameter)
57+
{
58+
this.facesContext = facesContext;
59+
PathConverter pathConverter = mapping.getPathConverterForPathParam(pathParameter);
60+
if (pathConverter != null) {
61+
customConverterId = StringUtils.trimToNull(pathConverter.getConverterId());
62+
}
63+
else {
64+
customConverterId = null;
65+
}
66+
}
67+
68+
/**
69+
* Converters the string value into the corresponding object. Returns <code>null</code> if the value is
70+
* <code>null</code>.
71+
*/
72+
public Object getAsObject(Class<?> type, String value)
73+
{
74+
75+
// return null for null values
76+
if (value == null) {
77+
return null;
78+
}
79+
80+
// create the converter for the type
81+
Converter converter = createConverter(type);
82+
83+
// return the converted value
84+
if (converter != null) {
85+
return converter.getAsObject(facesContext, new NullComponent(), value);
86+
}
87+
else {
88+
return value;
89+
}
90+
91+
}
92+
93+
/**
94+
* Converts the supplied value into the string representation. Returns <code>null</code> if the value is
95+
* <code>null</code>.
96+
*/
97+
public String getAsString(Object value)
98+
{
99+
100+
// return null for null values
101+
if (value == null) {
102+
return null;
103+
}
104+
105+
// create the converter for the type
106+
Converter converter = createConverter(value.getClass());
107+
108+
// return the converted value
109+
if (converter != null) {
110+
return converter.getAsString(facesContext, new NullComponent(), value);
111+
}
112+
else {
113+
return value.toString();
114+
}
115+
116+
}
117+
118+
private Converter createConverter(Class<?> type)
119+
{
120+
121+
Converter converter = null;
122+
123+
// check for a custom converter
124+
if (StringUtils.isNotBlank(customConverterId)) {
125+
126+
converter = facesContext.getApplication().createConverter(customConverterId);
127+
128+
// fail if the converter does not exist
129+
if (converter == null) {
130+
throw new IllegalStateException("Cannot find JSF converter: " + customConverterId);
131+
}
132+
133+
}
134+
135+
// use the default converter
136+
else {
137+
converter = facesContext.getApplication().createConverter(type);
138+
}
139+
140+
return converter;
141+
}
142+
}

0 commit comments

Comments
 (0)