Tuesday, August 18, 2009

Vacation report

So - we made it home after 3 weeks in mostly France. A great trip with many memorable moments. There are some great benefits with bringing your own car. It is much easier to improvise and it is possible to bring home bottles of strong liquid. In Provence we picked a winemaker by chance while driving on small inland roads. They let us taste several kinds of vin rouge and we ended up buying 2x6 bottles that are now safely stored in our basement.

We started our trip with a couple of days in Bruges - once the main trading town in north western Europe. Nowadays it is a very nice and lively city to stroll around in with many old houses and canals. And great beer. Every place has a couple of great beers on tap. Compare that to Sweden where we have to del with Spendrups and Pripps on tap - ouch. The food was good - the best place we ate at was De Vlaamsche Pot with traditional flemish cuisine. Highly recommended. On our way towards France we stopped at the trappist monastery of Westvlateren and had great lunch with even greater beer. The shop had run out of it unfortunatley so we couldn't bring any home.

Next Paris. Me and Käthe met with my kids there and then we made all the touristy things: Disneyland, Louvres, Tour Eiffel, Champs Elysees, boat trip on Seine, a glimpse of Tour de France, Montmartre etc. We also visited the great impressionist Musee d'Orsay. A great couple of days with some really nice food experiences as well. One lunch at Relais de l'Entrecote on 15 rue Marbeuf close to elysees where they only serve heavenly entrecote with frites. Great vin de pays to go with it. The place to visit if you are really hungry. Last night we tried another "relais" called Relais de Gascon offering huge brilliant salads and great pasta.

Walk around Paris


From Paris we headed towards Normandie via short visits to Versailles (gardens only - schloss closed on mondays....) and Rouen with the cathedral famous for being painted in different light by Monet. We stayed a couple of nights at the charming Hotêl St Etienne in Caen. This one star hotel was the cheapest and best we stayed at and the only one that came with free wireless. We made a couple of daytrips from Caen to the d-day beaches and cemetaries and to the nice monastery island Mont St Michel. The outstanding l'Embroche on 17 Rue Porte au Berger served the best meal of the entire vacation. Standard french food extremly well cooked.

Normandie : omaha beach

mont st michel

Now we headed south to the house in Sollies-Toucas north of Toulon where we were to spend a week with all our kids flying and biking in from their different locations. On our way there we stopped at hotels in Poitiers and Le Puy-en-Velay. Outside of Poitiers the very french theme park Futuroscope was a nice experience with a very amazing laser show at nightfall. In Le Puy-en-Velay we had great salads at le Croco on 5 Rue Chaussade and an awesome local beer called Villavia. Le puy is a great old town with a cathedral, monastery and red mary statue at the hill and cliff in the midst of the town. Religion at its heart which make sense - this is one of the starting points for the pilgrimage to Santiago Compostela in north western Spain.

le puy en velay by night

The week in Provence was mostly lazy staying at the house by the pool and cooking great meals with great wine. We made one daytrip to Avignon to visit the papal palaces - really nice. And then we headed home via Italy and the alps. It is amazing that you get great food even at the rest stops in italy. We had great Pasta di Liguria on our way home and we made sure to stop one last time before the swiss border to have a decent espresso. The road through the alps were astonishing - no pics taken though. The last day we managed to drive from Ulm in southern Germany all the way home - some 1600 kms.

More pics are available in this set at flickr. Not all pics are there yet though. The tiny camera has yet to make it on to the internet....

Sunday, August 16, 2009

JAXB with Maven

Fredrik was recently blogging about "Two way SSL with Java" and about his frustration about the lack of knowledge and documentation. This time I want to blog about another area of software development where frustration is running high - XML processing. On the surface things looks easy, but who does not remember the classpath problems around XML parsing in the early days (SAX vs XERCES vs DOM, etc). And who really fully understands the XSD syntax and is able to create a properly designed xsd? And documentation - let's just not talk about it!

But let's get to the actual problem. I wanted to use JAXB to write a parser for XML files matching a given xsd. Given that Sun introduced JAXB as official XML binding framework for Java it seems the obvious choice for the task. Unfortunately, another requirement was that the code would work under JDK 5 and 6. However, the JAXB API is only offical part of the JDK since version 6. To solve this problem a little trick is needed. Let's take it step by step using Maven as build system. First we have to include the plugin by adding the following to the POM:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>jaxb2-maven-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>xjc</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <packageName>org.hibernate.validation.xml</packageName>
        <outputDirectory>${basedir}/target/generated-sources</outputDirectory>
        <extension>true</extension>
    </configuration>
</plugin>

The task of the plugin is to create the Java binding classes for the xsd. Per default the xsd file has to be placed into the directory /src/main/xsd. The configuration in the POM specifies the default package name for the generated classes and in which directory the classes have to be generated. The meaning of extension we will discuss a little later.

The setup so far will work fine with JDK 6, but in order to make it work for JDK 5 as well we have to use profiles. The idea is to add the required libraries which are missing in JDK 5 depending on the JDK version. This can be achieved by adding the following to the POM:

<profiles>
        <profile>
            <id>jaxb</id>
            <activation>
                <jdk>1.5</jdk>
            </activation>
            <dependencies>
                <dependency>
                    <groupId>javax.xml.bind</groupId>
                    <artifactId>jaxb-api</artifactId>
                    <version>2.1</version>
                </dependency>
                <dependency>
                    <groupId>com.sun.xml.bind</groupId>
                    <artifactId>jaxb-impl</artifactId>
                    <version>2.1.3</version>
                </dependency>
            </dependencies>
        </profile>
    </profiles> 

So far things were quite simple to figure out. The hard part is one more requirement I haven't mentioned yet. The parser should be tolerant against whitespaces. Some googling and I figured out that in JAXB this kind of things are done with adapters. In fact the CollapsedStringAdapter seems to be exactly what I want. The javadoc says:"This adapter removes leading and trailing whitespaces, then truncate any sequnce of tab, CR, LF, and SP by a single whitespace character '". Great, but how do I configure JAXB so that it automatically uses this adapter. Here the frustration really starts. The best a search for CollapsedStringAdapter results in is the javadoc for this class. Nothing about how to use it. Not in plain JAXB nor in conjunction with the maven plugin. Frustration started to mount again. To cut a long story short. It took many mailing list posts and a lot of try and error to figure it out. The outcome is that first of all you need the true bit in the plugin configuration. Next you have to create a file called binding-customization.xjb in src/main/xjb. The file has to contain the following:

<?xml version="1.0" encoding="UTF-8"?>
<jxb:bindings version="1.0" xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc">
    <jxb:bindings schemaLocation="../xsd/validation-mapping-1.0.xsd" node="/xs:schema">
        <jxb:globalBindings>
            <xjc:javaType name="java.lang.String" xmlType="xs:string" 
                adapter="javax.xml.bind.annotation.adapters.CollapsedStringAdapter"/>
        </jxb:globalBindings>
    </jxb:bindings>
</jxb:bindings>

In the customization file a global binding gets defined for all java.lang.String types. It looks simple, but believe me it was damn hard to figure out. Hopefully this blog will save someone the painful hours I had.

My personal conclusion was that documetnation around XML related topics still sucks :(

--Hardy