Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Jato: The new kid on the open source block, Part 2

Look in-depth at Java-to-XML translation

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Page 2 of 5

<Jato:macro> and <Jato:debug> statements

Jato macros, a collection of Jato and regular XML tags, provide a powerful facility to reduce script line count and enable recursive algorithms. In Jato, you invoke macros using the <Jato:inline> tag and declare them in a <Jato:macros> section using <Jato:macro> tags as shown in Listing 1:

Listing 1: Jato script demonstrating macros and debug

1. <Jato:defs xmlns:Jato='http://jato.sourceforge.net'>
2.    <Jato:inline macro='printMsg'/> 
3.
4.    <Jato:macros>
5.       <Jato:macro name='printMsg'>
6.          <Jato:debug>Invoked macro</Jato:debug> 
7.       </Jato:macro>
8.    </Jato:macros>
9. </Jato:defs>


The script in Listing 1 performs the following:

  • Line 2: Invokes the 'printMsg' macro, the only top-level line of script. Think of it as being the same as a one-line main() method that invokes another method.
  • Line 4: The <Jato:macros> section contains all the macro definitions as delineated by the <Jato:macro> tags.
  • Line 5: Declares the 'printMsg' macro.
  • Line 6: The macro consists of a single line that prints the message "Invoked macro." Later examples will demonstrate other capabilities of the <Jato:debug> tag.


This script does not generate an XML document, rather it simply prints the message "Jato Debug: Invoked macro" to the standard output stream. The script can be easily run without writing any Java code by using the org.jato.JavaToXml script runner:

> java org.jato.JavaToXml -f macro-demo.xml -nop
JATO Debug: Invoked macro


In the code above, the -f flag instructs the script runner to use the Jato script called macro-demo.xml instead of the default script name, java-to-xml.xml. The -nop option instructs the interpreter to suppress printing the generated XML document.

Let's look at a few notes about invoking macros:

  • Macros may be invoked within other macros
  • A macro can be recursively invoked
  • A macro can contain just about any Jato script or XML tags (which must be well-formed within the macro definition)
  • Macros can be invoked from practically anyplace in a Jato script


Establish a script structure

Using Jato macros, we can develop the structure for our Jato script that will generate an XML document describing the contents of a filesystem. Before developing the Jato script, let's examine a Java program that iterates over the contents of a java.io.File. The program will provide several useful insights for developing the structure of our Jato script.

The process of traversing a hierarchical file structure is easily expressed using a recursive algorithm. The ListDir class implements such a recursive algorithm in Listing 2. Notice the recursive call in ls() at line 17 when the File being inspected is a directory:

Listing 2: Recursive directory traversal in Java

1.  public class ListDir {
2.     public static void main(String args[]) {
3.        //get the root node for iterating
4.        File root = new File(System.getProperty("user.dir"));
5.
6.        //iterate contents of directory
7.        ls(root); 
8.     }
9.
10.    static void ls(File f) { 
11.       System.out.println("Traversing directory: " + f);
12.       File list[] = f.listFiles();
13.
14.       for (int i=0; i<list.length; i++) {
15.          if (list[i].isDirectory()) {
16.             //recurse file
17.             ls(list[i]);
18.          } else {
19.             System.out.println("File: " + list[i]);
20.          }
21.       }
22.    }
23. }


The code in Listing 3 implements a Jato script that performs the same recursive directory iteration except it will output <dir> and <file> tags describing directories and files, respectively:

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources