Programming Tutorials Browser Tutorials Articles Struts Tutorials Hibernate Tutorials

  Tutorial: Jato: The new kid on the open source block - JavaWorld March 2001

Jato: The new kid on the open source block - JavaWorld March 2001

Tutorial Details:

Jato: The new kid on the open source block, Part 1
Jato: The new kid on the open source block, Part 1
By: By Andy Krumel
A new library for converting between Java and XML
ept separate, XML and Java are environmentally friendly, but sound scientific evidence indicates the effort developers exert to merge them may contribute to global warming. This article, the first of three, introduces the open-source Jato API, a better way to turn XML documents into Java application objects and vice versa. First, we will examine Jato's key features, architecture, and important classes. Then we will develop Jato scripts to perform basic Java-to-XML and XML-to-Java transformations. So get ready to strip out those hundreds of lines of XML parsing code and replace them with a few lines of Jato script!
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
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.
What is Jato?
Jato is an open-source Java API and XML language for transforming XML documents into a set of Java objects and back again. Jato scripts describe the operations to perform and leave the algorithms for implementing the operations to an interpreter. A Jato script expresses the relationships between XML elements and Java objects, freeing the developer from writing iteration loops, recursive routines, error-checking code, and many other error-prone, verbose, and monotonous XML parsing chores.
Jato has many advantages over directly employing traditional Java XML APIs such as JDOM, SAX, or DOM. Those advantages include:
Jato encourages XML and Java designs to be optimized for their specified tasks. Well-designed systems have a low degree of coupling, allowing one to make independent changes to portions of a system without it breaking. Indeed, it's a good idea for XML DTD and Java object-oriented design to be developed and deployed independently.
With Jato, developers simply express the XML elements that map to or from specific Java classes. The Jato interpreter then implements the necessary parsing and generation algorithms to accomplish the desired actions. As such, you avoid the monotonous, monolithic, and difficult-to-maintain XML parsing and generation code.
Using XML to describe transformation to or from XML in Java applications just seems natural. I will never forget the first time an application called for parsing an XML document to create a set of Java objects. After about 150 lines of code, most of the remaining 850 lines consisted of cut-and-paste operations followed by altering element, attribute, class, and method names. Whenever a task involves that much cut-and-paste, it just begs to be automated.
Key Jato features
Jato's design and feature set directly result from implementing real-world projects. Jato is feature-rich, but hopefully not at the expense of usability or readability. Its key features include:
Open source: The source code for Jato is freely available for use, modification, and improvement. Please contribute your ideas and energy to the project (see Resources ).
Completely dynamic: Jato reads Jato scripts at runtime and employs reflection and JDOM to transform between Java and XML. Other APIs that perform similar tasks require the developer to create a schema, generate a set of classes from the schema, compile the classes, and distribute the compiled classes.
Supports the JavaBeans naming conventions: If a class contains portions that adhere to the JavaBeans specification, life gets a little simpler.
Supports complex constructors and methods: Most real-world applications rely on classes that require nondefault constructors and methods that do not adhere to the JavaBeans specification. Jato can invoke static methods along with multiargument constructors and methods.
Macros: Jato script elements can be grouped and invoked within the primary script or other macros. Macros reduce the amount of Jato code required to specify a transformation.
Conditional transformations: Transformations can be based on the type of XML element, the presence or value of an attribute, the state of an object, or the phases of the moon.
Built on JDOM: Developers can drive the Jato interpreter using any XML parser or generator.
Supports namespaces: Many similar APIs do not adequately handle namespaces; Jato does.
Custom Jato tags: Developers can easily add new functions to the Jato interpreter. Custom tags enable developers to seamlessly integrate Jato with databases, obtain information from networked applications, perform custom formatting, and handle unique situations.
Jato architecture
Jato consists of several classes and two interfaces, all of which reside in the org.jato package. Let's take a look:
JavaToXml : A helper class for performing Java-to-XML transformations from a command line or embedded in an application
XmlToJava : A helper class for performing XML-to-Java transformations from a command line or embedded in an application
ObjectGenerator : Jato interpreter class for XML-to-Java transformations
ObjectHelper : The interface the developer implements to receive instantiated Java objects
ObjectAdaptor : An adapter class for ObjectHelper
XmlGenerator : The Jato interpreter class for Java-to-XML transformations
XmlHelper : The interface the developer implements to provide objects for XML generation
XmlAdapter : An adapter class for XmlHelper
JatoException : Thrown by a Jato interpreter to indicate an error
TagHandler : The interface for implementing custom Jato tag handlers
When specifying a transformation, regardless of the direction, the developer provides:
Jato script: XML that expresses how to process XML and Java and generate desired output.
Helper class: Provides and publishes important transformation objects and values. The class serves as an adapter class that interfaces the application with Jato.
Figure 1 illustrates the relationship between the ObjectGenerator , ObjectHelper , Jato script, and source XML when performing an XML-to-Java transformation.
Figure 1. XML-to-Java transformation relationships
The ObjectGenerator acts as the Jato interpreter and controls the transformation process. It uses the Jato script to establish a plan for transforming the XML document. The ObjectHelper class receives generated objects marked for publishing in the Jato script through its publish() method.
Figure 2 illustrates the relationship between the XmlGenerator , XmlHelper , Jato descriptor, and Java objects when performing a Java-to-XML transformation.
Figure 2. Java-to-XML transformation relationships
The XmlGenerator acts as the Jato interpreter and controls the transformation process. It uses the Jato script to establish a plan for transforming the Java application objects. The XmlHelper acts as the interface point for the current application. The XmlGenerator requests objects from the XmlHelper and generates an XML document that reflects the state of the application.
Java-to-XML example
The quickest way to understand Jato is to examine sample Java-to-XML and XML-to-Java transformations. The first example translates the system properties obtained using the System class's getProperties() and getProperty() methods into XML. If you have never examined the system properties, Listing 1 serves as an example program that iterates over the properties and prints them to the console:
Listing 1: A program that prints the system properties
import java.util.Properties;
import java.util.Enumeration;
class PrintProps {
public static void main(String args[]) {
Properties props = System.getProperties();
Enumeration e = props.keys();
String key, prop;
System.out.println("System properties:");
while(e.hasMoreElements()) {
key = (String)e.nextElement();
System.out.print("\t" + key + "=");
prop = System.getProperty(key);
System.out.println(prop);
}
}
}
A little verbose, but the reason for that will become apparent when we compare it line-by-line with a Jato script that accomplishes an equivalent task. The application outputs:
System properties:
java.specification.name=Java Platform API Specification
awt.toolkit=sun.awt.windows.WToolkit
java.version=1.2
java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment
user.timezone=America/Los_Angeles
java.specification.version=1.2
java.vm.vendor=Sun Microsystems Inc.
java.vm.specification.version=1.0
. . . .
In Listing 2, we see a Jato program that outputs an XML version of the system properties:
Listing 2: A program that performs a Java-to-XML translation
1. import java.util.Properties;
2. import org.jdom.JDOMException;
3. import org.jato.XmlHelperAdapter ;
4.
5. public class SimpleJavaToXML extends XmlHelperAdapter {
6. SimpleJavaToXML() throws JDOMException {
7. super(sJatoXML);
8. }
9.
10. public Object getObject(String key) {
11. Properties props = System.getProperties();
12. return props.keys();
13. }
14.
15. static String sJatoXML =
16. "\n" +
17. "\n" +
18. " \n" +
19. " \n" +
20. " \n" +
21. " 22. " class='System'>\n" +
23. " \n" +
24. "
\n" +
25. "
\n" +
26. "
\n" +
27. "
";
28. }
The org.jato.JavaToXml class runs scripts from the command line or allows them to be easily embedded in your applications. To run this script and save it to a file called simple.xml , type this at the com


 

Read Tutorial at: Click here to view the tutorial

Rate Tutorial:
Jato: The new kid on the open source block - JavaWorld March 2001

View Tutorial:
Jato: The new kid on the open source block - JavaWorld March 2001

Related Tutorials:

Cut down on logging errors with Jylog
Cut down on logging errors with Jylog
 
XSLT blooms with Java
XSLT blooms with Java
 
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
 
Servlet 2.4: What's in store
Servlet 2.4: What's in store
 
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
 
Apache Geronimo
Apache Geronimo Welcome to Apache Geronimo, the J2EE server project of the Apache Software Foundation. Please help us make this a world class, certified J2EE container!
 
Janino -- an Embedded Java Compiler
What is Janino? Janino is a compiler that reads a JavaTM expression, block, class body, source file or a set of source files, and generates JavaTM bytecode that is loaded and executed directly. Janino is not intended to be a development tool, but an embe
 
IberAgents
Introduction IberAgents is a web application framework that enables the creation of SOAP-interoperable components in Java, with life cycle management and remote configuration. Development started in 2001; we now have a mature, solid open-source platform
 
The eXo platform
The eXo platform The eXo platform™ software is a powerful Open Source corporate portal and content management system. Users of the platform have a customized single point of access to the company's information system and resources. The use of a corpora
 
100 % java open-source cryptographic software libraries.
Cryptixtm is an international volunteer effort to produce robust, open-source cryptographic software libraries. Cryptix products are free, both for commercial and non-commercial use and are being used by developers all over the world. Development is ...
 
Integrating Struts, Tiles, and JavaServer Faces
Integrating Struts, Tiles, and JavaServer Faces. Bring the power, flexibility, and manageability of the three technologies together.
 
Unweaving a Tangled Web With HTMLParser and Lucene
In this article I'll show you the basic technique in building a search engine using two powerful Open Source products: HTMLParser and Lucene.
 
Struts, JavaServer Faces, and Java Studio Creator:
The Evolution of Web Application Frameworks Sun Microsystems' Craig McClanahan, the creator of the Apache Struts Framework, co-specification lead for JavaServer Faces 1.0, and prime architect for Sun Java Studio Creator's new release, explains all three.
 
Janino -- an Embedded JavaTM Compiler
Janino is a compiler that reads a JavaTM expression, block, class body, source file or a set of source files, and generates JavaTM bytecode that is loaded and executed directly. Janino is not intended to be a development tool, but an embedded compiler for
 
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
 
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.
 
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.
 
Solaris 10 OS Certification Beta Exams
If you are an expert in system and network administration, you can get involved in the creation of three new Solaris 10 certification exams. These Beta exams count toward official Solaris Certification and allow you to provide comments and technical feedb
 
Site navigation
 

 

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

Copyright © 2006. All rights reserved.