Programming Tutorials Browser Tutorials Articles Struts Tutorials Hibernate Tutorials

  Tutorial: Jato: The new kid on the open source block, Part 2 - JavaWorld April 2001

Jato: The new kid on the open source block, Part 2 - JavaWorld April 2001

Tutorial Details:

Jato: The new kid on the open source block, Part 2
Jato: The new kid on the open source block, Part 2
By: By Andy Krumel
Look in-depth at Java-to-XML translation
ato, a new open source Java/XML translator, provides an effective solution to interfacing Java applications and XML documents. In Part 1 of this series, I provided a quick overview of using Jato to transform from Java-to-XML and XML-to-Java. In Part 1 I also discussed Jato's key features, and its important classes and interfaces.
Read the whole "Jato: The New Kid on the Open Source Block" series:
Part 1: A new library for converting between Java and XML
Part 2: Look in-depth at Java-to-XML translation
Part 3: Translate XML documents into Java objects
This article, the second in a series of three, moves from the general to the specific by examining Java-to-XML transformations in detail. In that context, we'll see examples illustrating how to:
Utilize macros to reduce Jato scripting
Use the Jato debugger to understand script execution
Perform conditional XML generation
Obtain an understanding of script execution using debug statements
Recursively transform hierarchically structured object systems
Invoke Java methods polymorphically
Invoke custom Jato functions
The techniques covered in this article represent ideal generating scripts to persist application-configuration information, serialize Java objects for long-term storage, and dynamically generate XML for viewing in Web browsers. As a vehicle for discussing these topics, we will develop a Jato script that converts a directory structure as represented by the java.io.File class into XML. The completed script will perform the following tasks:
Set a path attribute in the XML document's root node that specifies the path of the base directory.
Write permissions, modification date, and size information about each file and directory contained in the base directory.
Recursively write information about the contents of the base directory. Thus, each directory found in the base directory will be examined; moreover, information about each of its files and directories will be generated.
The following listing provides sample output from the finished script:





java-to-xml.xml


FileToXML.java




Like many XML documents, our example can generate deeply nested XML elements. With recursive macro calls, we can handily generate and process such documents. One of the difficulties with recursive programs is tracking what the program is currently processing. Jato provides an excellent means for tracking program execution with the tag. We'll see macros and debugging next.
Note: At the time of this writing, Jato is in beta 2, with tremendous development work being piled into it. Occasionally, a change is made that will break backwards compatibility. To ensure the article examples work properly, the distribution will include all the samples from this series.
and 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 tag and declare them in a section using tags as shown in Listing 1:
Listing 1: Jato script demonstrating macros and debug
1.
2.
3.
4.
5.
6. Invoked macro
7.

8.

9.

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 section contains all the macro definitions as delineated by the 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 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; i15. 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
and tags describing directories and files, respectively:
Listing 3: Jato script for recursively traversing a directory
1.
2.
3.
4.
5.

6.
7.
8.
9.
10. Transforming directory
11.
12.
13.
14. Recursive call to 'dir' macro
15.
16.
17. Transforming file
18.
19.
20.

21.

22.

23.

24.

25.

26.

27.

The generated XML document appears as:




java-to-xml.xml
FileToXML.java



The script in Listing 3:
Line 3: Gets the Java object keyed by 'root' from the translator helper. This will be a java.io.File object.
Line 4: Transfers control to the 'dir' macro specified at line 8.
Line 9: Inserts a
tag into the output document. Remember any non-Jato tags are inserted into the destination document.
Line 11: Sets the 'name' attribute to the value of the current object's name property. In this case, the File method getName() is invoked and the returned String is used as the property value.
Line 12: Invokes the method File[] listFiles() on the current object (a File object). The script inside the tag runs on each element in the array. During this evaluation the element being evaluated becomes the current object.
Line 13: Jato conditional checks the state of the directory property. The directory property maps to the boolean isDirectory() method of the File class. If the property is true, the script in the tag is evaluated; otherwise, the tag at line 15 is evaluated. Note that Jato also supports a tag.
Line 15: Recursively invokes the 'dir' macro on the current File object and current
tag. This is the key step to building a hierarchical XML document that mirrors the hierarchical structure of the filesystem.
Line 10 and 17: The tag prints out information about the current interpreter state. print-object='true' tells Jato to print the current object's toString() value. In addition, you may specify a 'print-elt' attribute that will print the state of the current XML tag being manipulated. In this case, the interpreter prints out messages that appear as:
JATO Debug: Transforming directory:
Object = E:\ARTICLES\jw-jato-2\examples\step-1
JATO Debug: Transforming file:
Object = E:\ARTICLES\jw-jato-2\examples\step-1\java-to-xml.xml
JATO Debug: Transforming file:
Object = E:\ARTICLES\jw-jato-2\examples\step-1\FileToXML.java
Line 19: The tag sets text for the current XML tag. The value of the text is obtained by getting the current object's name property


 

Read Tutorial at: Click here to view the tutorial

Rate Tutorial:
Jato: The new kid on the open source block, Part 2 - JavaWorld April 2001

View Tutorial:
Jato: The new kid on the open source block, Part 2 - JavaWorld April 2001

Related Tutorials:

XSLT blooms with Java
XSLT blooms with Java
 
Java security evolution and concepts, Part 5
Java security evolution and concepts, Part 5
 
Take command of your software
Take command of your software
 
Create your own type 3 JDBC driver, Part 3
Create your own type 3 JDBC driver, Part 3
 
Jabber away with instant messaging
Jabber away with instant messaging
 
Jini's relevance emerges, Part 1
Jini's relevance emerges, Part 1
 
Check out three collections libraries
Check out three collections libraries
 
Test email components in your software
Test email components in your software
 
Beware the dangers of generic Exceptions
Beware the dangers of generic Exceptions
 
Nice widget
Nice widget
 
Fixing the Java Memory Model, Part 2
Writing concurrent code is hard to begin with; the language should not make it any harder. While the Java platform included support for threading from the outset, including a cross-platform memory model that was intended to provide \"Write Once, Run Anywh
 
Taming Tiger
Taming Tiger, Part 2 Understanding generics Welcome to the second part of this three-part series on Sun Microsystems' latest release of the Java 2 Platform, Standard Edition (J2SE). To refresh your memory, Part 1 was a quick introduction to J2SE 1.5
 
Strut your stuff with JSP tags
Learn how to use the custom tags from the open source Struts library and create extensions that ease the coding of properties associated with field values and user input validation. The Struts package is part of the open source Jakarta project.
 
Integrating Struts, Tiles, and JavaServer Faces
Integrating Struts, Tiles, and JavaServer Faces. Bring the power, flexibility, and manageability of the three technologies together.
 
What is Persistence Framework?
What is Persistence Framework? What is Persistence Framework? A persistence framework moves the program data in its most natural form (in memory objects) to and from a permanent data store the database. The persistence framework manages the
 
Open Source Web Frameworks in Java
Open Source Web Frameworks in Java Open Source Web Frameworks in Java Struts Struts Frame work is the implementation of Model-View-Controller (MVC) design pattern for the JSP. Struts is maintained as a part of Apache Jakarta project and is open
 
We are providing Fedora Cord 2 Linux CD's .
We are providing Fedora Cord 2 Linux CD's . Fedora Core 2 Linux Now Available Fedora Cord2 CD's We are providing the free downloadable version of Fedora Core 2 CDs, which is distributed under GNU public license. Fedora is the latest version of
 
Free Linux Distribution in India
Free Linux Distribution in India Free Linux Distribution in India Your premium place to get Free Linux CDs in India Popularity of Linux is growing in World including India. So to be part of this we have decided to help Indian community in getting
 
Buy Slackware 10.0 in India from us. Slackware 10.0 is available with us.
Buy Slackware 10.0 in India from us. Slackware 10.0 is available with us. Slackware 10 Linux Now Available Linux Slackware 10 CD's Slackware 10.0 contains the GNOME 2.6.1 (including a collection of pre-compiled GNOME applications), and KDE 3.2.3,
 
Q&A on Open Source Development and the Solaris 10 OS
A Sun VP talks about how open source developers can work closely with Sun. Find out about a pilot program engaging with sys admins and developers to build the Solaris community. For more details visit http://opensolaris.org.
 
Site navigation
 

 

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2006. All rights reserved.