The XMLSerializer class of the Serialization project, introduced in“XML Serialization Essentials” and listed in Listing B-2 includes the transformObject method, shown in Listing 5-1.
Listing 5-1 The transformObject method in XMLSerializer .java
/** |
* Serializes objects and data to a stream, which can also be |
* transformed. The product of the process is written to a file. |
* |
* @param source object to serialize or transform |
* @param filename filename of the target document, |
* including path and extension |
* @param transformation type of transformation to perform; |
* indicates which transformation script to use. |
* When <code>null</code>, no transformation |
* is performed, only serialization. |
* |
* @return <code>true</code> when the process succeeds. |
*/ |
public static boolean transformObject(Object source, String filename, String transformation) { |
boolean success = false; |
try { |
// Create a stream to the output file. |
NSXMLOutputStream stream = (NSXMLOutputStream)openStream(filename, false, transformation);// 1 |
// Serialize data to XML output stream. |
stream.writeObject(source); |
stream.flush(); |
closeStream(filename); |
success = true; |
} |
catch (IOException e) { |
e.printStackTrace(); |
} |
return success; |
} |
The transformObject method opens an output stream (line numbered 1) to a file using the openStream method (Listing 5-2), which initializes the XML transformer using the initializeTransformer method (line 1), shown in Listing 5-3. The openStream method also turns indenting on using the NSXMLOutputFormat(boolean) constructor of the NSXMLOutputFormat class (lines 2 and 3).
Listing 5-2 The openStream method in XMLSerializer.java
/** |
* Opens a file stream to or from a file and a corresponding |
* output or input object stream. |
* Adds the pair of streams to an internal dictionary for use by |
* the <code>closeStream</code> method. |
* |
* @param filename fully qualified filename of the |
* target or source file; identifies |
* the channel to open |
* @param input_stream indicates whether the stream returned |
* is an input stream or an output stream: |
* <code>true</code> for an input stream and |
* <code>false</code> for an output stream. |
* @param transformation type of transformation to perform; |
* indicates which transformation script to use. |
* When <code>null</code>, no transformation |
* is performed, only serialization. |
* |
* @return object stream, <code>null</code> when the stream |
* could not be created. |
*/ |
private static Object openStream(String filename, boolean input_stream, String transformation) throws IOException { |
BufferedOutputStream file_output_stream = null; |
BufferedInputStream file_input_stream = null; |
Channel channel; |
Object xml_stream = null; |
if (input_stream) { |
// Create an input stream from the file. |
file_input_stream = new BufferedInputStream(new FileInputStream(filename)); |
// Create object input stream. |
xml_stream = new NSXMLInputStream(file_input_stream); |
channel = new Channel(file_input_stream, xml_stream, input_stream); |
} else { |
// Create an output stream to the file. |
file_output_stream = new BufferedOutputStream(new FileOutputStream(filename)); |
// Create object output stream. |
if (transformation != null) { |
xml_stream = initializeTransformer(file_output_stream, transformation);// 1 |
} else { |
xml_stream = new NSXMLOutputStream(file_output_stream); |
} |
// Set the format of the output document (XML). |
NSXMLOutputFormat format = new NSXMLOutputFormat(true);// 2 |
((NSXMLOutputStream)xml_stream).setOutputFormat(format);// 3 |
channel = new Channel(file_output_stream, xml_stream, input_stream); |
} |
channels.setObjectForKey(channel, filename); |
return xml_stream; |
} |
Listing 5-3 The initializeTransformer method in XMLSerializer.java
/** |
* Initializes the transformer. |
* |
* @param file_stream target file stream |
* @param transformation type of transformation to perform; |
* indicates which transformation file to use |
* |
* @throws IOException when there's a problem initializing the transformer. |
*/ |
private static NSXMLOutputStream initializeTransformer(BufferedOutputStream file_stream, String transformation) throws IOException { |
NSXMLOutputStream xml_stream = new NSXMLOutputStream(file_stream, new File(transformationURI(transformation)));// 1 |
Transformer transformer = ((NSXMLOutputStream)xml_stream).transformer(); |
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); |
return xml_stream; |
} |
The initializeTransformer method takes a stream to a file in the file_stream parameter, which it uses to create the NSXMLOutputStream object (1) that transforms whatever is written to it using the transformation script indicated by the transformation parameter. Then it sets an output property of the transformer; in this case it sets the indentation level of the new document.
Last updated: 2005-08-11