Phillip,

I have attached a modified version of your code that does not use
a DocumentFragment and does not use CSS styling. It should do what you
want.

Your example actually showed two issues in our DOM implementations and
we are going to fix them shortly. It seems that replacing an element
with a DocumentFragment gets us in an infinite loop and that we got
a regression on removeAttribute (used when you do SVG streaming with
useCss=true).

Thanks,
Vincent.

Phillip Larsen wrote:
> 
> Hi, I am creating a web charting application using Batik.
> 
> I create a Document by first creating a DOMImplementation and
> then using this DOMImplementation to create the Document.
> 
> The Document is used to create a SVGGeneratorContext object and then
> the SVGGeneratorContext is used to create the SVGGraphics2D object.
> 
> I then use this SVGGraphics2D object as the graphical context in the
> charting component. I am assuming that the Document I created is used
> to hold the DOM tree created when performing Java2D commands with the
> SVGGraphics2D object as graphical context.
> 
> The charting component contains Java2D commands to create a simple line
> chart where the lines for each series of data are connected with circles,
> i.e. each value in the line chart is marked by a circle.
> 
> I want to be able to perform drill down on this chart by clicking on the
> circles to retrieve more information. Thus I need to be able to make the
> circles in the chart clickable. To do so I think I need to go through
> the DOM tree for the chart and replace each circle element with an X-link
> element where the cirle is a child element of the X-link element.
> 
> Here is the code I am using to perform what I have described above :
> 
> public static void writeChartAsSVG(OutputStream out, JFreeChart chart, int width, 
>int height) throws
> IOException
>     {
>         SVGGeneratorContext ctx;
>         SVGGraphics2D       svgGenerator;
>         XmlWriter           xmlwriter;
>         DOMImplementation   domImpl;
>         Document            document;
>         DocumentFragment    docFrag;
>         NodeList            circleNodes;
>         Node                dummyNode;
>         Node                parentNode;
>         Element             linkElement;
>         Element             root;
>         Element             circle;
>         int                 numCircleNodes;
> 
>         // Get a DOMImplementation
>       domImpl = GenericDOMImplementation.getDOMImplementation();
> 
>         // Create an instance of org.w3c.dom.Document
>       document = domImpl.createDocument(null, "svg", null);
> 
>         ctx = SVGGeneratorContext.createDefault(document);
>         svgGenerator = new SVGGraphics2D(ctx,false);
> 
>         chart.draw(svgGenerator, new Rectangle2D.Double(0, 0, width, height));
> 
>         docFrag     = document.createDocumentFragment();
>         linkElement = (Element) 
>docFrag.appendChild(document.createElement(SVGSyntax.SVG_A_TAG));
>       
>linkElement.setAttributeNS(SVGSyntax.SVG_NAMESPACE_URI,SVGSyntax.ATTR_XLINK_HREF,"http://www.vg.no";);
> 
>         root = svgGenerator.getRoot();
> 
>         circle = 
>document.createElementNS(SVGSyntax.SVG_NAMESPACE_URI,SVGSyntax.SVG_RECT_TAG);
>         
>circle.setAttributeNS(SVGSyntax.SVG_NAMESPACE_URI,SVGSyntax.SVG_FILL_ATTRIBUTE,"red");
>         root.appendChild(circle);
> 
>         circleNodes = root.getElementsByTagName(SVGSyntax.SVG_CIRCLE_TAG);
>         numCircleNodes = circleNodes.getLength();
>       for(int nodeCounter=0;nodeCounter<numCircleNodes;nodeCounter++)
>       {
>        dummyNode = circleNodes.item(nodeCounter).cloneNode(true);
>        linkElement.appendChild(dummyNode);
> 
>        parentNode = circleNodes.item(nodeCounter).getParentNode();
>        parentNode.replaceChild(docFrag,circleNodes.item(nodeCounter));
> 
>        docFrag = document.createDocumentFragment();
>          linkElement = (Element) 
>docFrag.appendChild(document.createElement(SVGSyntax.SVG_A_TAG));
>        
>linkElement.setAttributeNS(SVGSyntax.SVG_NAMESPACE_URI,SVGSyntax.ATTR_XLINK_HREF,"http://www.vg.no";);
>       }
> 
>         // Finally, stream out SVG to the standard output using UTF-8
>         // character to byte encoding
>         boolean useCSS = true;
>         Writer svgOutputWriter = new OutputStreamWriter(out, "UTF-8");
>         svgGenerator.stream(root,svgOutputWriter,useCSS);
>     }
> 
> When I try to stream out using the svgGenerator.stream method
> all I receive is the SVG document without any changes made.
> It seems as though I am not able to access the DOM tree and
> performs the replacements. Help on how to access the DOM tree created
> by the SVGGenerator and manipulate the nodes of the tree
> before streaming them to the output would be greatly appreciated.
> 
> Regards
> Phillip Larsen
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
/*****************************************************************************
 * Copyright (C) The Apache Software Foundation. All rights reserved.        *
 * ------------------------------------------------------------------------- *
 * This software is published under the terms of the Apache Software License *
 * version 1.1, a copy of which has been included with this distribution in  *
 * the LICENSE file.                                                         *
 *****************************************************************************/

package org.apache.batik.svggen;

import java.awt.*;
import java.awt.geom.*;

import java.io.*;

import org.apache.batik.util.SVGConstants;


import org.apache.batik.svggen.*;
import org.apache.batik.dom.*;
import org.apache.batik.dom.svg.*;
import org.w3c.dom.*;

public class DOMManipulation {
    public static class CirclePainter implements Painter {
        public void paint(Graphics2D g){
            g.translate(30,30);
            g.setPaint(Color.blue);
            g.draw(new Ellipse2D.Float(0,0,60,60));
            g.setPaint(Color.red);
            g.fill(new Ellipse2D.Float(100,100,60,60));
        }
    }
    
    static Painter painter = new CirclePainter();

    public static void writeAsSVG() throws
        IOException {
        SVGGeneratorContext ctx;
        SVGGraphics2D       svgGenerator;
        XmlWriter           xmlwriter;
        DOMImplementation   domImpl;
        Document            document;
        DocumentFragment    docFrag;
        NodeList            circleNodes;
        Node                dummyNode;
        Node                parentNode;
        Element             linkElement;
        Element             root;
        Element             circle;
        int                 numCircleNodes;
        
        // Get a DOMImplementation
        domImpl = SVGDOMImplementation.getDOMImplementation();

        // Create an instance of org.w3c.dom.Document
        document = domImpl.createDocument(null, "svg", null);
        
        ctx = SVGGeneratorContext.createDefault(document);
        svgGenerator = new SVGGraphics2D(ctx,false);
        svgGenerator.setSVGCanvasSize(new Dimension(400,400));

        painter.paint(svgGenerator);
        
        // docFrag     = document.createDocumentFragment();
        linkElement = document.createElement(SVGSyntax.SVG_A_TAG);
        
linkElement.setAttributeNS(SVGSyntax.SVG_NAMESPACE_URI,SVGSyntax.ATTR_XLINK_HREF,"http://www.vg.no";);
        
        root = svgGenerator.getRoot();
        
        circle = 
document.createElementNS(SVGSyntax.SVG_NAMESPACE_URI,SVGSyntax.SVG_RECT_TAG);
        
circle.setAttributeNS(SVGSyntax.SVG_NAMESPACE_URI,SVGSyntax.SVG_FILL_ATTRIBUTE,"red");
        root.appendChild(circle);
        
        
        circleNodes = root.getElementsByTagName(SVGSyntax.SVG_CIRCLE_TAG);
        numCircleNodes = circleNodes.getLength();
        for(int nodeCounter=0;nodeCounter<numCircleNodes;nodeCounter++) {
            dummyNode = circleNodes.item(nodeCounter).cloneNode(true);
            linkElement.appendChild(dummyNode);
                
            parentNode = circleNodes.item(nodeCounter).getParentNode();
            parentNode.replaceChild(linkElement,circleNodes.item(nodeCounter));

            docFrag = document.createDocumentFragment();
            linkElement = document.createElement(SVGSyntax.SVG_A_TAG);
            
linkElement.setAttributeNS(SVGSyntax.SVG_NAMESPACE_URI,SVGSyntax.ATTR_XLINK_HREF,"http://www.vg.no";);
        }

        // Finally, stream out SVG to the standard output using UTF-8
        // character to byte encoding
        boolean useCSS = true;
        Writer svgOutputWriter = new OutputStreamWriter(System.out, "UTF-8");
        svgGenerator.stream(root,svgOutputWriter,useCSS);
        svgOutputWriter.flush();
    }

    public static void main(String args[]) throws Exception {
        writeAsSVG();
        System.exit(0);
    }
}




---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to