<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Andrew E. Bruno &#187; Java</title>
	<atom:link href="http://left.subtree.org/category/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://left.subtree.org</link>
	<description>A sourceful of secrets</description>
	<lastBuildDate>Mon, 10 May 2010 03:56:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='left.subtree.org' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/e14c799c6e8030a8abefcb495c0b0e17?s=96&#038;d=http://s2.wp.com/i/buttonw-com.png</url>
		<title>Andrew E. Bruno &#187; Java</title>
		<link>http://left.subtree.org</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://left.subtree.org/osd.xml" title="Andrew E. Bruno" />
	<atom:link rel='hub' href='http://left.subtree.org/?pushpress=hub'/>
		<item>
		<title>Creating executable jars with Maven</title>
		<link>http://left.subtree.org/2008/01/24/creating-executable-jars-with-maven/</link>
		<comments>http://left.subtree.org/2008/01/24/creating-executable-jars-with-maven/#comments</comments>
		<pubDate>Fri, 25 Jan 2008 04:53:16 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://left.subtree.org/2008/01/24/creating-executable-jars-with-maven/</guid>
		<description><![CDATA[After wrestling with Maven assemblies for while I finally figured out how to build executable jars. The Maven assembly plugin allows you to define ways to package up your project for distribution by creating various assembly descriptor files. Here&#8217;s a quick example of a Maven assembly for building an executable jar (uberjar). For this example [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=left.subtree.org&amp;blog=13566420&amp;post=18&amp;subd=qnot&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>After wrestling with <a href="http://maven.apache.org/plugins/maven-assembly-plugin/">Maven assemblies</a> for while I finally figured out how to build executable jars. The Maven assembly plugin allows you to define ways to package up your project for distribution by creating various assembly descriptor files. Here&#8217;s a quick example of a Maven assembly for building an executable jar (uberjar). For this example we&#8217;ll create a brand new project from scratch but it should be easy to see how to integrate into an existing project.</p>
<p>First step lets create a test project:</p>
<pre class="brush: plain;">
$ mvn archetype:create -DgroupId=org.qnot.example -DartifactId=hello-world
$ cd hello-world
</pre>
<p>Next add a few dependencies to the project. In this example we&#8217;ll add a few libraries from jakarta commons. The &lt;dependencies/&gt; section in the pom.xml should now look like this:</p>
<pre class="brush: xml;">
  &lt;dependencies&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;junit&lt;/groupId&gt;
      &lt;artifactId&gt;junit&lt;/artifactId&gt;
      &lt;version&gt;3.8.1&lt;/version&gt;
      &lt;scope&gt;test&lt;/scope&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;commons-cli&lt;/groupId&gt;
      &lt;artifactId&gt;commons-cli&lt;/artifactId&gt;
      &lt;version&gt;1.1&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;commons-lang&lt;/groupId&gt;
      &lt;artifactId&gt;commons-lang&lt;/artifactId&gt;
      &lt;version&gt;2.3&lt;/version&gt;
    &lt;/dependency&gt;
  &lt;/dependencies&gt;
</pre>
<p>Create a META-INF/ directory to store the MANIFEST.MF file which defines the main class in the executable jar.</p>
<pre class="brush: plain;">
$ mkdir -p src/main/resources/META-INF/
$ echo 'Main-Class: org.qnot.example.App' &gt; MANIFEST.MF
</pre>
<p>Create a src/assemble directory to store the assembly descriptor files</p>
<pre class="brush: plain;">
$ mkdir src/assemble
</pre>
<p>Next we&#8217;ll create the actual assembly descriptor file which defines how to package up the jar. Create the file src/assemble/exe.xml with the following xml:</p>
<pre class="brush: xml;">
&lt;assembly&gt;
  &lt;id&gt;exe&lt;/id&gt;
  &lt;formats&gt;
    &lt;format&gt;jar&lt;/format&gt;
  &lt;/formats&gt;
  &lt;includeBaseDirectory&gt;false&lt;/includeBaseDirectory&gt;
  &lt;dependencySets&gt;
    &lt;dependencySet&gt;
      &lt;outputDirectory&gt;&lt;/outputDirectory&gt;
      &lt;outputFileNameMapping&gt;&lt;/outputFileNameMapping&gt;
      &lt;unpack&gt;true&lt;/unpack&gt;
      &lt;scope&gt;runtime&lt;/scope&gt;
      &lt;includes&gt;
        &lt;include&gt;commons-lang:commons-lang&lt;/include&gt;
        &lt;include&gt;commons-cli:commons-cli&lt;/include&gt;
      &lt;/includes&gt;
    &lt;/dependencySet&gt;
  &lt;/dependencySets&gt;
  &lt;fileSets&gt;
    &lt;fileSet&gt;
      &lt;directory&gt;target/classes&lt;/directory&gt;
      &lt;outputDirectory&gt;&lt;/outputDirectory&gt;
    &lt;/fileSet&gt;
  &lt;/fileSets&gt;
&lt;/assembly&gt;
</pre>
<p>Inside the <code>&lt;dependecySets/&gt;</code> is where you can add all the libraries you&#8217;d like to include in the uberjar. These must also be defined in your pom.</p>
<p>Finally, add the maven-assembly-plugin to the pom:</p>
<pre class="brush: xml;">
  &lt;build&gt;
    &lt;finalName&gt;hello-world&lt;/finalName&gt;
    &lt;plugins&gt;
      &lt;plugin&gt;
        &lt;artifactId&gt;maven-assembly-plugin&lt;/artifactId&gt;
        &lt;configuration&gt;
          &lt;descriptors&gt;
            &lt;descriptor&gt;src/assemble/exe.xml&lt;/descriptor&gt;
          &lt;/descriptors&gt;
          &lt;archive&gt;
            &lt;manifestFile&gt;src/main/resources/META-INF/MANIFEST.MF&lt;/manifestFile&gt;
          &lt;/archive&gt;
        &lt;/configuration&gt;
      &lt;/plugin&gt;
    &lt;/plugins&gt;
  &lt;/build&gt;
</pre>
<p>To run the assembly and build the executable jar:</p>
<pre class="brush: plain;">
$ mvn assembly:assembly
$ java -jar target/hello-world-exe.jar
Hello World!
</pre>
<p>I tested the hello-world example using the latest Maven release (2.0.8) and maven-assembly-plugin-2.2-beta-1. If you run into any issues try and update your Maven plugins by running:</p>
<pre class="brush: plain;">
$ mvn -U compile
</pre>
<p>You can download the example hello-world project <a href="http://www.qnot.org/code/hello-world.tar.gz">here</a>.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/qnot.wordpress.com/18/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/qnot.wordpress.com/18/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/qnot.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/qnot.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/qnot.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/qnot.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/qnot.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/qnot.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/qnot.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/qnot.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/qnot.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/qnot.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/qnot.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/qnot.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/qnot.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/qnot.wordpress.com/18/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=left.subtree.org&amp;blog=13566420&amp;post=18&amp;subd=qnot&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://left.subtree.org/2008/01/24/creating-executable-jars-with-maven/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">sigma110</media:title>
		</media:content>
	</item>
		<item>
		<title>Rotate Labels JFreeChart</title>
		<link>http://left.subtree.org/2007/08/14/rotate-labels-jfreechart/</link>
		<comments>http://left.subtree.org/2007/08/14/rotate-labels-jfreechart/#comments</comments>
		<pubDate>Wed, 15 Aug 2007 03:06:55 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://left.subtree.org/2007/08/14/rotate-labels-jfreechart/</guid>
		<description><![CDATA[When creating a chart that has rather long labels for the x-axis it is sometimes desirable to rotate them a bit so they fit on the plot. The method to use is setCategoryLabelPositions(..) on the CategoryAxis class. Here&#8217;s a quick example: And the code.. import java.io.File; import java.io.IOException; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartUtilities; import org.jfree.chart.ChartColor; import [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=left.subtree.org&amp;blog=13566420&amp;post=17&amp;subd=qnot&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>When creating a chart that has rather long labels for the x-axis it is sometimes desirable to rotate them a bit so they fit on the plot. The method to use is <code>setCategoryLabelPositions(..)</code> on the <code>CategoryAxis</code> class. Here&#8217;s a quick example:</p>
<p><img src="http://qnot.files.wordpress.com/2007/08/rotate_labels.png?w=400&#038;h=300" alt="" title="rotate_labels" width="400" height="300" class="aligncenter size-full wp-image-172" /></p>
<p>And the code..</p>
<pre class="brush: java; highlight: [38];">
import java.io.File;
import java.io.IOException;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.ChartColor;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.data.category.DefaultCategoryDataset;

public class RotateLabels {
    public static void main(String[] args) {
        DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
        dataSet.addValue(51, &quot;series&quot;, &quot;Colonel Forbin&quot;);
        dataSet.addValue(92, &quot;series&quot;, &quot;The Lizards&quot;);
        dataSet.addValue(33, &quot;series&quot;, &quot;Wilson&quot;);
        dataSet.addValue(77, &quot;series&quot;, &quot;Rutherford the Brave&quot;);
        dataSet.addValue(37, &quot;series&quot;, &quot;The Unit Monster&quot;);
        dataSet.addValue(97, &quot;series&quot;, &quot;The Famous Mockingbird&quot;);
        dataSet.addValue(67, &quot;series&quot;, &quot;Poster Nutbag&quot;);

        JFreeChart chart = ChartFactory.createBarChart(
            &quot;Gamehendge&quot;,
            null,
            null,
            dataSet,
            PlotOrientation.VERTICAL,
            false,
            false,
            false
        );

        CategoryPlot plot = (CategoryPlot)chart.getPlot();
        CategoryAxis xAxis = (CategoryAxis)plot.getDomainAxis();
        xAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);

        chart.setBackgroundPaint(ChartColor.WHITE);
        try {
            ChartUtilities.saveChartAsPNG(new File(&quot;chart.png&quot;), chart, 400, 300);
        } catch(IOException e) {
            e.printStackTrace();
        }
    }
}
</pre>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/qnot.wordpress.com/17/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/qnot.wordpress.com/17/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/qnot.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/qnot.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/qnot.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/qnot.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/qnot.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/qnot.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/qnot.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/qnot.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/qnot.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/qnot.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/qnot.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/qnot.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/qnot.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/qnot.wordpress.com/17/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=left.subtree.org&amp;blog=13566420&amp;post=17&amp;subd=qnot&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://left.subtree.org/2007/08/14/rotate-labels-jfreechart/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">sigma110</media:title>
		</media:content>

		<media:content url="http://qnot.files.wordpress.com/2007/08/rotate_labels.png" medium="image">
			<media:title type="html">rotate_labels</media:title>
		</media:content>
	</item>
		<item>
		<title>MySQL bigint types and iBATIS</title>
		<link>http://left.subtree.org/2007/07/16/mysql-bigint-types-and-ibatis/</link>
		<comments>http://left.subtree.org/2007/07/16/mysql-bigint-types-and-ibatis/#comments</comments>
		<pubDate>Mon, 16 Jul 2007 21:22:37 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://left.subtree.org/2007/07/16/mysql-bigint-types-and-ibatis/</guid>
		<description><![CDATA[One nuance I recently ran into while using iBATIS was inserting data into MySQL bigint unsigned columns. iBATIS doesn&#8217;t seem to have a way to handle BigInteger data types and throws an exception when attempting to do an insert. Fetching data out seemed to work OK because if iBATIS doesn&#8217;t know how to handle a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=left.subtree.org&amp;blog=13566420&amp;post=15&amp;subd=qnot&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>One nuance I recently ran into while using <a href="http://ibatis.apache.org/">iBATIS</a> was inserting data into <a href="http://www.mysql.com/">MySQL</a> bigint unsigned columns. iBATIS doesn&#8217;t seem to have a way to handle BigInteger data types and throws an exception when attempting to do an insert. Fetching data out seemed to work OK because if iBATIS doesn&#8217;t know how to handle a certain type it just returns a java.lang.Object. The way to go about inserting BigInteger types is to set up a type handler. Here&#8217;s an example type handler for BigInteger types:</p>
<pre class="brush: java;">
package org.qnot.util;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.SQLException;
import java.sql.Types;

import com.ibatis.sqlmap.client.extensions.ParameterSetter;
import com.ibatis.sqlmap.client.extensions.ResultGetter;
import com.ibatis.sqlmap.client.extensions.TypeHandlerCallback;

public class BigIntegerTypeHandler implements TypeHandlerCallback {

    public Object getResult(ResultGetter getter) throws SQLException {
        if(getter.wasNull()) {
            return null;
        }

        Object o = getter.getObject();
        if(o instanceof BigDecimal) {
            BigDecimal bd = (BigDecimal)o;
            return bd.toBigInteger();
        } else if(o instanceof BigInteger) {
            return (BigInteger)o;
        } else {
            return o;
        }
    }

    public void setParameter(ParameterSetter setter, Object parameter)
            throws SQLException {
        if (parameter == null) {
            setter.setNull(Types.BIGINT);
        } else {
            BigInteger i = (BigInteger) parameter;
            setter.setBigDecimal(new BigDecimal(i));
        }
    }

    public Object valueOf(String s) {
        return s;
    }
}
</pre>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/qnot.wordpress.com/15/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/qnot.wordpress.com/15/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/qnot.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/qnot.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/qnot.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/qnot.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/qnot.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/qnot.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/qnot.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/qnot.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/qnot.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/qnot.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/qnot.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/qnot.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/qnot.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/qnot.wordpress.com/15/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=left.subtree.org&amp;blog=13566420&amp;post=15&amp;subd=qnot&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://left.subtree.org/2007/07/16/mysql-bigint-types-and-ibatis/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">sigma110</media:title>
		</media:content>
	</item>
		<item>
		<title>Converting MIF to XML &#8211; Java Version</title>
		<link>http://left.subtree.org/2007/01/31/converting-mif-to-xml-java-version/</link>
		<comments>http://left.subtree.org/2007/01/31/converting-mif-to-xml-java-version/#comments</comments>
		<pubDate>Thu, 01 Feb 2007 03:57:01 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://left.subtree.org/2007/01/31/converting-mif-to-xml-java-version/</guid>
		<description><![CDATA[In my previous post I discussed a tool called mif2xml for converting MIF files to an intermediate XML dialect. In this post I&#8217;ll talk about the Java port of mif2xml called mif2xml-j which you can download here including just the executable jar or browse the source online via svn. JFlex is a lexical analyzer generator [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=left.subtree.org&amp;blog=13566420&amp;post=7&amp;subd=qnot&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://left.subtree.org/2007/01/25/converting-mif-to-xml/">previous post</a> I discussed a tool called <code>mif2xml</code> for converting MIF files to an intermediate XML dialect. In this post I&#8217;ll talk about the Java port of <code>mif2xml</code> called <code>mif2xml-j</code> which you can download <a href="http://code.qnot.org/svn/projects/mif2xml-j/releases/">here</a> including just the <a href="http://code.qnot.org/svn/projects/mif2xml-j/releases/mif2xml-0.2.jar">executable jar</a> or browse the <a href="http://code.qnot.org/svn/projects/mif2xml-j/trunk/">source online</a> via svn.</p>
<p><a href="http://www.jflex.de/">JFlex</a> is a lexical analyzer generator for Java and is the library I chose to use for creating the MIF lexer. The first step was to get JFlex integrated into my build environment. For this project I decided to use <a href="http://ant.apache.org/">ant</a> but integrating JFlex into another build environment <span id="more-7"></span>should be straightforward. I created the following directory structure:</p>
<pre class="brush: plain; light: true;">
--/
  |-- src/main/jflex/               - JFlex lexical specifications
  |-- src/main/resources/MANIFEST   - Defines main class for executable jar
  |-- src/main/java/                - Java source
  |-- lib/                          - 3rd party libraries (JFlex.jar)
  |-- build.xml                     - Ant build file
</pre>
<p>JFlex comes bundled with a <code>JFlexAntTask</code> which provides a very convenient <code>&lt;jflex/&gt;</code> task. Here&#8217;s a snippet of the ant build file I created which shows how to set it up:</p>
<pre class="brush: xml;">
&lt;property name=&quot;src&quot;   location=&quot;${basedir}/src/main/java&quot; /&gt;
&lt;property name=&quot;lib&quot; location=&quot;${basedir}/lib&quot; /&gt;
&lt;property name=&quot;scanner-file&quot; value=&quot;${basedir}/src/main/jflex/mif.jflex&quot; /&gt;

&lt;path id=&quot;classpath&quot;&gt;
    &lt;pathelement location=&quot;${build}&quot; /&gt;
    &lt;fileset dir=&quot;${lib}&quot;&gt;
        &lt;include name=&quot;*.jar&quot; /&gt;
    &lt;/fileset&gt;
&lt;/path&gt;

&lt;taskdef classpathref=&quot;classpath&quot; classname=&quot;JFlex.anttask.JFlexTask&quot; name=&quot;jflex&quot; /&gt;

&lt;target name=&quot;jflex&quot; description=&quot;Generate the MIF lexer&quot;&gt;
    &lt;echo message=&quot;Generating the MIF Lexer&quot; /&gt;
    &lt;jflex file=&quot;${scanner-file}&quot; destdir=&quot;${src}&quot; /&gt;
&lt;/target&gt;
</pre>
<p>I found writing the lexical specification in JFlex and flex to be very similar. JFlex has a great <a href="http://www.jflex.de/manual.html">user manual</a> which contains a lot of useful info. Here&#8217;s the <code>mif.jflex</code> file:</p>
<pre class="brush: cpp;">
/*
 * Copyright 2007 Andrew Bruno &lt;aeb@qnot.org&gt;
 * Licensed under the Apache License, Version 2.0
 */

package org.qnot.mif2xml;
import java.util.Stack;

%%

%{
  private Stack&lt;Tag&gt; tags = new Stack&lt;Tag&gt;();
  private StringBuffer data = new StringBuffer();
  private StringBuffer facet = new StringBuffer();
%}

%line
%char
%standalone
%class  MifLexer
%xstate DATA
%xstate STR
%xstate FACET

ID=[A-Za-z][A-Za-z0-9]*
TAG=&quot;&lt;&quot;{ID}&quot; &quot;
TAG_END=&quot;&gt;&quot;
NONNEWLINE=[^\r|\n|\r\n]
NEWLINE=[\r|\n|\r\n]
WHITE_SPACE_CHAR=[ \n\t]

%%

&lt;YYINITIAL&gt; {
   {TAG}   {
        Tag tag = new Tag();
        tag.setName(yytext().substring(1, yytext().length()-1));
        tags.push(tag);
        tag.writeStart();
        data = new StringBuffer();
        yybegin(DATA);
    }

    {TAG_END}   {
        if(!tags.empty()) {
            Tag tag = (Tag)tags.pop();
            tag.writeEnd();
        }
    }

    ^&quot;=&quot;[a-zA-Z][a-zA-Z0-9]*{NEWLINE} {
        facet = new StringBuffer();
        facet.append(yytext());
        yybegin(FACET);
    }

    {WHITE_SPACE_CHAR}+   {  /* eat up whitespace */ }
    {NONNEWLINE}          {  /* eat up everything else  */ }
}

&lt;DATA&gt; {
    {NEWLINE}  {
        if(!tags.empty()) {
            Tag tag = (Tag)tags.pop();
            tag.setValue(data.toString());
            tags.push(tag);
        }
        yybegin(YYINITIAL);
    }
    &quot;`&quot;  {  yybegin(STR); }
    {TAG_END}  {
        if(!tags.empty()) {
            Tag tag = (Tag)tags.pop();
            String value = tag.getValue();

            String dataStr = data.toString();
            if(dataStr != null &amp;&amp; dataStr.length() &gt; 0) {
                value = dataStr;
            }

            if(value != null) {
                value = value.replaceAll(&quot;^\\s+&quot;, &quot;&quot;);
                value = value.replaceAll(&quot;\\s+$&quot;, &quot;&quot;);
            }

            tag.setValue(value);
            tag.writeEnd();
        }
        yybegin(YYINITIAL);
    }
    [^\n|\r|\r\n|`|&gt;] {
        data.append(yytext());
    }
}

&lt;STR&gt; {
    &quot;'&quot;  {
        if(!tags.empty()) {
            Tag tag = (Tag)tags.pop();
            if(tag.getValue() == null || tag.getValue().length() == 0) {
                tag.setValue(&quot;`'&quot;);
            }
            tags.push(tag);
        }
        yybegin(YYINITIAL);
    }
    [^']*  {
        if(!tags.empty()) {
            Tag tag = (Tag)tags.pop();
            StringBuffer buf = new StringBuffer();
            buf.append(&quot;`&quot;);
            buf.append(yytext());
            buf.append(&quot;'&quot;);
            tag.setValue(buf.toString());
            tags.push(tag);
        }
    }
}

&lt;FACET&gt; {
    ^&quot;=EndInset&quot;{NEWLINE} {
        facet.append(yytext());
        Tag.writeFacet(facet.toString());
        yybegin(YYINITIAL);
    }

    .*{NEWLINE} {
        facet.append(yytext());
    }
}
</pre>
<p>I created a simple <code>Tag</code> class to encapsulate a MIF XML tag and handle writing out each tag. The <code>MifLexer</code> keeps a stack of <code>Tag</code> instances while it&#8217;s processing the input file:</p>
<pre class="brush: java;">
/*
 * Copyright 2007 Andrew Bruno &lt;aeb@qnot.org&gt;
 * Licensed under the Apache License, Version 2.0
 */

package org.qnot.mif2xml;

public class Tag {
    private String name;
    private String value;

    public String getName() {
        return this.name;
    }

    public String getValue() {
        return this.value;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public void writeEnd() {
        if(value != null &amp;&amp; value.length() &gt; 0) {
            System.out.print(escape(value) + &quot;&lt;/&quot; + name + &quot;&gt;&quot;);
        } else {
            System.out.print(&quot;&lt;/&quot; + name + &quot;&gt;&quot;);
        }
    }

    public void writeStart() {
        System.out.print(&quot;&lt;&quot; + name + &quot;&gt;&quot; );
    }

    public static void writeFacet(String facet) {
        System.out.print(&quot;&lt;_facet&gt;&lt;![CDATA[&quot;);
        System.out.print(facet);
        System.out.print(&quot;]]&gt;&lt;/_facet&gt;&quot;);
    }

    private String escape(String str) {
        str = str.replaceAll(&quot;&amp;&quot;, &quot;&amp;amp;&quot;);
        str = str.replaceAll(&quot;\&quot;&quot;, &quot;&amp;quot;&quot;);
        str = str.replaceAll(&quot;&gt;&quot;, &quot;&amp;gt;&quot;);
        str = str.replaceAll(&quot;&lt;&quot;, &quot;&amp;lt;&quot;);
        str = str.replaceAll(&quot;^\\s+&quot;, &quot;&quot;);
        str = str.replaceAll(&quot;\\s+$&quot;, &quot;&quot;);

        return str;
    }
}
</pre>
<p>There&#8217;s a separate <code>Main</code> class which creates a new instance of the <code>MifLexer</code> class for processing the file passed in on the command line. I&#8217;d like to eventually extend this class so that it handles command line options and possibly even runs some XSLT&#8217;s over the generated MIF XML.</p>
<pre class="brush: java;">
/*
 * Copyright 2007 Andrew Bruno &lt;aeb@qnot.org&gt;
 * Licensed under the Apache License, Version 2.0
 */

package org.qnot.mif2xml;

import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.FileReader;

public class Main {
    public static void main(String[] args) {
        if(args.length != 1) {
            System.err.println(&quot;Usage : mif2xml &lt;inputfile&gt;&quot;);
            System.exit(1);
        }

        try {
            MifLexer scanner = new MifLexer(new FileReader(args[0]));
            System.out.print(&quot;&lt;?xml version=\&quot;1.0\&quot;?&gt;&lt;mif&gt;&quot;);
            scanner.yylex();
            System.out.print(&quot;&lt;/mif&gt;&quot;);
        } catch(FileNotFoundException e) {
            System.out.println(&quot;File not found : &quot;+args[0]);
        } catch(IOException e) {
            System.out.println(&quot;I/O error scanning file '&quot;+args[0]+&quot;': &quot;+e.getMessage());
        } catch(Exception e) {
            System.out.println(&quot;Unexpected exception: &quot; + e.getMessage());
            e.printStackTrace();
        }
    }
}
</pre>
<p>To run the code download the <a href="http://code.qnot.org/svn/projects/mif2xml-j/releases/mif2xml-0.1.jar">executable jar</a> and run</p>
<pre class="brush: plain; light: true;">
$ java -jar mif2xml-0.1.jar myfile.mif
</pre>
<p>The MIF XML will be printed to stdout.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/qnot.wordpress.com/7/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/qnot.wordpress.com/7/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/qnot.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/qnot.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/qnot.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/qnot.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/qnot.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/qnot.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/qnot.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/qnot.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/qnot.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/qnot.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/qnot.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/qnot.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/qnot.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/qnot.wordpress.com/7/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=left.subtree.org&amp;blog=13566420&amp;post=7&amp;subd=qnot&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://left.subtree.org/2007/01/31/converting-mif-to-xml-java-version/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">sigma110</media:title>
		</media:content>
	</item>
		<item>
		<title>Creating Sparklines with JFreeChart</title>
		<link>http://left.subtree.org/2007/01/15/creating-sparklines-with-jfreechart/</link>
		<comments>http://left.subtree.org/2007/01/15/creating-sparklines-with-jfreechart/#comments</comments>
		<pubDate>Mon, 15 Jan 2007 08:05:01 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://left.subtree.org/2007/01/14/creating-sparklines-with-jfreechart/</guid>
		<description><![CDATA[Sparklines are very small charts usually displayed along side some text and help quickly compare time series data. They are usually rendered without any axis, labels, or tick marks and appear as just a simple line. Sparklines were developed by Edward Tufte and further explained here. JFreeChart does not have any built in classes for [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=left.subtree.org&amp;blog=13566420&amp;post=4&amp;subd=qnot&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://en.wikipedia.org/wiki/Sparkline">Sparklines</a> are very small charts usually displayed along side some text and help quickly compare time series data. They are usually rendered without any axis, labels, or tick marks and appear as just a simple line. Sparklines were developed by <a href="http://www.edwardtufte.com/">Edward Tufte</a> and further explained <a href="http://www.edwardtufte.com/bboard/q-and-a-fetch-msg?msg_id=0001OR&amp;topic_id=1">here</a>.</p>
<p><a href="http://www.jfree.org/jfreechart/">JFreeChart</a> does not have any built in classes for creating sparklines but are easily created by adjusting a few settings in the basic charting classes. Here&#8217;s a few quick examples of some sparklines generated using JFreeChart:</p>
<table style="border:none;">
<tr>
<td style="border:none;"><span style="font-size:medium;">Foo 90</span></td>
<td style="border:none;"><img src="http://qnot.files.wordpress.com/2007/01/sparkline.png?w=100&#038;h=30" alt="" title="sparkline" width="100" height="30" class="alignnone size-full wp-image-195" /></td>
</tr>
<tr>
<td style="border:none;"><span style="font-size:medium;">Bar 34</span></td>
<td style="border:none;"><img src="http://qnot.files.wordpress.com/2007/01/sparkline1.png?w=100&#038;h=30" alt="" title="sparkline1" width="100" height="30" class="alignnone size-full wp-image-196" /></td>
</tr>
<tr>
<td style="border:none;"><span style="font-size:medium;">Baz 54</span></td>
<td style="border:none;"><img src="http://qnot.files.wordpress.com/2007/01/sparkline2.png?w=100&#038;h=30" alt="" title="sparkline2" width="100" height="30" class="alignnone size-full wp-image-197" /></td>
</tr>
</table>
<p>To create sparklines using JFreeChart you just need to turn off the display of labels, tickmarks, lines, etc. on the domain/range axis as well as the XYPlot.</p>
<p>Here&#8217;s a complete example:</p>
<pre class="brush: java; highlight: [43,44,45,46,47,48,51,52,53,54,55,56,62,63,64,65]; wrap-lines: false;">
import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.DateTickUnit;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.ui.RectangleInsets;

public class Sparkline {
    public static void main(String[] args) {
        TimeSeriesCollection dataSet = new TimeSeriesCollection();
        Day day = new Day();
        TimeSeries data = new TimeSeries(&quot;Sparkline&quot;, day.getClass());

        // XXX add real data here
        Random r = new Random();
        Calendar c = Calendar.getInstance();
        for(int i = 0; i &lt; 100; i++) {
            int val = r.nextInt(100);
            if(val &lt; 50)
                val += 50;
            c.add(Calendar.DATE, 7);
            Date date = c.getTime();
            data.add(new Day(date), val);
        }

        dataSet.addSeries(data);

        // The sparkline is created by setting a bunch of the visible properties
        // on the domain, range axis and the XYPlot to false
        DateAxis x = new DateAxis();
        x.setTickUnit(new DateTickUnit(DateTickUnit.MONTH, 1));
        x.setTickLabelsVisible(false);
        x.setTickMarksVisible(false);
        x.setAxisLineVisible(false);
        x.setNegativeArrowVisible(false);
        x.setPositiveArrowVisible(false);
        x.setVisible(false);

        NumberAxis y = new NumberAxis();
        y.setTickLabelsVisible(false);
        y.setTickMarksVisible(false);
        y.setAxisLineVisible(false);
        y.setNegativeArrowVisible(false);
        y.setPositiveArrowVisible(false);
        y.setVisible(false);

        XYPlot plot = new XYPlot();
        plot.setInsets(new RectangleInsets(-1, -1, 0, 0));
        plot.setDataset(dataSet);
        plot.setDomainAxis(x);
        plot.setDomainGridlinesVisible(false);
        plot.setDomainCrosshairVisible(false);
        plot.setRangeGridlinesVisible(false);
        plot.setRangeCrosshairVisible(false);
        plot.setRangeAxis(y);
        plot.setRenderer(new StandardXYItemRenderer(
                StandardXYItemRenderer.LINES));

        JFreeChart chart = new JFreeChart(null, JFreeChart.DEFAULT_TITLE_FONT,
                plot, false);
        chart.setBorderVisible(false);

        try {
            ChartUtilities.saveChartAsPNG(new File(&quot;sparkline.png&quot;), chart,
                    100, 30);
        } catch(IOException e) {
            System.err.println(&quot;Failed to render chart as png: &quot;
                    + e.getMessage());
            e.printStackTrace();
        }
    }
}
</pre>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/qnot.wordpress.com/4/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/qnot.wordpress.com/4/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/qnot.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/qnot.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/qnot.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/qnot.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/qnot.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/qnot.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/qnot.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/qnot.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/qnot.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/qnot.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/qnot.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/qnot.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/qnot.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/qnot.wordpress.com/4/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=left.subtree.org&amp;blog=13566420&amp;post=4&amp;subd=qnot&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://left.subtree.org/2007/01/15/creating-sparklines-with-jfreechart/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">sigma110</media:title>
		</media:content>

		<media:content url="http://qnot.files.wordpress.com/2007/01/sparkline.png" medium="image">
			<media:title type="html">sparkline</media:title>
		</media:content>

		<media:content url="http://qnot.files.wordpress.com/2007/01/sparkline1.png" medium="image">
			<media:title type="html">sparkline1</media:title>
		</media:content>

		<media:content url="http://qnot.files.wordpress.com/2007/01/sparkline2.png" medium="image">
			<media:title type="html">sparkline2</media:title>
		</media:content>
	</item>
	</channel>
</rss>