/*
* This software and supporting documentation were developed by
*
* Siemens Corporate Technology
* Competence Center Knowledge Management and Business Transformation
* D-81730 Munich, Germany
*
* Authors (representing a really great team ;-) )
* Stefan B. Augustin, Thorbj�rn Hansen, Manfred Langen
*
* This software is Open Source under GNU General Public License (GPL).
* Read the text of this license in LICENSE.TXT
* or look at www.opensource.org/licenses/
*
* Once more we emphasize, that:
* THIS SOFTWARE IS MADE AVAILABLE, AS IS, WITHOUT ANY WARRANTY
* REGARDING THE SOFTWARE, ITS PERFORMANCE OR
* FITNESS FOR ANY PARTICULAR USE, FREEDOM FROM ANY COMPUTER DISEASES OR
* ITS CONFORMITY TO ANY SPECIFICATION. THE ENTIRE RISK AS TO QUALITY AND
* PERFORMANCE OF THE SOFTWARE IS WITH THE USER.
*
*/
// HTMLwithXSLBuilder2
// ************ package ****************************************************
package appl.Portal.Utils.XML;
// ************ imports ******************************************************
import KFM.Exceptions.*;
import KFM.log.*;
import javax.xml.transform.Source;
import javax.xml.transform.Result;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.dom.DOMResult;
import org.w3c.dom.Node;
import java.io.*;
import java.util.Hashtable;
import java.util.Enumeration;
/**
* <P> Class HTMLwithXSLBuilder2 will construct HTML output from XML input using
* XSL transformations. It is the successor of class HTMLwithXSLBuilder and was
* programmed for switching from Xalan1 to Xalan2.</P>
*
* <P> Note that HTMLwithXSLBuilder2 will not try to be upwards compatible with
* HTMLwithXSLBuilder, as the old class had a bad design. </P>
*
* <H2> Usage </H2>
* <PRE>
* See main() method of this class.
* </PRE>
*
* For the XSL implementation, we use Xalan2 from the OpenXML project of Apache.
* It is freely available at http://xml.apache.org.
* The product chosen should not shine through at too many places, though.
* Where a product-specific statement is inevitable, we mark it as '//@XALAN'. <P>
*
* @version 1.0 (2001-10-23)
*/
public class HTMLwithXSLBuilder2
{
/**
* Construct an instance of HTMLwithXSLBuilder2.
*/
public HTMLwithXSLBuilder2()
{
}
/**
* Transform a XML document to a HTML document using a XSL Stylesheet.
* A very general method as the parameters allow for flexibility.
*
* @param aXmlInput either a DOM Node, an InputStream, a Reader or an URI (a String)
* @param a XslInput either a DOM Node, an InputStream, a Reader or an URI (a String)
* @param aHtmlOutput either a DOM Node, a Writer or an OutputStream.
* @param aParamList a Hashtable of XSL params (can also be null)
*/
public void transform(
Object aXmlInput,
Object aXslInput,
Object aHtmlOutput,
Hashtable aParamList)
throws IllegalArgumentException
{
Source tXmlSource = null;
Source tXslSource = null;
Result tOutputTarget = null;
// test and convert the flexible parameters
if (aXmlInput instanceof Node) {
// aXmlInput is a DOM object
tXmlSource = new DOMSource((Node)aXmlInput);
} else if (aXmlInput instanceof InputStream) {
// aXmlInput is a InputStream (XML byte stream)
tXmlSource = new StreamSource((InputStream)aXmlInput);
} else if (aXmlInput instanceof Reader) {
// aXmlInput is a Reader (XML character stream)
tXmlSource = new StreamSource((Reader)aXmlInput);
} else if (aXmlInput instanceof String) {
// aXmlInput is an URL (conforming to the URI syntax)
tXmlSource = new StreamSource((String)aXmlInput);
} else {
throw new IllegalArgumentException("HTMLwithXSLBuilder2::transform : Illegal argument for XML input");
}
if (aXslInput instanceof Node) {
tXslSource = new DOMSource((Node)aXslInput);
} else if (aXslInput instanceof InputStream) {
tXslSource = new StreamSource((InputStream)aXslInput);
} else if (aXslInput instanceof Reader) {
tXslSource = new StreamSource((Reader)aXslInput);
} else if (aXslInput instanceof String) {
tXslSource = new StreamSource((String)aXslInput);
} else {
throw new IllegalArgumentException("HTMLwithXSLBuilder2::transform : Illegal argument for XSL input");
}
if (aHtmlOutput instanceof Node) {
tOutputTarget = new DOMResult((Node)aHtmlOutput);
} else if (aHtmlOutput instanceof OutputStream) {
tOutputTarget = new StreamResult((OutputStream)aHtmlOutput);
} else if (aHtmlOutput instanceof Writer) {
tOutputTarget = new StreamResult((Writer)aHtmlOutput);
} else {
throw new IllegalArgumentException("HTMLwithXSLBuilder2::transform : Illegal argument for HTML output");
}
try {
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(tXslSource);
// Set the Stylesheet params (if any)
if (aParamList != null) {
String tKey;
Object tValue;
for (Enumeration tKeys = aParamList.keys() ; tKeys.hasMoreElements() ;) {
tKey = (String) tKeys.nextElement();
tValue = aParamList.get(tKey);
if (tValue == null) {
// Should never happen
throw new ProgrammerException
("HTMLwithXSLBuilder2: internal error, problem with Hashtable aParamList");
}
transformer.setParameter(tKey, /* parameter name */
tValue /* parameter value */ );
}
}
// Use the transformer to apply the Stylesheet to an XML document
// and write the output to the specified Result object.
transformer.transform(tXmlSource, tOutputTarget);
} catch(TransformerFactoryConfigurationError ex) {
throw new ProgrammerException
("HTMLwithXSLBuilder2: transform returns TransformerFactoryConfigurationError: " + ex.getMessage());
} catch(TransformerConfigurationException ex) {
throw new ProgrammerException
("HTMLwithXSLBuilder2: transform returns TransformerConfigurationException: " + ex.getMessage());
} catch(TransformerException ex) {
throw new ProgrammerException
("HTMLwithXSLBuilder2: transform returns TransformerException: " + ex.getMessage());
}
}
/**
* A main method to convert XML into HTML using an XSL stylesheet from the command line.<P>
*
* Usage:
* <PRE>
* java HTMLwithXSLBuilder2 XML-file XSL-file [HTML-file]
* </PRE>
*
* Calling this with an incorrect number of arguments prints a usage info.<BR>
* Omitting the HTML-file prints the output to System.out.
*
* @param args the array of command line arguments, see usage above
*/
static public void main (String[] args)
{
if (args.length !=2 && args.length !=3)
usage();
FileInputStream tXMLFileStream = null;
FileInputStream tXSLFileStream = null;
OutputStream tHTMLStream = null;
// Open XML file
try {
tXMLFileStream = new FileInputStream(args[0]);
} catch (FileNotFoundException ex) {
System.out.println ("Cannot open XML-file : " + args[0]);
usage();
}
// Open XSL file
try {
tXSLFileStream = new FileInputStream(args[1]);
} catch (FileNotFoundException ex) {
System.out.println ("Cannot open XSL-file : " + args[1]);
usage();
}
// Assign output for HTML, either file or System.out
if (args.length == 3)
try {
tHTMLStream = new FileOutputStream(args[2]);
} catch (IOException ex) {
System.out.println ("Cannot create HTML-file : " + args[2]);
usage();
}
else
tHTMLStream = System.out;
Hashtable tParams = new Hashtable();
// If your XSL Stylesheet should be parameterized, set the values here:
// tParams.put("param1", "'Hallo'"); // Note the single quotes
// tParams.put("param2", "$param1"); // param2 should have the same value as param1
// Use an HTMLwithXSLBuilder2 object to do the XML/XSL to HTML transformation
HTMLwithXSLBuilder2 tHTMLBuilder = new HTMLwithXSLBuilder2();
tHTMLBuilder.transform(tXMLFileStream, tXSLFileStream, tHTMLStream, tParams);
}
/**
* A helper method to print a usage information and exit.
*/
static private void usage ()
{
System.out.println ("Usage:");
System.out.println ("\t java HTMLwithXSLBuilder2 XmlFile XslFile [HtmlFile]");
System.exit(-1);
}
}