Using Spring Type Conversion

Spring type conversion make it possible to create converters which can convert between different types of objects. These converters can be registered to a conversion service. This service can be used to convert a given object to a given type.

To create a converter you have to write a class which implements the following interface.

public interface Converter<S, T> {
   T convert(S source);
}

Built in converters

Several converter implementations are provided in the core.convert.support package as a convenience. These include converters from Strings to Numbers and other common types. Consider StringToInteger as an example Converter implementation:

final class StringToInteger implements Converter<String, Integer> {
    public Integer convert(String source) {
        return Integer.valueOf(source);
    }
}

Creating custom converters

To create a custom converter you have to write a class similar to the above, where you describe how to convert from one type to another.

First let’s create two types. We will perform the conversion between them.

ConsumerEntity contains the data of a consumer queried from the database.

public class ConsumerEntity {
   private int id;
   private String firstName;
   private String lastName;
   private String zipCode;
   private String city;
   private String street;

   // Getters and setters omitted for brevity
}

ConsumerBo on the other hand contains the fields that we would like to display about the consumer. It is called a BO (Business Object) because it contains data important to the business. In this case it means data we would like to display about a consumer.

public class ConsumerBo {
    private int id;
    private String name;
    private String address;

    // Getters and setters omitted for brevity
}

Now, let’s create a class called ConsumerConverter which will perform the conversion. Remember, this class must implement org.springframework.core.convert.support.Converter.

import org.springframework.core.convert.converter.Converter;
import conversion.example.ConsumerBo;
import conversion.example.ConsumerEntity;

public class ConsumerConverter implements Converter<ConsumerEntity, ConsumerBo>{
    public ConsumerBo convert(ConsumerEntity source) {
        ConsumerBo result = new ConsumerBo();
        result.setId(source.getId());
        result.setName(source.getFirstName() + " " + source.getLastName());
        result.setAddress(source.getZipCode() + ", " + source.getCity() + ", " + source.getStreet());
        return result;
    }
}

Register the custom converter

To use our custom converters, we have to register them in the application context xml using a bean definition like the following:

<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
    <property name="converters">
        <list>
            <bean class="conversion.example.converter.ConsumerConverter"/>
        </list>
    </property>
</bean>

Invoking the conversion service

Below there is an examaple of using the conversion service we created. The service itself is autowired by Spring into the controller. This autowired object can be used to explicitly perform the conversion from one class to another.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.ConversionService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import conversion.example.ConsumerBo;
import conversion.example.ConsumerEntity;

@Controller
public class HomeController {

    @Autowired
    ConversionService conversionService;

    @RequestMapping("home")
    public ModelAndView displayUser() {
        ModelAndView result = new ModelAndView("user");

        ConsumerEntity consumerEntity = new ConsumerEntity();
        consumerEntity.setId(1);
        consumerEntity.setFirstName("John");
        consumerEntity.setLastName("Doe");
        consumerEntity.setCity("Tokio");
        consumerEntity.setZipCode("2000");
        consumerEntity.setStreet("Oykayong 20.");

        ConsumerBo consumerBo = conversionService.convert(consumerEntity, ConsumerBo.class);

        result.addObject("user", consumerBo);
        return result;
    }
}

Download source

You can download the source code of a working example here.

References

How to add syntax highlighting to Blogger

Blogger has a lot of great widgets built in to include various funcionality in your blog, but syntax highlighting is not one of them. Fortunately it is fairly easy to add this capability, you just have to do some customizing as described below.

Add custom CSS and JavaScript includes to the template

As the first step you have to include the CSS and JavaScript code into the template you are currently using. On you blog’s admin panel select Template and click Edit HTML.

template_edit_html_2014_01_19

In the code search for the </head> tag and just before that insert the following code.

<!-- Syntax Highlighter START -->
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shCore.css' rel='stylesheet' type='text/css'/>
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js' type='text/javascript'/>

<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushAS3.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushBash.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushColdFusion.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCSharp.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCpp.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCss.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushDelphi.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushDiff.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushErlang.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushGroovy.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJScript.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJava.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJavaFX.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPerl.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPhp.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPlain.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPowerShell.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPython.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushRuby.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushScala.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushSql.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushVb.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js' type='text/javascript'/>

<script language='javascript' type='text/javascript'>
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.all();
</script>
<!-- Syntax Highlighter END -->

Add code to your post

Go to the post editor screen and switch to HTML view.

post_edit_screen_2014_01_19

In the HTML view paste your code and surround it with a <pre> tag, and set the highlighting mode in the class attribute of the tag. Here is an example of that.

<pre class="brush:java">
public static void main(String[] args) {
    System.out.println("Hello World!");
}

This produces the following output:

public static void main(String[] args) {
    System.out.println("Hello World!");
}

Keep in mind that if you have angle brackets in your code then you have to escape them (with this tool for example), otherwise it will be interpreted as part of the HTML.

Getting rid of unnecessary brushes

The code I pasted above contains a lot of brushes, which make it possible to use a lot of different highlighting methods. However if you know for sure that you will never need some of them, then you can just delete the unnecessary ones saving some HTTP requests.

Use other themes

There are a few CSS themes available on the author’s website if you would like to change the appearance of the code blocks.

Warning

With certain Blogger themes this method may not work.

Pictures uploaded to Blogger post become grey

Recently I was writing my first ever blog post and wanted to include some screenshots in it. I uploaded the screenshots in Blogger’s editor and I noticed that something was off with them. It became even more apparent when I published the post and viewed it on the live site.

The images had too much grey in them. My first though was that I messed them up when converting to a smaller size, but quickly ruled out this possibility when I saw that the images on my local drive had no traces of the unwanted greyness.

The first image is the original, the second is after it was uploaded to blogger.

example_image_white_2014_01_19

example_image_grey_2014_01_19

It turned out that Google has some “smart” algorithms which try to enhance to quality of your uploaded images. And of course it is enabled by default. I don’t know how they perform on regular pictures taken for example on a holiday, but they definitely do bad for screenshots with a lot of white in them.

The following images illustrate step-by-step how to turn this feature off:

google_plus_profile_button_2014_01_19

google_plus_settings_button_2014_01_19

google_plus_auto_enhance_auto_awsome_2014_01_19

Use the first heading as page name in DokuWiki

By default the text of the links that point to DokuWiki pages is the same as the page name, which is something like use_heading_as_page_name.

This is not too readable. Luckily DokuWiki provides a configuration option that lets you change this behaviour.

Open conf/local.php and add the following row to it:

$conf['useheading'] = 1;

The result of this setting is that the text of all links that point to DokuWiki pages will now be the same as the first heading of the page.