Example - demo.bat

The following example shows an implementation of a simple application built using the Stylus Studio File System Java API. The application, demo.bat, shows three uses of invoking adapter URLs to convert files using built-in and user-defined adapters.

Demonstration Files

The files required to run this demonstration are installed in the \examples\Adapters directory where you installed Stylus Studio. They are summarized in Table 24.

Class
Description
demo.bat
The demonstration driver batch file.
demo.class
The compiled class file.
demo.java
The source for the demonstration; this file contains the usage comments.
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 should have also been installed in your Stylus Studio \Adapter directory.)
three.txt
The input file for the third demonstration run by demo.bat.
adaptermap.
properties
Normally, Stylus Studio builds a map of all of the internally-supplied adapters and user-added adapters, and it places this map in a file called adaptermap.properties. For this demonstration, a small copy of the adaptermap.properties file is included in the \examples\Adapters directory.
Table 24. File System Java API Demonstration Files

demo.java

Here is the demo.java file called by demo.bat.

import java.io.File;
               
import java.io.FileOutputStream;
               
import java.io.OutputStream;
               
import java.io.FileInputStream;
               
import java.io.InputStream;
               
import java.io.IOException;
               
 
               
import com.exln.stylus.io.StylusFile;
               
import com.exln.stylus.io.StylusFileFactory;
               
 
               
public class demo {
               
	public static void main(String args[]) {
               
		StylusFileFactory sff = StylusFileFactory.getFactory();
               
 
               
		try {
               
			StylusFile in_1 = 
sff.createStylusFile("adapter:///CSV:sep=,:first=yes?file:one.csv");
               
			File out_1 = new File("one.xml");
               
			InputStream is = in_1.getInputStream();
               
			OutputStream os = new FileOutputStream(out_1);
               
			copy(is, os);
               
			in_1.close();
               
			System.out.println("test 1 succeeded: one.csv -> one.xml");
               
		} catch (IOException ioe) {
               
			ioe.printStackTrace();
               
		}
               
 
               
		try {
               
			File in_2 = new File("two.xml");
               
			StylusFile out_2 = 
sff.createStylusFile("adapter:///CSV:sep=,:first=yes?file:two.csv");
               
			InputStream is = new FileInputStream(in_2);
               
			OutputStream os = out_2.getOutputStream();
               
			copy(is, os);
               
			out_2.close();
               
			System.out.println("test 2 succeeded: two.xml -> two.csv");
               
		} catch (IOException ioe) {
               
			ioe.printStackTrace();
               
		}
               
 
               

            
		try {
               
			StylusFile in_3 = 
sff.createStylusFile("adapter:///three.conv?file:three.txt");
               
			File out_3 = new File("three.xml");
               
			InputStream is = in_3.getInputStream();
               
			OutputStream os = new FileOutputStream(out_3);
               
			copy(is, os);
               
			in_3.close();
               
			System.out.println("test 3 succeeded: three.txt -> three.xml");
               
		} catch (IOException ioe) {
               
			ioe.printStackTrace();
               
	static void copy(InputStream is, OutputStream os) throws IOException {
               
		byte buffer[] = new byte[8192];
               
		int bytesRead;
               
		while ((bytesRead = is.read(buffer)) != -1) {
               
			os.write(buffer, 0, bytesRead);
               
		}
               
		os.flush();
               
		os.close();
               
	}
               
 
               
}
               

            

Required classes

The file starts with the Java and Stylus Studio File System Java API classes and interfaces required by demo.java. The StylusFile interface is used to define an abstract representation of a file in a custom file system; the StylusFileFactory class enables the demo.bat application to read and write files.

import java.io.File;
               
import java.io.FileOutputStream;
               
import java.io.OutputStream;
               
import java.io.FileInputStream;
               
import java.io.InputStream;
               
import java.io.IOException;
               
 
               
import com.exln.stylus.io.StylusFile;
               
import com.exln.stylus.io.StylusFileFactory;
               

            

Setting the installation directory

User-defined adapters ( .conv files) are saved to the Convert to XML Definitions folder in the XML Converters file system. If your application uses user-defined adapters, such as the three.conv file used by the demo.bat, you need to identify the Stylus Studio installation directory so that the adapter can be located. You can do this using System.setProperty, as shown in the following lines of code.

public class demo {
               
	public static void main(String args[]) {
               
	//	System.setProperty("com.stylusstudio.rootdir", "C:\\Program 
Files\\Stylus Studio XML Professional Edition");
               

            

These lines are commented out in demo.java because you can also specify the Stylus Studio installation directory using the -D argument at the command line, as shown in demo.bat: "-Dcom.stylusstudio.rootdir=%STYLUS%").

Creating an instance of StylusFile

Next, the StylusFileFactory class is called to create a new instance of the StylusFile, object, named sff in this example.

		StylusFileFactory sff = StylusFileFactory.getFactory();
               

            

The copy () method

The copy() method is used to create new files by saving the file specified in the InputStream ( is) to the file specified in the OutputStream ( os).

The copy() method is defined in demo.java as follows:

	static void copy(InputStream is, OutputStream os) throws IOException {
               
		byte buffer[] = new byte[8192];
               
		int bytesRead;
               
		while ((bytesRead = is.read(buffer)) != -1) {
               
			os.write(buffer, 0, bytesRead);
               
		}
               
		os.flush();
               
		os.close();
               
	}
               

            

Whether your files are saved as XML or non-XML depends on how the InputStream and the OutputStream in your applications are defined. You can define either stream using an adapter URL. When the

  • InputStream is specified using the adapter URL, a non-XML file is converted to XML using the adapter specified in the adapter URL.
  • OutputStream is specified using the adapter URL, an XML document is converted to a non-XML file, again, using the adapter specified in the adapter URL.
Converting a file to XML

Once the new instance of StylusFile has been created, the demo.bat application can execute its first exception block. The adapter URL specified in this exception block uses the built-in Comma-Separated Values adapter ( adapter:///CSV:) to convert a CSV file named one.csv into an XML document named one.xml.

		try {
               
			StylusFile in_1 = 
sff.createStylusFile("adapter:///CSV:sep=,:first=yes?file:one.csv");
               
			File out_1 = new File("one.xml");
               
			InputStream is = in_1.getInputStream();
               
			OutputStream os = new FileOutputStream(out_1);
               
			copy(is, os);
               
			in_1.close();
               
			System.out.println("test 1 succeeded: one.csv -> one.xml");
               
		} catch (IOException ioe) {
               
			ioe.printStackTrace();
               
		}
               

            

In this block of code, the adapter URL is used to specify the InputStream.

Converting an XML document to another format

This exception block specifies the OutputStream using the adapter URL to convert an XML document named two.xml to a CSV file named two.csv.

		try {
               
			File in_2 = new File("two.xml");
               
			StylusFile out_2 = 
sff.createStylusFile("adapter:///CSV:sep=,:first=yes?file:two.csv");
               
			InputStream is = new FileInputStream(in_2);
               
			OutputStream os = out_2.getOutputStream();
               
			copy(is, os);
               
			out_2.close();
               
			System.out.println("test 2 succeeded: two.xml -> two.csv");
               
		} catch (IOException ioe) {
               
			ioe.printStackTrace();
               
		}
               

            

Using an adapter URL with a user-defined adapter

The final exception block uses a user-defined adapter built using the Convert to XML module ( three.conv) to convert a fixed-width file name three.txt to an XML document named three.xml.

		try {
               
			StylusFile in_3 = 
sff.createStylusFile("adapter:///three.conv?file:three.txt");
               
			File out_3 = new File("three.xml");
               
			InputStream is = in_3.getInputStream();
               
			OutputStream os = new FileOutputStream(out_3);
               
			copy(is, os);
               
			in_3.close();
               
			System.out.println("test 3 succeeded: three.txt -> three.xml");
               
		} catch (IOException ioe) {
               
			ioe.printStackTrace();
               
		}
               

            

As with the first exception block, this block uses the adapter URL to specify the InputStream.

 
Free Stylus Studio XML Training: