Home >
Online Product Documentation >
Table of Contents >
Example - demo.bat
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.
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.
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.
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.
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:
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.
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.
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.
As with the first exception block, this block uses the adapter URL to specify the
InputStream.