Producing SVG in a Java Servlet - Example!

Hi,

the last days I had some questions concerning how to generate a valid
SVG-File within a Java Servlet.
I got some very good advice. Thanks to everybody gaving me hints.

For everybody interested in this topic, I will post an Example that
generates SVG-Code using the SVG-DOM.
It's mostly the solution from Dean Jackson using the SVG-DOM Implementation
of the SVG Toolkit.

There's just a little trick to cast the ServletOutputStream to an
OutpustStream in order to work with the XML-Writer Object.

That the SVG-Code is shown correctly in the browser, it's necessary to set
both mime types: 
*	in the Server Properties, to serve image/svg-xml and in the
JavaServlet
*			and within the ServletResponse
(res.setContentType("image/svg-xml");

Bernd Grolig

Here is the Code:

import org.csiro.svg.parser.*;
import org.csiro.svg.dom.*;
import org.w3c.dom.svg.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
	
// this is a sample file that shows how to create an svg document and
// write it to a file
public class CreateSVG extends HttpServlet {



	public void doGet (HttpServletRequest req, HttpServletResponse res)
		throws ServletException, IOException {

		res.setContentType("image/svg-xml");
		ServletOutputStream out = res.getOutputStream();
    
    try {
      SVGDocument svgDoc = new SVGDocumentImpl(true);  // the "true"
indicates that this is to be a stylable svg doc

      // create root <svg> element and add it to doc
      SVGSVGElement root =  (SVGSVGElement)svgDoc.createElement("svg");
      svgDoc.appendChild(root);

      // set width and height of <svg> element
      SVGLength width = root.createSVGLength();
      width.setValueAsString("15cm");
      SVGLength height = root.createSVGLength();
      height.setValue(600);
      root.setWidth(width);
      root.setHeight(height);

      // add a circle
      SVGCircleElement circle =
(SVGCircleElement)svgDoc.createElement("circle");
      SVGLength cx = root.createSVGLength();
      cx.setValue(100);
      circle.setCx(cx);
      SVGLength cy = root.createSVGLength();
      cy.setValue(100);
      circle.setCy(cy);
      circle.setAttribute("r", "50");
      circle.setAttribute("style", "fill:red; stroke:black;
stroke-width:5");
      root.appendChild(circle);

      // add a rectangle
      SVGRectElement rect = (SVGRectElement)svgDoc.createElement("rect");
      SVGLength rectX = root.createSVGLength();
      rectX.setValueAsString("200px");
      rect.setX(rectX);
      SVGLength rectY = root.createSVGLength();
      rectY.setValueAsString("250px");
      rect.setY(rectY);
      rect.setAttribute("width", "200");
      rect.setAttribute("height", "150");
      rect.setAttribute("style", "fill:none; stroke:blue; stroke-width:10");
      root.appendChild(rect);

      // write the svg doc to a file
      // FileOutputStream out = new FileOutputStream(new File(args[0]));
      XmlWriter writer = new XmlWriter((OutputStream)out);

      // need to pass in the doctype declaration so that it can be put at
the top of the xml file
      writer.print(svgDoc, "<!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 20000303
Stylable//EN' 'http://www.w3.org/TR/2000/03/WD
SVG-20000303/DTD/svg-20000303-stylable.dtd'>");
      out.close();

    } catch (IOException e) {
      System.out.println("IOException: " + e.getMessage());
      System.exit(1);
    }

  }
}

Received on Thursday, 8 June 2000 08:55:56 UTC