Example - demo.java

The example file, demo.java, runs five sample demonstrations that show how the Stylus Studio Java API can be used to convert data to and from XML stored in a number of different formats using both built-in adapters and user-defined converters. This section describes the files associated with the demonstrations and how to run it. It also describes the code samples used for each of the sample demonstrations.

Demonstration Files

The files required to run the demonstrations are summarized in Table 149. All but one, three.conv, are installed in the \examples\Adapters directory where you installed Stylus Studio. The three.conv file is installed in your Stylus Studio \Adapter directory.

Class
Description
demo.bat
The demonstration driver batch file.
demo.java
The source for the demonstration; this file contains the usage comments.
demo.sh
The demonstration driver file for Unix
one.csv
The input file for the first demonstration run by demo.bat.
two.xml
The input file for the second demonstration run by demo.bat.
three.conv
The adapter definition for the third demonstration run by demo.bat. (This file is installed in your Stylus Studio \Adapter directory.)
three.txt
The input file for the third demonstration run by demo.bat.
Table 149. Stylus Studio Java API Demonstration Files

Running demo.java

To start the demo.java demonstration:
1. Open a console window.
2. Change to the examples\Adapters directory where you installed Stylus Studio.
3. Run demo.bat (or demo.sh, if you installed the Stylus Studio XML Deployment Components on a Unix machine).

Required Classes

In addition to standard Java classes like java.io.FileOutputStream and javax.xml.transform.Transformer, the demo.java application includes several classes from the Stylus Studio Java API.

import com.exln.stylus.io.StylusFileFactory;
               
import com.stylusstudio.converter.Converter;
               
import com.stylusstudio.converter.Conversion;
               
import com.stylusstudio.converter.ConverterException;
               
import com.stylusstudio.converter.ConverterFactory;
               

            

The com.exln.stylus.io.StylusFileFactory class allows the application to use the StylusFileFactory.unlockAPI() method. This method, which provides access to the Stylus Studio Java API, takes as its parameter the Stylus Studio installation ID. If you are developing applications that you plan to deploy on one or more servers, you must use the installation ID associated with your copy of the Stylus Studio XML Deployment Components. Otherwise, you can use the Stylus Studio 2007 XML Enterprise Suite installation ID.

Setting the Installation Directory

The STYLUS_DIR variable is set automatically during the Stylus Studio installation. This variable is used to define exampleDir, which is to specify the location of the source files used by the demo.java sample demonstration.

String exampleDir = System.getProperty("com.stylusstudio.exampledir");
               
	if (exampleDir == null) {
               
		if (new File("./one.csv").exists())
               
			exampleDir = System.getProperty("user.dir");
               
		else exampleDir = STYLUS_DIR + File.separator + "examples" + 
File.separator + "Adapters";
               
}
               
exampleDir += File.separator;
               
 
               
String uriBase = "file:" + exampleDir.replace('\\', '/')
               

            

You can also specify the Stylus Studio installation directory manually, using the -D argument at the command line, like this: -Dcom.stylusstudio.exampledir=c:\myStylus\.

Example 1

Example 1 converts a comma-separated values (CSV) file, one.csv, to an XML file, one.xml, using Stylus Studio's built-in CSV adapter. The conversion parameter for the new Converter object is specified as a Stylus Studio adapter: URL that indicates which built-in adapter to use to convert the input file to the output file. Only two CSV adapter settings are shown; default values are used for all settings unless you specify them in the adapter: URL.

ConverterFactory factory = ConverterFactory.newInstance();
               
StreamSource input = new StreamSource(exampleDir + "one.csv");
               
StreamResult output = new StreamResult(exampleDir + "one.xml");
               
 
               
Converter toXML = factory.newConvertToXML("adapter:CSV:sep=,:first=yes");
               
toXML.convert(input, output);
               
 
               
System.out.println("test 1 finished: one.csv -> one.xml");
               

            

Both input and output streams are opened and closed by the Converter object.

Example 2

Example 2 is similar to Example 1, but instead of converting a non-XML file to XML, it does the opposite. It also shows how to use the Stylus Studio URI resolver to create both the input stream (sff.resolve("two.xml", uriBase)) and output stream (sff.outputStreamResolver("two.csv", uriBase)).

StylusFileFactory sff = StylusFileFactory.getFactory();
               
 
               
input = (StreamSource) sff.resolve("two.xml", uriBase);
               
 
               
if (input==null) 
               
	throw new IOException("Unable to find 'two.xml' in " + uriBase);
               
 
               
output = (StreamResult) sff.outputStreamResolver("two.csv", uriBase);
               
 
               
if (output==null) 
               
	throw new IOException("Unable to create 'two.csv' in " + uriBase);
               
 
               
Converter fromXML = factory.newConvertFromXML("adapter:CSV:sep=,:first=yes");
               
fromXML.convert(input, output);
               

            

In this example, since we opened the input and output streams, we must also close them.

input.getInputStream().close();
               
output.getOutputStream().close();
               
 
               
System.out.println("test 2 finished: two.xml -> two.csv");
               

            

Example 3

Example 3 uses a user-defined converter, three.conv, built using the Stylus Studio Convert to XML module, to convert a fixed-width file, three.txt, to XML. Here, we create our own StreamSource and StreamResult objects - because we are converting a local text file, there is no need to use the Stylus Studio URI Resolver.

InputStream is = new FileInputStream(exampleDir + "three.txt");
               
OutputStream os = new FileOutputStream(exampleDir + "three.xml");
               
 
               
input = new StreamSource(is);
               
output = new StreamResult(os);
               
 
               
toXML = factory.newConvertToXML("adapter:three.conv");
               
toXML.convert(input, output);
               
 
               
is.close();
               
os.close();
               
 
               
System.out.println("test 3 finished: three.txt -> three.xml");
               

            

Example 4

Examples 1, 2, and 3 performed simple conversion of one file type to another - some type of converter (either a Stylus Studio built-in adapter or a user-defined converter) was given an input and converted it to another format.

Example 4 generates conversion output (in this case, a CSV file, one.csv, converted to XML using the Stylus Studio built-in adapter) as SAX events. These SAX events are then sent to the transformer's ContentHandler. The process is illustrated in Figure 510.

Figure 510. Post-Conversion XSLT Processing

Here is the code for Example 4:

TransformerFactory tFactory = TransformerFactory.newInstance();
               
if (!(tFactory instanceof SAXTransformerFactory)) {
               
	System.out.println("Unable to run example 4");
               
	System.out.println(tFactory.getClass().getName() + " is not a 
SAXTransformerFactory");
               
}
               
else {
               
 
               
	SAXTransformerFactory saxFactory = (SAXTransformerFactory)tFactory;
               
 
               
	is = new FileInputStream(exampleDir + "one.csv");
               
	input = new StreamSource(is);
               
 
               
	os = new FileOutputStream(exampleDir + "four.xml");
               
	StreamResult finalResult = new StreamResult(os);
               
 
               
	TransformerHandler handler = saxFactory.newTransformerHandler();
               
	handler.setResult(finalResult);
               
			SAXResult intermediateResult = new SAXResult(handler);
               
 
               
	toXML = factory.newConvertToXML("adapter:///CSV:sep=,:first=yes");
               
	toXML.convert(input, intermediateResult);
               
 
               
	is.close();
               
	os.close();
               
 
               
	System.out.println("test 4 finished: one.csv -> four.xml");
               
}
               

            

The finalResult is an XML document, four.xml. In this example, we used a null transformer ( TransformerHandler handler = saxFactory.newTransformerHandler();), but you could specify any XSLT transformation here ( newTransformerHandler(new StreamSource(...)), for example) to perform any processing on the intermediate result you required.

Example 5

In Example 5, output from an XSLT transformation is sent to a converter, which takes the XML it is given (as a saxSource) and converts it to CSV. This process is summarized in Figure 511.

Figure 511. Passing an XSLT Result to a Converter

Here is the code for Example 5:

is = new FileInputStream(exampleDir + "two.xml");
               
input = new StreamSource(is);
               
 
               
os = new FileOutputStream(exampleDir + "five.csv");
               
StreamResult finalResult = new StreamResult(os);
               
 
               
SAXSource saxSource = new SAXSource();
               
Conversion conversion = fromXML.convert(saxSource, finalResult);
               
 
               
Transformer transformer = tFactory.newTransformer();
               
SAXResult intermediateResult = new SAXResult((ContentHandler)conversion);
               
transformer.setErrorListener((ErrorListener)conversion);
               
transformer.transform(input, intermediateResult);
               
 
               
is.close();
               
os.close();
               
 
               
System.out.println("test 5 finished: two.xml -> five.csv");
               

            

As with Example 4, no XSLT transformation instructions were specified ( newTransformer()); you can specify any XSLT transformation you require. To convert the transformation's output to CSV, we have reused an instance of the fromXML object, which we created in Example 2. This object uses the built-in Stylus Studio CSV adapter.

 
Free Stylus Studio XML Training:
W3C Member