Java XML Tools
Java Code Generation
Taking your XSLT and XQuery To-Go with the new Java Code Generator in Stylus Studio® X15 Release 2 XML Enterprise Suite, by Clyde Kessel, Sr. Software Engineer, the Stylus Studio® Team.
So you've used Stylus Studio® to write and debug XSLTor XQuery, and it runs like a charm — inside Stylus Studio®. But now your boss wants you to take your XQuery and XSLT stylesheets to your production environment. Well, if your platform is Java (JRE 1.4 or later), Stylus Studio® X15 Release 2 XML Enterprise Suite can automatically generate the Java code required to deploy the XSLT or XQuery as a Java application or class within your Java application, using the Stylus Studio® Java Code Generator, illustrated below (click to enlarge). In this tutorial we're going to show you how.
What the Java Code Generator Does for You
Stylus Studio® makes it easy to write and debug XSLT and XQuery programs; you write, test, debug and run them using the
Stylus Studio® XML IDE. For many use cases,
that's all you need to do when you need to run your XSLT, you can just launch Stylus Studio® and run the file. Other use cases require something more automated. For example, suppose the XSLT or
XQuery is part of a client/server application, or needs to be run as part of a periodic batch job. In situations like this,
you will need to use the new Stylus Studio® Java Code Generator to build a Java application that runs the XSLT or XQuery.
The Stylus Studio® Java Code Generator will create a
To assist in the transition from XML to Java, Stylus Studio® creates the .java files, opens them inside the built-in Java IDE (which supports Java editing, compilation, debugging, and more), and finally, helps you set up a classpath tailored to the options you have chosen.
Creating a Standalone Java Application to Run an XSLT
We're going to use XSLT for the examples in this article but you can leverage all of Stylus Studio®'s Java code generation features with XQuery files as well. We'll start by opening up one of the examples provided with the Stylus Studio® examples project:
To create XSLT or XQuery in Stylus Studio®, you create a scenario. Scenarios allow you to define settings and options to associate with the XSLT or XQuery, such as the source file you want to transform or query, whether or not you want to perform any post-processing on the result, and so on. Let's check our XSLT's scenario properties to ensure that sampleBooksToCatalog.xsl is configured to use a Java XSLT processor (as opposed to say, MSXML). You access the XSLT scenario properties by clicking the browse [...] button next to the scenario name. Select the Processor tab and display the Processor drop-down list, as shown here:
The Stylus Studio® XML Java Code Generator works with the several popular Java XSLT processors, including the Saxon XSLT processor. (For XQuery, you can use the Saxon XQuery Processor and DataDirect XQuery processors.) Select the processor that you are using in your application, and click OK to save the change. Now, you're ready to generate some Java code.
Select XSLT > Generate Java Code... from the Stylus Studio® menu to open the code generation dialog box.
Stylus Studio®'s Java Code Generator provides multiple code generation options to let you customize the code being generated for you.
The first time you generate Java code, you'll need to fill in the Target directory field to specify where to output the
.java file. You can optionally specify the Package name field. By default, Stylus Studio® uses the file name for the
generated class name, but you can change the value of the Class name field if you want. Other options
let you specify whether or not you want to make the generated class public, whether or not you want to generated
.java file to include the
Once the generated .java file (.ZIP, 4KB) is done, Stylus Studio® automatically opens it in the built-in Java Editor. From there you can easily run the Java code by selecting Java > Run from the menu, or you can step through with the Stylus Studio® Java debugger. We’ll run the Java code shortly, but let's first take a peek at the classpath your new Java file requires by selecting Project > Set classpath from the Stylus Studio® menu.
So Many Jars... so Little Time
After you have created your XSLT or XQuery and generated the Java code for it, you can use Project Classpath dialog box (again, select Project > Set classpath from the Stylus Studio® menu) to see what the jar files Stylus Studio® has put in the classpath. When you run the .java file as a standalone application, just copy/paste the same jar files in your classpath, and the application will run perfectly.
The Scenario Properties Dialog Box
The Scenario Properties dialog box lets you specify options for running your XSLT or XQuery, including the following, all of which are reflected in the generated Java code. You can use scenario properties to specify:
One of the key highlights of the Java Code generator is its unique ability to generate the code needed to seamlessly access a wide variety of different data sources. If you have checked Use Stylus URI Resolver on the Scenario Properties dialog box Processor tab (it is selected by default), you can generate the Java code needed to directly access any of the Stylus Studio® file systems, including:
The Stylus Studio® URI Resolver
The XSLT or XQuery processor needs to know how to resolve a URL whenever it needs to open an external file, including:
The processor knows how to resolve standard URLs like
Support for Multiple Scenarios in the Same Java Application
The Stylus Studio® Scenario Properties dialog box lets you create several scenarios for the same XSLT or XQuery program. This feature is fully integrated with the Java Code Generator, allowing you to easily and simultaneously create Java code for any scenarios you select. If you have defined multiple XSLT several scenarios (each one using a different processor or XML Schema for validation, for example), each scenario is listed in the Java Code Generation dialog box; simply check the boxes for the scenarios you want included in the Java program.
The Java code generated by Stylus Studio® includes a
Embedding Your Program in a Larger Application
As described earlier, you can optionally have Stylus Studio® include the
Stylus Studio® Java Code Generator in a Nutshell
Stylus Studio® X15 Release 2 XML Enterprise Suite provides powerful XML tools to develop, test, debug, and now, deploy your XML applications by generating the code needed to execute XSLT and XQuery in your Java applications! Save hours of needless typing and debugging by downloading the Stylus Studio® Java Code Generator today!
PURCHASE STYLUS STUDIO ONLINE TODAY!!
Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!
Try Stylus XML & Java IDE
Download a free trial of our award-winning XML & Java IDE today!
Learn XQuery in 10 Minutes!
Say goodbye to 10-minute abs, and say Hello to "Learn XQuery in Ten Minutes!", the world's fastest and easiest XQuery primer, now available for free!
Check out this conversation with Jason Hunter on XML and JAVA Technologies