list4xt : Mailing list for the XT users community.
[list4xt] Re: Output as a string
Subject: [list4xt] Re: Output as a string
User: Website From: Mahesh Zambani (Mzambani@go2online.com)
Date: 10/02/2001 - 02:18
A working, complete example of String output..basically a modified copy of
com.jclark.xsl.sax.Driver.java...
import com.jclark.xsl.sax.*;
import org.xml.sax.*;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.File;
import java.io.FileDescriptor;
import java.net.URL;
import java.io.*;
public class MyTransformer {
static class ErrorHandlerImpl implements ErrorHandler {
public void warning(SAXParseException e) {
printSAXParseException(e);
}
public void error(SAXParseException e) {
printSAXParseException(e);
}
public void fatalError(SAXParseException e) throws SAXException {
throw e;
}
}
// my modifications to Driver.java start here
public String transform(String s, String xslURL)
throws java.io.IOException
{
XSLProcessorImpl xsl = new XSLProcessorImpl();
setParser(xsl);
xsl.setErrorHandler(new ErrorHandlerImpl());
OutputMethodHandlerImpl outputMethodHandler = new
OutputMethodHandlerImpl(xsl);
xsl.setOutputMethodHandler(outputMethodHandler);
InputSource in = new InputSource( new StringReader(s) );
InputSource stylesheet = new InputSource(xslURL);
Destination out = new OutputStreamDestination( new
ByteArrayOutputStream() );
outputMethodHandler.setDestination(out);
transform(xsl, stylesheet, in);
return new String (out.getOutputStream("","").toString() );
}
// my modifications to Driver.java end here
static boolean transform(XSLProcessor xsl,
InputSource stylesheetSource,
InputSource inputSource) {
try {
xsl.loadStylesheet(stylesheetSource);
xsl.parse(inputSource);
return true;
}
catch (SAXParseException e) {
printSAXParseException(e);
}
catch (SAXException e) {
System.err.println(e.getMessage());
}
catch (IOException e) {
System.err.println(e.toString());
}
return false;
}
static void printSAXParseException(SAXParseException e) {
String systemId = e.getSystemId();
int lineNumber = e.getLineNumber();
if (systemId != null)
System.err.print(systemId + ":");
if (lineNumber >= 0)
System.err.print(lineNumber + ":");
if (systemId != null || lineNumber >= 0)
System.err.print(" ");
System.err.println(e.getMessage());
}
static void setParser(XSLProcessorImpl xsl) {
String parserClass = System.getProperty("com.jclark.xsl.sax.parser");
if (parserClass == null)
parserClass = System.getProperty("org.xml.sax.parser");
if (parserClass == null)
parserClass = "com.jclark.xml.sax.CommentDriver";
try {
Object parserObj = Class.forName(parserClass).newInstance();
if (parserObj instanceof XMLProcessorEx)
xsl.setParser((XMLProcessorEx)parserObj);
else
xsl.setParser((Parser)parserObj);
return;
}
catch (ClassNotFoundException e) {
System.err.println(e.toString());
}
catch (InstantiationException e) {
System.err.println(e.toString());
}
catch (IllegalAccessException e) {
System.err.println(e.toString());
}
catch (ClassCastException e) {
System.err.println(parserClass + " is not a SAX driver");
}
System.exit(1);
}
/**
* Generates an <code>InputSource</code> from a file name.
*/
static public InputSource fileInputSource(String str) {
return fileInputSource(new File(str));
}
static public InputSource fileInputSource(File file) {
String path = file.getAbsolutePath();
String fSep = System.getProperty("file.separator");
if (fSep != null && fSep.length() == 1)
path = path.replace(fSep.charAt(0), '/');
if (path.length() > 0 && path.charAt(0) != '/')
path = '/' + path;
try {
return new InputSource(new URL("file", "", path).toString());
}
catch (java.net.MalformedURLException e) {
/* According to the spec this could only happen if the file
protocol were not recognized. */
throw new Error("unexpected MalformedURLException");
}
}
}
--
Mailing list for the XT users community. (http://public-root.com)
(mailto:list4xt-request@4xt.org?Subject=unsubscribe to unsubscribe)
Archive générée par hypermail 2b28 le 25/04/2001 - 07:16 CEST
webmaster@4xt.org
|