Wednesday, October 22, 2003

Reading XML Files Using XSLT

I'm learning how to represent non-English data in XML and ran into an article titled "Well-structured XML Goes Cosmopolitan" written by Ilari Aarnio and hosted by DevX.com. Buried in the article was an interesting technique that showed how to read XML documents from within XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
    <xsl:param name="currLang">en</xsl:param>
    <xsl:output method="xml" encoding="UTF-8" indent="yes" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"/>

    <xsl:template match="/">
        <xsl:variable name="cvData">
            <xsl:text>cv-</xsl:text>
            <xsl:value-of select="$currLang"/>
            <xsl:text>.xml</xsl:text>
    </xsl:variable>
    <xsl:apply-templates select="document($cvData)"/>
    </xsl:template>

    <!-- add normal XSLT processing (ie, more template tags) here -->

</xsl:stylesheet>

Tuesday, October 14, 2003

Using synchronized in Java

For quite awhile, I've been using the sychnronized keyword in an incorrect manner, probably because of my ColdFusion background. Synchronization locks in ColdFusion lock access to a block of code based on some arbitrary lock phrase. As long as two locks have the same lock phrase, they single-thread the process through the protected code. The concept that I recently relearned was that Java sychronizes on an object - typicallly 'this'. Which has some interesting ramifications when one object starts two threads which both refer to static objects. The parent can have every method sychronized without affected the child threads. The above issue is relative mundane, but my blindness to the true nature of Java synchronization shows how important it is to constantly reexam even your most basic understandings about the tools that you use everyday.

Friday, October 03, 2003

Creating an Ant Task with Eclipse

  1. Add ant.jar to your eclipse project.
  2. Create package to hold task classes. I recommend xxx.yyy.ant.tasks
  3. Create task class. Here is a simple one:
    package com.affy.ant.tasks;
    import org.apache.tools.ant.*;
    public class HelloWorld extends Task {
        public void execute() throws BuildException {
            System.out.println("Hello World!");
        }
    }
    
  4. Select Window, Preferences, Ant, Classpath
  5. Set ANT_HOME
  6. Add the folder where your class files are stored (xxx/build/classes, for example)
  7. Select Task, Add Task
  8. Assign a task name (ie, HelloWorld) to use in build.xml
  9. Select location (the folder xxx/build/classes for example)

That's how easy it is to start writing your own Ant tasks. If you're going to invoke build.xml from within Eclipse, then you only need the following lines:

    <target name="HelloWorld">
        <HelloWorld/>
    </target>

Otherwise, you need to tell Ant where to find the class file. For example,

    <path id="cp">
        <pathelement path="xxx/build/classes"/>
    </path>
    <target name="HelloWorld">
        <taskdef name="HelloWorld" classname="org.wwre.ant.tasks.HelloWorld" classpathref="cp"/>
        <HelloWorld/>
    </target>