<commit_before>/*
 * =============================================================================
 * 
 *   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 <NAME>
 * 
 * @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) {<commit_msg>Complete implementation of Spring-based StandardConversionService<commit_after>/*
 * =============================================================================
 * 
 *   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.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 <NAME>;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) {

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

    }


    @Override
    protected <T> T convertOther(final Configuration configuration, final IProcessingContext processingContext, final Object object, final Class<T> targetClass) {

        if (object == null) {
            return null;
        }
        final TypeDescriptor objectTypeDescriptor = TypeDescriptor.forObject(object);
        final TypeDescriptor targetTypeDescriptor = TypeDescriptor.valueOf(targetClass);
        final TypeConverter typeConverter = getSpringConversionService(processingContext);
        if (typeConverter == null || !typeConverter.canConvert(objectTypeDescriptor, targetTypeDescriptor)) {
            return super.convertOther(configuration, processingContext, object, targetClass);
        }
        return (T) typeConverter.convertValue(object, objectTypeDescriptor, targetTypeDescriptor);

    }







    private static final TypeConverter getSpringConversionService(final IProcessingContext processingContext) {
{'fraction_non_alphanumeric': 0.12401372212692967, 'fraction_numerical': 0.005488850771869639, 'mean_word_length': 4.262635379061372, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2773634', 'n_tokens_mistral': 1390, 'n_tokens_neox': 1309, 'n_words': 396}