Bug 10787

Summary: NullPointerException when using TranscoderInput on InputStream
Product: Batik - Now in Jira Reporter: Vincent Cautaerts <vincent>
Component: SVG DOMAssignee: Batik Developer's Mailing list <batik-dev>
Status: RESOLVED INVALID    
Severity: normal    
Priority: P3    
Version: 1.5   
Target Milestone: ---   
Hardware: PC   
OS: Linux   

Description Vincent Cautaerts 2002-07-14 15:44:54 UTC
With the 'saveAsJPEG' example given in Batik doc. for TranscoderInput, if the
SVG file is read from a 'java.io.FileInputStream' opened on the SVG file,
instead of a 'File' pointing to the same file, a NullPointerException is thrown.

i.e. if the lines

            String svgURI = new File(args[0]).toURL().toString();
            input = new TranscoderInput(svgURI);

are replaced by

            InputStream is=new FileInputStream(args[0]);
            input = new TranscoderInput(is);

the file can't be loaded anymore... a NullPointerException is thrown by

        t.transcode(input, output);

Note: tested with 'henryV.svg' sample file.

Here follows the stack trace:

Exception in thread "main" java.lang.NullPointerException
        at java.net.URL.<init>(URL.java:366)
        at java.net.URL.<init>(URL.java:329)
        at java.net.URL.<init>(URL.java:321)
        at java.net.URL.<init>(URL.java:252)
        at java.net.URL.<init>(URL.java:275)
        at org.apache.batik.util.ParsedURLData.buildURL(Unknown Source)
        at org.apache.batik.util.ParsedURLData.openStreamInternal(Unknown Source)
        at org.apache.batik.util.ParsedURLData.openStream(Unknown Source)
        at org.apache.batik.util.ParsedURL.openStream(Unknown Source)
        at org.apache.batik.dom.svg.SAXSVGDocumentFactory.createDocument(Unknown
Source)
        at org.apache.batik.bridge.DocumentLoader.loadDocument(Unknown Source)
        at org.apache.batik.bridge.URIResolver.getNode(Unknown Source)
        at org.apache.batik.bridge.URIResolver.getElement(Unknown Source)
        at org.apache.batik.bridge.BridgeContext.getReferencedElement(Unknown
Source)
        at org.apache.batik.bridge.CSSUtilities.convertFilter(Unknown Source)
        at
org.apache.batik.bridge.AbstractGraphicsNodeBridge.buildGraphicsNode(Unknown Source)
        at org.apache.batik.bridge.GVTBuilder.buildGraphicsNode(Unknown Source)
        at org.apache.batik.bridge.GVTBuilder.buildComposite(Unknown Source)
        at org.apache.batik.bridge.GVTBuilder.buildGraphicsNode(Unknown Source)
        at org.apache.batik.bridge.GVTBuilder.buildComposite(Unknown Source)
        at org.apache.batik.bridge.GVTBuilder.build(Unknown Source)
        at org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(Unknown
Source)
        at org.apache.batik.transcoder.image.ImageTranscoder.transcode(Unknown
Source)
        at org.apache.batik.transcoder.XMLAbstractTranscoder.transcode(Unknown
Source)
Comment 1 Bill Sharar 2002-08-24 14:21:23 UTC
I get the same error using a Reader with TranscoderInput
Comment 2 Martin 2002-11-08 06:44:26 UTC
I get the same thing on 1.1 , different error on 1.5 with (testRotate.svg used 
as and example input) the class below called from a servlet that does this:


        response.setContentType("image/png");
        PrintWriter out = response.getWriter();
        
        ConvertSVG convertSVG = new ConvertSVG();
        
        FileReader in = new FileReader("E:\\batik-1.5
\\samples\\textRotate.svg");
        
        convertSVG.PNG(in,out);
        
        out.close();


------

**** The Transcoder class:

-----------------------------------------

import java.io.*;

import org.apache.batik.transcoder.image.PNGTranscoder;
import org.apache.batik.transcoder.TranscoderException;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;

/**
 *
 * @author  martin@technomation.net
 */
public class ConvertSVG { 
    
    /** Creates a new instance of SVGtoPNG */
    public ConvertSVG() {
    }
    
    private static void toPNG( TranscoderInput input, TranscoderOutput output ) 
throws TranscoderException {
        PNGTranscoder t = new PNGTranscoder();
        t.transcode(input, output);
    }
        
    
    /**
     * @param inputStream
     * @param outputStream
     * @throws IOException  */   
    public static void PNG( Reader reader, Writer writer ) throws IOException {
        // Transcoder input
        TranscoderInput input = new TranscoderInput( reader );
        
        // create the transcoder output
        BufferedWriter ostream = new BufferedWriter( writer );
        TranscoderOutput output = new TranscoderOutput(ostream);
        
        // save the image
        try {
            toPNG(input, output);
        } catch ( TranscoderException e ) {
            System.err.println( "Transcoder exception thrown transcoding 
stream :" + e.toString() );
            throw new IOException( "Transcoder exception thrown transcoding 
stream :" + e.toString() );
        }
        
        // flush and close the stream then exit
        ostream.flush();
        ostream.close();
    }    
}

---------------------------------


**** The error on 1.1:


java.lang.NullPointerException
	at java.net.URL.(URL.java:366)
	at java.net.URL.(URL.java:329)
	at java.net.URL.(URL.java:321)
	at java.net.URL.(URL.java:252)
	at java.net.URL.(URL.java:275)
	at org.apache.batik.util.ParsedURLData.buildURL(Unknown Source)
	at org.apache.batik.util.ParsedURLData.openStreamInternal(Unknown 
Source)
	at org.apache.batik.util.ParsedURLData.openStream(Unknown Source)
	at org.apache.batik.util.ParsedURL.openStream(Unknown Source)
	at org.apache.batik.dom.svg.SAXSVGDocumentFactory.createDocument
(Unknown Source)
	at org.apache.batik.bridge.DocumentLoader.loadDocument(Unknown Source)
	at org.apache.batik.bridge.URIResolver.getNode(Unknown Source)
	at org.apache.batik.bridge.URIResolver.getElement(Unknown Source)
	at org.apache.batik.bridge.BridgeContext.getReferencedElement(Unknown 
Source)
	at org.apache.batik.bridge.SVGUseElementBridge.createGraphicsNode
(Unknown Source)
	at org.apache.batik.bridge.GVTBuilder.buildGraphicsNode(Unknown Source)
	at org.apache.batik.bridge.GVTBuilder.buildComposite(Unknown Source)
	at org.apache.batik.bridge.GVTBuilder.build(Unknown Source)
	at org.apache.batik.transcoder.image.ImageTranscoder.transcode(Unknown 
Source)
	at org.apache.batik.transcoder.XMLAbstractTranscoder.transcode(Unknown 
Source)
	at com.dnaShare.charts.ConvertSVG.toPNG(ConvertSVG.java:28)
	at com.dnaShare.charts.ConvertSVG.PNG(ConvertSVG.java:100)



---------------------------


*****    The error on 1.5rc4:

java.lang.NullPointerException
	at org.apache.batik.css.engine.CSSEngine.parseStyleSheet(Unknown Source)
	at org.apache.batik.css.engine.CSSEngine.parseStyleSheet(Unknown Source)
	at 
org.apache.batik.dom.svg.SVGStyleSheetProcessingInstruction.getCSSStyleSheet
(Unknown Source)
	at org.apache.batik.css.engine.CSSEngine.getStyleSheetNodes(Unknown 
Source)
	at org.apache.batik.css.engine.CSSEngine.getCascadedStyleMap(Unknown 
Source)
	at org.apache.batik.css.engine.CSSEngine.getComputedStyle(Unknown 
Source)
	at org.apache.batik.bridge.CSSUtilities.getComputedStyle(Unknown Source)
	at org.apache.batik.bridge.CSSUtilities.convertVisibility(Unknown 
Source)
	at org.apache.batik.bridge.SVGSVGElementBridge.createGraphicsNode
(Unknown Source)
	at org.apache.batik.bridge.GVTBuilder.build(Unknown Source)
	at org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(Unknown 
Source)
	at org.apache.batik.transcoder.image.ImageTranscoder.transcode(Unknown 
Source)
	at org.apache.batik.transcoder.XMLAbstractTranscoder.transcode(Unknown 
Source)
	at com.dnaShare.charts.ConvertSVG.toPNG(ConvertSVG.java:28)
	at com.dnaShare.charts.ConvertSVG.PNG(ConvertSVG.java:100)


Comment 3 Thomas Deweese 2003-03-02 02:39:39 UTC
  The problem is with documents that have relative reference there is no
way to resolve the reference when you just provide a stream.