You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-users@xmlgraphics.apache.org by Kevin Lindsey <ke...@kevlindev.com> on 2003/09/03 20:14:36 UTC

Preview Problems

Hi,

I'm currently using a Tomcat servlet to transform an uploaded SVG document.  Once the document is transformed, I generate a PNG preview with Batik and then insert that image into the transformed document as a base64 encoding image; however, my preview image is always blank.

I've done some searches and see that I should do some sanity tests like making sure that the root element is an svg element and that it is in the SVG namespace.  All of that checks out OK.  As a test, I have been able to generate the preview of the non-transformed document.  This seems to be specific to SVG documents that are transformed with Xalan.  As another test I wrote the transformed file to a temporary file and then tried to rasterize that file...same result...blank image.  It is very strange because I can view the temporary file in Squiggle (and ASV3).  It certainly looks like a namespace problem, but everything seems to be fine.

FWIW, I am serializing the transformed and then reparsing to create a new SVG document.  My understanding from the code in Squiggle is that I have to do this.

Let's see I'm running WinXP, Java 1.4.1_04, Tomcat 4.1, Batik 1.5, and Xalan 2.5.

A snippet from the transform code looks like:

        // load stylesheet
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer(
            new StreamSource(path)
        );
        
        // apply transform
        StringWriter sw = new StringWriter();
        transformer.transform(
            new StreamSource(source),
            new StreamResult(sw)
        );
        sw.flush();
        sw.close();
        
        // build and return SVGDocument
        return new SAXSVGDocumentFactory(
            XMLResourceDescriptor.getXMLParserClassName()
        ).createSVGDocument(
            "http://localhost:8080/uploadTest/",
            new StringReader(sw.toString())
        );

A snippet from the preview insertion looks like:

        PNGTranscoder t = new PNGTranscoder();
        TranscoderInput input = new TranscoderInput(svgDocument);
        ByteArrayOutputStream ostream = new ByteArrayOutputStream();
        TranscoderOutput output = new TranscoderOutput(ostream);
        
        // setup transcoder hints
        t.addTranscodingHint(PNGTranscoder.KEY_INDEXED, new Integer(256));
        t.addTranscodingHint(ImageTranscoder.KEY_BACKGROUND_COLOR, Color.white);
        t.addTranscodingHint(ImageTranscoder.KEY_WIDTH, new Float(100));
        t.addTranscodingHint(ImageTranscoder.KEY_HEIGHT, new Float(100));

        // transcode
        t.transcode(input, output);
        
        // convert result to base64 encoding
        ostream.close();
        Base64 b64 = new Base64();
        String data = b64.encode(ostream.toByteArray());

Thank you for any assistance with this.  I seem to have run out of things to try.

Kevin
KevLinDev - http://www.kevlindev.com

Re: Preview Problems

Posted by Kevin Lindsey <ke...@kevlindev.com>.
Thomas,

>>>><?xml version="1.0" encoding="utf-8"?>
>>>><svg xmlns:xlink="http://www.w3.org/1999/xlink"
>>>>xmlns="http://www.w3.org/2000/svg">
>>>
>>>     No width/height?  Hmm might try setting it.
>>
>> Interesting.  When I set an absolute length with or without units, I get
a
>> preview.
>
>     You might try current CVS I recently fixed a bug in this area - but
that
> bug caused an exception to be thrown...

I downloaded and built from CVS and now I'm getting a preview with
width/height as percentages now.

Thanks a bunch.

Kevin
KevLinDev - http://www.kevlindev.com


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org


Re: Preview Problems

Posted by Thomas DeWeese <Th...@Kodak.com>.
Kevin Lindsey wrote:
> From: "Thomas DeWeese"
> 
> 
>>><?xml version="1.0" encoding="utf-8"?>
>>><svg xmlns:xlink="http://www.w3.org/1999/xlink"
>>>xmlns="http://www.w3.org/2000/svg">
>>
>>     No width/height?  Hmm might try setting it.
> 
> 
> Interesting.  When I set an absolute length with or without units, I get a
> preview.  However, when I use percentages, I do not get a preview.  This
> explains why my test SVG documents were generating previews and my
> transformed SVG was not: the test documents have width and height defined.
> 
> OK, so this is good news, but should percentages work too?  It seems that
> setting ImageTranscoder.KEY_WIDTH and ImageTranscoder.KEY_HEIGHT would
> establish the context for percentages, unless I'm misunderstanding how these
> transcoder hints work.

    You might try current CVS I recently fixed a bug in this area - but that
bug caused an exception to be thrown...



---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org


Re: Preview Problems

Posted by Kevin Lindsey <ke...@kevlindev.com>.
From: "Thomas DeWeese"

> > <?xml version="1.0" encoding="utf-8"?>
> > <svg xmlns:xlink="http://www.w3.org/1999/xlink"
> > xmlns="http://www.w3.org/2000/svg">
>
>      No width/height?  Hmm might try setting it.

Interesting.  When I set an absolute length with or without units, I get a
preview.  However, when I use percentages, I do not get a preview.  This
explains why my test SVG documents were generating previews and my
transformed SVG was not: the test documents have width and height defined.

OK, so this is good news, but should percentages work too?  It seems that
setting ImageTranscoder.KEY_WIDTH and ImageTranscoder.KEY_HEIGHT would
establish the context for percentages, unless I'm misunderstanding how these
transcoder hints work.

Thanks for all of the help.

Kevin
KevLinDev - http://www.kevlindev.com


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org


Re: Preview Problems

Posted by Thomas DeWeese <Th...@Kodak.com>.
Kevin Lindsey wrote:
> From: "Thomas DeWeese"
> 
> 
>>>AFAIK, I'm catching all exceptions and none of them are firing and the
>>>transformed document is generated correctly, minus the blank preview.
>>
>>   Not all errors are communicated using exceptions.  Non-fatal errors are
>>sent to the Transcoders ErrorHandler.  By default it writes these to
>>System.err - in Tomcat this probably goes to some error log or
> 
> something...
> 
> It looks like all of the Tomcat log files are clean (no errors messages to
> be found).
> 
> 
>>Can you provide sample 'transformed' content?
> 
> 
> Due to NDA's, I cannot, but the file is quite simple.  Here is a mock-up of
> what I'm generating:
> 
> <?xml version="1.0" encoding="utf-8"?>
> <svg xmlns:xlink="http://www.w3.org/1999/xlink"
> xmlns="http://www.w3.org/2000/svg">

     No width/height?  Hmm might try setting it.  Also you
might try not generating indexed PNG's (I think it should
work but I don't think it's used very heavily) - Or try generating
some other format - JPEG, TIFF...

>>Also you might try printing/dumping the output of 'toString' on a bunch of
>>the DOM nodes in this SVGDocument - you should get things like:
>>batik.dom.svg.SVGOMSVGElement, batik.dom.svg.SVGOMGElement,
>>batik.dom.svg.SVGOMPathElement, ... if instead you get
>>batik.dom.GenericElement then you definately have a namespace/config
> 
> issue.
> 
> Yep, I'm getting SVGOM* elements.

   Well I'm running out of ideas here, it sounds like everything is
Ok up to the transcode.  You might try pulling to code out of
Tomcat and sending the document to the JSVGCanvas just to see it
that works...

   You could also either step through or add annotations to the
rendering processes in the Batik GraphicsNodes (basically add
a println to the 'paint/primitivePaint' methods of the various
classes in 'batik.gvt').

   This really is a new one....



---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org


Re: Preview Problems

Posted by Kevin Lindsey <ke...@kevlindev.com>.
From: "Thomas DeWeese"

> > AFAIK, I'm catching all exceptions and none of them are firing and the
> > transformed document is generated correctly, minus the blank preview.
>
>    Not all errors are communicated using exceptions.  Non-fatal errors are
> sent to the Transcoders ErrorHandler.  By default it writes these to
> System.err - in Tomcat this probably goes to some error log or
something...

It looks like all of the Tomcat log files are clean (no errors messages to
be found).

> Can you provide sample 'transformed' content?

Due to NDA's, I cannot, but the file is quite simple.  Here is a mock-up of
what I'm generating:

<?xml version="1.0" encoding="utf-8"?>
<svg xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns="http://www.w3.org/2000/svg">
    <style type="text/css"><![CDATA[
        // simple styling data here
    ]]></style>
    <script type="text/ecmascript"><![CDATA[
        // a bunch of script here that only builds JS data structures
    ]]></script>
    <g>
        <g id="id1">
            <path class="pathClass" d="M..."/>
            <text class="textClass" ...>...</text>
        </g>
        <g id="id2">
            <path class="pathClass" d="M..."/>
            <text class="textClass" ...>...</text>
        </g>
    </g>
    <!-- many more paths to follow -->
</svg>

Really very simple and the result previews in ASV3 (and Squiggle if I save
it out to a file and view it).

> Also you might try printing/dumping the output of 'toString' on a bunch of
> the DOM nodes in this SVGDocument - you should get things like:
> batik.dom.svg.SVGOMSVGElement, batik.dom.svg.SVGOMGElement,
> batik.dom.svg.SVGOMPathElement, ... if instead you get
> batik.dom.GenericElement then you definately have a namespace/config
issue.

Yep, I'm getting SVGOM* elements.

> > Do you by any chance know of an example showing this that I can look at?
>    Actually the latest version of the transcoders (I think in 1.5 but it
> might have happened shortly there after) will do this for you.  So you
> could look at 'batik.transcoder.SVGAbstractTranscoder.java:206' from
> viewCVS.

Thanks.  I'll have a look.

Kevin
KevLinDev - http://www.kevlindev.com


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org


Re: Preview Problems

Posted by Thomas DeWeese <Th...@Kodak.com>.
Kevin Lindsey wrote:
> From: "Thomas DeWeese"
> 
> 
> 
>>>I'm currently using a Tomcat servlet to transform an uploaded SVG
>>>document.
>>>Once the document is transformed, I generate a PNG preview with Batik
>>>and then insert that image into the transformed document as a base64
>>>encoding image; however, my preview image is always blank.


> 
> 
>     [snip]
> 
> 
>>   The problem almost certainly has to do with the base url of the
>> document.
>>If you transcode from a stream it doesn't have one (unless you provide one
>>to the input stream).  You can also provide an xml:base attribute to set the
>>base to resolve URL's against.

 >         ).createSVGDocument(
 >             "http://localhost:8080/uploadTest/",
 >             new StringReader( some_string_here )
 >         );

    Actually it looks like you are providing a base so scratch this...

>>   Are you sure you aren't getting error messages of any sort?
> 
> 
> AFAIK, I'm catching all exceptions and none of them are firing and the
> transformed document is generated correctly, minus the blank preview.

   Not all errors are communicated using exceptions.  Non-fatal errors are
sent to the Transcoders ErrorHandler.  By default it writes these to
System.err - in Tomcat this probably goes to some error log or something...
You could provide your own ErrorHandler that always threw an exception.

> What's confusing me is that if I build the SVGDocument from the uploaded
> file (which is a string), I get a preview, but when I build from the results
> of the XSLT transform (again a string), I don't.  I generate the SVGDocument
> with the exact same call (except for the string parameter, of course):
> 
>         SVGDocument svgDocument = new SAXSVGDocumentFactory(
>             XMLResourceDescriptor.getXMLParserClassName()

   You could see what happens with a 'null' XSL transformation (still probably
mucks with doc types etc).  Can you provide sample 'transformed' content?
Also you might try printing/dumping the output of 'toString' on a bunch of
the DOM nodes in this SVGDocument - you should get things like:
batik.dom.svg.SVGOMSVGElement, batik.dom.svg.SVGOMGElement,
batik.dom.svg.SVGOMPathElement, ... if instead you get
batik.dom.GenericElement then you definately have a namespace/config issue.


> I'm not doubting your suggestion...I'm just trying to understand what I'm
> doing wrong.
> 
> 
>>>FWIW, I am serializing the transformed and then reparsing to create a
> 
> new
> 
>>>SVG document.  My understanding from the code in Squiggle is that I have
>>>to do this.
>>
>>   Pretty much, what you really need is for Xalan to create the output
>>document using our DOM implementation.  You might find it faster to
>>deep clone the DOM into our implementation rather than serialize and
>>parse.
> 
> Do you by any chance know of an example showing this that I can look at?
> This is my first real foray into Java+XML and embarrassingly, I'm feeling a
> little lost at this point.

   Actually the latest version of the transcoders (I think in 1.5 but it
might have happened shortly there after) will do this for you.  So you
could look at 'batik.transcoder.SVGAbstractTranscoder.java:206' from
viewCVS. You could try it but for now I would stick with the parsing
route.

> 
> Thanks for the help,
> Kevin
> KevLinDev - http://www.kevlindev.com
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
> For additional commands, e-mail: batik-users-help@xml.apache.org
> 
> 




---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org


Re: Preview Problems

Posted by Kevin Lindsey <ke...@kevlindev.com>.
From: "Thomas DeWeese"


> > I'm currently using a Tomcat servlet to transform an uploaded SVG
document.
> > Once the document is transformed, I generate a PNG preview with Batik
and
> > then insert that image into the transformed document as a base64
encoding
> > image; however, my preview image is always blank.

    [snip]

>    The problem almost certainly has to do with the base url of the
document.
> If you transcode from a stream it doesn't have one (unless you provide one
to
> the input stream).  You can also provide an xml:base attribute to set the
> base to resolve URL's against.
>
>    Are you sure you aren't getting error messages of any sort?

AFAIK, I'm catching all exceptions and none of them are firing and the
transformed document is generated correctly, minus the blank preview.
What's confusing me is that if I build the SVGDocument from the uploaded
file (which is a string), I get a preview, but when I build from the results
of the XSLT transform (again a string), I don't.  I generate the SVGDocument
with the exact same call (except for the string parameter, of course):

        SVGDocument svgDocument = new SAXSVGDocumentFactory(
            XMLResourceDescriptor.getXMLParserClassName()
        ).createSVGDocument(
            "http://localhost:8080/uploadTest/",
            new StringReader( some_string_here )
        );

I'm not doubting your suggestion...I'm just trying to understand what I'm
doing wrong.

> > FWIW, I am serializing the transformed and then reparsing to create a
new
> > SVG document.  My understanding from the code in Squiggle is that I have
> > to do this.
>
>    Pretty much, what you really need is for Xalan to create the output
> document using our DOM implementation.  You might find it faster to
> deep clone the DOM into our implementation rather than serialize and
parse.

Do you by any chance know of an example showing this that I can look at?
This is my first real foray into Java+XML and embarrassingly, I'm feeling a
little lost at this point.

Thanks for the help,
Kevin
KevLinDev - http://www.kevlindev.com


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org


Re: Preview Problems

Posted by Thomas DeWeese <Th...@Kodak.com>.
Kevin Lindsey wrote:
> Hi,
> 
> I'm currently using a Tomcat servlet to transform an uploaded SVG document.  
> Once the document is transformed, I generate a PNG preview with Batik and 
> then insert that image into the transformed document as a base64 encoding 
> image; however, my preview image is always blank.
> 
> I've done some searches and see that I should do some sanity tests like making
> sure that the root element is an svg element and that it is in the SVG namespace.  
> All of that checks out OK.  As a test, I have been able to generate the preview 
> of the non-transformed document.  This seems to be specific to SVG documents that 
> are transformed with Xalan.  As another test I wrote the transformed file to a 
> temporary file and then tried to rasterize that file...same result...blank image.
>  It is very strange because I can view the temporary file in Squiggle (and ASV3).
>  It certainly looks like a namespace problem, but everything seems to be fine.

   The problem almost certainly has to do with the base url of the document.
If you transcode from a stream it doesn't have one (unless you provide one to
the input stream).  You can also provide an xml:base attribute to set the
base to resolve URL's against.

   Are you sure you aren't getting error messages of any sort?

> FWIW, I am serializing the transformed and then reparsing to create a new 
> SVG document.  My understanding from the code in Squiggle is that I have 
> to do this.

   Pretty much, what you really need is for Xalan to create the output
document using our DOM implementation.  You might find it faster to
deep clone the DOM into our implementation rather than serialize and parse.

> Let's see I'm running WinXP, Java 1.4.1_04, Tomcat 4.1, Batik 1.5, and Xalan 2.5.
> 
> A snippet from the transform code looks like:
> 
>         // load stylesheet
>         TransformerFactory tFactory = TransformerFactory.newInstance();
>         Transformer transformer = tFactory.newTransformer(
>             new StreamSource(path)
>         );
>         
>         // apply transform
>         StringWriter sw = new StringWriter();
>         transformer.transform(
>             new StreamSource(source),
>             new StreamResult(sw)
>         );
>         sw.flush();
>         sw.close();
>         
>         // build and return SVGDocument
>         return new SAXSVGDocumentFactory(
>             XMLResourceDescriptor.getXMLParserClassName()
>         ).createSVGDocument(
>             "http://localhost:8080/uploadTest/",
>             new StringReader(sw.toString())
>         );
> 
> A snippet from the preview insertion looks like:
> 
>         PNGTranscoder t = new PNGTranscoder();
>         TranscoderInput input = new TranscoderInput(svgDocument);
>         ByteArrayOutputStream ostream = new ByteArrayOutputStream();
>         TranscoderOutput output = new TranscoderOutput(ostream);
>         
>         // setup transcoder hints
>         t.addTranscodingHint(PNGTranscoder.KEY_INDEXED, new Integer(256));
>         t.addTranscodingHint(ImageTranscoder.KEY_BACKGROUND_COLOR, Color.white);
>         t.addTranscodingHint(ImageTranscoder.KEY_WIDTH, new Float(100));
>         t.addTranscodingHint(ImageTranscoder.KEY_HEIGHT, new Float(100));
> 
>         // transcode
>         t.transcode(input, output);
>         
>         // convert result to base64 encoding
>         ostream.close();
>         Base64 b64 = new Base64();
>         String data = b64.encode(ostream.toByteArray());
> 
> Thank you for any assistance with this.  I seem to have run out of things to try.
> 
> Kevin
> KevLinDev - http://www.kevlindev.com
> 




---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org