Latest Publications

JPA EntityManager merge may need a refresh

Problem: I am using using JPA to update (save changes) of an entity. Then I simply want to re-display it in a web view. Of course the update is done using an EntityManager merge method. Howver in my view I am losing the associated entities(for example: other OneToMany entities related to the one that I am updating).

This is in fact the natural behavior of the EntityManager.merge method:

EntityManager.merge method copies the state of the passed object to the persistent entity with the same identifier and then returns a reference to that persistent entity. The object passed is not attached to the persistence context.

To solve the problem I just had to do an EntityManager refresh after the merge.
If you are not returning to the entity’s view, refresh is not necessary.

For some additional info look here:

RTF character encoding

I am using RTFTemplate for merging an RTF model(Template) with data comming from Java objects. One of the problems with RTFTemplate is that it does not property encode characters.

After testing without success with different Velocity configuration in RTFTemplate and characters escaping, I found out that the best solution is to use the iText-rtf package. iText has build-in functionality to print into RTF documents and its class RtfDocument has methods for encoding characters according to RTF standard.

All I needed to do is to prepare my context using the following escape method:

import com.lowagie.text.rtf.document.RtfDocument;
...
	public static String escape(String sentence) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		try {
			new RtfDocument().filterSpecialChar(baos, sentence, true, true);
		} catch (IOException e) {
			// will not happen for ByteArrayOutputStream
		}
		return new String(baos.toByteArray());
	}

The following dependencies are necessary:

	    
			com.lowagie
			itext-rtf
			2.1.4
		
		
			com.lowagie
			itext
			2.1.7
		

IOException while loading persisted sessions

Tomcat saves its state when shutdowns. Improper shutdown, for example forced shutdown in developer environment(e.i. Eclipse), may cause the following error: “SEVERE: IOException while loading persisted sessions”.

Solution is simple:
- stop Tomcat
- delete all the content in the your Tomcat/work directory (main cause can be SESSION.ser)
- restart Tomcat

Unaccented Letters

If you need to remove accented letters from a string and replace them by their regular ASCII equivalent, here is a possible solution:

Step 1
Available since java 1.6, you may use the java.text.Normalizer.normalize() for pre-processing:
if you pass for example pass à, the method returns a + ` .

Step 2
Then using a regular expression, we clean up the string to keep only valid US-ASCII characters.

import java.text.Normalizer;
import java.util.regex.Pattern;

public class UnaccentedLetters {

	public static String process(String s) {

		// java.text.Normalizer is available from JDK1.6

		String temp = Normalizer.normalize(s, Normalizer.Form.NFD);
		Pattern pattern = Pattern.compile("\\p{InCombiningDiacriticalMarks}+");
		return pattern.matcher(temp).replaceAll("");
	}

	public static void main(String args[]) throws Exception {
		String value = "é à î";
		System.out.println(UnaccentedLetters.process(value));
		// output : e a i
	}
}

When can be useful: preparing text for indexing, prepare file names before writing them in a zip.

How To Generate XSD from XML

Suppose you get XML files as input for a project and you need to further analyze them(e.g. in order to write a transformation), but… you are missing the XSD(this means XML Schema definition,  a .xsd file that describes the structure of an XML).

There are several free solutions for generating XSD from XML:

  1. Online tools
  2. Command line tools
    • Trang
      Java based(version 5 or newer).
      Example of command line:    java -jar trang.jar   input.xml   schema.xsd

Hello world!

Welcome!

I’m starting this blog as a notebook for the things on which I am experimenting or for stuff I may find interesting.