xmlgraphics-batik-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ahmad Morad <mo...@db.informatik.uni-kassel.de>
Subject Re: SVGDocument
Date Mon, 04 Mar 2002 16:14:36 GMT
Hi 
> >  private SVGDocument doc2svgDoc (Document doc) {
> >        SVGDocument svgDoc;
>
>          svgDoc =
> (SVGDocument)org.apache.batik.dom.util.DOMUtilities.deepCloneDocument(doc,
> org.apache.batik.dom.svg.SVGDOMImplementation.getDOMImplementation());
>
> >        return svgDoc;
> >   }
>
> I have not try to compile, but it should work ;-)
>
Hi, 

thanks for help. 

1. the "deepCloneDocument" doesn't work correct. It doesn't 
   create attributes for the child nodes. This could be a bug in the          
   "Node.cloneNode()" or "Document.importNode()" methods in the Batik         
    implementation of Node, Document. ???

2. There is no documentation for the DOMUtilities class in the batik Javadoc  
   API? 

3. I wrote the following method which work well for my purpose. 

---
    public static Node cloneNodeRec(Node source, Document targetDoc) 
    {
	String nodeName;
	int sourceType  = source.getNodeType();
	Node result = null;
	NamedNodeMap attr;
	NodeList children;

	switch (sourceType) {
	case Node.DOCUMENT_NODE:
	    break;
	case Node.ELEMENT_NODE:
	    nodeName = source.getNodeName();
	    Element resultE =  targetDoc.createElement(nodeName);
	    NamedNodeMap attrs = source.getAttributes();
	    for(int i = 0; i < attrs.getLength();i++) {
		Node current = attrs.item(i);
		resultE.setAttributeNode((Attr)targetDoc.importNode(attrs.item(i), true));
	    }
	    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;
	default:
	    nodeName = source.getNodeName();
	    Element resultD = targetDoc.createElement(nodeName);
	    NamedNodeMap attrs2 = source.getAttributes();
	    for(int i = 0; i < attrs2.getLength();i++) {
		Node current = attrs2.item(i);
		resultD.setAttributeNode((Attr)targetDoc.importNode(attrs2.item(i), true));
	    }
	    result = (Node) resultD;
	    // no children!!
	}
	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((Attr)result.importNode(attr.item(i), true));
	    }
	}
	NodeList children = root.getChildNodes();	    
	if(children != null) {
	    for (int i = 0; i < children.getLength(); i++) {
		rroot.appendChild(cloneNodeRec(children.item(i), result));
	    }
	}
	return result;
    }
---


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


Mime
View raw message