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.
You still need the following things :
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.
Save the following code under editor/test/XMLToSVGTest.java
:
import fr.loria.taxi.transformer.TransformerXMLToSVG; import fr.loria.taxi.transformer.util.XMLToSVGParameters; 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]; // HERE WE SHOULD FILL IN THE args ARRAY WITH THE LIST OF PARAMETERS AND THEIR VALUE 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 --"); System.err.println(e.getMessage()); System.exit(1); } 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. System.exit(0); } catch (Exception e) { // We report any error that might happen during the process of transformation System.err.println("-- Something wrong happened during the transformation --"); System.err.println(e.getMessage()); } } }
Open a console, go to directory editor/test
and compile
XMLToSVGTest.java
with the following command :
javac -classpath ../lib/taxi.jar XMLToSVGTest.java
For compiling, you must specify the jar containing the classes
transformer.TransformerXMLToSVG
and import fr.loria.taxi.transformer.util.XMLToSVGParameters
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.
You must import 2 classes :
transformer.TransformerXMLToSVG
: the Transformer used to apply the
transformation,transformer.util.XMLToSVGParameters
: holds the parameters passed to
the Transformer.The method main(String[] args)
does 2 things :
XMLToSVGParameters
object by calling the method
getParameters(),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.
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.
We will now complete the previous example by adding parameters to transform an XML
document shipped with TAXI : editor/XML/XML/ex1.cd
.