I can write that Javascript template library in 5 lines

Do you want to write your own Mustache or Underscore templating library real fast?

I’m porting an application written in 2007 using prototype and YUI into an Angular framework.  This application is used in many hospitals around the country to process medical data such as patient events, MRIs, genetic data etc.  Different groups around the country have written plugins for the framework, so the new AngularJS framework must be backwards compatible with the old plugins. The plugins communicate with “cells” on the server using XML messages.  The messages are implemented as XML templates with replacement values like <user>{{{username}}}</user> using Prototype’s Template.  Prototype doesn’t play terribly well with AngularJS (almost immediately death, actually).  Angular’s $interpolate is probably a little two heavy-handed and choked on the input anyway (I think the the double-quotes int he namespaces were giving it problems). So I needed to replace Prototype’s Template.

I couldn’t believe how easy it was to solve!

As the old saying goes, I had a problem.  I knew I could solve it with RegEx, therefore, I had two problems. Actually the RegEx problem is pretty simple – look for three open braces in a row [\{]{3,3}, followed anything that wasn’t three closed braces in a row [\}]{3,3}.  The real trick here that made it easy is a variation in Javascript’s String.replace that excepts a function (instead of a string) as the replacement value.  The function is passed the match, and the function just looks up the match in the replacement values, thus leading to the dynamic replacement effect that’s needed.

I2b2Template.prototype.evaluate = function (templateString, replacements) {
    return templateString.replace(/[\{]{3,3}[\w]*([^}]+)[\w]*[\{]{3,3}/g, function (captured) {
        var captureWithoutBraces = captured.substring(3, captured.length-3);
        if (angular.isDefined(replacements[captureWithoutBraces])) {
            return replacements[captureWithoutBraces];

Obviously it’s not too fancy as a templating system.  It doesn’t work with trees of values (user.name).  It only works with triple braces, but that can be easily changed to anything else (must replace the hard coded regex with RexEx(..).  These features were not the requirement and there’s no need to do more than necessary.  Keeping it simple.


Cute JAXB Error

Developers are taught not to be too cute, especially with error messages, it’s often not funny to users.  Maybe self-deprecation is the secret.  Chrome’s “Aw, Snap” makes a bad experience better.  Here’s a message I found while trying to create JAXB classes from a schema into a directory that I had not yet created:

cowardly refuses to write to a non-existent directory “../src/main/java”

Generating Java Getter and Setters with Regular Expressions and IntelliJ IDEA

This is mostly a story about the power of a good UI, again demonstrated by IntelliJ IDEA (Version 9, Ultimate).

I have a buddy that says, “If you have a problem, and you want to solve it with regular expressions, then you have two problems.”

I’ve done a fair bit of regular expressions in my day.  My first contracting gig way back in 1991 (I was 24) was using regex to transform specifications in Word documents into something parseable by a system that would store the data in a database that could then be searched by a desktop app.  I’ve never been an expert at regex, at least not since I left that gig, but with a cheatsheet I know what I am doing, mostly.  Still, it often takes me more time than it’s worth.  Enter IntelliJ IDEA.

I had a generated interface for what amounts to an Entity with hundreds of getters (or the interfaces of them).  I needed to create a value object from it.  Of course, I’ll eventually use code generation, but I’m need to move quickly right now, so I figured I’d take a stab at using RegEx.  I started using IDEA and was surprised to see that as I typed my RegEx, it caught many of my mistakes by showing red underlines (like Word misspelling) and gray backgrounds for warnings.  Within 5 minutes, I had it working.  That would have taken me 20 minutes easily with a decent likelihood of being a complete waste of time if I couldn’t figure out my mistakes through eyeball inspection.

Here’s the search that matches “Type getFoo();” or “boolean isFoo();” (with support for generics):

([ \t]+)([a-zA-Z.0-9<>]+)[ ]+(get|is)([^(]*)\(\);

And the replacement expression to make:
Type Foo;
Foo getFoo(return Foo;):
void setFoo(Foo Foo) {this.Foo=Foo);

\t$2 $4;\n\tpublic $2 $3$4(){return $4;}\n\tpublic void set$4($2 $4){this.$4 = $4;}


Flex Building error “unable to export SWC oem” (Flexmojos)

I’ve been working on perfecting a build environment for Flex4+Flash Builder 4+JBoss+Maven that works for IDEA & Eclipse (FB), hot deploys to JBoss, and plays well in continuous integration (Hudson).  I was following a Maven anti-pattern that’s easy to fall into – make your maven aggregator projects your parent projects.  This is a bad idea since projects that the aggregator builds may have common settings with other children, but not with all other children (swc’s vs. swf’s in particular).  So I did the right thing and created a tree of parent pom’s that my projects use as parents, leaving the aggregator as just an aggregator.

This post is just to capture the solution to an error that I came across.  One of my swc’s was generated this error during compilation:

"unable to export SWC oem"

Googling showed:

- lots of people ran into this when mixing different versions of the flex compiler.

- lots of people ran into it when dealing with resources and fonts

The fix was to specify the mergeResourceBundle option in the flexmojo's config:


Installing Postgresql for Mac for Django


That was easy, but after doing it, I realized there was an official install. I would have uninstalled it, but the promised uninstalled script n the README was non existent. 😦 (And just deleting one dir would be insufficient.) It looks like all the other postgresql installs, so I think the db is OK, but I wish I had the tools that come with the official install, I’ll figure that out later.

I then followed this to get psycopg2 installed to prep Postgresql for use with DJango

A snap.

Creating an Executable Apache Pivot App using Maven

Apache Pivot is a very interesting new RIA platform.  You can think of it as a competitor to Flex, Silverlight, and JavaFX.  Unlike all three of those languages, it uses Java and XML – two generally familiar languages, to create applications.  It’s structured similar to Flex – it has an XML file that describes an application’s layout and allows enough functionality to create simple and not so simple applications with just XML with embedded scripts.  Unlike Flex, the “code behind” language is straight up Java.  It’s deployment model is just like an applet’s, so it’s ubiquitous.  When you dig deeper, there are lots of little niceties – things that are simple and make sense.  It’s nice to work with.

This post describes how to set up a maven project to build and deploy an Apache Pivot application.  Once Pivot is in the maven central repo (it’s not quite out of incubator as of this writing), then the result of this post can be made into an archetype and contributed back.  The result of this post is is this GitHub repository commit.  (Git is very nifty – the fact that the entire set of files is available from one Git commit id is, well, wicked, as we say in Massachusetts.)

We’ll steal the code from the Pivot StockTracker tutorial, since it will be extended with a graph and used as an example of how and why to use the EventBus in UIs.  The examples will be posted on the EventBus.org blog soon.   I’ll add a blog entry here when the eventbus.org blog post is available.

Start by using the maven quickstart to create a blank Java project from an archetype:

mvn archetype:create -DgroupId=org.eventbus.tutorial -DartifactId=stocktracker
cd stocktracker
mvn install

To populate the example, we can pull the StockTracker sources to the our package in the src/main/java directory and delete the files we don’t need:

mkdir src/main/java/org/eventbus/tutorials/pivot/stocktracker
cd src/main/java/org/eventbus/tutorials/pivot/stocktracker
svn co http://svn.apache.org/repos/asf/incubator/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/stocktracker/ .
rm -rf .svn
rm App.java
rm ../../../../../../../test/java/org/eventbus/tutorial/AppTest.java

According to Maven conventions, only the java code belongs in this directory, so let’s move the other files to the resource directory so that they can easily be picked up from the jar at runtime.  A better practice may be to make image, json, and wtkx directory, or maybe make a main/wtkx directory for the latter, but let’s keep it simple.  The community may come up with better standards for Pivot development with Maven.

cd ../../../../../..
mkdir resources
mv java/org/eventbus/tutorial/*.json resources/org/eventbus/tutorials/pivot/stocktracker
mv java/org/eventbus/tutorial/*.png resources/org/eventbus/tutorials/pivot/stocktracker
mv java/org/eventbus/tutorial/*.wtkx resources/org/eventbus/tutorials/pivot/stocktracker

Change all the packages in the Java files to the new package.

find . -name *.java -print | xargs sed -i -e 's/org.apache.pivot.tutorials.stocktracker/org.eventbus.tutorials.pivot.stocktracker/g'
find . -name *.wtkx -print | xargs sed -i -e 's/org.apache.pivot.tutorials.stocktracker/org.eventbus.tutorials.pivot.stocktracker/g'

(This is why I keep such detailed blogs.  It took me while to figure out that the -e is required on Mac, but not Linux, and I’m sure I’ll forget again, but I’ll remember that I had to do this before.)


The maven default build uses Java 1.3 source, even though Java 1.5 is about to be obsoleted as of this writing, so we’ll have to tell maven to get with the new decade and use  Java 6  (I think Pivot will work for Java 5 too) by adding this before <dependencies>:


Pivot is not in the central maven repo yet, so we have to install it locally, which means building it first.  The Pivot team is rock solid, pulling and building the project is easy and reliable.  See the BUILD file in the svn trunk for details, but on a Mac using Java 6, it’s like this:

cd ~
mkdir pivot
svn co http://svn.apache.org/repos/asf/incubator/pivot/trunk/ .
export CLASSPATH=~/dev/maven-ant-tasks-2.1.0.jar:~/dev/junit/junit-4.8.1.jar:/System/Library/Frameworks/JavaVM.framework/Resources/Deploy.bundle/Contents/Home/lib/plugin.jar:/System/Library/Frameworks/JavaVM.framework/Resources/Deploy.bundle/Contents/Home/lib/javaws.jar
ant maven-install

This builds all the pivot jars using ant and installs them to your local maven repo.

Now go back to our project and add the pivot dependencies to pom.xml:


You might not need charts, but I will for the EventBus stuff coming soon.



mvn install

should lead to a successful build.


It would be real nice to generate an executable jar file that has the entire classpath in it so that launching the app is a simple java -jar command.  To do this add the following as a plugin after the compiler plugin configuration shown above:


Now run build again and launch the app:

mvn clean install
java -jar target/stocktracker-1.0-SNAPSHOT-jar-with-dependencies.jar

Pivot Stock Tracker

Pivot Stock Tracker Built with Maven

Setting up a GitHub repo, Secure Access on Mac

I installed GitHub by first upgrading my mac to Snow Leopard, then installing the free XCode tools for Snow Leopard the Snow Leopard upgrade was not necessary, but I wanted to upgrade anyway and the XCode tools were specific to the OS version.  After the XCode install, typed git in the terminal and I was in business.

Following up on the post before last, there’s nothing interesting at all in setting up security for amazon ec2 and GitHub, since we pointed to a specific key file when ssh’ing into Amazon.  Secure access to GitHub is very well documented. The MacOS makes dealing with pasphrases very easy.  Wow, I’m impressed and am certainly not going back to Windows.

I created a repo using the GitHub web pages for my account, which creates a page for the repo with full instructions on how to use it.  How convenient.