Powered by SmartDoc

Generating Java with Relaxer

Now that you are familiar with some of the simple tasks you can perform with Relaxer, you are ready to move on to more complex tasks, such as Java source code generation. To do this, you can use a schema or an instance. You can use instances, Relax Core schemas, and RELAX NG schemas for code generation, but not XSD schemas or DTDs.

To make things a little easier, you'll begin with a single-element document, inst.xml, found in the inst directory:

inst.xml
<?xml version="1.0" encoding="ISO-8859-1"?>

<inst>2001-01-01T00:00:01.0000001</inst>

This document contains only a single document element whose content represents an ISO 8601 date and time. To generate Java source code for this document, type in this line at a command prompt:

C:\Relaxer>relaxer -verbose -java -useJAXP inst.xml

From this command, Relaxer produces the following four artifacts (Java files):

Inst.java is based on inst.xml, and provides 10 constructors for creating objects based on this data model. For example, Inst.java provides a default constructor (no arguments), another that accepts a DOM document as an argument of type org.w3c.dom.Document, and another that accepts a URL argument of type java.net.URL, to name a few.

Inst.java also provides a number of methods that you can use to access or manipulate the content of a document that has this form. For example, you can use the getContent() and setContent() methods to retreive or change the content of <inst> elements. These methods accept arguments of type java.sql.Timestamp. Relaxer computed this type based on its evaluation of the content of <inst>. Other methods include makeDocument() which creates a DOM representation of the object modeled after inst.xml, and makeTextDocument() which outputs a representation of the object as an XML document.

The other three Java files were generated automatically by Relaxer and support underlying functionality. The fields and methods in these files are not user-accessible.

Using Javadoc

To easily examine the Java source that Relaxer produces, apply Javadoc to the source files with this line:

C:\Relaxer>javadoc -d doc Inst.java RStack.java UJAXP.java \
    URelaxer.java

This places the Javadoc output files in the subdirectory doc. Open the file doc/index.html in a browser. Now you can navigate through the documentation of the classes to see all the fields, constructors, and methods that Relaxer created.

Compiling and Running the Java Code

Now it's time to compile these Java files with javac:

C:\Relaxer>javac Inst.java

This compiles Inst.java, and RStack.java, UJAXP.java, and URelaxer.java as well.

In the example archive, you will also find a Java file that Relaxer did not create. It is called TestInst.java. Here is the source code for this program:

TestInst.java
import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;

/**
 * A small application that changes an <code>&lt;inst&gt;</code> document.
 * @author Mike Fitzgerald
 *
 */
public class TestInst {

    public static void main(String[] args)
	throws IOException, SAXException, ParserConfigurationException {

        // Instantiate an Inst object
	Inst inst = new Inst(new File(args[0]));
 
        // Print the XML representation of the document
	System.out.println("\nOriginal document:\n");
	System.out.println(inst.makeTextDocument());
 
        // Set content of inst to current date/time 
        Timestamp ts = new Timestamp(new Date().getTime());
	inst.setContent(ts);

        // Again, print the XML representation of the document
	System.out.println("\nUpdated document:\n");
	System.out.println(inst.makeTextDocument());

    }

}

Compile and run this program:

C:\Relaxer>javac TestInst.java
C:\Relaxer>java TestInst

The output for this program will look something like this:

TestInst output
Original document:

<inst>2001-01-01T00:00:01.0000001</inst>

Updated document:

<inst>2003-01-17T16:46:49.118</inst>

The document is changed programmatically by a call to the setContent() method, using a type that is not intrinsic to XML, java.sql.Timestamp.

To continue exploring Relaxer, see examples in the sample directory, part of the Relaxer distribution, such as sample/hello.