Jabbering Giraffe

Cocoon sitemap variables

Whilst dragging myself through an issue for a client last night, I found another cocoon feature I wasn’t aware of: sitemap variables.

Of course, I’d seen Input Modules before, in fact I’ve written one. They’re nice and simple and look like this:

  

But then I came across this little line:

  

What the heck is that? I immediately jumped into the cocoon source code, thanks to Jukka Zitting. First, I saw _no_ examples of this syntax in any sitemap.xmap. Marvellous.

After a bit of digging, I ended up at VariableExpressionTokenizer (thanks to a comment). This revealed (in yet another comment) that strings of the form ${…} are handled using the new cocoon-expression-language.

… snip several hours of wasted time …

Now, at this point, I’ve gone down the garden path. I’ve spent quite a few hours debugging this and it’s completely in the wrong direction — it seems like cocoon has far too many ways of inserting variables into the sitemap.

Finally, I’ve managed to end up looking at AvalonUtils.replaceProperties() (which gets called from SitemapLanguage.build()). This gets passed in a Settings object (i.e. something you configured with the cocoon-spring-configurator). So any reference to ${something} will look it up directly in your configuration.

It’s actually slightly more generic than that. Looking through the code, AvalonUtils.replaceProperties() also gets called any time that an avalon component is set up. So, any older components can benefit from the new Settings as well.

Summary:

  1. Create a property file in your block. e.g. src/main/resources/META-INF/cocoon/properties/app.properties.
  2. Set a value in there. e.g. contact.email=dom@example.com
  3. In src/main/resources/COB-INF/sitemap.xmap, you can now say ${contact.email} and the correct value will be substituted. e.g.