Programming Tutorials Browser Tutorials Articles Struts Tutorials Hibernate Tutorials

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

Jato: The new kid on the open source block, Part 3 - JavaWorld May 2001

Tutorial Details:

Jato: The new kid on the open source block, Part 3
Jato: The new kid on the open source block, Part 3
By: By Andy Krumel
Translate XML documents into Java objects
elcome back for Part 3 in this series on Jato, the open source, increasingly capable, XML/Java translator. In Part 1 , I introduced Jato and how to perform XML-to-Java and Java-to-XML transformations. Since then Jato has experienced a complete redesign, a number of enhancements, and the addition of an interactive debugger. Part 2 focused on generating XML documents from complex Java object structures through an application that generated an XML document containing file system information. Part 2 further discussed macro writing, XML element and attribute creation, debug statements utilization, Java methods invocation, and Jato function writing.
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
In Part 3, we turn our attention to XML-to-Java transformations and the conditional tags and , as well as Jato expressions, recursive XML traversal, constructors invocation, JavaBeans property settings, conditional parameter lists, and a little Swing.
Note: You may find the Jato Syntax Reference Guide useful for obtaining more detailed information on all the tags presented in this article.
Sounds exciting, so let's get started.
The new assignment
In Part 2 we transformed the hierarchical file-structure information contained in a File object into an XML document called site.xml . A partial listing of site.xml is shown is Listing 1:
Listing 1. Partial listing of site.xml





SimpleXmlToJava.java


SimpleXmlToJava.java




file


In our new assignment, we must create a Jato script that transforms site.xml into a Swing user interface. The finished application is shown in Figure 1.
Figure 1. JatoTree finished appearance
The user interface makes heavy use of the JTreeTable class source code provided in " Creating TreeTables: Part 2 ," Scott Violet and Kathy Walrath (java.sun.com).
Before jumping into the Jato script that transforms site.xml , let's lightly explore Jato expressions, a feature required to get an A on our assignment.
Jato expressions
Jato introduced expressions with the release of Beta 2 in March 2001. Expressions provide a compact syntax for obtaining current interpreter state information and evaluating logic that would require a large amount of Jato script. Jato uses expressions to perform conditional checks for and tags, generate debug statements, set the current object, manipulate the current input and output XML elements, construct variable names, and cause surly programmers to perform random acts of kindness. In this aritcle, I'll quickly introduce expressions, while a future article will deal with this subject in detail.
As an introduction to Jato expressions, consider the following debug statement:

"Current system time: " ~ class.System.currentTimeMillis()

This statement will print the following message to the console:
Jato Debug: Current system time: 985279624868
Ah, the funky and useful things Jato expressions can accomplish. In this case, the expression engine parses this expression as:
Operator: The concatenation operator ( '~' ).
Left operand: Literal string ( "Current system time: " ).
Right operand: A chained functional expression:
class : Instructs the expression engine to evaluate the expression between the two dots to obtain a class name. Fully qualified names are not required if the class resides in an imported package. The expression engine then invokes the method specified after the second dot. Parameters may be passed by placing comma-separated expressions between the parenthesis.
System : Specifies the simple class name for java.lang.System as an unquoted literal string, but the engine lets developers write complex expressions that invoke a method on the current object or read an XML attribute. The expression engine allows single, double, and unquoted literals.
currentTimeMillis() : Invokes the static method currentTimeMillis() on the System class passing zero parameters.
Using the return value from one expression to invoke another expression is called functional chaining -- similar to method chaining in Java. For instance, Java allows the following statement to determine the index of the first occurrence of the string "numb" :
int index = new String("Comfortably Numb").toLowerCase().indexOf("numb");
This statement creates a String instance, converts it to lowercase, and then determines the index of the word "numb" . The return value from the previous method invokes the next method. The same line of Java code could be spread over multiple lines as:
String title = new String("Comfortably Numb");
title = title.toLowerCase();
int index = title.indexOf("numb");
Unlike Java, where method chaining equals a luxury but not a necessity, Jato often requires function chaining to accomplish a given task. If you are a little confused by the current example, the examples in the following sections will provide additional expression examples.
Establish a script structure
To design and write the script, you must understand how to create JTreeTable components. The JTreeTable is a multiple column tree component that combines a JTree and JTable . The component uses a TreeTableModel as the data model; we will employ a concrete implementation called the FileSystemModel . The FileSystemModel maintains a list of FileNode objects, which is an abstract base class that defines methods for obtaining a file name, type, size, and last modified date. The code in Listing 2 creates a JTreeTable with a root and two descendants. Figure 2 shows the appearance of the JTreeTable when configured using Listing 2:
Listing 2. Example JTreeTable creation
import tree.*; //the JTreeTable package
//create a root node and add file nodes
DirNode root = new DirNode();
//create a directory node and add to root
DirNode node1;
node1 = new DirNode("level 1", "rw");
root.addChild(node1);
//create a content node (leaf) and add to node 1
ContentNode node2;
node2 = new ContentNode("level 2", "rw");
node2.setSize(12345);
node2.modifiedOn(new Date());
node1.addChild(node2);
//use the root node and use it to create the data model and tree
JTreeTable treeTable = new JTreeTable(new FileSystemModel(root));
Figure 2. Appearance of JTreeTable
Notice that all JTreeTable classes reside in the tree package. The code fragment in Listing 2 uses two concrete implementations of the FileNode class:
DirNode : Represents a directory that can contain other directory and file nodes. The DirNode provides the following constructors:
public DirNode() //used to create root nodes .
public DirNode(String name, String perms) .
ContentNode : Represents a data file and is always a leaf node. ContentNode defines a single constructor: public ContentNode(String name, String perms) .
The code fragment makes use of two methods defined in the FileNode parent class:
public void setSize(int size) : Implemented in the ContentNode class to set the byte size. The DirNode class ignores the method and calculates a size by summing the sizes of its child nodes.
public void modifiedOn(Date mod) : Sets the file system object's last modified date. Notice it requires a java.util.Date instance.
Putting it all together, our script needs to perform the following:
Create a root DirNode
Iterate each element in site.xml
Instantiate a DirNode instance for each
tag and add it to the parent DirNode :
Instantiate a ContentNode instance for each tag and add it to the parent DirNode
Create a Date object using the value specified in the modified attribute and invoke the node's modifiedOn() method
For each element, we also need to invoke the setSize() method passing size attribute's value
Create a FileSystemModel using the root node and instantiate the tree
The code in Listing 3 establishes the structure for the Jato script:
Listing 3. Template for script to create JTreeTable
1.
2. java.util.Date
3. tree.*
4.
5.
6.
7.
8.

9.
10.
11.
12.
13. Create dir node
14.
15.
16.
17.
18.

19.

20.
21.
22.
23. Create file node
24.

25.

26.

27.

The template performs the following actions:
Lines 2 and 3: Imports the Date class and all the JTableTree classes in the tree package.
Line 5: The tag creates Java objects. In this case, the type attribute specifies the DirNode class using the default constructor. The publish attribute instructs the Jato interpreter to invoke the helper's publish() method and pass the directory node and the identifying key value of 'root' .
Line 6: Invokes the 'dir' macro, which will process the root's


 

Read Tutorial at: Click here to view the tutorial

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

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

Related Tutorials:

Java Tip 72: Press Escape to close your Swing dialog windows
Java Tip 72: Press Escape to close your Swing dialog windows
 
Cut down on logging errors with Jylog
Cut down on logging errors with Jylog
 
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
 
Jini's relevance emerges, Part 1
Jini's relevance emerges, Part 1
 
Test email components in your software
Test email components in your software
 
Beware the dangers of generic Exceptions
Beware the dangers of generic Exceptions
 
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
 
Jappo - an open and modular Java preprocessor
Jappo - an open and modular Java preprocessor Jappo is Java preprocessor. It examines input files for preprocessor statements (i.e. macros) that are then interpreted, resulting in the alteration of the input content that is stored as target file. Jappo
 
Taming Tiger, Part 3
J2SE 5—code named "Tiger"—is the most significant revision to the Java language since its original inception. Tarak Modi's primary goal with his three-part series on Tiger is to familiarize readers with J2SE 5's most important additions and show how t
 
Java Beans, Part 1 Introducing Java Beans
The basic idea of the Beans tutorial is to get you to the point where you can quickly create beans. You may want to write new beans from scratch, or you may want to take existing components, applets, or other classes and turn them into beans.
 
Integrating Struts, Tiles, and JavaServer Faces
Integrating Struts, Tiles, and JavaServer Faces. Bring the power, flexibility, and manageability of the three technologies together.
 
Definition of Bioinformatics
Definition of Bioinformatics Definition of Bioinformatics About Bioinformatics In February 2001, the human genome was finally deciphered! In other words, scientists have succeeded in reading the chain of more than 3 billion base pairs that
 
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
 
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
 
Buy Fedora Core 3 Linux CD's in India.
Buy Fedora Core 3 Linux CD's in India. Fedora Core 3 Linux Now Available Fedora Core 3 CD's We are providing the free downloadable version of Fedora Core 3 Linux CDs, which is distributed under GNU public license. ABOUT FEDORA Fedora is the
 
Buy Fedora Core 3 Test 1 Linux CD's in India.
Buy Fedora Core 3 Test 1 Linux CD's in India. Fedora Core 3 Test 1 Linux Fedora Core 3 Is Available Now Available Fedora Core 3 Test 1CD's We are providing the free downloadable version of Fedora Core 3 Test 1 Linux CDs, which is distributed
 
Chat Transcript: Solving the Device Fragmentation Problem
Read the questions that your fellow developers had about the new feature in NetBeans Mobility Pack 4.0 that helps solve device fragmentation problems, and the answers straight from the engineers who created the module.
 
Site navigation
 

 

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

Copyright © 2006. All rights reserved.