I'm trying to use Xalan to convert XML to an SVG DOM,
and then pass that to the rasterizer to produce a JPG file.
Given this XML source file:
==================
<Reports>
<height>20</height>
</Reports>
================
and this XSL transformation:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:java="http://xml.apache.org/xslt/java"
exclude-result-prefixes="java"
>
<xsl:output method="xml"
indent="yes" standalone="no"
doctype-public = "-//W3C//DTD SVG 20001102//EN"
doctype-system =
"http://www.w3.org/TR/2000/CR-SVG-20001102/DTD/svg-20001102.dtd"
/>
<xsl:template match="Reports">
<svg width="350" height="200" viewBox="0 0 350 200">
<defs>
<path id="wind-arrow" d="M 40 40 h 25"
fill="none" stroke="black"/>
</defs>
<g>
<rect x="10" y="10" width="30" fill="blue" stroke="none">
<xsl:attribute name="height"><xsl:value-of
select="height"/></xsl:attribute>
</rect>
</g>
<g>
<circle cx="50" cy="50" fill="red" stroke="none">
<xsl:attribute name="r"><xsl:value-of
select="height"/></xsl:attribute>
</circle>
</g>
</svg>
</xsl:template>
<!-- discard any text nodes -->
<xsl:template match="text()"/>
<!-- don't automatically scan all elements -->
<xsl:template match="*"/>
</xsl:stylesheet>
==============================
And the following code:
import java.io.*;
import java.text.*;
import java.util.*;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.net.URLDecoder;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.stream.*;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;
import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.apache.batik.transcoder.image.JPEGTranscoder;
import org.apache.batik.transcoder.image.PNGTranscoder;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.dom.GenericDOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentType;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.svg.SVGDocument;
import org.xml.sax.helpers.XMLReaderFactory;
import org.xml.sax.XMLReader;
import org.xml.sax.InputSource;
import org.apache.xalan.serialize.Serializer;
import org.apache.xalan.serialize.SerializerFactory;
import org.apache.xalan.templates.OutputProperties;
public class TransformApp {
ResourceBundle rb = ResourceBundle.getBundle("TransformFileStrings");
private Templates xslTemplate;
private String retrievedXML;
private String callLetters;
private String cityName;
private Document svgDoc;
private DOMResult transformDOM;
public TransformApp( ) {
} // <init>(void)
public static void main(String argv[]) {
TransformApp maker = new TransformApp();
maker.do_it_all();
}
public void do_it_all()
{
init();
doPost();
// emitJPG2();
}
public void init()
{
String path;
String title;
path = rb.getString("pathName");
title = rb.getString( "xslFileName" );
try
{
TransformerFactory factory = TransformerFactory.newInstance();
xslTemplate = factory.newTemplates(new StreamSource(path + title));
}
catch (Exception ex)
{
xslTemplate = null;
}
}
public void doPost()
{
StreamSource xmlString;
boolean downloadOK;
downloadOK = getReports( );
if (downloadOK)
{
try
{
xmlString = new StreamSource(
new StringReader( retrievedXML )
);
Transformer transformer = xslTemplate.newTransformer();
transformer.setOutputProperty("encoding", "UTF-8");
DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
svgDoc = impl.createDocument( svgNS, "svg", null);
transformDOM = new DOMResult( svgDoc.getDocumentElement() );
transformer.transform(
xmlString,
transformDOM
);
svgDoc.getDocumentElement().setAttribute("width",
"350");
svgDoc.getDocumentElement().setAttribute("height",
"200");
emitJPG( );
}
catch (Exception e)
{
e.printStackTrace();
}
}
else
{
System.err.println("Could not get the file");
}
}
private boolean getReports( )
{
BufferedReader input;
boolean success;
String title;
title = rb.getString( "xmlFileName" );
try {
// Get response data.
input = new BufferedReader(new FileReader(title));
StringBuffer strBuf = new StringBuffer(2048);
String str;
while (null != ((str = input.readLine())))
{
strBuf.append(str);
strBuf.append("\n");
}
retrievedXML = strBuf.toString();
success = true;
input.close ();
}
catch (Exception e)
{
if (e.getMessage() != null)
{
success = false;
}
else
{
success = true;
}
}
return success;
}
public void emitJPG ( )
{
JPEGTranscoder t = new JPEGTranscoder();
// set the transcoding hints
t.addTranscodingHint(JPEGTranscoder.KEY_QUALITY,
new Float(.8));
// create the transcoder input
TranscoderInput input = new TranscoderInput( svgDoc );
try {
TranscoderOutput output =
new TranscoderOutput( new FileOutputStream(
rb.getString( "jpgFileName") ) );
t.transcode(input, output);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void emitJPG2 ( )
{
JPEGTranscoder t = new JPEGTranscoder();
// set the transcoding hints
t.addTranscodingHint(JPEGTranscoder.KEY_QUALITY,
new Float(.8));
// create the transcoder input
TranscoderInput input = new TranscoderInput(
rb.getString( "svgFileURI" ) );
try {
TranscoderOutput output =
new TranscoderOutput( new FileOutputStream(
rb.getString( "jpgFileName") ) );
// write the image
t.transcode(input, output);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
==================================================
If I run this code, I get a Document which, when serialized, looks
like this:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg contentScriptType="text/ecmascript" width="350" zoomAndPan="magnify"
contentStyleType="text/css" height="200" preserveAspectRatio="xMidYMid meet"
xmlns="http://www.w3.org/2000/svg"><svg width="350" height="200" viewBox="0 0 350
200"><defs><path fill="none" id="wind-arrow" d="M 40 40 h 25"
stroke="black"/></defs><g><rect
x="10" y="10" fill="blue" width="30" height="20" stroke="none"/></g><g><circle
fill="red" r="20" cy="50" cx="50" stroke="none"/></g></svg></svg>
=====================================================
However, it doesn't create a JPG file; the transcoder dies.
I've narrowed the problem down to
org/apache/batik/transcoder/image/ImageTranscoder.java
I added some extra printStackTrace() calls, and I get this error:
java.lang.NullPointerException
at sun.java2d.SunGraphics2D.drawRenderedImage(SunGraphics2D.java:1849)
at
org.apache.batik.transcoder.image.ImageTranscoder.transcode(ImageTranscoder.java:261)
at
org.apache.batik.transcoder.XMLAbstractTranscoder.transcode(XMLAbstractTranscoder.java:126)
at TransformApp.emitJPG(TransformApp.java:173)
at TransformApp.doPost(TransformApp.java:109)
at TransformApp.do_it_all(TransformApp.java:58)
at TransformApp.main(TransformApp.java:52)
org.apache.batik.transcoder.TranscoderException: null
which comes from this line:
BufferedImage rend = renderer.getOffScreen();
producing null.
==================================================
If I change the main() routine in my test program to comment out
doPost() and call emitJPG2() instead, which goes straight from a file
which I created by saving the serialized output shown above into a text
file, no error occurs, and I get the desired JPG output.
What am I doing wrong?
--
J. David Eisenberg
http://catcode.com
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]