2020
2121import javax .el .ELException ;
2222import javax .faces .context .FacesContext ;
23- import javax .faces .convert .Converter ;
2423
2524import com .ocpsoft .pretty .PrettyException ;
26- import com .ocpsoft .pretty .faces .config .mapping .PathConverter ;
2725import com .ocpsoft .pretty .faces .config .mapping .PathParameter ;
2826import com .ocpsoft .pretty .faces .config .mapping .QueryParameter ;
2927import com .ocpsoft .pretty .faces .config .mapping .UrlMapping ;
3028import com .ocpsoft .pretty .faces .url .QueryString ;
3129import com .ocpsoft .pretty .faces .url .URL ;
3230import com .ocpsoft .pretty .faces .url .URLPatternParser ;
3331import 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}
0 commit comments