| Raben Systems Inc. | |
| Software Development Services |
| Home | About Us | Articles | Downloads | Solar maps | Space weather graphs | Wireless |
An XSLT Editor Tool - Part 2, XSLT Transformations
May 2, 2002 -- Part 2, XSLT Transformations. The tutorial continues with the development of the code necessary to transform XML using XSLT. (If you have not already done so, you may wish to read Part 1 - Basic GUI, Opening and Displaying XML Files). After the user loads an XML file and an XSLT file, the transformation is performed by pressing the "Transform" button at the bottom left of the display (See Figure 1).

Figure 1. The XSLT Editor GUI
An event handler for the action event that occurs when the user presses the "Transform" button is created by using the Forte IDE Component Inspector (See Figure 2).

Figure 2. Create action handler for transformButton
The method gets the contents of the XML editor pane and converts it to an XML document. The contents of the XSLT editor pane is read, converted to an XML document, and then written out to the local file system as a temporary file. The XSLT file (often referred to as a stylesheet) may include other documents. By saving the stylesheet as a temporary file, the directory that the temp file is saved in will be searched for the included files. The transform is then performed. During the call to the "transformDocument" the variable "transformMethod" is set to the value of the stylesheet's output method variable. If the "method" variable is set to html, the result editor pane displays the output as HTML, otherwise it is displayed as text.
/**
* Transform contents of xml editor pane using the stylesheet in the
* xslt editor pane, and display the transformed document in resultEditorPane
* @param evt ActionEvent
*/
private void transformButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_transformButtonActionPerformed
try {
// Read contents of xml editor pane, and create xml document
org.w3c.dom.Document inputDoc=getDocumentFromEditorPane(xmlEditorPane);
// Read contents of xslt editor pane, and create xslt document
org.w3c.dom.Document styleSheetDoc=getDocumentFromEditorPane(xsltEditorPane);
// Save style sheet as a temporary local file
File tempFile=File.createTempFile(".xsl","XsltEditor",lastXsltDir);
tempFile.deleteOnExit();
saveDocument(styleSheetDoc,tempFile);
Templates styleSheet=getStyleSheet(tempFile);
// Perform the transform
outputDoc=transformDocument(inputDoc,styleSheet);
String str=documentToString(outputDoc);
// The variable transformMethod is same as the stylesheet output method variable
// This is used to determine whether it is displayed as html or text
if ("html".equals(transformMethod)) {
putHtmlInEditorPane(str,resultEditorPane);
}
else {
putTextInEditorPane(str,resultEditorPane);
}
tabbedPane.setSelectedIndex(2);
}
catch (org.xml.sax.SAXException e) {
log("Error occurred during transform "+e.getMessage());
tabbedPane.setSelectedIndex(3);
}
catch (TransformerException e) {
log("Error occurred during transform "+e.getMessage());
tabbedPane.setSelectedIndex(3);
}
catch (IOException e) {
log("Error occurred during transform "+e.getMessage());
tabbedPane.setSelectedIndex(3);
}
}
The contents of the XML and XSLT editor panes is retrieved and transformed to an XML document with the method "getDocumentFromEditorPane" method. The method gets the text content of the editor pane, read, parsed, and returned as a DOM document.
/**
* Retrieve contents of editor pane, parse it, and return as a DOM document
* @param editorPane JEditorPane the selected editor pane
* @throws SAXException if there is an error parsing the document
* @throws IOException if there is a problem reading the text
*/
private org.w3c.dom.Document getDocumentFromEditorPane(JEditorPane editorPane) throws SAXException,IOException {
StringReader reader=new StringReader(editorPane.getText());
InputSource inputSource=new InputSource(reader);
return documentBuilder.parse(inputSource);
}
The "saveDocument" method creates a DOM source for the document to be written to a file. Then a StreamResult is created and passed to the transform routine. The transform writes the contents of the document in memory to a file.
/**
* Save the document to a file
* @param doc Document the document to be saved
* @param fil File The file to write to
* @throws TransformerException For errors in document or configuration of the transformer
*/
public void saveDocument(org.w3c.dom.Document doc, File fil) throws TransformerException {
// Create dom source for the document
DOMSource domSource=new DOMSource(doc);
// Create the result stream for the transform
StreamResult result = new StreamResult(fil);
// Use a Transformer for output
TransformerFactory tFactory =TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
transformer.setOutputProperty("indent","yes");
// transform the document to the result stream
transformer.transform(domSource, result);
log("Saved document "+doc.getDocumentElement().getTagName()+" in file "+fil.getAbsolutePath());
}
The "getStyleSheet" method returns the referenced file as a XSLT stylesheet.
/**
* Load xslt from file, convert to a stream source,
* and return style sheet
* @return Templates XSL stylesheet
* @throws IOException if there is a problem loading xsl file from file system
* @throws TransformerConfigurationException if there is a problem with configuration (check classpath)
* @throws SAXException if there is a problem parsing the XSLT
*/
private Templates getStyleSheet(File fil) throws IOException, TransformerConfigurationException,SAXException {;
StreamSource streamSource=new StreamSource(fil);
TransformerFactory factory=TransformerFactory.newInstance();
return factory.newTemplates(streamSource);
}
And finally, the "transformDocument" method transforms the XML document using the XSLT stylesheet.
/**
* Transform xml document using a stylesheet template
* @param doc Document DOM document to be transformed
* @param styleSheet Templates Style sheet template specifying the transform
* @throws IOException if one of the parameters is null or if there is an error
* in the configuration or style sheet
*/
private org.w3c.dom.Document transformDocument( org.w3c.dom.Document doc,Templates styleSheet) throws IOException {
org.w3c.dom.Document retDoc=null;
try {
DOMSource domSource = new DOMSource(doc);
DOMResult domResult = new DOMResult();
// Perform the transformation, placing the output in the DOMResult.
Transformer transformer=styleSheet.newTransformer();
transformer.transform(domSource, domResult);
retDoc=(org.w3c.dom.Document)domResult.getNode();
domSource=null;
domResult=null;
transformer=null;
}
catch (TransformerConfigurationException e) {
log("Error loading transformer stylesheet "+e.getMessage());
throw new IOException("transformDocument:"+e.getMessage());
}
catch (TransformerException e) {
log("Error performing transformation "+e.getMessage());
throw new IOException("tranformDocument:"+e.getMessage());
}
catch (NullPointerException e) {
log("Error occurred, document was null");
throw new IOException(e.getMessage());
}
return retDoc;
}
Article index
Back
to XSLT Editor Part 1
Next
download the XmlEditor application and look at an example!
| ©2003 Raben Systems, Inc. All rights reserved. |
Home • About
Us • Accessibility •
Articles • Downloads •
Legal |