You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-dev@xmlgraphics.apache.org by Ahmad Morad <mo...@db.informatik.uni-kassel.de> on 2002/03/05 11:15:48 UTC

JSVGComponent.setSVGDocument method

Does anybody have experience with the 
JSVGComponent.setSVGDocument(SVGDocument doc) method? 

I used the same document one time with the setURI() method, and the other
throw producing a dom from the file and calling setSVGDocument(). 
If I serialize the dom I produced, I get the same SVG document. So I guess
there is a little bug with the setSVGDocument() method!!  

--
 /**
     * Sets the SVG document to display.
     */
    public void setSVGDocument(SVGDocument doc) {
        ...

        if (eventsEnabled && svgDocument != null) {
            // fire the unload event
            Event evt = svgDocument.createEvent("SVGEvents");
            evt.initEvent("SVGUnload", false, false);
            ((EventTarget)(svgDocument.getRootElement())).dispatchEvent(evt);
        }

        svgDocument = doc;

        ... 
    }
--

I think the svgDocument must be null at the first call of setSVGDocument(). 
This is however not the case. The line : 

   ((EventTarget)(svgDocument.getRootElement())).dispatchEvent(evt);

will be executed at the first call and  produce an exception like:

--
java.lang.ClassCastException: org.apache.batik.dom.GenericElementNS
        at org.apache.batik.dom.svg.SVGOMDocument.getRootElement(Unknown      
        Source)
       ...
--

Thanks,
Morad.

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


Re: JSVGComponent.setSVGDocument method

Posted by Ahmad Morad <mo...@db.informatik.uni-kassel.de>.
Am Dienstag, 5. März 2002 11:30 schrieben Sie:
> I have experience with it: I wrote it...
> What I can say is that the problem don't come from the component.
> Sorry but I must repeat what I wrote in my previous email: you are creating
> the elements of the DOM in the wrong namespace. Use createElementNS in you
> cloning method instead of createElement and it will work.

Hi Stephane!

Could you please tell how did you produced a SVGDocument from an 
svg source file? 

-- 
First I thought it's very simple and now I get every time new problems.
But I am sure the deepCloneDocumen() method doesn't work correct. Now I use
following  code to solve the problem. 

--


    private static Attr copyAttr (Attr attr, Document doc) 
    {
	Attr newAttr = doc.createAttributeNS(attr.getNamespaceURI(), attr.getName());
	newAttr.setValue(attr.getValue());
	return newAttr;
    }
    
    public static Node cloneNodeRec(Node source, Document targetDoc) 
    {
	Node result = null;
	NamedNodeMap attr;

	switch (source.getNodeType()) {
	case Node.DOCUMENT_NODE:
	    break;
	case Node.ELEMENT_NODE:
	    Element resultE =  targetDoc.createElementNS(source.getNamespaceURI(), 
source.getNodeName());
 	    attr = source.getAttributes();
	    for(int i = 0; i < attr.getLength();i++) {
		resultE.setAttributeNode(copyAttr((Attr) attr.item(i), targetDoc));
	    }
	    NodeList children = source.getChildNodes();	    
	    if(children != null) {
		for (int i = 0; i < children.getLength(); i++) {
		    resultE.appendChild(cloneNodeRec(children.item(i), targetDoc));
		}
	    }
	    result = (Node) resultE;
	    break;
	case Node.TEXT_NODE:
	    Node txtNode = targetDoc.createTextNode(source.getNodeValue());
	    result = txtNode;
	    break;
	case Node.PROCESSING_INSTRUCTION_NODE:
	    ProcessingInstruction pi = (ProcessingInstruction) source;
	    ProcessingInstruction resultPI = targetDoc.createProcessingInstruction(
					               pi.getTarget(), 
						       pi.getData());
	    result = (Node) resultPI;
	    break;
	default:
	    Element resultD = targetDoc.createElementNS(source.getNamespaceURI(), 
source.getNodeName());
	    attr = source.getAttributes();
	    for(int i = 0; i < attr.getLength();i++) {
		resultD.setAttributeNode(copyAttr((Attr) attr.item(i), targetDoc));
	    }
	    result = (Node) resultD;
	}
	return result;
    }

    /**
     * Deep clones a document using the given DOM implementation.
     */
    public static Document deepCloneDocument(Document doc, DOMImplementation 
impl) {
	
	Element root = doc.getDocumentElement();
	Document result = impl.createDocument(root.getNamespaceURI(),
						     root.getNodeName(),
						     null);
	Element rroot = result.getDocumentElement();	
	if (root.hasAttributes()) {
	    NamedNodeMap attr = root.getAttributes();
	    int len = attr.getLength();
	    for (int i = 0; i < len; i++) {
		rroot.setAttributeNode(copyAttr((Attr) attr.item(i), result));
	    }
	}
	NodeList children = root.getChildNodes();	    
	if(children != null) {
	    for (int i = 0; i < children.getLength(); i++) {
		rroot.appendChild(cloneNodeRec(children.item(i), result));
	    }
	}
	return result;
    }

    public static SVGDocument domToSVGDom (Document doc) 
    {
	SVGDocument svgDoc;
	SVGDOMImplementation impl = (SVGDOMImplementation) 
	    SVGDOMImplementation.getDOMImplementation();
	svgDoc = (SVGDocument) deepCloneDocument(doc, impl);
	return svgDoc;
    }
--

It produce a correct SVGDocument object (I can print out in a file, and then
 visualize using JSVGCanvas.setURI(fiename). Now I got the problem:
      java.lang.ClassCastException: org.apache.batik.dom.GenericElementNS
        at org.apache.batik.dom.svg.SVGOMDocument.getRootElement(Unknown      
         Source)
        at org.apache.batik.swing.svg.JSVGComponent.setSVGDocument(Unknown    
        Source)
        at tcldbc.TclDBSVGViewer.viewTable(TclDBSVGViewer.java:109)
--

I have no Idea what "getRootElement" is, and what the meaning of "Unknown 
Source"?  which source?

--
Sorry for disturbing you so much with my problems!
Morad.


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


RE: JSVGComponent.setSVGDocument method

Posted by Stephane Hillion <sh...@ilog.fr>.
I have experience with it: I wrote it...
What I can say is that the problem don't come from the component.
Sorry but I must repeat what I wrote in my previous email: you are creating
the elements of the DOM in the wrong namespace. Use createElementNS in you
cloning method instead of createElement and it will work.

> -----Original Message-----
> From: Ahmad Morad [mailto:morad@db.informatik.uni-kassel.de]
> Sent: mardi 5 mars 2002 11:16
> To: batik-dev@xml.apache.org
> Subject: JSVGComponent.setSVGDocument method
>
>
> Does anybody have experience with the
> JSVGComponent.setSVGDocument(SVGDocument doc) method?
>
> I used the same document one time with the setURI() method, and the other
> throw producing a dom from the file and calling setSVGDocument().
> If I serialize the dom I produced, I get the same SVG document. So I guess
> there is a little bug with the setSVGDocument() method!!
>
> --
>  /**
>      * Sets the SVG document to display.
>      */
>     public void setSVGDocument(SVGDocument doc) {
>         ...
>
>         if (eventsEnabled && svgDocument != null) {
>             // fire the unload event
>             Event evt = svgDocument.createEvent("SVGEvents");
>             evt.initEvent("SVGUnload", false, false);
>
> ((EventTarget)(svgDocument.getRootElement())).dispatchEvent(evt);
>         }
>
>         svgDocument = doc;
>
>         ...
>     }
> --
>
> I think the svgDocument must be null at the first call of
> setSVGDocument().
> This is however not the case. The line :
>
>    ((EventTarget)(svgDocument.getRootElement())).dispatchEvent(evt);
>
> will be executed at the first call and  produce an exception like:
>
> --
> java.lang.ClassCastException: org.apache.batik.dom.GenericElementNS
>         at
> org.apache.batik.dom.svg.SVGOMDocument.getRootElement(Unknown
>         Source)
>        ...
> --
>
> Thanks,
> Morad.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: batik-dev-help@xml.apache.org
>
>


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