Monday, April 22, 2013

Character set encoding in xml



This is one of the interesting topics to work on but sometimes it gets really difficult to debug. One issue that I came across recently was related to character encoding in ooxml documents..
In my case, we were reading a template ooxml (Word 2007/10 based document) and replacing a place holder with some specific value and then writing it to another file. The template based ooxml document would open from Word application and show the placeholder.






However, when we read the file and replaced the place holder with valid value, the  newly created  ooxml document wouldn’t open in Word and give the following error.


On further analysis, I found that the newly created invalid xml somehow added some junk characters as shown below (left hand side is invalid xml and right hand side is valid one) –


On further analysis, I found that it was an issue while reading the file and by default, cp1252 character encoding was being used.


When I changed it to UTF- 8 as shown below,




it worked like a charm.

We need to be really careful especially when dealing with xml documents especially with respect to character encoding because what goes in doesn’t necessarily come out as it is....


Tuesday, April 16, 2013

Using String IN Clause for MyBatis




Recently, I came across an issue where I had to use IN clause with String in MyBatis.
We cannot use it by default because MyBatis assumes it as a single string and so, we don’t get the expected result.
After searching for some time, I was finally able to implement it using MyBatis as shown below.
Event Sql Map
<mapper namespace="monitor">
….
….
<select id="CountEvents" parameterType="map" resultType="java.lang.Integer">
                                select count(*) as value
                                from TABLE
                                <where>
                                                <if test="list != null">
                                                                EVENT_STATUS IN
                                                                <foreach item="itemList" index="index"
                                                                                 collection="list"
                                                                                open="(" separator=","
                                                                                close=")">
                                                                                #{itemList}
                                                                </foreach>
                                                </if>
                                </where>
                </select>

Invocation 
moSession.selectOne(fsSqlName, foQueryParameters);
        where
moSession  = org.apache.ibatis.session.SqlSession
               fsSqlName = monitor. CountEvents
               foQueryParameters = List containing Strings to be included 
                                                       inside IN clause( itemList  )
                                fsStatus = A,B,C ( for below case ) 
                                String loItem[]  = null;
                                List itemList = null;        
                                 if(!StringUtils.isEmpty(fsStatus ))
                                  { 
                                                 loItem = fsStatus.split(",");
                                                 itemList  = Arrays.asList(loItem);
}