On 2001.10.30 16:41 Stephane Hillion wrote:
> 
> It is difficult to help you without some examples or pieces of code.
> But what you must have in mind is that the xmlns attribute and prefix is
> used 
> by XML parsers to bind elements to namespaces, and once you are in the
> DOM 
> world it is not used anymore.
> To make the importation work, you must be sure that all the elements of
> the 
> SVG fragment were in the SVG namespace at creation time.

I have attached some code and an example svg file. This should indicate
what I am trying to do.
Note the xmlns attribute is repeated on some of the svg elements.

So how do I make sure that all the elements are in the svg namespace at
creation time, the example (I would have thought) does this but the
namespace uri of the elements is still null. (I am using xalan 2.2d11
xerces 1.4.3 and current cvs batik)

>From what you said it sounds like it is not possible and batik won't let me
do it.
I could not repeat the npe, it must be a combination of a number of
factors.


<?xml version="1.0"?>
<foo>
  <foo>
<svg:svg height="20" width="20" xmlns:svg="http://www.w3.org/2000/svg";>
  <svg:g style="fill:red; stroke:#000000">
     <svg:rect height="15" width="15" x="0" y="0"/>
     <svg:rect height="15" width="15" x="5" y="5"/>
  </svg:g>
</svg:svg>
  </foo>
  <foo>
<svg height="20" width="20" xmlns="http://www.w3.org/2000/svg";>
  <g style="fill:red; stroke:#000000">
     <rect height="15" width="15" x="0" y="0"/>
     <rect height="15" width="15" x="5" y="5"/>
  </g>
</svg>
  </foo>
</foo>
import java.io.*;
import java.util.*;

import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.apache.batik.dom.util.DOMUtilities;

import org.w3c.dom.*;
import org.w3c.dom.svg.SVGSVGElement;
import org.w3c.dom.svg.SVGDocument;

public class DOMImport {
    static String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;

    public static void main(String[] args) {
        Document doc = null;
        try {
            InputStream is = new FileInputStream("foo.xml");
            doc = javax.xml.parsers.DocumentBuilderFactory.newInstance().
                  newDocumentBuilder().parse(is);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Element root = null;
        root = doc.getDocumentElement();

        DOMImplementation impl =
          SVGDOMImplementation.getDOMImplementation();
        Document svgDocument = impl.createDocument(svgNS, "svg", null);
        Element svgRoot = svgDocument.getDocumentElement();

        System.out.println("trying with DOMUtilities.importNode()");
        try {
            Document svgDoc1 = getSVGDocument1(root, "xmlns");
            SVGSVGElement svg = ((SVGDocument) svgDoc1).getRootElement();
            Node newsvg = svgDocument.importNode(svg, true);
            svgRoot.appendChild(newsvg);
        } catch (Throwable e) {
            e.printStackTrace();
        }

        System.out.println("trying with DOMUtilities.importNode()");
        try {
            Document svgDoc1 = getSVGDocument1(root, "xmlns:svg");
            SVGSVGElement svg = ((SVGDocument) svgDoc1).getRootElement();
            Node newsvg = svgDocument.importNode(svg, true);
            svgRoot.appendChild(newsvg);
        } catch (Throwable e) {
            e.printStackTrace();
        }

        System.out.println("trying with modified importNode()");
        try {
            Document svgDoc2 = getSVGDocument2(root, "xmlns");
            SVGSVGElement svg = ((SVGDocument) svgDoc2).getRootElement();
            Node newsvg = svgDocument.importNode(svg, true);
            svgRoot.appendChild(newsvg);
        } catch (Throwable e) {
            e.printStackTrace();
        }

        System.out.println("trying with modified importNode()");
        try {
            Document svgDoc2 = getSVGDocument2(root, "xmlns:svg");
            SVGSVGElement svg = ((SVGDocument) svgDoc2).getRootElement();
            Node newsvg = svgDocument.importNode(svg, true);
            svgRoot.appendChild(newsvg);
        } catch (Throwable e) {
            e.printStackTrace();
        }

        // this is an attempt to repeat an npe but doesn't do it.
        try {
            Document svgDoc2 = getSVGDocument2(root, "xmlns");
            SVGSVGElement svg = ((SVGDocument) svgDoc2).getRootElement();
            Element view = svgDocument.createElementNS(svgNS, "svg");
            Node newsvg = svgDocument.importNode(svg, true);
            view.appendChild(newsvg);
            svgRoot.appendChild(view);
        } catch (Throwable e) {
            e.printStackTrace();
        }

        try {
            PrintWriter writer = new PrintWriter(System.out);
            DOMUtilities.writeDocument(svgDocument, writer);
            writer.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.exit(0);
    }

    static Document getSVGDocument1(Element ele, String attr) {
        Document doc;

        NodeList childs = ele.getChildNodes();
        for (int i = 0; i < childs.getLength(); i++) {
            Node obj = childs.item(i);
            if (obj instanceof Element) {
                //System.out.println(obj.getNodeName());
                Element rootEle = (Element) obj;
                String space = rootEle.getAttribute(attr);
                if (svgNS.equals(space)) {
                    try {
                        doc = javax.xml.parsers.DocumentBuilderFactory.newInstance().
                              newDocumentBuilder().newDocument();
                        Node node = doc.importNode(obj, true);
                        doc.appendChild(node);
                        DOMImplementation impl =
                          SVGDOMImplementation.getDOMImplementation();
                        // due to namespace problem attributes are not cloned
                        doc = DOMUtilities.deepCloneDocument(doc, impl);

                        return doc;
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    doc = getSVGDocument1(rootEle, attr);
                    if (doc != null) {
                        return doc;
                    }
                }
            }
        }
        return null;
    }

    static Document getSVGDocument2(Element ele, String attr) {
        Document doc;
        String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;

        NodeList childs = ele.getChildNodes();
        for (int i = 0; i < childs.getLength(); i++) {
            Node obj = childs.item(i);
            if (obj instanceof Element) {
                //System.out.println(obj.getNodeName());
                Element rootEle = (Element) obj;
                String space = rootEle.getAttribute(attr);
                if (svgNS.equals(space)) {
                    try {
                        doc = javax.xml.parsers.DocumentBuilderFactory.newInstance().
                              newDocumentBuilder().newDocument();
                        Node node = doc.importNode(obj, true);
                        doc.appendChild(node);
                        DOMImplementation impl =
                          SVGDOMImplementation.getDOMImplementation();
                        // due to namespace problem attributes are not cloned
                        doc = deepCloneDocument(doc, impl);

                        return doc;
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    doc = getSVGDocument2(rootEle, attr);
                    if (doc != null) {
                        return doc;
                    }
                }
            }
        }
        return null;
    }

    public static Document deepCloneDocument(Document doc,
            DOMImplementation impl) {
        Element root = doc.getDocumentElement();
        Document result =
          impl.createDocument(svgNS, root.getNodeName(), null);
        Element rroot = result.getDocumentElement();
        boolean before = true;
        for (Node n = doc.getFirstChild(); n != null;
                n = n.getNextSibling()) {
            if (n == root) {
                before = false;
                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));
                    }
                }
                for (Node c = root.getFirstChild(); c != null;
                        c = c.getNextSibling()) {
                    rroot.appendChild(result.importNode(c, true));
                }
            } else {
                if (n.getNodeType() != Node.DOCUMENT_TYPE_NODE) {
                    if (before) {
                        result.insertBefore(result.importNode(n, true),
                                            rroot);
                    } else {
                        result.appendChild(result.importNode(n, true));
                    }
                }
            }
        }
        return result;
    }

}
trying with DOMUtilities.importNode()
java.lang.ClassCastException: org.apache.batik.dom.GenericElement
        at 
org.apache.batik.dom.svg.SVGOMDocument.getRootElement(SVGOMDocument.java:231)
        at DOMImport.main(DOMImport.java:34)
trying with DOMUtilities.importNode()
java.lang.ClassCastException: org.apache.batik.dom.GenericElement
        at 
org.apache.batik.dom.svg.SVGOMDocument.getRootElement(SVGOMDocument.java:231)
        at DOMImport.main(DOMImport.java:44)
trying with modified importNode()
trying with modified importNode()
<svg contentScriptType="text/ecmascript" zoomAndPan="magnify" 
contentStyleType="text/css" preserveAspectRatio="xMidYMid meet" 
xmlns="http://www.w3.org/2000/svg"; version="1.0"><svg contentScriptType="" width="20" 
zoomAndPan="" xmlns="http://www.w3.org/2000/svg"; contentStyleType="" height="20" 
preserveAspectRatio="" xmlns="" version="">
  <g>
     <rect/>
     <rect/>
  </g>
</svg><svg:svg contentScriptType="" width="20" zoomAndPan="" 
xmlns:svg="http://www.w3.org/2000/svg"; contentStyleType="" height="20" 
preserveAspectRatio="" xmlns="" version="">
  <svg:g>
     <svg:rect/>
     <svg:rect/>
  </svg:g>
</svg:svg><svg contentScriptType="text/ecmascript" zoomAndPan="magnify" 
contentStyleType="text/css" preserveAspectRatio="xMidYMid meet" 
xmlns="http://www.w3.org/2000/svg"; version="1.0"><svg contentScriptType="" width="20" 
zoomAndPan="" xmlns="http://www.w3.org/2000/svg"; contentStyleType="" height="20" 
preserveAspectRatio="" xmlns="" version="">
  <g>
     <rect/>
     <rect/>
  </g>
</svg></svg></svg>

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to