Joe Developer
ikvm and freemarker in C#
I've been doing a fair bit of .NET work lately, mostly C#, and I've a need for a templating engine for dynamic text generation. Coming from the Java realm my first thought was of Freemarker. I've used FM a decent amount, am comfortable with it, and want the same fucntionality in a C# app.
Perhaps there's something out there, I've searched around a bit, but couldn't find just the thing to get the job done.
Enter ikvm. ikvm is an interesting project in that it aims to do .NET<->Java interop in a native way, offering a lot of options. One of the options is the simplest - use your Java libraries directly in your .NET code by translating the java bytecode into msil (Microsoft Intermediate Language - the .NET bytecode equiv).
Turns out this works exactly as advertised, and within an hour or so I was using .NET pojos (poNos?) in a little console app with a little FM template hard coded.
There are a few problems though, the largest of which is the difference between the javabeans naming convention for properties and the .NET convention.
For example, if I have a Java class Person with a property name, I'd have:
public String getName(){...}
public void setName(String value){...}
in C#, I'd have:
public string Name
{
get
{
return this.name;
}
set
{
this.name = value;
}
}
note that "value" is an implicitly typed object of the type that the property is... in this case a string. It's a different syntax to get used to, but you can do everything you'd do in a getter and setter in what is an arguably more readable syntax.
So I use the ikvm tool ikvmc to translate my bytecode (had to fiddle a bit to also translate all the runtime dependencies) into freemarker-2.3.8wdeps.dll (like a jar file but I "compiled" all the dependencies into one library - bad for complex dependencies but easier for a quick test).
Simple, examples go off without a hitch. (using primatives in the FM context), but when I put an instance of Person in, things don't go so well. I try to use ${person.Name} in my FM template and it blows up exactly the way you'd think it would - FM attempts to find a method called "getName" on the Person class, it's not there on the .NET class, so things don't work. Obviously. It occurs to me that the .NET way with properties is much easier to think about what with the not having to create getName from name to call the right method, but that's the way things are. (see the getter/setter versus Property bit above)
Quick solution - create a little decorator that extends HashMap, overrides get(Object key), use the .NET reflection system there and return the right value. By the way, I extended HashMap in .NET via the wonder that is the IKVM.GNU.Classpath library that ships with ikvm. Thanks to the folks who make this little bit of magic possible.
This creates a number of issues as far as I'm concerned, starting with how absolutely kludgy it is. In my template rather than ${person.Name} I have to use ${person['Name']}. This creates all sorts of other issues, as I'd have to do far more messy magic to get this to work for complex object graphs (say, the Address property of a Person expression like ${person.Address.ZipCode}).
The "right" answer is, I think, to create a different object wrapper implementation for FM and use that in the FM configuration, one that can do all the nice Method/Property caching for me(as the FM implementation does for performance), resolve indexed properties, and the like. I'll be working on that in my copious free time as I love the power that FM gives, but I'm living (not unhappily) in a .NET world quite a bit these days.
The short story is - ikvm and the GNU classpath project rock, as I was able to start to get the job done using a great Java library from within a .NET application in a matter of a couple of hours. Amazing. I love code re-use, and since there are so many great OSS Java libs out there, I'm sure now that some of them will be available to me in .NET - a boon to productivity and surely a more less clunky interop than web services, a .NET embedded JVM (though ikvm does that too as a fully embeddable .NET JVM implementation), or Java->JNI->COM->.NET.
Note too - this says nothing of the performance of the generated msil... right now it's about POC and functionality, not billion transactions per second on a 486.
For the curious, below is the command line I used to get my version of FM and dependencies compiled to a dll. There are a TON of warnings with this configuration, but hey, I just DL'ed ikvm a few hours ago, it's going to take me a minute to get the kinks worked out with the tool.
set PATH=c:\java\lib\ikvm.32\ikvm-0.32.0.0\bin;%PATH% ikvmc -out:freemarker-2.3.8wdeps.dll -target:dll ant.jar dom4j-1.6.jar javax.servlet.jar javax.servlet.jsp.jar jaxen-core.jar jaxen-jdom.jar jdom.jar log4j-1.2.11.jar xalan-2.6.0.jar xerces-2.4.0.jar xerces.jar xercesImpl.jar xml-apis.jar freemarker.jarAnd here's my shady little test code in C#
using System;
using System.Collections.Generic;
using System.Text;
using freemarker;
using freemarker.template;
using java.io;
namespace freemarker_test
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Starting...");
Configuration config = new Configuration();
Person p = new Person();
p.Name = "Brian";
string templateText = "number: ${testNumber}, name: ${person['Name']}";
StringReader reader = new StringReader(templateText);
Template t = new Template("fooTemplate", reader, config);
java.util.Map map = new java.util.HashMap();
map.put("testNumber", 1);
map.put("person", new DecoratingMap(p));
StringWriter writer = new StringWriter();
t.process(map, writer);
Console.WriteLine("Text: " + writer.toString());
Console.WriteLine("Press any key to exit...");
Console.ReadLine();
}
}
public class Person
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
}
//a terrible little hack until a full on .NET object wrapper can be created for FM
public class DecoratingMap : java.util.HashMap
{
private object o;
public DecoratingMap(object subject)
{
this.o = subject;
}
public override object get(object key)
{
System.Reflection.PropertyInfo p = o.GetType().GetProperty((string)key);
object ret = p.GetValue(o, null);
return ret;
}
}
}
Posted at 10:49PM Dec 30, 2006 by Brian in General | Comments[702]
Posted by Student Loan Consolidation Program on February 05, 2007 at 06:58 AM CST #
Posted by Google Blog on February 05, 2007 at 01:15 PM CST #
Posted by manhattan on February 15, 2007 at 11:39 PM CST #
Posted by air conditioner window unit on February 16, 2007 at 03:19 PM CST #
Posted by cover nissan titan tonneau on February 17, 2007 at 08:48 PM CST #
Posted by downtown houston corporate housing on February 18, 2007 at 12:24 PM CST #
Posted by student loan consolidation program on February 20, 2007 at 07:51 AM CST #
Posted by web catalog search S on February 21, 2007 at 03:01 AM CST #
Posted by web catalog search D on February 21, 2007 at 04:10 AM CST #
Posted by web catalog search F on February 21, 2007 at 05:15 AM CST #
Posted by web catalog search F on February 21, 2007 at 06:17 AM CST #
Posted by web catalog search T on February 21, 2007 at 07:22 AM CST #
Posted by web catalog search U on February 21, 2007 at 08:30 AM CST #
Posted by web catalog search N on February 21, 2007 at 09:44 AM CST #
Posted by web catalog search A on February 21, 2007 at 10:52 AM CST #
Posted by web catalog search K on February 21, 2007 at 11:57 AM CST #
Posted by web catalog search G on February 21, 2007 at 01:08 PM CST #
Posted by web catalog search S on February 22, 2007 at 10:20 PM CST #
Posted by web catalog search N on February 22, 2007 at 11:38 PM CST #
Posted by web catalog search Z on February 23, 2007 at 12:44 AM CST #
Posted by dubai job vacancy on February 23, 2007 at 01:09 AM CST #
Posted by cheap phentermine on February 23, 2007 at 02:16 AM CST #
Posted by manoscritti inediti on February 25, 2007 at 01:05 PM CST #
Posted by scaffalature per cantina on February 25, 2007 at 02:10 PM CST #
Posted by assoggettamento imposta bollo domanda asta on February 25, 2007 at 03:14 PM CST #
Posted by relative atomic mass on February 25, 2007 at 04:22 PM CST #
Posted by mercatino libro testo usato milano on February 25, 2007 at 05:32 PM CST #
Posted by camino misura on February 25, 2007 at 06:35 PM CST #
Posted by anime novembre 1984 on February 25, 2007 at 07:41 PM CST #
Posted by sartre heidegger on February 25, 2007 at 08:47 PM CST #
Posted by hip hop it on February 25, 2007 at 09:59 PM CST #
Posted by stresa lago maggiore on February 25, 2007 at 11:04 PM CST #
Posted by evidence faith no more on February 26, 2007 at 12:11 AM CST #
Posted by repubblica sudafricana ristorante on February 26, 2007 at 08:59 AM CST #
Posted by agenzia matrimoniale sordomuto on February 26, 2007 at 10:09 AM CST #
Posted by trombare vergine on February 26, 2007 at 11:20 AM CST #
Posted by grace metalious on February 26, 2007 at 12:28 PM CST #
Posted by eternity ring london on February 26, 2007 at 01:40 PM CST #
Posted by dj selection 76 on February 26, 2007 at 02:51 PM CST #
Posted by giunto estensibili flessibile on February 26, 2007 at 03:54 PM CST #
Posted by da parallela a usb on February 26, 2007 at 05:02 PM CST #
Posted by argento semilavorati on February 26, 2007 at 06:08 PM CST #
Posted by free mp3 on February 26, 2007 at 07:04 PM CST #
Posted by web catalog search P on February 26, 2007 at 07:15 PM CST #
Posted by free mp3 downloads on February 26, 2007 at 08:19 PM CST #
Posted by web catalog search A on February 26, 2007 at 08:21 PM CST #
Posted by web catalog search F on February 26, 2007 at 09:29 PM CST #
Posted by web catalog search D on February 26, 2007 at 10:35 PM CST #
Posted by web catalog search V on February 26, 2007 at 11:44 PM CST #
Posted by web catalog search G on February 27, 2007 at 10:16 PM CST #
Posted by free mp3 on February 27, 2007 at 10:53 PM CST #
Posted by web catalog search B on February 27, 2007 at 11:29 PM CST #
Posted by free mp3 downloads on February 28, 2007 at 03:11 AM CST #
Posted by free music downloads on February 28, 2007 at 09:05 AM CST #
Posted by free music download on February 28, 2007 at 04:12 PM CST #
Posted by free music on March 01, 2007 at 02:47 AM CST #
Posted by free music on March 01, 2007 at 02:49 AM CST #
Posted by MUSIC VIDEOS on March 01, 2007 at 04:48 AM CST #
Posted by LIMEWIRE on March 01, 2007 at 07:55 AM CST #
Posted by free music downloads on March 01, 2007 at 01:33 PM CST #
Posted by free music downloads on March 01, 2007 at 01:34 PM CST #
Posted by music download on March 01, 2007 at 06:58 PM CST #
Posted by FREE MUSIC on March 01, 2007 at 08:30 PM CST #
Posted by free music on March 01, 2007 at 08:36 PM CST #
Posted by free music download on March 02, 2007 at 12:08 AM CST #
Posted by free mp3 downloads on March 02, 2007 at 03:13 AM CST #
Posted by mp3 downloads on March 02, 2007 at 05:04 AM CST #
Posted by MYSPACE MUSIC on March 02, 2007 at 10:04 AM CST #
Posted by thong models on March 02, 2007 at 01:31 PM CST #
Posted by free mp3 on March 02, 2007 at 01:47 PM CST #
Posted by tapis roulant turner on March 02, 2007 at 02:45 PM CST #
Posted by free mp3 downloads on March 02, 2007 at 03:05 PM CST #
Posted by mp3 on March 02, 2007 at 03:08 PM CST #
Posted by gohan videl sesso on March 02, 2007 at 04:00 PM CST #
Posted by scheda controllo attuatore lineare on March 02, 2007 at 05:15 PM CST #
Posted by arthur luc besson on March 02, 2007 at 06:30 PM CST #
Posted by meteo nord est italia on March 02, 2007 at 07:46 PM CST #
Posted by free music on March 02, 2007 at 07:51 PM CST #
Posted by cosmetico viso clinique on March 02, 2007 at 09:03 PM CST #
Posted by free music on March 02, 2007 at 09:52 PM CST #
Posted by innsbruck innsbruck mountain new year s eva on March 02, 2007 at 10:17 PM CST #
Posted by veronica maia on March 02, 2007 at 11:32 PM CST #
Posted by hotel nuoro on March 03, 2007 at 12:45 AM CST #
Posted by free mp3 on March 03, 2007 at 06:34 AM CST #
Posted by free music on March 03, 2007 at 02:40 PM CST #
Posted by free music on March 04, 2007 at 03:25 PM CST #
Posted by FREE MUSIC DOWNLOADS on March 04, 2007 at 10:02 PM CST #
Posted by wellbutrin sr on March 05, 2007 at 04:14 AM CST #
Posted by prozac on March 05, 2007 at 06:33 AM CST #
Posted by butalbital apap on March 05, 2007 at 09:00 AM CST #
Posted by ambien on March 05, 2007 at 10:13 AM CST #
Posted by free music on March 05, 2007 at 10:37 AM CST #
Posted by free music downloads on March 05, 2007 at 12:41 PM CST #
Posted by acyclovir online on March 05, 2007 at 01:24 PM CST #
Posted by celexa on March 05, 2007 at 04:28 PM CST #
Posted by drug celexa on March 06, 2007 at 07:23 AM CST #
Posted by cod xanax on March 06, 2007 at 01:23 PM CST #
Posted by vibratore meccanico on March 06, 2007 at 09:34 PM CST #
Posted by olympia de gouge on March 06, 2007 at 10:47 PM CST #
Posted by disdire suoneria sms on March 07, 2007 at 12:02 AM CST #
Posted by vacanza lago trasimeno on March 07, 2007 at 01:17 AM CST #
Posted by simili rotten on March 07, 2007 at 02:33 AM CST #
Posted by diazepam without perscription on March 07, 2007 at 02:53 AM CST #
Posted by valium on March 07, 2007 at 03:26 AM CST #
Posted by capodanno rieti on March 07, 2007 at 03:45 AM CST #
Posted by carisoprodol tablets on March 07, 2007 at 04:24 AM CST #
Posted by ossa scorpione on March 07, 2007 at 05:04 AM CST #
Posted by traduzione satyricon petronio on March 07, 2007 at 06:22 AM CST #
Posted by dj ben aka billy brown on March 07, 2007 at 07:37 AM CST #
Posted by free music on March 07, 2007 at 08:43 AM CST #
Posted by cooperativa edilizia bologna itwww virgilio it on March 07, 2007 at 08:52 AM CST #
Posted by aprica alloggi on March 07, 2007 at 10:09 AM CST #
Posted by mds pro vent on March 07, 2007 at 11:24 AM CST #
Posted by parliamo musica on March 07, 2007 at 12:38 PM CST #
Posted by FREE MUSIC on March 07, 2007 at 01:45 PM CST #
Posted by lcd dvd integrato on March 07, 2007 at 01:53 PM CST #
Posted by definizione finitura superficiale lamiera on March 07, 2007 at 03:04 PM CST #
Posted by free music on March 07, 2007 at 03:51 PM CST #
Posted by minnie the moocher testo on March 07, 2007 at 04:21 PM CST #
Posted by offerta alghero on March 07, 2007 at 05:34 PM CST #
Posted by pensionamento docente on March 07, 2007 at 06:44 PM CST #
Posted by verifiche grammatica on March 07, 2007 at 08:00 PM CST #
Posted by hpv dna test on March 07, 2007 at 09:12 PM CST #
Posted by quadrato magico lotto on March 07, 2007 at 10:28 PM CST #
Posted by poltrona ufficio schienale regolabile on March 07, 2007 at 11:40 PM CST #
Posted by free music on March 07, 2007 at 11:48 PM CST #
Posted by divo download free on March 08, 2007 at 12:55 AM CST #
Posted by free music on March 08, 2007 at 05:16 AM CST #
Posted by limewire on March 08, 2007 at 07:39 AM CST #
Posted by cheap adipex online on March 08, 2007 at 08:42 AM CST #
Posted by alprazolam on March 08, 2007 at 09:50 AM CST #
Posted by FREE MUSIC on March 08, 2007 at 10:05 AM CST #
Posted by ativan on March 08, 2007 at 10:50 AM CST #
Posted by order vicodin on March 08, 2007 at 11:38 AM CST #
Posted by mosaico arredamento on March 08, 2007 at 12:59 PM CST #
Posted by giacca uomo autunno inverno 2006 2007 on March 08, 2007 at 02:13 PM CST #
Posted by watson carisoprodol on March 08, 2007 at 02:32 PM CST #
Posted by free music on March 08, 2007 at 02:42 PM CST #
Posted by occupazione italiana albania on March 08, 2007 at 03:27 PM CST #
Posted by viagra on March 08, 2007 at 03:42 PM CST #
Posted by casato anna maria on March 08, 2007 at 04:36 PM CST #
Posted by cialis on March 08, 2007 at 04:38 PM CST #
Posted by fioricet on March 08, 2007 at 05:38 PM CST #
Posted by crotto la sorgente on March 08, 2007 at 05:49 PM CST #
Posted by levitra on line on March 08, 2007 at 06:35 PM CST #
Posted by associate degree on March 08, 2007 at 07:00 PM CST #
Posted by drug meridia on March 08, 2007 at 07:35 PM CST #
Posted by ristorante 13 giugno milano on March 08, 2007 at 08:13 PM CST #
Posted by order soma online on March 08, 2007 at 08:44 PM CST #
Posted by tempio di antonino on March 08, 2007 at 09:29 PM CST