From XML to SVG : How to by programming

We saw in the previous chapter how to apply transformation from command line. In this chapter, we will explain how to apply transformation by programming.

1. What is needed

You still need the following things :

2. Example 1

2.1. First step

In this first step, we will code the example to set up the main architecture. During the second step, we will really build the list of paramaters needed by the Transformer.

2.1.1. Code

Save the following code under editor/test/ :


    public class XMLToSVGTest {
       * This method prepares the parameters needed by the process of transformation. If any
       * error happens, we display an error message and leave the application.
       * @return The list of parameters as an XMLToSVGParameters object.
      public static XMLToSVGParameters getParameters() {
        XMLToSVGParameters parameters = null;

        try {
          // The array containing the parameters
          String[] args = new String[0];


          parameters = new XMLToSVGParameters(args);
        } catch (Exception e) {
          // We report any error that might happen with the parameters and leave the application
          System.err.println("-- Something wrong with the parameters --");

        return parameters;

       * Main method.
      public static void main(String[] args) {
        try {
          // Prepares the list of parameters
          XMLToSVGParameters parameters = getParameters();

          // Creates the Transformer and calls the process() method that operates the process
          // of transformation
          new TransformerXMLToSVG(parameters).process();

          // We force the exit of the application because an instance of an AWT object has been
          // created during the process of transformation, thus, the system doesn't relinquish
          // control as it assumes that some GUI is running.
        } catch (Exception e) {
          // We report any error that might happen during the process of transformation
          System.err.println("-- Something wrong happened during the transformation --");

Open a console, go to directory editor/test and compile with the following command :

    javac -classpath ../lib/taxi.jar

For compiling, you must specify the jar containing the classes transformer.TransformerXMLToSVG and import imported at the very top of your code. These 2 classes are contained in the jar taxi.jar (in directory lib) shipped with TAXI.

Note : classes and sources are usually not saved in the same directory. This is not the case with the previous command line used for compiling. I chose to do that to focus only on the process of transformation and use the minimum number of necessary options on the command line.

2.1.2. Explaining the code

You must import 2 classes :

The method main(String[] args) does 2 things :

  1. prepares the XMLToSVGParameters object by calling the method getParameters(),
  2. instanciates the TransformerXMLToSVG (the constructor takes a XMLToSVGParameters object as parameter) and applies the tranformation by calling the method process() on that object.

A try catch block encloses this code as Exception may be thrown during the transformation.

We wrote a method called getParameters() to prepare the XMLToSVGParameters object that holds the different parameters and their respective value. In this method, we instanciate a XMLToSVGParameters object (the constructor takes an array of String objects that represents the transformation process' parameters and their value). We didn't specify yet the list of parameters in that array of String. We are going to do that in the Second step.

The creation of the XMLToSVGParameters is enclosed in a try catch block as it may throw Exception if something is wrong with the arrays of String representing the transformation process' parameters and their value. In such a case, we display an error message and stop the application.

The lines in bold in the code above are very important. When the process of transformation ends successfully, the Java Virtual Machine does not relinquish control, thus, we force the application to end with System.exit(0);. Why does not the Java Virtual Machine relinquish control ? Because an instance of a java.awt.Label may have been created by the class grp.FinalBoxTextContent to compute the size of the string contained by a tbox (a text box of the GRP document). Then, the Java Virtual Machine assumes that some GUI is running and keeps turning even when the end of the method main(String[] args) is reached.

2.1.3. Running the code

Run the example with this command (depending on the OS you run it, you will not use the same classpath separator) :

    java -classpath ../lib/taxi.jar;. XMLToSVGTest (under Windows)
    java -classpath ../lib/taxi.jar:. XMLToSVGTest (under other OS)

You get the following output :

    -- Something wrong with the parameters --
    Parameter 'defs' is missing.

In the method getParameters(), we got an Exception because, so far, the object XMLToSVGParameters is created with an empty array. We intercepted this Exception, showed its message, and left the application.

2.2. Second step

We will now complete the previous example by adding parameters to transform an XML document shipped with TAXI : editor/XML/XML/

2.2.1. Code