![]() |
![]() |
|
|
Using the Run-Time Component
The run-time component of XML Translator consists of a Java class named WLXT. This class has various methods used to translate data between binary and XML formats. This Java class can be deployed in an EJB using BEA WebLogic Server, invoked from a workflow in BEA WebLogic Process Integrator, or integrated into any Java application.
The XML Translator class provides several parse() methods that translate binary data into XML. XML Translator also provides several serialize() methods that translate XML data to a binary format. Binary data formats are described via MFL documents. XML Translator uses MFL documents to read and write binary data to or from XML. MFL documents are specified by a URL in a parse() or serialize() method. The code samples below illustrate how to use XML Translator to parse binary data into XML, and serialize XML into binary.
Binary to XML
The following code listing uses the parse() method of XML Translator to parse a file containing binary data into XML.
Listing 6-1 Sample Binary to XML Parse() Method
1 import com.bea.wlxt.*;
2 import org.w3c.dom.Document;
3 import java.io.FileInputStream;
4 import java.net.URL;
5
6 public class Example
7 {
8 public static void main(String[] args)
9 {
10 try
11 {
12 WLXT wlxt = new WLXT();
13 URL mflDocumentName = new URL("file:mymfl.mfl");
14 FileInputStream in = new FileInputStream("mybinaryfile");
15
16 Document doc = wlxt.parse(mflDocumentName, in, null);
17 String xml = wlxt.getXMLText(doc, 0, 2);
18 System.out.println(xml);
19 }
20 catch (Exception e)
21 {
22 e.printStackTrace(System.err);
23 }
24 }
25}
In the prior listing, a new instance of the XML Translator class is instantiated at line 12. A Uniform Resource Locator (URL) is created for a MFL file that was previously created with Format Builder. A FileInputStream is created for some binary data that exists in the file mybinaryfile. The URL for the MFL document, and the stream of binary data, are then passed into the parse method of XML Translator at line 16. The parse method converts the binary data into an instance of a W3C Document object. This object can be converted to XML text via XML Translator getXMLText() method (as shown on line 17), or manipulated directly via the W3C DOM API.
Generating XML with a Reference to a DTD
WXLT also includes parse() methods that allow a reference to a Document Type Definition (DTD) or an XML Schema to be output in the resulting XML document. The following listing illustrates this generation.
Listing 6-2 Sample XML Generation with a DTD Reference Code Example
1 import com.bea.wlxt.*;
2 import org.w3c.dom.Document
3 import java.io.FileInputStream;
4 import java.net.URL;
5
6 public class Example2
7 {
8 public static void main(String[] args)
9 {
10 try
11 {
12 WLXT wlxt = new WLXT();
13 URL mflDocumentName = new URL("file:mymfl.mfl");
14 FileInputStream in = new FileInputStream("mybinaryfile");
15
16 Documentdoc=wlxt.parse(mflDocumentName,in,"mydtd.dtd",
17 null);String xml = wlxt.getXMLText(doc, 0, 2);
18 System.out.println(xml);
19 }
20 catch (Exception e)
21 {
22 e.printStackTrace(System.err);
23 }
24 }
25 }
The only difference between Listing 4-2 and Listing 4-1 occurs in line 16. On line 16, a different parse method is invoked that allows a DTD file to be specified (mydtd.dtd), so that it is referenced in the resulting XML document.
Thus, the resulting XML has a DOCTYPE statement that refers to the DTD mydtd.dtd (see the following example).
<?xml version="1.0"?>
<!DOCTYPE someRootNode SYSTEM `mydtd.dtd'>
A similar parse method allows the resulting XML to refer to an XML Schema.
Passing in a Debug Writer
All of the parse() methods of XML Translator allow a PrintWriter to be passed in as the last parameter of the parse() method. If this parameter is not null, XML Translator will print debug messages to this PrintWriter. This allows you to debug the translation if the MFL document and the binary data do not agree. If debug messages are not desired, pass in null for this parameter as shown in the previous listings.
Listing 6-3 Passing in a Debug Writer Sample
1 import com.bea.wlxt.*;
2 import org.w3c.dom.Document
3 import java.io.FileInputStream;
4 import java.io.PrintWriter;
5 import java.net.URL;
6
7 public class Example3
8 {
9 public static void main(String[] args)
10 {
11 try
12 {
13 WLXT wlxt = new WLXT();
14 URL mflDocumentName = new URL("file:mymfl.mfl");
15 FileInputStream in = new FileInputStream
16 ("mybinaryfile");
17 Document doc=wlxt.parse(mflDocumentName,in,new
PrintWriter(System.out,true));
18 String xml = wlxt.getXMLText(doc, 0, 2);
19 System.out.println(xml);
20 }
21 catch (Exception e)
22 {
23 e.printStackTrace(System.err);
24 }
25 }
26 }
At line 17, as a last parameter to the parse() method, a PrintWriter object is created from the System.out PrintStream. This will cause debug messages such as the ones shown below to be written to the console.
Listing 6-4 Debug Output
Parsing FieldFormat NAME at offset 0
Field NAME Found delimiter [;]
Field NAME type String offset 0 value=[John Doe]
Done FieldFormat NAME
Group PAYINFO repeat until delim=[*]
Parsing 1st instance of StructFormat PAYINFO at offset 18
Parsing FieldFormat PAYDATE at offset 18
.
.
.
XML to Binary
The following code listing illustrates using XML Translator to convert XML text to binary format.
Listing 6-5 Sample XML to Binary Conversion
1 import com.bea.wlxt.*;
2 import java.io.FileInputStream;
3 import java.io.FileOutputStream;
4 import java.net.URL;
5
6 public class Example4
7 {
8 public static void main(String[] args)
9 {
10 try
11 {
12 WLXT wlxt = new WLXT();
13 URL mflDocumentName = new URL("file:mymfl.mfl");
14 FileInputStream in = new FileInputStream("myxml.xml");
15 FileOutputStream out = new FileOutputStream("mybinaryfile");
16
17 wlxt.serialize(mflDocumentName, in, out, null);
18 out.close();
19 }
20 catch (Exception e)
21 {
22 e.printStackTrace(System.err);
23 }
24 }
25 }
In the code example above, a new instance of XML Translator class is created at line 12. Then a URL is created for an MFL file, and a FileInputStream is created for a file containing XML text. A FileOutputStream is also instantiated to store the binary data that will result from the XML to binary translation. On line 17, the serialize() method of XML Translator is invoked, to translate the XML data contained in the FileInputStream 'in' (myxml.xml), to the binary format described in 'mymfl.mfl'. This binary data is written to the FileOutputStream 'out' (which is the file 'mybinaryfile').
Converting a Document object to Binary
The listing below illustrates converting a W3C Document object to a binary format.
Listing 6-6 Converting a Document Object to Binary
1 import com.bea.wlxt.*;
2 import java.io.FileOutputStream;
3 import java.net.URL;
4
5 import org.w3c.dom.Document;
6
7 import org.apache.xerces.parsers.DOMParser;
8
9 public class Example5
10 {
11 public static void main(String[] args)
12 {
13 // Parse XML into a Document object
14 Document doc = null;
15 try
16 {
17 DOMParser parser = new DOMParser();
18 parser.parse("myxml.xml");
19 doc = parser.getDocument();
20 }
21 catch (Exception e)
22 {
23 e.printStackTrace(System.err);
24 System.exit(1);
25 }
26
27 try
28 {
29 WLXT wlxt = new WLXT();
30 URL mflDocumentName = new URL("file:mymfl.mfl");
31 FileOutputStream out = new
FileOutputStream("mybinaryfile");
32
33 wlxt.serialize(mflDocumentName, doc, out, null);
34 out.close();
35 }
36 catch (Exception e)
37 {
38 e.printStackTrace(System.err);
39 }
40 }
41 }
This example illustrates passing in a Document object to the serialize() method of the XML Translator class. This is useful when your application already has XML in the form of a Document object, or has created a Document object using the DOM API. Lines 14 through 25 convert the XML text in the file 'myxml.xml' to a Document object using an XML parser. This Document object is passed to XML Translator on line 33, to convert it to the binary format specified by the MFL file 'mymfl.mfl'.
Passing in a debug writer
The serialize methods also support passing in a PrintWriter parameter for the logging of debug messages. An example invocation of the serialize method with a PrintWriter object is given below.
wlxt.serialize(mflDocumentName, in, out, new
PrintWriter(System.out, true));
This will cause debug messages such as the ones shown below to be written to the console.
Debug Output
The following code represents debug output.
Listing 6-7 Debug Output
Processing xml and mfl nodes tcp1
Processing xml node NAME
Checking MFL node NAME
Found matching MFL node NAME
Writing field NAME value John Doe
Processing xml node PAYINFO
Checking MFL node PAYINFO
XML to XML Transformation
XML Translator also provides methods to transform XML via XSLT. XSLT is a language for transforming XML documents. A XSLT stylesheet is an XML document that describes transformations that are to be performed on the nodes of an XML document. The XML Translator class provides transform() methods that apply an XSLT stylesheet to an XML document. Using a stylesheet, an XML document can be transformed into HTML, PDF, or another XML dialect.
The listing below illustrates transforming an XML document using one of the transform methods provided by the XML Translator class.
Listing 6-8 XML to XML Transformation
1 import com.bea.wlxt.*;
2 import java.io.FileInputStream;
3 import java.io.FileOutputStream;
4 import java.net.URL;
5
6 import org.xml.sax.InputSource;
7
8 public class Example7
9 {
10 public static void main(String[] args)
11 {
12
13 try
14 {
15 WLXT wlxt = new WLXT();
16 URL stylesheet = new URL("file:mystylesheet.xsl");
17 FileInputStream in = new FileInputStream("myxml.xml");
18 FileOuputStream out = new FileOutputStream
19 ("myoutputfile")
20
21 wlxt.transform(new InputSource(in), out, stylesheet);
22
23 out.close();
24 }
25 catch (Exception e)
26 {
27 e.printStackTrace(System.err);
28 }
29 }
30 }
On line 15, an instance of XML Translator is created. On the following line a URL is created for a previously created XSLT stylesheet. A FileInputStream is then created for a file containing XML text. A FileOutputStream is also created for the text that results from the XSLT transformation. On line 21, a transform() method of the XML Translator class is invoked to transform the XML in the file 'myxml.xml', according to the XSLT stylesheet 'mystylesheet.xsl'. The output of the transformation is written to the file 'myoutputfile'.
Initialization methods
The XML Translator class provides several methods to 'preprocess' MFL documents and XSLT stylesheets. Once these documents are preprocessed, they are cached internally, and reused when referenced in an parse(), serialize(), or transform() method. This greatly improves the performance of these methods, since the MFL document or XSLT stylesheet has already been processed and cached. This is particularly useful when XML Translator is used in an EJB or servlet, where the same MFL documents or XSLT stylesheets are used repeatedly.
init() method
The XML Translator class provides two init() methods that take either a java.util.Properties object or the file name of a Properties file as a parameter. This init() method will retrieve the 'WLXT.stylesheets' and 'WLXT.MFLDocuments' properties from the Properties object. Each property is expected to contain a comma-delimited list of documents that are to be preprocessed and cached. When these documents are later referenced in a parse(), serialize(), or transform() method, the preprocessed version will be retrieved from the cache. The listing below demonstrates using an init() method to initialize an instance of the XML Translator class.
Listing 6-9 Properties file myconfig.cfg:
WLXT.MFLDocuments=file:mymfl.mfl
WLXT.stylesheets=file:mystylesheet.xsl
Listing 6-10 Source code example of init() method using file 'myconfig.cfg'
1 import com.bea.wlxt.*;
2 import java.io.FileInputStream;
3 import java.io.FileOutputStream;
4 import java.net.URL;
5
6 import org.xml.sax.InputSource;
7 import org.w3c.dom.Document;
8
9 public class Example8
10 {
11 public static void main(String[] args)
12 {
13
14 WLXT wlxt = null;
15
16 // Initialize WLXT with a properties file
17 try
18 {
19 wlxt = new WLXT();
20 wlxt.init("myconfig.cfg");
21 }
22 catch (Exception e)
23 {
24 e.printStackTrace(System.err);
25 }
26
27 // Parse binary data into XML
28 Document doc = null;
29 try
30 {
31 URL mflDocumentName = new URL("file:mymfl.mfl");
32 FileInputStream in = new FileInputStream("mybinaryfile");
33
34 doc = wlxt.parse(mflDocumentName, in, null);
35 }
36 catch (Exception e)
37 {
38 e.printStackTrace(System.err);
39 }
40
41 try
42 {
43 URL stylesheet = new URL("file:mystylesheet.xsl");
44 FileOutputStream out = new FileOutputStream
45 ("myoutputfile")
46
47 wlxt.transform(doc, out, stylesheet);
48
49 out.close();
50 }
51 catch (Exception e)
52 {
53 e.printStackTrace(System.err);
54 }
55 }
56 }
The init() method on line 20 of the listing above, causes the XML Translator object to preprocess the documents listed in the file 'myconfig.cfg'. When an MFL document is specified in the parse() method of line 34, this MFL document has already been processed an cached inside the XML Translator object. The same is true of the stylesheet that is referenced in the invocation of the transform() method on line 46.
Java API Documentation
For the complete reference to using the XML Translator class, see the Java API Documentation located in the apidoc subdirectory of your XML Translator installation.
Run-Time Plug-In to WebLogic Process Integrator
BEA XML Translator Plug-In for WebLogic Process Integrator provides for an exchange of information between applications by supporting data translations between binary formats from legacy systems and XML. The XML Translator Plug-In provides Process Integrator actions that allow you to access XML to Binary and Binary to XML translations.
In addition to this data translation capability, the XML Translator Plug-In provides event data processing in binary format, in-memory caching of MFL documents and translation object pooling to boost performance, a BinaryData variable type to edit and display binary data, exporting of entirely self-contained workflow definition packages, and execution within a WebLogic Server clustered environment.
The following illustration describes the relationship between XML Translator and Process Integrator.
Figure 6-1 Run-Time Plug-In to Process Integrator
![]() |
![]() |
![]() |
|
Copyright © 2001 BEA Systems, Inc. All rights reserved.
|