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/01 11:20:31 UTC
SVGDocument
Hi,
In my Applikation I read the visualization from a socket. I read the
XML-Content from the Inputstream and produce a dom Document. If this is an
HTML I use the JEditorPane swing widget to render it. If this is SVG
I want to use the JSVGCanvas widget. This became as first argument a
SVGDocument object.
In the batik API, the SVGDocument is a (dom) Document!
So the type cast
Document doc;
SVGDocument svgDoc = (SVGDocument) doc;
must be correct! However I got type a cast Ecception. It sounds
to be a small a problem, but I don't really find a way how to
create a SVGDocument from a (dom) Document. Something like:
private SVGDocument doc2svgDoc (Document doc) {
SVGDocument svgDoc;
...
return svgDoc;
}
---------------------------------------------------------------------
To unsubscribe, e-mail: batik-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-dev-help@xml.apache.org
Re: SVGDocument
Posted by Ahmad Morad <mo...@db.informatik.uni-kassel.de>.
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
RE: SVGDocument
Posted by Stephane Hillion <sh...@ilog.fr>.
> From: Ahmad Morad [mailto:morad@db.informatik.uni-kassel.de]
> Sent: vendredi 1 mars 2002 11:21
> To: batik-dev@xml.apache.org
> Subject: SVGDocument
>
>
> Hi,
>
> In my Applikation I read the visualization from a socket. I read the
> XML-Content from the Inputstream and produce a dom Document. If this is an
> HTML I use the JEditorPane swing widget to render it. If this is SVG
> I want to use the JSVGCanvas widget. This became as first argument a
> SVGDocument object.
>
>
> In the batik API, the SVGDocument is a (dom) Document!
> So the type cast
>
> Document doc;
> SVGDocument svgDoc = (SVGDocument) doc;
>
> must be correct! However I got type a cast Ecception. It sounds
> to be a small a problem, but I don't really find a way how to
> create a SVGDocument from a (dom) Document. Something like:
>
> 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 ;-)
---------------------------------------------------------------------
To unsubscribe, e-mail: batik-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-dev-help@xml.apache.org