Home >
Online Product Documentation >
Table of Contents >
Example - demo.java
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.
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.
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.
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)).
In this example, since we opened the input and output streams, we must also close them.
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.
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:
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:
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.