Home > Online Product Documentation > Table of Contents > Invoking an Adapter Programmatically
You can use the Stylus Studio File System Java API to invoke an adapter programmatically. This section provides background information on using adapter URLs in Stylus Studio and in your applications, and it describes
demo.bat, a simple Java program installed with Stylus Studio.
This section covers the following topics:
Stylus Studio uses URLs extensively to reach a variety of data sources. For example, if you use the built-in CSV converter to open the
three.txt file in the
\examples\Adapters directory as an XML document, Stylus Studio builds the following URL:
The instructions to the adapter engine from this instance of the adapter URL are described in Table 20.
While the basic format of the adapter URL is the same from one adapter to another, there are differences. For example:
adapter scheme can also be used to reference a user-defined converter (a
.conv file). In this case, the adapter URL specifies only the location of the
.conv file; the converter file itself contains information about its property settings. An adapter URL that references a user-defined converter might look like this:
This converter uses
myConverter.conv to convert the file
inventory.txt to some format (specified in the converter file when you built it).
Adapter URLs are displayed
|
Note
|
|
Adapter URLs in the URL field of the Select XML Converter dialog box are displayed using escape characters. |
You can use either as sources for the adapter URL strings you use in your Java applications. See Constructing Your Own Adapter URL for more information.
In Java, we write the following to retrieve the content of a file,
myfile.txt, that resides on an FTP server:
The content of
myfile.txt is placed in an instance of
java.net.URL called
myUrl.
The
StylusFile object in the Stylus Studio File System Java API is similar to
java.net.URL. In Stylus Studio, you use the
createStylusFile() method in the
StylusFileFactory class to create an instance of a
StylusFile object, the contents of which is specified by an adapter URL. Consider the following simple Java application, which uses the URL composed by Stylus Studio that was introduced in
Adapter URLs.
In this example, the
InputStream object holds the conversion result supplied by the adapter URL (that is, the conversion of
three.txt using the CSV adapter).
See Example - demo.bat for a description of the sample Java application installed with Stylus Studio, which illustrates different uses of the adapter URL.
Generally speaking, you should use Stylus Studio to construct adapter URLs, which you can then copy, as strings, into a Java application. Adapter URLs can be complex - properties and their values vary from one adapter to another, for example - so using Stylus Studio to construct them helps reduce errors in your applications.
|
Tip
|
|
You can optionally display a file's full URL in the Project window by selecting Show Full URL from the Project window's shortcut menu. |
The complete URL appears in the text editor.
escape=\:quotes='" becomes
escape=\\:quotes='\" (the single quote does not need to be escaped).
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 the invoking adapter URLs to convert files using built-in and user-defined converters.
The files required to run this demonstration are installed in the
\examples\Adapters directory where you installed Stylus Studio. They are summarized in
Table 21.
Here is the
demo.java file called by
demo.bat.
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.
User-defined converters (
.conv files) are saved to the XML Converters file system. If your application uses user-defined converters, such as the
three.conv file used by the
demo.bat, you need to identify the Stylus Studio installation directory so set the converter 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%").
Next, the
StylusFileFactory class is called to create a new instance of the
StylusFile, object, named
sff in this demonstration.
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, you convert a non-XML file to XML using the built-in or user-defined converter specified in the adapter URL.
OutputStream is specified using the adapter URL, you are convert a non-XML file to XML, again, using the built-in or user-defined converter specified in the adapter URL.
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 converter (
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.
This exception block specifies the
OutputStream using the adapter URL to convert an XML document named
two.xml to a CSV file named
two.txt.
The final exception block uses a user-defined converter built using the Convert to XML module (
three.conv) to convert a fixed-width file name
three.txt to and XML document named
three.xml.
As with the first exception block, this block uses the adapter URL to specify the
InputStream.
You can learn about other uses for the Stylus Studio File System Java API in Stylus Studio File System Java API, located in Chapter 13Extending Stylus Studio.
Javadoc for the Stylus Studio File System Java API is installed with Stylus Studio. It is available in the Stylus Studio /doc/Javadoc directory. Open index.html to get started. The Javadoc is also available on the Stylus Studio Web site, www.stylusstudio.com.