Debugging Java Files

 

Support for debugging Java extensions is available only in Stylus Studio XML Professional Edition.

The Stylus Studio debugger allows you to follow Java processing as well as XSLT processing. With the Stylus Studio debugger, you can observe the interaction between your Java code and XML data.

When you debug a transformation, the transformation can include the processing of Java files. Such Java files might be servlets, server extensions, extension functions, or other kinds of Java programs that involve stylesheets. If you need to make a change to your Java file, you can compile it right in Stylus Studio. Click Compile in the upper left corner of the Java file window. Stylus Studio automatically saves the file before it compiles it.

This section discusses the following topics:

Requirements for Java Debugging

If you want to use Stylus Studio to debug Java code, you must have the Sun Java Runtime Environment (JRE) 1.4.x installed. If you want to use Stylus Studio to assist you in editing and compiling Java code, you must have the Sun JDK 1.4.x installed.

You can download the Sun Java products from http://www.javasoft.com/j2se/.

After you install the JRE, you must run the Stylus Studio auto-detect feature.

To run the auto-detect feature:
1. Select Tools > Options from the Stylus Studio menu bar.
2. In the Options dialog box, click Java Virtual Machine.
3. In the Java Virtual Machine page, click Auto detect.

Also, in the Parameters field of the Java Virtual Machine page, there should be something like the following:

-Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket, 
server=y,suspend=n,address=8000 -Djava.compiler=NONE
               

            

To confirm that your set up is correct, select Help > About Stylus Studio from the Stylus Studio menu bar. The About Stylus Studio dialog box should indicate that the JVM is running in debug mode.

Setting Options for Debugging Java

You can specify the following options when you use Stylus Studio to debug Java code:

  • Source Path is the path that Stylus Studio uses to locate the source files during debugging.
  • Prompt user for source file path confirmation indicates that if Stylus Studio cannot find the source files you are debugging, it prompts you to specify the source file path. If you do not set this option, and Stylus Studio cannot find a source file, the behavior varies according to what the debugger is trying to do. For example, if the debugger is stepping into an instruction that calls a function that is defined in a Java file that Stylus Studio cannot find, the debugger steps over the instruction.
  • Never step into classes starting with one of the following strings of characters: contains a list of classes, one on each line, that you do not want to step into. For example, these might be classes that are part of the core language, or classes that you do not have source files for. If you specify java.lang, the debugger skips all classes whose names start with java.lang, for example, java.lang.String and java.lang.Object.
  • JVM communication time out indicates the amount of time that Stylus Studio waits for a response from the JVM. If Stylus Studio does not receive a response in the specified amount of time, it stops trying to communicate with the JVM. The default is 5 seconds.
  • Show JDWP Events in the Output Window indicates whether you want Stylus Studio to log all communication events in the Stylus Studio Output Window during a debugging session.

Stylus Studio also allows you to set options that specify the Java virtual machine (JVM) you use. You can specify the run-time library, the home directory, and parameters for starting the JVM. Select Tools > Options from the Stylus Studio menu bar.

See Specifying Stylus Studio Options for instructions for setting these options.

Using the Java Editor

To use the Stylus Studio Java editor, open a Java file in Stylus Studio.

To specify arguments that Stylus Studio uses to run the active Java class, select Java > Class Properties from the Stylus Studio menu bar. Stylus Studio displays the Class Properties dialog box. Enter the arguments required to run your code. (You must have a Java file open in Stylus Studio for Java to appear in the menu bar.)

The same debugging capabilities that are available when you are debugging XSLT stylesheets are available when you are debugging stand-alone Java applications.

When you use the Java editor, the Sense:X auto-completion feature is available. The Java editor browses your import directives to gather information about the packages you are using and provides auto-completion when using methods or data members defined in imported classes. The auto-completion mechanism also provides you with tips about the signature of the class method and its required arguments. The same applies to the classes that you are editing. Also, the CLASSPATH is used to help you auto-complete import directives. Type Ctrl+Space if you want Stylus Studio to auto-complete keywords and class names that are defined in java.lang.package.

The Stylus Studio Java editor also does background error checking. As you type Java code, Stylus Studio displays red lines that indicate syntax errors. Move the cursor over the red line to display a pop-up error message.

When you use the Java editor, you can configure the character encoding that Stylus Studio uses to save and load files. To do this, ensure that a Java file is the active file. Then select Edit > Change Encoding from the Stylus Studio menu bar.

Context-sensitive help for your Java classes is available in the Java editor. The directory that contains the javadoc-generated documentation must be in the Stylus Studio class path (in the Stylus Studio menu bar select Tools > Options > Java Virtual Machine) or in your CLASSPATH environment variable. You can then press F1 when your cursor is on a class name in the Java editor. Stylus Studio opens the related javadoc-generated documentation.

Stylus Studio and JVM

Stylus Studio allows you to debug a running application. You can attach Stylus Studio to a local or remote JVM, and run your application in debug mode. In the Stylus Studio tool bar, click Attach to debug a standalone Java program that is running an external JVM. ( Attach is not for debugging Java extensions.)

To execute a class, open the Java source in Stylus Studio and press F5. Of course, the class must be in your CLASSPATH environment variable or in the Stylus Studio ClassPath (select Tools > Options > Java Virtual Machine).

To verify the JVM that Stylus Studio is trying to load:
1. Select Tools > Options from the Stylus Studio menu bar.
2. In the Options dialog box that appears, expand Application Settings and click Java Virtual Machine.

The Home Directory field indicates the version of the JVM.

When you suspend processing, display the Output Window to view any output from the Java virtual machine. To display the Output Window, click Output Window in the Stylus Studio tool bar.

Example of Debugging Java Files

Stylus Studio includes sample files that you can experiment with to learn how to use the debugger with an application that includes stylesheets and Java files. To get you started, this section provides step-by-step instructions for using the debugger with these sample files. You should perform the steps in each topic in the order of the topics.

For complete information about how to use the debugger, see Chapter 6Debugging Stylesheets.

This section includes the following topics:

Setting Up to Debug Sample Java/XSLT Application

To set up Stylus Studio to debug the sample Java/XSLT application:
1. From the Stylus Studio menu bar, select Tools > Options.
2. In the Options dialog box that appears, expand Application Settings and click Java Virtual Machine.
3. If the examples\javaExtension directory is already in the ClassPath field, click OK.

If the examples\javaExtension directory is not in the ClassPath field, click Browse next to the ClassPath field. In the Browse for Folder dialog box that appears, navigate to and select the javaExtension directory, which is in the examples directory of your Stylus Studio installation directory. Click OK. Restart Stylus Studio. For ClassPath changes to take effect, you must restart Stylus Studio whenever you modify the ClassPath field. For information about the relationship between the CLASSPATH environment variable and this field, see About Java Virtual Machine Options.

4. In the Stylus Studio tool bar, select File > Open.
5. In the Open dialog box that Stylus Studio displays, navigate to the examples\javaExtension directory in your Stylus Studio installation directory.
6. Double-click IntDate.xsl.

Stylus Studio opens the IntDate.xsl stylesheet in the XSLT editor. The tree for the XML source document, IntDate.xml, also appears.

7. In the XSLT editor tool bar, click Preview Result .

The IntDate scenario has already been defined. Stylus Studio applies the stylesheet and displays the results (a list of dates) in the Preview window.

Inserting a Breakpoint in the Sample Java/XSLT Application

This topic is part of a sequence that starts with Setting Up to Debug Sample Java/XSLT Application.

u To insert a breakpoint in the sample stylesheet:
1. In the XSLT editor, examine the template that matches the date element.

As you can see, the select attribute in the xsl:value-of instruction invokes the IntDate Java extension function.

2. In the body of the template, click just before the xsl:value-of instruction.
3. In the Stylus Studio tool bar, click Toggle Breakpoint .
4. Press F5 to apply the stylesheet.

Alternative: In the Stylus Studio tool bar, click Start Debugging .

The XSLT processor suspends processing at the breakpoint, displays a yellow triangle to indicate where processing has been suspended, and displays a message in the Preview window.

Gathering Debug Information About the Sample Java/XSLT Application

This topic is part of a sequence that starts with Setting Up to Debug Sample Java/XSLT Application.

To obtain debug information:
1. In the Stylus Studio tool bar, click Step into or press F11.

Stylus Studio opens and displays the Java source file that contains the IntDate extension function. Now the Variables window displays a list of the variables in the extension function. There is still no output in the Preview window.

Stylus Studio might display the Browse For Folder dialog box. It is prompting you to specify where it can find the Java source file that contains the extension function invoked in the line that has the breakpoint. Stylus Studio does not display the Browse for Folder dialog box when the .java file is in the same directory as the .class file. Click the javaExtension directory and click OK.

2. In the Stylus Studio tool bar, click Output Window .

Stylus Studio displays the Output Window, which displays output from the Java virtual machine.

3. In the Stylus Studio tool bar, click Step Over or F10 to move to the next line of Java code.

The yellow triangle moves to show the new location. If the values of the variables change, the Variables window reflects this.

4. Press Step Out to return to the stylesheet.

The Variables window now displays only the context node. Processing was suspended when the second date child element of the doc document element was the context node.

The Preview window now displays a few lines of HTML.

5. In the Preview window, click in the first line of text.

Stylus Studio displays the Backmap Stack window, which contains a list of the XSLT instructions that have been executed. Also, in the XSLT Source tab, Stylus Studio displays a blue triangle that indicates the line in the stylesheet that generated the output line you clicked in.

 
Free Stylus Studio XML Training: