|
|
import org.w3c.dom.*;
import java.util.*;
import java.io.*;
import java.net.*;
import oracle.sql.*;
import oracle.xml.parser.v2.*;
import org.xml.sax.InputSource;
public class XMLtoHTML
{
/**
* Transforms an xml document using a stylesheet
*/
public static void convert (String xslfile,
CLOB xmlclob,
CLOB[] htmlclob) throws Exception
{
DOMParser parser;
XMLDocument xml, xsldoc;
URL xslURL;
try
{
// Parse xsl and xml documents
parser = new DOMParser();
parser.setPreserveWhitespace(true);
// parser input XSL file
xslURL = createURL(xslfile);
parser.parse(xslURL);
xsldoc = parser.getDocument();
// parser input XML file
parser.parse(xmlclob.getCharacterStream());
xml = parser.getDocument();
// instantiate a stylesheet
XSLStylesheet xsl
= new XSLStylesheet(xsldoc,xslURL);
XSLProcessor processor = new XSLProcessor();
// display any warnings that may occur
processor.showWarnings(true);
processor.setErrorStream(System.err);
Writer w = htmlclob[0].getCharacterOutputStream();
PrintWriter pw = new PrintWriter(w);
// Process XSL
processor.processXSL(xsl, xml, pw);
}
catch (Exception e)
{
e.printStackTrace();
}
}
// Helper method to create a URL from a file name
static URL createURL(String fileName)
{
URL url = null;
try
{
url = new URL(fileName);
}
catch (MalformedURLException ex)
{
File f = new File(fileName);
try
{
String path = f.getAbsolutePath();
// This is a bunch of weird code that is required to
// make a valid URL on the Windows platform, due
// to inconsistencies in what getAbsolutePath returns.
String fs = System.getProperty("file.separator");
if (fs.length() == 1)
{
char sep = fs.charAt(0);
if (sep != '/')
path = path.replace(sep, '/');
if (path.charAt(0) != '/')
path = '/' + path;
}
path = "file://" + path;
url = new URL(path);
}
catch (MalformedURLException e)
{
System.out.println("Cannot create url: "+fileName);
System.exit(0);
}
}
return url;
}
}
|