xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Peter Rice <pe...@boppie.com>
Subject Displaying svg Element
Date Wed, 12 Dec 2001 15:08:33 GMT
I am building an xhtml/mathml editor/viewer and I want to allow embedded 
svg tags. The problem I am trying to solve is the proper use of batik to 
display the svg image. I already have the Document loaded into memory, and 
I am trying to grab the svg Element and use GVTBuilder.build() to get a 
GraphicsNode object, then ask it to paint the graphic. I am getting the 
following exception.

Is there an easy fix, or am I, perhaps, on the wrong track altogether?

/*java.lang.ClassCastException: org.apache.xerces.dom.DeferredDocumentImpl
         at 
org.apache.batik.bridge.CSSUtilities.getViewCSS(CSSUtilities.java:87)
         at 
org.apache.batik.bridge.CSSUtilities.getComputedStyle(CSSUtilities.java:96)
         at 
org.apache.batik.bridge.CSSUtilities.convertDisplay(CSSUtilities.java:358)
         at org.apache.batik.bridge.GVTBuilder.build(GVTBuilder.java:101)
         at temp.SVGTagTest.displaySvg(SVGTagTest.java:76)
         at temp.SVGTagTest.<init>(SVGTagTest.java:52)
         at temp.SVGTagTest.main(SVGTagTest.java:43) */

package temp;

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.*;
import org.w3c.dom.*;
import org.w3c.dom.svg.SVGAElement;
import org.apache.xerces.parsers.DOMParser;
import org.xml.sax.InputSource;

import org.apache.batik.gvt.GraphicsNode;
import org.apache.batik.gvt.GraphicsNodeRenderContext;
import org.apache.batik.bridge.GVTBuilder;
import org.apache.batik.bridge.UserAgent;
import org.apache.batik.bridge.BridgeContext;
import org.apache.batik.gvt.event.EventDispatcher;
import org.apache.batik.gvt.filter.ConcreteGraphicsNodeRableFactory;
import org.apache.batik.bridge.UserAgent;
import org.apache.batik.bridge.BridgeExtension;
import java.awt.geom.AffineTransform;
import java.awt.geom.Dimension2D;
import java.awt.image.renderable.RenderContext;

public class SVGTagTest extends JFrame{
	
	private Document doc;
	private Component canvas;
	private Element svgElem;
	private GraphicsNode graphicsNode;
	
	public static void main(String[] args) throws Exception {
		SVGTagTest test = new SVGTagTest();
	}
	
	public SVGTagTest() throws Exception {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(300,300);
		setVisible(true);
		canvas = getContentPane();
		read(new FileReader("/Projects/Editor/temp/SVGExample.xml"));
		displaySvg();
	}
	
	public void read(Reader is) throws Exception{
		InputSource saxIs = new InputSource(is);
		DOMParser parser = new DOMParser();
		parser.setIncludeIgnorableWhitespace(false);
		parser.setFeature("http://apache.org/xml/features/dom/create-entity-ref-no 
parser.setFeature("http://apache.org/xml/features/dom/create-entity-ref-node 
s",false);
		parser.parse(saxIs);
		doc = parser.getDocument();
	}
	
	public void displaySvg(){
		svgElem = (Element)doc.getElementsByTagName("svg").item(0);
		if (svgElem == null) return;
		GVTBuilder b = new GVTBuilder();
		Graphics2D g = (Graphics2D)canvas.getGraphics();
		GraphicsNodeRenderContext gnrc = new GraphicsNodeRenderContext(
			g.getTransform(),
			g.getClip(),
			new RenderingHints(null), null, null,
			new ConcreteGraphicsNodeRableFactory());
		UserAgent ua = new NLMSVGUserAgent(canvas);
		BridgeContext ctx = new BridgeContext(ua, gnrc);
		graphicsNode = b.build(ctx, svgElem); // exception here!
		graphicsNode.paint(g, gnrc);
	}
}

class NLMSVGUserAgent implements UserAgent {
	
	private Component component;
	private AffineTransform transform;

	public NLMSVGUserAgent(Component comp){
		component = comp;
		transform = new AffineTransform();
  	}

	public Dimension2D getViewportSize() {
		return component.getSize();
	}

	public EventDispatcher getEventDispatcher() {
		return null;
	}

	public void displayError(String message) {
		System.out.println(message);
	}

	public void displayError(Exception ex) {
		System.out.println(ex.getMessage());
	}

	public void displayMessage(String message) {
		System.out.println(message);
	}

	public float getPixelToMM() {
		return 0.264583333333333333333f; // 96 dpi
	}

	public String getLanguages() {
		return "en";
	}

	public String getUserStyleSheetURI() {
		return null;
	}

	public void openLink(SVGAElement elt) {
	}

	protected void fireLinkActivatedEvent(SVGAElement elt, String href) {
	}

	public void setSVGCursor(Cursor cursor) {
		//JSVGComponent.this.setCursor(cursor);
	}

	public String getXMLParserClassName() {
		return "org.apache.crimson.parser.XMLReaderImpl";
	}

	public AffineTransform getTransform() {
		return transform;
	}

	public Point getClientAreaLocationOnScreen() {
		return component.getLocationOnScreen();
	}

	public boolean hasFeature(String s) {
		return false;
	}

	protected Map extensions = new HashMap();

	public boolean supportExtension(String s) {
		return extensions.containsKey(s);
	}

	public void registerExtension(BridgeExtension ext) {
		Iterator i = ext.getImplementedExtensions();
		while (i.hasNext())
			extensions.put(i.next(), ext);
	}
}


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


Mime
View raw message