Example usage for org.springframework.core.convert.converter ConverterRegistry addConverter

List of usage examples for org.springframework.core.convert.converter ConverterRegistry addConverter

Introduction

In this page you can find the example usage for org.springframework.core.convert.converter ConverterRegistry addConverter.

Prototype

void addConverter(GenericConverter converter);

Source Link

Document

Add a generic converter to this registry.

Usage

From source file:com.github.bfour.fpliteraturecollector.application.FPLCNeo4jConfiguration.java

@Bean
protected ConversionService neo4jConversionService() throws Exception {
    ConversionService conversionService = super.neo4jConversionService();
    ConverterRegistry registry = (ConverterRegistry) conversionService;
    registry.addConverter(new StringToCrawlerConverter());
    registry.addConverter(new CrawlerToStringConverter());
    registry.addConverter(new StringToPathConverter());
    registry.addConverter(new PathToStringConverter());
    registry.addConverter(new StringToColorConverter());
    registry.addConverter(new ColorToStringConverter());
    registry.addConverter(new StringToLinkConverter());
    registry.addConverter(new LinkToStringConverter());
    return conversionService;
}

From source file:org.grails.datastore.mapping.appengine.engine.AppEngineEntityPersister.java

public AppEngineEntityPersister(MappingContext context, final PersistentEntity entity, AppEngineSession conn,
        DatastoreService datastoreService) {
    super(context, entity, conn);
    this.datastoreService = datastoreService;
    this.entityFamily = getFamily(entity, entity.getMapping());
    ConverterRegistry conversionService = context.getConverterRegistry();

    conversionService.addConverter(new Converter<Object, com.google.appengine.api.datastore.Key>() {
        public com.google.appengine.api.datastore.Key convert(Object source) {
            if (source instanceof com.google.appengine.api.datastore.Key) {
                return (com.google.appengine.api.datastore.Key) source;
            } else if (source instanceof Long) {
                return KeyFactory.createKey(entityFamily, (Long) source);
            } else {
                return KeyFactory.createKey(entityFamily, source.toString());
            }/* w w  w  . j  av  a 2 s .co m*/
        }
    });
}

From source file:org.grails.datastore.mapping.model.types.BasicTypeConverterRegistrar.java

public void register(ConverterRegistry registry) {
    registry.addConverter(new Converter<Date, String>() {
        public String convert(Date date) {
            return String.valueOf(date.getTime());
        }/*w ww.  ja  v  a2 s.com*/
    });

    registry.addConverter(new Converter<Date, Calendar>() {
        public Calendar convert(Date date) {
            final GregorianCalendar calendar = new GregorianCalendar();
            calendar.setTime(date);
            return calendar;
        }
    });

    registry.addConverter(new Converter<Integer, Long>() {
        public Long convert(Integer integer) {
            return integer.longValue();
        }
    });

    registry.addConverter(new Converter<Long, Integer>() {
        public Integer convert(Long longValue) {
            return longValue.intValue();
        }
    });

    registry.addConverter(new Converter<Integer, Double>() {
        public Double convert(Integer integer) {
            return integer.doubleValue();
        }
    });

    registry.addConverter(new Converter<CharSequence, Date>() {
        public Date convert(CharSequence s) {
            try {
                final Long time = Long.valueOf(s.toString());
                return new Date(time);
            } catch (NumberFormatException e) {
                throw new IllegalArgumentException(e);
            }
        }
    });

    registry.addConverter(new Converter<CharSequence, Double>() {
        public Double convert(CharSequence s) {
            try {
                return Double.valueOf(s.toString());
            } catch (NumberFormatException e) {
                throw new IllegalArgumentException(e);
            }
        }
    });

    registry.addConverter(new Converter<CharSequence, Integer>() {
        public Integer convert(CharSequence s) {
            try {
                return Integer.valueOf(s.toString());
            } catch (NumberFormatException e) {
                throw new IllegalArgumentException(e);
            }
        }
    });

    registry.addConverter(new Converter<CharSequence, Long>() {
        public Long convert(CharSequence s) {
            try {
                return Long.valueOf(s.toString());
            } catch (NumberFormatException e) {
                throw new IllegalArgumentException(e);
            }
        }
    });

    registry.addConverter(new Converter<Object, String>() {
        public String convert(Object o) {
            return o.toString();
        }
    });

    registry.addConverter(new Converter<Calendar, String>() {
        public String convert(Calendar calendar) {
            return String.valueOf(calendar.getTime().getTime());
        }
    });

    registry.addConverter(new Converter<CharSequence, Calendar>() {
        public Calendar convert(CharSequence s) {
            try {
                Date date = new Date(Long.valueOf(s.toString()));
                Calendar c = new GregorianCalendar();
                c.setTime(date);
                return c;
            } catch (NumberFormatException e) {
                throw new IllegalArgumentException(e);
            }
        }
    });
}

From source file:org.grails.datastore.mapping.mongo.config.MongoMappingContext.java

private void initialize(Class[] classes) {
    registerMongoTypes();// w w w .  jav  a  2  s .c o m
    final ConverterRegistry converterRegistry = getConverterRegistry();

    converterRegistry.addConverter(new Converter<String, ObjectId>() {
        public ObjectId convert(String source) {
            if (ObjectId.isValid(source)) {
                return new ObjectId(source);
            } else {
                return null;
            }
        }
    });

    converterRegistry.addConverter(new Converter<ObjectId, String>() {
        public String convert(ObjectId source) {
            return source.toString();
        }
    });

    converterRegistry.addConverter(new Converter<byte[], Binary>() {
        public Binary convert(byte[] source) {
            return new Binary(source);
        }
    });

    converterRegistry.addConverter(new Converter<Binary, byte[]>() {
        public byte[] convert(Binary source) {
            return source.getData();
        }
    });

    converterRegistry.addConverter(new Converter<Decimal128, BigDecimal>() {
        @Override
        public BigDecimal convert(Decimal128 source) {
            return source.bigDecimalValue();
        }
    });

    converterRegistry.addConverter(new Converter<BigDecimal, Decimal128>() {
        @Override
        public Decimal128 convert(BigDecimal source) {
            return new Decimal128(source);
        }
    });

    converterRegistry.addConverter(new Converter<Decimal128, BigInteger>() {
        @Override
        public BigInteger convert(Decimal128 source) {
            return source.bigDecimalValue().toBigInteger();
        }
    });

    converterRegistry.addConverter(new Converter<BigInteger, Decimal128>() {
        @Override
        public Decimal128 convert(BigInteger source) {
            return new Decimal128(new BigDecimal(source.toString()));
        }
    });

    for (Converter converter : CodecExtensions.getBsonConverters()) {
        converterRegistry.addConverter(converter);
    }

    addPersistentEntities(classes);
    hasCodecCache.clear();
}

From source file:org.grails.datastore.mapping.simpledb.model.types.SimpleDBTypeConverterRegistrar.java

protected void overwrite(ConverterRegistry registry, @SuppressWarnings("rawtypes") Converter converter) {
    //get type info for the specified converter
    GenericConverter.ConvertiblePair typeInfo = getRequiredTypeInfo(converter, Converter.class);
    if (typeInfo == null) {
        throw new IllegalArgumentException("Unable to the determine sourceType <S> and targetType <T> which "
                + "your Converter<S, T> converts between; declare these generic types. Converter class: "
                + converter.getClass().getName());
    }/*from  w  w w. j a va 2 s .  c o  m*/

    //now remove converters that we will overwrite for SimpleDB
    registry.removeConvertible(typeInfo.getSourceType(), typeInfo.getTargetType());

    //now add
    registry.addConverter(converter);
}

From source file:org.grails.plugins.AbstractGrailsPluginManager.java

/**
 * Base implementation that simply goes through the list of plugins and calls doWithRuntimeConfiguration on each
 * @param springConfig The RuntimeSpringConfiguration instance
 *///from   w  w w .  j  av  a2  s.co m
public void doRuntimeConfiguration(RuntimeSpringConfiguration springConfig) {
    ApplicationContext context = springConfig.getUnrefreshedApplicationContext();
    AutowireCapableBeanFactory autowireCapableBeanFactory = context.getAutowireCapableBeanFactory();
    if (autowireCapableBeanFactory instanceof ConfigurableListableBeanFactory) {
        ConfigurableListableBeanFactory beanFactory = (ConfigurableListableBeanFactory) autowireCapableBeanFactory;
        ConversionService existingConversionService = beanFactory.getConversionService();
        ConverterRegistry converterRegistry;
        if (existingConversionService == null) {
            GenericConversionService conversionService = new GenericConversionService();
            converterRegistry = conversionService;
            beanFactory.setConversionService(conversionService);
        } else {
            converterRegistry = (ConverterRegistry) existingConversionService;
        }

        converterRegistry.addConverter(
                new Converter<GrailsApplication, org.codehaus.groovy.grails.commons.GrailsApplication>() {
                    @Override
                    public org.codehaus.groovy.grails.commons.GrailsApplication convert(
                            GrailsApplication source) {
                        return new LegacyGrailsApplication(source);
                    }
                });
        converterRegistry.addConverter(new Converter<NavigableMap.NullSafeNavigator, Object>() {
            @Override
            public Object convert(NavigableMap.NullSafeNavigator source) {
                return null;
            }
        });
    }
    checkInitialised();
    for (GrailsPlugin plugin : pluginList) {
        if (plugin.supportsCurrentScopeAndEnvironment()
                && plugin.isEnabled(context.getEnvironment().getActiveProfiles())) {
            plugin.doWithRuntimeConfiguration(springConfig);
        }
    }
}