/*
 * =============================================================================
 * 
 *   Copyright (c) 2011-2012, The THYMELEAF team (http://www.thymeleaf.org)
 * 
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 * 
 * =============================================================================
 */
package org.thymeleaf.spring3.expression;


import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.TypeConverter;
import org.thymeleaf.Configuration;
import org.thymeleaf.context.IProcessingContext;
import org.thymeleaf.standard.expression.AbstractStandardConversionService;

/**
 * 
 * @author Daniel Fern&aacute;ndez
 * 
 * @since 2.1.0
 *
 */
public final class SpringStandardConversionService extends AbstractStandardConversionService {


    private static final TypeDescriptor TYPE_STRING = TypeDescriptor.valueOf(String.class);



    public SpringStandardConversionService() {
        // Should only be instanced from SpringStandardDialect
        super();
    }



    @Override
    protected String convertToString(final Configuration configuration, final IProcessingContext processingContext, final Object object) {

        final TypeConverter typeConverter = getSpringConversionService(processingContext);
        if (typeConverter == null) {
            return super.convertToString(configuration, processingContext, object);
        }
        final TypeDescriptor objectTypeDescriptor = TypeDescriptor.forObject(object);
        if (!typeConverter.canConvert(objectTypeDescriptor, TYPE_STRING)) {
            return super.convertToString(configuration, processingContext, object);
        }
        return (String) typeConverter.convertValue(object, objectTypeDescriptor, TYPE_STRING);
    }




    private static final TypeConverter getSpringConversionService(final IProcessingContext processingContext) {

        final EvaluationContext evaluationContext =
                (EvaluationContext) processingContext.getContext().getVariables().
                        get(ThymeleafEvaluationContext.THYMELEAF_EVALUATION_CONTEXT_CONTEXT_VARIABLE_NAME);

        if (evaluationContext != null) {
            return evaluationContext.getTypeConverter();
        }

        return null;

    }



}

@@ -873,67 +873,8 @@
 ;%0A%0A%0A
-import org.springframework.core.convert.ConversionService;%0A
 impo
@@ -1745,24 +1745,175 @@
  object) %7B%0A%0A
+        if (object == null) %7B%0A            return null;%0A        %7D%0A        final TypeDescriptor objectTypeDescriptor = TypeDescriptor.forObject(object);%0A
         fina
@@ -2016,32 +2016,96 @@
 onverter == null
+ %7C%7C !typeConverter.canConvert(objectTypeDescriptor, TYPE_STRING)
 ) %7B%0A            
@@ -2198,98 +2198,650 @@
     
-final TypeDescriptor objectTypeDescriptor = TypeDescriptor.forObject(object);%0A        if (
+return (String) typeConverter.convertValue(object, objectTypeDescriptor, TYPE_STRING);%0A%0A    %7D%0A%0A%0A    @Override%0A    protected %3CT%3E T convertOther(final Configuration configuration, final IProcessingContext processingContext, final Object object, final Class%3CT%3E targetClass) %7B%0A%0A        if (object == null) %7B%0A            return null;%0A        %7D%0A        final TypeDescriptor objectTypeDescriptor = TypeDescriptor.forObject(object);%0A        final TypeDescriptor targetTypeDescriptor = TypeDescriptor.valueOf(targetClass);%0A        final TypeConverter typeConverter = getSpringConversionService(processingContext);%0A        if (typeConverter == null %7C%7C 
 !typ
@@ -2880,35 +2880,44 @@
 Descriptor, 
-TYPE_STRING
+targetTypeDescriptor
 )) %7B%0A       
@@ -2937,32 +2937,29 @@
 uper.convert
-ToString
+Other
 (configurati
@@ -2979,32 +2979,45 @@
 gContext, object
+, targetClass
 );%0A        %7D%0A   
@@ -3021,38 +3021,33 @@
         return (
-String
+T
 ) typeConverter.
@@ -3089,30 +3089,40 @@
 riptor, 
-TYPE_STRING
+targetTypeDescriptor
 );%0A
+%0A
     %7D%0A%0A%0A
@@ -3119,24 +3119,27 @@
 %0A%0A    %7D%0A%0A%0A%0A%0A
+%0A%0A%0A
     private 

Complete implementation of Spring-based StandardConversionService