This section contains the following examples:
Notes
This simply transforms a populated array of Java objects (of type "Player") found in the Game object into an array of "Person" objects. To run, the classpath must include:
Code
Try
{
TMTransformer aTMTransformer =
TMTransformerFactory.getNewTMTransformer(this, projectName, 2);
TMJavaAdapter aSrcTMJavaAdapter =
TMBuiltInAdaptorFactory.makeNewReadTMJavaAdapter(aTMTransformer);
aTMTransformer.setSource(projectName,aSrcTMJavaAdapter);
TMJavaAdapter aTgtTMJavaAdapter = TMBuiltInAdaptorFactory.makeNewWriteTMJavaAdapter(aTMTransformer);
aTMTransformer.setTarget(projectName,aTgtTMJavaAdapter);
aTMTransformer.openProjectAndResources();
// add some data to the read adapter - this can be done before
// this point if required
// here we could use aReadAdapter.addObject(aGame) but then the
// only objects known to the read adapter by name are the Game
// objects.
// if you want to transform from Person then we need to tell about
// the PERSON objects
// we could add them one a time using addObject or better still add
// the whole array using addObjects
aSrcTMJavaAdapter.addObjects(aGame.getPlayers());
aTMTransformer.setTxtLogging(System.out, TMC.eLogNONE);
// run the transfrom and get the result object
aTMTransformer.runAllTransforms();
// the file xmltarget.xml will have been created
SimpleUtilities.showJavaObjects(aTgtTMJavaAdapter, "Player");
}
catch (TMException aTMException)
{
System.out.println(aTMException.toString());
return;
}
Java to XML (as serialised file)
This example of Java to XML (as a serialised file) simply transforms a populated array of Java objects (of type "Player") found in the Game object into an XML file. To run, the classpath must include:
In this example, the file "xmltarget.xml" will be created in the current directory.
Code
TMXMLAdapter aTMXMLAdapter = null;
try
{
TMTransformer aTMTransformer = TMTransformerFactory.getNewTMTransformer(this, projectName, 2);
TMJavaAdapter aTMJavaAdapter =
TMBuiltInAdaptorFactory.makeNewReadTMJavaAdapter(aTMTransformer);
aTMTransformer.setSource(projectName,aTMJavaAdapter);
aTMXMLAdapter=TMBuiltInAdaptorFactory.makeNewWriteTMXMLAdapter(aTMTransformer);
aTMTransformer.setTarget(projectName,aTMXMLAdapter);
Map params = new HashMap();
params.put(TMXMLAdapter.NAME_URL, AllExamplesInJava.EXAMPLE_TGT_PATH + "xmltarget.xml");
aTMXMLAdapter.setInstanceConnectionData(params);
aTMTransformer.openProjectAndResources();
// add some data to the read adapter
aTMJavaAdapter.addObjects(aGame.getPlayers());
aTMTransformer.setTxtLogging(System.out, TMC.eLogNONE);
aTMTransformer.runAllTransforms();
// the file xmltarget.xml will have been created
}
catch (TMException aTMException)
{
aTMException.printStackTrace();
return;
}
Java to XML (as DOM in memory)
This example simply transforms a populated array of Java objects (of type "Player") found in the Game object into a org.w3c.dom.Document object (or DOM). To run, the classpath must include:
The transform returns an org.w3c.dom.Document object.
Code
TMTMXMLAdapter aTMXMLAdapter = null;
Document aDocument = null;
try
{
TMTransformer aTMTransformer = TMTransformerFactory.getNewTMTransformer(this, projectName, 2);
TMJavaAdapter aTMJavaAdapter = TMBuiltInAdaptorFactory.makeNewReadTMJavaAdapter(aTMTransformer);
aTMTransformer.setSource(projectName,aTMJavaAdapter);
// Here we DON'T specify a file to write to as we are going to ask
// for the DOM output
aTMXMLAdapter=TMBuiltInAdaptorFactory.makeNewWriteTMXMLAdapter(aTMTransformer);
aTMTransformer.setTarget(projectName,aTMXMLAdapter);
aTMTransformer.openProjectAndResources();
// add some data to the read adapter
aTMJavaAdapter.addObjects(aGame.getPlayers());
aTMTransformer.setTxtLogging(System.out, TMC.eLogNONE);
aTMTransformer.runAllTransforms();
aDocument = aTMXMLAdapter.getXMLDocument();
}
catch (TMException aTMException)
{
aTMException.printStackTrace();
return;
}
Java to XML (as serialised memory output)
This example transforms a populated an array of Java objects (of type "Player") found in the Game object into an serialised version of the XML text. To run, the classpath must include:
The output returns as an OutputStream.
Code
TMXMLAdapter aTMXMLAdapter = null;
try
{
TMTransformer aTMTransformer = TMTransformerFactory.getNewTMTransformer(this, projectName, 2);
TMJavaAdapter aTMJavaAdapter =
TMBuiltInAdaptorFactory.makeNewReadTMJavaAdapter(aTMTransformer);
aTMTransformer.setSource(projectName,aTMJavaAdapter);
// but we DON'T specify a file to write to as we are going to ask
// for the serializeStream output
aTMXMLAdapter=TMBuiltInAdaptorFactory.makeNewWriteTMXMLAdapter(aTMTransformer);
aTMTransformer.setTarget(projectName,aTMXMLAdapter);
aTMTransformer.openProjectAndResources();
// add some data to the read adapter
aTMJavaAdapter.addObjects(aGame.getPlayers());
aTMTransformer.setTxtLogging(System.out, TMC.eLogNONE);
aTMTransformer.runAllTransforms();
aTMXMLAdapter.serializeStream(System.out);
}
catch (TMException aTMException)
This XML (as serialised file) to Java example simply transforms an XML file into an array of "Person" objects. To run the classpath must include:
Code
TMJavaAdapter aTMJavaAdapter = null;
try
{
TMTransformer aTMTransformer = TMTransformerFactory.getNewTMTransformer(this, projectName, 2);
TMJDBCAdapter aTMJDBCAdapter =
TMBuiltInAdaptorFactory.makeNewReadTMJDBCAdapter(aTMTransformer);
aTMTransformer.setSource(projectName,aTMJDBCAdapter);
aTMJavaAdapter=TMBuiltInAdaptorFactory.makeNewWriteTMJavaAdapter(aTMTransformer);
aTMTransformer.setTarget(projectName,aTMJavaAdapter);
Map params = new HashMap();
params.put(TMJDBCAdapter.NAME_URL,theURL);
params.put(TMJDBCAdapter.NAME_DRIVER,TMC.sDC_ACCESS_DRV);
aTMJDBCAdapter.setInstanceConnectionData(params);
aTMTransformer.openProjectAndResources();
aTMTransformer.runAllTransforms();
}
Source file
The XML source file for the examples looks like:
<Game>
<Weapon name ="ROPE" colour = "BROWN"/>
<Weapon name ="REVOLVER" colour = "SILVER"/>
<Weapon name ="REVOLVER" colour = "BLACK"/>
<Weapon name ="SPANNER" colour = "METALLIC"/>
<Weapon name ="KNIFE" colour = "RED"/>
<Weapon name ="BASEBALL BAT" colour = "WOOD-EFFECT"/>
<Weapon name ="FLICK-KNIFE" colour = "GREEN TORTOISE"/>
<Weapon name ="BRICK" colour = "ORANGE"/>
<Room name = "BILLIARD ROOM"/>
<Room name = "DRAWING ROOM"/>
<Room name = "KITCHEN"/>
<Room name = "DINING ROOM"/>
<Room name = "LOUNGE"/>
<Room name = "BEDROOM"/>
<Room name = "BATHROOM"/>
<Room name = "LIVING ROOM"/>
<Person title = "Miss" name ="SCARLET" man ="false" roomName = "BILLIARD ROOM"/>
<Person title = "Colonel" name ="MUSTARD" man ="true" roomName= "DRAWING ROOM"/>
<Person title = "Prof" name = "PLUM" man ="true" roomName = "KITCHEN"/>
<Person title = "Mrs" name = "PEACOCK" man ="false" roomName = "DINING ROOM"/>
<Person title = "Mrs" name = "WHITE" man ="false" roomName = "LOUNGE"/>
<Person title = "Rev" name = "GREEN" man ="true" roomName = "BEDROOM"/>
<Person title = "Dr" name = "BLACK" man ="true" roomName = "LIVING ROOM"/>
</Game>
XML (as W3C Document ) to Java
This XML (as W3C Document ) to Java example simply transforms an XML file into an array of "Person" objects. To run, the classpath must include:
The code must also provide a populated org.w3c.dom.Document called aDocument;
Code
TMJavaAdapter aTgtTMAdapter=null;
try
{
TMTransformer aTMTransformer = TMTransformerFactory.getNewTMTransformer(this,projectName,3);
TMAdapter aSrcTMAdapter = TMBuiltInAdaptorFactory.makeNewReadTMXMLAdapter(aTMTransformer);
aTgtTMAdapter = TMBuiltInAdaptorFactory.makeNewWriteTMJavaAdapter(aTMTransformer);
aTMTransformer.setSource(projectName,aSrcTMAdapter);
aTMTransformer.setTarget(projectName,aTgtTMAdapter);
Document aDocument = SimpleUtilities.getDomFromFile(AllExamplesInJava.EXAMPLE_SRC_PATH+"xmlsource.xml");
Map srcConnectionData = new HashMap();
srcConnectionData.put(TMXMLAdapter.NAME_ORG_W3C_DOM_DOCUMENT,aDocument);
aSrcTMAdapter.setInstanceConnectionData(srcConnectionData);
aTMTransformer.openProjectAndResources();
aTMTransformer.runAllTransforms();
XML (as inputStream ) to Java
This XML (as inputStream) to Java example simply transforms an XML file into an array of "Person" objects. To run, the classpath must include:
The code must also provide an InputStream containing a valid relevant XML serialization ;
Code
try
{
FileInputStream aFileInputStream = new FileInputStream(AllExamplesInJava.EXAMPLE_SRC_PATH + "xmlsource.xml");
TMTransformer aTMTransformer = TMTransformerFactory.getNewTMTransformer(this,projectName,3);
TMXMLAdapter aTMXMLAdapter = TMBuiltInAdaptorFactory.makeNewReadTMXMLAdapter(aTMTransformer);
aTMTransformer.setSource(projectName,aTMXMLAdapter);
aWriteAdapter = TMBuiltInAdaptorFactory.makeNewWriteTMJavaAdapter(aTMTransformer);
aTMTransformer.setTarget(projectName,aWriteAdapter);
Map param = new HashMap ();
param.put(TMXMLAdapter.NAME_JAVA_IO_INPUTSTREAM,aFileInputStream);
aTMXMLAdapter.setInstanceConnectionData(param);
aTMTransformer.openProjectAndResources();
aTMTransformer.runAllTransforms();
}
catch (TMException e)
This Java to Database URL example simply transforms a populated array of Java objects (of type "Player") found in the Game object into a database identified by URL, name and password. To run, the classpath must include:
Code
TMTransformer aTMTransformer = TMTransformerFactory.getNewTMTransformer(this, projectName, 2);
TMJavaAdapter aTMJavaAdapter=TMBuiltInAdaptorFactory.makeNewReadTMJavaAdapter(aTMTransformer);
aTMTransformer.setSource(projectName,aTMJavaAdapter );
TMJDBCAdapter aTMJDBCAdapter=TMBuiltInAdaptorFactory.makeNewWriteTMJDBCAdapter(aTMTransformer);
aTMTransformer.setTarget(projectName,aTMJDBCAdapter);
Map params = new HashMap();
params.put(TMJDBCAdapter.NAME_URL,theURL);
params.put(TMJDBCAdapter.NAME_DRIVER,TMC.sDC_ACCESS_DRV);
// most data bses also require
// params.put(TMJDBCAdapter.NAME_USER ...
// params.put(TMJDBCAdapter.NAME_PASSWORD ...
aTMJDBCAdapter.setInstanceConnectionData(params);
aTMTransformer.openProjectAndResources();
aTMJavaAdapter.addObjects(aGame.getPlayers());
aTMTransformer.runAllTransforms();
// the RDBMS adapter will have flushed the data and closed the connection
This Java to Open RDBMS example simply transforms a populated array of Java objects (of type "Player") found in the Game object into a database via an open database connection. To run, the classpath must include:
Code
Connection aConnection = SimpleUtilities.getConnection(theURL);
try
{
// set the system up to run
TMTransformer aTMTransformer = TMTransformerFactory.getNewTMTransformer(this, projectName, 2);
TMJavaAdapter aTMJavaAdapter =
TMBuiltInAdaptorFactory.makeNewReadTMJavaAdapter(aTMTransformer);
aTMTransformer.setSource(projectName,aTMJavaAdapter);
TMJDBCAdapter aTMJDBCAdapter =
TMBuiltInAdaptorFactory.makeNewWriteTMJDBCAdapter(aTMTransformer);
aTMTransformer.setTarget(projectName,aTMJDBCAdapter);
Map params = new HashMap();
params.put(TMJDBCAdapter.NAME_SQL_CONNECTION,aConnection);
aTMJDBCAdapter.setInstanceConnectionData(params);
aTMTransformer.openProjectAndResources();
// add some data to the read adapter
aTMJavaAdapter.addObjects(aGame.getPlayers());
aTMTransformer.runAllTransforms();
}
catch (TMException ...
Database URL to Java
This example transforms from a database to an array of Java objects. To run, the classpath must include:
The URL, name password must refer to a database accessible via JDBC.
Code
TMTransformer aTMTransformer = TMTransformerFactory.getNewTMTransformer(this, projectName, 2);
TMJDBCAdapter aTMJDBCAdapter=TMBuiltInAdaptorFactory.makeNewReadTMJDBCAdapter(aTMTransformer);
aTMTransformer.setSource(projectName,aTMJDBCAdapter);
aTMJavaAdapter=TMBuiltInAdaptorFactory.makeNewWriteTMJavaAdapter(aTMTransformer);
aTMTransformer.setTarget(projectName,aTMJavaAdapter);
Map params = new HashMap();
params.put(TMJDBCAdapter.NAME_URL,theURL);
params.put(TMJDBCAdapter.NAME_DRIVER,TMC.sDC_ACCESS_DRV);
aTMJDBCAdapter.setInstanceConnectionData(params);
aTMTransformer.openProjectAndResources();
aTMTransformer.runAllTransforms();
This Open RDBMS Connection to Java example simply transforms from the player items found via the open JDBC connection to an array of "Person" objects. To run the classpath must include:
The connection must be an open JDBC Connection object/.
Code
Connection aConnection = SimpleUtilities.getConnection(theURL);
TMJavaAdapter aTMJavaAdapter;
try
{
TMTransformer aTMTransformer = TMTransformerFactory.getNewTMTransformer(this, projectName, 2);
TMJDBCAdapter aTMJDBCAdapter =
TMBuiltInAdaptorFactory.makeNewReadTMJDBCAdapter(aTMTransformer);
aTMTransformer.setSource(projectName,aTMJDBCAdapter);
aTMJavaAdapter=TMBuiltInAdaptorFactory.makeNewWriteTMJavaAdapter(aTMTransformer);
aTMTransformer.setTarget(projectName,aTMJavaAdapter);
Map params = new HashMap();
params.put(TMJDBCAdapter.NAME_SQL_CONNECTION,aConnection);
aTMJDBCAdapter.setInstanceConnectionData(params);
aTMTransformer.openProjectAndResources();
aTMTransformer.runAllTransforms();
}
catch (TMException e)
Java to Java (with Error Handler)
This example is the same as the first Java to Java example except that it introduces an error handler.
If no error handlers are defined, then errors are regarded as unhandled and sent to the general purpose output text stream. Once an error handler has been added, the error is transformed by the code in the error handler and placed into the output device specified by the error handler (which maybe XML, Java or a database). The error will not be sent to the general purpose output text stream.
Any number of error handlers may be defined. Every error is handled by every error handler in turn. The only exception to this is if an error should occur within an error handler. In this case the error is not handled by the error handler which threw the exception but by all the other error handlers .
It is important not to confuse the concept of an error handler - that is to say a project dedicated to the transformation of errors from TMError into any output target - with the code contained within an SML catch error block. The two concepts are quite separate. Thus if both an SML catch error block and an error handler exist then error is processed in the following manner:
Note also that Error Handlers of registered with a given TMTransformer object. Thus where the TMTransformer object creates other TMTransformer objects during the course of the execution (see next example) then those new TMTransformer objects will not inherit currently registered Error Handlers
Code
TMJavaAdapter aTgtTMJavaAdapter = null;
try
{
TMTransformer aTMTransformer = TMTransformerFactory.getNewTMTransformer(this, projectName, 2);
TMJavaAdapter aSrcTMJavaAdapter = TMBuiltInAdaptorFactory.makeNewReadTMJavaAdapter(aTMTransformer);
aTMTransformer.setSource(projectName,aSrcTMJavaAdapter);
aTgtTMJavaAdapter = TMBuiltInAdaptorFactory.makeNewWriteTMJavaAdapter(aTMTransformer);
aTMTransformer.setTarget(projectName,aTgtTMJavaAdapter);
// add an error handler - it only needs a target
aTMTransformer.addProject(errorHandlerProjectName, 1, true);
TMXMLAdapter errorHandlerOutput = TMBuiltInAdaptorFactory.makeNewWriteTMXMLAdapter(aTMTransformer);
aTMTransformer.setTarget(errorHandlerProjectName,errorHandlerOutput);
Map params = new HashMap();
params.put(TMXMLAdapter.NAME_URL,"errorExample.xml");
errorHandlerOutput.setInstanceConnectionData(params);
aTMTransformer.openProjectAndResources();
// add some data to the read adapter
TMJavaAdapter aReadAdapter = (TMJavaAdapter) aTMTransformer.getReadTMAdapter();
// here we could use aReadAdapter.addObject(aGame) but then the only objects
aReadAdapter.addObjects(aGame.getPlayers());
aTMTransformer.setTxtLogging(System.out, TMC.eLogNONE);
aTMTransformer.runAllTransforms();
}
catch (TMException...
Java to Java (with embedded objects)
This example expands the Java to Java example to show how an embedded XML stream may be forwarded from one transformation to another. This is typical of the techniques used to gain access to any embedded XML document placed within a JMS message.
Basic Java Code
This is identical to the Java to Java example. The only difference in the distributed examples is that the "game" object contains a populated XML string in addition to its other data.
SML Code
The SML code contained logic to discover the presence of the XML String and to send it to a Java function defined within translation builder.
This is the SML code:
TRANSFORM (Location) <- (Weapon)
IF (myName='XML-Data') THEN
X:=23;
LOGS('Calling out!');
myName := MakeDOM(myColour);
ELSE
myName := myColour+CHARAT(myName,2);
END_IF;
The function makeDOM has the following body. The header section in italics is automatically generated by TM Design Tool. The rest of the function is basically boiler plate code to get the initial user object passed into to the first transformation and then invoke a further transformation upon at code.
public static java.math.BigDecimal MakeDOM(
TMContext theTMContext,
java.lang.String P1) throws Exception
{
// get the theTMTransformer that started this transform
TMTransformer theTMTransformer = theTMContext.getTMTransformer();
// now get the user's object set when they started the TMTransformer
// cast it to the type we set
JavatoJavaEmbeddedXML aUserObjct = (JavatoJavaEmbeddedXML)
theTMTransformer.getUserReferenceObject();
// get the current write adapter
TMJavaAdapter aTMJavaAdapter=theTMTransformer.getWriteTMJavaAdapter();
// start another transform - with same target and the source is the
// String containing the embedded XML
aUserObject.runExampleForXML(P1,aTMJavaAdapter); //user code
return null;
}
The method runExampleForXML is now able to create another TMTransformer object in the conventional manner. Apart from using the method setTargetAdapterFromExistingAdapter to allow it to use the existing target adapter there is nothing special about this Java code - it follows the same pattern as the previous Java code.
Note that any TMException's which are thrown from this transformation will be sent back to the calling project and maybe caught by the Error Handlers defined in the calling project.
Code
public void runExampleForXML(String P1, TMJavaAdapter aTMJavaAdapter)
{
String projectName = "XMLToJava";
TMJavaAdapter aWriteAdapter = null;
try
{
TMTransformer aTMTransformer = TMTransformerFactory.getNewTMTransformer(this, projectName, 3);
StringReader aStringReader = new StringReader(P1);
Document aDocument = makeDomObj(P1);
TMXMLAdapter aTMXMLAdapter = TMBuiltInAdaptorFactory.makeNewReadTMXMLAdapter(aTMTransformer);
aTMTransformer.setSource(projectName,aTMXMLAdapter);
Map param = new HashMap ();
param.put(TMXMLAdapter.NAME_ORG_W3C_DOM_DOCUMENT,aDocument);
aTMXMLAdapter.setInstanceConnectionData(param);
aTMTransformer.setTargetAdapterFromExistingAdapter(null, aTMJavaAdapter);
aTMTransformer.openProjectAndResources();
aTMTransformer.runAllTransforms();
}
catch (TMException aTMException)
{
System.out.println(aTMException.toString());
return;
}
}
This example transforms from an array of Java objects to a set of CSV files. The CSV is written using the class 'samples.openinterfaces.csv.write.TmExampleCSVWriteAdapter' which is also supplied in the
distrbution.
TMTransformer aTMTransformer = TMTransformerFactory.getNewTMTransformer(this, projectName, 1);
TMJavaAdapter aTMJavaAdapter = TMBuiltInAdaptorFactory.makeNewReadTMJavaAdapter(aTMTransformer);
aTMTransformer.setSource(projectName,aTMJavaAdapter);
TMAdapter aTMAdapter = new samples.openinterfaces.csv.write.TmExampleCSVWriteAdapter();
aTMTransformer.setTarget(projectName,aTMAdapter);
Map params = new HashMap();
params.put(samples.openinterfaces.csv.write.TmExampleCSVWriteAdapter.
DIRECTORY, AllExamplesInJava.EXAMPLE_TGT_PATH+"csv");
aTMAdapter.setInstanceConnectionData(params);
aTMTransformer.openProjectAndResources();
// add some data to the read adapter - this can be done before this
// point if required
aTMJavaAdapter.addObjects(aGame.getPlayers());
aTMTransformer.setTxtLogging(System.out, TMC.eLogNONE);
aTMTransformer.runAllTransforms();
Generic to Java
This example transforms from a set of CSV files to an array of Java objects. The CSV is read using the class 'samples.openinterfaces.csv.read.TmExampleCSVReadAdapter' which is also supplied in the distribution.
TMTransformer aTMTransformer = TMTransformerFactory.getNewTMTransformer(this, projectName, 1);
TMAdapter aTMAdapter = new samples.openinterfaces.csv.read.TmExampleCSVReadAdapter();
aTMTransformer.setSource(projectName,aTMAdapter);
TMJavaAdapter aTMJavaAdapter = TMBuiltInAdaptorFactory.makeNewWriteTMJavaAdapter(aTMTransformer);
aTMTransformer.setTarget(projectName,aTMJavaAdapter);
Map params = new HashMap();
params.put(samples.openinterfaces.csv.read.TmExampleCSVReadAdapter.
DIRECTORY, AllExamplesInJava.EXAMPLE_SRC_PATH+"csv");
params.put(samples.openinterfaces.csv.read.TmExampleCSVReadAdapter.
EXTENSION, "csv");
aTMAdapter.setInstanceConnectionData(params);
aTMTransformer.openProjectAndResources();
aTMTransformer.setTxtLogging(System.out, TMC.eLogNONE);
aTMTransformer.runAllTransforms();